source: trunk/kLdr/kLdrModMachO.h@ 3525

Last change on this file since 3525 was 3525, checked in by bird, 18 years ago

made the format headers usable externally.

File size: 40.9 KB
Line 
1/* $Id $ */
2/** @file
3 * Mach-0 structures, types and defines.
4 */
5
6#ifndef __kLdrModMachO_h__
7#define __kLdrModMachO_h__
8
9#include "kLdrBase.h"
10
11/** @defgroup grp_mach_o The Mach-O Structures, Types, and Defines.
12 * @{
13 */
14
15
16#ifndef IMAGE_FAT_SIGNATURE
17/** The FAT signature (universal binaries). */
18# define IMAGE_FAT_SIGNATURE UINT32_C(0xcafebabe)
19#endif
20#ifndef IMAGE_FAT_SIGNATURE_OE
21/** The FAT signature (universal binaries), other endian. */
22# define IMAGE_FAT_SIGNATURE_OE UINT32_C(0xbebafeca)
23#endif
24
25/**
26 * The fat header found at the start of universal binaries.
27 * It is followed by \a nfat_arch numbers of \a fat_arch structures.
28 */
29typedef struct fat_header
30{
31 uint32_t magic;
32 uint32_t nfat_arch;
33} fat_header_t;
34
35/**
36 * Description of fat file item.
37 */
38typedef struct fat_arch
39{
40 int32_t cputype;
41 int32_t cpusubtype;
42 uint32_t offset;
43 uint32_t size;
44 uint32_t align; /**< Power of 2. */
45} fat_arch_t;
46
47
48
49#ifndef IMAGE_MACHO32_SIGNATURE
50/** The 32-bit Mach-O signature. */
51# define IMAGE_MACHO32_SIGNATURE UINT32_C(0xfeedface)
52#endif
53#ifndef IMAGE_MACHO32_SIGNATURE_OE
54/** The 32-bit Mach-O signature, other endian. */
55# define IMAGE_MACHO32_SIGNATURE_OE UINT32_C(0xcefaedfe)
56#endif
57#define MH_MAGIC IMAGE_MACHO32_SIGNATURE
58#define MH_CIGAM IMAGE_MACHO32_SIGNATURE_OE
59
60/**
61 * 32-bit Mach-O header.
62 * This is followed by \a ncmds number of load commands.
63 * @see mach_header_64
64 */
65typedef struct mach_header_32
66{
67 uint32_t magic;
68 int32_t cputype;
69 int32_t cpusubtype;
70 uint32_t filetype;
71 uint32_t ncmds;
72 uint32_t sizeofcmds;
73 uint32_t flags;
74} mach_header_32_t;
75
76
77
78#ifndef IMAGE_MACHO64_SIGNATURE
79/** The 64-bit Mach-O signature. */
80# define IMAGE_MACHO64_SIGNATURE UINT32_C(0xfeedfacf)
81#endif
82#ifndef IMAGE_MACHO64_SIGNATURE_OE
83/** The 64-bit Mach-O signature, other endian. */
84# define IMAGE_MACHO64_SIGNATURE_OE UINT32_C(0xfefaedfe)
85#endif
86#define MH_MAGIC_64 IMAGE_MACHO64_SIGNATURE
87#define MH_CIGAM_64 IMAGE_MACHO64_SIGNATURE_OE
88
89/**
90 * 64-bit Mach-O header.
91 * This is followed by \a ncmds number of load commands.
92 * @see mach_header
93 */
94typedef struct mach_header_64
95{
96 uint32_t magic;
97 int32_t cputype;
98 int32_t cpusubtype;
99 uint32_t filetype;
100 uint32_t ncmds;
101 uint32_t sizeofcmds;
102 uint32_t flags;
103 uint32_t reserved; /**< (for proper struct and command alignment I guess) */
104} mach_header_64_t;
105
106
107/** @name File types (mach_header_64::filetype, mach_header_32::filetype)
108 * @{
109 */
110#define MH_OBJECT UINT32_C(1) /**< Object (relocatable). */
111#define MH_EXECUTE UINT32_C(2) /**< Executable (demand paged). */
112#define MH_FVMLIB UINT32_C(3) /**< Fixed VM shared library. */
113#define MH_CORE UINT32_C(4) /**< Core file. */
114#define MH_PRELOAD UINT32_C(5) /**< Preloaded executable. */
115#define MH_DYLIB UINT32_C(6) /**< Dynamically bound shared library. */
116#define MH_DYLINKER UINT32_C(7) /**< Dynamic linker. */
117#define MH_BUNDLE UINT32_C(8) /**< Dymamically bound bundle. */
118#define MH_DYLIB_STUB UINT32_C(9) /**< Shared library stub for static linking. */
119#define MH_DSYM UINT32_C(10)/**< Debug symbols. */
120
121/** @} */
122
123
124/** @name Mach-O Header flags (mach_header_64::flags, mach_header_32::flags)
125 * @{
126 */
127#define MH_NOUNDEFS UINT32_C(0x00000001) /**< No undefined symbols. */
128#define MH_INCRLINK UINT32_C(0x00000002) /**< Partial increment link output. */
129#define MH_DYLDLINK UINT32_C(0x00000004) /**< Food for the dynamic linker, not for ld. */
130#define MH_BINDATLOAD UINT32_C(0x00000008) /**< Bind all undefined symbols at load time. */
131#define MH_PREBOUND UINT32_C(0x00000010) /**< Contains prebound undefined symbols. */
132#define MH_SPLIT_SEGS UINT32_C(0x00000020) /**< Read-only and read-write segments are split. */
133#define MH_LAZY_INIT UINT32_C(0x00000040) /**< Obsolete flag for doing lazy init when data is written. */
134#define MH_TWOLEVEL UINT32_C(0x00000080) /**< Uses two-level name space bindings. */
135#define MH_FORCE_FLAT UINT32_C(0x00000100) /**< Task: The executable forces all images to use flat name space bindings. */
136#define MH_NOMULTIDEFS UINT32_C(0x00000200) /**< No multiple symbol definitions, safe to use two-level namespace hints. */
137#define MH_NOFIXPREBINDING UINT32_C(0x00000400) /**< The dynamic linker should not notify the prebinding agent about this executable. */
138#define MH_PREBINDABLE UINT32_C(0x00000800) /**< Not prebound, but it can be. Invalid if MH_PREBOUND is set. */
139#define MH_ALLMODSBOUND UINT32_C(0x00001000) /**< Binds to all two-level namespace modules of preqs. Requires MH_PREBINDABLE and MH_TWOLEVEL to be set. */
140#define MH_SUBSECTIONS_VIA_SYMBOLS UINT32_C(0x00002000) /**< Safe to divide sections into sub-sections via symbols for dead code stripping. */
141#define MH_CANONICAL UINT32_C(0x00004000) /**< Canonicalized via unprebind. */
142#define MH_WEAK_DEFINES UINT32_C(0x00008000) /**< The (finally) linked image has weak symbols. */
143#define MH_BINDS_TO_WEAK UINT32_C(0x00010000) /**< The (finally) linked image uses weak symbols. */
144#define MH_ALLOW_STACK_EXECUTION UINT32_C(0x00020000) /**< Task: allow stack execution. (MH_EXECUTE only) */
145#define MH_VALID_FLAGS UINT32_C(0x0003ffff) /**< Mask containing the defined flags. */
146/** @} */
147
148
149/** @name CPU types / bits (mach_header_64::cputype, mach_header_32::cputype, fat_arch::cputype)
150 * @{
151 */
152#define CPU_ARCH_MASK INT32_C(0xff000000)
153#define CPU_ARCH_ABI64 INT32_C(0x01000000)
154#define CPU_TYPE_ANY INT32_C(-1)
155#define CPU_TYPE_VAX INT32_C(1)
156#define CPU_TYPE_MC680x0 INT32_C(6)
157#define CPU_TYPE_X86 INT32_C(7)
158#define CPU_TYPE_I386 CPU_TYPE_X86
159#define CPU_TYPE_X86_64 (CPU_TYPE_X86 | CPU_ARCH_ABI64)
160#define CPU_TYPE_MC98000 INT32_C(10)
161#define CPU_TYPE_HPPA INT32_C(11)
162#define CPU_TYPE_MC88000 INT32_C(13)
163#define CPU_TYPE_SPARC INT32_C(14)
164#define CPU_TYPE_I860 INT32_C(15)
165#define CPU_TYPE_POWERPC INT32_C(18)
166#define CPU_TYPE_POWERPC64 (CPU_TYPE_POWERPC | CPU_ARCH_ABI64)
167/** @} */
168
169
170/** @name CPU subtypes (mach_header_64::cpusubtype, mach_header_32::cpusubtype, fat_arch::cpusubtype)
171 * @{ */
172#define CPU_SUBTYPE_MULTIPLE INT32_C(-1)
173#define CPU_SUBTYPE_LITTLE_ENDIAN INT32_C(0) /**< figure this one out. */
174#define CPU_SUBTYPE_BIG_ENDIAN INT32_C(1) /**< ditto */
175
176/* VAX */
177#define CPU_SUBTYPE_VAX_ALL INT32_C(0)
178#define CPU_SUBTYPE_VAX780 INT32_C(1)
179#define CPU_SUBTYPE_VAX785 INT32_C(2)
180#define CPU_SUBTYPE_VAX750 INT32_C(3)
181#define CPU_SUBTYPE_VAX730 INT32_C(4)
182#define CPU_SUBTYPE_UVAXI INT32_C(5)
183#define CPU_SUBTYPE_UVAXII INT32_C(6)
184#define CPU_SUBTYPE_VAX8200 INT32_C(7)
185#define CPU_SUBTYPE_VAX8500 INT32_C(8)
186#define CPU_SUBTYPE_VAX8600 INT32_C(9)
187#define CPU_SUBTYPE_VAX8650 INT32_C(10)
188#define CPU_SUBTYPE_VAX8800 INT32_C(11)
189#define CPU_SUBTYPE_UVAXIII INT32_C(12)
190
191/* MC680xx */
192#define CPU_SUBTYPE_MC680x0_ALL INT32_C(1)
193#define CPU_SUBTYPE_MC68030 INT32_C(1)
194#define CPU_SUBTYPE_MC68040 INT32_C(2)
195#define CPU_SUBTYPE_MC68030_ONLY INT32_C(3)
196
197/* I386 */
198#define CPU_SUBTYPE_INTEL(fam, model) ( (int32_t)(((model) << 4) | (fam)) )
199#define CPU_SUBTYPE_INTEL_FAMILY(subtype) ( (subtype) & 0xf )
200#define CPU_SUBTYPE_INTEL_MODEL(subtype) ( (subtype) >> 4 )
201#define CPU_SUBTYPE_INTEL_FAMILY_MAX 0xf
202#define CPU_SUBTYPE_INTEL_MODEL_ALL 0
203
204#define CPU_SUBTYPE_I386_ALL CPU_SUBTYPE_INTEL(3, 0)
205#define CPU_SUBTYPE_386 CPU_SUBTYPE_INTEL(3, 0)
206#define CPU_SUBTYPE_486 CPU_SUBTYPE_INTEL(4, 0)
207#define CPU_SUBTYPE_486SX CPU_SUBTYPE_INTEL(4, 8)
208#define CPU_SUBTYPE_586 CPU_SUBTYPE_INTEL(5, 0)
209#define CPU_SUBTYPE_PENT CPU_SUBTYPE_INTEL(5, 0)
210#define CPU_SUBTYPE_PENTPRO CPU_SUBTYPE_INTEL(6, 1)
211#define CPU_SUBTYPE_PENTII_M3 CPU_SUBTYPE_INTEL(6, 3)
212#define CPU_SUBTYPE_PENTII_M5 CPU_SUBTYPE_INTEL(6, 5)
213#define CPU_SUBTYPE_CELERON CPU_SUBTYPE_INTEL(7, 6)
214#define CPU_SUBTYPE_CELERON_MOBILE CPU_SUBTYPE_INTEL(7, 7)
215#define CPU_SUBTYPE_PENTIUM_3 CPU_SUBTYPE_INTEL(8, 0)
216#define CPU_SUBTYPE_PENTIUM_3_M CPU_SUBTYPE_INTEL(8, 1)
217#define CPU_SUBTYPE_PENTIUM_3_XEON CPU_SUBTYPE_INTEL(8, 2)
218#define CPU_SUBTYPE_PENTIUM_M CPU_SUBTYPE_INTEL(9, 0)
219#define CPU_SUBTYPE_PENTIUM_4 CPU_SUBTYPE_INTEL(10, 0)
220#define CPU_SUBTYPE_PENTIUM_4_M CPU_SUBTYPE_INTEL(10, 1)
221#define CPU_SUBTYPE_ITANIUM CPU_SUBTYPE_INTEL(11, 0)
222#define CPU_SUBTYPE_ITANIUM_2 CPU_SUBTYPE_INTEL(11, 1)
223#define CPU_SUBTYPE_XEON CPU_SUBTYPE_INTEL(12, 0)
224#define CPU_SUBTYPE_XEON_MP CPU_SUBTYPE_INTEL(12, 1)
225
226/* X86 */
227#define CPU_SUBTYPE_X86_ALL INT32_C(3) /* CPU_SUBTYPE_I386_ALL */
228#define CPU_SUBTYPE_X86_64_ALL INT32_C(3) /* CPU_SUBTYPE_I386_ALL */
229#define CPU_SUBTYPE_X86_ARCH1 INT32_C(4) /* CPU_SUBTYPE_I486_ALL */
230
231/* MIPS */
232#define CPU_SUBTYPE_MIPS_ALL INT32_C(0)
233#define CPU_SUBTYPE_MIPS_R2300 INT32_C(1)
234#define CPU_SUBTYPE_MIPS_R2600 INT32_C(2)
235#define CPU_SUBTYPE_MIPS_R2800 INT32_C(3)
236#define CPU_SUBTYPE_MIPS_R2000a INT32_C(4)
237#define CPU_SUBTYPE_MIPS_R2000 INT32_C(5)
238#define CPU_SUBTYPE_MIPS_R3000a INT32_C(6)
239#define CPU_SUBTYPE_MIPS_R3000 INT32_C(7)
240
241/* MC98000 (PowerPC) */
242#define CPU_SUBTYPE_MC98000_ALL INT32_C(0)
243#define CPU_SUBTYPE_MC98601 INT32_C(1)
244
245/* HP-PA */
246#define CPU_SUBTYPE_HPPA_ALL INT32_C(0)
247#define CPU_SUBTYPE_HPPA_7100 INT32_C(0)
248#define CPU_SUBTYPE_HPPA_7100LC INT32_C(1)
249
250/* MC88000 */
251#define CPU_SUBTYPE_MC88000_ALL INT32_C(0)
252#define CPU_SUBTYPE_MC88100 INT32_C(1)
253#define CPU_SUBTYPE_MC88110 INT32_C(2)
254
255/* SPARC */
256#define CPU_SUBTYPE_SPARC_ALL INT32_C(0)
257
258/* I860 */
259#define CPU_SUBTYPE_I860_ALL INT32_C(0)
260#define CPU_SUBTYPE_I860_860 INT32_C(1)
261
262/* PowerPC */
263#define CPU_SUBTYPE_POWERPC_ALL INT32_C(0)
264#define CPU_SUBTYPE_POWERPC_601 INT32_C(1)
265#define CPU_SUBTYPE_POWERPC_602 INT32_C(2)
266#define CPU_SUBTYPE_POWERPC_603 INT32_C(3)
267#define CPU_SUBTYPE_POWERPC_603e INT32_C(4)
268#define CPU_SUBTYPE_POWERPC_603ev INT32_C(5)
269#define CPU_SUBTYPE_POWERPC_604 INT32_C(6)
270#define CPU_SUBTYPE_POWERPC_604e INT32_C(7)
271#define CPU_SUBTYPE_POWERPC_620 INT32_C(8)
272#define CPU_SUBTYPE_POWERPC_750 INT32_C(9)
273#define CPU_SUBTYPE_POWERPC_7400 INT32_C(10)
274#define CPU_SUBTYPE_POWERPC_7450 INT32_C(11)
275#define CPU_SUBTYPE_POWERPC_Max INT32_C(10)
276#define CPU_SUBTYPE_POWERPC_SCVger INT32_C(11)
277#define CPU_SUBTYPE_POWERPC_970 INT32_C(100)
278
279/** @} */
280
281
282
283/** @defgroup grp_macho_o_lc Load Commands
284 * @{ */
285
286/**
287 * The load command common core structure.
288 *
289 * After the Mach-O header follows an array of variable sized
290 * load command which all has this header in common.
291 */
292typedef struct load_command
293{
294 uint32_t cmd; /**< The load command id. */
295 uint32_t cmdsize; /**< The size of the command (including this header). */
296} load_command_t;
297
298/** @name Load Command IDs (load_command::cmd)
299 * @{
300 */
301/** Flag that when set requires the dynamic linker to fail if it doesn't
302 * grok the command. The dynamic linker will otherwise ignore commands it
303 * doesn't understand. Introduced with Mac OS X 10.1. */
304#define LC_REQ_DYLD UINT32_C(0x80000000)
305
306#define LC_SEGMENT_32 UINT32_C(0x01) /**< Segment to be mapped (32-bit). See segment_command_32. */
307#define LC_SYMTAB UINT32_C(0x02) /**< 'stab' symbol table. See symtab_command. */
308#define LC_SYMSEG UINT32_C(0x03) /**< Obsoleted gdb symbol table. */
309#define LC_THREAD UINT32_C(0x04) /**< Thread. See thread_command. */
310#define LC_UNIXTHREAD UINT32_C(0x05) /**< Unix thread (includes stack and stuff). See thread_command. */
311#define LC_LOADFVMLIB UINT32_C(0x06) /**< Load a specified fixed VM shared library (obsolete?). See fvmlib_command. */
312#define LC_IDFVMLIB UINT32_C(0x07) /**< Fixed VM shared library id (obsolete?). See fvmlib_command. */
313#define LC_IDENT UINT32_C(0x08) /**< Identification info (obsolete). See ident_command. */
314#define LC_FVMFILE UINT32_C(0x09) /**< Fixed VM file inclusion (internal). See fvmfile_command. */
315#define LC_PREPAGE UINT32_C(0x0a) /**< Prepage command (internal). See ?? */
316#define LC_DYSYMTAB UINT32_C(0x0b) /**< Symbol table for dynamic linking. See dysymtab_command. */
317#define LC_LOAD_DYLIB UINT32_C(0x0c) /**< Load a dynamically linked shared library. See dylib_command. */
318#define LC_ID_DYLIB UINT32_C(0x0d) /**< Dynamically linked share library ident. See dylib_command. */
319#define LC_LOAD_DYLINKER UINT32_C(0x0e) /**< Load a dynamical link editor. See dylinker_command. */
320#define LC_ID_DYLINKER UINT32_C(0x0f) /**< Dynamic link editor ident. See dylinker_command. */
321#define LC_PREBOUND_DYLIB UINT32_C(0x10) /**< Prebound modules for dynamically linking of a shared lib. See prebound_dylib_command. */
322#define LC_ROUTINES UINT32_C(0x11) /**< Image routines. See routines_command_32. */
323#define LC_SUB_FRAMEWORK UINT32_C(0x12) /**< Sub framework. See sub_framework_command. */
324#define LC_SUB_UMBRELLA UINT32_C(0x13) /**< Sub umbrella. See sub_umbrella_command. */
325#define LC_SUB_CLIENT UINT32_C(0x14) /**< Sub client. See sub_client_command. */
326#define LC_SUB_LIBRARY UINT32_C(0x15) /**< Sub library. See sub_library_command. */
327#define LC_TWOLEVEL_HINTS UINT32_C(0x16) /**< Two-level namespace lookup hints. See twolevel_hints_command. */
328#define LC_PREBIND_CKSUM UINT32_C(0x17) /**< Prebind checksum. See prebind_cksum_command. */
329#define LC_LOAD_WEAK_DYLIB (UINT32_C(0x18) | LC_REQ_DYLD) /**< Dylib that can be missing, all symbols weak. See dylib_command. */
330#define LC_SEGMENT_64 UINT32_C(0x19) /**< segment to be mapped (64-bit). See segment_command_32. */
331#define LC_ROUTINES_64 UINT32_C(0x1a) /**< Image routines (64-bit). See routines_command_32. */
332#define LC_UUID UINT32_C(0x1b) /**< The UUID of the object module. See uuid_command. */
333/** @} */
334
335
336/**
337 * Load Command String.
338 */
339typedef struct lc_str
340{
341 /** Offset of the string relative to the load_command structure.
342 * The string is zero-terminated. the size of the load command
343 * is zero padded up to a multiple of 4 bytes. */
344 uint32_t offset;
345} lc_str_t;
346
347
348/**
349 * Segment load command (32-bit).
350 */
351typedef struct segment_command_32
352{
353 uint32_t cmd; /**< LC_SEGMENT */
354 uint32_t cmdsize; /**< sizeof(self) + sections. */
355 char segname[16]; /**< The segment name. */
356 uint32_t vmaddr; /**< Memory address of this segment. */
357 uint32_t vmsize; /**< Size of this segment. */
358 uint32_t fileoff; /**< The file location of the segment. */
359 uint32_t filesize; /**< The file size of the segment. */
360 uint32_t maxprot; /**< Maximum VM protection. */
361 uint32_t initprot; /**< Initial VM protection. */
362 uint32_t nsects; /**< Number of section desciptors following this structure. */
363 uint32_t flags; /**< Flags (SG_*). */
364} segment_command_32_t;
365
366
367/**
368 * Segment load command (64-bit).
369 * Same as segment_command_32 except 4 members has been blown up to 64-bit.
370 */
371typedef struct segment_command_64
372{
373 uint32_t cmd; /**< LC_SEGMENT */
374 uint32_t cmdsize; /**< sizeof(self) + sections. */
375 char segname[16]; /**< The segment name. */
376 uint64_t vmaddr; /**< Memory address of this segment. */
377 uint64_t vmsize; /**< Size of this segment. */
378 uint64_t fileoff; /**< The file location of the segment. */
379 uint64_t filesize; /**< The file size of the segment. */
380 uint32_t maxprot; /**< Maximum VM protection. */
381 uint32_t initprot; /**< Initial VM protection. */
382 uint32_t nsects; /**< Number of section desciptors following this structure. */
383 uint32_t flags; /**< Flags (SG_*). */
384} segment_command_64_t;
385
386/** @name Segment flags (segment_command_64::flags, segment_command_32::flags)
387 * @{ */
388/** Map the file bits in the top end of the memory area for the segment
389 * instead of the low end. Intended for stacks in core dumps.
390 * The part of the segment memory not covered by file bits will be zeroed. */
391#define SG_HIGHVM UINT32_C(0x00000001)
392/** This segment is the virtual memory allocated by a fixed VM library.
393 * (Used for overlap checking in the linker.) */
394#define SG_FVMLIB UINT32_C(0x00000002)
395/** No relocations for or symbols that's relocated to in this segment.
396 * The segment can therefore safely be replaced. */
397#define SG_NORELOC UINT32_C(0x00000004)
398/** The segment is protected.
399 * The first page isn't protected if it starts at file offset 0
400 * (so that the mach header and this load command can be easily mapped). */
401#define SG_PROTECTED_VERSION_1 UINT32_C(0x00000008)
402/** @} */
403
404
405/**
406 * 32-bit section (part of a segment load command).
407 */
408typedef struct section_32
409{
410 char sectname[16]; /**< The section name. */
411 char segname[16]; /**< The name of the segment this section goes into. */
412 uint32_t addr; /**< The memory address of this section. */
413 uint32_t size; /**< The size of this section. */
414 uint32_t offset; /**< The file offset of this section. */
415 uint32_t align; /**< The section alignment (**2). */
416 uint32_t reloff; /**< The file offset of the relocations. */
417 uint32_t nreloc; /**< The number of relocations. */
418 uint32_t flags; /**< The section flags; section type and attribs */
419 uint32_t reserved1; /**< Reserved / offset / index. */
420 uint32_t reserved2; /**< Reserved / count / sizeof. */
421} section_32_t;
422
423/**
424 * 64-bit section (part of a segment load command).
425 */
426typedef struct section_64
427{
428 char sectname[16]; /**< The section name. */
429 char segname[16]; /**< The name of the segment this section goes into. */
430 uint64_t addr; /**< The memory address of this section. */
431 uint64_t size; /**< The size of this section. */
432 uint32_t offset; /**< The file offset of this section. */
433 uint32_t align; /**< The section alignment (**2). */
434 uint32_t reloff; /**< The file offset of the relocations. */
435 uint32_t nreloc; /**< The number of relocations. */
436 uint32_t flags; /**< The section flags; section type and attribs */
437 uint32_t reserved1; /**< Reserved / offset / index. */
438 uint32_t reserved2; /**< Reserved / count / sizeof. */
439 uint32_t reserved3; /**< (Just) Reserved. */
440} section_64_t;
441
442/** @name Section flags (section_64::flags, section_32::flags)
443 * @{
444 */
445/** Section type mask. */
446#define SECTION_TYPE UINT32_C(0x000000ff)
447/** Regular section. */
448#define S_REGULAR 0x0
449/** Zero filled section. */
450#define S_ZEROFILL 0x1
451/** C literals. */
452#define S_CSTRING_LITERALS 0x2
453/** 4 byte literals. */
454#define S_4BYTE_LITERALS 0x3
455/** 8 byte literals. */
456#define S_8BYTE_LITERALS 0x4
457/** Pointer to literals. */
458#define S_LITERAL_POINTERS 0x5
459/** Section containing non-lazy symbol pointers.
460 * Reserved1 == start index in the indirect symbol table. */
461#define S_NON_LAZY_SYMBOL_POINTERS 0x6
462/** Section containing lazy symbol pointers.
463 * Reserved1 == start index in the indirect symbol table. */
464#define S_LAZY_SYMBOL_POINTERS 0x7
465/** Section containing symbol stubs.
466 * Reserved2 == stub size. */
467#define S_SYMBOL_STUBS 0x8
468/** Section containing function pointers for module initialization. . */
469#define S_MOD_INIT_FUNC_POINTERS 0x9
470/** Section containing function pointers for module termination. . */
471#define S_MOD_TERM_FUNC_POINTERS 0xa
472/** Section containing symbols that are to be coalesced. */
473#define S_COALESCED 0xb
474/** Zero filled section that be larger than 4GB. */
475#define S_GB_ZEROFILL 0xc
476/** Section containing pairs of function pointers for interposing. */
477#define S_INTERPOSING 0xd
478/** 16 byte literals. */
479#define S_16BYTE_LITERALS 0xe
480
481/** Section attribute mask. */
482#define SECTION_ATTRIBUTES UINT32_C(0xffffff00)
483
484/** User settable attribute mask. */
485#define SECTION_ATTRIBUTES_USR UINT32_C(0xff000000)
486/** Pure instruction (code). */
487#define S_ATTR_PURE_INSTRUCTIONS UINT32_C(0x80000000)
488/** ranlib, ignore my symbols... */
489#define S_ATTR_NO_TOC UINT32_C(0x40000000)
490/** May strip static symbols when linking int a MH_DYLDLINK file. */
491#define S_ATTR_STRIP_STATIC_SYMS UINT32_C(0x20000000)
492/** No dead stripping. */
493#define S_ATTR_NO_DEAD_STRIP UINT32_C(0x10000000)
494/** Live support. */
495#define S_ATTR_LIVE_SUPPORT UINT32_C(0x08000000)
496/** Contains self modifying code (generally i386 code stub for dyld). */
497#define S_ATTR_SELF_MODIFYING_CODE UINT32_C(0x04000000)
498/** Debug info (DWARF usually). */
499#define S_ATTR_DEBUG UINT32_C(0x02000000)
500
501/** System settable attribute mask. */
502#define SECTION_ATTRIBUTES_SYS UINT32_C(0x00ffff00)
503/** Contains some instructions (code). */
504#define S_ATTR_SOME_INSTRUCTIONS UINT32_C(0x00000400)
505/** Has external relocations. */
506#define S_ATTR_EXT_RELOC UINT32_C(0x00000200)
507/** Has internal (local) relocations. */
508#define S_ATTR_LOC_RELOC UINT32_C(0x00000100)
509/** @} */
510
511/** @name Known Segment and Section Names.
512 * Some of these implies special linker behaviour.
513 * @{
514 */
515/** Page zero - not-present page for catching invalid access. (MH_EXECUTE typically) */
516#define SEG_PAGEZERO "__PAGEZERO"
517/** Traditional UNIX text segment.
518 * Defaults to R-X. */
519#define SEG_TEXT "__TEXT"
520/** The text part of SEG_TEXT. */
521#define SECT_TEXT "__text"
522/** The fvmlib initialization. */
523#define SECT_FVMLIB_INIT0 "__fvmlib_init0"
524/** The section following the fvmlib initialization. */
525#define SECT_FVMLIB_INIT1 "__fvmlib_init1"
526/** The traditional UNIX data segment. (DGROUP to DOS and OS/2 people.) */
527#define SEG_DATA "__DATA"
528/** The initialized data section. */
529#define SECT_DATA "__data"
530/** The uninitialized data section. */
531#define SECT_BSS "__bss"
532/** The common symbol section. */
533#define SECT_COMMON "__common"
534/** Objective-C runtime segment. */
535#define SEG_OBJC "__OBJC"
536/** Objective-C symbol table section. */
537#define SECT_OBJC_SYMBOLS "__symbol_table"
538/** Objective-C module information section. */
539#define SECT_OBJC_MODULES "__module_info"
540/** Objective-C string table section. */
541#define SECT_OBJC_STRINGS "__selector_strs"
542/** Objective-C string table section. */
543#define SECT_OBJC_REFS "__selector_refs"
544/** Icon segment. */
545#define SEG_ICON "__ICON"
546/** The icon headers. */
547#define SECT_ICON_HEADER "__header"
548/** The icons in the TIFF format. */
549#define SECT_ICON_TIFF "__tiff"
550/** ld -seglinkedit segment containing all the structs create and maintained
551 * by the linker. MH_EXECUTE and MH_FVMLIB only. */
552#define SEG_LINKEDIT "__LINKEDIT"
553/** The unix stack segment. */
554#define SEG_UNIXSTACK "__UNIXSTACK"
555/** The segment for the self modifying code for dynamic linking.
556 * Implies RWX permissions. */
557#define SEG_IMPORT "__IMPORT"
558/** @} */
559
560
561/** @todo fvmlib */
562/** @todo fvmlib_command (LC_IDFVMLIB or LC_LOADFVMLIB) */
563/** @todo dylib */
564/** @todo dylib_command (LC_ID_DYLIB, LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB) */
565/** @todo sub_framework_command (LC_SUB_FRAMEWORK) */
566/** @todo sub_client_command (LC_SUB_CLIENT) */
567/** @todo sub_umbrella_command (LC_SUB_UMBRELLA) */
568/** @todo sub_library_command (LC_SUB_LIBRARY) */
569/** @todo prebound_dylib_command (LC_PREBOUND_DYLIB) */
570/** @todo dylinker_command (LC_ID_DYLINKER or LC_LOAD_DYLINKER) */
571
572
573/**
574 * Thread command.
575 *
576 * State description of a thread that is to be created. The description
577 * is made up of a number of state structures preceded by a 32-bit flavor
578 * and 32-bit count field stating the kind of stat structure and it's size
579 * in uint32_t items respecitvly.
580 *
581 * LC_UNIXTHREAD differs from LC_THREAD in that it implies stack creation
582 * and that it's started with the typical main(int, char **, char **) frame
583 * on the stack.
584 */
585typedef struct thread_command
586{
587 uint32_t cmd; /**< LC_UNIXTHREAD or LC_THREAD. */
588 uint32_t cmdsize; /**< The size of the command (including this header). */
589} thread_command_t;
590
591
592/** @todo routines_command (LC_ROUTINES) */
593/** @todo routines_command_64 (LC_ROUTINES_64) */
594
595
596/**
597 * Symbol table command.
598 * Contains a.out style symbol table with some tricks.
599 */
600typedef struct symtab_command
601{
602 uint32_t cmd; /**< LC_SYMTAB */
603 uint32_t cmdsize; /** sizeof(symtab_command_t) */
604 uint32_t symoff; /** The file offset of the symbol table. */
605 uint32_t nsyms; /** The number of symbols in the symbol table. */
606 uint32_t stroff; /** The file offset of the string table. */
607 uint32_t strsize; /** The size of the string table. */
608} symtab_command_t;
609
610
611/** @todo dysymtab_command (LC_DYSYMTAB) */
612/** @todo dylib_table_of_contents */
613/** @todo dylib_module_32 */
614/** @todo dylib_module_64 */
615/** @todo dylib_reference */
616/** @todo twolevel_hints_command (LC_TWOLEVEL_HINTS) */
617/** @todo twolevel_hint */
618/** @todo prebind_cksum_command (LC_PREBIND_CKSUM) */
619
620
621/**
622 * UUID generated by ld.
623 */
624typedef struct uuid_command
625{
626 uint32_t cmd; /**< LC_UUID */
627 uint32_t cmdsize; /**< sizeof(uuid_command_t) */
628 uint8_t uuid[16]; /** The UUID bytes. */
629} uuid_command_t;
630
631
632/** @todo symseg_command (LC_SYMSEG) */
633/** @todo ident_command (LC_IDENT) */
634/** @todo fvmfile_command (LC_FVMFILE) */
635
636/** @} */
637
638
639
640/** @defgroup grp_macho_o_syms Symbol Table
641 * @{ */
642
643/**
644 * The 32-bit Mach-O version of the nlist structure.
645 *
646 * This differs from the a.out nlist struct in that the unused n_other field
647 * was renamed to n_sect and used for keeping the relevant section number.
648 * @remark This structure is not name mach_nlist_32 in the Apple headers, but nlist.
649 */
650typedef struct macho_nlist_32
651{
652 union
653 {
654 int32_t n_strx; /**< Offset (index) into the string table. 0 means "". */
655 } n_un;
656 uint8_t n_type; /**< Symbol type. */
657 uint8_t n_sect; /**< Section number of NO_SECT. */
658 int16_t n_desc; /**< Type specific, debug info details mostly.*/
659 uint32_t n_value; /**< The symbol value or stab offset. */
660} macho_nlist_32_t;
661
662
663/**
664 * The 64-bit Mach-O version of the nlist structure.
665 * @see macho_nlist_32
666 */
667typedef struct macho_nlist_64
668{
669 union
670 {
671 uint32_t n_strx; /**< Offset (index) into the string table. 0 means "". */
672 } n_un;
673 uint8_t n_type; /**< Symbol type. */
674 uint8_t n_sect; /**< Section number of NO_SECT. */
675 int16_t n_desc; /**< Type specific, debug info details mostly.*/
676 uint64_t n_value; /**< The symbol value or stab offset. */
677} macho_nlist_64_t;
678
679
680/** @name Symbol Type Constants (macho_nlist_32_t::n_type, macho_nlist_64_t::n_type)
681 *
682 * In the Mach-O world n_type is somewhat similar to a.out, meaning N_EXT, N_UNDF, N_ABS
683 * and the debug symbols are essentially the same, but the remaining stuff is different.
684 * The main reason for this is that the encoding of section has been moved to n_sect
685 * to permit up to 255 sections instead of the fixed 3 a.out sections (not counting
686 * the abs symbols and set vectors).
687 *
688 * To avoid confusion with a.out the Mach-O constants has been fitted with a MACHO_
689 * prefix here.
690 *
691 * Common symbols (aka communal symbols and comdefs) are represented by
692 * n_type = MACHO_N_EXT | MACHO_N_UNDF, n_sect = NO_SECT and n_value giving
693 * the size.
694 *
695 *
696 * Symbol table entries can be inserted directly in the assembly code using
697 * this notation:
698 * @code
699 * .stabs "n_name", n_type, n_sect, n_desc, n_value
700 * @endcode
701 *
702 * (1) The line number is optional, GCC doesn't set it.
703 * (2) The type is optional, GCC doesn't set it.
704 * (3) The binutil header is "skeptical" about the line. I'm skeptical about the whole thing... :-)
705 * (M) Mach-O specific?
706 * (S) Sun specific?
707 * @{
708 */
709
710/* Base masks. */
711#define MACHO_N_EXT UINT8_C(0x01) /**< External symbol (when set) (N_EXT). */
712#define MACHO_N_TYPE UINT8_C(0x0e) /**< Symbol type (N_TYPE without the 8th bit). */
713#define MACHO_N_PEXT UINT8_C(0x10) /**< Private extern symbol (when set). (M) */
714#define MACHO_N_STAB UINT8_C(0xe0) /**< Debug symbol mask (N_STAB). */
715
716/* MACHO_N_TYPE values. */
717#define MACHO_N_UNDF UINT8_C(0x00) /**< MACHO_N_TYPE: Undefined symbol (N_UNDF). n_sect = NO_SECT. */
718#define MACHO_N_ABS UINT8_C(0x02) /**< MACHO_N_TYPE: Absolute symbol (N_UNDF). n_sect = NO_SECT. */
719#define MACHO_N_INDR UINT8_C(0x0a) /**< MACHO_N_TYPE: Indirect symbol, n_value is the index of the symbol. (M) */
720#define MACHO_N_PBUD UINT8_C(0x0c) /**< MACHO_N_TYPE: Prebound undefined symbo (defined in a dylib). (M) */
721#define MACHO_N_SECT UINT8_C(0x0e) /**< MACHO_N_TYPE: Defined in the section given by n_sects. (M) */
722
723/* Debug symbols. */
724#define MACHO_N_GSYM UINT8_C(0x20) /**< Global variable. "name",, NO_SECT, type, 0 (2) */
725#define MACHO_N_FNAME UINT8_C(0x22) /**< Function name (F77). "name",, NO_SECT, 0, 0 */
726#define MACHO_N_FUN UINT8_C(0x24) /**< Function / text var. "name",, section, line, address (1) */
727#define MACHO_N_STSYM UINT8_C(0x26) /**< Static data symbol. "name",, section, type, address (2) */
728#define MACHO_N_LCSYM UINT8_C(0x28) /**< static bss symbol. "name",, section, type, address (2) */
729 /* omits N_MAIN and N_ROSYM. */
730#define MACHO_N_BNSYM UINT8_C(0x2e) /**< Begin nsect symbol. 0,, section, 0, address (M) */
731#define MACHO_N_PC UINT8_C(0x30) /**< Global pascal symbol. "name",, NO_SECT, subtype?, line (3) */
732 /* omits N_NSYMS, N_NOMAP and N_OBJ. */
733#define MACHO_N_OPT UINT8_C(0x3c) /**< Options for the debugger related to the language of the
734 source file. "options?",,,, */
735#define MACHO_N_RSYM UINT8_C(0x40) /**< Register variable. "name",, NO_SECT, type, register */
736 /* omits N_M2C */
737#define MACHO_N_SLINE UINT8_C(0x44) /**< Source line. 0,, section, line, address */
738 /* omits N_DSLINE, N_BSLINE / N_BROWS, N_DEFD and N_FLINE. */
739#define MACHO_N_ENSYM UINT8_C(0x4e) /**< End nsect symbol. 0,, section, 0, address (M) */
740 /* omits N_EHDECL / N_MOD2 and N_CATCH. */
741#define MACHO_N_SSYM UINT8_C(0x60) /**< Struct/union element. "name",, NO_SECT, type, offset */
742 /* omits N_ENDM */
743#define MACHO_N_SO UINT8_C(0x64) /**< Source file name. "fname",, section, 0, address */
744#define MACHO_N_OSO UINT8_C(0x66) /**< Object file name. "fname",, 0, 0, st_mtime (M?) */
745 /* omits N_ALIAS */
746#define MACHO_N_LSYM UINT8_C(0x80) /**< Stack variable. "name",, NO_SECT, type, frame_offset */
747#define MACHO_N_BINCL UINT8_C(0x82) /**< Begin #include. "fname",, NO_SECT, 0, sum? */
748#define MACHO_N_SOL UINT8_C(0x84) /**< #included file. "fname",, section, 0, start_address (S) */
749#define MACHO_N_PARAMS UINT8_C(0x86) /**< Compiler params. "params",, NO_SECT, 0, 0 */
750#define MACHO_N_VERSION UINT8_C(0x88) /**< Compiler version. "version",, NO_SECT, 0, 0 */
751#define MACHO_N_OLEVEL UINT8_C(0x8A) /**< Compiler -O level. "level",, NO_SECT, 0, 0 */
752#define MACHO_N_PSYM UINT8_C(0xa0) /**< Parameter variable. "name",, NO_SECT, type, frame_offset */
753#define MACHO_N_EINCL UINT8_C(0xa2) /**< End #include. "fname",, NO_SECT, 0, 0 (S) */
754#define MACHO_N_ENTRY UINT8_C(0xa4) /**< Alternate entry point. "name",, section, line, address */
755#define MACHO_N_LBRAC UINT8_C(0xc0) /**< Left bracket. 0,, NO_SECT, nesting_level, address */
756#define MACHO_N_EXCL UINT8_C(0xc2) /**< Deleted include file. "fname",, NO_SECT, 0, sum? (S) */
757 /* omits N_SCOPE */
758#define MACHO_N_RBRAC UINT8_C(0xe0) /**< Right bracket. 0,, NO_SECT, nesting_level, address */
759#define MACHO_N_BCOMM UINT8_C(0xe2) /**< Begin common. "name",, NO_SECT?, 0, 0 */
760#define MACHO_N_ECOMM UINT8_C(0xe4) /**< End common. "name",, section, 0, 0 */
761#define MACHO_N_ECOML UINT8_C(0xe8) /**< End local common. 0,, section, 0, address */
762#define MACHO_N_LENG UINT8_C(0xfe) /**< Length-value of the preceding entry.
763 "name",, NO_SECT, 0, length */
764
765/** @} */
766
767/** @name Symbol Description Bits (macho_nlist_32_t::n_desc, macho_nlist_64_t::n_desc)
768 *
769 * Mach-O puts the n_desc field to a number of uses, like lazy binding , library
770 * ordinal numbers for -twolevel_namespace, stripping and weak symbol handling.
771 *
772 * @remark The REFERENCE_FLAGS_* are really not flags in the normal sense (bit),
773 * they are more like enum values.
774 * @{
775 */
776
777#define REFERENCE_TYPE UINT16_C(0x000f) /**< The reference type mask. */
778#define REFERENCE_FLAG_UNDEFINED_NON_LAZY 0 /**< Normal undefined symbol. */
779#define REFERENCE_FLAG_UNDEFINED_LAZY 1 /**< Lazy undefined symbol. */
780#define REFERENCE_FLAG_DEFINED 2 /**< Defined symbol (dynamic linking). */
781#define REFERENCE_FLAG_PRIVATE_DEFINED 3 /**< Defined private symbol (dynamic linking). */
782#define REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY 4 /**< Normal undefined private symbol. */
783#define REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY 5 /**< Lazy undefined private symbol. */
784
785#define REFERENCED_DYNAMICALLY UINT16_C(0x0010) /**< Don't strip. */
786
787
788/** Get the dynamic library ordinal. */
789#define GET_LIBRARY_ORDINAL(n_desc) \
790 (((n_desc) >> 8) & 0xff)
791/** Set the dynamic library ordinal. */
792#define SET_LIBRARY_ORDINAL(n_desc, ordinal) \
793 (n_desc) = (((n_desc) & 0xff) | (((ordinal) & 0xff) << 8))
794#define SELF_LIBRARY_ORDINAL 0x00 /**< Special ordinal for refering to onself. */
795#define MAX_LIBRARY_ORDINAL 0xfd /**< Maximum ordinal number. */
796#define DYNAMIC_LOOKUP_ORDINAL 0xfe /**< Special ordinal number for dynamic lookup. (Mac OS X 10.3 and later) */
797#define EXECUTABLE_ORDINAL 0xff /**< Special ordinal number for the executable. */
798
799
800/** Only MH_OBJECT: Never dead strip me! */
801#define N_NO_DEAD_STRIP UINT16_C(0x0020)
802/** Not MH_OBJECT: Discarded symbol. */
803#define N_DESC_DISCARDED UINT16_C(0x0020)
804/** Weak external symbol. Symbol can be missing, in which case it's will have the value 0. */
805#define N_WEAK_REF UINT16_C(0x0040)
806/** Weak symbol definition. The symbol can be overridden by another weak
807 * symbol already present or by a non-weak (strong) symbol definition.
808 * Currently only supported for coalesed symbols.
809 * @remark This bit means something differently for undefined symbols, see N_REF_TO_WEAK.
810 */
811#define N_WEAK_DEF UINT16_C(0x0080)
812/** Reference to a weak symbol, resolve using flat namespace searching.
813 * @remark This bit means something differently for defined symbols, see N_WEAK_DEF. */
814#define N_REF_TO_WEAK UINT16_C(0x0080)
815
816/** @} */
817
818/** @} */
819
820
821/** @defgroup grp_macho_o_relocs Relocations
822 * @{ */
823
824/**
825 * Relocation entry.
826 *
827 * Differs from a.out in the meaning of r_symbolnum when r_extern=0 and
828 * that r_pad is made into r_type.
829 *
830 * @remark This structure and type has been prefixed with macho_ to avoid
831 * confusion with the original a.out type.
832 */
833typedef struct macho_relocation_info
834{
835 int32_t r_address; /**< Section relative address of the fixup.
836 The top bit (signed) indicates that this is a scattered
837 relocation if set, see scattered_relocation_info_t. */
838 uint32_t r_symbolnum : 24, /**< r_extern=1: Symbol table index, relocate with the address of this symbol.
839 r_extern=0: Section ordinal, relocate with the address of this section. */
840 r_pcrel : 1, /**< PC (program counter) relative fixup; subtract the fixup address. */
841 r_length : 2, /**< Fixup length: 0=uint8_t, 1=uint16_t, 2=uint32_t, 3=uint64_t. */
842 r_extern : 1, /**< External or internal fixup, decides the r_symbolnum interpretation.. */
843 r_type : 4; /**< Relocation type; 0 is standard, non-zero are machine specific. */
844} macho_relocation_info_t;
845
846/** Special section ordinal value for absolute relocations. */
847#define R_ABS 0
848
849/** Flag in r_address indicating that the relocation is of the
850 * scattered_relocation_info_t kind and not macho_relocation_info_t. */
851#define R_SCATTERED UINT32_C(0x80000000)
852
853/**
854 * Scattered relocation.
855 *
856 * This is a hack mainly for RISC machines which restricts section size
857 * to 16MB among other things.
858 *
859 * The reason for the big/little endian differences here is of course because
860 * of the R_SCATTERED mask and the way bitfields are implemented by the
861 * C/C++ compilers.
862 */
863typedef struct scattered_relocation_info
864{
865#ifdef KLDR_LITTLE_ENDIAN
866 uint32_t r_address : 24, /**< Section relative address of the fixup. (macho_relocation_info_t::r_address) */
867 r_type : 4, /**< Relocation type; 0 is standard, non-zero are machine specific. (macho_relocation_info_t::r_type) */
868 r_length : 2, /**< Fixup length: 0=uint8_t, 1=uint16_t, 2=uint32_t, 3=uint64_t. (macho_relocation_info_t::r_length) */
869 r_pcrel : 1, /**< PC (program counter) relative fixup; subtract the fixup address. (macho_relocation_info_t::r_pcrel) */
870 r_scattered : 1; /**< Set if scattered relocation, clear if normal relocation. */
871#elif defined(KLDR_BIG_ENDIAN)
872 uint32_t r_scattered : 1, /**< Set if scattered relocation, clear if normal relocation. */
873 r_pcrel : 1, /**< PC (program counter) relative fixup; subtract the fixup address. (macho_relocation_info_t::r_pcrel) */
874 r_length : 2, /**< Fixup length: 0=uint8_t, 1=uint16_t, 2=uint32_t, 3=uint64_t. (macho_relocation_info_t::r_length) */
875 r_type : 4, /**< Relocation type; 0 is standard, non-zero are machine specific. (macho_relocation_info_t::r_type) */
876 r_address : 24; /**< Section relative address of the fixup. (macho_relocation_info_t::r_address) */
877#else
878# error "Neither KLDR_LITTLE_ENDIAN nor KLDR_BIG_ENDIAN is defined!"
879#endif
880 int32_t r_value; /**< The value the fixup is refering to (without offset added). */
881} scattered_relocation_info_t;
882
883/**
884 * Relocation type values for a generic implementation (for r_type).
885 */
886typedef enum reloc_type_generic
887{
888 GENERIC_RELOC_VANILLA = 0, /**< Standard relocation. */
889 GENERIC_RELOC_PAIR, /**< Follows GENERIC_RELOC_SECTDIFF. */
890 GENERIC_RELOC_SECTDIFF, /**< ??? */
891 GENERIC_RELOC_PB_LA_PTR, /**< Prebound lazy pointer whatever that. */
892 GENERIC_RELOC_LOCAL_SECTDIFF /**< ??? */
893} reloc_type_generic_t;
894
895/** @} */
896
897
898/** @} */
899#endif
900
Note: See TracBrowser for help on using the repository browser.