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