如何在运行时获取构建UUID和映像基址 [英] How to get the build UUID in runtime and the image base address

查看:119
本文介绍了如何在运行时获取构建UUID和映像基址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否需要获取构建UUID,您可以在dSYM生成的文件和iOS中的图像基地址中查看。

Is there away to get the build UUID, the one that you can check in the dSYM generated file and the image base address in iOS.

不太好它涉及低级别的东西,任何人都可以启发?

Not so good when it comes to low level stuff, anyone can enlighten?

推荐答案

这是一个类似于Kerni的答案的解决方案,但适用于任何平台(iOS设备+模拟器和OS X)和任何架构(32位+ 64位)。它还返回 NSUUID 而不是 NSString

Here is a solution similar to Kerni's answer but which works for any platform (iOS device + simulator and OS X) and any architecture (32-bit + 64-bit). It also returns a NSUUID instead of a NSString.

#import <mach-o/dyld.h>
#import <mach-o/loader.h>

static NSUUID *ExecutableUUID(void)
{
    const struct mach_header *executableHeader = NULL;
    for (uint32_t i = 0; i < _dyld_image_count(); i++)
    {
        const struct mach_header *header = _dyld_get_image_header(i);
        if (header->filetype == MH_EXECUTE)
        {
            executableHeader = header;
            break;
        }
    }

    if (!executableHeader)
        return nil;

    BOOL is64bit = executableHeader->magic == MH_MAGIC_64 || executableHeader->magic == MH_CIGAM_64;
    uintptr_t cursor = (uintptr_t)executableHeader + (is64bit ? sizeof(struct mach_header_64) : sizeof(struct mach_header));
    const struct segment_command *segmentCommand = NULL;
    for (uint32_t i = 0; i < executableHeader->ncmds; i++, cursor += segmentCommand->cmdsize)
    {
        segmentCommand = (struct segment_command *)cursor;
        if (segmentCommand->cmd == LC_UUID)
        {
            const struct uuid_command *uuidCommand = (const struct uuid_command *)segmentCommand;
            return [[NSUUID alloc] initWithUUIDBytes:uuidCommand->uuid];
        }
    }

    return nil;
}

这篇关于如何在运行时获取构建UUID和映像基址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