Mach-O
是Mach Object
文件格式的缩写,是mac
以及iOS
上可执行文件的格式。Mach-O
文件对应有多种格式:xcode
- 目标文件
.o
- 库文件:
.a
静态库文件.dylib
动态库文件.framework
系统级为动态库文件,本身建立的为静态库文件- 可执行文件及
MDW.app
内部的MDW
文件(通用二进制文件)dyld
动态连接器将依赖的动态库加载到内存.dsym
符号表
在Xcode
中咱们能够直接建立.c
文件,经过终端clang
命令来对.c
文件进行编译或生成可执行文件,下面看一下clang
怎样使用的。bash
一、建立一个main.c文件以下:架构
#include <stdio.h>
int main(){
printf("打印:yahibo\n");
return 0;
}
复制代码
二、编译文件app
clang -c main.c
复制代码
会生成main.o
文件,该文件即为mach-o
文件,经过命令file main.o
查看文件信息以下:ide
main.o: Mach-O 64-bit object x86_64
复制代码
是一个object
类型的文件称为目标文件,并非可执行文件函数
三、生成可执行文件ui
clang main.o
会生成a.out
文件,便可执行文件,经过ls
查看clang -o main main.o
也会生成可执行文件main
clang -o main main.c
直接根据源文件生成可执行文件main
size -x -l -m a.out
查看文件信息,以下:Segment __PAGEZERO: 0x100000000 (vmaddr 0x0 fileoff 0)
Segment __TEXT: 0x1000 (vmaddr 0x100000000 fileoff 0)
Section __text: 0x2a (addr 0x100000f50 offset 3920)
Section __stubs: 0x6 (addr 0x100000f7a offset 3962)
Section __stub_helper: 0x1a (addr 0x100000f80 offset 3968)
Section __cstring: 0x11 (addr 0x100000f9a offset 3994)
Section __unwind_info: 0x48 (addr 0x100000fac offset 4012)
total 0xa3
Segment __DATA: 0x1000 (vmaddr 0x100001000 fileoff 4096)
Section __nl_symbol_ptr: 0x10 (addr 0x100001000 offset 4096)
Section __la_symbol_ptr: 0x8 (addr 0x100001010 offset 4112)
total 0x18
Segment __LINKEDIT: 0x1000 (vmaddr 0x100002000 fileoff 8192)
total 0x100003000
复制代码
四、执行文件this
./a.out 或 ./main
复制代码
输出:spa
打印:yahibo
复制代码
以上步骤能够用来写c
并编译执行。操作系统
多个文件是如何编译的呢?
开发中根据不一样功能模块咱们会分不少文件来实现,在clang
中是能够对多个文件进行一次性打包,生成一个可执行文件。以下:
一、新建一个功能文件
people.c
#include <stdio.h>
void sleep(){
printf("正在睡觉\n");
}
复制代码
二、在main.c
中声明sleep
方法并调用
void sleep();//声明方法
int main(){
printf("打印:yahibo\n");
sleep();//调用方法
return 0;
}
复制代码
三、编译为可执行文件
clang -o main main.c people.c
复制代码
四、执行可执行文件
./main
复制代码
运行以下:
打印:yahibo
正在睡觉
复制代码
在iOS
中不一样手机对应着可能不一样的架构,如arm6四、armv七、armv7s
。为了兼容不一样架构的手机,苹果推出了通用二进制文件,包含了应用程序经常使用的这些架构,所以通用二进制文件,比单一架构二进制文件要大不少。
1
处默认架构为arm6四、armv7
armv7s
还须要在1
处添加armv7s
字符经过以上配置真实编译出来的是包含arm6四、armv7
架构,由于工程中使用了第三方静态库不包含armv7s
所以这里配置为标准架构模式。
在xxx.app
中的xxx黑色文件
便是通用二进制文件,右键xxx.app
显示包内容便可得到。
经过lipo
命令能够查看、拆分及合并以上提出的架构,在作静态库时也会使用,来合并真机下和模拟器下的静态库,以适应不一样的调试环境。
MDW.app
中我获取可执行文件MDW
一、查看架构信息
lipo -info MDW
复制代码
打印以下:
Architectures in the fat file: MDW are: armv7 arm64
复制代码
二、拆分armv七、arm64
架构
lipo MDW -thin armv7 -output MDW_armv7
lipo MDW -thin arm64 -output MDW_arm64
复制代码
查看armv7信息:
lipo -info MDW_armv7
复制代码
打印以下:
Non-fat file: MDW_armv7 is architecture: armv7
复制代码
查看arm64信息:
lipo -info MDW_arm64
复制代码
打印以下:
Non-fat file: MDW_arm64 is architecture: arm64
复制代码
三、合并架构
lipo -create MDW_armv7 MDW_arm64 -output MDW_ALL
复制代码
查看合并后的信息
lipo -info MDW_ALL
复制代码
打印以下:
Architectures in the fat file: MDW_ALL are: armv7 arm64
复制代码
产生的可执行文件如图:
官方图解:
文件分为三个部分:
Header:
包含Mach-O
文件的基本信息,字节顺序、架构类型、加载指令的数量等Load commands:
包含区域位置、符号表、动态符号表,加载Mach-O
文件时使用这里的数据肯定内存分布Data:
数据段segement
,包含具体代码、常量、类、方法等,有多个segment
,每一个segment
有0到多个section
,每一个段有一个虚拟地址映射到进程的地址空间直接使用MachOView打开MDW可执行文件,以下:
armv七、arm64
架构除了以上直接查看header
,还能够经过otool
命令查看header
信息:
otool -f MDW
复制代码
打印以下:
Fat headers
fat_magic 0xcafebabe
nfat_arch 2
architecture 0
cputype 12
cpusubtype 9
capabilities 0x0
offset 16384
size 7587424
align 2^14 (16384)
architecture 1
cputype 16777228
cpusubtype 0
capabilities 0x0
offset 7618560
size 8748384
align 2^14 (16384)
复制代码
或
otool -h MDW
复制代码
打印:
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedface 12 9 0x00 2 48 5080 0x00210085
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedfacf 16777228 0 0x00 2 48 5752 0x00210085
复制代码
armv七、arm64
架构下的header
信息 在objc4
源码loader.h文件中有mach_header
的结构体定义,以下:struct mach_header {
uint32_t magic; /* mach magic number identifier */
cpu_type_t cputype; /* cpu specifier */
cpu_subtype_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
};
struct mach_header_64 {
uint32_t magic; /* mach magic number identifier */
cpu_type_t cputype; /* cpu specifier */
cpu_subtype_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
uint32_t reserved; /* reserved */
};
复制代码
magic:
魔数,肯定是64位仍是32位cputype:cpu
类型cpusubtype:cpu
子类型filetype:Mach-O
支持多种文件类型,使用filetype
来标注具体文件类型ncmds:
加载命令的数量sizeofcmds:
命令区域(load commands
)总的字节大小flags:
标识二进制文件所支持的功能,主要与系统的加载、连接有关Header
以后是load commands
段为加载命令段,在header
结构体中有对加载命令段相关信息的描述,用于解析加载命令。在objc4
源码loader.h中,有对loadcommand
的定义:
struct load_command {
uint32_t cmd; /* type of load command */
uint32_t cmdsize; /* total size of command in bytes */
};
复制代码
cmd:
命令类型,针对不一样架构有不一样的结构(32位、64位)cmdsize:
命令所占字节大小(32位size必须为4字节的倍数,64位size必须为8字节的倍数) 在文件中有两个结构体segment_command
和segment_command_64
针对不一样架构的结构体,内部设置字段相同。以segment_command_64
为例:struct segment_command_64 { /* for 64-bit architectures */
uint32_t cmd; /* LC_SEGMENT_64 */
uint32_t cmdsize; /* includes sizeof section_64 structs */
char segname[16]; /* segment name */
uint64_t vmaddr; /* memory address of this segment */
uint64_t vmsize; /* memory size of this segment */
uint64_t fileoff; /* file offset of this segment */
uint64_t filesize; /* amount to map from the file */
vm_prot_t maxprot; /* maximum VM protection */
vm_prot_t initprot; /* initial VM protection */
uint32_t nsects; /* number of sections in segment */
uint32_t flags; /* flags */
};
复制代码
cmd:
加载命令类型LC_SEGMENT:
表示这好似一个段加载命令,须要将它加载到对应的进程空间上LC_LOAD_DYLIB:
这是一个须要动态加载的连接库,它使用dylib_command
结构体表示LC_MAIN:
记录了可执行文件的主函数main()
的位置,它使用entry_point_command
结构体表示LC_CODE_SIGNATURE:
代码签名的加载命令,描述了Mach-O
的代码签名信息,它属于连接信息,使用linkedit_data_command
结构体表示cmdsize:
加载命令所占内存大小segname:
存放16字节大小的段名字,当前是__PAGEZERO
。vmaddr:
段的虚拟内存起始地址vmsize:
段的虚拟内存大小fileoff:
段在文件中偏移量filesize:
段在文件大小maxprot:
段页面所须要的最高内存保护(4=r,2=w,1=x)initprot:
段页面初始的内存保护nsects:
段中包含section的数量flags:
其余杂项标志位 在看MachOView
中的loadcommands
字段:以上为是应用程序全部加载命令,经过上面流程可以看到对系统库的加载顺序。对比项目中引入的库文件,顺序是一致的,以下图:
以上加载命令含义以下:
LC_SEGMENT_64:
将文件中的段映射到进程地址空间中LC_DYLD_INFO_ONLY:
动态连接相关信息LC_SYMTAB:
符号表信息,位置、偏移、数据个数,供dyld使用LC_DYSYMTAB:
动态符号表信息,供dyld使用LC_LOAD_DYLINKER:
连接器信息,记录使用那些连接器完成内核后序的加载工做LC_UUID:Mach-O
文件的惟一标识LC_VERSION_MIN_MACOSX:
支持最低操做系统版本LC_SOURCE_VERSION:
源代码的版本号LC_MAIN:
设置主线程的入口即栈大小LC_LOAD_DYLIB:
依赖库信息,dyld
经过该命令去加载依赖库LC_FUNCTION_STARTS:
函数的起始地址表LC_CODE_SIGNATURE:
代码签名Data
区域由Segment
段和Section
节组成:
segment
主要有__TEXT
和__DATA
组成__text:
是主程序代码__stubs、__stub_helper:
是动态连接的桩__cstring:
程序中c语言字符串__const:
常量Section含义:
Section64(__TEXT,__objc_methname):
OC类名Section64(__DATA,__objc_classlist):
OC类列表Section64(__DATA,__objc_protollist):
OC原型列表Section64(__DATA,__objc_imageinfo):
OC镜像信息Section64(__DATA,__objc_selfrefs):
OC类自引用Section64(__DATA,__objc_superrefs):
OC类超类的引用Section64(__DATA,__ivar):
OC类成员变量等等,都是经过section
来对OC
中的具体类别作加载的。segment
段分32位和64位,字段相同,以64为例以下:
struct section_64 { /* for 64-bit architectures */
char sectname[16]; /* name of this section */
char segname[16]; /* segment this section goes in */
uint64_t addr; /* memory address of this section */
uint64_t size; /* size in bytes of this section */
uint32_t offset; /* file offset of this section */
uint32_t align; /* section alignment (power of 2) */
uint32_t reloff; /* file offset of relocation entries */
uint32_t nreloc; /* number of relocation entries */
uint32_t flags; /* flags (section type and attributes)*/
uint32_t reserved1; /* reserved (for offset or index) */
uint32_t reserved2; /* reserved (for count or sizeof) */
uint32_t reserved3; /* reserved */
};
复制代码
sectname:
是__text
,就是主程序代码section
所属的segment
名,第一个是__TEXT
addr:
当前section
在内存中的起始位置size:
当前section
所占内存大小offset:
当前section
的文件偏移align:
字节大小对齐reloff:
重定位入口的文件偏移,0nreloc:
须要重定位的入口数量,0flags:
包含section
的type
和attributes
reserved一、reserved2
预留字段