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