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