Mac OS X ABI Mach-O File Format Reference(2)

683 阅读33分钟

Mac OS X ABI Mach-O File Format Reference(1)

Mac OS X ABI Mach-O File Format Reference(3)


Load Command Data Structures

The load command structures are located directly after the header of the object file, and they specify both the logical structure of the file and the layout of the file in virtual memory. Each load command begins with fields that specify the command type and the size of the command data.

加载命令结构直接位于对象文件头的后面,它们指定文件的逻辑结构和虚拟内存中文件的布局。每个加载命令都以指定命令类型和命令数据大小的字段开头。

load_command

Contains fields that are common to all load commands.

包含所有加载命令通用的字段。

struct load_command {
	uint32_t cmd;		/* type of load command */
	uint32_t cmdsize;	/* total size of command in bytes */
};

Fields

  • cmd

    An integer indicating the type of load command. Table 4 lists the valid load command types.

  • cmdsize

    An integer specifying the total size in bytes of the load command data structure. Each load command structure contains a different set of data, depending on the load command type, so each might have a different size. In 32-bit architectures, the size must always be a multiple of 4; in 64-bit architectures, the size must always be a multiple of 8. If the load command data does not divide evenly by 4 or 8 (depending on whether the target architecture is 32-bit or 64-bit, respectively), add bytes containing zeros to the end until it does.

    一个整数,指定加载命令数据结构的总大小(字节)。每个加载命令结构都包含不同的数据集,具体取决于加载命令类型,因此每个数据集的大小可能不同。在32位体系结构中,大小必须始终是4的倍数;在64位体系结构中,大小必须始终是8的倍数。如果加载命令数据没有被4或8等分(分别取决于目标体系结构是32位还是64位),请在末尾添加包含零的字节,直到它等分为止。

Discussion

Table 4 lists the valid load command types, with links to the full data structures for each type.

表4列出了有效的加载命令类型,每种类型都有指向完整数据结构的链接。

Table 4 Mach-O load commands

