美文网首页
iOS遍历自身加载的dylib

iOS遍历自身加载的dylib

作者: 人仙儿a | 来源:发表于2017-09-12 16:28 被阅读188次

    最近发现一些_dylib开头的api,很是好奇。找到了它的一些具体的用法。

    打印加载的所有的dylib和img_header.代码如下:

    for (int i = 0; i < _dyld_image_count(); i++) {
            const char *name = _dyld_get_image_name(i);
            const struct mach_header *header = _dyld_get_image_header(i);
            //intptr_t vmaddr_slide = _dyld_get_image_vmaddr_slide(i);//打印偏移地址
            printf("%d name:%s\n", i, name);
        }
    

    日志:

    0 name:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/dyld_sim
    1 name:/Users/a58/Library/Developer/CoreSimulator/Devices/3F8D1485-7DAD-42E7-A019-BA710FA71AEE/data/Containers/Bundle/Application/25067A85-AE2F-4F6F-AE8F-C332D645D0B9/TestFFFF.app/TestFFFF
    

    其实lldb中有个命令可以实现类似功能,image list

    (lldb) image list
    [  0] E6129F9E-BC72-3033-8CF6-E77E70EDF975 0x00000001013f6000 /Users/a58/Library/Developer/Xcode/DerivedData/TestFFFF-buqtyppwrvhhrnchydxlzayrmcma/Build/Products/Debug-iphonesimulator/TestFFFF.app/TestFFFF 
    [  1] 322C06B7-8878-311D-888C-C8FD2CA96FF3 0x000000010bb21000 /usr/lib/dyld 
    [  2] 14AD0238-D077-378B-82A8-AC2D2ADC9DDF 0x00000001013fe000 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/dyld_sim 
    

    mach_header结构体不方便打印输出,如图:

    cpusubtype

    指的是cpu的类型, powerpc指是是电脑,arm指的是iphone, 每种类型列表如下:

    /*
     *  PowerPC subtypes
     */
    #define CPU_SUBTYPE_POWERPC_ALL     ((cpu_subtype_t) 0)
    #define CPU_SUBTYPE_POWERPC_601     ((cpu_subtype_t) 1)
    #define CPU_SUBTYPE_POWERPC_602     ((cpu_subtype_t) 2)
    #define CPU_SUBTYPE_POWERPC_603     ((cpu_subtype_t) 3)
    #define CPU_SUBTYPE_POWERPC_603e    ((cpu_subtype_t) 4)
    #define CPU_SUBTYPE_POWERPC_603ev   ((cpu_subtype_t) 5)
    #define CPU_SUBTYPE_POWERPC_604     ((cpu_subtype_t) 6)
    #define CPU_SUBTYPE_POWERPC_604e    ((cpu_subtype_t) 7)
    #define CPU_SUBTYPE_POWERPC_620     ((cpu_subtype_t) 8)
    #define CPU_SUBTYPE_POWERPC_750     ((cpu_subtype_t) 9)
    #define CPU_SUBTYPE_POWERPC_7400    ((cpu_subtype_t) 10)
    #define CPU_SUBTYPE_POWERPC_7450    ((cpu_subtype_t) 11)
    #define CPU_SUBTYPE_POWERPC_970     ((cpu_subtype_t) 100)
    
    /*
     *  ARM subtypes
     */
    #define CPU_SUBTYPE_ARM_ALL             ((cpu_subtype_t) 0)
    #define CPU_SUBTYPE_ARM_V4T             ((cpu_subtype_t) 5)
    #define CPU_SUBTYPE_ARM_V6              ((cpu_subtype_t) 6)
    #define CPU_SUBTYPE_ARM_V5TEJ           ((cpu_subtype_t) 7)
    #define CPU_SUBTYPE_ARM_XSCALE      ((cpu_subtype_t) 8)
    #define CPU_SUBTYPE_ARM_V7      ((cpu_subtype_t) 9)
    #define CPU_SUBTYPE_ARM_V7F     ((cpu_subtype_t) 10) /* Cortex A9 */
    #define CPU_SUBTYPE_ARM_V7S     ((cpu_subtype_t) 11) /* Swift */
    #define CPU_SUBTYPE_ARM_V7K     ((cpu_subtype_t) 12)
    #define CPU_SUBTYPE_ARM_V6M     ((cpu_subtype_t) 14) /* Not meant to be run under xnu */
    #define CPU_SUBTYPE_ARM_V7M     ((cpu_subtype_t) 15) /* Not meant to be run under xnu */
    #define CPU_SUBTYPE_ARM_V7EM        ((cpu_subtype_t) 16) /* Not meant to be run under xnu */
    
    #define CPU_SUBTYPE_ARM_V8      ((cpu_subtype_t) 13)
    
    /*
     *  ARM64 subtypes
     */
    #define CPU_SUBTYPE_ARM64_ALL           ((cpu_subtype_t) 0)
    #define CPU_SUBTYPE_ARM64_V8            ((cpu_subtype_t) 1)
    

    cputype

    cpu类型,mac主要是x86和x86_64,iphone主要是arm和arm64

    #define CPU_TYPE_ANY        ((cpu_type_t) -1)
    
    #define CPU_TYPE_VAX        ((cpu_type_t) 1)
    /* skip             ((cpu_type_t) 2)    */
    /* skip             ((cpu_type_t) 3)    */
    /* skip             ((cpu_type_t) 4)    */
    /* skip             ((cpu_type_t) 5)    */
    #define CPU_TYPE_MC680x0    ((cpu_type_t) 6)
    #define CPU_TYPE_X86        ((cpu_type_t) 7)
    #define CPU_TYPE_I386       CPU_TYPE_X86        /* compatibility */
    #define CPU_TYPE_X86_64     (CPU_TYPE_X86 | CPU_ARCH_ABI64)
    
    /* skip CPU_TYPE_MIPS       ((cpu_type_t) 8)    */
    /* skip             ((cpu_type_t) 9)    */
    #define CPU_TYPE_MC98000    ((cpu_type_t) 10)
    #define CPU_TYPE_HPPA           ((cpu_type_t) 11)
    #define CPU_TYPE_ARM        ((cpu_type_t) 12)
    #define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)
    #define CPU_TYPE_MC88000    ((cpu_type_t) 13)
    #define CPU_TYPE_SPARC      ((cpu_type_t) 14)
    #define CPU_TYPE_I860       ((cpu_type_t) 15)
    /* skip CPU_TYPE_ALPHA      ((cpu_type_t) 16)   */
    /* skip             ((cpu_type_t) 17)   */
    #define CPU_TYPE_POWERPC        ((cpu_type_t) 18)
    #define CPU_TYPE_POWERPC64      (CPU_TYPE_POWERPC | CPU_ARCH_ABI64)
    

    用mach_vm_region_info APIs实现

    _dyld_image系列函数不是线程安全的,用mach_vm_region_info更好!代码在github:https://github.com/davidrhodus/misc/blob/master/iOS-internals/vmmap.c
    代码量很大,有兴趣的可以研究一下~

    参考:iOS模块遍历方法

    相关文章

      网友评论

          本文标题:iOS遍历自身加载的dylib

          本文链接:https://www.haomeiwen.com/subject/pegcsxtx.html