mach-o 介绍

42 篇文章 0 订阅

一、简介

Mach-O是一种文件格式,是mac上可执行文件的格式,类似于windows上的PE格式 (Portable Executable ), linux上的elf格式 (Executable and Linking Format)。我们编写的C、C++、swift、OC,最终编译链接生成Mach-O可执行文件

二、mach-o文件类型分为:

image

 

总共有11种。

  1. MH_OBJECT (0x1 )
  • 目标文件(.0)
  • 静态库文件(.a),静态库文件其实就是多个.o文件的合集.比如支持多种cpu建构的.a库文件.
  1. MH_EXECUTE (0x2) 可执行文件,
  • 比如.app文件
  1. MH_DYLIB 动态库文件
  • .dylib文件
  • .framework/xx文件
  1. MH_DYLINKER (0x7) 动态链接编辑器
  • usr/lib/dyld
  1. MH_DYSM 符号文件
  • dSYM/Content/Resources/DWARF/xx常用与app崩溃信息分析

三、Mach-O格式

Mach-O是一个以数据块分组的二进制字节流,这些数据块包含元信息,比如字节顺序、CPU类型、数据块大小等等。
典型的Mach-O文件包含三个区域:
1.Header:保存Mach-O的一些基本信息,包括平台、文件类型、指令数、指令总大小,dyld标记Flags等等。
2.Load Commands:紧跟Header,加载Mach-O文件时会使用这部分数据确定内存分布,对系统内核加载器和动态连接器起指导作用。
3.Data:每个segment的具体数据保存在这里,包含具体的代码、数据等等。

用一张图表示Mach-O

image

(一)、Header
数据结构

/*
 * The 32-bit mach header appears at the very beginning of the object file for
 * 32-bit architectures.
 */
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 */
};

/* Constant for the magic field of the mach_header (32-bit architectures) */
#define MH_MAGIC    0xfeedface  /* the mach magic number */
#define MH_CIGAM    0xcefaedfe  /* NXSwapInt(MH_MAGIC) */

/*
 * The 64-bit mach header appears at the very beginning of object files for
 * 64-bit architectures.
 */
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 */
};

/* Constant for the magic field of the mach_header_64 (64-bit architectures) */
#define MH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
#define MH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */

根据定义与注释,得到以下解释

名称含义
magicMach-O魔数,FAT:0xcafebabeARMv7:0xfeedface,ARM64:0xfeedfacf
cputype、cpusubtypeCPU架构及子版本
filetypeMH_EXECUTABLE(可执行二进制文件)、MH_OBJECT(目标文件)、MH_DYLIB(动态库),有11种宏定义类型,具体可查看源码
ncmds加载命令的数量
sizeofcmds所有加载命令的大小
flagsdyld加载需要的一些标记,有28种宏定义,具体看源码,其中MH_PIE表示启用ASLR地址空间布局随机化
reserved64位保留字段

使用MachOView查看某可执行文件:

屏幕快照 2019-01-13 下午10.30.39.gif

(二)、Load Commands
数据结构:

/*
 * The load commands directly follow the mach_header.  The total size of all
 * of the commands is given by the sizeofcmds field in the mach_header.  All
 * load commands must have as their first two fields cmd and cmdsize.  The cmd
 * field is filled in with a constant for that command type.  Each command type
 * has a structure specifically for it.  The cmdsize field is the size in bytes
 * of the particular load command structure plus anything that follows it that
 * is a part of the load command (i.e. section structures, strings, etc.).  To
 * advance to the next load command the cmdsize can be added to the offset or
 * pointer of the current load command.  The cmdsize for 32-bit architectures
 * MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple
 * of 8 bytes (these are forever the maximum alignment of any load commands).
 * The padded bytes must be zero.  All tables in the object file must also
 * follow these rules so the file can be memory mapped.  Otherwise the pointers
 * to these tables will not work well or at all on some machines.  With all
 * padding zeroed like objects will compare byte for byte.
 */
struct load_command {
    uint32_t cmd;       /* type of load command */
    uint32_t cmdsize;   /* total size of command in bytes */
};

注释的大概意思:
load_commands紧跟mach_header,load_commands展开后的数目与总大小已经在mach_header有记录,所有加载指令都是以cmd、cmdsize起头。cmd字段用该命令类型的常量表示,有专门的结构;cmdsize字段以字节为单位,主要记录偏移量让load command指针进入下一条加载指令,32位架构的cmdsize是以4字节的倍数,64位结构的cmdsize是以8字节的倍数(加载指令永远是这样对齐),不够用0填充字节。文件中的所有表都遵循这样的规则,这样就可以被映射到内存,否则的话指针不能很好地指向。

使用MachOView查看Load Commands区:

image

Load Commands下常见的加载指令:

