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