Mach-O 文件二

作者: Superman168 | 来源:发表于2018-07-11 19:11 被阅读0次

    前言

    我们知道对于OS X和iOS 来说 Mach-O 是其可执行文件的格式。在 Mach-O 文件一 中,我们知道了可执行文件、库文件、Dsym文件、动态库、动态连接器都是这种格式的。

    Mach-O 文件的组成结构

    Mach-O 的组成结构如下图所示包括了Header、Load commands、Data(包含Segement的具体数据),我们可以通过 MachOView 工具来查看 MachO 文件的构成:

    单个架构的 Mach-O 文件的组成 多个架构的 Mach-O 文件的组成

    Header 文件组成:

    Mach-O 的头文件,存储一些基本信息,比如当前文件对应的CPU 类型、文件类型等

    Header 文件组成

    Load Commands 结构

    ** Load Commands ** 顾名思义,加载指令,包含了很多内容,类似区域位置,符号表,动态符号等,相当于一个表,这些加载指令清晰地告诉加载器如何处理二进制数据,有些命令是由内核处理的,有些是由动态链接器处理的。

    Load Commands 结构 image.png

    LC_SEGMENT_64和LC_SEGMENT是加载的主要命令,它负责指导内核来设置进程的内存空间

    Data 文件(Section&Segment)

    __TEXT代表的是Segment,小写的__text代表 Section

    image.png

    __TEXT,__text 存放的汇编代码,
    其他段村烦的所有的类名,方法名称,字符串等。

    注:终端中可以使用 otool 工具来查看 Mach-O 文件的相关信息。

        otool
        -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)
        -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
    

    如:

    otool -h macho_arm64
    Mach header
          magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
     0xfeedfacf 16777228          0  0x00           2    21       2544 0x00200085
    
    otool -l macho_arm64
    macho_arm64:
    Mach header
          magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
     0xfeedfacf 16777228          0  0x00           2    21       2544 0x00200085
    Load command 0
          cmd LC_SEGMENT_64
      cmdsize 72
    
    ...
    

    相关文章

      网友评论

        本文标题:Mach-O 文件二

        本文链接:https://www.haomeiwen.com/subject/vefkpftx.html