如何在运行时确定二进制图像的架构?

6

崩溃日志包含“二进制图像”部分,其中包含有关架构(armv6 / armv7)和所有加载模块的标识符的信息。如何在运行时确定此信息?(至少仅适用于应用程序可执行文件)
NSBundle具有executableArchitectures方法,但如何确定正在运行的架构?

3个回答

19

好的,现在是长答案的时间了。应用程序中dyld映像的Mach头包含您要查找的信息。我添加了一个示例,仅测试了其工作情况,因此不建议直接将其粘贴到生产代码中。它获取所有当前加载的dyld images的所有Mach头,并打印一个与崩溃日志的二进制图像部分非常相似的输出。我调用的方法不是线程安全的。我缺少的唯一一件事是二进制图像的结束地址,因为我没有费力去查找如何找到它。

Main.m

#import <UIKit/UIKit.h>

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

void printImage(const struct mach_header *header)
{
    uint8_t *header_ptr = (uint8_t*)header;
    typedef struct load_command load_command;

    const NXArchInfo *info = NXGetArchInfoFromCpuType(header->cputype, header->cpusubtype);

    //Print the architecture ex. armv7
    printf("%s ", info->name);

    header_ptr += sizeof(struct mach_header);
    load_command *command = (load_command*)header_ptr;

    for(int i = 0; i < header->ncmds > 0; ++i)
    {
        if(command->cmd == LC_UUID)
        {
            struct uuid_command ucmd = *(struct uuid_command*)header_ptr;

            CFUUIDRef cuuid = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *((CFUUIDBytes*)ucmd.uuid));
            CFStringRef suuid = CFUUIDCreateString(kCFAllocatorDefault, cuuid);
            CFStringEncoding encoding = CFStringGetFastestEncoding(suuid);

            //Print UUID
            printf("<%s> ", CFStringGetCStringPtr(suuid, encoding));

            CFRelease(cuuid);
            CFRelease(suuid);

            break;
        }

        header_ptr += command->cmdsize;
        command = (load_command*)header_ptr;
    }
}

void printBinaryImages()
{
    printf("Binary Images:\n");
    //Get count of all currently loaded DYLD
    uint32_t count = _dyld_image_count();

    for(uint32_t i = 0; i < count; i++)
    {
        //Name of image (includes full path)
        const char *dyld = _dyld_get_image_name(i);

        //Get name of file
        int slength = strlen(dyld);

        int j;
        for(j = slength - 1; j>= 0; --j)
            if(dyld[j] == '/') break;

        //strndup only available in iOS 4.3
        char *name = strndup(dyld + ++j, slength - j);
        printf("%s ", name);
        free(name);

        const struct mach_header *header = _dyld_get_image_header(i);
        //print address range
        printf("0x%X - ??? ", (uint32_t)header);

        printImage(header);

        //print file path
        printf("%s\n",  dyld);
    }
    printf("\n");
}

int main(int argc, char *argv[])
{        
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    printBinaryImages();
    [pool release];
    return retVal;
}

示例输出:

Binary Images:
TestBed 0x1000 - ??? i386 <E96D079C-E035-389D-AA12-71E968C76BFE> /Users/username/Library/Application Support/iPhone Simulator/4.3/Applications/6F64D9F8-9179-4E21-AE32-4D4604BE77E5/TestBed.app/TestBed
UIKit 0x8000 - ??? i386 <72030911-362F-3E47-BAF3-ACD2CB6F88C0> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit
Foundation 0x772000 - ??? i386 <EB718CBD-1D57-3D31-898D-7CFA9C172A46> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/Foundation.framework/Foundation
CoreGraphics 0xA10000 - ??? i386 <D168A716-71F2-337A-AE0B-9DCF51AE9181> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics
libSystem.dylib 0xCAA000 - ??? i386 <8DF0AFCD-FFA5-3049-88E2-7410F8398749> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/lib/libSystem.dylib
...

谢谢您的回答!由于我只需要应用程序可执行文件的CPU架构,所以我使用了dladdr(this_function),然后使用Dl_info.dli_fbase作为指向struct mach_header的指针。 - muxa
@Joe,你知道从_mh_execute_header获取bundle uuid(几乎相同的方式)是否适用于Appstore二进制文件吗?它在开发人员和Ad-hoc构建中都有效,但我想确认一下。 - Tertium
@Tertium 我不确定那个函数的位置,但如果它是私有框架的一部分,那么它将不被允许。 - Joe
@Joe mh_execute_header 不是一个函数,而是应用程序镜像的 mach_header。我的捆绑包已经成功地通过 Organizer->Archives->Validate 进行了验证,所以我认为它不是被禁止的。但我想确保在为 App Store 编译的捆绑包中,_dyld_get... 函数能够正常工作,并且 mach_headers 包含 uuid 信息,就像在开发/ adhoc 构建中一样。 - Tertium
@Tertium 我不认为它们是私有的。它们是SDK的/usr/include/目录的一部分,应该没问题。由于我没有使用这些API提交过应用程序,所以无法百分之百地保证它会被批准。 - Joe

2

如果您正在构建应用程序,并且想要快速了解有关体系结构的信息,可以检查一些预处理器定义来确定应用程序当前所构建的体系结构。确保首先检查可用的最高版本的ARM,因为每个新的版本都定义了以前所有旧版本。

#if __arm__
#import <arm/arch.h>

#ifdef __ARM_ARCH_6K__
//This is armv6
#endif //__ARM_ARCH_6K__
#endif //__arm__

0

我们可以使用sysctl,sysctlbyname系统调用来获取或设置系统信息。

示例代码:

#import <sys/sysctl.h>
#import <mach/machine.h>

int32_t value = 0;
size_t length = sizeof(value);
sysctlbyname("hw.cputype", &value, &length, NULL, 0);

if (value == CPU_TYPE_ARM64) {
    // arm64
}
else if (value == CPU_TYPE_ARM) {
    // armv7/armv7s
}
else if (value == CPU_TYPE_X86) {
    // simulator
}

我只列出了2016年最常见的架构。要获取更多详细信息,请查找"hw.cpusubtype",例如 CPU_SUBTYPE_ARM_V6 CPU_SUBTYPE_ARM_V7 CPU_SUBTYPE_ARM_V7S


这将返回设备的CPU类型,当问题涉及二进制模块编译的架构时。因此,@Joe的答案是正确的。 - muxa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接