Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===-- llvm/BinaryFormat/MachO.h - The MachO file format -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines manifest constants for the MachO object file format.
  10. //
  11. //===----------------------------------------------------------------------===//
  12.  
  13. #ifndef LLVM_BINARYFORMAT_MACHO_H
  14. #define LLVM_BINARYFORMAT_MACHO_H
  15.  
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Support/SwapByteOrder.h"
  20.  
  21. namespace llvm {
  22.  
  23. class Triple;
  24.  
  25. namespace MachO {
  26. // Enums from <mach-o/loader.h>
  27. enum : uint32_t {
  28.   // Constants for the "magic" field in llvm::MachO::mach_header and
  29.   // llvm::MachO::mach_header_64
  30.   MH_MAGIC = 0xFEEDFACEu,
  31.   MH_CIGAM = 0xCEFAEDFEu,
  32.   MH_MAGIC_64 = 0xFEEDFACFu,
  33.   MH_CIGAM_64 = 0xCFFAEDFEu,
  34.   FAT_MAGIC = 0xCAFEBABEu,
  35.   FAT_CIGAM = 0xBEBAFECAu,
  36.   FAT_MAGIC_64 = 0xCAFEBABFu,
  37.   FAT_CIGAM_64 = 0xBFBAFECAu
  38. };
  39.  
  40. enum HeaderFileType {
  41.   // Constants for the "filetype" field in llvm::MachO::mach_header and
  42.   // llvm::MachO::mach_header_64
  43.   MH_OBJECT = 0x1u,
  44.   MH_EXECUTE = 0x2u,
  45.   MH_FVMLIB = 0x3u,
  46.   MH_CORE = 0x4u,
  47.   MH_PRELOAD = 0x5u,
  48.   MH_DYLIB = 0x6u,
  49.   MH_DYLINKER = 0x7u,
  50.   MH_BUNDLE = 0x8u,
  51.   MH_DYLIB_STUB = 0x9u,
  52.   MH_DSYM = 0xAu,
  53.   MH_KEXT_BUNDLE = 0xBu,
  54.   MH_FILESET = 0xCu,
  55. };
  56.  
  57. enum {
  58.   // Constant bits for the "flags" field in llvm::MachO::mach_header and
  59.   // llvm::MachO::mach_header_64
  60.   MH_NOUNDEFS = 0x00000001u,
  61.   MH_INCRLINK = 0x00000002u,
  62.   MH_DYLDLINK = 0x00000004u,
  63.   MH_BINDATLOAD = 0x00000008u,
  64.   MH_PREBOUND = 0x00000010u,
  65.   MH_SPLIT_SEGS = 0x00000020u,
  66.   MH_LAZY_INIT = 0x00000040u,
  67.   MH_TWOLEVEL = 0x00000080u,
  68.   MH_FORCE_FLAT = 0x00000100u,
  69.   MH_NOMULTIDEFS = 0x00000200u,
  70.   MH_NOFIXPREBINDING = 0x00000400u,
  71.   MH_PREBINDABLE = 0x00000800u,
  72.   MH_ALLMODSBOUND = 0x00001000u,
  73.   MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
  74.   MH_CANONICAL = 0x00004000u,
  75.   MH_WEAK_DEFINES = 0x00008000u,
  76.   MH_BINDS_TO_WEAK = 0x00010000u,
  77.   MH_ALLOW_STACK_EXECUTION = 0x00020000u,
  78.   MH_ROOT_SAFE = 0x00040000u,
  79.   MH_SETUID_SAFE = 0x00080000u,
  80.   MH_NO_REEXPORTED_DYLIBS = 0x00100000u,
  81.   MH_PIE = 0x00200000u,
  82.   MH_DEAD_STRIPPABLE_DYLIB = 0x00400000u,
  83.   MH_HAS_TLV_DESCRIPTORS = 0x00800000u,
  84.   MH_NO_HEAP_EXECUTION = 0x01000000u,
  85.   MH_APP_EXTENSION_SAFE = 0x02000000u,
  86.   MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000u,
  87.   MH_SIM_SUPPORT = 0x08000000u,
  88.   MH_DYLIB_IN_CACHE = 0x80000000u,
  89. };
  90.  
  91. enum : uint32_t {
  92.   // Flags for the "cmd" field in llvm::MachO::load_command
  93.   LC_REQ_DYLD = 0x80000000u
  94. };
  95.  
  96. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) LCName = LCValue,
  97.  
  98. enum LoadCommandType : uint32_t {
  99. #include "llvm/BinaryFormat/MachO.def"
  100. };
  101.  
  102. #undef HANDLE_LOAD_COMMAND
  103.  
  104. enum : uint32_t {
  105.   // Constant bits for the "flags" field in llvm::MachO::segment_command
  106.   SG_HIGHVM = 0x1u,
  107.   SG_FVMLIB = 0x2u,
  108.   SG_NORELOC = 0x4u,
  109.   SG_PROTECTED_VERSION_1 = 0x8u,
  110.   SG_READ_ONLY = 0x10u,
  111.  
  112.   // Constant masks for the "flags" field in llvm::MachO::section and
  113.   // llvm::MachO::section_64
  114.   SECTION_TYPE = 0x000000ffu,           // SECTION_TYPE
  115.   SECTION_ATTRIBUTES = 0xffffff00u,     // SECTION_ATTRIBUTES
  116.   SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
  117.   SECTION_ATTRIBUTES_SYS = 0x00ffff00u  // SECTION_ATTRIBUTES_SYS
  118. };
  119.  
  120. /// These are the section type and attributes fields.  A MachO section can
  121. /// have only one Type, but can have any of the attributes specified.
  122. enum SectionType : uint32_t {
  123.   // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
  124.   // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
  125.  
  126.   /// S_REGULAR - Regular section.
  127.   S_REGULAR = 0x00u,
  128.   /// S_ZEROFILL - Zero fill on demand section.
  129.   S_ZEROFILL = 0x01u,
  130.   /// S_CSTRING_LITERALS - Section with literal C strings.
  131.   S_CSTRING_LITERALS = 0x02u,
  132.   /// S_4BYTE_LITERALS - Section with 4 byte literals.
  133.   S_4BYTE_LITERALS = 0x03u,
  134.   /// S_8BYTE_LITERALS - Section with 8 byte literals.
  135.   S_8BYTE_LITERALS = 0x04u,
  136.   /// S_LITERAL_POINTERS - Section with pointers to literals.
  137.   S_LITERAL_POINTERS = 0x05u,
  138.   /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
  139.   S_NON_LAZY_SYMBOL_POINTERS = 0x06u,
  140.   /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
  141.   S_LAZY_SYMBOL_POINTERS = 0x07u,
  142.   /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
  143.   /// the Reserved2 field.
  144.   S_SYMBOL_STUBS = 0x08u,
  145.   /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
  146.   /// initialization.
  147.   S_MOD_INIT_FUNC_POINTERS = 0x09u,
  148.   /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
  149.   /// termination.
  150.   S_MOD_TERM_FUNC_POINTERS = 0x0au,
  151.   /// S_COALESCED - Section contains symbols that are to be coalesced.
  152.   S_COALESCED = 0x0bu,
  153.   /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
  154.   /// gigabytes).
  155.   S_GB_ZEROFILL = 0x0cu,
  156.   /// S_INTERPOSING - Section with only pairs of function pointers for
  157.   /// interposing.
  158.   S_INTERPOSING = 0x0du,
  159.   /// S_16BYTE_LITERALS - Section with only 16 byte literals.
  160.   S_16BYTE_LITERALS = 0x0eu,
  161.   /// S_DTRACE_DOF - Section contains DTrace Object Format.
  162.   S_DTRACE_DOF = 0x0fu,
  163.   /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
  164.   /// lazy loaded dylibs.
  165.   S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
  166.   /// S_THREAD_LOCAL_REGULAR - Thread local data section.
  167.   S_THREAD_LOCAL_REGULAR = 0x11u,
  168.   /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
  169.   S_THREAD_LOCAL_ZEROFILL = 0x12u,
  170.   /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
  171.   /// structure data.
  172.   S_THREAD_LOCAL_VARIABLES = 0x13u,
  173.   /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
  174.   /// local structures.
  175.   S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
  176.   /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
  177.   /// variable initialization pointers to functions.
  178.   S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
  179.   /// S_INIT_FUNC_OFFSETS - Section with 32-bit offsets to initializer
  180.   /// functions.
  181.   S_INIT_FUNC_OFFSETS = 0x16u,
  182.  
  183.   LAST_KNOWN_SECTION_TYPE = S_INIT_FUNC_OFFSETS
  184. };
  185.  
  186. enum : uint32_t {
  187.   // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
  188.   // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
  189.  
  190.   /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
  191.   /// instructions.
  192.   S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
  193.   /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
  194.   /// in a ranlib table of contents.
  195.   S_ATTR_NO_TOC = 0x40000000u,
  196.   /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
  197.   /// in files with the MY_DYLDLINK flag.
  198.   S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
  199.   /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
  200.   S_ATTR_NO_DEAD_STRIP = 0x10000000u,
  201.   /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
  202.   S_ATTR_LIVE_SUPPORT = 0x08000000u,
  203.   /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
  204.   /// dyld.
  205.   S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
  206.   /// S_ATTR_DEBUG - A debug section.
  207.   S_ATTR_DEBUG = 0x02000000u,
  208.  
  209.   // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
  210.   // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
  211.  
  212.   /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
  213.   S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
  214.   /// S_ATTR_EXT_RELOC - Section has external relocation entries.
  215.   S_ATTR_EXT_RELOC = 0x00000200u,
  216.   /// S_ATTR_LOC_RELOC - Section has local relocation entries.
  217.   S_ATTR_LOC_RELOC = 0x00000100u,
  218.  
  219.   // Constant masks for the value of an indirect symbol in an indirect
  220.   // symbol table
  221.   INDIRECT_SYMBOL_LOCAL = 0x80000000u,
  222.   INDIRECT_SYMBOL_ABS = 0x40000000u
  223. };
  224.  
  225. enum DataRegionType {
  226.   // Constants for the "kind" field in a data_in_code_entry structure
  227.   DICE_KIND_DATA = 1u,
  228.   DICE_KIND_JUMP_TABLE8 = 2u,
  229.   DICE_KIND_JUMP_TABLE16 = 3u,
  230.   DICE_KIND_JUMP_TABLE32 = 4u,
  231.   DICE_KIND_ABS_JUMP_TABLE32 = 5u
  232. };
  233.  
  234. enum RebaseType {
  235.   REBASE_TYPE_POINTER = 1u,
  236.   REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
  237.   REBASE_TYPE_TEXT_PCREL32 = 3u
  238. };
  239.  
  240. enum { REBASE_OPCODE_MASK = 0xF0u, REBASE_IMMEDIATE_MASK = 0x0Fu };
  241.  
  242. enum RebaseOpcode {
  243.   REBASE_OPCODE_DONE = 0x00u,
  244.   REBASE_OPCODE_SET_TYPE_IMM = 0x10u,
  245.   REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
  246.   REBASE_OPCODE_ADD_ADDR_ULEB = 0x30u,
  247.   REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40u,
  248.   REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50u,
  249.   REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60u,
  250.   REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70u,
  251.   REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
  252. };
  253.  
  254. enum BindType {
  255.   BIND_TYPE_POINTER = 1u,
  256.   BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
  257.   BIND_TYPE_TEXT_PCREL32 = 3u
  258. };
  259.  
  260. enum BindSpecialDylib {
  261.   BIND_SPECIAL_DYLIB_SELF = 0,
  262.   BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
  263.   BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2,
  264.   BIND_SPECIAL_DYLIB_WEAK_LOOKUP = -3
  265. };
  266.  
  267. enum {
  268.   BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1u,
  269.   BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
  270.  
  271.   BIND_OPCODE_MASK = 0xF0u,
  272.   BIND_IMMEDIATE_MASK = 0x0Fu
  273. };
  274.  
  275. enum BindOpcode {
  276.   BIND_OPCODE_DONE = 0x00u,
  277.   BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10u,
  278.   BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20u,
  279.   BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30u,
  280.   BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
  281.   BIND_OPCODE_SET_TYPE_IMM = 0x50u,
  282.   BIND_OPCODE_SET_ADDEND_SLEB = 0x60u,
  283.   BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
  284.   BIND_OPCODE_ADD_ADDR_ULEB = 0x80u,
  285.   BIND_OPCODE_DO_BIND = 0x90u,
  286.   BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0u,
  287.   BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
  288.   BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
  289. };
  290.  
  291. enum {
  292.   EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03u,
  293.   EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04u,
  294.   EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08u,
  295.   EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10u
  296. };
  297.  
  298. enum ExportSymbolKind {
  299.   EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00u,
  300.   EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01u,
  301.   EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE = 0x02u
  302. };
  303.  
  304. enum {
  305.   // Constant masks for the "n_type" field in llvm::MachO::nlist and
  306.   // llvm::MachO::nlist_64
  307.   N_STAB = 0xe0,
  308.   N_PEXT = 0x10,
  309.   N_TYPE = 0x0e,
  310.   N_EXT = 0x01
  311. };
  312.  
  313. enum NListType : uint8_t {
  314.   // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
  315.   // llvm::MachO::nlist_64
  316.   N_UNDF = 0x0u,
  317.   N_ABS = 0x2u,
  318.   N_SECT = 0xeu,
  319.   N_PBUD = 0xcu,
  320.   N_INDR = 0xau
  321. };
  322.  
  323. enum SectionOrdinal {
  324.   // Constants for the "n_sect" field in llvm::MachO::nlist and
  325.   // llvm::MachO::nlist_64
  326.   NO_SECT = 0u,
  327.   MAX_SECT = 0xffu
  328. };
  329.  
  330. enum {
  331.   // Constant masks for the "n_desc" field in llvm::MachO::nlist and
  332.   // llvm::MachO::nlist_64
  333.   // The low 3 bits are the for the REFERENCE_TYPE.
  334.   REFERENCE_TYPE = 0x7,
  335.   REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
  336.   REFERENCE_FLAG_UNDEFINED_LAZY = 1,
  337.   REFERENCE_FLAG_DEFINED = 2,
  338.   REFERENCE_FLAG_PRIVATE_DEFINED = 3,
  339.   REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
  340.   REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
  341.   // Flag bits (some overlap with the library ordinal bits).
  342.   N_ARM_THUMB_DEF = 0x0008u,
  343.   REFERENCED_DYNAMICALLY = 0x0010u,
  344.   N_NO_DEAD_STRIP = 0x0020u,
  345.   N_WEAK_REF = 0x0040u,
  346.   N_WEAK_DEF = 0x0080u,
  347.   N_SYMBOL_RESOLVER = 0x0100u,
  348.   N_ALT_ENTRY = 0x0200u,
  349.   N_COLD_FUNC = 0x0400u,
  350.   // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
  351.   // as these are in the top 8 bits.
  352.   SELF_LIBRARY_ORDINAL = 0x0,
  353.   MAX_LIBRARY_ORDINAL = 0xfd,
  354.   DYNAMIC_LOOKUP_ORDINAL = 0xfe,
  355.   EXECUTABLE_ORDINAL = 0xff
  356. };
  357.  
  358. enum StabType {
  359.   // Constant values for the "n_type" field in llvm::MachO::nlist and
  360.   // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
  361.   N_GSYM = 0x20u,
  362.   N_FNAME = 0x22u,
  363.   N_FUN = 0x24u,
  364.   N_STSYM = 0x26u,
  365.   N_LCSYM = 0x28u,
  366.   N_BNSYM = 0x2Eu,
  367.   N_PC = 0x30u,
  368.   N_AST = 0x32u,
  369.   N_OPT = 0x3Cu,
  370.   N_RSYM = 0x40u,
  371.   N_SLINE = 0x44u,
  372.   N_ENSYM = 0x4Eu,
  373.   N_SSYM = 0x60u,
  374.   N_SO = 0x64u,
  375.   N_OSO = 0x66u,
  376.   N_LSYM = 0x80u,
  377.   N_BINCL = 0x82u,
  378.   N_SOL = 0x84u,
  379.   N_PARAMS = 0x86u,
  380.   N_VERSION = 0x88u,
  381.   N_OLEVEL = 0x8Au,
  382.   N_PSYM = 0xA0u,
  383.   N_EINCL = 0xA2u,
  384.   N_ENTRY = 0xA4u,
  385.   N_LBRAC = 0xC0u,
  386.   N_EXCL = 0xC2u,
  387.   N_RBRAC = 0xE0u,
  388.   N_BCOMM = 0xE2u,
  389.   N_ECOMM = 0xE4u,
  390.   N_ECOML = 0xE8u,
  391.   N_LENG = 0xFEu
  392. };
  393.  
  394. enum : uint32_t {
  395.   // Constant values for the r_symbolnum field in an
  396.   // llvm::MachO::relocation_info structure when r_extern is 0.
  397.   R_ABS = 0,
  398.  
  399.   // Constant bits for the r_address field in an
  400.   // llvm::MachO::relocation_info structure.
  401.   R_SCATTERED = 0x80000000
  402. };
  403.  
  404. enum RelocationInfoType {
  405.   // Constant values for the r_type field in an
  406.   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  407.   // structure.
  408.   GENERIC_RELOC_INVALID = 0xff,
  409.   GENERIC_RELOC_VANILLA = 0,
  410.   GENERIC_RELOC_PAIR = 1,
  411.   GENERIC_RELOC_SECTDIFF = 2,
  412.   GENERIC_RELOC_PB_LA_PTR = 3,
  413.   GENERIC_RELOC_LOCAL_SECTDIFF = 4,
  414.   GENERIC_RELOC_TLV = 5,
  415.  
  416.   // Constant values for the r_type field in a PowerPC architecture
  417.   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  418.   // structure.
  419.   PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  420.   PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
  421.   PPC_RELOC_BR14 = 2,
  422.   PPC_RELOC_BR24 = 3,
  423.   PPC_RELOC_HI16 = 4,
  424.   PPC_RELOC_LO16 = 5,
  425.   PPC_RELOC_HA16 = 6,
  426.   PPC_RELOC_LO14 = 7,
  427.   PPC_RELOC_SECTDIFF = 8,
  428.   PPC_RELOC_PB_LA_PTR = 9,
  429.   PPC_RELOC_HI16_SECTDIFF = 10,
  430.   PPC_RELOC_LO16_SECTDIFF = 11,
  431.   PPC_RELOC_HA16_SECTDIFF = 12,
  432.   PPC_RELOC_JBSR = 13,
  433.   PPC_RELOC_LO14_SECTDIFF = 14,
  434.   PPC_RELOC_LOCAL_SECTDIFF = 15,
  435.  
  436.   // Constant values for the r_type field in an ARM architecture
  437.   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  438.   // structure.
  439.   ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  440.   ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
  441.   ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
  442.   ARM_RELOC_LOCAL_SECTDIFF = 3,
  443.   ARM_RELOC_PB_LA_PTR = 4,
  444.   ARM_RELOC_BR24 = 5,
  445.   ARM_THUMB_RELOC_BR22 = 6,
  446.   ARM_THUMB_32BIT_BRANCH = 7, // obsolete
  447.   ARM_RELOC_HALF = 8,
  448.   ARM_RELOC_HALF_SECTDIFF = 9,
  449.  
  450.   // Constant values for the r_type field in an ARM64 architecture
  451.   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  452.   // structure.
  453.  
  454.   // For pointers.
  455.   ARM64_RELOC_UNSIGNED = 0,
  456.   // Must be followed by an ARM64_RELOC_UNSIGNED
  457.   ARM64_RELOC_SUBTRACTOR = 1,
  458.   // A B/BL instruction with 26-bit displacement.
  459.   ARM64_RELOC_BRANCH26 = 2,
  460.   // PC-rel distance to page of target.
  461.   ARM64_RELOC_PAGE21 = 3,
  462.   // Offset within page, scaled by r_length.
  463.   ARM64_RELOC_PAGEOFF12 = 4,
  464.   // PC-rel distance to page of GOT slot.
  465.   ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
  466.   // Offset within page of GOT slot, scaled by r_length.
  467.   ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
  468.   // For pointers to GOT slots.
  469.   ARM64_RELOC_POINTER_TO_GOT = 7,
  470.   // PC-rel distance to page of TLVP slot.
  471.   ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
  472.   // Offset within page of TLVP slot, scaled by r_length.
  473.   ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
  474.   // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
  475.   ARM64_RELOC_ADDEND = 10,
  476.  
  477.   // Constant values for the r_type field in an x86_64 architecture
  478.   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  479.   // structure
  480.   X86_64_RELOC_UNSIGNED = 0,
  481.   X86_64_RELOC_SIGNED = 1,
  482.   X86_64_RELOC_BRANCH = 2,
  483.   X86_64_RELOC_GOT_LOAD = 3,
  484.   X86_64_RELOC_GOT = 4,
  485.   X86_64_RELOC_SUBTRACTOR = 5,
  486.   X86_64_RELOC_SIGNED_1 = 6,
  487.   X86_64_RELOC_SIGNED_2 = 7,
  488.   X86_64_RELOC_SIGNED_4 = 8,
  489.   X86_64_RELOC_TLV = 9
  490. };
  491.  
  492. // Values for segment_command.initprot.
  493. // From <mach/vm_prot.h>
  494. enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
  495.  
  496. // Values for platform field in build_version_command.
  497. enum PlatformType {
  498.   PLATFORM_UNKNOWN = 0,
  499.   PLATFORM_MACOS = 1,
  500.   PLATFORM_IOS = 2,
  501.   PLATFORM_TVOS = 3,
  502.   PLATFORM_WATCHOS = 4,
  503.   PLATFORM_BRIDGEOS = 5,
  504.   PLATFORM_MACCATALYST = 6,
  505.   PLATFORM_IOSSIMULATOR = 7,
  506.   PLATFORM_TVOSSIMULATOR = 8,
  507.   PLATFORM_WATCHOSSIMULATOR = 9,
  508.   PLATFORM_DRIVERKIT = 10,
  509. };
  510.  
  511. // Values for tools enum in build_tool_version.
  512. enum { TOOL_CLANG = 1, TOOL_SWIFT = 2, TOOL_LD = 3 };
  513.  
  514. // Structs from <mach-o/loader.h>
  515.  
  516. struct mach_header {
  517.   uint32_t magic;
  518.   uint32_t cputype;
  519.   uint32_t cpusubtype;
  520.   uint32_t filetype;
  521.   uint32_t ncmds;
  522.   uint32_t sizeofcmds;
  523.   uint32_t flags;
  524. };
  525.  
  526. struct mach_header_64 {
  527.   uint32_t magic;
  528.   uint32_t cputype;
  529.   uint32_t cpusubtype;
  530.   uint32_t filetype;
  531.   uint32_t ncmds;
  532.   uint32_t sizeofcmds;
  533.   uint32_t flags;
  534.   uint32_t reserved;
  535. };
  536.  
  537. struct load_command {
  538.   uint32_t cmd;
  539.   uint32_t cmdsize;
  540. };
  541.  
  542. struct segment_command {
  543.   uint32_t cmd;
  544.   uint32_t cmdsize;
  545.   char segname[16];
  546.   uint32_t vmaddr;
  547.   uint32_t vmsize;
  548.   uint32_t fileoff;
  549.   uint32_t filesize;
  550.   uint32_t maxprot;
  551.   uint32_t initprot;
  552.   uint32_t nsects;
  553.   uint32_t flags;
  554. };
  555.  
  556. struct segment_command_64 {
  557.   uint32_t cmd;
  558.   uint32_t cmdsize;
  559.   char segname[16];
  560.   uint64_t vmaddr;
  561.   uint64_t vmsize;
  562.   uint64_t fileoff;
  563.   uint64_t filesize;
  564.   uint32_t maxprot;
  565.   uint32_t initprot;
  566.   uint32_t nsects;
  567.   uint32_t flags;
  568. };
  569.  
  570. struct section {
  571.   char sectname[16];
  572.   char segname[16];
  573.   uint32_t addr;
  574.   uint32_t size;
  575.   uint32_t offset;
  576.   uint32_t align;
  577.   uint32_t reloff;
  578.   uint32_t nreloc;
  579.   uint32_t flags;
  580.   uint32_t reserved1;
  581.   uint32_t reserved2;
  582. };
  583.  
  584. struct section_64 {
  585.   char sectname[16];
  586.   char segname[16];
  587.   uint64_t addr;
  588.   uint64_t size;
  589.   uint32_t offset;
  590.   uint32_t align;
  591.   uint32_t reloff;
  592.   uint32_t nreloc;
  593.   uint32_t flags;
  594.   uint32_t reserved1;
  595.   uint32_t reserved2;
  596.   uint32_t reserved3;
  597. };
  598.  
  599. inline bool isVirtualSection(uint8_t type) {
  600.   return (type == MachO::S_ZEROFILL || type == MachO::S_GB_ZEROFILL ||
  601.           type == MachO::S_THREAD_LOCAL_ZEROFILL);
  602. }
  603.  
  604. struct fvmlib {
  605.   uint32_t name;
  606.   uint32_t minor_version;
  607.   uint32_t header_addr;
  608. };
  609.  
  610. // The fvmlib_command is obsolete and no longer supported.
  611. struct fvmlib_command {
  612.   uint32_t cmd;
  613.   uint32_t cmdsize;
  614.   struct fvmlib fvmlib;
  615. };
  616.  
  617. struct dylib {
  618.   uint32_t name;
  619.   uint32_t timestamp;
  620.   uint32_t current_version;
  621.   uint32_t compatibility_version;
  622. };
  623.  
  624. struct dylib_command {
  625.   uint32_t cmd;
  626.   uint32_t cmdsize;
  627.   struct dylib dylib;
  628. };
  629.  
  630. struct sub_framework_command {
  631.   uint32_t cmd;
  632.   uint32_t cmdsize;
  633.   uint32_t umbrella;
  634. };
  635.  
  636. struct sub_client_command {
  637.   uint32_t cmd;
  638.   uint32_t cmdsize;
  639.   uint32_t client;
  640. };
  641.  
  642. struct sub_umbrella_command {
  643.   uint32_t cmd;
  644.   uint32_t cmdsize;
  645.   uint32_t sub_umbrella;
  646. };
  647.  
  648. struct sub_library_command {
  649.   uint32_t cmd;
  650.   uint32_t cmdsize;
  651.   uint32_t sub_library;
  652. };
  653.  
  654. // The prebound_dylib_command is obsolete and no longer supported.
  655. struct prebound_dylib_command {
  656.   uint32_t cmd;
  657.   uint32_t cmdsize;
  658.   uint32_t name;
  659.   uint32_t nmodules;
  660.   uint32_t linked_modules;
  661. };
  662.  
  663. struct dylinker_command {
  664.   uint32_t cmd;
  665.   uint32_t cmdsize;
  666.   uint32_t name;
  667. };
  668.  
  669. struct thread_command {
  670.   uint32_t cmd;
  671.   uint32_t cmdsize;
  672. };
  673.  
  674. struct routines_command {
  675.   uint32_t cmd;
  676.   uint32_t cmdsize;
  677.   uint32_t init_address;
  678.   uint32_t init_module;
  679.   uint32_t reserved1;
  680.   uint32_t reserved2;
  681.   uint32_t reserved3;
  682.   uint32_t reserved4;
  683.   uint32_t reserved5;
  684.   uint32_t reserved6;
  685. };
  686.  
  687. struct routines_command_64 {
  688.   uint32_t cmd;
  689.   uint32_t cmdsize;
  690.   uint64_t init_address;
  691.   uint64_t init_module;
  692.   uint64_t reserved1;
  693.   uint64_t reserved2;
  694.   uint64_t reserved3;
  695.   uint64_t reserved4;
  696.   uint64_t reserved5;
  697.   uint64_t reserved6;
  698. };
  699.  
  700. struct symtab_command {
  701.   uint32_t cmd;
  702.   uint32_t cmdsize;
  703.   uint32_t symoff;
  704.   uint32_t nsyms;
  705.   uint32_t stroff;
  706.   uint32_t strsize;
  707. };
  708.  
  709. struct dysymtab_command {
  710.   uint32_t cmd;
  711.   uint32_t cmdsize;
  712.   uint32_t ilocalsym;
  713.   uint32_t nlocalsym;
  714.   uint32_t iextdefsym;
  715.   uint32_t nextdefsym;
  716.   uint32_t iundefsym;
  717.   uint32_t nundefsym;
  718.   uint32_t tocoff;
  719.   uint32_t ntoc;
  720.   uint32_t modtaboff;
  721.   uint32_t nmodtab;
  722.   uint32_t extrefsymoff;
  723.   uint32_t nextrefsyms;
  724.   uint32_t indirectsymoff;
  725.   uint32_t nindirectsyms;
  726.   uint32_t extreloff;
  727.   uint32_t nextrel;
  728.   uint32_t locreloff;
  729.   uint32_t nlocrel;
  730. };
  731.  
  732. struct dylib_table_of_contents {
  733.   uint32_t symbol_index;
  734.   uint32_t module_index;
  735. };
  736.  
  737. struct dylib_module {
  738.   uint32_t module_name;
  739.   uint32_t iextdefsym;
  740.   uint32_t nextdefsym;
  741.   uint32_t irefsym;
  742.   uint32_t nrefsym;
  743.   uint32_t ilocalsym;
  744.   uint32_t nlocalsym;
  745.   uint32_t iextrel;
  746.   uint32_t nextrel;
  747.   uint32_t iinit_iterm;
  748.   uint32_t ninit_nterm;
  749.   uint32_t objc_module_info_addr;
  750.   uint32_t objc_module_info_size;
  751. };
  752.  
  753. struct dylib_module_64 {
  754.   uint32_t module_name;
  755.   uint32_t iextdefsym;
  756.   uint32_t nextdefsym;
  757.   uint32_t irefsym;
  758.   uint32_t nrefsym;
  759.   uint32_t ilocalsym;
  760.   uint32_t nlocalsym;
  761.   uint32_t iextrel;
  762.   uint32_t nextrel;
  763.   uint32_t iinit_iterm;
  764.   uint32_t ninit_nterm;
  765.   uint32_t objc_module_info_size;
  766.   uint64_t objc_module_info_addr;
  767. };
  768.  
  769. struct dylib_reference {
  770.   uint32_t isym : 24, flags : 8;
  771. };
  772.  
  773. // The twolevel_hints_command is obsolete and no longer supported.
  774. struct twolevel_hints_command {
  775.   uint32_t cmd;
  776.   uint32_t cmdsize;
  777.   uint32_t offset;
  778.   uint32_t nhints;
  779. };
  780.  
  781. // The twolevel_hints_command is obsolete and no longer supported.
  782. struct twolevel_hint {
  783.   uint32_t isub_image : 8, itoc : 24;
  784. };
  785.  
  786. // The prebind_cksum_command is obsolete and no longer supported.
  787. struct prebind_cksum_command {
  788.   uint32_t cmd;
  789.   uint32_t cmdsize;
  790.   uint32_t cksum;
  791. };
  792.  
  793. struct uuid_command {
  794.   uint32_t cmd;
  795.   uint32_t cmdsize;
  796.   uint8_t uuid[16];
  797. };
  798.  
  799. struct rpath_command {
  800.   uint32_t cmd;
  801.   uint32_t cmdsize;
  802.   uint32_t path;
  803. };
  804.  
  805. struct linkedit_data_command {
  806.   uint32_t cmd;
  807.   uint32_t cmdsize;
  808.   uint32_t dataoff;
  809.   uint32_t datasize;
  810. };
  811.  
  812. struct data_in_code_entry {
  813.   uint32_t offset;
  814.   uint16_t length;
  815.   uint16_t kind;
  816. };
  817.  
  818. struct source_version_command {
  819.   uint32_t cmd;
  820.   uint32_t cmdsize;
  821.   uint64_t version;
  822. };
  823.  
  824. struct encryption_info_command {
  825.   uint32_t cmd;
  826.   uint32_t cmdsize;
  827.   uint32_t cryptoff;
  828.   uint32_t cryptsize;
  829.   uint32_t cryptid;
  830. };
  831.  
  832. struct encryption_info_command_64 {
  833.   uint32_t cmd;
  834.   uint32_t cmdsize;
  835.   uint32_t cryptoff;
  836.   uint32_t cryptsize;
  837.   uint32_t cryptid;
  838.   uint32_t pad;
  839. };
  840.  
  841. struct version_min_command {
  842.   uint32_t cmd;     // LC_VERSION_MIN_MACOSX or
  843.                     // LC_VERSION_MIN_IPHONEOS
  844.   uint32_t cmdsize; // sizeof(struct version_min_command)
  845.   uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  846.   uint32_t sdk;     // X.Y.Z is encoded in nibbles xxxx.yy.zz
  847. };
  848.  
  849. struct note_command {
  850.   uint32_t cmd;        // LC_NOTE
  851.   uint32_t cmdsize;    // sizeof(struct note_command)
  852.   char data_owner[16]; // owner name for this LC_NOTE
  853.   uint64_t offset;     // file offset of this data
  854.   uint64_t size;       // length of data region
  855. };
  856.  
  857. struct build_tool_version {
  858.   uint32_t tool;    // enum for the tool
  859.   uint32_t version; // version of the tool
  860. };
  861.  
  862. struct build_version_command {
  863.   uint32_t cmd;      // LC_BUILD_VERSION
  864.   uint32_t cmdsize;  // sizeof(struct build_version_command) +
  865.                      // ntools * sizeof(struct build_tool_version)
  866.   uint32_t platform; // platform
  867.   uint32_t minos;    // X.Y.Z is encoded in nibbles xxxx.yy.zz
  868.   uint32_t sdk;      // X.Y.Z is encoded in nibbles xxxx.yy.zz
  869.   uint32_t ntools;   // number of tool entries following this
  870. };
  871.  
  872. struct dyld_env_command {
  873.   uint32_t cmd;
  874.   uint32_t cmdsize;
  875.   uint32_t name;
  876. };
  877.  
  878. struct dyld_info_command {
  879.   uint32_t cmd;
  880.   uint32_t cmdsize;
  881.   uint32_t rebase_off;
  882.   uint32_t rebase_size;
  883.   uint32_t bind_off;
  884.   uint32_t bind_size;
  885.   uint32_t weak_bind_off;
  886.   uint32_t weak_bind_size;
  887.   uint32_t lazy_bind_off;
  888.   uint32_t lazy_bind_size;
  889.   uint32_t export_off;
  890.   uint32_t export_size;
  891. };
  892.  
  893. struct linker_option_command {
  894.   uint32_t cmd;
  895.   uint32_t cmdsize;
  896.   uint32_t count;
  897. };
  898.  
  899. struct fileset_entry_command {
  900.   uint32_t cmd;
  901.   uint32_t cmdsize;
  902.   uint64_t vmaddr;
  903.   uint64_t fileoff;
  904.   uint32_t entry_id;
  905. };
  906.  
  907. // The symseg_command is obsolete and no longer supported.
  908. struct symseg_command {
  909.   uint32_t cmd;
  910.   uint32_t cmdsize;
  911.   uint32_t offset;
  912.   uint32_t size;
  913. };
  914.  
  915. // The ident_command is obsolete and no longer supported.
  916. struct ident_command {
  917.   uint32_t cmd;
  918.   uint32_t cmdsize;
  919. };
  920.  
  921. // The fvmfile_command is obsolete and no longer supported.
  922. struct fvmfile_command {
  923.   uint32_t cmd;
  924.   uint32_t cmdsize;
  925.   uint32_t name;
  926.   uint32_t header_addr;
  927. };
  928.  
  929. struct tlv_descriptor_32 {
  930.   uint32_t thunk;
  931.   uint32_t key;
  932.   uint32_t offset;
  933. };
  934.  
  935. struct tlv_descriptor_64 {
  936.   uint64_t thunk;
  937.   uint64_t key;
  938.   uint64_t offset;
  939. };
  940.  
  941. struct tlv_descriptor {
  942.   uintptr_t thunk;
  943.   uintptr_t key;
  944.   uintptr_t offset;
  945. };
  946.  
  947. struct entry_point_command {
  948.   uint32_t cmd;
  949.   uint32_t cmdsize;
  950.   uint64_t entryoff;
  951.   uint64_t stacksize;
  952. };
  953.  
  954. // Structs from <mach-o/fat.h>
  955. struct fat_header {
  956.   uint32_t magic;
  957.   uint32_t nfat_arch;
  958. };
  959.  
  960. struct fat_arch {
  961.   uint32_t cputype;
  962.   uint32_t cpusubtype;
  963.   uint32_t offset;
  964.   uint32_t size;
  965.   uint32_t align;
  966. };
  967.  
  968. struct fat_arch_64 {
  969.   uint32_t cputype;
  970.   uint32_t cpusubtype;
  971.   uint64_t offset;
  972.   uint64_t size;
  973.   uint32_t align;
  974.   uint32_t reserved;
  975. };
  976.  
  977. // Structs from <mach-o/reloc.h>
  978. struct relocation_info {
  979.   int32_t r_address;
  980.   uint32_t r_symbolnum : 24, r_pcrel : 1, r_length : 2, r_extern : 1,
  981.       r_type : 4;
  982. };
  983.  
  984. struct scattered_relocation_info {
  985. #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  986.   uint32_t r_scattered : 1, r_pcrel : 1, r_length : 2, r_type : 4,
  987.       r_address : 24;
  988. #else
  989.   uint32_t r_address : 24, r_type : 4, r_length : 2, r_pcrel : 1,
  990.       r_scattered : 1;
  991. #endif
  992.   int32_t r_value;
  993. };
  994.  
  995. // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
  996. struct any_relocation_info {
  997.   uint32_t r_word0, r_word1;
  998. };
  999.  
  1000. // Structs from <mach-o/nlist.h>
  1001. struct nlist_base {
  1002.   uint32_t n_strx;
  1003.   uint8_t n_type;
  1004.   uint8_t n_sect;
  1005.   uint16_t n_desc;
  1006. };
  1007.  
  1008. struct nlist {
  1009.   uint32_t n_strx;
  1010.   uint8_t n_type;
  1011.   uint8_t n_sect;
  1012.   int16_t n_desc;
  1013.   uint32_t n_value;
  1014. };
  1015.  
  1016. struct nlist_64 {
  1017.   uint32_t n_strx;
  1018.   uint8_t n_type;
  1019.   uint8_t n_sect;
  1020.   uint16_t n_desc;
  1021.   uint64_t n_value;
  1022. };
  1023.  
  1024. // Values for dyld_chained_fixups_header::imports_format.
  1025. enum ChainedImportFormat {
  1026.   DYLD_CHAINED_IMPORT = 1,
  1027.   DYLD_CHAINED_IMPORT_ADDEND = 2,
  1028.   DYLD_CHAINED_IMPORT_ADDEND64 = 3,
  1029. };
  1030.  
  1031. // Values for dyld_chained_fixups_header::symbols_format.
  1032. enum {
  1033.   DYLD_CHAINED_SYMBOL_UNCOMPRESSED = 0,
  1034.   DYLD_CHAINED_SYMBOL_ZLIB = 1,
  1035. };
  1036.  
  1037. // Values for dyld_chained_starts_in_segment::page_start.
  1038. enum {
  1039.   DYLD_CHAINED_PTR_START_NONE = 0xFFFF,
  1040.   DYLD_CHAINED_PTR_START_MULTI = 0x8000,
  1041.   DYLD_CHAINED_PTR_START_LAST = 0x8000,
  1042. };
  1043.  
  1044. // Values for dyld_chained_starts_in_segment::pointer_format.
  1045. enum {
  1046.   DYLD_CHAINED_PTR_ARM64E = 1,
  1047.   DYLD_CHAINED_PTR_64 = 2,
  1048.   DYLD_CHAINED_PTR_32 = 3,
  1049.   DYLD_CHAINED_PTR_32_CACHE = 4,
  1050.   DYLD_CHAINED_PTR_32_FIRMWARE = 5,
  1051.   DYLD_CHAINED_PTR_64_OFFSET = 6,
  1052.   DYLD_CHAINED_PTR_ARM64E_KERNEL = 7,
  1053.   DYLD_CHAINED_PTR_64_KERNEL_CACHE = 8,
  1054.   DYLD_CHAINED_PTR_ARM64E_USERLAND = 9,
  1055.   DYLD_CHAINED_PTR_ARM64E_FIRMWARE = 10,
  1056.   DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE = 11,
  1057.   DYLD_CHAINED_PTR_ARM64E_USERLAND24 = 12,
  1058. };
  1059.  
  1060. /// Structs for dyld chained fixups.
  1061. /// dyld_chained_fixups_header is the data pointed to by LC_DYLD_CHAINED_FIXUPS
  1062. /// load command.
  1063. struct dyld_chained_fixups_header {
  1064.   uint32_t fixups_version; ///< 0
  1065.   uint32_t starts_offset;  ///< Offset of dyld_chained_starts_in_image.
  1066.   uint32_t imports_offset; ///< Offset of imports table in chain_data.
  1067.   uint32_t symbols_offset; ///< Offset of symbol strings in chain_data.
  1068.   uint32_t imports_count;  ///< Number of imported symbol names.
  1069.   uint32_t imports_format; ///< DYLD_CHAINED_IMPORT*
  1070.   uint32_t symbols_format; ///< 0 => uncompressed, 1 => zlib compressed
  1071. };
  1072.  
  1073. /// dyld_chained_starts_in_image is embedded in LC_DYLD_CHAINED_FIXUPS payload.
  1074. /// Each each seg_info_offset entry is the offset into this struct for that
  1075. /// segment followed by pool of dyld_chain_starts_in_segment data.
  1076. struct dyld_chained_starts_in_image {
  1077.   uint32_t seg_count;
  1078.   uint32_t seg_info_offset[1];
  1079. };
  1080.  
  1081. struct dyld_chained_starts_in_segment {
  1082.   uint32_t size;              ///< Size of this, including chain_starts entries
  1083.   uint16_t page_size;         ///< Page size in bytes (0x1000 or 0x4000)
  1084.   uint16_t pointer_format;    ///< DYLD_CHAINED_PTR*
  1085.   uint64_t segment_offset;    ///< VM offset from the __TEXT segment
  1086.   uint32_t max_valid_pointer; ///< Values beyond this are not pointers on 32-bit
  1087.   uint16_t page_count;        ///< Length of the page_start array
  1088.   uint16_t page_start[1];     ///< Page offset of first fixup on each page, or
  1089.                               ///< DYLD_CHAINED_PTR_START_NONE if no fixups
  1090. };
  1091.  
  1092. // DYLD_CHAINED_IMPORT
  1093. struct dyld_chained_import {
  1094.   uint32_t lib_ordinal : 8;
  1095.   uint32_t weak_import : 1;
  1096.   uint32_t name_offset : 23;
  1097. };
  1098.  
  1099. // DYLD_CHAINED_IMPORT_ADDEND
  1100. struct dyld_chained_import_addend {
  1101.   uint32_t lib_ordinal : 8;
  1102.   uint32_t weak_import : 1;
  1103.   uint32_t name_offset : 23;
  1104.   int32_t addend;
  1105. };
  1106.  
  1107. // DYLD_CHAINED_IMPORT_ADDEND64
  1108. struct dyld_chained_import_addend64 {
  1109.   uint64_t lib_ordinal : 16;
  1110.   uint64_t weak_import : 1;
  1111.   uint64_t reserved : 15;
  1112.   uint64_t name_offset : 32;
  1113.   uint64_t addend;
  1114. };
  1115.  
  1116. // The `bind` field (most significant bit) of the encoded fixup determines
  1117. // whether it is dyld_chained_ptr_64_bind or dyld_chained_ptr_64_rebase.
  1118.  
  1119. // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
  1120. struct dyld_chained_ptr_64_bind {
  1121.   uint64_t ordinal : 24;
  1122.   uint64_t addend : 8;
  1123.   uint64_t reserved : 19;
  1124.   uint64_t next : 12;
  1125.   uint64_t bind : 1; // set to 1
  1126. };
  1127.  
  1128. // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
  1129. struct dyld_chained_ptr_64_rebase {
  1130.   uint64_t target : 36;
  1131.   uint64_t high8 : 8;
  1132.   uint64_t reserved : 7;
  1133.   uint64_t next : 12;
  1134.   uint64_t bind : 1; // set to 0
  1135. };
  1136.  
  1137. // Byte order swapping functions for MachO structs
  1138.  
  1139. inline void swapStruct(fat_header &mh) {
  1140.   sys::swapByteOrder(mh.magic);
  1141.   sys::swapByteOrder(mh.nfat_arch);
  1142. }
  1143.  
  1144. inline void swapStruct(fat_arch &mh) {
  1145.   sys::swapByteOrder(mh.cputype);
  1146.   sys::swapByteOrder(mh.cpusubtype);
  1147.   sys::swapByteOrder(mh.offset);
  1148.   sys::swapByteOrder(mh.size);
  1149.   sys::swapByteOrder(mh.align);
  1150. }
  1151.  
  1152. inline void swapStruct(fat_arch_64 &mh) {
  1153.   sys::swapByteOrder(mh.cputype);
  1154.   sys::swapByteOrder(mh.cpusubtype);
  1155.   sys::swapByteOrder(mh.offset);
  1156.   sys::swapByteOrder(mh.size);
  1157.   sys::swapByteOrder(mh.align);
  1158.   sys::swapByteOrder(mh.reserved);
  1159. }
  1160.  
  1161. inline void swapStruct(mach_header &mh) {
  1162.   sys::swapByteOrder(mh.magic);
  1163.   sys::swapByteOrder(mh.cputype);
  1164.   sys::swapByteOrder(mh.cpusubtype);
  1165.   sys::swapByteOrder(mh.filetype);
  1166.   sys::swapByteOrder(mh.ncmds);
  1167.   sys::swapByteOrder(mh.sizeofcmds);
  1168.   sys::swapByteOrder(mh.flags);
  1169. }
  1170.  
  1171. inline void swapStruct(mach_header_64 &H) {
  1172.   sys::swapByteOrder(H.magic);
  1173.   sys::swapByteOrder(H.cputype);
  1174.   sys::swapByteOrder(H.cpusubtype);
  1175.   sys::swapByteOrder(H.filetype);
  1176.   sys::swapByteOrder(H.ncmds);
  1177.   sys::swapByteOrder(H.sizeofcmds);
  1178.   sys::swapByteOrder(H.flags);
  1179.   sys::swapByteOrder(H.reserved);
  1180. }
  1181.  
  1182. inline void swapStruct(load_command &lc) {
  1183.   sys::swapByteOrder(lc.cmd);
  1184.   sys::swapByteOrder(lc.cmdsize);
  1185. }
  1186.  
  1187. inline void swapStruct(symtab_command &lc) {
  1188.   sys::swapByteOrder(lc.cmd);
  1189.   sys::swapByteOrder(lc.cmdsize);
  1190.   sys::swapByteOrder(lc.symoff);
  1191.   sys::swapByteOrder(lc.nsyms);
  1192.   sys::swapByteOrder(lc.stroff);
  1193.   sys::swapByteOrder(lc.strsize);
  1194. }
  1195.  
  1196. inline void swapStruct(segment_command_64 &seg) {
  1197.   sys::swapByteOrder(seg.cmd);
  1198.   sys::swapByteOrder(seg.cmdsize);
  1199.   sys::swapByteOrder(seg.vmaddr);
  1200.   sys::swapByteOrder(seg.vmsize);
  1201.   sys::swapByteOrder(seg.fileoff);
  1202.   sys::swapByteOrder(seg.filesize);
  1203.   sys::swapByteOrder(seg.maxprot);
  1204.   sys::swapByteOrder(seg.initprot);
  1205.   sys::swapByteOrder(seg.nsects);
  1206.   sys::swapByteOrder(seg.flags);
  1207. }
  1208.  
  1209. inline void swapStruct(segment_command &seg) {
  1210.   sys::swapByteOrder(seg.cmd);
  1211.   sys::swapByteOrder(seg.cmdsize);
  1212.   sys::swapByteOrder(seg.vmaddr);
  1213.   sys::swapByteOrder(seg.vmsize);
  1214.   sys::swapByteOrder(seg.fileoff);
  1215.   sys::swapByteOrder(seg.filesize);
  1216.   sys::swapByteOrder(seg.maxprot);
  1217.   sys::swapByteOrder(seg.initprot);
  1218.   sys::swapByteOrder(seg.nsects);
  1219.   sys::swapByteOrder(seg.flags);
  1220. }
  1221.  
  1222. inline void swapStruct(section_64 &sect) {
  1223.   sys::swapByteOrder(sect.addr);
  1224.   sys::swapByteOrder(sect.size);
  1225.   sys::swapByteOrder(sect.offset);
  1226.   sys::swapByteOrder(sect.align);
  1227.   sys::swapByteOrder(sect.reloff);
  1228.   sys::swapByteOrder(sect.nreloc);
  1229.   sys::swapByteOrder(sect.flags);
  1230.   sys::swapByteOrder(sect.reserved1);
  1231.   sys::swapByteOrder(sect.reserved2);
  1232. }
  1233.  
  1234. inline void swapStruct(section &sect) {
  1235.   sys::swapByteOrder(sect.addr);
  1236.   sys::swapByteOrder(sect.size);
  1237.   sys::swapByteOrder(sect.offset);
  1238.   sys::swapByteOrder(sect.align);
  1239.   sys::swapByteOrder(sect.reloff);
  1240.   sys::swapByteOrder(sect.nreloc);
  1241.   sys::swapByteOrder(sect.flags);
  1242.   sys::swapByteOrder(sect.reserved1);
  1243.   sys::swapByteOrder(sect.reserved2);
  1244. }
  1245.  
  1246. inline void swapStruct(dyld_info_command &info) {
  1247.   sys::swapByteOrder(info.cmd);
  1248.   sys::swapByteOrder(info.cmdsize);
  1249.   sys::swapByteOrder(info.rebase_off);
  1250.   sys::swapByteOrder(info.rebase_size);
  1251.   sys::swapByteOrder(info.bind_off);
  1252.   sys::swapByteOrder(info.bind_size);
  1253.   sys::swapByteOrder(info.weak_bind_off);
  1254.   sys::swapByteOrder(info.weak_bind_size);
  1255.   sys::swapByteOrder(info.lazy_bind_off);
  1256.   sys::swapByteOrder(info.lazy_bind_size);
  1257.   sys::swapByteOrder(info.export_off);
  1258.   sys::swapByteOrder(info.export_size);
  1259. }
  1260.  
  1261. inline void swapStruct(dylib_command &d) {
  1262.   sys::swapByteOrder(d.cmd);
  1263.   sys::swapByteOrder(d.cmdsize);
  1264.   sys::swapByteOrder(d.dylib.name);
  1265.   sys::swapByteOrder(d.dylib.timestamp);
  1266.   sys::swapByteOrder(d.dylib.current_version);
  1267.   sys::swapByteOrder(d.dylib.compatibility_version);
  1268. }
  1269.  
  1270. inline void swapStruct(sub_framework_command &s) {
  1271.   sys::swapByteOrder(s.cmd);
  1272.   sys::swapByteOrder(s.cmdsize);
  1273.   sys::swapByteOrder(s.umbrella);
  1274. }
  1275.  
  1276. inline void swapStruct(sub_umbrella_command &s) {
  1277.   sys::swapByteOrder(s.cmd);
  1278.   sys::swapByteOrder(s.cmdsize);
  1279.   sys::swapByteOrder(s.sub_umbrella);
  1280. }
  1281.  
  1282. inline void swapStruct(sub_library_command &s) {
  1283.   sys::swapByteOrder(s.cmd);
  1284.   sys::swapByteOrder(s.cmdsize);
  1285.   sys::swapByteOrder(s.sub_library);
  1286. }
  1287.  
  1288. inline void swapStruct(sub_client_command &s) {
  1289.   sys::swapByteOrder(s.cmd);
  1290.   sys::swapByteOrder(s.cmdsize);
  1291.   sys::swapByteOrder(s.client);
  1292. }
  1293.  
  1294. inline void swapStruct(routines_command &r) {
  1295.   sys::swapByteOrder(r.cmd);
  1296.   sys::swapByteOrder(r.cmdsize);
  1297.   sys::swapByteOrder(r.init_address);
  1298.   sys::swapByteOrder(r.init_module);
  1299.   sys::swapByteOrder(r.reserved1);
  1300.   sys::swapByteOrder(r.reserved2);
  1301.   sys::swapByteOrder(r.reserved3);
  1302.   sys::swapByteOrder(r.reserved4);
  1303.   sys::swapByteOrder(r.reserved5);
  1304.   sys::swapByteOrder(r.reserved6);
  1305. }
  1306.  
  1307. inline void swapStruct(routines_command_64 &r) {
  1308.   sys::swapByteOrder(r.cmd);
  1309.   sys::swapByteOrder(r.cmdsize);
  1310.   sys::swapByteOrder(r.init_address);
  1311.   sys::swapByteOrder(r.init_module);
  1312.   sys::swapByteOrder(r.reserved1);
  1313.   sys::swapByteOrder(r.reserved2);
  1314.   sys::swapByteOrder(r.reserved3);
  1315.   sys::swapByteOrder(r.reserved4);
  1316.   sys::swapByteOrder(r.reserved5);
  1317.   sys::swapByteOrder(r.reserved6);
  1318. }
  1319.  
  1320. inline void swapStruct(thread_command &t) {
  1321.   sys::swapByteOrder(t.cmd);
  1322.   sys::swapByteOrder(t.cmdsize);
  1323. }
  1324.  
  1325. inline void swapStruct(dylinker_command &d) {
  1326.   sys::swapByteOrder(d.cmd);
  1327.   sys::swapByteOrder(d.cmdsize);
  1328.   sys::swapByteOrder(d.name);
  1329. }
  1330.  
  1331. inline void swapStruct(uuid_command &u) {
  1332.   sys::swapByteOrder(u.cmd);
  1333.   sys::swapByteOrder(u.cmdsize);
  1334. }
  1335.  
  1336. inline void swapStruct(rpath_command &r) {
  1337.   sys::swapByteOrder(r.cmd);
  1338.   sys::swapByteOrder(r.cmdsize);
  1339.   sys::swapByteOrder(r.path);
  1340. }
  1341.  
  1342. inline void swapStruct(source_version_command &s) {
  1343.   sys::swapByteOrder(s.cmd);
  1344.   sys::swapByteOrder(s.cmdsize);
  1345.   sys::swapByteOrder(s.version);
  1346. }
  1347.  
  1348. inline void swapStruct(entry_point_command &e) {
  1349.   sys::swapByteOrder(e.cmd);
  1350.   sys::swapByteOrder(e.cmdsize);
  1351.   sys::swapByteOrder(e.entryoff);
  1352.   sys::swapByteOrder(e.stacksize);
  1353. }
  1354.  
  1355. inline void swapStruct(encryption_info_command &e) {
  1356.   sys::swapByteOrder(e.cmd);
  1357.   sys::swapByteOrder(e.cmdsize);
  1358.   sys::swapByteOrder(e.cryptoff);
  1359.   sys::swapByteOrder(e.cryptsize);
  1360.   sys::swapByteOrder(e.cryptid);
  1361. }
  1362.  
  1363. inline void swapStruct(encryption_info_command_64 &e) {
  1364.   sys::swapByteOrder(e.cmd);
  1365.   sys::swapByteOrder(e.cmdsize);
  1366.   sys::swapByteOrder(e.cryptoff);
  1367.   sys::swapByteOrder(e.cryptsize);
  1368.   sys::swapByteOrder(e.cryptid);
  1369.   sys::swapByteOrder(e.pad);
  1370. }
  1371.  
  1372. inline void swapStruct(dysymtab_command &dst) {
  1373.   sys::swapByteOrder(dst.cmd);
  1374.   sys::swapByteOrder(dst.cmdsize);
  1375.   sys::swapByteOrder(dst.ilocalsym);
  1376.   sys::swapByteOrder(dst.nlocalsym);
  1377.   sys::swapByteOrder(dst.iextdefsym);
  1378.   sys::swapByteOrder(dst.nextdefsym);
  1379.   sys::swapByteOrder(dst.iundefsym);
  1380.   sys::swapByteOrder(dst.nundefsym);
  1381.   sys::swapByteOrder(dst.tocoff);
  1382.   sys::swapByteOrder(dst.ntoc);
  1383.   sys::swapByteOrder(dst.modtaboff);
  1384.   sys::swapByteOrder(dst.nmodtab);
  1385.   sys::swapByteOrder(dst.extrefsymoff);
  1386.   sys::swapByteOrder(dst.nextrefsyms);
  1387.   sys::swapByteOrder(dst.indirectsymoff);
  1388.   sys::swapByteOrder(dst.nindirectsyms);
  1389.   sys::swapByteOrder(dst.extreloff);
  1390.   sys::swapByteOrder(dst.nextrel);
  1391.   sys::swapByteOrder(dst.locreloff);
  1392.   sys::swapByteOrder(dst.nlocrel);
  1393. }
  1394.  
  1395. inline void swapStruct(any_relocation_info &reloc) {
  1396.   sys::swapByteOrder(reloc.r_word0);
  1397.   sys::swapByteOrder(reloc.r_word1);
  1398. }
  1399.  
  1400. inline void swapStruct(nlist_base &S) {
  1401.   sys::swapByteOrder(S.n_strx);
  1402.   sys::swapByteOrder(S.n_desc);
  1403. }
  1404.  
  1405. inline void swapStruct(nlist &sym) {
  1406.   sys::swapByteOrder(sym.n_strx);
  1407.   sys::swapByteOrder(sym.n_desc);
  1408.   sys::swapByteOrder(sym.n_value);
  1409. }
  1410.  
  1411. inline void swapStruct(nlist_64 &sym) {
  1412.   sys::swapByteOrder(sym.n_strx);
  1413.   sys::swapByteOrder(sym.n_desc);
  1414.   sys::swapByteOrder(sym.n_value);
  1415. }
  1416.  
  1417. inline void swapStruct(linkedit_data_command &C) {
  1418.   sys::swapByteOrder(C.cmd);
  1419.   sys::swapByteOrder(C.cmdsize);
  1420.   sys::swapByteOrder(C.dataoff);
  1421.   sys::swapByteOrder(C.datasize);
  1422. }
  1423.  
  1424. inline void swapStruct(linker_option_command &C) {
  1425.   sys::swapByteOrder(C.cmd);
  1426.   sys::swapByteOrder(C.cmdsize);
  1427.   sys::swapByteOrder(C.count);
  1428. }
  1429.  
  1430. inline void swapStruct(fileset_entry_command &C) {
  1431.   sys::swapByteOrder(C.cmd);
  1432.   sys::swapByteOrder(C.cmdsize);
  1433.   sys::swapByteOrder(C.vmaddr);
  1434.   sys::swapByteOrder(C.fileoff);
  1435.   sys::swapByteOrder(C.entry_id);
  1436. }
  1437.  
  1438. inline void swapStruct(version_min_command &C) {
  1439.   sys::swapByteOrder(C.cmd);
  1440.   sys::swapByteOrder(C.cmdsize);
  1441.   sys::swapByteOrder(C.version);
  1442.   sys::swapByteOrder(C.sdk);
  1443. }
  1444.  
  1445. inline void swapStruct(note_command &C) {
  1446.   sys::swapByteOrder(C.cmd);
  1447.   sys::swapByteOrder(C.cmdsize);
  1448.   sys::swapByteOrder(C.offset);
  1449.   sys::swapByteOrder(C.size);
  1450. }
  1451.  
  1452. inline void swapStruct(build_version_command &C) {
  1453.   sys::swapByteOrder(C.cmd);
  1454.   sys::swapByteOrder(C.cmdsize);
  1455.   sys::swapByteOrder(C.platform);
  1456.   sys::swapByteOrder(C.minos);
  1457.   sys::swapByteOrder(C.sdk);
  1458.   sys::swapByteOrder(C.ntools);
  1459. }
  1460.  
  1461. inline void swapStruct(build_tool_version &C) {
  1462.   sys::swapByteOrder(C.tool);
  1463.   sys::swapByteOrder(C.version);
  1464. }
  1465.  
  1466. inline void swapStruct(data_in_code_entry &C) {
  1467.   sys::swapByteOrder(C.offset);
  1468.   sys::swapByteOrder(C.length);
  1469.   sys::swapByteOrder(C.kind);
  1470. }
  1471.  
  1472. inline void swapStruct(uint32_t &C) { sys::swapByteOrder(C); }
  1473.  
  1474. // The prebind_cksum_command is obsolete and no longer supported.
  1475. inline void swapStruct(prebind_cksum_command &C) {
  1476.   sys::swapByteOrder(C.cmd);
  1477.   sys::swapByteOrder(C.cmdsize);
  1478.   sys::swapByteOrder(C.cksum);
  1479. }
  1480.  
  1481. // The twolevel_hints_command is obsolete and no longer supported.
  1482. inline void swapStruct(twolevel_hints_command &C) {
  1483.   sys::swapByteOrder(C.cmd);
  1484.   sys::swapByteOrder(C.cmdsize);
  1485.   sys::swapByteOrder(C.offset);
  1486.   sys::swapByteOrder(C.nhints);
  1487. }
  1488.  
  1489. // The prebound_dylib_command is obsolete and no longer supported.
  1490. inline void swapStruct(prebound_dylib_command &C) {
  1491.   sys::swapByteOrder(C.cmd);
  1492.   sys::swapByteOrder(C.cmdsize);
  1493.   sys::swapByteOrder(C.name);
  1494.   sys::swapByteOrder(C.nmodules);
  1495.   sys::swapByteOrder(C.linked_modules);
  1496. }
  1497.  
  1498. // The fvmfile_command is obsolete and no longer supported.
  1499. inline void swapStruct(fvmfile_command &C) {
  1500.   sys::swapByteOrder(C.cmd);
  1501.   sys::swapByteOrder(C.cmdsize);
  1502.   sys::swapByteOrder(C.name);
  1503.   sys::swapByteOrder(C.header_addr);
  1504. }
  1505.  
  1506. // The symseg_command is obsolete and no longer supported.
  1507. inline void swapStruct(symseg_command &C) {
  1508.   sys::swapByteOrder(C.cmd);
  1509.   sys::swapByteOrder(C.cmdsize);
  1510.   sys::swapByteOrder(C.offset);
  1511.   sys::swapByteOrder(C.size);
  1512. }
  1513.  
  1514. // The ident_command is obsolete and no longer supported.
  1515. inline void swapStruct(ident_command &C) {
  1516.   sys::swapByteOrder(C.cmd);
  1517.   sys::swapByteOrder(C.cmdsize);
  1518. }
  1519.  
  1520. inline void swapStruct(fvmlib &C) {
  1521.   sys::swapByteOrder(C.name);
  1522.   sys::swapByteOrder(C.minor_version);
  1523.   sys::swapByteOrder(C.header_addr);
  1524. }
  1525.  
  1526. // The fvmlib_command is obsolete and no longer supported.
  1527. inline void swapStruct(fvmlib_command &C) {
  1528.   sys::swapByteOrder(C.cmd);
  1529.   sys::swapByteOrder(C.cmdsize);
  1530.   swapStruct(C.fvmlib);
  1531. }
  1532.  
  1533. // Get/Set functions from <mach-o/nlist.h>
  1534.  
  1535. inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
  1536.   return (((n_desc) >> 8u) & 0xffu);
  1537. }
  1538.  
  1539. inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
  1540.   n_desc = (((n_desc)&0x00ff) | (((ordinal)&0xff) << 8));
  1541. }
  1542.  
  1543. inline uint8_t GET_COMM_ALIGN(uint16_t n_desc) {
  1544.   return (n_desc >> 8u) & 0x0fu;
  1545. }
  1546.  
  1547. inline void SET_COMM_ALIGN(uint16_t &n_desc, uint8_t align) {
  1548.   n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
  1549. }
  1550.  
  1551. // Enums from <mach/machine.h>
  1552. enum : uint32_t {
  1553.   // Capability bits used in the definition of cpu_type.
  1554.   CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
  1555.   CPU_ARCH_ABI64 = 0x01000000, // 64 bit ABI
  1556.   CPU_ARCH_ABI64_32 = 0x02000000, // ILP32 ABI on 64-bit hardware
  1557. };
  1558.  
  1559. // Constants for the cputype field.
  1560. enum CPUType {
  1561.   CPU_TYPE_ANY = -1,
  1562.   CPU_TYPE_X86 = 7,
  1563.   CPU_TYPE_I386 = CPU_TYPE_X86,
  1564.   CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
  1565.   /* CPU_TYPE_MIPS      = 8, */
  1566.   CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
  1567.   CPU_TYPE_ARM = 12,
  1568.   CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
  1569.   CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32,
  1570.   CPU_TYPE_SPARC = 14,
  1571.   CPU_TYPE_POWERPC = 18,
  1572.   CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
  1573. };
  1574.  
  1575. enum : uint32_t {
  1576.   // Capability bits used in the definition of cpusubtype.
  1577.   CPU_SUBTYPE_MASK = 0xff000000,  // Mask for architecture bits
  1578.   CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
  1579.  
  1580.   // Special CPU subtype constants.
  1581.   CPU_SUBTYPE_MULTIPLE = ~0u
  1582. };
  1583.  
  1584. // Constants for the cpusubtype field.
  1585. enum CPUSubTypeX86 {
  1586.   CPU_SUBTYPE_I386_ALL = 3,
  1587.   CPU_SUBTYPE_386 = 3,
  1588.   CPU_SUBTYPE_486 = 4,
  1589.   CPU_SUBTYPE_486SX = 0x84,
  1590.   CPU_SUBTYPE_586 = 5,
  1591.   CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
  1592.   CPU_SUBTYPE_PENTPRO = 0x16,
  1593.   CPU_SUBTYPE_PENTII_M3 = 0x36,
  1594.   CPU_SUBTYPE_PENTII_M5 = 0x56,
  1595.   CPU_SUBTYPE_CELERON = 0x67,
  1596.   CPU_SUBTYPE_CELERON_MOBILE = 0x77,
  1597.   CPU_SUBTYPE_PENTIUM_3 = 0x08,
  1598.   CPU_SUBTYPE_PENTIUM_3_M = 0x18,
  1599.   CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
  1600.   CPU_SUBTYPE_PENTIUM_M = 0x09,
  1601.   CPU_SUBTYPE_PENTIUM_4 = 0x0a,
  1602.   CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
  1603.   CPU_SUBTYPE_ITANIUM = 0x0b,
  1604.   CPU_SUBTYPE_ITANIUM_2 = 0x1b,
  1605.   CPU_SUBTYPE_XEON = 0x0c,
  1606.   CPU_SUBTYPE_XEON_MP = 0x1c,
  1607.  
  1608.   CPU_SUBTYPE_X86_ALL = 3,
  1609.   CPU_SUBTYPE_X86_64_ALL = 3,
  1610.   CPU_SUBTYPE_X86_ARCH1 = 4,
  1611.   CPU_SUBTYPE_X86_64_H = 8
  1612. };
  1613. inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
  1614.   return Family | (Model << 4);
  1615. }
  1616. inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
  1617.   return ((int)ST) & 0x0f;
  1618. }
  1619. inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) { return ((int)ST) >> 4; }
  1620. enum { CPU_SUBTYPE_INTEL_FAMILY_MAX = 15, CPU_SUBTYPE_INTEL_MODEL_ALL = 0 };
  1621.  
  1622. enum CPUSubTypeARM {
  1623.   CPU_SUBTYPE_ARM_ALL = 0,
  1624.   CPU_SUBTYPE_ARM_V4T = 5,
  1625.   CPU_SUBTYPE_ARM_V6 = 6,
  1626.   CPU_SUBTYPE_ARM_V5 = 7,
  1627.   CPU_SUBTYPE_ARM_V5TEJ = 7,
  1628.   CPU_SUBTYPE_ARM_XSCALE = 8,
  1629.   CPU_SUBTYPE_ARM_V7 = 9,
  1630.   //  unused  ARM_V7F     = 10,
  1631.   CPU_SUBTYPE_ARM_V7S = 11,
  1632.   CPU_SUBTYPE_ARM_V7K = 12,
  1633.   CPU_SUBTYPE_ARM_V6M = 14,
  1634.   CPU_SUBTYPE_ARM_V7M = 15,
  1635.   CPU_SUBTYPE_ARM_V7EM = 16
  1636. };
  1637.  
  1638. enum CPUSubTypeARM64 {
  1639.   CPU_SUBTYPE_ARM64_ALL = 0,
  1640.   CPU_SUBTYPE_ARM64_V8 = 1,
  1641.   CPU_SUBTYPE_ARM64E = 2,
  1642. };
  1643.  
  1644. enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
  1645.  
  1646. enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
  1647.  
  1648. enum CPUSubTypePowerPC {
  1649.   CPU_SUBTYPE_POWERPC_ALL = 0,
  1650.   CPU_SUBTYPE_POWERPC_601 = 1,
  1651.   CPU_SUBTYPE_POWERPC_602 = 2,
  1652.   CPU_SUBTYPE_POWERPC_603 = 3,
  1653.   CPU_SUBTYPE_POWERPC_603e = 4,
  1654.   CPU_SUBTYPE_POWERPC_603ev = 5,
  1655.   CPU_SUBTYPE_POWERPC_604 = 6,
  1656.   CPU_SUBTYPE_POWERPC_604e = 7,
  1657.   CPU_SUBTYPE_POWERPC_620 = 8,
  1658.   CPU_SUBTYPE_POWERPC_750 = 9,
  1659.   CPU_SUBTYPE_POWERPC_7400 = 10,
  1660.   CPU_SUBTYPE_POWERPC_7450 = 11,
  1661.   CPU_SUBTYPE_POWERPC_970 = 100,
  1662.  
  1663.   CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
  1664.   CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
  1665. };
  1666.  
  1667. Expected<uint32_t> getCPUType(const Triple &T);
  1668. Expected<uint32_t> getCPUSubType(const Triple &T);
  1669.  
  1670. struct x86_thread_state32_t {
  1671.   uint32_t eax;
  1672.   uint32_t ebx;
  1673.   uint32_t ecx;
  1674.   uint32_t edx;
  1675.   uint32_t edi;
  1676.   uint32_t esi;
  1677.   uint32_t ebp;
  1678.   uint32_t esp;
  1679.   uint32_t ss;
  1680.   uint32_t eflags;
  1681.   uint32_t eip;
  1682.   uint32_t cs;
  1683.   uint32_t ds;
  1684.   uint32_t es;
  1685.   uint32_t fs;
  1686.   uint32_t gs;
  1687. };
  1688.  
  1689. struct x86_thread_state64_t {
  1690.   uint64_t rax;
  1691.   uint64_t rbx;
  1692.   uint64_t rcx;
  1693.   uint64_t rdx;
  1694.   uint64_t rdi;
  1695.   uint64_t rsi;
  1696.   uint64_t rbp;
  1697.   uint64_t rsp;
  1698.   uint64_t r8;
  1699.   uint64_t r9;
  1700.   uint64_t r10;
  1701.   uint64_t r11;
  1702.   uint64_t r12;
  1703.   uint64_t r13;
  1704.   uint64_t r14;
  1705.   uint64_t r15;
  1706.   uint64_t rip;
  1707.   uint64_t rflags;
  1708.   uint64_t cs;
  1709.   uint64_t fs;
  1710.   uint64_t gs;
  1711. };
  1712.  
  1713. enum x86_fp_control_precis {
  1714.   x86_FP_PREC_24B = 0,
  1715.   x86_FP_PREC_53B = 2,
  1716.   x86_FP_PREC_64B = 3
  1717. };
  1718.  
  1719. enum x86_fp_control_rc {
  1720.   x86_FP_RND_NEAR = 0,
  1721.   x86_FP_RND_DOWN = 1,
  1722.   x86_FP_RND_UP = 2,
  1723.   x86_FP_CHOP = 3
  1724. };
  1725.  
  1726. struct fp_control_t {
  1727.   unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1728.       precis : 1, : 2, pc : 2, rc : 2, : 1, : 3;
  1729. };
  1730.  
  1731. struct fp_status_t {
  1732.   unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1733.       precis : 1, stkflt : 1, errsumm : 1, c0 : 1, c1 : 1, c2 : 1, tos : 3,
  1734.       c3 : 1, busy : 1;
  1735. };
  1736.  
  1737. struct mmst_reg_t {
  1738.   char mmst_reg[10];
  1739.   char mmst_rsrv[6];
  1740. };
  1741.  
  1742. struct xmm_reg_t {
  1743.   char xmm_reg[16];
  1744. };
  1745.  
  1746. struct x86_float_state64_t {
  1747.   int32_t fpu_reserved[2];
  1748.   fp_control_t fpu_fcw;
  1749.   fp_status_t fpu_fsw;
  1750.   uint8_t fpu_ftw;
  1751.   uint8_t fpu_rsrv1;
  1752.   uint16_t fpu_fop;
  1753.   uint32_t fpu_ip;
  1754.   uint16_t fpu_cs;
  1755.   uint16_t fpu_rsrv2;
  1756.   uint32_t fpu_dp;
  1757.   uint16_t fpu_ds;
  1758.   uint16_t fpu_rsrv3;
  1759.   uint32_t fpu_mxcsr;
  1760.   uint32_t fpu_mxcsrmask;
  1761.   mmst_reg_t fpu_stmm0;
  1762.   mmst_reg_t fpu_stmm1;
  1763.   mmst_reg_t fpu_stmm2;
  1764.   mmst_reg_t fpu_stmm3;
  1765.   mmst_reg_t fpu_stmm4;
  1766.   mmst_reg_t fpu_stmm5;
  1767.   mmst_reg_t fpu_stmm6;
  1768.   mmst_reg_t fpu_stmm7;
  1769.   xmm_reg_t fpu_xmm0;
  1770.   xmm_reg_t fpu_xmm1;
  1771.   xmm_reg_t fpu_xmm2;
  1772.   xmm_reg_t fpu_xmm3;
  1773.   xmm_reg_t fpu_xmm4;
  1774.   xmm_reg_t fpu_xmm5;
  1775.   xmm_reg_t fpu_xmm6;
  1776.   xmm_reg_t fpu_xmm7;
  1777.   xmm_reg_t fpu_xmm8;
  1778.   xmm_reg_t fpu_xmm9;
  1779.   xmm_reg_t fpu_xmm10;
  1780.   xmm_reg_t fpu_xmm11;
  1781.   xmm_reg_t fpu_xmm12;
  1782.   xmm_reg_t fpu_xmm13;
  1783.   xmm_reg_t fpu_xmm14;
  1784.   xmm_reg_t fpu_xmm15;
  1785.   char fpu_rsrv4[6 * 16];
  1786.   uint32_t fpu_reserved1;
  1787. };
  1788.  
  1789. struct x86_exception_state64_t {
  1790.   uint16_t trapno;
  1791.   uint16_t cpu;
  1792.   uint32_t err;
  1793.   uint64_t faultvaddr;
  1794. };
  1795.  
  1796. inline void swapStruct(x86_thread_state32_t &x) {
  1797.   sys::swapByteOrder(x.eax);
  1798.   sys::swapByteOrder(x.ebx);
  1799.   sys::swapByteOrder(x.ecx);
  1800.   sys::swapByteOrder(x.edx);
  1801.   sys::swapByteOrder(x.edi);
  1802.   sys::swapByteOrder(x.esi);
  1803.   sys::swapByteOrder(x.ebp);
  1804.   sys::swapByteOrder(x.esp);
  1805.   sys::swapByteOrder(x.ss);
  1806.   sys::swapByteOrder(x.eflags);
  1807.   sys::swapByteOrder(x.eip);
  1808.   sys::swapByteOrder(x.cs);
  1809.   sys::swapByteOrder(x.ds);
  1810.   sys::swapByteOrder(x.es);
  1811.   sys::swapByteOrder(x.fs);
  1812.   sys::swapByteOrder(x.gs);
  1813. }
  1814.  
  1815. inline void swapStruct(x86_thread_state64_t &x) {
  1816.   sys::swapByteOrder(x.rax);
  1817.   sys::swapByteOrder(x.rbx);
  1818.   sys::swapByteOrder(x.rcx);
  1819.   sys::swapByteOrder(x.rdx);
  1820.   sys::swapByteOrder(x.rdi);
  1821.   sys::swapByteOrder(x.rsi);
  1822.   sys::swapByteOrder(x.rbp);
  1823.   sys::swapByteOrder(x.rsp);
  1824.   sys::swapByteOrder(x.r8);
  1825.   sys::swapByteOrder(x.r9);
  1826.   sys::swapByteOrder(x.r10);
  1827.   sys::swapByteOrder(x.r11);
  1828.   sys::swapByteOrder(x.r12);
  1829.   sys::swapByteOrder(x.r13);
  1830.   sys::swapByteOrder(x.r14);
  1831.   sys::swapByteOrder(x.r15);
  1832.   sys::swapByteOrder(x.rip);
  1833.   sys::swapByteOrder(x.rflags);
  1834.   sys::swapByteOrder(x.cs);
  1835.   sys::swapByteOrder(x.fs);
  1836.   sys::swapByteOrder(x.gs);
  1837. }
  1838.  
  1839. inline void swapStruct(x86_float_state64_t &x) {
  1840.   sys::swapByteOrder(x.fpu_reserved[0]);
  1841.   sys::swapByteOrder(x.fpu_reserved[1]);
  1842.   // TODO swap: fp_control_t fpu_fcw;
  1843.   // TODO swap: fp_status_t fpu_fsw;
  1844.   sys::swapByteOrder(x.fpu_fop);
  1845.   sys::swapByteOrder(x.fpu_ip);
  1846.   sys::swapByteOrder(x.fpu_cs);
  1847.   sys::swapByteOrder(x.fpu_rsrv2);
  1848.   sys::swapByteOrder(x.fpu_dp);
  1849.   sys::swapByteOrder(x.fpu_ds);
  1850.   sys::swapByteOrder(x.fpu_rsrv3);
  1851.   sys::swapByteOrder(x.fpu_mxcsr);
  1852.   sys::swapByteOrder(x.fpu_mxcsrmask);
  1853.   sys::swapByteOrder(x.fpu_reserved1);
  1854. }
  1855.  
  1856. inline void swapStruct(x86_exception_state64_t &x) {
  1857.   sys::swapByteOrder(x.trapno);
  1858.   sys::swapByteOrder(x.cpu);
  1859.   sys::swapByteOrder(x.err);
  1860.   sys::swapByteOrder(x.faultvaddr);
  1861. }
  1862.  
  1863. struct x86_state_hdr_t {
  1864.   uint32_t flavor;
  1865.   uint32_t count;
  1866. };
  1867.  
  1868. struct x86_thread_state_t {
  1869.   x86_state_hdr_t tsh;
  1870.   union {
  1871.     x86_thread_state64_t ts64;
  1872.     x86_thread_state32_t ts32;
  1873.   } uts;
  1874. };
  1875.  
  1876. struct x86_float_state_t {
  1877.   x86_state_hdr_t fsh;
  1878.   union {
  1879.     x86_float_state64_t fs64;
  1880.   } ufs;
  1881. };
  1882.  
  1883. struct x86_exception_state_t {
  1884.   x86_state_hdr_t esh;
  1885.   union {
  1886.     x86_exception_state64_t es64;
  1887.   } ues;
  1888. };
  1889.  
  1890. inline void swapStruct(x86_state_hdr_t &x) {
  1891.   sys::swapByteOrder(x.flavor);
  1892.   sys::swapByteOrder(x.count);
  1893. }
  1894.  
  1895. enum X86ThreadFlavors {
  1896.   x86_THREAD_STATE32 = 1,
  1897.   x86_FLOAT_STATE32 = 2,
  1898.   x86_EXCEPTION_STATE32 = 3,
  1899.   x86_THREAD_STATE64 = 4,
  1900.   x86_FLOAT_STATE64 = 5,
  1901.   x86_EXCEPTION_STATE64 = 6,
  1902.   x86_THREAD_STATE = 7,
  1903.   x86_FLOAT_STATE = 8,
  1904.   x86_EXCEPTION_STATE = 9,
  1905.   x86_DEBUG_STATE32 = 10,
  1906.   x86_DEBUG_STATE64 = 11,
  1907.   x86_DEBUG_STATE = 12
  1908. };
  1909.  
  1910. inline void swapStruct(x86_thread_state_t &x) {
  1911.   swapStruct(x.tsh);
  1912.   if (x.tsh.flavor == x86_THREAD_STATE64)
  1913.     swapStruct(x.uts.ts64);
  1914. }
  1915.  
  1916. inline void swapStruct(x86_float_state_t &x) {
  1917.   swapStruct(x.fsh);
  1918.   if (x.fsh.flavor == x86_FLOAT_STATE64)
  1919.     swapStruct(x.ufs.fs64);
  1920. }
  1921.  
  1922. inline void swapStruct(x86_exception_state_t &x) {
  1923.   swapStruct(x.esh);
  1924.   if (x.esh.flavor == x86_EXCEPTION_STATE64)
  1925.     swapStruct(x.ues.es64);
  1926. }
  1927.  
  1928. const uint32_t x86_THREAD_STATE32_COUNT =
  1929.     sizeof(x86_thread_state32_t) / sizeof(uint32_t);
  1930.  
  1931. const uint32_t x86_THREAD_STATE64_COUNT =
  1932.     sizeof(x86_thread_state64_t) / sizeof(uint32_t);
  1933. const uint32_t x86_FLOAT_STATE64_COUNT =
  1934.     sizeof(x86_float_state64_t) / sizeof(uint32_t);
  1935. const uint32_t x86_EXCEPTION_STATE64_COUNT =
  1936.     sizeof(x86_exception_state64_t) / sizeof(uint32_t);
  1937.  
  1938. const uint32_t x86_THREAD_STATE_COUNT =
  1939.     sizeof(x86_thread_state_t) / sizeof(uint32_t);
  1940. const uint32_t x86_FLOAT_STATE_COUNT =
  1941.     sizeof(x86_float_state_t) / sizeof(uint32_t);
  1942. const uint32_t x86_EXCEPTION_STATE_COUNT =
  1943.     sizeof(x86_exception_state_t) / sizeof(uint32_t);
  1944.  
  1945. struct arm_thread_state32_t {
  1946.   uint32_t r[13];
  1947.   uint32_t sp;
  1948.   uint32_t lr;
  1949.   uint32_t pc;
  1950.   uint32_t cpsr;
  1951. };
  1952.  
  1953. inline void swapStruct(arm_thread_state32_t &x) {
  1954.   for (int i = 0; i < 13; i++)
  1955.     sys::swapByteOrder(x.r[i]);
  1956.   sys::swapByteOrder(x.sp);
  1957.   sys::swapByteOrder(x.lr);
  1958.   sys::swapByteOrder(x.pc);
  1959.   sys::swapByteOrder(x.cpsr);
  1960. }
  1961.  
  1962. struct arm_thread_state64_t {
  1963.   uint64_t x[29];
  1964.   uint64_t fp;
  1965.   uint64_t lr;
  1966.   uint64_t sp;
  1967.   uint64_t pc;
  1968.   uint32_t cpsr;
  1969.   uint32_t pad;
  1970. };
  1971.  
  1972. inline void swapStruct(arm_thread_state64_t &x) {
  1973.   for (int i = 0; i < 29; i++)
  1974.     sys::swapByteOrder(x.x[i]);
  1975.   sys::swapByteOrder(x.fp);
  1976.   sys::swapByteOrder(x.lr);
  1977.   sys::swapByteOrder(x.sp);
  1978.   sys::swapByteOrder(x.pc);
  1979.   sys::swapByteOrder(x.cpsr);
  1980. }
  1981.  
  1982. struct arm_state_hdr_t {
  1983.   uint32_t flavor;
  1984.   uint32_t count;
  1985. };
  1986.  
  1987. struct arm_thread_state_t {
  1988.   arm_state_hdr_t tsh;
  1989.   union {
  1990.     arm_thread_state32_t ts32;
  1991.   } uts;
  1992. };
  1993.  
  1994. inline void swapStruct(arm_state_hdr_t &x) {
  1995.   sys::swapByteOrder(x.flavor);
  1996.   sys::swapByteOrder(x.count);
  1997. }
  1998.  
  1999. enum ARMThreadFlavors {
  2000.   ARM_THREAD_STATE = 1,
  2001.   ARM_VFP_STATE = 2,
  2002.   ARM_EXCEPTION_STATE = 3,
  2003.   ARM_DEBUG_STATE = 4,
  2004.   ARN_THREAD_STATE_NONE = 5,
  2005.   ARM_THREAD_STATE64 = 6,
  2006.   ARM_EXCEPTION_STATE64 = 7
  2007. };
  2008.  
  2009. inline void swapStruct(arm_thread_state_t &x) {
  2010.   swapStruct(x.tsh);
  2011.   if (x.tsh.flavor == ARM_THREAD_STATE)
  2012.     swapStruct(x.uts.ts32);
  2013. }
  2014.  
  2015. const uint32_t ARM_THREAD_STATE_COUNT =
  2016.     sizeof(arm_thread_state32_t) / sizeof(uint32_t);
  2017.  
  2018. const uint32_t ARM_THREAD_STATE64_COUNT =
  2019.     sizeof(arm_thread_state64_t) / sizeof(uint32_t);
  2020.  
  2021. struct ppc_thread_state32_t {
  2022.   uint32_t srr0;
  2023.   uint32_t srr1;
  2024.   uint32_t r0;
  2025.   uint32_t r1;
  2026.   uint32_t r2;
  2027.   uint32_t r3;
  2028.   uint32_t r4;
  2029.   uint32_t r5;
  2030.   uint32_t r6;
  2031.   uint32_t r7;
  2032.   uint32_t r8;
  2033.   uint32_t r9;
  2034.   uint32_t r10;
  2035.   uint32_t r11;
  2036.   uint32_t r12;
  2037.   uint32_t r13;
  2038.   uint32_t r14;
  2039.   uint32_t r15;
  2040.   uint32_t r16;
  2041.   uint32_t r17;
  2042.   uint32_t r18;
  2043.   uint32_t r19;
  2044.   uint32_t r20;
  2045.   uint32_t r21;
  2046.   uint32_t r22;
  2047.   uint32_t r23;
  2048.   uint32_t r24;
  2049.   uint32_t r25;
  2050.   uint32_t r26;
  2051.   uint32_t r27;
  2052.   uint32_t r28;
  2053.   uint32_t r29;
  2054.   uint32_t r30;
  2055.   uint32_t r31;
  2056.   uint32_t ct;
  2057.   uint32_t xer;
  2058.   uint32_t lr;
  2059.   uint32_t ctr;
  2060.   uint32_t mq;
  2061.   uint32_t vrsave;
  2062. };
  2063.  
  2064. inline void swapStruct(ppc_thread_state32_t &x) {
  2065.   sys::swapByteOrder(x.srr0);
  2066.   sys::swapByteOrder(x.srr1);
  2067.   sys::swapByteOrder(x.r0);
  2068.   sys::swapByteOrder(x.r1);
  2069.   sys::swapByteOrder(x.r2);
  2070.   sys::swapByteOrder(x.r3);
  2071.   sys::swapByteOrder(x.r4);
  2072.   sys::swapByteOrder(x.r5);
  2073.   sys::swapByteOrder(x.r6);
  2074.   sys::swapByteOrder(x.r7);
  2075.   sys::swapByteOrder(x.r8);
  2076.   sys::swapByteOrder(x.r9);
  2077.   sys::swapByteOrder(x.r10);
  2078.   sys::swapByteOrder(x.r11);
  2079.   sys::swapByteOrder(x.r12);
  2080.   sys::swapByteOrder(x.r13);
  2081.   sys::swapByteOrder(x.r14);
  2082.   sys::swapByteOrder(x.r15);
  2083.   sys::swapByteOrder(x.r16);
  2084.   sys::swapByteOrder(x.r17);
  2085.   sys::swapByteOrder(x.r18);
  2086.   sys::swapByteOrder(x.r19);
  2087.   sys::swapByteOrder(x.r20);
  2088.   sys::swapByteOrder(x.r21);
  2089.   sys::swapByteOrder(x.r22);
  2090.   sys::swapByteOrder(x.r23);
  2091.   sys::swapByteOrder(x.r24);
  2092.   sys::swapByteOrder(x.r25);
  2093.   sys::swapByteOrder(x.r26);
  2094.   sys::swapByteOrder(x.r27);
  2095.   sys::swapByteOrder(x.r28);
  2096.   sys::swapByteOrder(x.r29);
  2097.   sys::swapByteOrder(x.r30);
  2098.   sys::swapByteOrder(x.r31);
  2099.   sys::swapByteOrder(x.ct);
  2100.   sys::swapByteOrder(x.xer);
  2101.   sys::swapByteOrder(x.lr);
  2102.   sys::swapByteOrder(x.ctr);
  2103.   sys::swapByteOrder(x.mq);
  2104.   sys::swapByteOrder(x.vrsave);
  2105. }
  2106.  
  2107. struct ppc_state_hdr_t {
  2108.   uint32_t flavor;
  2109.   uint32_t count;
  2110. };
  2111.  
  2112. struct ppc_thread_state_t {
  2113.   ppc_state_hdr_t tsh;
  2114.   union {
  2115.     ppc_thread_state32_t ts32;
  2116.   } uts;
  2117. };
  2118.  
  2119. inline void swapStruct(ppc_state_hdr_t &x) {
  2120.   sys::swapByteOrder(x.flavor);
  2121.   sys::swapByteOrder(x.count);
  2122. }
  2123.  
  2124. enum PPCThreadFlavors {
  2125.   PPC_THREAD_STATE = 1,
  2126.   PPC_FLOAT_STATE = 2,
  2127.   PPC_EXCEPTION_STATE = 3,
  2128.   PPC_VECTOR_STATE = 4,
  2129.   PPC_THREAD_STATE64 = 5,
  2130.   PPC_EXCEPTION_STATE64 = 6,
  2131.   PPC_THREAD_STATE_NONE = 7
  2132. };
  2133.  
  2134. inline void swapStruct(ppc_thread_state_t &x) {
  2135.   swapStruct(x.tsh);
  2136.   if (x.tsh.flavor == PPC_THREAD_STATE)
  2137.     swapStruct(x.uts.ts32);
  2138. }
  2139.  
  2140. const uint32_t PPC_THREAD_STATE_COUNT =
  2141.     sizeof(ppc_thread_state32_t) / sizeof(uint32_t);
  2142.  
  2143. // Define a union of all load command structs
  2144. #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
  2145.  
  2146. LLVM_PACKED_START
  2147. union alignas(4) macho_load_command {
  2148. #include "llvm/BinaryFormat/MachO.def"
  2149. };
  2150. LLVM_PACKED_END
  2151.  
  2152. inline void swapStruct(dyld_chained_fixups_header &C) {
  2153.   sys::swapByteOrder(C.fixups_version);
  2154.   sys::swapByteOrder(C.starts_offset);
  2155.   sys::swapByteOrder(C.imports_offset);
  2156.   sys::swapByteOrder(C.symbols_offset);
  2157.   sys::swapByteOrder(C.imports_count);
  2158.   sys::swapByteOrder(C.imports_format);
  2159.   sys::swapByteOrder(C.symbols_format);
  2160. }
  2161.  
  2162. inline void swapStruct(dyld_chained_starts_in_image &C) {
  2163.   sys::swapByteOrder(C.seg_count);
  2164.   // getStructOrErr() cannot copy the variable-length seg_info_offset array.
  2165.   // Its elements must be byte swapped manually.
  2166. }
  2167.  
  2168. inline void swapStruct(dyld_chained_starts_in_segment &C) {
  2169.   sys::swapByteOrder(C.size);
  2170.   sys::swapByteOrder(C.page_size);
  2171.   sys::swapByteOrder(C.pointer_format);
  2172.   sys::swapByteOrder(C.segment_offset);
  2173.   sys::swapByteOrder(C.max_valid_pointer);
  2174.   sys::swapByteOrder(C.page_count);
  2175.   // seg_info_offset entries must be byte swapped manually.
  2176. }
  2177.  
  2178. /* code signing attributes of a process */
  2179.  
  2180. enum CodeSignAttrs {
  2181.   CS_VALID = 0x00000001,          /* dynamically valid */
  2182.   CS_ADHOC = 0x00000002,          /* ad hoc signed */
  2183.   CS_GET_TASK_ALLOW = 0x00000004, /* has get-task-allow entitlement */
  2184.   CS_INSTALLER = 0x00000008,      /* has installer entitlement */
  2185.  
  2186.   CS_FORCED_LV =
  2187.       0x00000010, /* Library Validation required by Hardened System Policy */
  2188.   CS_INVALID_ALLOWED = 0x00000020, /* (macOS Only) Page invalidation allowed by
  2189.                                       task port policy */
  2190.  
  2191.   CS_HARD = 0x00000100,             /* don't load invalid pages */
  2192.   CS_KILL = 0x00000200,             /* kill process if it becomes invalid */
  2193.   CS_CHECK_EXPIRATION = 0x00000400, /* force expiration checking */
  2194.   CS_RESTRICT = 0x00000800,         /* tell dyld to treat restricted */
  2195.  
  2196.   CS_ENFORCEMENT = 0x00001000, /* require enforcement */
  2197.   CS_REQUIRE_LV = 0x00002000,  /* require library validation */
  2198.   CS_ENTITLEMENTS_VALIDATED =
  2199.       0x00004000, /* code signature permits restricted entitlements */
  2200.   CS_NVRAM_UNRESTRICTED =
  2201.       0x00008000, /* has com.apple.rootless.restricted-nvram-variables.heritable
  2202.                      entitlement */
  2203.  
  2204.   CS_RUNTIME = 0x00010000,       /* Apply hardened runtime policies */
  2205.   CS_LINKER_SIGNED = 0x00020000, /* Automatically signed by the linker */
  2206.  
  2207.   CS_ALLOWED_MACHO =
  2208.       (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | CS_RESTRICT |
  2209.        CS_ENFORCEMENT | CS_REQUIRE_LV | CS_RUNTIME | CS_LINKER_SIGNED),
  2210.  
  2211.   CS_EXEC_SET_HARD = 0x00100000, /* set CS_HARD on any exec'ed process */
  2212.   CS_EXEC_SET_KILL = 0x00200000, /* set CS_KILL on any exec'ed process */
  2213.   CS_EXEC_SET_ENFORCEMENT =
  2214.       0x00400000, /* set CS_ENFORCEMENT on any exec'ed process */
  2215.   CS_EXEC_INHERIT_SIP =
  2216.       0x00800000, /* set CS_INSTALLER on any exec'ed process */
  2217.  
  2218.   CS_KILLED = 0x01000000, /* was killed by kernel for invalidity */
  2219.   CS_DYLD_PLATFORM =
  2220.       0x02000000, /* dyld used to load this is a platform binary */
  2221.   CS_PLATFORM_BINARY = 0x04000000, /* this is a platform binary */
  2222.   CS_PLATFORM_PATH =
  2223.       0x08000000, /* platform binary by the fact of path (osx only) */
  2224.  
  2225.   CS_DEBUGGED = 0x10000000, /* process is currently or has previously been
  2226.                 debugged and allowed to run with invalid pages */
  2227.   CS_SIGNED = 0x20000000, /* process has a signature (may have gone invalid) */
  2228.   CS_DEV_CODE =
  2229.       0x40000000, /* code is dev signed, cannot be loaded into prod signed code
  2230.                      (will go away with rdar://problem/28322552) */
  2231.   CS_DATAVAULT_CONTROLLER =
  2232.       0x80000000, /* has Data Vault controller entitlement */
  2233.  
  2234.   CS_ENTITLEMENT_FLAGS = (CS_GET_TASK_ALLOW | CS_INSTALLER |
  2235.                           CS_DATAVAULT_CONTROLLER | CS_NVRAM_UNRESTRICTED),
  2236. };
  2237.  
  2238. /* executable segment flags */
  2239.  
  2240. enum CodeSignExecSegFlags {
  2241.  
  2242.   CS_EXECSEG_MAIN_BINARY = 0x1,     /* executable segment denotes main binary */
  2243.   CS_EXECSEG_ALLOW_UNSIGNED = 0x10, /* allow unsigned pages (for debugging) */
  2244.   CS_EXECSEG_DEBUGGER = 0x20,       /* main binary is debugger */
  2245.   CS_EXECSEG_JIT = 0x40,            /* JIT enabled */
  2246.   CS_EXECSEG_SKIP_LV = 0x80,        /* OBSOLETE: skip library validation */
  2247.   CS_EXECSEG_CAN_LOAD_CDHASH = 0x100, /* can bless cdhash for execution */
  2248.   CS_EXECSEG_CAN_EXEC_CDHASH = 0x200, /* can execute blessed cdhash */
  2249.  
  2250. };
  2251.  
  2252. /* Magic numbers used by Code Signing */
  2253.  
  2254. enum CodeSignMagic {
  2255.   CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
  2256.   CSMAGIC_REQUIREMENTS =
  2257.       0xfade0c01, /* Requirements vector (internal requirements) */
  2258.   CSMAGIC_CODEDIRECTORY = 0xfade0c02,      /* CodeDirectory blob */
  2259.   CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
  2260.   CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
  2261.   CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171,  /* embedded entitlements */
  2262.   CSMAGIC_DETACHED_SIGNATURE =
  2263.       0xfade0cc1, /* multi-arch collection of embedded signatures */
  2264.   CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
  2265.  
  2266.   CS_SUPPORTSSCATTER = 0x20100,
  2267.   CS_SUPPORTSTEAMID = 0x20200,
  2268.   CS_SUPPORTSCODELIMIT64 = 0x20300,
  2269.   CS_SUPPORTSEXECSEG = 0x20400,
  2270.   CS_SUPPORTSRUNTIME = 0x20500,
  2271.   CS_SUPPORTSLINKAGE = 0x20600,
  2272.  
  2273.   CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
  2274.   CSSLOT_INFOSLOT = 1,
  2275.   CSSLOT_REQUIREMENTS = 2,
  2276.   CSSLOT_RESOURCEDIR = 3,
  2277.   CSSLOT_APPLICATION = 4,
  2278.   CSSLOT_ENTITLEMENTS = 5,
  2279.  
  2280.   CSSLOT_ALTERNATE_CODEDIRECTORIES =
  2281.       0x1000, /* first alternate CodeDirectory, if any */
  2282.   CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5, /* max number of alternate CD slots */
  2283.   CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT =
  2284.       CSSLOT_ALTERNATE_CODEDIRECTORIES +
  2285.       CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
  2286.  
  2287.   CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
  2288.   CSSLOT_IDENTIFICATIONSLOT = 0x10001,
  2289.   CSSLOT_TICKETSLOT = 0x10002,
  2290.  
  2291.   CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
  2292.   CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
  2293.  
  2294.   CS_HASHTYPE_SHA1 = 1,
  2295.   CS_HASHTYPE_SHA256 = 2,
  2296.   CS_HASHTYPE_SHA256_TRUNCATED = 3,
  2297.   CS_HASHTYPE_SHA384 = 4,
  2298.  
  2299.   CS_SHA1_LEN = 20,
  2300.   CS_SHA256_LEN = 32,
  2301.   CS_SHA256_TRUNCATED_LEN = 20,
  2302.  
  2303.   CS_CDHASH_LEN = 20,    /* always - larger hashes are truncated */
  2304.   CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
  2305.  
  2306.   /*
  2307.    * Currently only to support Legacy VPN plugins, and Mac App Store
  2308.    * but intended to replace all the various platform code, dev code etc. bits.
  2309.    */
  2310.   CS_SIGNER_TYPE_UNKNOWN = 0,
  2311.   CS_SIGNER_TYPE_LEGACYVPN = 5,
  2312.   CS_SIGNER_TYPE_MAC_APP_STORE = 6,
  2313.  
  2314.   CS_SUPPL_SIGNER_TYPE_UNKNOWN = 0,
  2315.   CS_SUPPL_SIGNER_TYPE_TRUSTCACHE = 7,
  2316.   CS_SUPPL_SIGNER_TYPE_LOCAL = 8,
  2317. };
  2318.  
  2319. struct CS_CodeDirectory {
  2320.   uint32_t magic;         /* magic number (CSMAGIC_CODEDIRECTORY) */
  2321.   uint32_t length;        /* total length of CodeDirectory blob */
  2322.   uint32_t version;       /* compatibility version */
  2323.   uint32_t flags;         /* setup and mode flags */
  2324.   uint32_t hashOffset;    /* offset of hash slot element at index zero */
  2325.   uint32_t identOffset;   /* offset of identifier string */
  2326.   uint32_t nSpecialSlots; /* number of special hash slots */
  2327.   uint32_t nCodeSlots;    /* number of ordinary (code) hash slots */
  2328.   uint32_t codeLimit;     /* limit to main image signature range */
  2329.   uint8_t hashSize;       /* size of each hash in bytes */
  2330.   uint8_t hashType;       /* type of hash (cdHashType* constants) */
  2331.   uint8_t platform;       /* platform identifier; zero if not platform binary */
  2332.   uint8_t pageSize;       /* log2(page size in bytes); 0 => infinite */
  2333.   uint32_t spare2;        /* unused (must be zero) */
  2334.  
  2335.   /* Version 0x20100 */
  2336.   uint32_t scatterOffset; /* offset of optional scatter vector */
  2337.  
  2338.   /* Version 0x20200 */
  2339.   uint32_t teamOffset; /* offset of optional team identifier */
  2340.  
  2341.   /* Version 0x20300 */
  2342.   uint32_t spare3;      /* unused (must be zero) */
  2343.   uint64_t codeLimit64; /* limit to main image signature range, 64 bits */
  2344.  
  2345.   /* Version 0x20400 */
  2346.   uint64_t execSegBase;  /* offset of executable segment */
  2347.   uint64_t execSegLimit; /* limit of executable segment */
  2348.   uint64_t execSegFlags; /* executable segment flags */
  2349. };
  2350.  
  2351. static_assert(sizeof(CS_CodeDirectory) == 88);
  2352.  
  2353. struct CS_BlobIndex {
  2354.   uint32_t type;   /* type of entry */
  2355.   uint32_t offset; /* offset of entry */
  2356. };
  2357.  
  2358. struct CS_SuperBlob {
  2359.   uint32_t magic;  /* magic number */
  2360.   uint32_t length; /* total length of SuperBlob */
  2361.   uint32_t count;  /* number of index entries following */
  2362.   /* followed by Blobs in no particular order as indicated by index offsets */
  2363. };
  2364.  
  2365. enum SecCSDigestAlgorithm {
  2366.   kSecCodeSignatureNoHash = 0,     /* null value */
  2367.   kSecCodeSignatureHashSHA1 = 1,   /* SHA-1 */
  2368.   kSecCodeSignatureHashSHA256 = 2, /* SHA-256 */
  2369.   kSecCodeSignatureHashSHA256Truncated =
  2370.       3,                           /* SHA-256 truncated to first 20 bytes */
  2371.   kSecCodeSignatureHashSHA384 = 4, /* SHA-384 */
  2372.   kSecCodeSignatureHashSHA512 = 5, /* SHA-512 */
  2373. };
  2374.  
  2375. enum LinkerOptimizationHintKind {
  2376.   LOH_ARM64_ADRP_ADRP = 1,
  2377.   LOH_ARM64_ADRP_LDR = 2,
  2378.   LOH_ARM64_ADRP_ADD_LDR = 3,
  2379.   LOH_ARM64_ADRP_LDR_GOT_LDR = 4,
  2380.   LOH_ARM64_ADRP_ADD_STR = 5,
  2381.   LOH_ARM64_ADRP_LDR_GOT_STR = 6,
  2382.   LOH_ARM64_ADRP_ADD = 7,
  2383.   LOH_ARM64_ADRP_LDR_GOT = 8,
  2384. };
  2385.  
  2386. } // end namespace MachO
  2387. } // end namespace llvm
  2388.  
  2389. #endif
  2390.