• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Support/MachO.h - The MachO file format ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines manifest constants for the MachO object file format.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_MACHO_H
15 #define LLVM_SUPPORT_MACHO_H
16 
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/Host.h"
20 
21 namespace llvm {
22   namespace MachO {
23     // Enums from <mach-o/loader.h>
24     enum : uint32_t {
25       // Constants for the "magic" field in llvm::MachO::mach_header and
26       // llvm::MachO::mach_header_64
27       MH_MAGIC    = 0xFEEDFACEu,
28       MH_CIGAM    = 0xCEFAEDFEu,
29       MH_MAGIC_64 = 0xFEEDFACFu,
30       MH_CIGAM_64 = 0xCFFAEDFEu,
31       FAT_MAGIC   = 0xCAFEBABEu,
32       FAT_CIGAM   = 0xBEBAFECAu
33     };
34 
35     enum HeaderFileType {
36       // Constants for the "filetype" field in llvm::MachO::mach_header and
37       // llvm::MachO::mach_header_64
38       MH_OBJECT      = 0x1u,
39       MH_EXECUTE     = 0x2u,
40       MH_FVMLIB      = 0x3u,
41       MH_CORE        = 0x4u,
42       MH_PRELOAD     = 0x5u,
43       MH_DYLIB       = 0x6u,
44       MH_DYLINKER    = 0x7u,
45       MH_BUNDLE      = 0x8u,
46       MH_DYLIB_STUB  = 0x9u,
47       MH_DSYM        = 0xAu,
48       MH_KEXT_BUNDLE = 0xBu
49     };
50 
51     enum {
52       // Constant bits for the "flags" field in llvm::MachO::mach_header and
53       // llvm::MachO::mach_header_64
54       MH_NOUNDEFS                = 0x00000001u,
55       MH_INCRLINK                = 0x00000002u,
56       MH_DYLDLINK                = 0x00000004u,
57       MH_BINDATLOAD              = 0x00000008u,
58       MH_PREBOUND                = 0x00000010u,
59       MH_SPLIT_SEGS              = 0x00000020u,
60       MH_LAZY_INIT               = 0x00000040u,
61       MH_TWOLEVEL                = 0x00000080u,
62       MH_FORCE_FLAT              = 0x00000100u,
63       MH_NOMULTIDEFS             = 0x00000200u,
64       MH_NOFIXPREBINDING         = 0x00000400u,
65       MH_PREBINDABLE             = 0x00000800u,
66       MH_ALLMODSBOUND            = 0x00001000u,
67       MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
68       MH_CANONICAL               = 0x00004000u,
69       MH_WEAK_DEFINES            = 0x00008000u,
70       MH_BINDS_TO_WEAK           = 0x00010000u,
71       MH_ALLOW_STACK_EXECUTION   = 0x00020000u,
72       MH_ROOT_SAFE               = 0x00040000u,
73       MH_SETUID_SAFE             = 0x00080000u,
74       MH_NO_REEXPORTED_DYLIBS    = 0x00100000u,
75       MH_PIE                     = 0x00200000u,
76       MH_DEAD_STRIPPABLE_DYLIB   = 0x00400000u,
77       MH_HAS_TLV_DESCRIPTORS     = 0x00800000u,
78       MH_NO_HEAP_EXECUTION       = 0x01000000u,
79       MH_APP_EXTENSION_SAFE      = 0x02000000u
80     };
81 
82     enum : uint32_t {
83       // Flags for the "cmd" field in llvm::MachO::load_command
84       LC_REQ_DYLD    = 0x80000000u
85     };
86 
87     enum LoadCommandType : uint32_t {
88       // Constants for the "cmd" field in llvm::MachO::load_command
89       LC_SEGMENT              = 0x00000001u,
90       LC_SYMTAB               = 0x00000002u,
91       LC_SYMSEG               = 0x00000003u,
92       LC_THREAD               = 0x00000004u,
93       LC_UNIXTHREAD           = 0x00000005u,
94       LC_LOADFVMLIB           = 0x00000006u,
95       LC_IDFVMLIB             = 0x00000007u,
96       LC_IDENT                = 0x00000008u,
97       LC_FVMFILE              = 0x00000009u,
98       LC_PREPAGE              = 0x0000000Au,
99       LC_DYSYMTAB             = 0x0000000Bu,
100       LC_LOAD_DYLIB           = 0x0000000Cu,
101       LC_ID_DYLIB             = 0x0000000Du,
102       LC_LOAD_DYLINKER        = 0x0000000Eu,
103       LC_ID_DYLINKER          = 0x0000000Fu,
104       LC_PREBOUND_DYLIB       = 0x00000010u,
105       LC_ROUTINES             = 0x00000011u,
106       LC_SUB_FRAMEWORK        = 0x00000012u,
107       LC_SUB_UMBRELLA         = 0x00000013u,
108       LC_SUB_CLIENT           = 0x00000014u,
109       LC_SUB_LIBRARY          = 0x00000015u,
110       LC_TWOLEVEL_HINTS       = 0x00000016u,
111       LC_PREBIND_CKSUM        = 0x00000017u,
112       LC_LOAD_WEAK_DYLIB      = 0x80000018u,
113       LC_SEGMENT_64           = 0x00000019u,
114       LC_ROUTINES_64          = 0x0000001Au,
115       LC_UUID                 = 0x0000001Bu,
116       LC_RPATH                = 0x8000001Cu,
117       LC_CODE_SIGNATURE       = 0x0000001Du,
118       LC_SEGMENT_SPLIT_INFO   = 0x0000001Eu,
119       LC_REEXPORT_DYLIB       = 0x8000001Fu,
120       LC_LAZY_LOAD_DYLIB      = 0x00000020u,
121       LC_ENCRYPTION_INFO      = 0x00000021u,
122       LC_DYLD_INFO            = 0x00000022u,
123       LC_DYLD_INFO_ONLY       = 0x80000022u,
124       LC_LOAD_UPWARD_DYLIB    = 0x80000023u,
125       LC_VERSION_MIN_MACOSX   = 0x00000024u,
126       LC_VERSION_MIN_IPHONEOS = 0x00000025u,
127       LC_FUNCTION_STARTS      = 0x00000026u,
128       LC_DYLD_ENVIRONMENT     = 0x00000027u,
129       LC_MAIN                 = 0x80000028u,
130       LC_DATA_IN_CODE         = 0x00000029u,
131       LC_SOURCE_VERSION       = 0x0000002Au,
132       LC_DYLIB_CODE_SIGN_DRS  = 0x0000002Bu,
133       LC_ENCRYPTION_INFO_64   = 0x0000002Cu,
134       LC_LINKER_OPTION        = 0x0000002Du,
135       LC_LINKER_OPTIMIZATION_HINT = 0x0000002Eu
136     };
137 
138     enum : uint32_t {
139       // Constant bits for the "flags" field in llvm::MachO::segment_command
140       SG_HIGHVM              = 0x1u,
141       SG_FVMLIB              = 0x2u,
142       SG_NORELOC             = 0x4u,
143       SG_PROTECTED_VERSION_1 = 0x8u,
144 
145 
146       // Constant masks for the "flags" field in llvm::MachO::section and
147       // llvm::MachO::section_64
148       SECTION_TYPE           = 0x000000ffu, // SECTION_TYPE
149       SECTION_ATTRIBUTES     = 0xffffff00u, // SECTION_ATTRIBUTES
150       SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
151       SECTION_ATTRIBUTES_SYS = 0x00ffff00u  // SECTION_ATTRIBUTES_SYS
152     };
153 
154     /// These are the section type and attributes fields.  A MachO section can
155     /// have only one Type, but can have any of the attributes specified.
156     enum SectionType : uint32_t {
157       // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
158       // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
159 
160       /// S_REGULAR - Regular section.
161       S_REGULAR                             = 0x00u,
162       /// S_ZEROFILL - Zero fill on demand section.
163       S_ZEROFILL                            = 0x01u,
164       /// S_CSTRING_LITERALS - Section with literal C strings.
165       S_CSTRING_LITERALS                    = 0x02u,
166       /// S_4BYTE_LITERALS - Section with 4 byte literals.
167       S_4BYTE_LITERALS                      = 0x03u,
168       /// S_8BYTE_LITERALS - Section with 8 byte literals.
169       S_8BYTE_LITERALS                      = 0x04u,
170       /// S_LITERAL_POINTERS - Section with pointers to literals.
171       S_LITERAL_POINTERS                    = 0x05u,
172       /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
173       S_NON_LAZY_SYMBOL_POINTERS            = 0x06u,
174       /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
175       S_LAZY_SYMBOL_POINTERS                = 0x07u,
176       /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
177       /// the Reserved2 field.
178       S_SYMBOL_STUBS                        = 0x08u,
179       /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
180       /// initialization.
181       S_MOD_INIT_FUNC_POINTERS              = 0x09u,
182       /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
183       /// termination.
184       S_MOD_TERM_FUNC_POINTERS              = 0x0au,
185       /// S_COALESCED - Section contains symbols that are to be coalesced.
186       S_COALESCED                           = 0x0bu,
187       /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
188       /// gigabytes).
189       S_GB_ZEROFILL                         = 0x0cu,
190       /// S_INTERPOSING - Section with only pairs of function pointers for
191       /// interposing.
192       S_INTERPOSING                         = 0x0du,
193       /// S_16BYTE_LITERALS - Section with only 16 byte literals.
194       S_16BYTE_LITERALS                     = 0x0eu,
195       /// S_DTRACE_DOF - Section contains DTrace Object Format.
196       S_DTRACE_DOF                          = 0x0fu,
197       /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
198       /// lazy loaded dylibs.
199       S_LAZY_DYLIB_SYMBOL_POINTERS          = 0x10u,
200       /// S_THREAD_LOCAL_REGULAR - Thread local data section.
201       S_THREAD_LOCAL_REGULAR                = 0x11u,
202       /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
203       S_THREAD_LOCAL_ZEROFILL               = 0x12u,
204       /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
205       /// structure data.
206       S_THREAD_LOCAL_VARIABLES              = 0x13u,
207       /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
208       /// local structures.
209       S_THREAD_LOCAL_VARIABLE_POINTERS      = 0x14u,
210       /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
211       /// variable initialization pointers to functions.
212       S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
213 
214       LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
215     };
216 
217     enum : uint32_t {
218       // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
219       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
220 
221       /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
222       /// instructions.
223       S_ATTR_PURE_INSTRUCTIONS   = 0x80000000u,
224       /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
225       /// in a ranlib table of contents.
226       S_ATTR_NO_TOC              = 0x40000000u,
227       /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
228       /// in files with the MY_DYLDLINK flag.
229       S_ATTR_STRIP_STATIC_SYMS   = 0x20000000u,
230       /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
231       S_ATTR_NO_DEAD_STRIP       = 0x10000000u,
232       /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
233       S_ATTR_LIVE_SUPPORT        = 0x08000000u,
234       /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
235       /// dyld.
236       S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
237       /// S_ATTR_DEBUG - A debug section.
238       S_ATTR_DEBUG               = 0x02000000u,
239 
240       // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
241       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
242 
243       /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
244       S_ATTR_SOME_INSTRUCTIONS   = 0x00000400u,
245       /// S_ATTR_EXT_RELOC - Section has external relocation entries.
246       S_ATTR_EXT_RELOC           = 0x00000200u,
247       /// S_ATTR_LOC_RELOC - Section has local relocation entries.
248       S_ATTR_LOC_RELOC           = 0x00000100u,
249 
250       // Constant masks for the value of an indirect symbol in an indirect
251       // symbol table
252       INDIRECT_SYMBOL_LOCAL = 0x80000000u,
253       INDIRECT_SYMBOL_ABS   = 0x40000000u
254     };
255 
256     enum DataRegionType {
257       // Constants for the "kind" field in a data_in_code_entry structure
258       DICE_KIND_DATA             = 1u,
259       DICE_KIND_JUMP_TABLE8      = 2u,
260       DICE_KIND_JUMP_TABLE16     = 3u,
261       DICE_KIND_JUMP_TABLE32     = 4u,
262       DICE_KIND_ABS_JUMP_TABLE32 = 5u
263     };
264 
265     enum RebaseType {
266       REBASE_TYPE_POINTER         = 1u,
267       REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
268       REBASE_TYPE_TEXT_PCREL32    = 3u
269     };
270 
271     enum {
272       REBASE_OPCODE_MASK    = 0xF0u,
273       REBASE_IMMEDIATE_MASK = 0x0Fu
274     };
275 
276     enum RebaseOpcode {
277       REBASE_OPCODE_DONE                               = 0x00u,
278       REBASE_OPCODE_SET_TYPE_IMM                       = 0x10u,
279       REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB        = 0x20u,
280       REBASE_OPCODE_ADD_ADDR_ULEB                      = 0x30u,
281       REBASE_OPCODE_ADD_ADDR_IMM_SCALED                = 0x40u,
282       REBASE_OPCODE_DO_REBASE_IMM_TIMES                = 0x50u,
283       REBASE_OPCODE_DO_REBASE_ULEB_TIMES               = 0x60u,
284       REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB            = 0x70u,
285       REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
286     };
287 
288     enum BindType {
289       BIND_TYPE_POINTER         = 1u,
290       BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
291       BIND_TYPE_TEXT_PCREL32    = 3u
292     };
293 
294     enum BindSpecialDylib {
295       BIND_SPECIAL_DYLIB_SELF            =  0,
296       BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
297       BIND_SPECIAL_DYLIB_FLAT_LOOKUP     = -2
298     };
299 
300     enum {
301       BIND_SYMBOL_FLAGS_WEAK_IMPORT         = 0x1u,
302       BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
303 
304       BIND_OPCODE_MASK                      = 0xF0u,
305       BIND_IMMEDIATE_MASK                   = 0x0Fu
306     };
307 
308     enum BindOpcode {
309       BIND_OPCODE_DONE                             = 0x00u,
310       BIND_OPCODE_SET_DYLIB_ORDINAL_IMM            = 0x10u,
311       BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB           = 0x20u,
312       BIND_OPCODE_SET_DYLIB_SPECIAL_IMM            = 0x30u,
313       BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM    = 0x40u,
314       BIND_OPCODE_SET_TYPE_IMM                     = 0x50u,
315       BIND_OPCODE_SET_ADDEND_SLEB                  = 0x60u,
316       BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB      = 0x70u,
317       BIND_OPCODE_ADD_ADDR_ULEB                    = 0x80u,
318       BIND_OPCODE_DO_BIND                          = 0x90u,
319       BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB            = 0xA0u,
320       BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED      = 0xB0u,
321       BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
322     };
323 
324     enum {
325       EXPORT_SYMBOL_FLAGS_KIND_MASK           = 0x03u,
326       EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION     = 0x04u,
327       EXPORT_SYMBOL_FLAGS_REEXPORT            = 0x08u,
328       EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER   = 0x10u
329     };
330 
331     enum ExportSymbolKind {
332       EXPORT_SYMBOL_FLAGS_KIND_REGULAR        = 0x00u,
333       EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL   = 0x01u,
334       EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE       = 0x02u
335     };
336 
337 
338     enum {
339       // Constant masks for the "n_type" field in llvm::MachO::nlist and
340       // llvm::MachO::nlist_64
341       N_STAB = 0xe0,
342       N_PEXT = 0x10,
343       N_TYPE = 0x0e,
344       N_EXT  = 0x01
345     };
346 
347     enum NListType {
348       // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
349       // llvm::MachO::nlist_64
350       N_UNDF = 0x0u,
351       N_ABS  = 0x2u,
352       N_SECT = 0xeu,
353       N_PBUD = 0xcu,
354       N_INDR = 0xau
355     };
356 
357     enum SectionOrdinal {
358       // Constants for the "n_sect" field in llvm::MachO::nlist and
359       // llvm::MachO::nlist_64
360       NO_SECT  = 0u,
361       MAX_SECT = 0xffu
362     };
363 
364     enum {
365       // Constant masks for the "n_desc" field in llvm::MachO::nlist and
366       // llvm::MachO::nlist_64
367       // The low 3 bits are the for the REFERENCE_TYPE.
368       REFERENCE_TYPE                            = 0x7,
369       REFERENCE_FLAG_UNDEFINED_NON_LAZY         = 0,
370       REFERENCE_FLAG_UNDEFINED_LAZY             = 1,
371       REFERENCE_FLAG_DEFINED                    = 2,
372       REFERENCE_FLAG_PRIVATE_DEFINED            = 3,
373       REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
374       REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY     = 5,
375       // Flag bits (some overlap with the library ordinal bits).
376       N_ARM_THUMB_DEF   = 0x0008u,
377       REFERENCED_DYNAMICALLY = 0x0010u,
378       N_NO_DEAD_STRIP   = 0x0020u,
379       N_WEAK_REF        = 0x0040u,
380       N_WEAK_DEF        = 0x0080u,
381       N_SYMBOL_RESOLVER = 0x0100u,
382       N_ALT_ENTRY       = 0x0200u,
383       // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
384       // as these are in the top 8 bits.
385       SELF_LIBRARY_ORDINAL   = 0x0,
386       MAX_LIBRARY_ORDINAL    = 0xfd,
387       DYNAMIC_LOOKUP_ORDINAL = 0xfe,
388       EXECUTABLE_ORDINAL     = 0xff
389     };
390 
391     enum StabType {
392       // Constant values for the "n_type" field in llvm::MachO::nlist and
393       // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
394       N_GSYM    = 0x20u,
395       N_FNAME   = 0x22u,
396       N_FUN     = 0x24u,
397       N_STSYM   = 0x26u,
398       N_LCSYM   = 0x28u,
399       N_BNSYM   = 0x2Eu,
400       N_PC      = 0x30u,
401       N_AST     = 0x32u,
402       N_OPT     = 0x3Cu,
403       N_RSYM    = 0x40u,
404       N_SLINE   = 0x44u,
405       N_ENSYM   = 0x4Eu,
406       N_SSYM    = 0x60u,
407       N_SO      = 0x64u,
408       N_OSO     = 0x66u,
409       N_LSYM    = 0x80u,
410       N_BINCL   = 0x82u,
411       N_SOL     = 0x84u,
412       N_PARAMS  = 0x86u,
413       N_VERSION = 0x88u,
414       N_OLEVEL  = 0x8Au,
415       N_PSYM    = 0xA0u,
416       N_EINCL   = 0xA2u,
417       N_ENTRY   = 0xA4u,
418       N_LBRAC   = 0xC0u,
419       N_EXCL    = 0xC2u,
420       N_RBRAC   = 0xE0u,
421       N_BCOMM   = 0xE2u,
422       N_ECOMM   = 0xE4u,
423       N_ECOML   = 0xE8u,
424       N_LENG    = 0xFEu
425     };
426 
427     enum : uint32_t {
428       // Constant values for the r_symbolnum field in an
429       // llvm::MachO::relocation_info structure when r_extern is 0.
430       R_ABS = 0,
431 
432       // Constant bits for the r_address field in an
433       // llvm::MachO::relocation_info structure.
434       R_SCATTERED = 0x80000000
435     };
436 
437     enum RelocationInfoType {
438       // Constant values for the r_type field in an
439       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
440       // structure.
441       GENERIC_RELOC_VANILLA        = 0,
442       GENERIC_RELOC_PAIR           = 1,
443       GENERIC_RELOC_SECTDIFF       = 2,
444       GENERIC_RELOC_PB_LA_PTR      = 3,
445       GENERIC_RELOC_LOCAL_SECTDIFF = 4,
446       GENERIC_RELOC_TLV            = 5,
447 
448       // Constant values for the r_type field in a PowerPC architecture
449       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
450       // structure.
451       PPC_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
452       PPC_RELOC_PAIR               = GENERIC_RELOC_PAIR,
453       PPC_RELOC_BR14               = 2,
454       PPC_RELOC_BR24               = 3,
455       PPC_RELOC_HI16               = 4,
456       PPC_RELOC_LO16               = 5,
457       PPC_RELOC_HA16               = 6,
458       PPC_RELOC_LO14               = 7,
459       PPC_RELOC_SECTDIFF           = 8,
460       PPC_RELOC_PB_LA_PTR          = 9,
461       PPC_RELOC_HI16_SECTDIFF      = 10,
462       PPC_RELOC_LO16_SECTDIFF      = 11,
463       PPC_RELOC_HA16_SECTDIFF      = 12,
464       PPC_RELOC_JBSR               = 13,
465       PPC_RELOC_LO14_SECTDIFF      = 14,
466       PPC_RELOC_LOCAL_SECTDIFF     = 15,
467 
468       // Constant values for the r_type field in an ARM architecture
469       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
470       // structure.
471       ARM_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
472       ARM_RELOC_PAIR               = GENERIC_RELOC_PAIR,
473       ARM_RELOC_SECTDIFF           = GENERIC_RELOC_SECTDIFF,
474       ARM_RELOC_LOCAL_SECTDIFF     = 3,
475       ARM_RELOC_PB_LA_PTR          = 4,
476       ARM_RELOC_BR24               = 5,
477       ARM_THUMB_RELOC_BR22         = 6,
478       ARM_THUMB_32BIT_BRANCH       = 7, // obsolete
479       ARM_RELOC_HALF               = 8,
480       ARM_RELOC_HALF_SECTDIFF      = 9,
481 
482       // Constant values for the r_type field in an ARM64 architecture
483       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
484       // structure.
485 
486       // For pointers.
487       ARM64_RELOC_UNSIGNED            = 0,
488       // Must be followed by an ARM64_RELOC_UNSIGNED
489       ARM64_RELOC_SUBTRACTOR          = 1,
490       // A B/BL instruction with 26-bit displacement.
491       ARM64_RELOC_BRANCH26            = 2,
492       // PC-rel distance to page of target.
493       ARM64_RELOC_PAGE21              = 3,
494       // Offset within page, scaled by r_length.
495       ARM64_RELOC_PAGEOFF12           = 4,
496       // PC-rel distance to page of GOT slot.
497       ARM64_RELOC_GOT_LOAD_PAGE21     = 5,
498       // Offset within page of GOT slot, scaled by r_length.
499       ARM64_RELOC_GOT_LOAD_PAGEOFF12  = 6,
500       // For pointers to GOT slots.
501       ARM64_RELOC_POINTER_TO_GOT      = 7,
502       // PC-rel distance to page of TLVP slot.
503       ARM64_RELOC_TLVP_LOAD_PAGE21    = 8,
504       // Offset within page of TLVP slot, scaled by r_length.
505       ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
506       // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
507       ARM64_RELOC_ADDEND              = 10,
508 
509 
510       // Constant values for the r_type field in an x86_64 architecture
511       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
512       // structure
513       X86_64_RELOC_UNSIGNED        = 0,
514       X86_64_RELOC_SIGNED          = 1,
515       X86_64_RELOC_BRANCH          = 2,
516       X86_64_RELOC_GOT_LOAD        = 3,
517       X86_64_RELOC_GOT             = 4,
518       X86_64_RELOC_SUBTRACTOR      = 5,
519       X86_64_RELOC_SIGNED_1        = 6,
520       X86_64_RELOC_SIGNED_2        = 7,
521       X86_64_RELOC_SIGNED_4        = 8,
522       X86_64_RELOC_TLV             = 9
523     };
524 
525     // Values for segment_command.initprot.
526     // From <mach/vm_prot.h>
527     enum {
528       VM_PROT_READ    = 0x1,
529       VM_PROT_WRITE   = 0x2,
530       VM_PROT_EXECUTE = 0x4
531     };
532 
533 
534     // Structs from <mach-o/loader.h>
535 
536     struct mach_header {
537       uint32_t magic;
538       uint32_t cputype;
539       uint32_t cpusubtype;
540       uint32_t filetype;
541       uint32_t ncmds;
542       uint32_t sizeofcmds;
543       uint32_t flags;
544     };
545 
546     struct mach_header_64 {
547       uint32_t magic;
548       uint32_t cputype;
549       uint32_t cpusubtype;
550       uint32_t filetype;
551       uint32_t ncmds;
552       uint32_t sizeofcmds;
553       uint32_t flags;
554       uint32_t reserved;
555     };
556 
557     struct load_command {
558       uint32_t cmd;
559       uint32_t cmdsize;
560     };
561 
562     struct segment_command {
563       uint32_t cmd;
564       uint32_t cmdsize;
565       char segname[16];
566       uint32_t vmaddr;
567       uint32_t vmsize;
568       uint32_t fileoff;
569       uint32_t filesize;
570       uint32_t maxprot;
571       uint32_t initprot;
572       uint32_t nsects;
573       uint32_t flags;
574     };
575 
576     struct segment_command_64 {
577       uint32_t cmd;
578       uint32_t cmdsize;
579       char segname[16];
580       uint64_t vmaddr;
581       uint64_t vmsize;
582       uint64_t fileoff;
583       uint64_t filesize;
584       uint32_t maxprot;
585       uint32_t initprot;
586       uint32_t nsects;
587       uint32_t flags;
588     };
589 
590     struct section {
591       char sectname[16];
592       char segname[16];
593       uint32_t addr;
594       uint32_t size;
595       uint32_t offset;
596       uint32_t align;
597       uint32_t reloff;
598       uint32_t nreloc;
599       uint32_t flags;
600       uint32_t reserved1;
601       uint32_t reserved2;
602     };
603 
604     struct section_64 {
605       char sectname[16];
606       char segname[16];
607       uint64_t addr;
608       uint64_t size;
609       uint32_t offset;
610       uint32_t align;
611       uint32_t reloff;
612       uint32_t nreloc;
613       uint32_t flags;
614       uint32_t reserved1;
615       uint32_t reserved2;
616       uint32_t reserved3;
617     };
618 
619     struct fvmlib {
620       uint32_t name;
621       uint32_t minor_version;
622       uint32_t header_addr;
623     };
624 
625     struct fvmlib_command {
626       uint32_t  cmd;
627       uint32_t cmdsize;
628       struct fvmlib fvmlib;
629     };
630 
631     struct dylib {
632       uint32_t name;
633       uint32_t timestamp;
634       uint32_t current_version;
635       uint32_t compatibility_version;
636     };
637 
638     struct dylib_command {
639       uint32_t cmd;
640       uint32_t cmdsize;
641       struct dylib dylib;
642     };
643 
644     struct sub_framework_command {
645       uint32_t cmd;
646       uint32_t cmdsize;
647       uint32_t umbrella;
648     };
649 
650     struct sub_client_command {
651       uint32_t cmd;
652       uint32_t cmdsize;
653       uint32_t client;
654     };
655 
656     struct sub_umbrella_command {
657       uint32_t cmd;
658       uint32_t cmdsize;
659       uint32_t sub_umbrella;
660     };
661 
662     struct sub_library_command {
663       uint32_t cmd;
664       uint32_t cmdsize;
665       uint32_t sub_library;
666     };
667 
668     struct prebound_dylib_command {
669       uint32_t cmd;
670       uint32_t cmdsize;
671       uint32_t name;
672       uint32_t nmodules;
673       uint32_t linked_modules;
674     };
675 
676     struct dylinker_command {
677       uint32_t cmd;
678       uint32_t cmdsize;
679       uint32_t name;
680     };
681 
682     struct thread_command {
683       uint32_t cmd;
684       uint32_t cmdsize;
685     };
686 
687     struct routines_command {
688       uint32_t cmd;
689       uint32_t cmdsize;
690       uint32_t init_address;
691       uint32_t init_module;
692       uint32_t reserved1;
693       uint32_t reserved2;
694       uint32_t reserved3;
695       uint32_t reserved4;
696       uint32_t reserved5;
697       uint32_t reserved6;
698     };
699 
700     struct routines_command_64 {
701       uint32_t cmd;
702       uint32_t cmdsize;
703       uint64_t init_address;
704       uint64_t init_module;
705       uint64_t reserved1;
706       uint64_t reserved2;
707       uint64_t reserved3;
708       uint64_t reserved4;
709       uint64_t reserved5;
710       uint64_t reserved6;
711     };
712 
713     struct symtab_command {
714       uint32_t cmd;
715       uint32_t cmdsize;
716       uint32_t symoff;
717       uint32_t nsyms;
718       uint32_t stroff;
719       uint32_t strsize;
720     };
721 
722     struct dysymtab_command {
723       uint32_t cmd;
724       uint32_t cmdsize;
725       uint32_t ilocalsym;
726       uint32_t nlocalsym;
727       uint32_t iextdefsym;
728       uint32_t nextdefsym;
729       uint32_t iundefsym;
730       uint32_t nundefsym;
731       uint32_t tocoff;
732       uint32_t ntoc;
733       uint32_t modtaboff;
734       uint32_t nmodtab;
735       uint32_t extrefsymoff;
736       uint32_t nextrefsyms;
737       uint32_t indirectsymoff;
738       uint32_t nindirectsyms;
739       uint32_t extreloff;
740       uint32_t nextrel;
741       uint32_t locreloff;
742       uint32_t nlocrel;
743     };
744 
745     struct dylib_table_of_contents {
746       uint32_t symbol_index;
747       uint32_t module_index;
748     };
749 
750     struct dylib_module {
751       uint32_t module_name;
752       uint32_t iextdefsym;
753       uint32_t nextdefsym;
754       uint32_t irefsym;
755       uint32_t nrefsym;
756       uint32_t ilocalsym;
757       uint32_t nlocalsym;
758       uint32_t iextrel;
759       uint32_t nextrel;
760       uint32_t iinit_iterm;
761       uint32_t ninit_nterm;
762       uint32_t objc_module_info_addr;
763       uint32_t objc_module_info_size;
764     };
765 
766     struct dylib_module_64 {
767       uint32_t module_name;
768       uint32_t iextdefsym;
769       uint32_t nextdefsym;
770       uint32_t irefsym;
771       uint32_t nrefsym;
772       uint32_t ilocalsym;
773       uint32_t nlocalsym;
774       uint32_t iextrel;
775       uint32_t nextrel;
776       uint32_t iinit_iterm;
777       uint32_t ninit_nterm;
778       uint32_t objc_module_info_size;
779       uint64_t objc_module_info_addr;
780     };
781 
782     struct dylib_reference {
783       uint32_t isym:24,
784                flags:8;
785     };
786 
787 
788     struct twolevel_hints_command {
789       uint32_t cmd;
790       uint32_t cmdsize;
791       uint32_t offset;
792       uint32_t nhints;
793     };
794 
795     struct twolevel_hint {
796       uint32_t isub_image:8,
797                itoc:24;
798     };
799 
800     struct prebind_cksum_command {
801       uint32_t cmd;
802       uint32_t cmdsize;
803       uint32_t cksum;
804     };
805 
806     struct uuid_command {
807       uint32_t cmd;
808       uint32_t cmdsize;
809       uint8_t uuid[16];
810     };
811 
812     struct rpath_command {
813       uint32_t cmd;
814       uint32_t cmdsize;
815       uint32_t path;
816     };
817 
818     struct linkedit_data_command {
819       uint32_t cmd;
820       uint32_t cmdsize;
821       uint32_t dataoff;
822       uint32_t datasize;
823     };
824 
825     struct data_in_code_entry {
826       uint32_t offset;
827       uint16_t length;
828       uint16_t kind;
829     };
830 
831     struct source_version_command {
832       uint32_t cmd;
833       uint32_t cmdsize;
834       uint64_t version;
835     };
836 
837     struct encryption_info_command {
838       uint32_t cmd;
839       uint32_t cmdsize;
840       uint32_t cryptoff;
841       uint32_t cryptsize;
842       uint32_t cryptid;
843     };
844 
845     struct encryption_info_command_64 {
846       uint32_t cmd;
847       uint32_t cmdsize;
848       uint32_t cryptoff;
849       uint32_t cryptsize;
850       uint32_t cryptid;
851       uint32_t pad;
852     };
853 
854     struct version_min_command {
855       uint32_t cmd;       // LC_VERSION_MIN_MACOSX or
856                           // LC_VERSION_MIN_IPHONEOS
857       uint32_t cmdsize;   // sizeof(struct version_min_command)
858       uint32_t version;   // X.Y.Z is encoded in nibbles xxxx.yy.zz
859       uint32_t sdk;       // X.Y.Z is encoded in nibbles xxxx.yy.zz
860     };
861 
862     struct dyld_info_command {
863       uint32_t cmd;
864       uint32_t cmdsize;
865       uint32_t rebase_off;
866       uint32_t rebase_size;
867       uint32_t bind_off;
868       uint32_t bind_size;
869       uint32_t weak_bind_off;
870       uint32_t weak_bind_size;
871       uint32_t lazy_bind_off;
872       uint32_t lazy_bind_size;
873       uint32_t export_off;
874       uint32_t export_size;
875     };
876 
877     struct linker_option_command {
878       uint32_t cmd;
879       uint32_t cmdsize;
880       uint32_t count;
881     };
882 
883     struct symseg_command {
884       uint32_t cmd;
885       uint32_t cmdsize;
886       uint32_t offset;
887       uint32_t size;
888     };
889 
890     struct ident_command {
891       uint32_t cmd;
892       uint32_t cmdsize;
893     };
894 
895     struct fvmfile_command {
896       uint32_t cmd;
897       uint32_t cmdsize;
898       uint32_t name;
899       uint32_t header_addr;
900     };
901 
902     struct tlv_descriptor_32 {
903       uint32_t thunk;
904       uint32_t key;
905       uint32_t offset;
906     };
907 
908     struct tlv_descriptor_64 {
909       uint64_t thunk;
910       uint64_t key;
911       uint64_t offset;
912     };
913 
914     struct tlv_descriptor {
915       uintptr_t thunk;
916       uintptr_t key;
917       uintptr_t offset;
918     };
919 
920     struct entry_point_command {
921       uint32_t cmd;
922       uint32_t cmdsize;
923       uint64_t entryoff;
924       uint64_t stacksize;
925     };
926 
927 
928     // Structs from <mach-o/fat.h>
929     struct fat_header {
930       uint32_t magic;
931       uint32_t nfat_arch;
932     };
933 
934     struct fat_arch {
935       uint32_t cputype;
936       uint32_t cpusubtype;
937       uint32_t offset;
938       uint32_t size;
939       uint32_t align;
940     };
941 
942     // Structs from <mach-o/reloc.h>
943     struct relocation_info {
944       int32_t r_address;
945       uint32_t r_symbolnum:24,
946                r_pcrel:1,
947                r_length:2,
948                r_extern:1,
949                r_type:4;
950     };
951 
952     struct scattered_relocation_info {
953 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
954       uint32_t r_scattered:1,
955                r_pcrel:1,
956                r_length:2,
957                r_type:4,
958                r_address:24;
959 #else
960       uint32_t r_address:24,
961                r_type:4,
962                r_length:2,
963                r_pcrel:1,
964                r_scattered:1;
965 #endif
966       int32_t r_value;
967     };
968 
969     // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
970     struct any_relocation_info {
971       uint32_t r_word0, r_word1;
972     };
973 
974     // Structs from <mach-o/nlist.h>
975     struct nlist_base {
976       uint32_t n_strx;
977       uint8_t n_type;
978       uint8_t n_sect;
979       uint16_t n_desc;
980     };
981 
982     struct nlist {
983       uint32_t n_strx;
984       uint8_t n_type;
985       uint8_t n_sect;
986       int16_t n_desc;
987       uint32_t n_value;
988     };
989 
990     struct nlist_64 {
991       uint32_t n_strx;
992       uint8_t n_type;
993       uint8_t n_sect;
994       uint16_t n_desc;
995       uint64_t n_value;
996     };
997 
998 
999     // Byte order swapping functions for MachO structs
1000 
swapStruct(mach_header & mh)1001     inline void swapStruct(mach_header &mh) {
1002       sys::swapByteOrder(mh.magic);
1003       sys::swapByteOrder(mh.cputype);
1004       sys::swapByteOrder(mh.cpusubtype);
1005       sys::swapByteOrder(mh.filetype);
1006       sys::swapByteOrder(mh.ncmds);
1007       sys::swapByteOrder(mh.sizeofcmds);
1008       sys::swapByteOrder(mh.flags);
1009     }
1010 
swapStruct(mach_header_64 & H)1011     inline void swapStruct(mach_header_64 &H) {
1012       sys::swapByteOrder(H.magic);
1013       sys::swapByteOrder(H.cputype);
1014       sys::swapByteOrder(H.cpusubtype);
1015       sys::swapByteOrder(H.filetype);
1016       sys::swapByteOrder(H.ncmds);
1017       sys::swapByteOrder(H.sizeofcmds);
1018       sys::swapByteOrder(H.flags);
1019       sys::swapByteOrder(H.reserved);
1020     }
1021 
swapStruct(load_command & lc)1022     inline void swapStruct(load_command &lc) {
1023       sys::swapByteOrder(lc.cmd);
1024       sys::swapByteOrder(lc.cmdsize);
1025     }
1026 
swapStruct(symtab_command & lc)1027     inline void swapStruct(symtab_command &lc) {
1028       sys::swapByteOrder(lc.cmd);
1029       sys::swapByteOrder(lc.cmdsize);
1030       sys::swapByteOrder(lc.symoff);
1031       sys::swapByteOrder(lc.nsyms);
1032       sys::swapByteOrder(lc.stroff);
1033       sys::swapByteOrder(lc.strsize);
1034     }
1035 
swapStruct(segment_command_64 & seg)1036     inline void swapStruct(segment_command_64 &seg) {
1037       sys::swapByteOrder(seg.cmd);
1038       sys::swapByteOrder(seg.cmdsize);
1039       sys::swapByteOrder(seg.vmaddr);
1040       sys::swapByteOrder(seg.vmsize);
1041       sys::swapByteOrder(seg.fileoff);
1042       sys::swapByteOrder(seg.filesize);
1043       sys::swapByteOrder(seg.maxprot);
1044       sys::swapByteOrder(seg.initprot);
1045       sys::swapByteOrder(seg.nsects);
1046       sys::swapByteOrder(seg.flags);
1047     }
1048 
swapStruct(segment_command & seg)1049     inline void swapStruct(segment_command &seg) {
1050       sys::swapByteOrder(seg.cmd);
1051       sys::swapByteOrder(seg.cmdsize);
1052       sys::swapByteOrder(seg.vmaddr);
1053       sys::swapByteOrder(seg.vmsize);
1054       sys::swapByteOrder(seg.fileoff);
1055       sys::swapByteOrder(seg.filesize);
1056       sys::swapByteOrder(seg.maxprot);
1057       sys::swapByteOrder(seg.initprot);
1058       sys::swapByteOrder(seg.nsects);
1059       sys::swapByteOrder(seg.flags);
1060     }
1061 
swapStruct(section_64 & sect)1062     inline void swapStruct(section_64 &sect) {
1063       sys::swapByteOrder(sect.addr);
1064       sys::swapByteOrder(sect.size);
1065       sys::swapByteOrder(sect.offset);
1066       sys::swapByteOrder(sect.align);
1067       sys::swapByteOrder(sect.reloff);
1068       sys::swapByteOrder(sect.nreloc);
1069       sys::swapByteOrder(sect.flags);
1070       sys::swapByteOrder(sect.reserved1);
1071       sys::swapByteOrder(sect.reserved2);
1072     }
1073 
swapStruct(section & sect)1074     inline void swapStruct(section &sect) {
1075       sys::swapByteOrder(sect.addr);
1076       sys::swapByteOrder(sect.size);
1077       sys::swapByteOrder(sect.offset);
1078       sys::swapByteOrder(sect.align);
1079       sys::swapByteOrder(sect.reloff);
1080       sys::swapByteOrder(sect.nreloc);
1081       sys::swapByteOrder(sect.flags);
1082       sys::swapByteOrder(sect.reserved1);
1083       sys::swapByteOrder(sect.reserved2);
1084     }
1085 
swapStruct(dyld_info_command & info)1086     inline void swapStruct(dyld_info_command &info) {
1087       sys::swapByteOrder(info.cmd);
1088       sys::swapByteOrder(info.cmdsize);
1089       sys::swapByteOrder(info.rebase_off);
1090       sys::swapByteOrder(info.rebase_size);
1091       sys::swapByteOrder(info.bind_off);
1092       sys::swapByteOrder(info.bind_size);
1093       sys::swapByteOrder(info.weak_bind_off);
1094       sys::swapByteOrder(info.weak_bind_size);
1095       sys::swapByteOrder(info.lazy_bind_off);
1096       sys::swapByteOrder(info.lazy_bind_size);
1097       sys::swapByteOrder(info.export_off);
1098       sys::swapByteOrder(info.export_size);
1099     }
1100 
swapStruct(dylib_command & d)1101     inline void swapStruct(dylib_command &d) {
1102       sys::swapByteOrder(d.cmd);
1103       sys::swapByteOrder(d.cmdsize);
1104       sys::swapByteOrder(d.dylib.name);
1105       sys::swapByteOrder(d.dylib.timestamp);
1106       sys::swapByteOrder(d.dylib.current_version);
1107       sys::swapByteOrder(d.dylib.compatibility_version);
1108     }
1109 
swapStruct(sub_framework_command & s)1110     inline void swapStruct(sub_framework_command &s) {
1111       sys::swapByteOrder(s.cmd);
1112       sys::swapByteOrder(s.cmdsize);
1113       sys::swapByteOrder(s.umbrella);
1114     }
1115 
swapStruct(sub_umbrella_command & s)1116     inline void swapStruct(sub_umbrella_command &s) {
1117       sys::swapByteOrder(s.cmd);
1118       sys::swapByteOrder(s.cmdsize);
1119       sys::swapByteOrder(s.sub_umbrella);
1120     }
1121 
swapStruct(sub_library_command & s)1122     inline void swapStruct(sub_library_command &s) {
1123       sys::swapByteOrder(s.cmd);
1124       sys::swapByteOrder(s.cmdsize);
1125       sys::swapByteOrder(s.sub_library);
1126     }
1127 
swapStruct(sub_client_command & s)1128     inline void swapStruct(sub_client_command &s) {
1129       sys::swapByteOrder(s.cmd);
1130       sys::swapByteOrder(s.cmdsize);
1131       sys::swapByteOrder(s.client);
1132     }
1133 
swapStruct(routines_command & r)1134     inline void swapStruct(routines_command &r) {
1135       sys::swapByteOrder(r.cmd);
1136       sys::swapByteOrder(r.cmdsize);
1137       sys::swapByteOrder(r.init_address);
1138       sys::swapByteOrder(r.init_module);
1139       sys::swapByteOrder(r.reserved1);
1140       sys::swapByteOrder(r.reserved2);
1141       sys::swapByteOrder(r.reserved3);
1142       sys::swapByteOrder(r.reserved4);
1143       sys::swapByteOrder(r.reserved5);
1144       sys::swapByteOrder(r.reserved6);
1145     }
1146 
swapStruct(routines_command_64 & r)1147     inline void swapStruct(routines_command_64 &r) {
1148       sys::swapByteOrder(r.cmd);
1149       sys::swapByteOrder(r.cmdsize);
1150       sys::swapByteOrder(r.init_address);
1151       sys::swapByteOrder(r.init_module);
1152       sys::swapByteOrder(r.reserved1);
1153       sys::swapByteOrder(r.reserved2);
1154       sys::swapByteOrder(r.reserved3);
1155       sys::swapByteOrder(r.reserved4);
1156       sys::swapByteOrder(r.reserved5);
1157       sys::swapByteOrder(r.reserved6);
1158     }
1159 
swapStruct(thread_command & t)1160     inline void swapStruct(thread_command &t) {
1161       sys::swapByteOrder(t.cmd);
1162       sys::swapByteOrder(t.cmdsize);
1163     }
1164 
swapStruct(dylinker_command & d)1165     inline void swapStruct(dylinker_command &d) {
1166       sys::swapByteOrder(d.cmd);
1167       sys::swapByteOrder(d.cmdsize);
1168       sys::swapByteOrder(d.name);
1169     }
1170 
swapStruct(uuid_command & u)1171     inline void swapStruct(uuid_command &u) {
1172       sys::swapByteOrder(u.cmd);
1173       sys::swapByteOrder(u.cmdsize);
1174     }
1175 
swapStruct(rpath_command & r)1176     inline void swapStruct(rpath_command &r) {
1177       sys::swapByteOrder(r.cmd);
1178       sys::swapByteOrder(r.cmdsize);
1179       sys::swapByteOrder(r.path);
1180     }
1181 
swapStruct(source_version_command & s)1182     inline void swapStruct(source_version_command &s) {
1183       sys::swapByteOrder(s.cmd);
1184       sys::swapByteOrder(s.cmdsize);
1185       sys::swapByteOrder(s.version);
1186     }
1187 
swapStruct(entry_point_command & e)1188     inline void swapStruct(entry_point_command &e) {
1189       sys::swapByteOrder(e.cmd);
1190       sys::swapByteOrder(e.cmdsize);
1191       sys::swapByteOrder(e.entryoff);
1192       sys::swapByteOrder(e.stacksize);
1193     }
1194 
swapStruct(encryption_info_command & e)1195     inline void swapStruct(encryption_info_command &e) {
1196       sys::swapByteOrder(e.cmd);
1197       sys::swapByteOrder(e.cmdsize);
1198       sys::swapByteOrder(e.cryptoff);
1199       sys::swapByteOrder(e.cryptsize);
1200       sys::swapByteOrder(e.cryptid);
1201     }
1202 
swapStruct(encryption_info_command_64 & e)1203     inline void swapStruct(encryption_info_command_64 &e) {
1204       sys::swapByteOrder(e.cmd);
1205       sys::swapByteOrder(e.cmdsize);
1206       sys::swapByteOrder(e.cryptoff);
1207       sys::swapByteOrder(e.cryptsize);
1208       sys::swapByteOrder(e.cryptid);
1209       sys::swapByteOrder(e.pad);
1210     }
1211 
swapStruct(dysymtab_command & dst)1212     inline void swapStruct(dysymtab_command &dst) {
1213       sys::swapByteOrder(dst.cmd);
1214       sys::swapByteOrder(dst.cmdsize);
1215       sys::swapByteOrder(dst.ilocalsym);
1216       sys::swapByteOrder(dst.nlocalsym);
1217       sys::swapByteOrder(dst.iextdefsym);
1218       sys::swapByteOrder(dst.nextdefsym);
1219       sys::swapByteOrder(dst.iundefsym);
1220       sys::swapByteOrder(dst.nundefsym);
1221       sys::swapByteOrder(dst.tocoff);
1222       sys::swapByteOrder(dst.ntoc);
1223       sys::swapByteOrder(dst.modtaboff);
1224       sys::swapByteOrder(dst.nmodtab);
1225       sys::swapByteOrder(dst.extrefsymoff);
1226       sys::swapByteOrder(dst.nextrefsyms);
1227       sys::swapByteOrder(dst.indirectsymoff);
1228       sys::swapByteOrder(dst.nindirectsyms);
1229       sys::swapByteOrder(dst.extreloff);
1230       sys::swapByteOrder(dst.nextrel);
1231       sys::swapByteOrder(dst.locreloff);
1232       sys::swapByteOrder(dst.nlocrel);
1233     }
1234 
swapStruct(any_relocation_info & reloc)1235     inline void swapStruct(any_relocation_info &reloc) {
1236       sys::swapByteOrder(reloc.r_word0);
1237       sys::swapByteOrder(reloc.r_word1);
1238     }
1239 
swapStruct(nlist_base & S)1240     inline void swapStruct(nlist_base &S) {
1241       sys::swapByteOrder(S.n_strx);
1242       sys::swapByteOrder(S.n_desc);
1243     }
1244 
swapStruct(nlist & sym)1245     inline void swapStruct(nlist &sym) {
1246       sys::swapByteOrder(sym.n_strx);
1247       sys::swapByteOrder(sym.n_desc);
1248       sys::swapByteOrder(sym.n_value);
1249     }
1250 
swapStruct(nlist_64 & sym)1251     inline void swapStruct(nlist_64 &sym) {
1252       sys::swapByteOrder(sym.n_strx);
1253       sys::swapByteOrder(sym.n_desc);
1254       sys::swapByteOrder(sym.n_value);
1255     }
1256 
swapStruct(linkedit_data_command & C)1257     inline void swapStruct(linkedit_data_command &C) {
1258       sys::swapByteOrder(C.cmd);
1259       sys::swapByteOrder(C.cmdsize);
1260       sys::swapByteOrder(C.dataoff);
1261       sys::swapByteOrder(C.datasize);
1262     }
1263 
swapStruct(linker_option_command & C)1264     inline void swapStruct(linker_option_command &C) {
1265       sys::swapByteOrder(C.cmd);
1266       sys::swapByteOrder(C.cmdsize);
1267       sys::swapByteOrder(C.count);
1268     }
1269 
swapStruct(version_min_command & C)1270     inline void swapStruct(version_min_command&C) {
1271       sys::swapByteOrder(C.cmd);
1272       sys::swapByteOrder(C.cmdsize);
1273       sys::swapByteOrder(C.version);
1274       sys::swapByteOrder(C.sdk);
1275     }
1276 
swapStruct(data_in_code_entry & C)1277     inline void swapStruct(data_in_code_entry &C) {
1278       sys::swapByteOrder(C.offset);
1279       sys::swapByteOrder(C.length);
1280       sys::swapByteOrder(C.kind);
1281     }
1282 
swapStruct(uint32_t & C)1283     inline void swapStruct(uint32_t &C) {
1284       sys::swapByteOrder(C);
1285     }
1286 
1287     // Get/Set functions from <mach-o/nlist.h>
1288 
GET_LIBRARY_ORDINAL(uint16_t n_desc)1289     static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
1290       return (((n_desc) >> 8u) & 0xffu);
1291     }
1292 
SET_LIBRARY_ORDINAL(uint16_t & n_desc,uint8_t ordinal)1293     static inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
1294       n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
1295     }
1296 
GET_COMM_ALIGN(uint16_t n_desc)1297     static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc) {
1298       return (n_desc >> 8u) & 0x0fu;
1299     }
1300 
SET_COMM_ALIGN(uint16_t & n_desc,uint8_t align)1301     static inline void SET_COMM_ALIGN (uint16_t &n_desc, uint8_t align) {
1302       n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
1303     }
1304 
1305     // Enums from <mach/machine.h>
1306     enum : uint32_t {
1307       // Capability bits used in the definition of cpu_type.
1308       CPU_ARCH_MASK  = 0xff000000,   // Mask for architecture bits
1309       CPU_ARCH_ABI64 = 0x01000000    // 64 bit ABI
1310     };
1311 
1312     // Constants for the cputype field.
1313     enum CPUType {
1314       CPU_TYPE_ANY       = -1,
1315       CPU_TYPE_X86       = 7,
1316       CPU_TYPE_I386      = CPU_TYPE_X86,
1317       CPU_TYPE_X86_64    = CPU_TYPE_X86 | CPU_ARCH_ABI64,
1318    /* CPU_TYPE_MIPS      = 8, */
1319       CPU_TYPE_MC98000   = 10, // Old Motorola PowerPC
1320       CPU_TYPE_ARM       = 12,
1321       CPU_TYPE_ARM64     = CPU_TYPE_ARM | CPU_ARCH_ABI64,
1322       CPU_TYPE_SPARC     = 14,
1323       CPU_TYPE_POWERPC   = 18,
1324       CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
1325     };
1326 
1327     enum : uint32_t {
1328       // Capability bits used in the definition of cpusubtype.
1329       CPU_SUBTYPE_MASK  = 0xff000000,   // Mask for architecture bits
1330       CPU_SUBTYPE_LIB64 = 0x80000000,   // 64 bit libraries
1331 
1332       // Special CPU subtype constants.
1333       CPU_SUBTYPE_MULTIPLE = ~0u
1334     };
1335 
1336     // Constants for the cpusubtype field.
1337     enum CPUSubTypeX86 {
1338       CPU_SUBTYPE_I386_ALL       = 3,
1339       CPU_SUBTYPE_386            = 3,
1340       CPU_SUBTYPE_486            = 4,
1341       CPU_SUBTYPE_486SX          = 0x84,
1342       CPU_SUBTYPE_586            = 5,
1343       CPU_SUBTYPE_PENT           = CPU_SUBTYPE_586,
1344       CPU_SUBTYPE_PENTPRO        = 0x16,
1345       CPU_SUBTYPE_PENTII_M3      = 0x36,
1346       CPU_SUBTYPE_PENTII_M5      = 0x56,
1347       CPU_SUBTYPE_CELERON        = 0x67,
1348       CPU_SUBTYPE_CELERON_MOBILE = 0x77,
1349       CPU_SUBTYPE_PENTIUM_3      = 0x08,
1350       CPU_SUBTYPE_PENTIUM_3_M    = 0x18,
1351       CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
1352       CPU_SUBTYPE_PENTIUM_M      = 0x09,
1353       CPU_SUBTYPE_PENTIUM_4      = 0x0a,
1354       CPU_SUBTYPE_PENTIUM_4_M    = 0x1a,
1355       CPU_SUBTYPE_ITANIUM        = 0x0b,
1356       CPU_SUBTYPE_ITANIUM_2      = 0x1b,
1357       CPU_SUBTYPE_XEON           = 0x0c,
1358       CPU_SUBTYPE_XEON_MP        = 0x1c,
1359 
1360       CPU_SUBTYPE_X86_ALL     = 3,
1361       CPU_SUBTYPE_X86_64_ALL  = 3,
1362       CPU_SUBTYPE_X86_ARCH1   = 4,
1363       CPU_SUBTYPE_X86_64_H    = 8
1364     };
CPU_SUBTYPE_INTEL(int Family,int Model)1365     static inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
1366       return Family | (Model << 4);
1367     }
CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST)1368     static inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
1369       return ((int)ST) & 0x0f;
1370     }
CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST)1371     static inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) {
1372       return ((int)ST) >> 4;
1373     }
1374     enum {
1375       CPU_SUBTYPE_INTEL_FAMILY_MAX = 15,
1376       CPU_SUBTYPE_INTEL_MODEL_ALL  = 0
1377     };
1378 
1379     enum CPUSubTypeARM {
1380       CPU_SUBTYPE_ARM_ALL     = 0,
1381       CPU_SUBTYPE_ARM_V4T     = 5,
1382       CPU_SUBTYPE_ARM_V6      = 6,
1383       CPU_SUBTYPE_ARM_V5      = 7,
1384       CPU_SUBTYPE_ARM_V5TEJ   = 7,
1385       CPU_SUBTYPE_ARM_XSCALE  = 8,
1386       CPU_SUBTYPE_ARM_V7      = 9,
1387       //  unused  ARM_V7F     = 10,
1388       CPU_SUBTYPE_ARM_V7S     = 11,
1389       CPU_SUBTYPE_ARM_V7K     = 12,
1390       CPU_SUBTYPE_ARM_V6M     = 14,
1391       CPU_SUBTYPE_ARM_V7M     = 15,
1392       CPU_SUBTYPE_ARM_V7EM    = 16
1393     };
1394 
1395     enum CPUSubTypeARM64 {
1396       CPU_SUBTYPE_ARM64_ALL   = 0
1397     };
1398 
1399     enum CPUSubTypeSPARC {
1400       CPU_SUBTYPE_SPARC_ALL   = 0
1401     };
1402 
1403     enum CPUSubTypePowerPC {
1404       CPU_SUBTYPE_POWERPC_ALL   = 0,
1405       CPU_SUBTYPE_POWERPC_601   = 1,
1406       CPU_SUBTYPE_POWERPC_602   = 2,
1407       CPU_SUBTYPE_POWERPC_603   = 3,
1408       CPU_SUBTYPE_POWERPC_603e  = 4,
1409       CPU_SUBTYPE_POWERPC_603ev = 5,
1410       CPU_SUBTYPE_POWERPC_604   = 6,
1411       CPU_SUBTYPE_POWERPC_604e  = 7,
1412       CPU_SUBTYPE_POWERPC_620   = 8,
1413       CPU_SUBTYPE_POWERPC_750   = 9,
1414       CPU_SUBTYPE_POWERPC_7400  = 10,
1415       CPU_SUBTYPE_POWERPC_7450  = 11,
1416       CPU_SUBTYPE_POWERPC_970   = 100,
1417 
1418       CPU_SUBTYPE_MC980000_ALL  = CPU_SUBTYPE_POWERPC_ALL,
1419       CPU_SUBTYPE_MC98601       = CPU_SUBTYPE_POWERPC_601
1420     };
1421 
1422     struct x86_thread_state64_t {
1423       uint64_t rax;
1424       uint64_t rbx;
1425       uint64_t rcx;
1426       uint64_t rdx;
1427       uint64_t rdi;
1428       uint64_t rsi;
1429       uint64_t rbp;
1430       uint64_t rsp;
1431       uint64_t r8;
1432       uint64_t r9;
1433       uint64_t r10;
1434       uint64_t r11;
1435       uint64_t r12;
1436       uint64_t r13;
1437       uint64_t r14;
1438       uint64_t r15;
1439       uint64_t rip;
1440       uint64_t rflags;
1441       uint64_t cs;
1442       uint64_t fs;
1443       uint64_t gs;
1444     };
1445 
1446     enum x86_fp_control_precis {
1447       x86_FP_PREC_24B = 0,
1448       x86_FP_PREC_53B = 2,
1449       x86_FP_PREC_64B = 3
1450     };
1451 
1452     enum x86_fp_control_rc {
1453       x86_FP_RND_NEAR = 0,
1454       x86_FP_RND_DOWN = 1,
1455       x86_FP_RND_UP = 2,
1456       x86_FP_CHOP = 3
1457     };
1458 
1459     struct fp_control_t {
1460       unsigned short
1461        invalid :1,
1462        denorm  :1,
1463        zdiv    :1,
1464        ovrfl   :1,
1465        undfl   :1,
1466        precis  :1,
1467                :2,
1468        pc      :2,
1469        rc      :2,
1470                :1,
1471                :3;
1472     };
1473 
1474     struct fp_status_t {
1475       unsigned short
1476         invalid :1,
1477         denorm  :1,
1478         zdiv    :1,
1479         ovrfl   :1,
1480         undfl   :1,
1481         precis  :1,
1482         stkflt  :1,
1483         errsumm :1,
1484         c0      :1,
1485         c1      :1,
1486         c2      :1,
1487         tos     :3,
1488         c3      :1,
1489         busy    :1;
1490     };
1491 
1492     struct mmst_reg_t {
1493       char mmst_reg[10];
1494       char mmst_rsrv[6];
1495     };
1496 
1497     struct xmm_reg_t {
1498       char xmm_reg[16];
1499     };
1500 
1501     struct x86_float_state64_t {
1502       int32_t fpu_reserved[2];
1503       fp_control_t fpu_fcw;
1504       fp_status_t fpu_fsw;
1505       uint8_t fpu_ftw;
1506       uint8_t fpu_rsrv1;
1507       uint16_t fpu_fop;
1508       uint32_t fpu_ip;
1509       uint16_t fpu_cs;
1510       uint16_t fpu_rsrv2;
1511       uint32_t fpu_dp;
1512       uint16_t fpu_ds;
1513       uint16_t fpu_rsrv3;
1514       uint32_t fpu_mxcsr;
1515       uint32_t fpu_mxcsrmask;
1516       mmst_reg_t fpu_stmm0;
1517       mmst_reg_t fpu_stmm1;
1518       mmst_reg_t fpu_stmm2;
1519       mmst_reg_t fpu_stmm3;
1520       mmst_reg_t fpu_stmm4;
1521       mmst_reg_t fpu_stmm5;
1522       mmst_reg_t fpu_stmm6;
1523       mmst_reg_t fpu_stmm7;
1524       xmm_reg_t fpu_xmm0;
1525       xmm_reg_t fpu_xmm1;
1526       xmm_reg_t fpu_xmm2;
1527       xmm_reg_t fpu_xmm3;
1528       xmm_reg_t fpu_xmm4;
1529       xmm_reg_t fpu_xmm5;
1530       xmm_reg_t fpu_xmm6;
1531       xmm_reg_t fpu_xmm7;
1532       xmm_reg_t fpu_xmm8;
1533       xmm_reg_t fpu_xmm9;
1534       xmm_reg_t fpu_xmm10;
1535       xmm_reg_t fpu_xmm11;
1536       xmm_reg_t fpu_xmm12;
1537       xmm_reg_t fpu_xmm13;
1538       xmm_reg_t fpu_xmm14;
1539       xmm_reg_t fpu_xmm15;
1540       char fpu_rsrv4[6*16];
1541       uint32_t fpu_reserved1;
1542     };
1543 
1544     struct x86_exception_state64_t {
1545       uint16_t trapno;
1546       uint16_t cpu;
1547       uint32_t err;
1548       uint64_t faultvaddr;
1549     };
1550 
swapStruct(x86_thread_state64_t & x)1551     inline void swapStruct(x86_thread_state64_t &x) {
1552       sys::swapByteOrder(x.rax);
1553       sys::swapByteOrder(x.rbx);
1554       sys::swapByteOrder(x.rcx);
1555       sys::swapByteOrder(x.rdx);
1556       sys::swapByteOrder(x.rdi);
1557       sys::swapByteOrder(x.rsi);
1558       sys::swapByteOrder(x.rbp);
1559       sys::swapByteOrder(x.rsp);
1560       sys::swapByteOrder(x.r8);
1561       sys::swapByteOrder(x.r9);
1562       sys::swapByteOrder(x.r10);
1563       sys::swapByteOrder(x.r11);
1564       sys::swapByteOrder(x.r12);
1565       sys::swapByteOrder(x.r13);
1566       sys::swapByteOrder(x.r14);
1567       sys::swapByteOrder(x.r15);
1568       sys::swapByteOrder(x.rip);
1569       sys::swapByteOrder(x.rflags);
1570       sys::swapByteOrder(x.cs);
1571       sys::swapByteOrder(x.fs);
1572       sys::swapByteOrder(x.gs);
1573     }
1574 
swapStruct(x86_float_state64_t & x)1575     inline void swapStruct(x86_float_state64_t &x) {
1576       sys::swapByteOrder(x.fpu_reserved[0]);
1577       sys::swapByteOrder(x.fpu_reserved[1]);
1578       // TODO swap: fp_control_t fpu_fcw;
1579       // TODO swap: fp_status_t fpu_fsw;
1580       sys::swapByteOrder(x.fpu_fop);
1581       sys::swapByteOrder(x.fpu_ip);
1582       sys::swapByteOrder(x.fpu_cs);
1583       sys::swapByteOrder(x.fpu_rsrv2);
1584       sys::swapByteOrder(x.fpu_dp);
1585       sys::swapByteOrder(x.fpu_ds);
1586       sys::swapByteOrder(x.fpu_rsrv3);
1587       sys::swapByteOrder(x.fpu_mxcsr);
1588       sys::swapByteOrder(x.fpu_mxcsrmask);
1589       sys::swapByteOrder(x.fpu_reserved1);
1590     }
1591 
swapStruct(x86_exception_state64_t & x)1592     inline void swapStruct(x86_exception_state64_t &x) {
1593       sys::swapByteOrder(x.trapno);
1594       sys::swapByteOrder(x.cpu);
1595       sys::swapByteOrder(x.err);
1596       sys::swapByteOrder(x.faultvaddr);
1597     }
1598 
1599     struct x86_state_hdr_t {
1600       uint32_t flavor;
1601       uint32_t count;
1602     };
1603 
1604     struct x86_thread_state_t {
1605       x86_state_hdr_t tsh;
1606       union {
1607         x86_thread_state64_t ts64;
1608       } uts;
1609     };
1610 
1611     struct x86_float_state_t {
1612       x86_state_hdr_t fsh;
1613       union {
1614         x86_float_state64_t fs64;
1615       } ufs;
1616     };
1617 
1618     struct x86_exception_state_t {
1619       x86_state_hdr_t esh;
1620       union {
1621         x86_exception_state64_t es64;
1622       } ues;
1623     };
1624 
swapStruct(x86_state_hdr_t & x)1625     inline void swapStruct(x86_state_hdr_t &x) {
1626       sys::swapByteOrder(x.flavor);
1627       sys::swapByteOrder(x.count);
1628     }
1629 
1630     enum X86ThreadFlavors {
1631       x86_THREAD_STATE32    = 1,
1632       x86_FLOAT_STATE32     = 2,
1633       x86_EXCEPTION_STATE32 = 3,
1634       x86_THREAD_STATE64    = 4,
1635       x86_FLOAT_STATE64     = 5,
1636       x86_EXCEPTION_STATE64 = 6,
1637       x86_THREAD_STATE      = 7,
1638       x86_FLOAT_STATE       = 8,
1639       x86_EXCEPTION_STATE   = 9,
1640       x86_DEBUG_STATE32     = 10,
1641       x86_DEBUG_STATE64     = 11,
1642       x86_DEBUG_STATE       = 12
1643     };
1644 
swapStruct(x86_thread_state_t & x)1645     inline void swapStruct(x86_thread_state_t &x) {
1646       swapStruct(x.tsh);
1647       if (x.tsh.flavor == x86_THREAD_STATE64)
1648         swapStruct(x.uts.ts64);
1649     }
1650 
swapStruct(x86_float_state_t & x)1651     inline void swapStruct(x86_float_state_t &x) {
1652       swapStruct(x.fsh);
1653       if (x.fsh.flavor == x86_FLOAT_STATE64)
1654         swapStruct(x.ufs.fs64);
1655     }
1656 
swapStruct(x86_exception_state_t & x)1657     inline void swapStruct(x86_exception_state_t &x) {
1658       swapStruct(x.esh);
1659       if (x.esh.flavor == x86_EXCEPTION_STATE64)
1660         swapStruct(x.ues.es64);
1661     }
1662 
1663     const uint32_t x86_THREAD_STATE64_COUNT =
1664       sizeof(x86_thread_state64_t) / sizeof(uint32_t);
1665     const uint32_t x86_FLOAT_STATE64_COUNT =
1666       sizeof(x86_float_state64_t) / sizeof(uint32_t);
1667     const uint32_t x86_EXCEPTION_STATE64_COUNT =
1668       sizeof(x86_exception_state64_t) / sizeof(uint32_t);
1669 
1670     const uint32_t x86_THREAD_STATE_COUNT =
1671       sizeof(x86_thread_state_t) / sizeof(uint32_t);
1672     const uint32_t x86_FLOAT_STATE_COUNT =
1673       sizeof(x86_float_state_t) / sizeof(uint32_t);
1674     const uint32_t x86_EXCEPTION_STATE_COUNT =
1675       sizeof(x86_exception_state_t) / sizeof(uint32_t);
1676 
1677   } // end namespace MachO
1678 } // end namespace llvm
1679 
1680 #endif
1681