最近發(fā)現(xiàn)一些_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
其實(shí)lldb中有個命令可以實(shí)現(xiàn)類似功能毡代,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結(jié)構(gòu)體不方便打印輸出劫拗,如圖:
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實(shí)現(xiàn)
_dyld_image系列函數(shù)不是線程安全的胁附,用mach_vm_region_info更好!代碼在github:https://github.com/davidrhodus/misc/blob/master/iOS-internals/vmmap.c
代碼量很大滓彰,有興趣的可以研究一下~
參考:iOS模塊遍歷方法