一瓮栗、Mach-O簡(jiǎn)介
Mach-O
是Mach object
的縮寫袍榆,是Mac\iOS
上用于存儲(chǔ)程序廉羔、庫(kù)的標(biāo)準(zhǔn)格式溉痢。
二、Mach-O格式的文件類型
#define MH_OBJECT 0x1 /* relocatable object file */
#define MH_EXECUTE 0x2 /* demand paged executable file */
#define MH_FVMLIB 0x3 /* fixed VM shared library file */
#define MH_CORE 0x4 /* core file */
#define MH_PRELOAD 0x5 /* preloaded executable file */
#define MH_DYLIB 0x6 /* dynamically bound shared library */
#define MH_DYLINKER 0x7 /* dynamic link editor */
#define MH_BUNDLE 0x8 /* dynamically bound bundle file */
#define MH_DYLIB_STUB 0x9 /* shared library stub for static */
/* linking only, no section contents */
#define MH_DSYM 0xa /* companion file with only debug */
/* sections */
#define MH_KEXT_BUNDLE 0xb /* x86_64 kexts */
#define MH_FILESET 0xc /* set of mach-o's */
可以在xnu
源碼中憋他,查看到Mach-O
格式的詳細(xì)定義 xnu源碼
三孩饼、常見(jiàn)的Mach-O文件類型
1、MH_OBJECT
- 目標(biāo)文件(.o)
- 靜態(tài)庫(kù)文件(.a)竹挡,靜態(tài)庫(kù)其實(shí)就是N個(gè).o合并在一起
2镀娶、MH_EXECUTE:可執(zhí)行文件
- .app/xxx
3、MH_DYLIB:動(dòng)態(tài)庫(kù)文件
- .dylib
- .framework/xx
4揪罕、MH_DYLINKER:動(dòng)態(tài)鏈接編輯器
- /usr/lib/dyld
5梯码、MH_DSYM:存儲(chǔ)著二進(jìn)制文件符號(hào)信息的文件
- .dSYM/Contents/Resources/DWARF/xx(常用于分析APP的崩潰信息)
四宝泵、窺探Mach-O的結(jié)構(gòu)
1、file:查看Mach-O的文件類型
file 文件路徑
2轩娶、otool:查看Mach-O特定部分和段的內(nèi)容
zydeMacBook-Pro:ppx zy$ otool
Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool [-arch arch_type] [-fahlLDtdorSTMRIHGvVcXmqQjCP] [-mcpu=arg] [--version] <object file> ...
-f print the fat headers
-a print the archive header
-h print the mach header
-l print the load commands
-L print shared libraries used
-D print shared library id name
-t print the text section (disassemble with -v)
-x print all text sections (disassemble with -v)
-p <routine name> start dissassemble from routine name
-s <segname> <sectname> print contents of section
-d print the data section
-o print the Objective-C segment
-r print the relocation entries
-S print the table of contents of a library (obsolete)
-T print the table of contents of a dynamic shared library (obsolete)
-M print the module table of a dynamic shared library (obsolete)
-R print the reference table of a dynamic shared library (obsolete)
-I print the indirect symbol table
-H print the two-level hints table (obsolete)
-G print the data in code table
-v print verbosely (symbolically) when possible
-V print disassembled operands symbolically
-c print argument strings of a core file
-X print no leading addresses or headers
-m don't use archive(member) syntax
-B force Thumb disassembly (ARM objects only)
-q use llvm's disassembler (the default)
-Q use otool(1)'s disassembler
-mcpu=arg use `arg' as the cpu for disassembly
-j print opcode bytes
-P print the info plist section as strings
-C print linker optimization hints
--version print the version of /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool
- 1儿奶、打印依賴動(dòng)態(tài)庫(kù)
otool -L Super
Super:
/usr/lib/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.8)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1200.3.0)
/usr/lib/libc++abi.dylib (compatibility version 1.0.0, current version 1200.3.0)
/usr/lib/libcompression.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libicucore.A.dylib (compatibility version 1.0.0, current version 68.2.0)
/usr/lib/liblzma.5.dylib (compatibility version 6.0.0, current version 6.3.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)
- 2、打印頭信息
otool -h Super
Super:
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedfacf 16777228 0 0x00 2 109 12240 0x00a10085
3鳄抒、lipo:常用于多架構(gòu)Mach-O文件的處理
- 查看架構(gòu)信息:
lipo -info 文件路徑
- 導(dǎo)出某種特定架構(gòu):
lipo 文件路徑 -thin 架構(gòu)類型 -output 輸出文件路徑
- 合并多種架構(gòu):
lipo 文件路徑1 文件路徑2 -output 輸出文件路徑
五闯捎、dyld和Mach-O
1.位置
在iOS
中,是使用了/usr/lib/dyld
程序來(lái)加載動(dòng)態(tài)庫(kù)
2.dyld源碼
3.dyld用于加載以下類型的Mach-O文件
查看源碼在方法 (loadPhase6(int fd, const struct stat& stat_buf, const char* path, const LoadContext& context)
)
// only MH_BUNDLE, MH_DYLIB, and some MH_EXECUTE can be dynamically loaded
const mach_header* mh = (mach_header*)firstPages;
switch ( mh->filetype ) {
case MH_EXECUTE:
case MH_DYLIB:
case MH_BUNDLE:
break;
default:
throw "mach-o, but wrong filetype";
}
六许溅、Mach-O的基本結(jié)構(gòu)
1瓤鼻、官方描述 Mach-O Programming Topics
Mach-O
2、Mach-O文件包含3個(gè)主要區(qū)域
1闹司、Header
- 文件類型娱仔、目標(biāo)架構(gòu)類型等
2、Load commands
- 描述文件在虛擬內(nèi)存中的邏輯結(jié)構(gòu)游桩、布局
3牲迫、Raw segment data
- 在Load commands中定義的Segment的原始數(shù)據(jù)