home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / darwin / darwinx86.iso / usr / include / mach-o / loader.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-14  |  41.3 KB  |  895 lines

  1. /*
  2.  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  3.  *
  4.  * @APPLE_LICENSE_HEADER_START@
  5.  * 
  6.  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
  7.  * Reserved.  This file contains Original Code and/or Modifications of
  8.  * Original Code as defined in and that are subject to the Apple Public
  9.  * Source License Version 1.1 (the "License").  You may not use this file
  10.  * except in compliance with the License.  Please obtain a copy of the
  11.  * License at http://www.apple.com/publicsource and read it before using
  12.  * this file.
  13.  * 
  14.  * The Original Code and all software distributed under the License are
  15.  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16.  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
  19.  * License for the specific language governing rights and limitations
  20.  * under the License.
  21.  * 
  22.  * @APPLE_LICENSE_HEADER_END@
  23.  */
  24. #ifndef _MACHO_LOADER_H_
  25. #define _MACHO_LOADER_H_
  26.  
  27. /*
  28.  * This file describes the format of mach object files.
  29.  */
  30.  
  31. /*
  32.  * <mach/machine.h> is needed here for the cpu_type_t and cpu_subtype_t types
  33.  * and contains the constants for the possible values of these types.
  34.  */
  35. #import <mach/machine.h>
  36.  
  37. /*
  38.  * <mach/vm_prot.h> is needed here for the vm_prot_t type and contains the 
  39.  * constants that are or'ed together for the possible values of this type.
  40.  */
  41. #import <mach/vm_prot.h>
  42.  
  43. /*
  44.  * <machine/thread_status.h> is expected to define the flavors of the thread
  45.  * states and the structures of those flavors for each machine.
  46.  */
  47. #import <mach/machine/thread_status.h>
  48. #import <architecture/byte_order.h>
  49.  
  50. /*
  51.  * The mach header appears at the very beginning of the object file.
  52.  */
  53. struct mach_header {
  54.     unsigned long    magic;        /* mach magic number identifier */
  55.     cpu_type_t    cputype;    /* cpu specifier */
  56.     cpu_subtype_t    cpusubtype;    /* machine specifier */
  57.     unsigned long    filetype;    /* type of file */
  58.     unsigned long    ncmds;        /* number of load commands */
  59.     unsigned long    sizeofcmds;    /* the size of all the load commands */
  60.     unsigned long    flags;        /* flags */
  61. };
  62.  
  63. /* Constant for the magic field of the mach_header */
  64. #define    MH_MAGIC    0xfeedface    /* the mach magic number */
  65. #define MH_CIGAM    NXSwapInt(MH_MAGIC)
  66.  
  67. /*
  68.  * The layout of the file depends on the filetype.  For all but the MH_OBJECT
  69.  * file type the segments are padded out and aligned on a segment alignment
  70.  * boundary for efficient demand pageing.  The MH_EXECUTE, MH_FVMLIB, MH_DYLIB,
  71.  * MH_DYLINKER and MH_BUNDLE file types also have the headers included as part
  72.  * of their first segment.
  73.  * 
  74.  * The file type MH_OBJECT is a compact format intended as output of the
  75.  * assembler and input (and possibly output) of the link editor (the .o
  76.  * format).  All sections are in one unnamed segment with no segment padding. 
  77.  * This format is used as an executable format when the file is so small the
  78.  * segment padding greatly increases its size.
  79.  *
  80.  * The file type MH_PRELOAD is an executable format intended for things that
  81.  * are not executed under the kernel (proms, stand alones, kernels, etc).  The
  82.  * format can be executed under the kernel but may demand paged it and not
  83.  * preload it before execution.
  84.  *
  85.  * A core file is in MH_CORE format and can be any in an arbritray legal
  86.  * Mach-O file.
  87.  *
  88.  * Constants for the filetype field of the mach_header
  89.  */
  90. #define    MH_OBJECT    0x1        /* relocatable object file */
  91. #define    MH_EXECUTE    0x2        /* demand paged executable file */
  92. #define    MH_FVMLIB    0x3        /* fixed VM shared library file */
  93. #define    MH_CORE        0x4        /* core file */
  94. #define    MH_PRELOAD    0x5        /* preloaded executable file */
  95. #define    MH_DYLIB    0x6        /* dynamicly bound shared library file*/
  96. #define    MH_DYLINKER    0x7        /* dynamic link editor */
  97. #define    MH_BUNDLE    0x8        /* dynamicly bound bundle file */
  98.  
  99. /* Constants for the flags field of the mach_header */
  100. #define    MH_NOUNDEFS    0x1        /* the object file has no undefined
  101.                        references, can be executed */
  102. #define    MH_INCRLINK    0x2        /* the object file is the output of an
  103.                        incremental link against a base file
  104.                        and can't be link edited again */
  105. #define MH_DYLDLINK    0x4        /* the object file is input for the
  106.                        dynamic linker and can't be staticly
  107.                        link edited again */
  108. #define MH_BINDATLOAD    0x8        /* the object file's undefined
  109.                        references are bound by the dynamic
  110.                        linker when loaded. */
  111. #define MH_PREBOUND    0x10        /* the file has its dynamic undefined
  112.                        references prebound. */
  113. #define MH_SPLIT_SEGS    0x20        /* the file has its read-only and
  114.                        read-write segments split */
  115. #define MH_LAZY_INIT    0x40        /* the shared library init routine is
  116.                        to be run lazily via catching memory
  117.                        faults to its writeable segments */
  118. #define MH_TWOLEVEL    0x80        /* the image is using two-level name
  119.                        space bindings */
  120. #define MH_FORCE_FLAT    0x100        /* the executable is forcing all images
  121.                        to use flat name space bindings */
  122. #define MH_NOMULTIDEFS    0x200        /* this umbrella guarantees no multiple
  123.                        defintions of symbols in its
  124.                        sub-images so the two-level namespace
  125.                        hints can alwasys be used. */
  126. /*
  127.  * The load commands directly follow the mach_header.  The total size of all
  128.  * of the commands is given by the sizeofcmds field in the mach_header.  All
  129.  * load commands must have as their first two fields cmd and cmdsize.  The cmd
  130.  * field is filled in with a constant for that command type.  Each command type
  131.  * has a structure specifically for it.  The cmdsize field is the size in bytes
  132.  * of the particular load command structure plus anything that follows it that
  133.  * is a part of the load command (i.e. section structures, strings, etc.).  To
  134.  * advance to the next load command the cmdsize can be added to the offset or
  135.  * pointer of the current load command.  The cmdsize MUST be a multiple of
  136.  * sizeof(long) (this is forever the maximum alignment of any load commands).
  137.  * The padded bytes must be zero.  All tables in the object file must also
  138.  * follow these rules so the file can be memory mapped.  Otherwise the pointers
  139.  * to these tables will not work well or at all on some machines.  With all
  140.  * padding zeroed like objects will compare byte for byte.
  141.  */
  142. struct load_command {
  143.     unsigned long cmd;        /* type of load command */
  144.     unsigned long cmdsize;        /* total size of command in bytes */
  145. };
  146.  
  147. /*
  148.  * After MacOS X 10.1 when a new load command is added that is required to be
  149.  * understood by the dynamic linker for the image to execute properly the
  150.  * LC_REQ_DYLD bit will be or'ed into the load command constant.  If the dynamic
  151.  * linker sees such a load command it it does not understand will issue a
  152.  * "unknown load command required for execution" error and refuse to use the
  153.  * image.  Other load commands without this bit that are not understood will
  154.  * simply be ignored.
  155.  */
  156. #define LC_REQ_DYLD 0x80000000
  157.  
  158. /* Constants for the cmd field of all load commands, the type */
  159. #define    LC_SEGMENT    0x1    /* segment of this file to be mapped */
  160. #define    LC_SYMTAB    0x2    /* link-edit stab symbol table info */
  161. #define    LC_SYMSEG    0x3    /* link-edit gdb symbol table info (obsolete) */
  162. #define    LC_THREAD    0x4    /* thread */
  163. #define    LC_UNIXTHREAD    0x5    /* unix thread (includes a stack) */
  164. #define    LC_LOADFVMLIB    0x6    /* load a specified fixed VM shared library */
  165. #define    LC_IDFVMLIB    0x7    /* fixed VM shared library identification */
  166. #define    LC_IDENT    0x8    /* object identification info (obsolete) */
  167. #define LC_FVMFILE    0x9    /* fixed VM file inclusion (internal use) */
  168. #define LC_PREPAGE      0xa     /* prepage command (internal use) */
  169. #define    LC_DYSYMTAB    0xb    /* dynamic link-edit symbol table info */
  170. #define    LC_LOAD_DYLIB    0xc    /* load a dynamicly linked shared library */
  171. #define    LC_ID_DYLIB    0xd    /* dynamicly linked shared lib identification */
  172. #define LC_LOAD_DYLINKER 0xe    /* load a dynamic linker */
  173. #define LC_ID_DYLINKER    0xf    /* dynamic linker identification */
  174. #define    LC_PREBOUND_DYLIB 0x10    /* modules prebound for a dynamicly */
  175.                 /*  linked shared library */
  176. #define    LC_ROUTINES    0x11    /* image routines */
  177. #define    LC_SUB_FRAMEWORK 0x12    /* sub framework */
  178. #define    LC_SUB_UMBRELLA 0x13    /* sub umbrella */
  179. #define    LC_SUB_CLIENT    0x14    /* sub client */
  180. #define    LC_SUB_LIBRARY  0x15    /* sub library */
  181. #define    LC_TWOLEVEL_HINTS 0x16    /* two-level namespace lookup hints */
  182.  
  183. /*
  184.  * A variable length string in a load command is represented by an lc_str
  185.  * union.  The strings are stored just after the load command structure and
  186.  * the offset is from the start of the load command structure.  The size
  187.  * of the string is reflected in the cmdsize field of the load command.
  188.  * Once again any padded bytes to bring the cmdsize field to a multiple
  189.  * of sizeof(long) must be zero.
  190.  */
  191. union lc_str {
  192.     unsigned long    offset;    /* offset to the string */
  193.     char        *ptr;    /* pointer to the string */
  194. };
  195.  
  196. /*
  197.  * The segment load command indicates that a part of this file is to be
  198.  * mapped into the task's address space.  The size of this segment in memory,
  199.  * vmsize, maybe equal to or larger than the amount to map from this file,
  200.  * filesize.  The file is mapped starting at fileoff to the beginning of
  201.  * the segment in memory, vmaddr.  The rest of the memory of the segment,
  202.  * if any, is allocated zero fill on demand.  The segment's maximum virtual
  203.  * memory protection and initial virtual memory protection are specified
  204.  * by the maxprot and initprot fields.  If the segment has sections then the
  205.  * section structures directly follow the segment command and their size is
  206.  * reflected in cmdsize.
  207.  */
  208. struct segment_command {
  209.     unsigned long    cmd;        /* LC_SEGMENT */
  210.     unsigned long    cmdsize;    /* includes sizeof section structs */
  211.     char        segname[16];    /* segment name */
  212.     unsigned long    vmaddr;        /* memory address of this segment */
  213.     unsigned long    vmsize;        /* memory size of this segment */
  214.     unsigned long    fileoff;    /* file offset of this segment */
  215.     unsigned long    filesize;    /* amount to map from the file */
  216.     vm_prot_t    maxprot;    /* maximum VM protection */
  217.     vm_prot_t    initprot;    /* initial VM protection */
  218.     unsigned long    nsects;        /* number of sections in segment */
  219.     unsigned long    flags;        /* flags */
  220. };
  221.  
  222. /* Constants for the flags field of the segment_command */
  223. #define    SG_HIGHVM    0x1    /* the file contents for this segment is for
  224.                    the high part of the VM space, the low part
  225.                    is zero filled (for stacks in core files) */
  226. #define    SG_FVMLIB    0x2    /* this segment is the VM that is allocated by
  227.                    a fixed VM library, for overlap checking in
  228.                    the link editor */
  229. #define    SG_NORELOC    0x4    /* this segment has nothing that was relocated
  230.                    in it and nothing relocated to it, that is
  231.                    it maybe safely replaced without relocation*/
  232.  
  233. /*
  234.  * A segment is made up of zero or more sections.  Non-MH_OBJECT files have
  235.  * all of their segments with the proper sections in each, and padded to the
  236.  * specified segment alignment when produced by the link editor.  The first
  237.  * segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
  238.  * and load commands of the object file before its first section.  The zero
  239.  * fill sections are always last in their segment (in all formats).  This
  240.  * allows the zeroed segment padding to be mapped into memory where zero fill
  241.  * sections might be.
  242.  *
  243.  * The MH_OBJECT format has all of its sections in one segment for
  244.  * compactness.  There is no padding to a specified segment boundary and the
  245.  * mach_header and load commands are not part of the segment.
  246.  *
  247.  * Sections with the same section name, sectname, going into the same segment,
  248.  * segname, are combined by the link editor.  The resulting section is aligned
  249.  * to the maximum alignment of the combined sections and is the new section's
  250.  * alignment.  The combined sections are aligned to their original alignment in
  251.  * the combined section.  Any padded bytes to get the specified alignment are
  252.  * zeroed.
  253.  *
  254.  * The format of the relocation entries referenced by the reloff and nreloc
  255.  * fields of the section structure for mach object files is described in the
  256.  * header file <reloc.h>.
  257.  */
  258. struct section {
  259.     char        sectname[16];    /* name of this section */
  260.     char        segname[16];    /* segment this section goes in */
  261.     unsigned long    addr;        /* memory address of this section */
  262.     unsigned long    size;        /* size in bytes of this section */
  263.     unsigned long    offset;        /* file offset of this section */
  264.     unsigned long    align;        /* section alignment (power of 2) */
  265.     unsigned long    reloff;        /* file offset of relocation entries */
  266.     unsigned long    nreloc;        /* number of relocation entries */
  267.     unsigned long    flags;        /* flags (section type and attributes)*/
  268.     unsigned long    reserved1;    /* reserved */
  269.     unsigned long    reserved2;    /* reserved */
  270. };
  271.  
  272. /*
  273.  * The flags field of a section structure is separated into two parts a section
  274.  * type and section attributes.  The section types are mutually exclusive (it
  275.  * can only have one type) but the section attributes are not (it may have more
  276.  * than one attribute).
  277.  */
  278. #define SECTION_TYPE         0x000000ff    /* 256 section types */
  279. #define SECTION_ATTRIBUTES     0xffffff00    /*  24 section attributes */
  280.  
  281. /* Constants for the type of a section */
  282. #define    S_REGULAR        0x0    /* regular section */
  283. #define    S_ZEROFILL        0x1    /* zero fill on demand section */
  284. #define    S_CSTRING_LITERALS    0x2    /* section with only literal C strings*/
  285. #define    S_4BYTE_LITERALS    0x3    /* section with only 4 byte literals */
  286. #define    S_8BYTE_LITERALS    0x4    /* section with only 8 byte literals */
  287. #define    S_LITERAL_POINTERS    0x5    /* section with only pointers to */
  288.                     /*  literals */
  289. /*
  290.  * For the two types of symbol pointers sections and the symbol stubs section
  291.  * they have indirect symbol table entries.  For each of the entries in the
  292.  * section the indirect symbol table entries, in corresponding order in the
  293.  * indirect symbol table, start at the index stored in the reserved1 field
  294.  * of the section structure.  Since the indirect symbol table entries
  295.  * correspond to the entries in the section the number of indirect symbol table
  296.  * entries is inferred from the size of the section divided by the size of the
  297.  * entries in the section.  For symbol pointers sections the size of the entries
  298.  * in the section is 4 bytes and for symbol stubs sections the byte size of the
  299.  * stubs is stored in the reserved2 field of the section structure.
  300.  */
  301. #define    S_NON_LAZY_SYMBOL_POINTERS    0x6    /* section with only non-lazy
  302.                            symbol pointers */
  303. #define    S_LAZY_SYMBOL_POINTERS        0x7    /* section with only lazy symbol
  304.                            pointers */
  305. #define    S_SYMBOL_STUBS            0x8    /* section with only symbol
  306.                            stubs, byte size of stub in
  307.                            the reserved2 field */
  308. #define    S_MOD_INIT_FUNC_POINTERS    0x9    /* section with only function
  309.                            pointers for initialization*/
  310. #define    S_MOD_TERM_FUNC_POINTERS    0xa    /* section with only function
  311.                            pointers for termination */
  312. #define    S_COALESCED            0xb    /* section contains symbols that
  313.                            are to be coalesced */
  314. /*
  315.  * Constants for the section attributes part of the flags field of a section
  316.  * structure.
  317.  */
  318. #define SECTION_ATTRIBUTES_USR     0xff000000    /* User setable attributes */
  319. #define S_ATTR_PURE_INSTRUCTIONS 0x80000000    /* section contains only true
  320.                            machine instructions */
  321. #define S_ATTR_NO_TOC          0x40000000    /* section contains coalesced
  322.                            symbols that are not to be
  323.                            in a ranlib table of
  324.                            contents */
  325. #define SECTION_ATTRIBUTES_SYS     0x00ffff00    /* system setable attributes */
  326. #define S_ATTR_SOME_INSTRUCTIONS 0x00000400    /* section contains some
  327.                            machine instructions */
  328. #define S_ATTR_EXT_RELOC     0x00000200    /* section has external
  329.                            relocation entries */
  330. #define S_ATTR_LOC_RELOC     0x00000100    /* section has local
  331.                            relocation entries */
  332.  
  333.  
  334. /*
  335.  * The names of segments and sections in them are mostly meaningless to the
  336.  * link-editor.  But there are few things to support traditional UNIX
  337.  * executables that require the link-editor and assembler to use some names
  338.  * agreed upon by convention.
  339.  *
  340.  * The initial protection of the "__TEXT" segment has write protection turned
  341.  * off (not writeable).
  342.  *
  343.  * The link-editor will allocate common symbols at the end of the "__common"
  344.  * section in the "__DATA" segment.  It will create the section and segment
  345.  * if needed.
  346.  */
  347.  
  348. /* The currently known segment names and the section names in those segments */
  349.  
  350. #define    SEG_PAGEZERO    "__PAGEZERO"    /* the pagezero segment which has no */
  351.                     /* protections and catches NULL */
  352.                     /* references for MH_EXECUTE files */
  353.  
  354.  
  355. #define    SEG_TEXT    "__TEXT"    /* the tradition UNIX text segment */
  356. #define    SECT_TEXT    "__text"    /* the real text part of the text */
  357.                     /* section no headers, and no padding */
  358. #define SECT_FVMLIB_INIT0 "__fvmlib_init0"    /* the fvmlib initialization */
  359.                         /*  section */
  360. #define SECT_FVMLIB_INIT1 "__fvmlib_init1"    /* the section following the */
  361.                             /*  fvmlib initialization */
  362.                         /*  section */
  363.  
  364. #define    SEG_DATA    "__DATA"    /* the tradition UNIX data segment */
  365. #define    SECT_DATA    "__data"    /* the real initialized data section */
  366.                     /* no padding, no bss overlap */
  367. #define    SECT_BSS    "__bss"        /* the real uninitialized data section*/
  368.                     /* no padding */
  369. #define SECT_COMMON    "__common"    /* the section common symbols are */
  370.                     /* allocated in by the link editor */
  371.  
  372. #define    SEG_OBJC    "__OBJC"    /* objective-C runtime segment */
  373. #define SECT_OBJC_SYMBOLS "__symbol_table"    /* symbol table */
  374. #define SECT_OBJC_MODULES "__module_info"    /* module information */
  375. #define SECT_OBJC_STRINGS "__selector_strs"    /* string table */
  376. #define SECT_OBJC_REFS "__selector_refs"    /* string table */
  377.  
  378. #define    SEG_ICON     "__ICON"    /* the icon segment */
  379. #define    SECT_ICON_HEADER "__header"    /* the icon headers */
  380. #define    SECT_ICON_TIFF   "__tiff"    /* the icons in tiff format */
  381.  
  382. #define    SEG_LINKEDIT    "__LINKEDIT"    /* the segment containing all structs */
  383.                     /* created and maintained by the link */
  384.                     /* editor.  Created with -seglinkedit */
  385.                     /* option to ld(1) for MH_EXECUTE and */
  386.                     /* FVMLIB file types only */
  387.  
  388. #define SEG_UNIXSTACK    "__UNIXSTACK"    /* the unix stack segment */
  389.  
  390. /*
  391.  * Fixed virtual memory shared libraries are identified by two things.  The
  392.  * target pathname (the name of the library as found for execution), and the
  393.  * minor version number.  The address of where the headers are loaded is in
  394.  * header_addr.
  395.  */
  396. struct fvmlib {
  397.     union lc_str    name;        /* library's target pathname */
  398.     unsigned long    minor_version;    /* library's minor version number */
  399.     unsigned long    header_addr;    /* library's header address */
  400. };
  401.  
  402. /*
  403.  * A fixed virtual shared library (filetype == MH_FVMLIB in the mach header)
  404.  * contains a fvmlib_command (cmd == LC_IDFVMLIB) to identify the library.
  405.  * An object that uses a fixed virtual shared library also contains a
  406.  * fvmlib_command (cmd == LC_LOADFVMLIB) for each library it uses.
  407.  */
  408. struct fvmlib_command {
  409.     unsigned long    cmd;        /* LC_IDFVMLIB or LC_LOADFVMLIB */
  410.     unsigned long    cmdsize;    /* includes pathname string */
  411.     struct fvmlib    fvmlib;        /* the library identification */
  412. };
  413.  
  414. /*
  415.  * Dynamicly linked shared libraries are identified by two things.  The
  416.  * pathname (the name of the library as found for execution), and the
  417.  * compatibility version number.  The pathname must match and the compatibility
  418.  * number in the user of the library must be greater than or equal to the
  419.  * library being used.  The time stamp is used to record the time a library was
  420.  * built and copied into user so it can be use to determined if the library used
  421.  * at runtime is exactly the same as used to built the program.
  422.  */
  423. struct dylib {
  424.     union lc_str  name;            /* library's path name */
  425.     unsigned long timestamp;        /* library's build time stamp */
  426.     unsigned long current_version;    /* library's current version number */
  427.     unsigned long compatibility_version;/* library's compatibility vers number*/
  428. };
  429.  
  430. /*
  431.  * A dynamicly linked shared library (filetype == MH_DYLIB in the mach header)
  432.  * contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
  433.  * An object that uses a dynamicly linked shared library also contains a
  434.  * dylib_command (cmd == LC_LOAD_DYLIB) for each library it uses.
  435.  */
  436. struct dylib_command {
  437.     unsigned long    cmd;        /* LC_ID_DYLIB or LC_LOAD_DYLIB */
  438.     unsigned long    cmdsize;    /* includes pathname string */
  439.     struct dylib    dylib;        /* the library identification */
  440. };
  441.  
  442. /*
  443.  * A dynamically linked shared library may be a subframework of an umbrella
  444.  * framework.  If so it will be linked with "-umbrella umbrella_name" where
  445.  * Where "umbrella_name" is the name of the umbrella framework. A subframework
  446.  * can only be linked against by its umbrella framework or other subframeworks
  447.  * that are part of the same umbrella framework.  Otherwise the static link
  448.  * editor produces an error and states to link against the umbrella framework.
  449.  * The name of the umbrella framework for subframeworks is recorded in the
  450.  * following structure.
  451.  */
  452. struct sub_framework_command {
  453.     unsigned long    cmd;        /* LC_SUB_FRAMEWORK */
  454.     unsigned long    cmdsize;    /* includes umbrella string */
  455.     union lc_str     umbrella;    /* the umbrella framework name */
  456. };
  457.  
  458. /*
  459.  * For dynamically linked shared libraries that are subframework of an umbrella
  460.  * framework they can allow clients other than the umbrella framework or other
  461.  * subframeworks in the same umbrella framework.  To do this the subframework
  462.  * is built with "-allowable_client client_name" and an LC_SUB_CLIENT load
  463.  * command is created for each -allowable_client flag.  The client_name is
  464.  * usually a framework name.  It can also be a name used for bundles clients
  465.  * where the bundle is built with "-client_name client_name".
  466.  */
  467. struct sub_client_command {
  468.     unsigned long    cmd;        /* LC_SUB_CLIENT */
  469.     unsigned long    cmdsize;    /* includes client string */
  470.     union lc_str     client;        /* the client name */
  471. };
  472.  
  473. /*
  474.  * A dynamically linked shared library may be a sub_umbrella of an umbrella
  475.  * framework.  If so it will be linked with "-sub_umbrella umbrella_name" where
  476.  * Where "umbrella_name" is the name of the sub_umbrella framework.  When
  477.  * staticly linking when -twolevel_namespace is in effect a twolevel namespace 
  478.  * umbrella framework will only cause its subframeworks and those frameworks
  479.  * listed as sub_umbrella frameworks to be implicited linked in.  Any other
  480.  * dependent dynamic libraries will not be linked it when -twolevel_namespace
  481.  * is in effect.  The primary library recorded by the static linker when
  482.  * resolving a symbol in these libraries will be the umbrella framework.
  483.  * Zero or more sub_umbrella frameworks may be use by an umbrella framework.
  484.  * The name of a sub_umbrella framework is recorded in the following structure.
  485.  */
  486. struct sub_umbrella_command {
  487.     unsigned long    cmd;        /* LC_SUB_UMBRELLA */
  488.     unsigned long    cmdsize;    /* includes sub_umbrella string */
  489.     union lc_str     sub_umbrella;    /* the sub_umbrella framework name */
  490. };
  491.  
  492. /*
  493.  * A dynamically linked shared library may be a sub_library of another shared
  494.  * library.  If so it will be linked with "-sub_library library_name" where
  495.  * Where "library_name" is the name of the sub_library shared library.  When
  496.  * staticly linking when -twolevel_namespace is in effect a twolevel namespace 
  497.  * shared library will only cause its subframeworks and those frameworks
  498.  * listed as sub_umbrella frameworks and libraries listed as sub_libraries to
  499.  * be implicited linked in.  Any other dependent dynamic libraries will not be
  500.  * linked it when -twolevel_namespace is in effect.  The primary library
  501.  * recorded by the static linker when resolving a symbol in these libraries
  502.  * will be the umbrella framework (or dynamic library). Zero or more sub_library
  503.  * shared libraries may be use by an umbrella framework or (or dynamic library).
  504.  * The name of a sub_library framework is recorded in the following structure.
  505.  * For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc".
  506.  */
  507. struct sub_library_command {
  508.     unsigned long    cmd;        /* LC_SUB_LIBRARY */
  509.     unsigned long    cmdsize;    /* includes sub_library string */
  510.     union lc_str     sub_library;    /* the sub_library name */
  511. };
  512.  
  513. /*
  514.  * A program (filetype == MH_EXECUTE) or bundle (filetype == MH_BUNDLE) that is
  515.  * prebound to its dynamic libraries has one of these for each library that
  516.  * the static linker used in prebinding.  It contains a bit vector for the
  517.  * modules in the library.  The bits indicate which modules are bound (1) and
  518.  * which are not (0) from the library.  The bit for module 0 is the low bit
  519.  * of the first byte.  So the bit for the Nth module is:
  520.  * (linked_modules[N/8] >> N%8) & 1
  521.  */
  522. struct prebound_dylib_command {
  523.     unsigned long    cmd;        /* LC_PREBOUND_DYLIB */
  524.     unsigned long    cmdsize;    /* includes strings */
  525.     union lc_str    name;        /* library's path name */
  526.     unsigned long    nmodules;    /* number of modules in library */
  527.     union lc_str    linked_modules;    /* bit vector of linked modules */
  528. };
  529.  
  530. /*
  531.  * A program that uses a dynamic linker contains a dylinker_command to identify
  532.  * the name of the dynamic linker (LC_LOAD_DYLINKER).  And a dynamic linker
  533.  * contains a dylinker_command to identify the dynamic linker (LC_ID_DYLINKER).
  534.  * A file can have at most one of these.
  535.  */
  536. struct dylinker_command {
  537.     unsigned long    cmd;        /* LC_ID_DYLINKER or LC_LOAD_DYLINKER */
  538.     unsigned long    cmdsize;    /* includes pathname string */
  539.     union lc_str    name;        /* dynamic linker's path name */
  540. };
  541.  
  542. /*
  543.  * Thread commands contain machine-specific data structures suitable for
  544.  * use in the thread state primitives.  The machine specific data structures
  545.  * follow the struct thread_command as follows.
  546.  * Each flavor of machine specific data structure is preceded by an unsigned
  547.  * long constant for the flavor of that data structure, an unsigned long
  548.  * that is the count of longs of the size of the state data structure and then
  549.  * the state data structure follows.  This triple may be repeated for many
  550.  * flavors.  The constants for the flavors, counts and state data structure
  551.  * definitions are expected to be in the header file <machine/thread_status.h>.
  552.  * These machine specific data structures sizes must be multiples of
  553.  * sizeof(long).  The cmdsize reflects the total size of the thread_command
  554.  * and all of the sizes of the constants for the flavors, counts and state
  555.  * data structures.
  556.  *
  557.  * For executable objects that are unix processes there will be one
  558.  * thread_command (cmd == LC_UNIXTHREAD) created for it by the link-editor.
  559.  * This is the same as a LC_THREAD, except that a stack is automatically
  560.  * created (based on the shell's limit for the stack size).  Command arguments
  561.  * and environment variables are copied onto that stack.
  562.  */
  563. struct thread_command {
  564.     unsigned long    cmd;        /* LC_THREAD or  LC_UNIXTHREAD */
  565.     unsigned long    cmdsize;    /* total size of this command */
  566.     /* unsigned long flavor           flavor of thread state */
  567.     /* unsigned long count           count of longs in thread state */
  568.     /* struct XXX_thread_state state   thread state for this flavor */
  569.     /* ... */
  570. };
  571.  
  572. /*
  573.  * The routines command contains the address of the dynamic shared library 
  574.  * initialization routine and an index into the module table for the module
  575.  * that defines the routine.  Before any modules are used from the library the
  576.  * dynamic linker fully binds the module that defines the initialization routine
  577.  * and then calls it.  This gets called before any module initialization
  578.  * routines (used for C++ static constructors) in the library.
  579.  */
  580. struct routines_command {
  581.     unsigned long    cmd;        /* LC_ROUTINES */
  582.     unsigned long    cmdsize;    /* total size of this command */
  583.     unsigned long    init_address;    /* address of initialization routine */
  584.     unsigned long    init_module;    /* index into the module table that */
  585.                         /*  the init routine is defined in */
  586.     unsigned long    reserved1;
  587.     unsigned long    reserved2;
  588.     unsigned long    reserved3;
  589.     unsigned long    reserved4;
  590.     unsigned long    reserved5;
  591.     unsigned long    reserved6;
  592. };
  593.  
  594. /*
  595.  * The symtab_command contains the offsets and sizes of the link-edit 4.3BSD
  596.  * "stab" style symbol table information as described in the header files
  597.  * <nlist.h> and <stab.h>.
  598.  */
  599. struct symtab_command {
  600.     unsigned long    cmd;        /* LC_SYMTAB */
  601.     unsigned long    cmdsize;    /* sizeof(struct symtab_command) */
  602.     unsigned long    symoff;        /* symbol table offset */
  603.     unsigned long    nsyms;        /* number of symbol table entries */
  604.     unsigned long    stroff;        /* string table offset */
  605.     unsigned long    strsize;    /* string table size in bytes */
  606. };
  607.  
  608. /*
  609.  * This is the second set of the symbolic information which is used to support
  610.  * the data structures for the dynamicly link editor.
  611.  *
  612.  * The original set of symbolic information in the symtab_command which contains
  613.  * the symbol and string tables must also be present when this load command is
  614.  * present.  When this load command is present the symbol table is organized
  615.  * into three groups of symbols:
  616.  *    local symbols (static and debugging symbols) - grouped by module
  617.  *    defined external symbols - grouped by module (sorted by name if not lib)
  618.  *    undefined external symbols (sorted by name if MH_BINDATLOAD is not set)
  619.  * In this load command there are offsets and counts to each of the three groups
  620.  * of symbols.
  621.  *
  622.  * This load command contains a the offsets and sizes of the following new
  623.  * symbolic information tables:
  624.  *    table of contents
  625.  *    module table
  626.  *    reference symbol table
  627.  *    indirect symbol table
  628.  * The first three tables above (the table of contents, module table and
  629.  * reference symbol table) are only present if the file is a dynamicly linked
  630.  * shared library.  For executable and object modules, which are files
  631.  * containing only one module, the information that would be in these three
  632.  * tables is determined as follows:
  633.  *     table of contents - the defined external symbols are sorted by name
  634.  *    module table - the file contains only one module so everything in the
  635.  *               file is part of the module.
  636.  *    reference symbol table - is the defined and undefined external symbols
  637.  *
  638.  * For dynamicly linked shared library files this load command also contains
  639.  * offsets and sizes to the pool of relocation entries for all sections
  640.  * separated into two groups:
  641.  *    external relocation entries
  642.  *    local relocation entries
  643.  * For executable and object modules the relocation entries continue to hang
  644.  * off the section structures.
  645.  */
  646. struct dysymtab_command {
  647.     unsigned long cmd;        /* LC_DYSYMTAB */
  648.     unsigned long cmdsize;    /* sizeof(struct dysymtab_command) */
  649.  
  650.     /*
  651.      * The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
  652.      * are grouped into the following three groups:
  653.      *    local symbols (further grouped by the module they are from)
  654.      *    defined external symbols (further grouped by the module they are from)
  655.      *    undefined symbols
  656.      *
  657.      * The local symbols are used only for debugging.  The dynamic binding
  658.      * process may have to use them to indicate to the debugger the local
  659.      * symbols for a module that is being bound.
  660.      *
  661.      * The last two groups are used by the dynamic binding process to do the
  662.      * binding (indirectly through the module table and the reference symbol
  663.      * table when this is a dynamicly linked shared library file).
  664.      */
  665.     unsigned long ilocalsym;    /* index to local symbols */
  666.     unsigned long nlocalsym;    /* number of local symbols */
  667.  
  668.     unsigned long iextdefsym;    /* index to externally defined symbols */
  669.     unsigned long nextdefsym;    /* number of externally defined symbols */
  670.  
  671.     unsigned long iundefsym;    /* index to undefined symbols */
  672.     unsigned long nundefsym;    /* number of undefined symbols */
  673.  
  674.     /*
  675.      * For the for the dynamic binding process to find which module a symbol
  676.      * is defined in the table of contents is used (analogous to the ranlib
  677.      * structure in an archive) which maps defined external symbols to modules
  678.      * they are defined in.  This exists only in a dynamicly linked shared
  679.      * library file.  For executable and object modules the defined external
  680.      * symbols are sorted by name and is use as the table of contents.
  681.      */
  682.     unsigned long tocoff;    /* file offset to table of contents */
  683.     unsigned long ntoc;        /* number of entries in table of contents */
  684.  
  685.     /*
  686.      * To support dynamic binding of "modules" (whole object files) the symbol
  687.      * table must reflect the modules that the file was created from.  This is
  688.      * done by having a module table that has indexes and counts into the merged
  689.      * tables for each module.  The module structure that these two entries
  690.      * refer to is described below.  This exists only in a dynamicly linked
  691.      * shared library file.  For executable and object modules the file only
  692.      * contains one module so everything in the file belongs to the module.
  693.      */
  694.     unsigned long modtaboff;    /* file offset to module table */
  695.     unsigned long nmodtab;    /* number of module table entries */
  696.  
  697.     /*
  698.      * To support dynamic module binding the module structure for each module
  699.      * indicates the external references (defined and undefined) each module
  700.      * makes.  For each module there is an offset and a count into the
  701.      * reference symbol table for the symbols that the module references.
  702.      * This exists only in a dynamicly linked shared library file.  For
  703.      * executable and object modules the defined external symbols and the
  704.      * undefined external symbols indicates the external references.
  705.      */
  706.     unsigned long extrefsymoff;  /* offset to referenced symbol table */
  707.     unsigned long nextrefsyms;     /* number of referenced symbol table entries */
  708.  
  709.     /*
  710.      * The sections that contain "symbol pointers" and "routine stubs" have
  711.      * indexes and (implied counts based on the size of the section and fixed
  712.      * size of the entry) into the "indirect symbol" table for each pointer
  713.      * and stub.  For every section of these two types the index into the
  714.      * indirect symbol table is stored in the section header in the field
  715.      * reserved1.  An indirect symbol table entry is simply a 32bit index into
  716.      * the symbol table to the symbol that the pointer or stub is referring to.
  717.      * The indirect symbol table is ordered to match the entries in the section.
  718.      */
  719.     unsigned long indirectsymoff; /* file offset to the indirect symbol table */
  720.     unsigned long nindirectsyms;  /* number of indirect symbol table entries */
  721.  
  722.     /*
  723.      * To support relocating an individual module in a library file quickly the
  724.      * external relocation entries for each module in the library need to be
  725.      * accessed efficiently.  Since the relocation entries can't be accessed
  726.      * through the section headers for a library file they are separated into
  727.      * groups of local and external entries further grouped by module.  In this
  728.      * case the presents of this load command who's extreloff, nextrel,
  729.      * locreloff and nlocrel fields are non-zero indicates that the relocation
  730.      * entries of non-merged sections are not referenced through the section
  731.      * structures (and the reloff and nreloc fields in the section headers are
  732.      * set to zero).
  733.      *
  734.      * Since the relocation entries are not accessed through the section headers
  735.      * this requires the r_address field to be something other than a section
  736.      * offset to identify the item to be relocated.  In this case r_address is
  737.      * set to the offset from the vmaddr of the first LC_SEGMENT command.
  738.      * For MH_SPLIT_SEGS images r_address is set to the the offset from the
  739.      * vmaddr of the first read-write LC_SEGMENT command.
  740.      *
  741.      * The relocation entries are grouped by module and the module table
  742.      * entries have indexes and counts into them for the group of external
  743.      * relocation entries for that the module.
  744.      *
  745.      * For sections that are merged across modules there must not be any
  746.      * remaining external relocation entries for them (for merged sections
  747.      * remaining relocation entries must be local).
  748.      */
  749.     unsigned long extreloff;    /* offset to external relocation entries */
  750.     unsigned long nextrel;    /* number of external relocation entries */
  751.  
  752.     /*
  753.      * All the local relocation entries are grouped together (they are not
  754.      * grouped by their module since they are only used if the object is moved
  755.      * from it staticly link edited address).
  756.      */
  757.     unsigned long locreloff;    /* offset to local relocation entries */
  758.     unsigned long nlocrel;    /* number of local relocation entries */
  759.  
  760. };    
  761.  
  762. /*
  763.  * An indirect symbol table entry is simply a 32bit index into the symbol table 
  764.  * to the symbol that the pointer or stub is refering to.  Unless it is for a
  765.  * non-lazy symbol pointer section for a defined symbol which strip(1) as 
  766.  * removed.  In which case it has the value INDIRECT_SYMBOL_LOCAL.  If the
  767.  * symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that.
  768.  */
  769. #define INDIRECT_SYMBOL_LOCAL    0x80000000
  770. #define INDIRECT_SYMBOL_ABS    0x40000000
  771.  
  772.  
  773. /* a table of contents entry */
  774. struct dylib_table_of_contents {
  775.     unsigned long symbol_index;    /* the defined external symbol
  776.                    (index into the symbol table) */
  777.     unsigned long module_index;    /* index into the module table this symbol
  778.                    is defined in */
  779. };    
  780.  
  781. /* a module table entry */
  782. struct dylib_module {
  783.     unsigned long module_name;    /* the module name (index into string table) */
  784.  
  785.     unsigned long iextdefsym;    /* index into externally defined symbols */
  786.     unsigned long nextdefsym;    /* number of externally defined symbols */
  787.     unsigned long irefsym;    /* index into reference symbol table */
  788.     unsigned long nrefsym;    /* number of reference symbol table entries */
  789.     unsigned long ilocalsym;    /* index into symbols for local symbols */
  790.     unsigned long nlocalsym;    /* number of local symbols */
  791.  
  792.     unsigned long iextrel;    /* index into external relocation entries */
  793.     unsigned long nextrel;    /* number of external relocation entries */
  794.  
  795.     unsigned long iinit_iterm;    /* low 16 bits are the index into the init
  796.                    section, high 16 bits are the index into
  797.                        the term section */
  798.     unsigned long ninit_nterm;    /* low 16 bits are the number of init section
  799.                    entries, high 16 bits are the number of
  800.                    term section entries */
  801.  
  802.     unsigned long        /* for this module address of the start of */
  803.     objc_module_info_addr;  /*  the (__OBJC,__module_info) section */
  804.     unsigned long        /* for this module size of */
  805.     objc_module_info_size;    /*  the (__OBJC,__module_info) section */
  806. };    
  807.  
  808. /* 
  809.  * The entries in the reference symbol table are used when loading the module
  810.  * (both by the static and dynamic link editors) and if the module is unloaded
  811.  * or replaced.  Therefore all external symbols (defined and undefined) are
  812.  * listed in the module's reference table.  The flags describe the type of
  813.  * reference that is being made.  The constants for the flags are defined in
  814.  * <mach-o/nlist.h> as they are also used for symbol table entries.
  815.  */
  816. struct dylib_reference {
  817.     unsigned long isym:24,    /* index into the symbol table */
  818.               flags:8;    /* flags to indicate the type of reference */
  819. };
  820.  
  821. /*
  822.  * The twolevel_hints_command contains the offset and number of hints in the
  823.  * two-level namespace lookup hints table.
  824.  */
  825. struct twolevel_hints_command {
  826.     unsigned long cmd;        /* LC_TWOLEVEL_HINTS */
  827.     unsigned long cmdsize;    /* sizeof(struct twolevel_hints_command) */
  828.     unsigned long offset;    /* offset to the hint table */
  829.     unsigned long nhints;    /* number of hints in the hint table */
  830. };
  831.  
  832. /*
  833.  * The entries in the two-level namespace lookup hints table are twolevel_hint
  834.  * structs.  These provide hints to the dynamic link editor where to start
  835.  * looking for an undefined symbol in a two-level namespace image.  The
  836.  * isub_image field is an index into the sub-images (sub-frameworks and
  837.  * sub-umbrellas list) that made up the two-level image that the undefined
  838.  * symbol was found in when it was built by the static link editor.  If
  839.  * isub-image is 0 the the symbol is expected to be defined in library and not
  840.  * in the sub-images.  If isub-image is non-zero it is an index into the array
  841.  * of sub-images for the umbrella with the first index in the sub-images being
  842.  * 1. The array of sub-images is the ordered list of sub-images of the umbrella
  843.  * that would be searched for a symbol that has the umbrella recorded as its
  844.  * primary library.  The table of contents index is an index into the
  845.  * library's table of contents.  This is used as the starting point of the
  846.  * binary search or a directed linear search.
  847.  */
  848. struct twolevel_hint {
  849.     unsigned long 
  850.     isub_image:8,    /* index into the sub images */
  851.     itoc:24;    /* index into the table of contents */
  852. };
  853.  
  854. /*
  855.  * The symseg_command contains the offset and size of the GNU style
  856.  * symbol table information as described in the header file <symseg.h>.
  857.  * The symbol roots of the symbol segments must also be aligned properly
  858.  * in the file.  So the requirement of keeping the offsets aligned to a
  859.  * multiple of a sizeof(long) translates to the length field of the symbol
  860.  * roots also being a multiple of a long.  Also the padding must again be
  861.  * zeroed. (THIS IS OBSOLETE and no longer supported).
  862.  */
  863. struct symseg_command {
  864.     unsigned long    cmd;        /* LC_SYMSEG */
  865.     unsigned long    cmdsize;    /* sizeof(struct symseg_command) */
  866.     unsigned long    offset;        /* symbol segment offset */
  867.     unsigned long    size;        /* symbol segment size in bytes */
  868. };
  869.  
  870. /*
  871.  * The ident_command contains a free format string table following the
  872.  * ident_command structure.  The strings are null terminated and the size of
  873.  * the command is padded out with zero bytes to a multiple of sizeof(long).
  874.  * (THIS IS OBSOLETE and no longer supported).
  875.  */
  876. struct ident_command {
  877.     unsigned long cmd;        /* LC_IDENT */
  878.     unsigned long cmdsize;        /* strings that follow this command */
  879. };
  880.  
  881. /*
  882.  * The fvmfile_command contains a reference to a file to be loaded at the
  883.  * specified virtual address.  (Presently, this command is reserved for
  884.  * internal use.  The kernel ignores this command when loading a program into
  885.  * memory).
  886.  */
  887. struct fvmfile_command {
  888.     unsigned long cmd;        /* LC_FVMFILE */
  889.     unsigned long cmdsize;        /* includes pathname string */
  890.     union lc_str    name;        /* files pathname */
  891.     unsigned long    header_addr;    /* files virtual address */
  892. };
  893.  
  894. #endif _MACHO_LOADER_H_
  895.