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