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