指令含义
LC_SEGMENT_64定义一段(Segment),加载后被映射到进程的内存空间中,包括里面的节(Section)
LC_DYLD_INFO_ONLY记录有关链接的信息,包括在__LINKEDIT中动态链接的相关信息的具体偏移大小(重定位,绑定,弱绑定,懒加载绑定,导出信息等),ONLY表示该指令是程序运行所必需的。
LC_SYMTAB定义符号表和字符串表,链接文件时被dyld使用,也用于调试器映射符号到源文件。符号表定义的本地符号仅用于调试,而已定义和未定义的external符号被链接器使用
LC_DYSYMTAB将符号表中给出符号的额外信息提供给dyld
LC_LOAD_DYLINKERdyld的默认路径
LC_UUIDMach-O唯一ID
LC_VERSION_MIN_IPHONES系统要求的最低版本
LC_SOURCE_VERSION构建二进制文件的源代码版本号
LC_MAIN应用程序入口,dyld的_main函数获取该地址,然后跳转
LC_ENCRYPTION_INFO_64文件加密标志,加密内容偏移和大小
LC_LOAD_DYLIB依赖的动态库,含动态库名,版本号等信息
LC_RPATH@rpath搜索路径
LC_DATA_IN_CODE定义在代码段内的非指令的表
LC_CODE_SIGNATURE代码签名信息

LC_SEGMENT_64段数据结构(说明附在注释部分):

/*
 * The 64-bit segment load command indicates that a part of this file is to be
 * mapped into a 64-bit task's address space.  If the 64-bit segment has
 * sections then section_64 structures directly follow the 64-bit segment
 * command and their size is reflected in cmdsize.
 */
struct segment_command_64 { /* for 64-bit architectures */
    uint32_t    cmd;        /* Load Command类型 */
    uint32_t    cmdsize;    /*包含的所有section结构体的大小 */
    char        segname[16];    /* 段名 */
    uint64_t    vmaddr;     /* 映射到虚拟地址的偏移 */
    uint64_t    vmsize;     /* 映射到虚拟地址的大小 */
    uint64_t    fileoff;    /* 相对于当前架构文件的偏移 */
    uint64_t    filesize;   /* 文件大小 */
    vm_prot_t   maxprot;    /* 段页面的最高内存保护 */
    vm_prot_t   initprot;   /* 初始内存保护 */
    uint32_t    nsects;     /* 包含的section数 */
    uint32_t    flags;      /* 段页面标志 */
};

该数据结构的段主要有以下4种:

含义
_PAGEZERO空指针陷阱段,映射到虚拟内存空间第一页,捕捉对NULL指针的引用
_TEXT代码段、只读数据段
_DATA读取和写入数据段
_LINKEDITdyld需要使用的信息,包括重定位、绑定、懒加载信息等

(三)、Data
Load Commands区域下来接着就是DATA区域,展开Load Commands下的LC_SEGMENT_64可以看到多个Section64,各个Section的具体信息可以在Load Commands紧接着的部分查看,它们是一一对应的:

image

section的数据结构如下:

struct section_64 { /* for 64-bit architectures */
    char        sectname[16];   /* 节名 */
    char        segname[16];    /* 所属段名 */
    uint64_t    addr;       /* 映射到虚拟地址的偏移 */
    uint64_t    size;       /* 节的大小 */
    uint32_t    offset;     /* 节在当前架构文件中的偏移 */
    uint32_t    align;      /* 节的字节对齐大小n,2^n */
    uint32_t    reloff;     /* 重定位入口的文件偏移 */
    uint32_t    nreloc;     /* 重定位入口个数 */
    uint32_t    flags;      /* 节的类型和属性*/
    uint32_t    reserved1;  /* reserved (for offset or index) */
    uint32_t    reserved2;  /* reserved (for count or sizeof) */
    uint32_t    reserved3;  /* 保留位,以上两同理 */
};

section节已经是最小的分类,大部分内容集中在__TEXT,__DATA这两段中,部分内容如下:

__TEXT节含义
__text程序可执行代码区域
__stubs间接符号存根,用于跳转到懒加载指针表
__stubs_helper懒加载符号加载辅助函数
__cstring只读的C字符串,包含OC的部分字符串和属性名
............
__DATA含义
__nl_symbol_ptr非懒加载指针表,dyld加载时立即绑定值
__la_symbol_ptr懒加载指针表,第1次调用才绑定值
__got非懒加载全局指针表
__mod_init_funcconstructor函数
__cfstringOC字符串
............

四、小结

了解Mach-O可以帮助我们理解dyld的加载Mach-O的过程以及与Mach-O相关的读取或操作,如fishhook、文件内偏移地址等。

五、参考

Dynamic Linking of Imported Functions in Mach-O
https://www.jianshu.com/p/7040dd1396f7

 

 

 

作者:司空123
链接:https://www.jianshu.com/p/5a44f69466cc
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值