xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_macho_loader.h (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*  This is a cut-down version of loader.h from cctools-895,
2     shrunk to eliminate aspects unwanted in libdwarf and to avoid
3     #include entirely.  All tab characters replaced with 4 spaces
4     so various things no line up as they used to.
5     cctools-895  in its original form
6     is available from https://opensource.apple.com/
7     see Developer Tools version 8.2.1. cctools-895/include/loader.h */
8 /*
9 * Copyright (c) 1999-2010 Apple Inc.  All Rights Reserved.
10 *
11 * @APPLE_LICENSE_HEADER_START@
12 *
13 * This file contains Original Code and/or Modifications of Original Code
14 * as defined in and that are subject to the Apple Public Source License
15 * Version 2.0 (the 'License'). You may not use this file except in
16 * compliance with the License. Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_HEADER_END@
29 */
30 #ifndef MACHO_LOADER_H
31 #define MACHO_LOADER_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
36 
37 
38 #if 0 /* Not used here. DavidA. September 2018 */
39 /*
40 * This file describes the format of mach object files.
41 */
42 #include <stdint.h>
43 
44 /*
45 * <mach/machine.h> is needed here for the cpu_type_t and cpu_subtype_t types
46 * and contains the constants for the possible values of these types.
47 */
48 #include <mach/machine.h>
49 
50 /*
51 * <mach/vm_prot.h> is needed here for the vm_prot_t type and contains the
52 * constants that are or'ed together for the possible values of this type.
53 */
54 #include <mach/vm_prot.h>
55 
56 /*
57 * <machine/thread_status.h> is expected to define the flavors of the thread
58 * states and the structures of those flavors for each machine.
59 */
60 #include <mach/machine/thread_status.h>
61 #include <architecture/byte_order.h>
62 #endif /* 0 */
63 
64 #ifndef TYP
65 #define TYP(n,l) char n[l]
66 #endif /* TYP */
67 
68 /*
69 * The 32-bit mach header appears at the very beginning of the object file for
70 * 32-bit architectures.
71 */
72 struct mach_header {
73     TYP(magic,4); /* mach magic number identifier */
74     TYP(cputype,4); /* cpu specifier */
75     TYP(cpusubtype,4); /* machine specifier */
76     TYP(filetype,4); /* type of file */
77     TYP(ncmds,4); /* number of load commands */
78     TYP(sizeofcmds,4); /* the size of all the load commands */
79     TYP(flags,4); /* flags */
80 };
81 
82 /*  Constant for the magic field of the
83     mach_header (32-bit architectures)
84     MH_MAGIC MH_MAGIC_64 appear in big-endian objects
85     MH_CIGAM MH_CIGAM_64 appear in little-endian objects */
86 #define    MH_MAGIC    0xfeedface    /* the mach magic number */
87 #define MH_CIGAM    0xcefaedfe    /* NXSwapInt(MH_MAGIC) */
88 
89 /*
90 * The 64-bit mach header appears at the very beginning of object files for
91 * 64-bit architectures.
92 */
93 struct mach_header_64 {
94     TYP(magic,4); /* mach magic number identifier */
95     TYP(cputype,4); /* cpu specifier */
96     TYP(cpusubtype,4); /* machine specifier */
97     TYP(filetype,4); /* type of file */
98     TYP(ncmds,4); /* number of load commands */
99     TYP(sizeofcmds,4); /* the size of all the load commands */
100     TYP(flags,4); /* flags */
101     TYP(reserved,4); /* reserved */
102 };
103 
104 /* Constant for the magic field of the mach_header_64 (64-bit architectures) */
105 #define MH_MAGIC_64 0xfeedfacf /* the 64-bit mach magic number */
106 #define MH_CIGAM_64 0xcffaedfe /* NXSwapInt(MH_MAGIC_64) */
107 
108 /*
109 * The layout of the file depends on the filetype.  For all but the MH_OBJECT
110 * file type the segments are padded out and aligned on a segment alignment
111 * boundary for efficient demand pageing.  The MH_EXECUTE, MH_FVMLIB, MH_DYLIB,
112 * MH_DYLINKER and MH_BUNDLE file types also have the headers included as part
113 * of their first segment.
114 *
115 * The file type MH_OBJECT is a compact format intended as output of the
116 * assembler and input (and possibly output) of the link editor (the .o
117 * format).  All sections are in one unnamed segment with no segment padding.
118 * This format is used as an executable format when the file is so small the
119 * segment padding greatly increases its size.
120 *
121 * The file type MH_PRELOAD is an executable format intended for things that
122 * are not executed under the kernel (proms, stand alones, kernels, etc).  The
123 * format can be executed under the kernel but may demand paged it and not
124 * preload it before execution.
125 *
126 * A core file is in MH_CORE format and can be any in an arbritray legal
127 * Mach-O file.
128 *
129 * Constants for the filetype field of the mach_header
130 */
131 #define    MH_OBJECT    0x1        /* relocatable object file */
132 #define    MH_EXECUTE    0x2        /* demand paged executable file */
133 #define    MH_FVMLIB    0x3        /* fixed VM shared library file */
134 #define    MH_CORE        0x4        /* core file */
135 #define    MH_PRELOAD    0x5        /* preloaded executable file */
136 #define    MH_DYLIB    0x6        /* dynamically bound shared library */
137 #define    MH_DYLINKER    0x7        /* dynamic link editor */
138 #define    MH_BUNDLE    0x8        /* dynamically bound bundle file */
139 #define    MH_DYLIB_STUB    0x9        /* shared library stub for static */
140     /*  linking only, no section contents */
141 #define    MH_DSYM        0xa        /* companion file with only debug */
142     /*  sections */
143 #define    MH_KEXT_BUNDLE    0xb        /* x86_64 kexts */
144 
145 /* Constants for the flags field of the mach_header */
146 #define    MH_NOUNDEFS    0x1        /* the object file has no undefined
147     references */
148 #define    MH_INCRLINK    0x2        /* the object file is the output of an
149     incremental link against a base file
150     and can't be link edited again */
151 #define MH_DYLDLINK    0x4        /* the object file is input for the
152     dynamic linker and can't be staticly
153     link edited again */
154 #define MH_BINDATLOAD    0x8        /* the object file's undefined
155     references are bound by the dynamic
156     linker when loaded. */
157 #define MH_PREBOUND    0x10        /* the file has its dynamic undefined
158     references prebound. */
159 #define MH_SPLIT_SEGS    0x20        /* the file has its read-only and
160     read-write segments split */
161 #define MH_LAZY_INIT    0x40        /* the shared library init routine is
162     to be run lazily via catching memory
163     faults to its writeable segments
164     (obsolete) */
165 #define MH_TWOLEVEL    0x80        /* the image is using two-level name
166     space bindings */
167 #define MH_FORCE_FLAT    0x100        /* the executable is forcing all images
168     to use flat name space bindings */
169 #define MH_NOMULTIDEFS    0x200        /* this umbrella guarantees no multiple
170     defintions of symbols in its
171     sub-images so the two-level namespace
172     hints can always be used. */
173 #define MH_NOFIXPREBINDING 0x400    /* do not have dyld notify the
174     prebinding agent about this
175     executable */
176 #define MH_PREBINDABLE  0x800           /* the binary is not prebound but can
177     have its prebinding redone. only used
178     when MH_PREBOUND is not set. */
179 #define MH_ALLMODSBOUND 0x1000        /* indicates that this binary binds to
180     all two-level namespace modules of
181     its dependent libraries. only used
182     when MH_PREBINDABLE and MH_TWOLEVEL
183     are both set. */
184 #define MH_SUBSECTIONS_VIA_SYMBOLS 0x2000/* safe to divide up the sections into
185     sub-sections via symbols for dead
186     code stripping */
187 #define MH_CANONICAL    0x4000        /* the binary has been canonicalized
188     via the unprebind operation */
189 #define MH_WEAK_DEFINES    0x8000        /* the final linked image contains
190     external weak symbols */
191 #define MH_BINDS_TO_WEAK 0x10000    /* the final linked image uses
192     weak symbols */
193 
194 #define MH_ALLOW_STACK_EXECUTION 0x20000/* When this bit is set, all stacks
195     in the task will be given stack
196     execution privilege.  Only used in
197     MH_EXECUTE filetypes. */
198 #define MH_ROOT_SAFE 0x40000           /* When this bit is set, the binary
199     declares it is safe for use in
200     processes with uid zero */
201 
202 #define MH_SETUID_SAFE 0x80000         /* When this bit is set, the binary
203     declares it is safe for use in
204     processes when issetugid() is true */
205 
206 #define MH_NO_REEXPORTED_DYLIBS 0x100000 /* When this bit is set on a dylib,
207     the static linker does not need to
208     examine dependent dylibs to see
209     if any are re-exported */
210 #define    MH_PIE 0x200000            /* When this bit is set, the OS will
211     load the main executable at a
212     random address.  Only used in
213     MH_EXECUTE filetypes. */
214 #define    MH_DEAD_STRIPPABLE_DYLIB 0x400000 /* Only for use on dylibs.  When
215     linking against a dylib that
216     has this bit set, the static linker
217     will automatically not create a
218     LC_LOAD_DYLIB load command to the
219     dylib if no symbols are being
220     referenced from the dylib. */
221 #define MH_HAS_TLV_DESCRIPTORS 0x800000 /* Contains a section of type
222     S_THREAD_LOCAL_VARIABLES */
223 
224 #define MH_NO_HEAP_EXECUTION 0x1000000    /* When this bit is set, the OS will
225     run the main executable with
226     a non-executable heap even on
227     platforms (e.g. i386) that don't
228     require it. Only used in MH_EXECUTE
229     filetypes. */
230 
231 #define MH_APP_EXTENSION_SAFE 0x02000000 /* The code was linked for use in an
232     application extension. */
233 
234 /*
235 * The load commands directly follow the mach_header.  The total size of all
236 * of the commands is given by the sizeofcmds field in the mach_header.  All
237 * load commands must have as their first two fields cmd and cmdsize.  The cmd
238 * field is filled in with a constant for that command type.  Each command type
239 * has a structure specifically for it.  The cmdsize field is the size in bytes
240 * of the particular load command structure plus anything that follows it that
241 * is a part of the load command (i.e. section structures, strings, etc.).  To
242 * advance to the next load command the cmdsize can be added to the offset or
243 * pointer of the current load command.  The cmdsize for 32-bit architectures
244 * MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple
245 * of 8 bytes (these are forever the maximum alignment of any load commands).
246 * The padded bytes must be zero.  All tables in the object file must also
247 * follow these rules so the file can be memory mapped.  Otherwise the pointers
248 * to these tables will not work well or at all on some machines.  With all
249 * padding zeroed like objects will compare byte for byte.
250 */
251 struct load_command {
252     TYP(cmd,4); /* type of load command */
253     TYP(cmdsize,4); /* total size of command in bytes */
254 };
255 
256 /*
257 * After MacOS X 10.1 when a new load command is added that is required to be
258 * understood by the dynamic linker for the image to execute properly the
259 * LC_REQ_DYLD bit will be or'ed into the load command constant.  If the dynamic
260 * linker sees such a load command it it does not understand will issue a
261 * "unknown load command required for execution" error and refuse to use the
262 * image.  Other load commands without this bit that are not understood will
263 * simply be ignored.
264 */
265 #define LC_REQ_DYLD 0x80000000
266 
267 /* Constants for the cmd field of all load commands, the type */
268 #define    LC_SEGMENT    0x1    /* segment of this file to be mapped */
269 #define    LC_SYMTAB    0x2    /* link-edit stab symbol table info */
270 #define    LC_SYMSEG    0x3    /* link-edit gdb symbol table info (obsolete) */
271 #define    LC_THREAD    0x4    /* thread */
272 #define    LC_UNIXTHREAD    0x5    /* unix thread (includes a stack) */
273 #define    LC_LOADFVMLIB    0x6    /* load a specified fixed VM shared library */
274 #define    LC_IDFVMLIB    0x7    /* fixed VM shared library identification */
275 #define    LC_IDENT    0x8    /* object identification info (obsolete) */
276 #define LC_FVMFILE    0x9    /* fixed VM file inclusion (internal use) */
277 #define LC_PREPAGE      0xa     /* prepage command (internal use) */
278 #define    LC_DYSYMTAB    0xb    /* dynamic link-edit symbol table info */
279 #define    LC_LOAD_DYLIB    0xc    /* load a dynamically linked shared library */
280 #define    LC_ID_DYLIB    0xd    /* dynamically linked shared lib ident */
281 #define LC_LOAD_DYLINKER 0xe    /* load a dynamic linker */
282 #define LC_ID_DYLINKER    0xf    /* dynamic linker identification */
283 #define    LC_PREBOUND_DYLIB 0x10    /* modules prebound for a dynamically */
284     /*  linked shared library */
285 #define    LC_ROUTINES    0x11    /* image routines */
286 #define    LC_SUB_FRAMEWORK 0x12    /* sub framework */
287 #define    LC_SUB_UMBRELLA 0x13    /* sub umbrella */
288 #define    LC_SUB_CLIENT    0x14    /* sub client */
289 #define    LC_SUB_LIBRARY  0x15    /* sub library */
290 #define    LC_TWOLEVEL_HINTS 0x16    /* two-level namespace lookup hints */
291 #define    LC_PREBIND_CKSUM  0x17    /* prebind checksum */
292 
293 /*
294 * load a dynamically linked shared library that is allowed to be missing
295 * (all symbols are weak imported).
296 */
297 #define    LC_LOAD_WEAK_DYLIB (0x18 | LC_REQ_DYLD)
298 
299 #define    LC_SEGMENT_64    0x19    /* 64-bit segment of this file to be
300     mapped */
301 #define    LC_ROUTINES_64    0x1a    /* 64-bit image routines */
302 #define LC_UUID        0x1b    /* the uuid */
303 #define LC_RPATH       (0x1c | LC_REQ_DYLD)    /* runpath additions */
304 #define LC_CODE_SIGNATURE 0x1d    /* local of code signature */
305 #define LC_SEGMENT_SPLIT_INFO 0x1e /* local of info to split segments */
306 #define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD) /* load and re-export dylib */
307 #define    LC_LAZY_LOAD_DYLIB 0x20    /* delay load of dylib until first use */
308 #define    LC_ENCRYPTION_INFO 0x21    /* encrypted segment information */
309 #define    LC_DYLD_INFO     0x22    /* compressed dyld information */
310 #define    LC_DYLD_INFO_ONLY (0x22|LC_REQ_DYLD)    /* compressed dyld information only */
311 #define    LC_LOAD_UPWARD_DYLIB (0x23 | LC_REQ_DYLD) /* load upward dylib */
312 #define LC_VERSION_MIN_MACOSX 0x24   /* build for MacOSX min OS version */
313 #define LC_VERSION_MIN_IPHONEOS 0x25 /* build for iPhoneOS min OS version */
314 #define LC_FUNCTION_STARTS 0x26 /* compressed table of function start addresses */
315 #define LC_DYLD_ENVIRONMENT 0x27 /* string for dyld to treat
316                     like environment variable */
317 #define LC_MAIN (0x28|LC_REQ_DYLD) /* replacement for LC_UNIXTHREAD */
318 #define LC_DATA_IN_CODE 0x29 /* table of non-instructions in __text */
319 #define LC_SOURCE_VERSION 0x2A /* source version used to build binary */
320 #define LC_DYLIB_CODE_SIGN_DRS 0x2B /* Code signing DRs copied from linked dylibs */
321 #define    LC_ENCRYPTION_INFO_64 0x2C /* 64-bit encrypted segment information */
322 #define LC_LINKER_OPTION 0x2D /* linker options in MH_OBJECT files */
323 #define LC_LINKER_OPTIMIZATION_HINT 0x2E /* optimization hints in MH_OBJECT files */
324 #define LC_VERSION_MIN_TVOS 0x2F /* build for AppleTV min OS version */
325 #define LC_VERSION_MIN_WATCHOS 0x30 /* build for Watch min OS version */
326 
327 /*
328 * A variable length string in a load command is represented by an lc_str
329 * union.  The strings are stored just after the load command structure and
330 * the offset is from the start of the load command structure.  The size
331 * of the string is reflected in the cmdsize field of the load command.
332 * Once again any padded bytes to bring the cmdsize field to a multiple
333 * of 4 bytes must be zero.
334 */
335 union lc_str {
336     TYP(offset,4); /* offset to the string */
337 #ifndef __LP64__
338     char        *ptr;    /* pointer to the string */
339 #endif
340 };
341 
342 /*
343 * The segment load command indicates that a part of this file is to be
344 * mapped into the task's address space.  The size of this segment in memory,
345 * vmsize, maybe equal to or larger than the amount to map from this file,
346 * filesize.  The file is mapped starting at fileoff to the beginning of
347 * the segment in memory, vmaddr.  The rest of the memory of the segment,
348 * if any, is allocated zero fill on demand.  The segment's maximum virtual
349 * memory protection and initial virtual memory protection are specified
350 * by the maxprot and initprot fields.  If the segment has sections then the
351 * section structures directly follow the segment command and their size is
352 * reflected in cmdsize.
353 */
354 struct segment_command { /* for 32-bit architectures */
355     TYP(cmd,4); /* LC_SEGMENT */
356     TYP(cmdsize,4); /* includes sizeof section structs */
357     char        segname[16];    /* segment name */
358     TYP(vmaddr,4); /* memory address of this segment */
359     TYP(vmsize,4); /* memory size of this segment */
360     TYP(fileoff,4); /* file offset of this segment */
361     TYP(filesize,4); /* amount to map from the file */
362     TYP(maxprot,4); /* maximum VM protection */
363     TYP(initprot,4); /* initial VM protection */
364     TYP(nsects,4); /* number of sections in segment */
365     TYP(flags,4); /* flags */
366 };
367 
368 /*
369 * The 64-bit segment load command indicates that a part of this file is to be
370 * mapped into a 64-bit task's address space.  If the 64-bit segment has
371 * sections then section_64 structures directly follow the 64-bit segment
372 * command and their size is reflected in cmdsize.
373 */
374 struct segment_command_64 { /* for 64-bit architectures */
375     TYP(cmd,4); /* LC_SEGMENT_64 */
376     TYP(cmdsize,4); /* includes sizeof section_64 structs */
377     char        segname[16];    /* segment name */
378     TYP(vmaddr,8); /* memory address of this segment */
379     TYP(vmsize,8); /* memory size of this segment */
380     TYP(fileoff,8); /* file offset of this segment */
381     TYP(filesize,8); /* amount to map from the file */
382     TYP(maxprot,4); /* maximum VM protection */
383     TYP(initprot,4); /* initial VM protection */
384     TYP(nsects,4); /* number of sections in segment */
385     TYP(flags,4); /* flags */
386 };
387 
388 /* Constants for the flags field of the segment_command */
389 #define    SG_HIGHVM    0x1    /* the file contents for this segment is for
390     the high part of the VM space, the low part
391     is zero filled (for stacks in core files) */
392 #define    SG_FVMLIB    0x2    /* this segment is the VM that is allocated by
393     a fixed VM library, for overlap checking in
394     the link editor */
395 #define    SG_NORELOC    0x4    /* this segment has nothing that was relocated
396     in it and nothing relocated to it, that is
397     it maybe safely replaced without relocation*/
398 #define SG_PROTECTED_VERSION_1    0x8 /* This segment is protected.  If the
399     segment starts at file offset 0, the
400     first page of the segment is not
401     protected.  All other pages of the
402     segment are protected. */
403 
404 /*
405 * A segment is made up of zero or more sections.  Non-MH_OBJECT files have
406 * all of their segments with the proper sections in each, and padded to the
407 * specified segment alignment when produced by the link editor.  The first
408 * segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
409 * and load commands of the object file before its first section.  The zero
410 * fill sections are always last in their segment (in all formats).  This
411 * allows the zeroed segment padding to be mapped into memory where zero fill
412 * sections might be. The gigabyte zero fill sections, those with the section
413 * type S_GB_ZEROFILL, can only be in a segment with sections of this type.
414 * These segments are then placed after all other segments.
415 *
416 * The MH_OBJECT format has all of its sections in one segment for
417 * compactness.  There is no padding to a specified segment boundary and the
418 * mach_header and load commands are not part of the segment.
419 *
420 * Sections with the same section name, sectname, going into the same segment,
421 * segname, are combined by the link editor.  The resulting section is aligned
422 * to the maximum alignment of the combined sections and is the new section's
423 * alignment.  The combined sections are aligned to their original alignment in
424 * the combined section.  Any padded bytes to get the specified alignment are
425 * zeroed.
426 *
427 * The format of the relocation entries referenced by the reloff and nreloc
428 * fields of the section structure for mach object files is described in the
429 * header file <reloc.h>.
430 */
431 struct section { /* for 32-bit architectures */
432     char        sectname[16];    /* name of this section */
433     char        segname[16];    /* segment this section goes in */
434     TYP(addr,4); /* memory address of this section */
435     TYP(size,4); /* size in bytes of this section */
436     TYP(offset,4); /* file offset of this section */
437     TYP(align,4); /* section alignment (power of 2) */
438     TYP(reloff,4); /* file offset of relocation entries */
439     TYP(nreloc,4); /* number of relocation entries */
440     TYP(flags,4); /* flags (section type and attributes)*/
441     TYP(reserved1,4); /* reserved (for offset or index) */
442     TYP(reserved2,4); /* reserved (for count or sizeof) */
443 };
444 
445 struct section_64 { /* for 64-bit architectures */
446     char        sectname[16];    /* name of this section */
447     char        segname[16];    /* segment this section goes in */
448     TYP(addr,8); /* memory address of this section */
449     TYP(size,8); /* size in bytes of this section */
450     TYP(offset,4); /* file offset of this section */
451     TYP(align,4); /* section alignment (power of 2) */
452     TYP(reloff,4); /* file offset of relocation entries */
453     TYP(nreloc,4); /* number of relocation entries */
454     TYP(flags,4); /* flags (section type and attributes)*/
455     TYP(reserved1,4); /* reserved (for offset or index) */
456     TYP(reserved2,4); /* reserved (for count or sizeof) */
457     TYP(reserved3,4); /* reserved */
458 };
459 
460 /*
461 * The flags field of a section structure is separated into two parts a section
462 * type and section attributes.  The section types are mutually exclusive (it
463 * can only have one type) but the section attributes are not (it may have more
464 * than one attribute).
465 */
466 #define SECTION_TYPE         0x000000ff    /* 256 section types */
467 #define SECTION_ATTRIBUTES     0xffffff00    /*  24 section attributes */
468 
469 /* Constants for the type of a section */
470 #define    S_REGULAR        0x0    /* regular section */
471 #define    S_ZEROFILL        0x1    /* zero fill on demand section */
472 #define    S_CSTRING_LITERALS    0x2    /* section with only literal C strings*/
473 #define    S_4BYTE_LITERALS    0x3    /* section with only 4 byte literals */
474 #define    S_8BYTE_LITERALS    0x4    /* section with only 8 byte literals */
475 #define    S_LITERAL_POINTERS    0x5    /* section with only pointers to */
476     /*  literals */
477 /*
478 * For the two types of symbol pointers sections and the symbol stubs section
479 * they have indirect symbol table entries.  For each of the entries in the
480 * section the indirect symbol table entries, in corresponding order in the
481 * indirect symbol table, start at the index stored in the reserved1 field
482 * of the section structure.  Since the indirect symbol table entries
483 * correspond to the entries in the section the number of indirect symbol table
484 * entries is inferred from the size of the section divided by the size of the
485 * entries in the section.  For symbol pointers sections the size of the entries
486 * in the section is 4 bytes and for symbol stubs sections the byte size of the
487 * stubs is stored in the reserved2 field of the section structure.
488 */
489 #define    S_NON_LAZY_SYMBOL_POINTERS    0x6    /* section with only non-lazy
490     symbol pointers */
491 #define    S_LAZY_SYMBOL_POINTERS        0x7    /* section with only lazy symbol
492     pointers */
493 #define    S_SYMBOL_STUBS            0x8    /* section with only symbol
494     stubs, byte size of stub in
495     the reserved2 field */
496 #define    S_MOD_INIT_FUNC_POINTERS    0x9    /* section with only function
497     pointers for initialization*/
498 #define    S_MOD_TERM_FUNC_POINTERS    0xa    /* section with only function
499     pointers for termination */
500 #define    S_COALESCED            0xb    /* section contains symbols that
501     are to be coalesced */
502 #define    S_GB_ZEROFILL            0xc    /* zero fill on demand section
503     (that can be larger than 4
504     gigabytes) */
505 #define    S_INTERPOSING            0xd    /* section with only pairs of
506     function pointers for
507     interposing */
508 #define    S_16BYTE_LITERALS        0xe    /* section with only 16 byte
509     literals */
510 #define    S_DTRACE_DOF            0xf    /* section contains
511     DTrace Object Format */
512 #define    S_LAZY_DYLIB_SYMBOL_POINTERS    0x10    /* section with only lazy
513     symbol pointers to lazy
514     loaded dylibs */
515 /*
516 * Section types to support thread local variables
517 */
518 #define S_THREAD_LOCAL_REGULAR                   0x11  /* template of initial
519     values for TLVs */
520 #define S_THREAD_LOCAL_ZEROFILL                  0x12  /* template of initial
521     values for TLVs */
522 #define S_THREAD_LOCAL_VARIABLES                 0x13  /* TLV descriptors */
523 #define S_THREAD_LOCAL_VARIABLE_POINTERS         0x14  /* pointers to TLV
524     descriptors */
525 #define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS    0x15  /* functions to call
526     to initialize TLV
527     values */
528 
529 /*
530 * Constants for the section attributes part of the flags field of a section
531 * structure.
532 */
533 #define SECTION_ATTRIBUTES_USR     0xff000000    /* User setable attributes */
534 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000    /* section contains only true
535     machine instructions */
536 #define S_ATTR_NO_TOC          0x40000000    /* section contains coalesced
537     symbols that are not to be
538     in a ranlib table of
539     contents */
540 #define S_ATTR_STRIP_STATIC_SYMS 0x20000000    /* ok to strip static symbols
541     in this section in files
542     with the MH_DYLDLINK flag */
543 #define S_ATTR_NO_DEAD_STRIP     0x10000000    /* no dead stripping */
544 #define S_ATTR_LIVE_SUPPORT     0x08000000    /* blocks are live if they
545     reference live blocks */
546 #define S_ATTR_SELF_MODIFYING_CODE 0x04000000    /* Used with i386 code stubs
547     written on by dyld */
548 /*
549 * If a segment contains any sections marked with S_ATTR_DEBUG then all
550 * sections in that segment must have this attribute.  No section other than
551 * a section marked with this attribute may reference the contents of this
552 * section.  A section with this attribute may contain no symbols and must have
553 * a section type S_REGULAR.  The static linker will not copy section contents
554 * from sections with this attribute into its output file.  These sections
555 * generally contain DWARF debugging info.
556 */
557 #define    S_ATTR_DEBUG         0x02000000    /* a debug section */
558 #define SECTION_ATTRIBUTES_SYS     0x00ffff00    /* system setable attributes */
559 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400    /* section contains some
560     machine instructions */
561 #define S_ATTR_EXT_RELOC     0x00000200    /* section has external
562     relocation entries */
563 #define S_ATTR_LOC_RELOC     0x00000100    /* section has local
564     relocation entries */
565 
566 
567 /*
568 * The names of segments and sections in them are mostly meaningless to the
569 * link-editor.  But there are few things to support traditional UNIX
570 * executables that require the link-editor and assembler to use some names
571 * agreed upon by convention.
572 *
573 * The initial protection of the "__TEXT" segment has write protection turned
574 * off (not writeable).
575 *
576 * The link-editor will allocate common symbols at the end of the "__common"
577 * section in the "__DATA" segment.  It will create the section and segment
578 * if needed.
579 */
580 
581 /* The currently known segment names and the section names in those segments */
582 
583 #define    SEG_PAGEZERO    "__PAGEZERO"    /* the pagezero segment which has no */
584                     /* protections and catches NULL */
585                     /* references for MH_EXECUTE files */
586 
587 
588 #define    SEG_TEXT    "__TEXT"    /* the tradition UNIX text segment */
589 #define    SECT_TEXT    "__text"    /* the real text part of the text */
590                     /* section no headers, and no padding */
591 #define SECT_FVMLIB_INIT0 "__fvmlib_init0"    /* the fvmlib initialization */
592                         /*  section */
593 #define SECT_FVMLIB_INIT1 "__fvmlib_init1"    /* the section following the */
594                             /*  fvmlib initialization */
595                         /*  section */
596 
597 #define    SEG_DATA    "__DATA"    /* the tradition UNIX data segment */
598 #define    SECT_DATA    "__data"    /* the real initialized data section */
599                     /* no padding, no bss overlap */
600 #define    SECT_BSS    "__bss"        /* the real uninitialized data section*/
601                     /* no padding */
602 #define SECT_COMMON    "__common"    /* the section common symbols are */
603                     /* allocated in by the link editor */
604 
605 #define    SEG_OBJC    "__OBJC"    /* objective-C runtime segment */
606 #define SECT_OBJC_SYMBOLS "__symbol_table"    /* symbol table */
607 #define SECT_OBJC_MODULES "__module_info"    /* module information */
608 #define SECT_OBJC_STRINGS "__selector_strs"    /* string table */
609 #define SECT_OBJC_REFS "__selector_refs"    /* string table */
610 
611 #define    SEG_ICON     "__ICON"    /* the icon segment */
612 #define    SECT_ICON_HEADER "__header"    /* the icon headers */
613 #define    SECT_ICON_TIFF   "__tiff"    /* the icons in tiff format */
614 
615 #define    SEG_LINKEDIT    "__LINKEDIT"    /* the segment containing all structs */
616                     /* created and maintained by the link */
617                     /* editor.  Created with -seglinkedit */
618                     /* option to ld(1) for MH_EXECUTE and */
619                     /* FVMLIB file types only */
620 
621 #define SEG_UNIXSTACK    "__UNIXSTACK"    /* the unix stack segment */
622 
623 #define SEG_IMPORT    "__IMPORT"    /* the segment for the self (dyld) */
624                     /* modifing code stubs that has read, */
625                     /* write and execute permissions */
626 
627 /*
628 * Fixed virtual memory shared libraries are identified by two things.  The
629 * target pathname (the name of the library as found for execution), and the
630 * minor version number.  The address of where the headers are loaded is in
631 * header_addr. (THIS IS OBSOLETE and no longer supported).
632 */
633 struct fvmlib {
634     union lc_str    name;        /* library's target pathname */
635     TYP(minor_version,4); /* library's minor version number */
636     TYP(header_addr,4); /* library's header address */
637 };
638 
639 /*
640 * A fixed virtual shared library (filetype == MH_FVMLIB in the mach header)
641 * contains a fvmlib_command (cmd == LC_IDFVMLIB) to identify the library.
642 * An object that uses a fixed virtual shared library also contains a
643 * fvmlib_command (cmd == LC_LOADFVMLIB) for each library it uses.
644 * (THIS IS OBSOLETE and no longer supported).
645 */
646 struct fvmlib_command {
647     TYP(cmd,4); /* LC_IDFVMLIB or LC_LOADFVMLIB */
648     TYP(cmdsize,4); /* includes pathname string */
649     struct fvmlib    fvmlib;        /* the library identification */
650 };
651 
652 /*
653 * Dynamicly linked shared libraries are identified by two things.  The
654 * pathname (the name of the library as found for execution), and the
655 * compatibility version number.  The pathname must match and the compatibility
656 * number in the user of the library must be greater than or equal to the
657 * library being used.  The time stamp is used to record the time a library was
658 * built and copied into user so it can be use to determined if the library used
659 * at runtime is exactly the same as used to built the program.
660 */
661 struct dylib {
662     union lc_str  name;            /* library's path name */
663     TYP(timestamp,4); /* library's build time stamp */
664     TYP(current_version,4); /* library's current version number */
665     TYP(compatibility_version,4); /* library's compatibility vers number*/
666 };
667 
668 /*
669 * A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
670 * contains a dylib_command (cmd == LC_ID_DYLIB) to identify the library.
671 * An object that uses a dynamically linked shared library also contains a
672 * dylib_command (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
673 * LC_REEXPORT_DYLIB) for each library it uses.
674 */
675 struct dylib_command {
676     TYP(cmd,4); /* LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB,
677         LC_REEXPORT_DYLIB */
678     TYP(cmdsize,4); /* includes pathname string */
679     struct dylib    dylib;        /* the library identification */
680 };
681 
682 /*
683 * A dynamically linked shared library may be a subframework of an umbrella
684 * framework.  If so it will be linked with "-umbrella umbrella_name" where
685 * Where "umbrella_name" is the name of the umbrella framework. A subframework
686 * can only be linked against by its umbrella framework or other subframeworks
687 * that are part of the same umbrella framework.  Otherwise the static link
688 * editor produces an error and states to link against the umbrella framework.
689 * The name of the umbrella framework for subframeworks is recorded in the
690 * following structure.
691 */
692 struct sub_framework_command {
693     TYP(cmd,4); /* LC_SUB_FRAMEWORK */
694     TYP(cmdsize,4); /* includes umbrella string */
695     union lc_str     umbrella;    /* the umbrella framework name */
696 };
697 
698 /*
699 * For dynamically linked shared libraries that are subframework of an umbrella
700 * framework they can allow clients other than the umbrella framework or other
701 * subframeworks in the same umbrella framework.  To do this the subframework
702 * is built with "-allowable_client client_name" and an LC_SUB_CLIENT load
703 * command is created for each -allowable_client flag.  The client_name is
704 * usually a framework name.  It can also be a name used for bundles clients
705 * where the bundle is built with "-client_name client_name".
706 */
707 struct sub_client_command {
708     TYP(cmd,4); /* LC_SUB_CLIENT */
709     TYP(cmdsize,4); /* includes client string */
710     union lc_str     client;        /* the client name */
711 };
712 
713 /*
714 * A dynamically linked shared library may be a sub_umbrella of an umbrella
715 * framework.  If so it will be linked with "-sub_umbrella umbrella_name" where
716 * Where "umbrella_name" is the name of the sub_umbrella framework.  When
717 * staticly linking when -twolevel_namespace is in effect a twolevel namespace
718 * umbrella framework will only cause its subframeworks and those frameworks
719 * listed as sub_umbrella frameworks to be implicited linked in.  Any other
720 * dependent dynamic libraries will not be linked it when -twolevel_namespace
721 * is in effect.  The primary library recorded by the static linker when
722 * resolving a symbol in these libraries will be the umbrella framework.
723 * Zero or more sub_umbrella frameworks may be use by an umbrella framework.
724 * The name of a sub_umbrella framework is recorded in the following structure.
725 */
726 struct sub_umbrella_command {
727     TYP(cmd,4); /* LC_SUB_UMBRELLA */
728     TYP(cmdsize,4); /* includes sub_umbrella string */
729     union lc_str     sub_umbrella;    /* the sub_umbrella framework name */
730 };
731 
732 /*
733 * A dynamically linked shared library may be a sub_library of another shared
734 * library.  If so it will be linked with "-sub_library library_name" where
735 * Where "library_name" is the name of the sub_library shared library.  When
736 * staticly linking when -twolevel_namespace is in effect a twolevel namespace
737 * shared library will only cause its subframeworks and those frameworks
738 * listed as sub_umbrella frameworks and libraries listed as sub_libraries to
739 * be implicited linked in.  Any other dependent dynamic libraries will not be
740 * linked it when -twolevel_namespace is in effect.  The primary library
741 * recorded by the static linker when resolving a symbol in these libraries
742 * will be the umbrella framework (or dynamic library). Zero or more sub_library
743 * shared libraries may be use by an umbrella framework or (or dynamic library).
744 * The name of a sub_library framework is recorded in the following structure.
745 * For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc".
746 */
747 struct sub_library_command {
748     TYP(cmd,4); /* LC_SUB_LIBRARY */
749     TYP(cmdsize,4); /* includes sub_library string */
750     union lc_str     sub_library;    /* the sub_library name */
751 };
752 
753 /*
754 * A program (filetype == MH_EXECUTE) that is
755 * prebound to its dynamic libraries has one of these for each library that
756 * the static linker used in prebinding.  It contains a bit vector for the
757 * modules in the library.  The bits indicate which modules are bound (1) and
758 * which are not (0) from the library.  The bit for module 0 is the low bit
759 * of the first byte.  So the bit for the Nth module is:
760 * (linked_modules[N/8] >> N%8) & 1
761 */
762 struct prebound_dylib_command {
763     TYP(cmd,4); /* LC_PREBOUND_DYLIB */
764     TYP(cmdsize,4); /* includes strings */
765     union lc_str    name;        /* library's path name */
766     TYP(nmodules,4); /* number of modules in library */
767     union lc_str    linked_modules;    /* bit vector of linked modules */
768 };
769 
770 /*
771 * A program that uses a dynamic linker contains a dylinker_command to identify
772 * the name of the dynamic linker (LC_LOAD_DYLINKER).  And a dynamic linker
773 * contains a dylinker_command to identify the dynamic linker (LC_ID_DYLINKER).
774 * A file can have at most one of these.
775 * This struct is also used for the LC_DYLD_ENVIRONMENT load command and
776 * contains string for dyld to treat like environment variable.
777 */
778 struct dylinker_command {
779     TYP(cmd,4); /* LC_ID_DYLINKER, LC_LOAD_DYLINKER or
780         LC_DYLD_ENVIRONMENT */
781     TYP(cmdsize,4); /* includes pathname string */
782     union lc_str    name;        /* dynamic linker's path name */
783 };
784 
785 /*
786 * Thread commands contain machine-specific data structures suitable for
787 * use in the thread state primitives.  The machine specific data structures
788 * follow the struct thread_command as follows.
789 * Each flavor of machine specific data structure is preceded by an unsigned
790 * long constant for the flavor of that data structure, an uint32_t
791 * that is the count of longs of the size of the state data structure and then
792 * the state data structure follows.  This triple may be repeated for many
793 * flavors.  The constants for the flavors, counts and state data structure
794 * definitions are expected to be in the header file <machine/thread_status.h>.
795 * These machine specific data structures sizes must be multiples of
796 * 4 bytes  The cmdsize reflects the total size of the thread_command
797 * and all of the sizes of the constants for the flavors, counts and state
798 * data structures.
799 *
800 * For executable objects that are unix processes there will be one
801 * thread_command (cmd == LC_UNIXTHREAD) created for it by the link-editor.
802 * This is the same as a LC_THREAD, except that a stack is automatically
803 * created (based on the shell's limit for the stack size).  Command arguments
804 * and environment variables are copied onto that stack.
805 */
806 struct thread_command {
807     TYP(cmd,4); /* LC_THREAD or LC_UNIXTHREAD */
808     TYP(cmdsize,4); /* total size of this command */
809     /* uint32_t flavor           flavor of thread state */
810     /* uint32_t count           count of longs in thread state */
811     /* struct XXX_thread_state state   thread state for this flavor */
812     /* ... */
813 };
814 
815 /*
816 * The routines command contains the address of the dynamic shared library
817 * initialization routine and an index into the module table for the module
818 * that defines the routine.  Before any modules are used from the library the
819 * dynamic linker fully binds the module that defines the initialization routine
820 * and then calls it.  This gets called before any module initialization
821 * routines (used for C++ static constructors) in the library.
822 */
823 struct routines_command { /* for 32-bit architectures */
824     TYP(cmd,4); /* LC_ROUTINES */
825     TYP(cmdsize,4); /* total size of this command */
826     TYP(init_address,4); /* address of initialization routine */
827     TYP(init_module,4); /* index into the module table that */
828         /*  the init routine is defined in */
829     TYP(reserved1,4);
830     TYP(reserved2,4);
831     TYP(reserved3,4);
832     TYP(reserved4,4);
833     TYP(reserved5,4);
834     TYP(reserved6,4);
835 };
836 
837 /*
838 * The 64-bit routines command.  Same use as above.
839 */
840 struct routines_command_64 { /* for 64-bit architectures */
841     TYP(cmd,4); /* LC_ROUTINES_64 */
842     TYP(cmdsize,4); /* total size of this command */
843     TYP(init_address,8); /* address of initialization routine */
844     TYP(init_module,8); /* index into the module table that */
845         /*  the init routine is defined in */
846     TYP(reserved1,8);
847     TYP(reserved2,8);
848     TYP(reserved3,8);
849     TYP(reserved4,8);
850     TYP(reserved5,8);
851     TYP(reserved6,8);
852 };
853 
854 /*
855 * The symtab_command contains the offsets and sizes of the link-edit 4.3BSD
856 * "stab" style symbol table information as described in the header files
857 * <nlist.h> and <stab.h>.
858 */
859 struct symtab_command {
860     TYP(cmd,4); /* LC_SYMTAB */
861     TYP(cmdsize,4); /* sizeof(struct symtab_command) */
862     TYP(symoff,4); /* symbol table offset */
863     TYP(nsyms,4); /* number of symbol table entries */
864     TYP(stroff,4); /* string table offset */
865     TYP(strsize,4); /* string table size in bytes */
866 };
867 
868 /*
869 * This is the second set of the symbolic information which is used to support
870 * the data structures for the dynamically link editor.
871 *
872 * The original set of symbolic information in the symtab_command which contains
873 * the symbol and string tables must also be present when this load command is
874 * present.  When this load command is present the symbol table is organized
875 * into three groups of symbols:
876 *    local symbols (static and debugging symbols) - grouped by module
877 *    defined external symbols - grouped by module (sorted by name if not lib)
878 *    undefined external symbols (sorted by name if MH_BINDATLOAD is not set,
879 *                         and in order the were seen by the static
880 *                    linker if MH_BINDATLOAD is set)
881 * In this load command there are offsets and counts to each of the three groups
882 * of symbols.
883 *
884 * This load command contains a the offsets and sizes of the following new
885 * symbolic information tables:
886 *    table of contents
887 *    module table
888 *    reference symbol table
889 *    indirect symbol table
890 * The first three tables above (the table of contents, module table and
891 * reference symbol table) are only present if the file is a dynamically linked
892 * shared library.  For executable and object modules, which are files
893 * containing only one module, the information that would be in these three
894 * tables is determined as follows:
895 *     table of contents - the defined external symbols are sorted by name
896 *    module table - the file contains only one module so everything in the
897 *               file is part of the module.
898 *    reference symbol table - is the defined and undefined external symbols
899 *
900 * For dynamically linked shared library files this load command also contains
901 * offsets and sizes to the pool of relocation entries for all sections
902 * separated into two groups:
903 *    external relocation entries
904 *    local relocation entries
905 * For executable and object modules the relocation entries continue to hang
906 * off the section structures.
907 */
908 struct dysymtab_command {
909     TYP(cmd,4); /* LC_DYSYMTAB */
910     TYP(cmdsize,4); /* sizeof(struct dysymtab_command) */
911 
912     /*
913     * The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
914     * are grouped into the following three groups:
915     *    local symbols (further grouped by the module they are from)
916     *    defined external symbols (further grouped by the module they are from)
917     *    undefined symbols
918     *
919     * The local symbols are used only for debugging.  The dynamic binding
920     * process may have to use them to indicate to the debugger the local
921     * symbols for a module that is being bound.
922     *
923     * The last two groups are used by the dynamic binding process to do the
924     * binding (indirectly through the module table and the reference symbol
925     * table when this is a dynamically linked shared library file).
926     */
927     TYP(ilocalsym,4); /* index to local symbols */
928     TYP(nlocalsym,4); /* number of local symbols */
929 
930     TYP(iextdefsym,4); /* index to externally defined symbols */
931     TYP(nextdefsym,4); /* number of externally defined symbols */
932 
933     TYP(iundefsym,4); /* index to undefined symbols */
934     TYP(nundefsym,4); /* number of undefined symbols */
935 
936     /*
937     * For the for the dynamic binding process to find which module a symbol
938     * is defined in the table of contents is used (analogous to the ranlib
939     * structure in an archive) which maps defined external symbols to modules
940     * they are defined in.  This exists only in a dynamically linked shared
941     * library file.  For executable and object modules the defined external
942     * symbols are sorted by name and is use as the table of contents.
943     */
944     TYP(tocoff,4); /* file offset to table of contents */
945     TYP(ntoc,4); /* number of entries in table of contents */
946 
947     /*
948     * To support dynamic binding of "modules" (whole object files) the symbol
949     * table must reflect the modules that the file was created from.  This is
950     * done by having a module table that has indexes and counts into the merged
951     * tables for each module.  The module structure that these two entries
952     * refer to is described below.  This exists only in a dynamically linked
953     * shared library file.  For executable and object modules the file only
954     * contains one module so everything in the file belongs to the module.
955     */
956     TYP(modtaboff,4); /* file offset to module table */
957     TYP(nmodtab,4); /* number of module table entries */
958 
959     /*
960     * To support dynamic module binding the module structure for each module
961     * indicates the external references (defined and undefined) each module
962     * makes.  For each module there is an offset and a count into the
963     * reference symbol table for the symbols that the module references.
964     * This exists only in a dynamically linked shared library file.  For
965     * executable and object modules the defined external symbols and the
966     * undefined external symbols indicates the external references.
967     */
968     TYP(extrefsymoff,4); /* offset to referenced symbol table */
969     TYP(nextrefsyms,4); /* number of referenced symbol table entries */
970 
971     /*
972     * The sections that contain "symbol pointers" and "routine stubs" have
973     * indexes and (implied counts based on the size of the section and fixed
974     * size of the entry) into the "indirect symbol" table for each pointer
975     * and stub.  For every section of these two types the index into the
976     * indirect symbol table is stored in the section header in the field
977     * reserved1.  An indirect symbol table entry is simply a 32bit index into
978     * the symbol table to the symbol that the pointer or stub is referring to.
979     * The indirect symbol table is ordered to match the entries in the section.
980     */
981     TYP(indirectsymoff,4); /* file offset to the indirect symbol table */
982     TYP(nindirectsyms,4); /* number of indirect symbol table entries */
983 
984     /*
985     * To support relocating an individual module in a library file quickly the
986     * external relocation entries for each module in the library need to be
987     * accessed efficiently.  Since the relocation entries can't be accessed
988     * through the section headers for a library file they are separated into
989     * groups of local and external entries further grouped by module.  In this
990     * case the presents of this load command who's extreloff, nextrel,
991     * locreloff and nlocrel fields are non-zero indicates that the relocation
992     * entries of non-merged sections are not referenced through the section
993     * structures (and the reloff and nreloc fields in the section headers are
994     * set to zero).
995     *
996     * Since the relocation entries are not accessed through the section headers
997     * this requires the r_address field to be something other than a section
998     * offset to identify the item to be relocated.  In this case r_address is
999     * set to the offset from the vmaddr of the first LC_SEGMENT command.
1000     * For MH_SPLIT_SEGS images r_address is set to the the offset from the
1001     * vmaddr of the first read-write LC_SEGMENT command.
1002     *
1003     * The relocation entries are grouped by module and the module table
1004     * entries have indexes and counts into them for the group of external
1005     * relocation entries for that the module.
1006     *
1007     * For sections that are merged across modules there must not be any
1008     * remaining external relocation entries for them (for merged sections
1009     * remaining relocation entries must be local).
1010     */
1011     TYP(extreloff,4); /* offset to external relocation entries */
1012     TYP(nextrel,4); /* number of external relocation entries */
1013 
1014     /*
1015     * All the local relocation entries are grouped together (they are not
1016     * grouped by their module since they are only used if the object is moved
1017     * from it staticly link edited address).
1018     */
1019     TYP(locreloff,4); /* offset to local relocation entries */
1020     TYP(nlocrel,4); /* number of local relocation entries */
1021 };
1022 
1023 /*
1024 * An indirect symbol table entry is simply a 32bit index into the symbol table
1025 * to the symbol that the pointer or stub is refering to.  Unless it is for a
1026 * non-lazy symbol pointer section for a defined symbol which strip(1) as
1027 * removed.  In which case it has the value INDIRECT_SYMBOL_LOCAL.  If the
1028 * symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that.
1029 */
1030 #define INDIRECT_SYMBOL_LOCAL    0x80000000
1031 #define INDIRECT_SYMBOL_ABS    0x40000000
1032 
1033 
1034 /* a table of contents entry */
1035 struct dylib_table_of_contents {
1036     TYP(symbol_index,4); /* the defined external symbol
1037         (index into the symbol table) */
1038     TYP(module_index,4); /* index into the module table this symbol
1039         is defined in */
1040 };
1041 
1042 /* a module table entry */
1043 struct dylib_module {
1044     TYP(module_name,4); /* the module name (index into string table) */
1045 
1046     TYP(iextdefsym,4); /* index into externally defined symbols */
1047     TYP(nextdefsym,4); /* number of externally defined symbols */
1048     TYP(irefsym,4); /* index into reference symbol table */
1049     TYP(nrefsym,4); /* number of reference symbol table entries */
1050     TYP(ilocalsym,4); /* index into symbols for local symbols */
1051     TYP(nlocalsym,4); /* number of local symbols */
1052 
1053     TYP(iextrel,4); /* index into external relocation entries */
1054     TYP(nextrel,4); /* number of external relocation entries */
1055 
1056     TYP(iinit_iterm,4); /* low 16 bits are the index into the init
1057         section, high 16 bits are the index into
1058         the term section */
1059     TYP(ninit_nterm,4); /* low 16 bits are the number of init section
1060         entries, high 16 bits are the number of
1061         term section entries */
1062 
1063     /* for this module address of the start of */
1064     /*  the (__OBJC,__module_info) section */
1065     TYP(objc_module_info_addr,4);
1066 
1067     /* for this module size of */
1068     /*  the (__OBJC,__module_info) section */
1069     TYP(objc_module_info_size,4);
1070 };
1071 
1072 /* a 64-bit module table entry */
1073 struct dylib_module_64 {
1074     TYP(module_name,4); /* the module name (index into string table) */
1075 
1076     TYP(iextdefsym,4); /* index into externally defined symbols */
1077     TYP(nextdefsym,4); /* number of externally defined symbols */
1078     TYP(irefsym,4); /* index into reference symbol table */
1079     TYP(nrefsym,4); /* number of reference symbol table entries */
1080     TYP(ilocalsym,4); /* index into symbols for local symbols */
1081     TYP(nlocalsym,4); /* number of local symbols */
1082 
1083     TYP(iextrel,4); /* index into external relocation entries */
1084     TYP(nextrel,4); /* number of external relocation entries */
1085 
1086     TYP(iinit_iterm,4); /* low 16 bits are the index into the init
1087         section, high 16 bits are the index into
1088         the term section */
1089     TYP(ninit_nterm,4); /* low 16 bits are the number of init section
1090         entries, high 16 bits are the number of
1091         term section entries */
1092 
1093     TYP(objc_module_info_size,4); /* for this module size of */
1094         /*  the (__OBJC,__module_info) section */
1095     TYP(objc_module_info_addr,8); /* for this module address of the start of */
1096         /*  the (__OBJC,__module_info) section */
1097 };
1098 
1099 /*
1100 * The entries in the reference symbol table are used when loading the module
1101 * (both by the static and dynamic link editors) and if the module is unloaded
1102 * or replaced.  Therefore all external symbols (defined and undefined) are
1103 * listed in the module's reference table.  The flags describe the type of
1104 * reference that is being made.  The constants for the flags are defined in
1105 * <mach-o/nlist.h> as they are also used for symbol table entries.
1106 */
1107 #if 0 /* dwarf readers not using this */
1108 struct dylib_reference {
1109     UNUSED uint32_t isym:24,        /* index into the symbol table */
1110     UNUSED flags:8;    /* flags to indicate the type of reference */
1111 };
1112 #endif /* 0 */
1113 
1114 /*
1115 * The twolevel_hints_command contains the offset and number of hints in the
1116 * two-level namespace lookup hints table.
1117 */
1118 struct twolevel_hints_command {
1119     TYP(cmd,4); /* LC_TWOLEVEL_HINTS */
1120     TYP(cmdsize,4); /* sizeof(struct twolevel_hints_command) */
1121     TYP(offset,4); /* offset to the hint table */
1122     TYP(nhints,4); /* number of hints in the hint table */
1123 };
1124 
1125 /*
1126 * The entries in the two-level namespace lookup hints table are twolevel_hint
1127 * structs.  These provide hints to the dynamic link editor where to start
1128 * looking for an undefined symbol in a two-level namespace image.  The
1129 * isub_image field is an index into the sub-images (sub-frameworks and
1130 * sub-umbrellas list) that made up the two-level image that the undefined
1131 * symbol was found in when it was built by the static link editor.  If
1132 * isub-image is 0 the the symbol is expected to be defined in library and not
1133 * in the sub-images.  If isub-image is non-zero it is an index into the array
1134 * of sub-images for the umbrella with the first index in the sub-images being
1135 * 1. The array of sub-images is the ordered list of sub-images of the umbrella
1136 * that would be searched for a symbol that has the umbrella recorded as its
1137 * primary library.  The table of contents index is an index into the
1138 * library's table of contents.  This is used as the starting point of the
1139 * binary search or a directed linear search.
1140 */
1141 #if 0
1142 /* Not used by dwarf readers. */
1143 struct twolevel_hint {
1144     UNUSED uint32_t
1145     isub_image:8,    /* index into the sub images */
1146     itoc:24;    /* index into the table of contents */
1147 };
1148 #endif
1149 
1150 /*
1151 * The prebind_cksum_command contains the value of the original check sum for
1152 * prebound files or zero.  When a prebound file is first created or modified
1153 * for other than updating its prebinding information the value of the check sum
1154 * is set to zero.  When the file has it prebinding re-done and if the value of
1155 * the check sum is zero the original check sum is calculated and stored in
1156 * cksum field of this load command in the output file.  If when the prebinding
1157 * is re-done and the cksum field is non-zero it is left unchanged from the
1158 * input file.
1159 */
1160 struct prebind_cksum_command {
1161     TYP(cmd,4); /* LC_PREBIND_CKSUM */
1162     TYP(cmdsize,4); /* sizeof(struct prebind_cksum_command) */
1163     TYP(cksum,4); /* the check sum or zero */
1164 };
1165 
1166 /*
1167 * The uuid load command contains a single 128-bit unique random number that
1168 * identifies an object produced by the static link editor.
1169 */
1170 struct uuid_command {
1171     TYP(cmd,4); /* LC_UUID */
1172     TYP(cmdsize,4); /* sizeof(struct uuid_command) */
1173     unsigned char uuid[16];    /* the 128-bit uuid */
1174 };
1175 
1176 /*
1177 * The rpath_command contains a path which at runtime should be added to
1178 * the current run path used to find @rpath prefixed dylibs.
1179 */
1180 struct rpath_command {
1181     TYP(cmd,4); /* LC_RPATH */
1182     TYP(cmdsize,4); /* includes string */
1183     union lc_str path;        /* path to add to run path */
1184 };
1185 
1186 /*
1187 * The linkedit_data_command contains the offsets and sizes of a blob
1188 * of data in the __LINKEDIT segment.
1189 */
1190 struct linkedit_data_command {
1191     TYP(cmd,4); /* LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO,
1192         LC_FUNCTION_STARTS, LC_DATA_IN_CODE,
1193         LC_DYLIB_CODE_SIGN_DRS or
1194         LC_LINKER_OPTIMIZATION_HINT. */
1195     TYP(cmdsize,4); /* sizeof(struct linkedit_data_command) */
1196     TYP(dataoff,4); /* file offset of data in __LINKEDIT segment */
1197     TYP(datasize,4); /* file size of data in __LINKEDIT segment */
1198 };
1199 
1200 /*
1201 * The encryption_info_command contains the file offset and size of an
1202 * of an encrypted segment.
1203 */
1204 struct encryption_info_command {
1205     TYP(cmd,4); /* LC_ENCRYPTION_INFO */
1206     TYP(cmdsize,4); /* sizeof(struct encryption_info_command) */
1207     TYP(cryptoff,4); /* file offset of encrypted range */
1208     TYP(cryptsize,4); /* file size of encrypted range */
1209     TYP(cryptid,4); /* which enryption system,
1210         0 means not-encrypted yet */
1211 };
1212 
1213 /*
1214 * The encryption_info_command_64 contains the file offset and size of an
1215 * of an encrypted segment (for use in x86_64 targets).
1216 */
1217 struct encryption_info_command_64 {
1218     TYP(cmd,4); /* LC_ENCRYPTION_INFO_64 */
1219     TYP(cmdsize,4); /* sizeof(struct encryption_info_command_64) */
1220     TYP(cryptoff,4); /* file offset of encrypted range */
1221     TYP(cryptsize,4); /* file size of encrypted range */
1222     TYP(cryptid,4); /* which enryption system,
1223         0 means not-encrypted yet */
1224     TYP(pad,4); /* padding to make this struct's size a multiple
1225         of 8 bytes */
1226 };
1227 
1228 /*
1229 * The version_min_command contains the min OS version on which this
1230 * binary was built to run.
1231 */
1232 struct version_min_command {
1233     TYP(cmd,4); /* LC_VERSION_MIN_MACOSX or
1234         LC_VERSION_MIN_IPHONEOS or
1235         LC_VERSION_MIN_WATCHOS or
1236         LC_VERSION_MIN_TVOS */
1237     TYP(cmdsize,4); /* sizeof(struct min_version_command) */
1238     TYP(version,4); /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
1239     TYP(sdk,4); /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
1240 };
1241 
1242 /*
1243 * The dyld_info_command contains the file offsets and sizes of
1244 * the new compressed form of the information dyld needs to
1245 * load the image.  This information is used by dyld on Mac OS X
1246 * 10.6 and later.  All information pointed to by this command
1247 * is encoded using byte streams, so no endian swapping is needed
1248 * to interpret it.
1249 */
1250 struct dyld_info_command {
1251     TYP(cmd,4); /* LC_DYLD_INFO or LC_DYLD_INFO_ONLY */
1252     TYP(cmdsize,4); /* sizeof(struct dyld_info_command) */
1253 
1254     /*
1255     * Dyld rebases an image whenever dyld loads it at an address different
1256     * from its preferred address.  The rebase information is a stream
1257     * of byte sized opcodes whose symbolic names start with REBASE_OPCODE_.
1258     * Conceptually the rebase information is a table of tuples:
1259     *    <seg-index, seg-offset, type>
1260     * The opcodes are a compressed way to encode the table by only
1261     * encoding when a column changes.  In addition simple patterns
1262     * like "every n'th offset for m times" can be encoded in a few
1263     * bytes.
1264     */
1265     TYP(rebase_off,4); /* file offset to rebase info */
1266     TYP(rebase_size,4); /* size of rebase info */
1267 
1268     /*
1269     * Dyld binds an image during the loading process, if the image
1270     * requires any pointers to be initialized to symbols in other images.
1271     * The bind information is a stream of byte sized
1272     * opcodes whose symbolic names start with BIND_OPCODE_.
1273     * Conceptually the bind information is a table of tuples:
1274     *    <seg-index, seg-offset, type, symbol-library-ordinal, symbol-name, addend>
1275     * The opcodes are a compressed way to encode the table by only
1276     * encoding when a column changes.  In addition simple patterns
1277     * like for runs of pointers initialzed to the same value can be
1278     * encoded in a few bytes.
1279     */
1280     TYP(bind_off,4); /* file offset to binding info */
1281     TYP(bind_size,4); /* size of binding info */
1282 
1283     /*
1284     * Some C++ programs require dyld to unique symbols so that all
1285     * images in the process use the same copy of some code/data.
1286     * This step is done after binding. The content of the weak_bind
1287     * info is an opcode stream like the bind_info.  But it is sorted
1288     * alphabetically by symbol name.  This enable dyld to walk
1289     * all images with weak binding information in order and look
1290     * for collisions.  If there are no collisions, dyld does
1291     * no updating.  That means that some fixups are also encoded
1292     * in the bind_info.  For instance, all calls to "operator new"
1293     * are first bound to libstdc++.dylib using the information
1294     * in bind_info.  Then if some image overrides operator new
1295     * that is detected when the weak_bind information is processed
1296     * and the call to operator new is then rebound.
1297     */
1298     TYP(weak_bind_off,4); /* file offset to weak binding info */
1299     TYP(weak_bind_size,4); /* size of weak binding info */
1300 
1301     /*
1302     * Some uses of external symbols do not need to be bound immediately.
1303     * Instead they can be lazily bound on first use.  The lazy_bind
1304     * are contains a stream of BIND opcodes to bind all lazy symbols.
1305     * Normal use is that dyld ignores the lazy_bind section when
1306     * loading an image.  Instead the static linker arranged for the
1307     * lazy pointer to initially point to a helper function which
1308     * pushes the offset into the lazy_bind area for the symbol
1309     * needing to be bound, then jumps to dyld which simply adds
1310     * the offset to lazy_bind_off to get the information on what
1311     * to bind.
1312     */
1313     TYP(lazy_bind_off,4); /* file offset to lazy binding info */
1314     TYP(lazy_bind_size,4); /* size of lazy binding infs */
1315 
1316     /*
1317     * The symbols exported by a dylib are encoded in a trie.  This
1318     * is a compact representation that factors out common prefixes.
1319     * It also reduces LINKEDIT pages in RAM because it encodes all
1320     * information (name, address, flags) in one small, contiguous range.
1321     * The export area is a stream of nodes.  The first node sequentially
1322     * is the start node for the trie.
1323     *
1324     * Nodes for a symbol start with a uleb128 that is the length of
1325     * the exported symbol information for the string so far.
1326     * If there is no exported symbol, the node starts with a zero byte.
1327     * If there is exported info, it follows the length.
1328     *
1329     * First is a uleb128 containing flags. Normally, it is followed by
1330     * a uleb128 encoded offset which is location of the content named
1331     * by the symbol from the mach_header for the image.  If the flags
1332     * is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
1333     * a uleb128 encoded library ordinal, then a zero terminated
1334     * UTF8 string.  If the string is zero length, then the symbol
1335     * is re-export from the specified dylib with the same name.
1336     * If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
1337     * the flags is two uleb128s: the stub offset and the resolver offset.
1338     * The stub is used by non-lazy pointers.  The resolver is used
1339     * by lazy pointers and must be called to get the actual address to use.
1340     *
1341     * After the optional exported symbol information is a byte of
1342     * how many edges (0-255) that this node has leaving it,
1343     * followed by each edge.
1344     * Each edge is a zero terminated UTF8 of the addition chars
1345     * in the symbol, followed by a uleb128 offset for the node that
1346     * edge points to.
1347     *
1348     */
1349     TYP(export_off,4); /* file offset to lazy binding info */
1350     TYP(export_size,4); /* size of lazy binding infs */
1351 };
1352 
1353 /*
1354 * The following are used to encode rebasing information
1355 */
1356 #define REBASE_TYPE_POINTER                    1
1357 #define REBASE_TYPE_TEXT_ABSOLUTE32                2
1358 #define REBASE_TYPE_TEXT_PCREL32                3
1359 
1360 #define REBASE_OPCODE_MASK                    0xF0
1361 #define REBASE_IMMEDIATE_MASK                    0x0F
1362 #define REBASE_OPCODE_DONE                    0x00
1363 #define REBASE_OPCODE_SET_TYPE_IMM                0x10
1364 #define REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB        0x20
1365 #define REBASE_OPCODE_ADD_ADDR_ULEB                0x30
1366 #define REBASE_OPCODE_ADD_ADDR_IMM_SCALED            0x40
1367 #define REBASE_OPCODE_DO_REBASE_IMM_TIMES            0x50
1368 #define REBASE_OPCODE_DO_REBASE_ULEB_TIMES            0x60
1369 #define REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB            0x70
1370 #define REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB    0x80
1371 
1372 
1373 /*
1374 * The following are used to encode binding information
1375 */
1376 #define BIND_TYPE_POINTER                    1
1377 #define BIND_TYPE_TEXT_ABSOLUTE32                2
1378 #define BIND_TYPE_TEXT_PCREL32                    3
1379 
1380 #define BIND_SPECIAL_DYLIB_SELF                     0
1381 #define BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE            -1
1382 #define BIND_SPECIAL_DYLIB_FLAT_LOOKUP                -2
1383 
1384 #define BIND_SYMBOL_FLAGS_WEAK_IMPORT                0x1
1385 #define BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION            0x8
1386 
1387 #define BIND_OPCODE_MASK                    0xF0
1388 #define BIND_IMMEDIATE_MASK                    0x0F
1389 #define BIND_OPCODE_DONE                    0x00
1390 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM            0x10
1391 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB            0x20
1392 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM            0x30
1393 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM        0x40
1394 #define BIND_OPCODE_SET_TYPE_IMM                0x50
1395 #define BIND_OPCODE_SET_ADDEND_SLEB                0x60
1396 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB            0x70
1397 #define BIND_OPCODE_ADD_ADDR_ULEB                0x80
1398 #define BIND_OPCODE_DO_BIND                    0x90
1399 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB            0xA0
1400 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED            0xB0
1401 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB        0xC0
1402 
1403 
1404 /*
1405 * The following are used on the flags byte of a terminal node
1406 * in the export information.
1407 */
1408 #define EXPORT_SYMBOL_FLAGS_KIND_MASK                0x03
1409 #define EXPORT_SYMBOL_FLAGS_KIND_REGULAR            0x00
1410 #define EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL            0x01
1411 #define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION            0x04
1412 #define EXPORT_SYMBOL_FLAGS_REEXPORT                0x08
1413 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER            0x10
1414 
1415 /*
1416 * The linker_option_command contains linker options embedded in object files.
1417 */
1418 struct linker_option_command {
1419     TYP(cmd,4); /* LC_LINKER_OPTION only used in MH_OBJECT filetypes */
1420     TYP(cmdsize,4);
1421     TYP(count,4); /* number of strings */
1422     /* concatenation of zero terminated UTF8 strings.
1423         Zero filled at end to align */
1424 };
1425 
1426 /*
1427 * The symseg_command contains the offset and size of the GNU style
1428 * symbol table information as described in the header file <symseg.h>.
1429 * The symbol roots of the symbol segments must also be aligned properly
1430 * in the file.  So the requirement of keeping the offsets aligned to a
1431 * multiple of a 4 bytes translates to the length field of the symbol
1432 * roots also being a multiple of a long.  Also the padding must again be
1433 * zeroed. (THIS IS OBSOLETE and no longer supported).
1434 */
1435 struct symseg_command {
1436     TYP(cmd,4); /* LC_SYMSEG */
1437     TYP(cmdsize,4); /* sizeof(struct symseg_command) */
1438     TYP(offset,4); /* symbol segment offset */
1439     TYP(size,4); /* symbol segment size in bytes */
1440 };
1441 
1442 /*
1443 * The ident_command contains a free format string table following the
1444 * ident_command structure.  The strings are null terminated and the size of
1445 * the command is padded out with zero bytes to a multiple of 4 bytes/
1446 * (THIS IS OBSOLETE and no longer supported).
1447 */
1448 struct ident_command {
1449     TYP(cmd,4); /* LC_IDENT */
1450     TYP(cmdsize,4); /* strings that follow this command */
1451 };
1452 
1453 /*
1454 * The fvmfile_command contains a reference to a file to be loaded at the
1455 * specified virtual address.  (Presently, this command is reserved for
1456 * internal use.  The kernel ignores this command when loading a program into
1457 * memory).
1458 */
1459 struct fvmfile_command {
1460     TYP(cmd,4); /* LC_FVMFILE */
1461     TYP(cmdsize,4); /* includes pathname string */
1462     union lc_str    name;        /* files pathname */
1463     TYP(header_addr,4); /* files virtual address */
1464 };
1465 
1466 
1467 /*
1468 * The entry_point_command is a replacement for thread_command.
1469 * It is used for main executables to specify the location (file offset)
1470 * of main().  If -stack_size was used at link time, the stacksize
1471 * field will contain the stack size need for the main thread.
1472 */
1473 struct entry_point_command {
1474     TYP(cmd,4); /* LC_MAIN only used in MH_EXECUTE filetypes */
1475     TYP(cmdsize,4); /* 24 */
1476     TYP(entryoff,8); /* file (__TEXT) offset of main() */
1477     TYP(stacksize,8); /* if not zero, initial stack size */
1478 };
1479 
1480 
1481 /*
1482 * The source_version_command is an optional load command containing
1483 * the version of the sources used to build the binary.
1484 */
1485 struct source_version_command {
1486     TYP(cmd,4); /* LC_SOURCE_VERSION */
1487     TYP(cmdsize,4); /* 16 */
1488     TYP(version,8); /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
1489 };
1490 
1491 
1492 /*
1493 * The LC_DATA_IN_CODE load commands uses a linkedit_data_command
1494 * to point to an array of data_in_code_entry entries. Each entry
1495 * describes a range of data in a code section.
1496 */
1497 struct data_in_code_entry {
1498     TYP(offset,4); /* from mach_header to start of data range*/
1499     TYP(length,2); /* number of bytes in data range */
1500     TYP(kind,2); /* a DICE_KIND_* value */
1501 };
1502 #define DICE_KIND_DATA              0x0001
1503 #define DICE_KIND_JUMP_TABLE8       0x0002
1504 #define DICE_KIND_JUMP_TABLE16      0x0003
1505 #define DICE_KIND_JUMP_TABLE32      0x0004
1506 #define DICE_KIND_ABS_JUMP_TABLE32  0x0005
1507 
1508 
1509 
1510 /*
1511 * Sections of type S_THREAD_LOCAL_VARIABLES contain an array
1512 * of tlv_descriptor structures.
1513 */
1514 struct tlv_descriptor
1515 {
1516     void*          (*thunk)(struct tlv_descriptor*);
1517     unsigned long  key;
1518     unsigned long  offset;
1519 };
1520 #ifdef __cplusplus
1521 }
1522 #endif /* __cplusplus */
1523 #endif /* MACHO_LOADER_H */
1524