CommandsData StructuresPurpose
LC_UUIDuuid_commandSpecifies the 128-bit UUID for an image
or its corresponding dSYM file.
为image或其相应的dSYM文件指定128位uuid
LC_SEGMENTsegment_commandDefines a segment of this file to be
mapped into the address space of the
process that loads this file. It also includes
all the sections contained by the segment.
定义要映射到加载此文件的进程的地址空间中的此文件段。它还包括该段所包含的所有部分。
LC_SEGMENT_64segment_command_64Defines a 64-bit segment of this file to be
mapped into the address space of the
process that loads this file. It also includes
all the sections contained by the segment.
LC_SYMTABsymtab_commandSpecifies the symbol table for this file.
This information is used by both static
and dynamic linkers when linking the
file, and also by debuggers to map
symbols to the original source code files
from which the symbols were generated.
指定此文件的符号表。静态和动态链接器在链接文件时都会使用此信息,调试器也会使用此信息将符号映射到生成符号的原始源代码文件。
LC_DYSYMTABdysymtab_commandSpecifies additional symbol table
information used by the dynamic linker.
指定动态链接器使用的其他符号表信息。
LC_THREAD LC_UNIXTHREADthread_commandFor an executable file, the LC_UNIXTHREAD
command defines the initial thread state
of the main thread of the process.
LC_THREAD is similar to LC_UNIXTHREAD
but does not cause the kernel to allocate
a stack.
对于可执行文件,LC_UNIXTHREAD命令定义进程主线程的初始线程状态。LC_THREAD类似于LC_UNIXTHREAD,但不会导致内核分配堆栈。
LC_LOAD_DYLIBdylib_commandDefines the name of a dynamic shared
library that this file links against.
定义此文件链接所针对的动态共享库的名称。
LC_ID_DYLIBdylib_commandSpecifies the install name of a dynamic
shared library.
指定动态共享库的安装名称。
LC_PREBOUND_DYLIBprebound_dylib_commandFor a shared library that this executable
is linked prebound against, specifies the
modules in the shared library that are
used.
对于此可执行文件链接到的共享库,请指定共享库中使用的模块。
LC_LOAD_DYLINKERdylinker_commandSpecifies the dynamic linker that the
kernel executes to load this file.
指定内核执行以加载此文件的动态链接器。
LC_ID_DYLINKERdylinker_commandIdentifies this file as a dynamic linker.
将此文件标识为动态链接器。
LC_ROUTINESroutines_commandContains the address of the shared library
initialization routine (specified by the
linker’s -init option).
包含共享库初始化例程的地址(由链接器的-init选项指定)。
LC_ROUTINES_64routines_command_64Contains the address of the shared library
64-bit initialization routine (specified by
the linker’s -init option).
LC_TWOLEVEL_HINTStwolevel_hints_commandContains the two-level namespace lookup
hint table.
包含两级命名空间查找提示表。
LC_SUB_FRAMEWORKsub_framework_commandIdentifies this file as the implementation
of a subframework of an umbrella
framework. The name of the umbrella
framework is stored in the string
parameter.
将此文件标识为伞形框架的子框架的实现。伞形框架的名称存储在字符串参数中。
LC_SUB_UMBRELLAsub_umbrella_commandSpecifies a file that is a subumbrella of
this umbrella framework.
指定作为此伞形框架的子伞形结构的文件。
LC_SUB_LIBRARYsub_library_commandIdentifies this file as the implementation
of a sublibrary of an umbrella framework.
The name of the umbrella framework is
stored in the string parameter. Note that
Apple has not defined a supported
location for sublibraries.
将此文件标识为伞形框架的子库的实现。伞形框架的名称存储在字符串参数中。请注意,Apple尚未为子库定义支持的位置。
LC_SUB_CLIENTsub_client_commandA subframework can explicitly allow
another framework or bundle to link
against it by including an LC_SUB_CLIENT
load command containing the name of
the framework or a client name for a
bundle.
子框架可以通过包含lc_sub_client load命令显式地允许另一个框架或捆绑包与之链接,该命令包含框架的名称或捆绑包的客户端名称。
  • uuid_command

    Specifies the 128-bit universally unique identifier (UUID) for an image or for its corresponding dSYM file.

    为image或其相应的dSYM文件指定128位uuid

    /*
     * The uuid load command contains a single 128-bit unique random number that
     * identifies an object produced by the static link editor.
     */
    struct uuid_command {
        uint32_t	cmd;		/* LC_UUID */
        uint32_t	cmdsize;	/* sizeof(struct uuid_command) */
        uint8_t	uuid[16];	/* the 128-bit uuid */
    };
    

    Fields

    • cmd Set to LC_UUID for this structure.

    • cmdsize Set to sizeof(uuid_command).

    • uuid

      ​ 128-bit unique identifier

    Declared In

    /usr/include/mach-o/loader.h

  • segment_command

    Specifies the range of bytes in a 32-bit Mach-O file that make up a segment. Those bytes are mapped by the loader into the address space of a program. Declared in /usr/include/mach-o/loader.h.

    指定组成段的32位mach-o文件中的字节范围。这些字节由加载程序映射到程序的地址空间中

    struct segment_command { /* for 32-bit architectures */
    	uint32_t	cmd;		/* LC_SEGMENT */
    	uint32_t	cmdsize;	/* includes sizeof section structs */
    	char		segname[16];	/* segment name */
    	uint32_t	vmaddr;		/* memory address of this segment */
    	uint32_t	vmsize;		/* memory size of this segment */
    	uint32_t	fileoff;	/* file offset of this segment */
    	uint32_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 */
    };
    
    /*
     * 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;		/* 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 */
    };
    

    Fields

    • cmd

      Common to all load command structures. Set to LC_SEGMENT for this structure.

    • cmdsize

      Common to all load command structures. For this structure, set this field to sizeof(segment_command) plus the size of all the section data structures that follow (sizeof(segment_command + (sizeof(section) * segment->nsect))).

    • segname

      A C string specifying the name of the segment. The value of this field can be any sequence of ASCII characters, although segment names defined by Apple begin with two underscores and consist of capital letters (as in ___TEXT and ___DATA). This field is fixed at 16 bytes in length.

      指定段名称的C字符串。此字段的值可以是任意一个ASCII字符序列,尽管Apple定义的段名称以两个下划线开头,并由大写字母组成(如在“_______TEXT”和"_____DATA"中)。此字段的长度固定为16字节。

    • vmaddr

      Indicates the starting virtual memory address of this segment.

      指示此段的起始虚拟内存地址。

    • vmsize

      Indicates the number of bytes of virtual memory occupied by this segment. See also the description of filesize, below.

      指示此段占用的虚拟内存字节数。另请参见下面的文件大小说明。

    • fileoff

      Indicates the offset in this file of the data to be mapped at vmaddr.

      指示此文件中要在vmaddr映射的数据的偏移量。

    • filesize

      Indicates the number of bytes occupied by this segment on disk. For segments that require more memory at runtime than they do at build time, vmsize can be larger than filesize. For example, the __ PAGEZERO segment generated by the linker for MH_EXECUTABLE files has a vmsize of 0x1000 but a filesize of 0. Because _ PAGEZERO contains no data, there is no need for it to occupy any space until runtime. Also, the static linker often allocates uninitialized data at the end of the __DATA segment; in this case, the vmsize is larger than the filesize. The loader guarantees that any memory of this sort is initialized with zeros.

      指示磁盘上此段占用的字节数。对于运行时比构建时需要更多内存的段,vmsize可以大于filesize。例如,链接器为MH_EXECUTABLE文件生成的u pagezero段的vmsize为0x1000,而filesize为0。因为pagezero不包含任何数据,所以在运行之前不需要占用任何空间。此外,静态链接器通常在数据段末尾分配未初始化的数据;在这种情况下,vmsize大于filesize。加载程序保证这类内存都是用零初始化的。

    • maxprot

      Specifies the maximum permitted virtual memory protections of this segment.

      指定此段允许的最大虚拟内存保护。

    • initprot

      Specifies the initial virtual memory protections of this segment.

      指定此段的初始虚拟内存保护。

    • nsects

      Indicates the number of section data structures following this load command.

      指示此load commands命令下的section数量

    • flags

      Defines a set of flags that affect the loading of this segment:

      • SG_HIGHVM—The file contents for this segment are for the high part of the virtual memory space; the low part is zero filled (for stacks in core files).
      • SG_NORELOC—This segment has nothing that was relocated in it and nothing relocated to it. It may be safely replaced without relocation.
      • 定义一组影响此段加载的标志: SG_HIGHVM—此段的文件内容用于虚拟内存空间的高部分;低部分为零填充(用于核心文件中的堆栈)。 SG_NORELOC-该段没有重新安置的任何东西,也没有重新安置的任何东西。可安全更换,无需重新安置。
  • section

    Defines the elements used by a 32-bit section. Directly following a segment_command data structure is an array of section data structures, with the exact count determined by the nsects field of the segment_command (page 20) structure. Declared in /usr/include/mach-o/loader.h.

    定义32位节使用的元素。段命令数据结构的正后方是一个段数据结构数组,其精确计数由段命令(第20页)结构的nsects字段确定。在/usr/include/mach-o/loader.h中声明。

    struct section { /* for 32-bit architectures */
    	char		sectname[16];	/* name of this section */
    	char		segname[16];	/* segment this section goes in */
    	uint32_t	addr;		/* memory address of this section */
    	uint32_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) */
    };
    
    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 */
    };
    

    Fields

    • sectname

      A string specifying the name of this section. The value of this field can be any sequence of ASCII characters, although section names defined by Apple begin with two underscores and consist of lowercase letters (as in __text and __data). This field is fixed at 16 bytes in length.

      指定此节名称的字符串。此字段的值可以是任意一个ASCII字符序列,尽管Apple定义的节名称以两个下划线开头,并且由小写字母组成(如文本和数据)。此字段的长度固定为16字节。

    • segname

      A string specifying the name of the segment that should eventually contain this section. For compactness, intermediate object files—files of type MH_OBJECT—contain only one segment, in which all sections are placed. The static linker places each section in the named segment when building the final product (any file that is not of type MH_OBJECT).

      指定最终应包含此节的段的名称的字符串。对于紧凑性,MH_OBJECT类型的中间对象文件只包含一个段,其中放置了所有的段。静态链接器在生成最终产品(任何不属于MH_OBJECT类型的文件)时将每个部分放置在命名段中。

    • addr

      An integer specifying the virtual memory address of this section.

      指定此节的虚拟内存地址的整数。

    • size

      An integer specifying the size in bytes of the virtual memory occupied by this section.

      一个整数,指定此节占用的虚拟内存的字节大小。

    • offset

      An integer specifying the offset to this section in the file.

      一个整数,指定文件中此节的偏移量。

    • align

      An integer specifying the section’s byte alignment. Specify this as a power of two; for example, a section with 8-byte alignment would have an align value of 3 (2 to the 3rd power equals 8).

      指定节字节对齐方式的整数。将其指定为2的幂;例如,具有8字节对齐的节的对齐值为3(2到3的幂等于8)。

    • reloff

      An integer specifying the file offset of the first relocation entry for this section.

      一个整数,指定此节的第一个重定位项的文件偏移量。

    • nreloc

      An integer specifying the number of relocation entries located at reloff for this section.

      一个整数,指定位于此节的reloff处的重定位条目数。

    • flags

      An integer divided into two parts. The least significant 8 bits contain the section type, while the most significant 24 bits contain a set of flags that specify other attributes of the section. These types and flags are primarily used by the static linker and file analysis tools, such as otool, to determine how to modify or display the section. These are the possible types:

      分成两部分的整数。最低有效8位包含节类型,而最高有效24位包含指定节的其他属性的标志集。这些类型和标志主要由静态链接器和文件分析工具(如otool)使用,以确定如何修改或显示节。以下是可能的类型:

      • S_REGULAR

        This section has no particular type. The standard tools create a __TEXT,__text section of this type.

        此节没有特定类型。标准工具会创建此类型的文本节。

      • S_ZEROFILL

        Zero-fill-on-demand section—when this section is first read from or written to, each page within is automatically filled with bytes containing zero.

        零按需填充节第一次读取或写入此节时,其中的每一页都会自动填充包含零的字节。

      • S_CSTRING_LITERALS

        This section contains only constant C strings. The standard tools create a __TEXT,__cstring section of this type.

        这个section仅包含C常量字符串。标准工具在此type下创建了__TEXT, __string

      • S_4BYTE_LITERALS

        This section contains only constant values that are 4 bytes long. The standard tools create a __TEXT,__literal4 section of this type.

      • S_8BYTE_LITERALS

        This section contains only constant values that are 8 bytes long. The standard tools create a __TEXT,__literal8 section of this type.

      • S_LITERAL_POINTERS

        This section contains only pointers to constant values.

        本节仅包含指向常量值的指针。

      • S_NON_LAZY_SYMBOL_POINTERS

        This section contains only non-lazy pointers to symbols.

        The standard tools create a section of the __DATA,__nl_symbol_ptrs section of this type.

        此部分仅包含指向符号的非懒加载指针。

      • S_LAZY_SYMBOL_POINTERS

        This section contains only lazy pointers to symbols. The

        standard tools create a __DATA,__la_symbol_ptrs section of this type.

        此部分仅包含指向符号的懒加载指针。

      • S_SYMBOL_STUBS

        This section contains symbol stubs. The standard tools create __TEXT,__symbol_stub and __TEXT,__picsymbol_stub sections of this type. See “Dynamic Code Generation” in Mach-O Programming Topics for more information.

        本节包含符号存根

      • S_MOD_INIT_FUNC_POINTERS

        This section contains pointers to module initialization functions. The standard tools create __DATA,__mod_init_func sections of this type.

        包含模块初始化方法的指针

      • S_MOD_TERM_FUNC_POINTERS

        This section contains pointers to module termination functions. The standard tools create __DATA,__mod_term_func sections of this type.

        包含模块结束方法的指针

      • S_COALESCED

        This section contains symbols that are coalesced by the static linker and possibly the dynamic linker. More than one file may contain coalesced definitions of the same symbol without causing multiple-defined-symbol errors.

        此部分包含由静态链接器(可能还有动态链接器)合并的符号。多个文件可能包含同一符号的合并定义,但不会导致多个定义的符号错误。

      • S_GB_ZEROFILL

        This is a zero-filled on-demand section. It can be larger than 4 GB. This section must be placed in a segment containing only zero-filled sections. If you place a zero-filled section in a segment with non–zero-filled sections, you may cause those sections to be unreachable with a 31-bit offset. That outcome stems from the fact that the size of a zero-filled section can be larger than 4 GB (in a 32-bit address space). As a result of this, the static linker would be unable to build the output file. See segment_command (page 20) for more information.

        这是一个零填充的按需部分。它可以大于4 GB。此节必须放在只包含零填充节的段中。如果将零填充部分放在具有非零填充部分的段中,则可能会导致以31位偏移量无法访问这些部分。这一结果源于这样一个事实:零填充部分的大小可以大于4GB(在32位地址空间中)。因此,静态链接器将无法生成输出文件。有关更多信息,请参阅StEngSub命令(第20页)。

      The following are the possible attributes of a section:

      • S_ATTR_PURE_INSTRUCTIONS

        This section contains only executable machine instructions. The standard tools set this flag for the sections __TEXT,__text, __TEXT,__symbol_stub, and __TEXT,__picsymbol_stub.

        本节仅包含可执行的机器指令。

      • S_ATTR_SOME_INSTRUCTIONS

        This section contains executable machine instructions.

      • S_ATTR_NO_TOC

        This section contains coalesced symbols that must not be placed in the

        table of contents (SYMDEF member) of a static archive library.

      • S_ATTR_EXT_RELOC

        This section contains references that must be relocated. These references refer to data that exists in other files (undefined symbols). To support external relocation, the maximum virtual memory protections of the segment that contains this section must allow both reading and writing.

        本节包含必须重新定位的引用。这些引用引用其他文件中存在的数据(未定义的符号)。为了支持外部重定位,包含此节的段的最大虚拟内存保护必须允许读写。

      • S_ATTR_LOC_RELOC

        This section contains references that must be relocated. These references refer to data within this file.

        本节包含必须重新定位的引用。这些引用引用此文件中的数据。

      • S_ATTR_STRIP_STATIC_SYMS

        The static symbols in this section can be stripped if the MH_DYLDLINK flag of the image’s mach_header (page 12) header structure is set.

      • S_ATTR_NO_DEAD_STRIP

        This section must not be dead-stripped. See “Linking” in Xcode User Guide for details.

      • S_ATTR_LIVE_SUPPORT

        This section must not be dead-stripped if they reference code that is live, but the reference is undetectable.

    • reserved1

      An integer reserved for use with certain section types. For symbol pointer sections and symbol stubs sections that refer to indirect symbol table entries, this is the index into the indirect table for this section’s entries. The number of entries is based on the section size divided by the size of the symbol pointer or stub. Otherwise, this field is set to 0.

      保留用于某些节类型的整数。对于引用间接符号表项的符号指针节和符号存根节,这是指向该节项的间接表的索引。条目的数目基于节大小除以符号指针或存根的大小。否则,此字段设置为0。

    • reserved2

      For sections of type S_SYMBOL_STUBS, an integer specifying the size (in bytes) of the symbol

      stub entries contained in the section. Otherwise, this field is reserved for future use and should be set to 0.

      对于S_SYMBOL_STUBS类型的section,使用整数指定符号的大小(以字节为单位) section中包含的stub条目。否则,此字段将保留以供将来使用,并应设置为0。

    Discussion

    Each section in a Mach-O file has both a type and a set of attribute flags. In intermediate object files, the type and attributes determine how the static linker copies the sections into the final product. Object file analysis tools (such as otool) use the type and attributes to determine how to read and display the sections. Some section types and attributes are used by the dynamic linker.

    These are important static-linking variants of the symbol type and attributes:

    mach-o文件中的每个部分都有一个类型和一组属性标志。在中间对象文件中,类型和属性决定静态链接器如何将节复制到最终产品中。对象文件分析工具(如otool)使用类型和属性来确定如何读取和显示节。动态链接器使用某些节类型和属性。 这些是符号类型和属性的重要静态链接变体:

    • Regular sections. In a regular section, only one definition of an external symbol may exist in intermediate object files. The static linker returns an error if it finds any duplicate external symbol definitions.

      在常规部分中,中间对象文件中只能存在外部符号的一个定义。如果发现任何重复的外部符号定义,静态链接器将返回错误。

    • Coalesced sections. In the final product, the static linker retains only one instance of each symbol defined in coalesced sections. To support complex language features (such as C++ vtables and RTTI) the compiler may create a definition of a particular symbol in every intermediate object file. The static linker and the dynamic linker would then reduce the duplicate definitions to the single definition used by the program.

      在最终产品中,静态链接器只保留合并部分中定义的每个符号的一个实例。为了支持复杂的语言特性(如C++ VTABLE和RTTI),编译器可以在每个中间对象文件中创建一个特定符号的定义。静态链接器和动态链接器将把重复的定义减少到程序使用的单个定义。

    • Coalesced sections with weak definitions Weak symbol definitions may appear only in coalesced sections. When the static linker finds duplicate definitions for a symbol, it discards any coalesced symbol definition that has the weak definition attribute set (see nlist (page 39)). If there are no non-weak definitions, the first weak definition is used instead. This feature is designed to support C++ templates; it allows explicit template instantiations to override implicit ones. The C++ compiler places explicit definitions in a regular section, and it places implicit definitions in a coalesced section, marked as weak definitions. Intermediate object files (and thus static archive libraries) built with weak definitions can be used only with the static linker in Mac OS X v10.2 and later. Final products (applications and shared libraries) should not contain weak definitions if they are expected to be used on earlier versions of Mac OS X.

      弱符号定义只能出现在合并部分中。当静态链接器发现符号的重复定义时,它将丢弃任何具有弱定义属性集的合并符号定义(请参阅nlist(第39页))。如果没有非弱定义,则使用第一个弱定义。该特性被设计为支持C++模板;它允许显式模板实例化来重写隐式模板。C++编译器将显式定义放在一个常规的部分中,它将隐含的定义放在一个合并的区段中,标记为弱定义。使用弱定义构建的中间对象文件(以及静态存档库)只能与Mac OS X v10.2及更高版本中的静态链接器一起使用。如果最终产品(应用程序和共享库)预期在早期版本的MacOSX上使用,则不应包含弱定义。

  • twolevel_hints_command

    Defines the attributes of a LC_TWOLEVEL_HINTS load command

    /*
     * The twolevel_hints_command contains the offset and number of hints in the
     * two-level namespace lookup hints table.
     */
    struct twolevel_hints_command {
        uint32_t cmd;	/* LC_TWOLEVEL_HINTS */
        uint32_t cmdsize;	/* sizeof(struct twolevel_hints_command) */
        uint32_t offset;	/* offset to the hint table */
        uint32_t nhints;	/* number of hints in the hint table */
    };
    

    Fields

    • cmd

      Common to all load command structures. Set to LC_TWOLEVEL_HINTS for this structure.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(twolevel_hints_command).

    • offset

      An integer specifying the byte offset from the start of this file to an array of

      twolevel_hint (page 30) data structures, known as the two-level namespace hint table.

      在文件内的offset

    • nhints

      The number of twolevel_hint data structures located at offset.

      位于偏移量的二级提示数据结构的数目。

    Discussion

    The static linker adds the LC_TWOLEVEL_HINTS load command and the two-level namespace hint table to the output file when building a two-level namespace image.

    讨论 静态链接器在生成两级命名空间映像时将lc_twowlevel_hints load命令和两级命名空间提示表添加到输出文件中。

    Special Considerations By default, ld does not include the LC_TWOLEVEL_HINTS command or the two-level namespace hint table in an MH_BUNDLE file because the presence of this load command causes the version of the dynamic linker shipped with Mac OS X v10.0 to crash. If you know the code will run only on Mac OS X v10.1 and later, you should explicitly enable the two-level namespace hint table. See -twolevel_namespace_hints in the ld man page for more information.

    特殊注意事项 默认情况下,ld不会在MH_BUNDLE文件中包含lc_twolevel_hint命令或两级名称空间提示表,因为这个load命令的存在会导致Mac OS X v10.0附带的动态链接器版本崩溃。如果您知道代码只在Mac OS X v10.1及更高版本上运行,那么应该显式启用两级名称空间提示表。有关更多信息,请参见ld手册页中的- twolevel_namespace_tips。

  • twolevel_hint

    Specifies an entry in the two-level namespace hint table. Declared in /usr/include/mach-o/loader.h.

    struct twolevel_hint {
        uint32_t 
    	isub_image:8,	/* index into the sub images */
    	itoc:24;	/* index into the table of contents */
    };
    

    Fields

    • isub_image

      The subimage in which the symbol is defined. It is an index into the list of images that make up the umbrella image. If this field is 0, the symbol is in the umbrella image itself. If the image is not an umbrella framework or library, this field is 0.

      定义符号的subimage。它是组成雨伞图像的图像列表的索引。如果此字段为0,则符号在伞图像本身中。如果图像不是伞形框架或库,则此字段为0。

    • itoc

      The symbol index into the table of contents of the image specified by the isub_image field.

      由isub_image字段指定的图像目录中的符号索引。

    Discussion

    The two-level namespace hint table provides the dynamic linker with suggested positions to start searching for symbols in the libraries the current image is linked against.

    Every undefined symbol (that is, every symbol of type N_UNDF or N_PBUD) in a two-level namespace image has a corresponding entry in the two-level hint table, at the same index.

    The static linker adds the LC_TWOLEVEL_HINTS load command and the two-level namespace hint table to the output file when building a two-level namespace image.

    By default, the linker does not include the LC_TWOLEVEL_HINTS command or the two-level namespace hint table in an MH_BUNDLE file, because the presence of this load command causes the version of the dynamic linker shipped with Mac OS X v10.0 to crash. If you know the code will run only on Mac OS X v10.1 and later, you should explicitly enable the two-level namespace hints. See the linker documentation for more information. 两级命名空间提示表为动态链接器提供建议的位置,以便开始在当前图像所链接的库中搜索符号。 两级命名空间映像中的每个未定义符号(即,类型为n_undf或n_pbud的每个符号)在两级提示表中的同一索引处都有相应的项。 静态链接器在生成两级命名空间映像时将lc_twowlevel_hints load命令和两级命名空间提示表添加到输出文件中。 默认情况下,链接器不包括mh_捆绑包文件中的lc_two level_hints命令或两级命名空间提示表,因为此加载命令的存在会导致mac os x v10.0附带的动态链接器版本崩溃。如果您知道代码将只在macosxv10.1及更高版本上运行,那么应该显式地启用两级命名空间提示。有关详细信息,请参阅链接器文档。

  • lc_str

    Defines a variable-length string. Declared in /usr/include/mach-o/loader.h.

    union lc_str {
    	uint32_t	offset;	/* offset to the string */
    #ifndef __LP64__
    	char		*ptr;	/* pointer to the string */
    #endif 
    };
    

    Fields

    • offset

      A long integer. A byte offset from the start of the load command that contains this string to the start of the string data.

      一个长整数。从包含此字符串的加载命令开始到字符串数据开始的字节偏移量。

    • ptr

      A pointer to an array of bytes. At runtime, this pointer contains the virtual memory address of the string data. The ptr field is not used in Mach-O files.

      指向字节数组的指针。在运行时,此指针包含字符串数据的虚拟内存地址。在mach-o文件中不使用ptr字段。

    Discussion

    Load commands store variable-length data such as library names using the lc_str data structure. Unless otherwise specified, the data consists of a C string.

    The data pointed to is stored just after the load command, and the size is added to the size of the load command. The string should be null terminated; any extra bytes to round up the size should be null. You can also determine the size of the string by subtracting the size of the load command data structure from the cmdsize field of the load command data structure. Load Commands使用lc_str数据结构存储可变长度的数据,例如库名称。除非另有说明,否则数据由C字符串组成。 指向的数据存储在load command之后,大小将添加到load command的大小中。字符串应以空结尾;要舍入大小的任何额外字节都应为空。还可以通过从加载命令数据结构的cmdSize字段中减去加载命令数据结构的大小来确定字符串的大小。

  • dylib

    Defines the data used by the dynamic linker to match a shared library against the files that have linked to it. Used exclusively in the dylib_command (page 32) data structure. Declared in /usr/include/mach-o/loader.h.

    定义动态链接器用于将共享库与链接到该库的文件匹配的数据

    /*
     * Dynamicly linked shared libraries are identified by two things.  The
     * pathname (the name of the library as found for execution), and the
     * compatibility version number.  The pathname must match and the compatibility
     * number in the user of the library must be greater than or equal to the
     * library being used.  The time stamp is used to record the time a library was
     * built and copied into user so it can be use to determined if the library used
     * at runtime is exactly the same as used to built the program.
     */
    struct dylib {
        union lc_str  name;			/* library's path name */
        uint32_t timestamp;			/* library's build time stamp */
        uint32_t current_version;		/* library's current version number */
        uint32_t compatibility_version;	/* library's compatibility vers number*/
    };
    

    Fields

    • name

      A data structure of type lc_str (page 31). Specifies the name of the shared library.

      指定共享库的名称 是一个lc_str的结构类型

    • timestamp

      The date and time when the shared library was built.

      创建共享库的日期和时间。

    • current_version

      The current version of the shared library.

      共享库的当前版本

    • compatibility_version

      The compatibility version of the shared library.

      共享库的兼容版本

  • dylib_command

    Defines the attributes of the LC_LOAD_DYLIB and LC_ID_DYLIB load commands. Declared in /usr/include/mach-o/loader.h.

    /*
     * A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
     * contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
     * An object that uses a dynamically linked shared library also contains a
     * dylib_command (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
     * LC_REEXPORT_DYLIB) for each library it uses.
     */
    struct dylib_command {
    	uint32_t	cmd;		/* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
    					   LC_REEXPORT_DYLIB */
    	uint32_t	cmdsize;	/* includes pathname string */
    	struct dylib	dylib;		/* the library identification */
    };
    

    Fields

    • cmd

      Common to all load command structures. For this structure, set to either LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or LC_ID_DYLIB.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(dylib_command) plus the size of the data pointed to by the name field of the dylib field.

    • dylib

      A data structure of type dylib (page 31). Specifies the attributes of the shared library.

      dylib类型的数据结构。指定共享库的属性。

    Discussion

    For each shared library that a file links against, the static linker creates an LC_LOAD_DYLIB command and sets its dylib field to the value of the dylib field of the LC_ID_DYLD load command of the target library. All the LC_LOAD_DYLIB commands together form a list that is ordered according to location in the file, earliest LC_LOAD_DYLIB command first. For two-level namespace files, undefined symbol entries in the symbol table refer to their parent shared libraries by index into this list. The index is called a library ordinal, and it is stored in the n_desc field of the nlist (page 39) data structure. 对于文件链接所针对的每个共享库,静态链接器创建一个LC_LOAD_DYLIB命令,并将其dylib字段设置为目标库的LC_ID_DYLD load commands的dylib字段的值。所有LC_LOAD_DYLIB命令一起形成一个列表,该列表根据文件中的位置进行排序,首先是最早的LC_LOAD_DYLIB命令。对于两级命名空间文件,符号表中未定义的符号项通过索引指向该列表中的父共享库。索引称为库序号,它存储在nlist数据结构的n_desc字段中。

    At runtime, the dynamic linker uses the name in the dyld field of the LC_LOAD_DYLIB command to locate the shared library. If it finds the library, the dynamic linker compares the version information of the LC_LOAD_DYLIB load command against the library’s version. For the dynamic linker to successfully link the shared library, the compatibility version of the shared library must be less than or equal to the compatibility version in the LC_LOAD_DYLIB command.

    在运行时,动态链接器使用LC_LOAD_DYLIB命令的dyld字段中的名称来定位共享库。如果找到库,动态链接器将LC_LOAD_DYLIB load commands的版本信息与库的版本进行比较。要使动态链接器成功链接共享库,共享库的兼容版本必须小于或等于LC_LOAD_DYLIB命令中的兼容版本。

    The dynamic linker uses the timestamp to determine whether it can use the prebinding information. The current version is returned by the function NSVersionOfRunTimeLibrary to allow you to determine the version of the library your program is using.

    动态链接器使用时间戳来确定是否可以使用预绑定信息。函数NSVersionOfRunTimeLibrary返回当前版本,允许您确定程序正在使用的库的版本。

  • dylinker_command

    Defines the attributes of the LC_LOAD_DYLINKER and LC_ID_DYLINKER load commands. Declared in /usr/include/mach-o/loader.h.

    /*
     * A program that uses a dynamic linker contains a dylinker_command to identify
     * the name of the dynamic linker (LC_LOAD_DYLINKER).  And a dynamic linker
     * contains a dylinker_command to identify the dynamic linker (LC_ID_DYLINKER).
     * A file can have at most one of these.
     * This struct is also used for the LC_DYLD_ENVIRONMENT load command and
     * contains string for dyld to treat like environment variable.
     */
    struct dylinker_command {
    	uint32_t	cmd;		/* LC_ID_DYLINKER, LC_LOAD_DYLINKER or
    					   LC_DYLD_ENVIRONMENT */
    	uint32_t	cmdsize;	/* includes pathname string */
    	union lc_str    name;		/* dynamic linker's path name */
    };
    

    Fields

    • cmd

      Common to all load command structures. For this structure, set to either LC_ID_DYLINKER or LC_LOAD_DYLINKER.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(dylinker_command), plus the size of the data pointed to by the name field.

    • name

      A data structure of type lc_str (page 31). Specifies the name of the dynamic linker

    Discussion

    Every executable file that is dynamically linked contains a LC_LOAD_DYLINKER command that specifies the name of the dynamic linker that the kernel must load in order to execute the file. The dynamic linker itself specifies its name using the LC_ID_DYLINKER load command.

    每个动态链接的可执行文件都包含一个LC_LOAD_DYLINKER命令,该命令指定内核为了执行文件必须加载的动态链接器的名称。动态链接器本身使用LC_ID_DYLINKER load command指定其名称。

  • prebound_dylib_command

    Defines the attributes of the LC_PREBOUND_DYLIB load command. For every library that a prebound executable file links to, the static linker adds one LC_PREBOUND_DYLIB command. Declared in /usr/include/mach-o/loader.h.

    /*
     * A program (filetype == MH_EXECUTE) that is
     * prebound to its dynamic libraries has one of these for each library that
     * the static linker used in prebinding.  It contains a bit vector for the
     * modules in the library.  The bits indicate which modules are bound (1) and
     * which are not (0) from the library.  The bit for module 0 is the low bit
     * of the first byte.  So the bit for the Nth module is:
     * (linked_modules[N/8] >> N%8) & 1
     */
    struct prebound_dylib_command {
    	uint32_t	cmd;		/* LC_PREBOUND_DYLIB */
    	uint32_t	cmdsize;	/* includes strings */
    	union lc_str	name;		/* library's path name */
    	uint32_t	nmodules;	/* number of modules in library */
    	union lc_str	linked_modules;	/* bit vector of linked modules */
    };
    

    Fields

    • cmd

      Common to all load command structures. For this structure, set to LC_PREBOUND_DYLIB.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(prebound_dylib_command) plus the size of the data pointed to by the name and linked_modules fields.

    • name

      A data structure of type lc_str (page 31). Specifies the name of the prebound shared library.

      预绑定共享库的名称

    • nmodules

      An integer. Specifies the number of modules the prebound shared library contains. The size of the linked_modules string is (nmodules / 8) + (nmodules % 8).

      一个整数。指定预绑定共享库包含的模块数。链接的模块字符串的大小为(nmodules/8)+(nmodules%8)

    • linked_modules

      A data structure of type lc_str (page 31). Usually, this data structure defines the offset of a C string; in this usage, it is a variable-length bitset, containing one bit for each module. Each bit represents whether the corresponding module is linked to a module in the current file, 1 for yes, 0 for no. The bit for the first module is the low bit of the first byte

      lc_str类型的数据结构(第31页)。通常,此数据结构定义C字符串的偏移量;在这种用法中,它是一个可变长度的位集,每个模块包含一个位。每个位表示对应的模块是否链接到当前文件中的模块,1表示是,0表示否。第一个模块的位是第一个字节的低位

  • thread_command

    Defines the attributes of the LC_THREAD and LC_UNIXTHREAD load commands. The data of this command is specific to each architecture and appears in thread_status.h, located in the architecture’s directory in /usr/include/mach. Declared in /usr/include/mach-o/loader.h.

    struct thread_command {
    	uint32_t	cmd;		/* LC_THREAD or  LC_UNIXTHREAD */
    	uint32_t	cmdsize;	/* total size of this command */
    	/* uint32_t flavor		   flavor of thread state */
    	/* uint32_t count		   count of uint32_t's in thread state */
    	/* struct XXX_thread_state state   thread state for this flavor */
    	/* ... */
    };
    

    Fields

    • cmd

      Common to all load command structures. For this structure, set to LC_THREAD or LC_UNIXTHREAD.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(thread_command)

      plus the size of the flavor and count fields plus the size of the CPU-specific thread state data structure.

    • flavor

      Integer specifying the particular flavor of the thread state data structure. See the thread_status.h file for your target architecture.

      整数,指定线程状态数据结构的特殊类型。请参阅目标体系结构的thread_status.h文件。

    • count

      Size of the thread state data, in number of 32-bit integers. The thread state data structure must be fully padded to 32-bit alignment.

      线程状态数据的大小,以32位整数为单位。线程状态数据结构必须完全填充为32位对齐。

  • routines_command

    Defines the attributes of the LC_ROUTINES load command, used in 32-bit architectures. Describes the location of the shared library initialization function, which is a function that the dynamic linker calls before allowing any of the routines in the library to be called. Declared in /usr/include/mach-o/loader.h. See also routines_command_64

    定义32位体系结构中使用的LC_ROUTINES load commands的属性。描述共享库初始化函数的位置,该函数是动态链接器在允许调用库中的任何例程之前调用的函数。在/usr/include/mach-o/loader.h中声明。另请参见例程命令(第36页)。

    /*
     * The routines command contains the address of the dynamic shared library 
     * initialization routine and an index into the module table for the module
     * that defines the routine.  Before any modules are used from the library the
     * dynamic linker fully binds the module that defines the initialization routine
     * and then calls it.  This gets called before any module initialization
     * routines (used for C++ static constructors) in the library.
     */
    struct routines_command { /* for 32-bit architectures */
    	uint32_t	cmd;		/* LC_ROUTINES */
    	uint32_t	cmdsize;	/* total size of this command */
    	uint32_t	init_address;	/* address of initialization routine */
    	uint32_t	init_module;	/* index into the module table that */
    				        /*  the init routine is defined in */
    	uint32_t	reserved1;
    	uint32_t	reserved2;
    	uint32_t	reserved3;
    	uint32_t	reserved4;
    	uint32_t	reserved5;
    	uint32_t	reserved6;
    };
    
    /*
     * The 64-bit routines command.  Same use as above.
     */
    struct routines_command_64 { /* for 64-bit architectures */
    	uint32_t	cmd;		/* LC_ROUTINES_64 */
    	uint32_t	cmdsize;	/* total size of this command */
    	uint64_t	init_address;	/* address of initialization routine */
    	uint64_t	init_module;	/* index into the module table that */
    					/*  the init routine is defined in */
    	uint64_t	reserved1;
    	uint64_t	reserved2;
    	uint64_t	reserved3;
    	uint64_t	reserved4;
    	uint64_t	reserved5;
    	uint64_t	reserved6;
    };
    

    Fields

    • cmd

      Common to all load command structures. For this structure, set to LC_ROUTINES.

    • cmdsize

      Common to all load command structures. For this structure, set to sizeof(routines_command).

    • init_address

      An integer specifying the virtual memory address of the initialization function.

      指定初始化函数的虚拟内存地址的整数。

    • init_module

      An integer specifying the index into the module table of the module containing the initialization function.

      一个整数,指定包含初始化函数的模块的模块表中的索引。

    • reserved1

      Reserved for future use. Set this field to 0.

      保留以备将来使用。将此字段设置为0

    • reserved2

      Reserved for future use. Set this field to 0.

    • reserved3

      Reserved for future use. Set this field to 0.

    • reserved4

      Reserved for future use. Set this field to 0.

    • reserved5

      Reserved for future use. Set this field to 0.

    • reserved6

      Reserved for future use. Set this field to 0.

    Discussion

    The static linker adds an LC_ROUTINES command when you specify a shared library initialization function using the -init option (see the ld man page for more information). 使用-init选项指定共享库初始化函数时,静态链接器会添加LC_ROUTINES命令(有关详细信息,请参阅ld手册页)。

  • sub_framework_command

    Defines the attributes of the LC_SUB_FRAMEWORK load command. Identifies the umbrella framework of which this file is a subframework. Declared in /usr/include/mach-o/loader.h.

    /*
     * A dynamically linked shared library may be a subframework of an umbrella
     * framework.  If so it will be linked with "-umbrella umbrella_name" where
     * Where "umbrella_name" is the name of the umbrella framework. A subframework
     * can only be linked against by its umbrella framework or other subframeworks
     * that are part of the same umbrella framework.  Otherwise the static link
     * editor produces an error and states to link against the umbrella framework.
     * The name of the umbrella framework for subframeworks is recorded in the
     * following structure.
     */
    struct sub_framework_command {
    	uint32_t	cmd;		/* LC_SUB_FRAMEWORK */
    	uint32_t	cmdsize;	/* includes umbrella string */
    	union lc_str 	umbrella;	/* the umbrella framework name */
    };
    

    Fields

    • cmd

    • cmdsize

    • umbrella

      A data structure of type lc_str (page 31). Specifies the name of the umbrella framework of which this file is a member.

  • sub_umbrella_command

    Defines the attributes of the LC_SUB_UMBRELLA load command. Identifies the named framework as a subumbrella of this framework. Unlike a subframework, any client may link to a subumbrella. Declared in /usr/include/mach-o/loader.h.

    /*
     * A dynamically linked shared library may be a sub_umbrella of an umbrella
     * framework.  If so it will be linked with "-sub_umbrella umbrella_name" where
     * Where "umbrella_name" is the name of the sub_umbrella framework.  When
     * staticly linking when -twolevel_namespace is in effect a twolevel namespace 
     * umbrella framework will only cause its subframeworks and those frameworks
     * listed as sub_umbrella frameworks to be implicited linked in.  Any other
     * dependent dynamic libraries will not be linked it when -twolevel_namespace
     * is in effect.  The primary library recorded by the static linker when
     * resolving a symbol in these libraries will be the umbrella framework.
     * Zero or more sub_umbrella frameworks may be use by an umbrella framework.
     * The name of a sub_umbrella framework is recorded in the following structure.
     */
    struct sub_umbrella_command {
    	uint32_t	cmd;		/* LC_SUB_UMBRELLA */
    	uint32_t	cmdsize;	/* includes sub_umbrella string */
    	union lc_str 	sub_umbrella;	/* the sub_umbrella framework name */
    };
    

    Fields

    • cmd

    • cmdsize

    • sub_umbrella

      A data structure of type lc_str (page 31). Specifies the name of the umbrella framework of which this file is a member.

  • sub_library_command

    Defines the attributes of the LC_SUB_LIBRARY load command. Identifies a sublibrary of this framework and marks this framework as an umbrella framework. Unlike a subframework, any client may link to a sublibrary. Declared in /usr/include/mach-o/loader.h.

    /*
     * A dynamically linked shared library may be a sub_library of another shared
     * library.  If so it will be linked with "-sub_library library_name" where
     * Where "library_name" is the name of the sub_library shared library.  When
     * staticly linking when -twolevel_namespace is in effect a twolevel namespace 
     * shared library will only cause its subframeworks and those frameworks
     * listed as sub_umbrella frameworks and libraries listed as sub_libraries to
     * be implicited linked in.  Any other dependent dynamic libraries will not be
     * linked it when -twolevel_namespace is in effect.  The primary library
     * recorded by the static linker when resolving a symbol in these libraries
     * will be the umbrella framework (or dynamic library). Zero or more sub_library
     * shared libraries may be use by an umbrella framework or (or dynamic library).
     * The name of a sub_library framework is recorded in the following structure.
     * For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc".
     */
    struct sub_library_command {
    	uint32_t	cmd;		/* LC_SUB_LIBRARY */
    	uint32_t	cmdsize;	/* includes sub_library string */
    	union lc_str 	sub_library;	/* the sub_library name */
    };
    
  • sub_client_command

    Defines the attributes of the LC_SUB_CLIENT load command. Specifies the name of a file that is allowed to link to this subframework. This file would otherwise be required to link to the umbrella framework of which this file is a component. Declared in /usr/include/mach-o/loader.h.

    /*
     * For dynamically linked shared libraries that are subframework of an umbrella
     * framework they can allow clients other than the umbrella framework or other
     * subframeworks in the same umbrella framework.  To do this the subframework
     * is built with "-allowable_client client_name" and an LC_SUB_CLIENT load
     * command is created for each -allowable_client flag.  The client_name is
     * usually a framework name.  It can also be a name used for bundles clients
     * where the bundle is built with "-client_name client_name".
     */
    struct sub_client_command {
    	uint32_t	cmd;		/* LC_SUB_CLIENT */
    	uint32_t	cmdsize;	/* includes client string */
    	union lc_str 	client;		/* the client name */
    };
    

    Special Considerations

    The ld tool generates a sub_client_command load command in the built product if you pass the option -allowable_client , where is the install name of a framework or the client name of a bundle. See the ld man page, specifically about the options -allowable_client and -client_name, for more information.

    如果传递选项-allowable_client,其中是框架的安装名称或捆绑包的客户端名称,则ld工具会在生成的产品中生成一个sub_client_command load commands。有关详细信息,请参阅ld手册页,特别是关于选项-允许的客户机和-客户机名称。