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