ARM64: initial backend import
[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     };
78
79     enum : uint32_t {
80       // Flags for the "cmd" field in llvm::MachO::load_command
81       LC_REQ_DYLD    = 0x80000000u
82     };
83
84     enum LoadCommandType : uint32_t {
85       // Constants for the "cmd" field in llvm::MachO::load_command
86       LC_SEGMENT              = 0x00000001u,
87       LC_SYMTAB               = 0x00000002u,
88       LC_SYMSEG               = 0x00000003u,
89       LC_THREAD               = 0x00000004u,
90       LC_UNIXTHREAD           = 0x00000005u,
91       LC_LOADFVMLIB           = 0x00000006u,
92       LC_IDFVMLIB             = 0x00000007u,
93       LC_IDENT                = 0x00000008u,
94       LC_FVMFILE              = 0x00000009u,
95       LC_PREPAGE              = 0x0000000Au,
96       LC_DYSYMTAB             = 0x0000000Bu,
97       LC_LOAD_DYLIB           = 0x0000000Cu,
98       LC_ID_DYLIB             = 0x0000000Du,
99       LC_LOAD_DYLINKER        = 0x0000000Eu,
100       LC_ID_DYLINKER          = 0x0000000Fu,
101       LC_PREBOUND_DYLIB       = 0x00000010u,
102       LC_ROUTINES             = 0x00000011u,
103       LC_SUB_FRAMEWORK        = 0x00000012u,
104       LC_SUB_UMBRELLA         = 0x00000013u,
105       LC_SUB_CLIENT           = 0x00000014u,
106       LC_SUB_LIBRARY          = 0x00000015u,
107       LC_TWOLEVEL_HINTS       = 0x00000016u,
108       LC_PREBIND_CKSUM        = 0x00000017u,
109       LC_LOAD_WEAK_DYLIB      = 0x80000018u,
110       LC_SEGMENT_64           = 0x00000019u,
111       LC_ROUTINES_64          = 0x0000001Au,
112       LC_UUID                 = 0x0000001Bu,
113       LC_RPATH                = 0x8000001Cu,
114       LC_CODE_SIGNATURE       = 0x0000001Du,
115       LC_SEGMENT_SPLIT_INFO   = 0x0000001Eu,
116       LC_REEXPORT_DYLIB       = 0x8000001Fu,
117       LC_LAZY_LOAD_DYLIB      = 0x00000020u,
118       LC_ENCRYPTION_INFO      = 0x00000021u,
119       LC_DYLD_INFO            = 0x00000022u,
120       LC_DYLD_INFO_ONLY       = 0x80000022u,
121       LC_LOAD_UPWARD_DYLIB    = 0x80000023u,
122       LC_VERSION_MIN_MACOSX   = 0x00000024u,
123       LC_VERSION_MIN_IPHONEOS = 0x00000025u,
124       LC_FUNCTION_STARTS      = 0x00000026u,
125       LC_DYLD_ENVIRONMENT     = 0x00000027u,
126       LC_MAIN                 = 0x80000028u,
127       LC_DATA_IN_CODE         = 0x00000029u,
128       LC_SOURCE_VERSION       = 0x0000002Au,
129       LC_DYLIB_CODE_SIGN_DRS  = 0x0000002Bu,
130       //                        0x0000002Cu,
131       LC_LINKER_OPTIONS       = 0x0000002Du,
132       LC_LINKER_OPTIMIZATION_HINT = 0x0000002Eu
133     };
134
135     enum : uint32_t {
136       // Constant bits for the "flags" field in llvm::MachO::segment_command
137       SG_HIGHVM              = 0x1u,
138       SG_FVMLIB              = 0x2u,
139       SG_NORELOC             = 0x4u,
140       SG_PROTECTED_VERSION_1 = 0x8u,
141
142
143       // Constant masks for the "flags" field in llvm::MachO::section and
144       // llvm::MachO::section_64
145       SECTION_TYPE           = 0x000000ffu, // SECTION_TYPE
146       SECTION_ATTRIBUTES     = 0xffffff00u, // SECTION_ATTRIBUTES
147       SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
148       SECTION_ATTRIBUTES_SYS = 0x00ffff00u  // SECTION_ATTRIBUTES_SYS
149     };
150
151     /// These are the section type and attributes fields.  A MachO section can
152     /// have only one Type, but can have any of the attributes specified.
153     enum SectionType : uint32_t {
154       // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
155       // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
156       S_REGULAR                             = 0x00u,
157       S_ZEROFILL                            = 0x01u,
158       S_CSTRING_LITERALS                    = 0x02u,
159       S_4BYTE_LITERALS                      = 0x03u,
160       S_8BYTE_LITERALS                      = 0x04u,
161       S_LITERAL_POINTERS                    = 0x05u,
162       S_NON_LAZY_SYMBOL_POINTERS            = 0x06u,
163       S_LAZY_SYMBOL_POINTERS                = 0x07u,
164       S_SYMBOL_STUBS                        = 0x08u,
165       S_MOD_INIT_FUNC_POINTERS              = 0x09u,
166       S_MOD_TERM_FUNC_POINTERS              = 0x0au,
167       S_COALESCED                           = 0x0bu,
168       S_GB_ZEROFILL                         = 0x0cu,
169       S_INTERPOSING                         = 0x0du,
170       S_16BYTE_LITERALS                     = 0x0eu,
171       S_DTRACE_DOF                          = 0x0fu,
172       S_LAZY_DYLIB_SYMBOL_POINTERS          = 0x10u,
173       S_THREAD_LOCAL_REGULAR                = 0x11u,
174       S_THREAD_LOCAL_ZEROFILL               = 0x12u,
175       S_THREAD_LOCAL_VARIABLES              = 0x13u,
176       S_THREAD_LOCAL_VARIABLE_POINTERS      = 0x14u,
177       S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
178
179       LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
180     };
181
182     enum : uint32_t {
183       // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
184       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
185       S_ATTR_PURE_INSTRUCTIONS   = 0x80000000u,
186       S_ATTR_NO_TOC              = 0x40000000u,
187       S_ATTR_STRIP_STATIC_SYMS   = 0x20000000u,
188       S_ATTR_NO_DEAD_STRIP       = 0x10000000u,
189       S_ATTR_LIVE_SUPPORT        = 0x08000000u,
190       S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
191       S_ATTR_DEBUG               = 0x02000000u,
192
193       // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
194       // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
195       S_ATTR_SOME_INSTRUCTIONS   = 0x00000400u,
196       S_ATTR_EXT_RELOC           = 0x00000200u,
197       S_ATTR_LOC_RELOC           = 0x00000100u,
198
199       // Constant masks for the value of an indirect symbol in an indirect
200       // symbol table
201       INDIRECT_SYMBOL_LOCAL = 0x80000000u,
202       INDIRECT_SYMBOL_ABS   = 0x40000000u
203     };
204
205     enum DataRegionType {
206       // Constants for the "kind" field in a data_in_code_entry structure
207       DICE_KIND_DATA             = 1u,
208       DICE_KIND_JUMP_TABLE8      = 2u,
209       DICE_KIND_JUMP_TABLE16     = 3u,
210       DICE_KIND_JUMP_TABLE32     = 4u,
211       DICE_KIND_ABS_JUMP_TABLE32 = 5u
212     };
213
214     enum RebaseType {
215       REBASE_TYPE_POINTER         = 1u,
216       REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
217       REBASE_TYPE_TEXT_PCREL32    = 3u
218     };
219
220     enum {
221       REBASE_OPCODE_MASK    = 0xF0u,
222       REBASE_IMMEDIATE_MASK = 0x0Fu
223     };
224
225     enum RebaseOpcode {
226       REBASE_OPCODE_DONE                               = 0x00u,
227       REBASE_OPCODE_SET_TYPE_IMM                       = 0x10u,
228       REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB        = 0x20u,
229       REBASE_OPCODE_ADD_ADDR_ULEB                      = 0x30u,
230       REBASE_OPCODE_ADD_ADDR_IMM_SCALED                = 0x40u,
231       REBASE_OPCODE_DO_REBASE_IMM_TIMES                = 0x50u,
232       REBASE_OPCODE_DO_REBASE_ULEB_TIMES               = 0x60u,
233       REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB            = 0x70u,
234       REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
235     };
236
237     enum BindType {
238       BIND_TYPE_POINTER         = 1u,
239       BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
240       BIND_TYPE_TEXT_PCREL32    = 3u
241     };
242
243     enum BindSpecialDylib {
244       BIND_SPECIAL_DYLIB_SELF            =  0,
245       BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
246       BIND_SPECIAL_DYLIB_FLAT_LOOKUP     = -2
247     };
248
249     enum {
250       BIND_SYMBOL_FLAGS_WEAK_IMPORT         = 0x1u,
251       BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
252
253       BIND_OPCODE_MASK                      = 0xF0u,
254       BIND_IMMEDIATE_MASK                   = 0x0Fu
255     };
256
257     enum BindOpcode {
258       BIND_OPCODE_DONE                             = 0x00u,
259       BIND_OPCODE_SET_DYLIB_ORDINAL_IMM            = 0x10u,
260       BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB           = 0x20u,
261       BIND_OPCODE_SET_DYLIB_SPECIAL_IMM            = 0x30u,
262       BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM    = 0x40u,
263       BIND_OPCODE_SET_TYPE_IMM                     = 0x50u,
264       BIND_OPCODE_SET_ADDEND_SLEB                  = 0x60u,
265       BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB      = 0x70u,
266       BIND_OPCODE_ADD_ADDR_ULEB                    = 0x80u,
267       BIND_OPCODE_DO_BIND                          = 0x90u,
268       BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB            = 0xA0u,
269       BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED      = 0xB0u,
270       BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
271     };
272
273     enum {
274       EXPORT_SYMBOL_FLAGS_KIND_MASK           = 0x03u,
275       EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION     = 0x04u,
276       EXPORT_SYMBOL_FLAGS_REEXPORT            = 0x08u,
277       EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER   = 0x10u
278     };
279
280     enum ExportSymbolKind {
281       EXPORT_SYMBOL_FLAGS_KIND_REGULAR        = 0x00u,
282       EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL   = 0x01u
283     };
284
285
286     enum {
287       // Constant masks for the "n_type" field in llvm::MachO::nlist and
288       // llvm::MachO::nlist_64
289       N_STAB = 0xe0,
290       N_PEXT = 0x10,
291       N_TYPE = 0x0e,
292       N_EXT  = 0x01
293     };
294
295     enum NListType {
296       // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
297       // llvm::MachO::nlist_64
298       N_UNDF = 0x0u,
299       N_ABS  = 0x2u,
300       N_SECT = 0xeu,
301       N_PBUD = 0xcu,
302       N_INDR = 0xau
303     };
304
305     enum SectionOrdinal {
306       // Constants for the "n_sect" field in llvm::MachO::nlist and
307       // llvm::MachO::nlist_64
308       NO_SECT  = 0u,
309       MAX_SECT = 0xffu
310     };
311
312     enum {
313       // Constant masks for the "n_desc" field in llvm::MachO::nlist and
314       // llvm::MachO::nlist_64
315       N_ARM_THUMB_DEF   = 0x0008u,
316       N_NO_DEAD_STRIP   = 0x0020u,
317       N_WEAK_REF        = 0x0040u,
318       N_WEAK_DEF        = 0x0080u,
319       N_SYMBOL_RESOLVER = 0x0100u
320     };
321
322     enum StabType {
323       // Constant values for the "n_type" field in llvm::MachO::nlist and
324       // llvm::MachO::nlist_64 when "(n_type & NlistMaskStab) != 0"
325       N_GSYM    = 0x20u,
326       N_FNAME   = 0x22u,
327       N_FUN     = 0x24u,
328       N_STSYM   = 0x26u,
329       N_LCSYM   = 0x28u,
330       N_BNSYM   = 0x2Eu,
331       N_OPT     = 0x3Cu,
332       N_RSYM    = 0x40u,
333       N_SLINE   = 0x44u,
334       N_ENSYM   = 0x4Eu,
335       N_SSYM    = 0x60u,
336       N_SO      = 0x64u,
337       N_OSO     = 0x66u,
338       N_LSYM    = 0x80u,
339       N_BINCL   = 0x82u,
340       N_SOL     = 0x84u,
341       N_PARAMS  = 0x86u,
342       N_VERSION = 0x88u,
343       N_OLEVEL  = 0x8Au,
344       N_PSYM    = 0xA0u,
345       N_EINCL   = 0xA2u,
346       N_ENTRY   = 0xA4u,
347       N_LBRAC   = 0xC0u,
348       N_EXCL    = 0xC2u,
349       N_RBRAC   = 0xE0u,
350       N_BCOMM   = 0xE2u,
351       N_ECOMM   = 0xE4u,
352       N_ECOML   = 0xE8u,
353       N_LENG    = 0xFEu
354     };
355
356     enum : uint32_t {
357       // Constant values for the r_symbolnum field in an
358       // llvm::MachO::relocation_info structure when r_extern is 0.
359       R_ABS = 0,
360
361       // Constant bits for the r_address field in an
362       // llvm::MachO::relocation_info structure.
363       R_SCATTERED = 0x80000000
364     };
365
366     enum RelocationInfoType {
367       // Constant values for the r_type field in an
368       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
369       // structure.
370       GENERIC_RELOC_VANILLA        = 0,
371       GENERIC_RELOC_PAIR           = 1,
372       GENERIC_RELOC_SECTDIFF       = 2,
373       GENERIC_RELOC_PB_LA_PTR      = 3,
374       GENERIC_RELOC_LOCAL_SECTDIFF = 4,
375       GENERIC_RELOC_TLV            = 5,
376
377       // Constant values for the r_type field in a PowerPC architecture
378       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
379       // structure.
380       PPC_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
381       PPC_RELOC_PAIR               = GENERIC_RELOC_PAIR,
382       PPC_RELOC_BR14               = 2,
383       PPC_RELOC_BR24               = 3,
384       PPC_RELOC_HI16               = 4,
385       PPC_RELOC_LO16               = 5,
386       PPC_RELOC_HA16               = 6,
387       PPC_RELOC_LO14               = 7,
388       PPC_RELOC_SECTDIFF           = 8,
389       PPC_RELOC_PB_LA_PTR          = 9,
390       PPC_RELOC_HI16_SECTDIFF      = 10,
391       PPC_RELOC_LO16_SECTDIFF      = 11,
392       PPC_RELOC_HA16_SECTDIFF      = 12,
393       PPC_RELOC_JBSR               = 13,
394       PPC_RELOC_LO14_SECTDIFF      = 14,
395       PPC_RELOC_LOCAL_SECTDIFF     = 15,
396
397       // Constant values for the r_type field in an ARM architecture
398       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
399       // structure.
400       ARM_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
401       ARM_RELOC_PAIR               = GENERIC_RELOC_PAIR,
402       ARM_RELOC_SECTDIFF           = GENERIC_RELOC_SECTDIFF,
403       ARM_RELOC_LOCAL_SECTDIFF     = 3,
404       ARM_RELOC_PB_LA_PTR          = 4,
405       ARM_RELOC_BR24               = 5,
406       ARM_THUMB_RELOC_BR22         = 6,
407       ARM_THUMB_32BIT_BRANCH       = 7, // obsolete
408       ARM_RELOC_HALF               = 8,
409       ARM_RELOC_HALF_SECTDIFF      = 9,
410
411       // Constant values for the r_type field in an ARM64 architecture
412       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
413       // structure.
414
415       // For pointers.
416       ARM64_RELOC_UNSIGNED            = 0,
417       // Must be followed by an ARM64_RELOC_UNSIGNED
418       ARM64_RELOC_SUBTRACTOR          = 1,
419       // A B/BL instruction with 26-bit displacement.
420       ARM64_RELOC_BRANCH26            = 2,
421       // PC-rel distance to page of target.
422       ARM64_RELOC_PAGE21              = 3,
423       // Offset within page, scaled by r_length.
424       ARM64_RELOC_PAGEOFF12           = 4,
425       // PC-rel distance to page of GOT slot.
426       ARM64_RELOC_GOT_LOAD_PAGE21     = 5,
427       // Offset within page of GOT slot, scaled by r_length.
428       ARM64_RELOC_GOT_LOAD_PAGEOFF12  = 6,
429       // For pointers to GOT slots.
430       ARM64_RELOC_POINTER_TO_GOT      = 7,
431       // PC-rel distance to page of TLVP slot.
432       ARM64_RELOC_TLVP_LOAD_PAGE21    = 8,
433       // Offset within page of TLVP slot, scaled by r_length.
434       ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
435       // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
436       ARM64_RELOC_ADDEND              = 10,
437
438
439       // Constant values for the r_type field in an x86_64 architecture
440       // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
441       // structure
442       X86_64_RELOC_UNSIGNED        = 0,
443       X86_64_RELOC_SIGNED          = 1,
444       X86_64_RELOC_BRANCH          = 2,
445       X86_64_RELOC_GOT_LOAD        = 3,
446       X86_64_RELOC_GOT             = 4,
447       X86_64_RELOC_SUBTRACTOR      = 5,
448       X86_64_RELOC_SIGNED_1        = 6,
449       X86_64_RELOC_SIGNED_2        = 7,
450       X86_64_RELOC_SIGNED_4        = 8,
451       X86_64_RELOC_TLV             = 9
452     };
453
454     // Values for segment_command.initprot.
455     // From <mach/vm_prot.h>
456     enum {
457       VM_PROT_READ    = 0x1,
458       VM_PROT_WRITE   = 0x2,
459       VM_PROT_EXECUTE = 0x4
460     };
461
462
463     // Structs from <mach-o/loader.h>
464
465     struct mach_header {
466       uint32_t magic;
467       uint32_t cputype;
468       uint32_t cpusubtype;
469       uint32_t filetype;
470       uint32_t ncmds;
471       uint32_t sizeofcmds;
472       uint32_t flags;
473     };
474
475     struct mach_header_64 {
476       uint32_t magic;
477       uint32_t cputype;
478       uint32_t cpusubtype;
479       uint32_t filetype;
480       uint32_t ncmds;
481       uint32_t sizeofcmds;
482       uint32_t flags;
483       uint32_t reserved;
484     };
485
486     struct load_command {
487       uint32_t cmd;
488       uint32_t cmdsize;
489     };
490
491     struct segment_command {
492       uint32_t cmd;
493       uint32_t cmdsize;
494       char segname[16];
495       uint32_t vmaddr;
496       uint32_t vmsize;
497       uint32_t fileoff;
498       uint32_t filesize;
499       uint32_t maxprot;
500       uint32_t initprot;
501       uint32_t nsects;
502       uint32_t flags;
503     };
504
505     struct segment_command_64 {
506       uint32_t cmd;
507       uint32_t cmdsize;
508       char segname[16];
509       uint64_t vmaddr;
510       uint64_t vmsize;
511       uint64_t fileoff;
512       uint64_t filesize;
513       uint32_t maxprot;
514       uint32_t initprot;
515       uint32_t nsects;
516       uint32_t flags;
517     };
518
519     struct section {
520       char sectname[16];
521       char segname[16];
522       uint32_t addr;
523       uint32_t size;
524       uint32_t offset;
525       uint32_t align;
526       uint32_t reloff;
527       uint32_t nreloc;
528       uint32_t flags;
529       uint32_t reserved1;
530       uint32_t reserved2;
531     };
532
533     struct section_64 {
534       char sectname[16];
535       char segname[16];
536       uint64_t addr;
537       uint64_t size;
538       uint32_t offset;
539       uint32_t align;
540       uint32_t reloff;
541       uint32_t nreloc;
542       uint32_t flags;
543       uint32_t reserved1;
544       uint32_t reserved2;
545       uint32_t reserved3;
546     };
547
548     struct fvmlib {
549       uint32_t name;
550       uint32_t minor_version;
551       uint32_t header_addr;
552     };
553
554     struct fvmlib_command {
555       uint32_t  cmd;
556       uint32_t cmdsize;
557       struct fvmlib fvmlib;
558     };
559
560     struct dylib {
561       uint32_t name;
562       uint32_t timestamp;
563       uint32_t current_version;
564       uint32_t compatibility_version;
565     };
566
567     struct dylib_command {
568       uint32_t cmd;
569       uint32_t cmdsize;
570       struct dylib dylib;
571     };
572
573     struct sub_framework_command {
574       uint32_t cmd;
575       uint32_t cmdsize;
576       uint32_t umbrella;
577     };
578
579     struct sub_client_command {
580       uint32_t cmd;
581       uint32_t cmdsize;
582       uint32_t client;
583     };
584
585     struct sub_umbrella_command {
586       uint32_t cmd;
587       uint32_t cmdsize;
588       uint32_t sub_umbrella;
589     };
590
591     struct sub_library_command {
592       uint32_t cmd;
593       uint32_t cmdsize;
594       uint32_t sub_library;
595     };
596
597     struct prebound_dylib_command {
598       uint32_t cmd;
599       uint32_t cmdsize;
600       uint32_t name;
601       uint32_t nmodules;
602       uint32_t linked_modules;
603     };
604
605     struct dylinker_command {
606       uint32_t cmd;
607       uint32_t cmdsize;
608       uint32_t name;
609     };
610
611     struct thread_command {
612       uint32_t cmd;
613       uint32_t cmdsize;
614     };
615
616     struct routines_command {
617       uint32_t cmd;
618       uint32_t cmdsize;
619       uint32_t init_address;
620       uint32_t init_module;
621       uint32_t reserved1;
622       uint32_t reserved2;
623       uint32_t reserved3;
624       uint32_t reserved4;
625       uint32_t reserved5;
626       uint32_t reserved6;
627     };
628
629     struct routines_command_64 {
630       uint32_t cmd;
631       uint32_t cmdsize;
632       uint64_t init_address;
633       uint64_t init_module;
634       uint64_t reserved1;
635       uint64_t reserved2;
636       uint64_t reserved3;
637       uint64_t reserved4;
638       uint64_t reserved5;
639       uint64_t reserved6;
640     };
641
642     struct symtab_command {
643       uint32_t cmd;
644       uint32_t cmdsize;
645       uint32_t symoff;
646       uint32_t nsyms;
647       uint32_t stroff;
648       uint32_t strsize;
649     };
650
651     struct dysymtab_command {
652       uint32_t cmd;
653       uint32_t cmdsize;
654       uint32_t ilocalsym;
655       uint32_t nlocalsym;
656       uint32_t iextdefsym;
657       uint32_t nextdefsym;
658       uint32_t iundefsym;
659       uint32_t nundefsym;
660       uint32_t tocoff;
661       uint32_t ntoc;
662       uint32_t modtaboff;
663       uint32_t nmodtab;
664       uint32_t extrefsymoff;
665       uint32_t nextrefsyms;
666       uint32_t indirectsymoff;
667       uint32_t nindirectsyms;
668       uint32_t extreloff;
669       uint32_t nextrel;
670       uint32_t locreloff;
671       uint32_t nlocrel;
672     };
673
674     struct dylib_table_of_contents {
675       uint32_t symbol_index;
676       uint32_t module_index;
677     };
678
679     struct dylib_module {
680       uint32_t module_name;
681       uint32_t iextdefsym;
682       uint32_t nextdefsym;
683       uint32_t irefsym;
684       uint32_t nrefsym;
685       uint32_t ilocalsym;
686       uint32_t nlocalsym;
687       uint32_t iextrel;
688       uint32_t nextrel;
689       uint32_t iinit_iterm;
690       uint32_t ninit_nterm;
691       uint32_t objc_module_info_addr;
692       uint32_t objc_module_info_size;
693     };
694
695     struct dylib_module_64 {
696       uint32_t module_name;
697       uint32_t iextdefsym;
698       uint32_t nextdefsym;
699       uint32_t irefsym;
700       uint32_t nrefsym;
701       uint32_t ilocalsym;
702       uint32_t nlocalsym;
703       uint32_t iextrel;
704       uint32_t nextrel;
705       uint32_t iinit_iterm;
706       uint32_t ninit_nterm;
707       uint32_t objc_module_info_size;
708       uint64_t objc_module_info_addr;
709     };
710
711     struct dylib_reference {
712       uint32_t isym:24,
713                flags:8;
714     };
715
716
717     struct twolevel_hints_command {
718       uint32_t cmd;
719       uint32_t cmdsize;
720       uint32_t offset;
721       uint32_t nhints;
722     };
723
724     struct twolevel_hint {
725       uint32_t isub_image:8,
726                itoc:24;
727     };
728
729     struct prebind_cksum_command {
730       uint32_t cmd;
731       uint32_t cmdsize;
732       uint32_t cksum;
733     };
734
735     struct uuid_command {
736       uint32_t cmd;
737       uint32_t cmdsize;
738       uint8_t uuid[16];
739     };
740
741     struct rpath_command {
742       uint32_t cmd;
743       uint32_t cmdsize;
744       uint32_t path;
745     };
746
747     struct linkedit_data_command {
748       uint32_t cmd;
749       uint32_t cmdsize;
750       uint32_t dataoff;
751       uint32_t datasize;
752     };
753
754     struct data_in_code_entry {
755       uint32_t offset;
756       uint16_t length;
757       uint16_t kind;
758     };
759
760     struct source_version_command {
761       uint32_t cmd;
762       uint32_t cmdsize;
763       uint64_t version;
764     };
765
766     struct encryption_info_command {
767       uint32_t cmd;
768       uint32_t cmdsize;
769       uint32_t cryptoff;
770       uint32_t cryptsize;
771       uint32_t cryptid;
772     };
773
774     struct version_min_command {
775       uint32_t cmd;       // LC_VERSION_MIN_MACOSX or
776                           // LC_VERSION_MIN_IPHONEOS
777       uint32_t cmdsize;   // sizeof(struct version_min_command)
778       uint32_t version;   // X.Y.Z is encoded in nibbles xxxx.yy.zz
779       uint32_t reserved;
780     };
781
782     struct dyld_info_command {
783       uint32_t cmd;
784       uint32_t cmdsize;
785       uint32_t rebase_off;
786       uint32_t rebase_size;
787       uint32_t bind_off;
788       uint32_t bind_size;
789       uint32_t weak_bind_off;
790       uint32_t weak_bind_size;
791       uint32_t lazy_bind_off;
792       uint32_t lazy_bind_size;
793       uint32_t export_off;
794       uint32_t export_size;
795     };
796
797     struct linker_options_command {
798       uint32_t cmd;
799       uint32_t cmdsize;
800       uint32_t count;
801     };
802
803     struct symseg_command {
804       uint32_t cmd;
805       uint32_t cmdsize;
806       uint32_t offset;
807       uint32_t size;
808     };
809
810     struct ident_command {
811       uint32_t cmd;
812       uint32_t cmdsize;
813     };
814
815     struct fvmfile_command {
816       uint32_t cmd;
817       uint32_t cmdsize;
818       uint32_t name;
819       uint32_t header_addr;
820     };
821
822     struct tlv_descriptor_32 {
823       uint32_t thunk;
824       uint32_t key;
825       uint32_t offset;
826     };
827
828     struct tlv_descriptor_64 {
829       uint64_t thunk;
830       uint64_t key;
831       uint64_t offset;
832     };
833
834     struct tlv_descriptor {
835       uintptr_t thunk;
836       uintptr_t key;
837       uintptr_t offset;
838     };
839
840     struct entry_point_command {
841       uint32_t cmd;
842       uint32_t cmdsize;
843       uint64_t entryoff;
844       uint64_t stacksize;
845     };
846
847
848     // Structs from <mach-o/fat.h>
849     struct fat_header {
850       uint32_t magic;
851       uint32_t nfat_arch;
852     };
853
854     struct fat_arch {
855       uint32_t cputype;
856       uint32_t cpusubtype;
857       uint32_t offset;
858       uint32_t size;
859       uint32_t align;
860     };
861
862     // Structs from <mach-o/reloc.h>
863     struct relocation_info {
864       int32_t r_address;
865       uint32_t r_symbolnum:24,
866                r_pcrel:1,
867                r_length:2,
868                r_extern:1,
869                r_type:4;
870     };
871
872     struct scattered_relocation_info {
873 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
874       uint32_t r_scattered:1,
875                r_pcrel:1,
876                r_length:2,
877                r_type:4,
878                r_address:24;
879 #else
880       uint32_t r_address:24,
881                r_type:4,
882                r_length:2,
883                r_pcrel:1,
884                r_scattered:1;
885 #endif
886       int32_t r_value;
887     };
888
889     // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
890     struct any_relocation_info {
891       uint32_t r_word0, r_word1;
892     };
893
894     // Structs from <mach-o/nlist.h>
895     struct nlist {
896       uint32_t n_strx;
897       uint8_t n_type;
898       uint8_t n_sect;
899       int16_t n_desc;
900       uint32_t n_value;
901     };
902
903     struct nlist_64 {
904       uint32_t n_strx;
905       uint8_t n_type;
906       uint8_t n_sect;
907       uint16_t n_desc;
908       uint64_t n_value;
909     };
910
911     // Get/Set functions from <mach-o/nlist.h>
912
913     static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
914       return (((n_desc) >> 8u) & 0xffu);
915     }
916
917     static inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
918       n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
919     }
920
921     static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc) {
922       return (n_desc >> 8u) & 0x0fu;
923     }
924
925     static inline void SET_COMM_ALIGN (uint16_t &n_desc, uint8_t align) {
926       n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
927     }
928
929     // Enums from <mach/machine.h>
930     enum : uint32_t {
931       // Capability bits used in the definition of cpu_type.
932       CPU_ARCH_MASK  = 0xff000000,   // Mask for architecture bits
933       CPU_ARCH_ABI64 = 0x01000000    // 64 bit ABI
934     };
935
936     // Constants for the cputype field.
937     enum CPUType {
938       CPU_TYPE_ANY       = -1,
939       CPU_TYPE_X86       = 7,
940       CPU_TYPE_I386      = CPU_TYPE_X86,
941       CPU_TYPE_X86_64    = CPU_TYPE_X86 | CPU_ARCH_ABI64,
942    /* CPU_TYPE_MIPS      = 8, */
943       CPU_TYPE_MC98000   = 10, // Old Motorola PowerPC
944       CPU_TYPE_ARM       = 12,
945       CPU_TYPE_ARM64     = CPU_TYPE_ARM | CPU_ARCH_ABI64,
946       CPU_TYPE_SPARC     = 14,
947       CPU_TYPE_POWERPC   = 18,
948       CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
949     };
950
951     enum : uint32_t {
952       // Capability bits used in the definition of cpusubtype.
953       CPU_SUB_TYPE_MASK  = 0xff000000,   // Mask for architecture bits
954       CPU_SUB_TYPE_LIB64 = 0x80000000,   // 64 bit libraries
955
956       // Special CPU subtype constants.
957       CPU_SUBTYPE_MULTIPLE = ~0u
958     };
959
960     // Constants for the cpusubtype field.
961     enum CPUSubTypeX86 {
962       CPU_SUBTYPE_I386_ALL       = 3,
963       CPU_SUBTYPE_386            = 3,
964       CPU_SUBTYPE_486            = 4,
965       CPU_SUBTYPE_486SX          = 0x84,
966       CPU_SUBTYPE_586            = 5,
967       CPU_SUBTYPE_PENT           = CPU_SUBTYPE_586,
968       CPU_SUBTYPE_PENTPRO        = 0x16,
969       CPU_SUBTYPE_PENTII_M3      = 0x36,
970       CPU_SUBTYPE_PENTII_M5      = 0x56,
971       CPU_SUBTYPE_CELERON        = 0x67,
972       CPU_SUBTYPE_CELERON_MOBILE = 0x77,
973       CPU_SUBTYPE_PENTIUM_3      = 0x08,
974       CPU_SUBTYPE_PENTIUM_3_M    = 0x18,
975       CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
976       CPU_SUBTYPE_PENTIUM_M      = 0x09,
977       CPU_SUBTYPE_PENTIUM_4      = 0x0a,
978       CPU_SUBTYPE_PENTIUM_4_M    = 0x1a,
979       CPU_SUBTYPE_ITANIUM        = 0x0b,
980       CPU_SUBTYPE_ITANIUM_2      = 0x1b,
981       CPU_SUBTYPE_XEON           = 0x0c,
982       CPU_SUBTYPE_XEON_MP        = 0x1c,
983
984       CPU_SUBTYPE_X86_ALL     = 3,
985       CPU_SUBTYPE_X86_64_ALL  = 3,
986       CPU_SUBTYPE_X86_ARCH1   = 4,
987       CPU_SUBTYPE_X86_64_H    = 8
988     };
989     static inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
990       return Family | (Model << 4);
991     }
992     static inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
993       return ((int)ST) & 0x0f;
994     }
995     static inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) {
996       return ((int)ST) >> 4;
997     }
998     enum {
999       CPU_SUBTYPE_INTEL_FAMILY_MAX = 15,
1000       CPU_SUBTYPE_INTEL_MODEL_ALL  = 0
1001     };
1002
1003     enum CPUSubTypeARM {
1004       CPU_SUBTYPE_ARM_ALL     = 0,
1005       CPU_SUBTYPE_ARM_V4T     = 5,
1006       CPU_SUBTYPE_ARM_V6      = 6,
1007       CPU_SUBTYPE_ARM_V5      = 7,
1008       CPU_SUBTYPE_ARM_V5TEJ   = 7,
1009       CPU_SUBTYPE_ARM_XSCALE  = 8,
1010       CPU_SUBTYPE_ARM_V7      = 9,
1011       //  unused  ARM_V7F     = 10,
1012       CPU_SUBTYPE_ARM_V7S     = 11,
1013       CPU_SUBTYPE_ARM_V7K     = 12,
1014       CPU_SUBTYPE_ARM_V6M     = 14,
1015       CPU_SUBTYPE_ARM_V7M     = 15,
1016       CPU_SUBTYPE_ARM_V7EM    = 16
1017     };
1018
1019     enum CPUSubTypeARM64 {
1020       CPU_SUBTYPE_ARM64_ALL   = 0
1021     };
1022
1023     enum CPUSubTypeSPARC {
1024       CPU_SUBTYPE_SPARC_ALL   = 0
1025     };
1026
1027     enum CPUSubTypePowerPC {
1028       CPU_SUBTYPE_POWERPC_ALL   = 0,
1029       CPU_SUBTYPE_POWERPC_601   = 1,
1030       CPU_SUBTYPE_POWERPC_602   = 2,
1031       CPU_SUBTYPE_POWERPC_603   = 3,
1032       CPU_SUBTYPE_POWERPC_603e  = 4,
1033       CPU_SUBTYPE_POWERPC_603ev = 5,
1034       CPU_SUBTYPE_POWERPC_604   = 6,
1035       CPU_SUBTYPE_POWERPC_604e  = 7,
1036       CPU_SUBTYPE_POWERPC_620   = 8,
1037       CPU_SUBTYPE_POWERPC_750   = 9,
1038       CPU_SUBTYPE_POWERPC_7400  = 10,
1039       CPU_SUBTYPE_POWERPC_7450  = 11,
1040       CPU_SUBTYPE_POWERPC_970   = 100,
1041
1042       CPU_SUBTYPE_MC980000_ALL  = CPU_SUBTYPE_POWERPC_ALL,
1043       CPU_SUBTYPE_MC98601       = CPU_SUBTYPE_POWERPC_601
1044     };
1045   } // end namespace MachO
1046 } // end namespace llvm
1047
1048 #endif