簡(jiǎn)介
Mach-O是Mach object的縮寫(xiě),是Mac\iOS上用于存儲(chǔ)程序、庫(kù)的標(biāo)準(zhǔn)格式
屬于Mach-O格式的文件類型有
可以在xnu源碼中,查看到Mach-O格式的詳細(xì)定義
xnu/EXTERNAL_HEADERS/mach-o/loader.h
#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 */
常見(jiàn)的Mach-O文件類型
-
MH_OBJECT
目標(biāo)文件(.o)
靜態(tài)庫(kù)文件(.a),靜態(tài)庫(kù)其實(shí)就是N個(gè).o合并在一起 -
MH_EXECUTE:可執(zhí)行文件
.app/xx -
MH_DYLIB:動(dòng)態(tài)庫(kù)文件
.dylib
.framework/xx -
MH_DYLINKER:動(dòng)態(tài)鏈接編輯器
/usr/lib/dyld - MH_DSYM:存儲(chǔ)著二進(jìn)制文件符號(hào)信息的文件
Mach-O的基本結(jié)構(gòu)
一個(gè)Mach-O文件包含3個(gè)主要區(qū)域
-
Header
文件類型、目標(biāo)架構(gòu)類型等 -
Load commands
描述文件在虛擬內(nèi)存中的邏輯結(jié)構(gòu)、布局 -
Raw segment data
在Load commands中定義的Segment的原始數(shù)據(jù)
窺探Mach-O的結(jié)構(gòu)
1、命令行工具
file:查看Mach-O的文件類型
file 文件目錄
otool:查看Mach-O特定部分和段的內(nèi)容
lipo:常用于多架構(gòu)Mach-O文件的處理
查看架構(gòu)信息:
lipo -info 文件路徑
導(dǎo)出某種特定架構(gòu):
lipo 文件路徑 -thin 架構(gòu)類型 -output 輸出文件路徑
合并多種架構(gòu):
lipo 文件路徑1 文件路徑2 -output 輸出文件路徑
2、GUI工具
dyld和Mach-O
- dyld也是Mach-O格式文件,屬于
MH_DYLINKER類型 -
dyld用于加載以下類型的Mach-O文件
MH_EXECUTE
MH_DYLIB
MH_BUNDLE - APP的可執(zhí)行文件、動(dòng)態(tài)庫(kù)都是由dyld負(fù)責(zé)加載的
其他
在Xcode中查看target的Mach-O類型
Build Settings->Mach-O Type

Xocde