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