Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:22

0001 //===-- llvm/BinaryFormat/MachO.h - The MachO file format -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines manifest constants for the MachO object file format.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_BINARYFORMAT_MACHO_H
0014 #define LLVM_BINARYFORMAT_MACHO_H
0015 
0016 #include "llvm/Support/Compiler.h"
0017 #include "llvm/Support/DataTypes.h"
0018 #include "llvm/Support/Error.h"
0019 #include "llvm/Support/SwapByteOrder.h"
0020 
0021 namespace llvm {
0022 
0023 class Triple;
0024 
0025 namespace MachO {
0026 // Enums from <mach-o/loader.h>
0027 enum : uint32_t {
0028   // Constants for the "magic" field in llvm::MachO::mach_header and
0029   // llvm::MachO::mach_header_64
0030   MH_MAGIC = 0xFEEDFACEu,
0031   MH_CIGAM = 0xCEFAEDFEu,
0032   MH_MAGIC_64 = 0xFEEDFACFu,
0033   MH_CIGAM_64 = 0xCFFAEDFEu,
0034   FAT_MAGIC = 0xCAFEBABEu,
0035   FAT_CIGAM = 0xBEBAFECAu,
0036   FAT_MAGIC_64 = 0xCAFEBABFu,
0037   FAT_CIGAM_64 = 0xBFBAFECAu
0038 };
0039 
0040 enum HeaderFileType {
0041   // Constants for the "filetype" field in llvm::MachO::mach_header and
0042   // llvm::MachO::mach_header_64
0043   MH_OBJECT = 0x1u,
0044   MH_EXECUTE = 0x2u,
0045   MH_FVMLIB = 0x3u,
0046   MH_CORE = 0x4u,
0047   MH_PRELOAD = 0x5u,
0048   MH_DYLIB = 0x6u,
0049   MH_DYLINKER = 0x7u,
0050   MH_BUNDLE = 0x8u,
0051   MH_DYLIB_STUB = 0x9u,
0052   MH_DSYM = 0xAu,
0053   MH_KEXT_BUNDLE = 0xBu,
0054   MH_FILESET = 0xCu,
0055 };
0056 
0057 enum {
0058   // Constant bits for the "flags" field in llvm::MachO::mach_header and
0059   // llvm::MachO::mach_header_64
0060   MH_NOUNDEFS = 0x00000001u,
0061   MH_INCRLINK = 0x00000002u,
0062   MH_DYLDLINK = 0x00000004u,
0063   MH_BINDATLOAD = 0x00000008u,
0064   MH_PREBOUND = 0x00000010u,
0065   MH_SPLIT_SEGS = 0x00000020u,
0066   MH_LAZY_INIT = 0x00000040u,
0067   MH_TWOLEVEL = 0x00000080u,
0068   MH_FORCE_FLAT = 0x00000100u,
0069   MH_NOMULTIDEFS = 0x00000200u,
0070   MH_NOFIXPREBINDING = 0x00000400u,
0071   MH_PREBINDABLE = 0x00000800u,
0072   MH_ALLMODSBOUND = 0x00001000u,
0073   MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
0074   MH_CANONICAL = 0x00004000u,
0075   MH_WEAK_DEFINES = 0x00008000u,
0076   MH_BINDS_TO_WEAK = 0x00010000u,
0077   MH_ALLOW_STACK_EXECUTION = 0x00020000u,
0078   MH_ROOT_SAFE = 0x00040000u,
0079   MH_SETUID_SAFE = 0x00080000u,
0080   MH_NO_REEXPORTED_DYLIBS = 0x00100000u,
0081   MH_PIE = 0x00200000u,
0082   MH_DEAD_STRIPPABLE_DYLIB = 0x00400000u,
0083   MH_HAS_TLV_DESCRIPTORS = 0x00800000u,
0084   MH_NO_HEAP_EXECUTION = 0x01000000u,
0085   MH_APP_EXTENSION_SAFE = 0x02000000u,
0086   MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000u,
0087   MH_SIM_SUPPORT = 0x08000000u,
0088   MH_DYLIB_IN_CACHE = 0x80000000u,
0089 };
0090 
0091 enum : uint32_t {
0092   // Flags for the "cmd" field in llvm::MachO::load_command
0093   LC_REQ_DYLD = 0x80000000u
0094 };
0095 
0096 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) LCName = LCValue,
0097 
0098 enum LoadCommandType : uint32_t {
0099 #include "llvm/BinaryFormat/MachO.def"
0100 };
0101 
0102 #undef HANDLE_LOAD_COMMAND
0103 
0104 enum : uint32_t {
0105   // Constant bits for the "flags" field in llvm::MachO::segment_command
0106   SG_HIGHVM = 0x1u,
0107   SG_FVMLIB = 0x2u,
0108   SG_NORELOC = 0x4u,
0109   SG_PROTECTED_VERSION_1 = 0x8u,
0110   SG_READ_ONLY = 0x10u,
0111 
0112   // Constant masks for the "flags" field in llvm::MachO::section and
0113   // llvm::MachO::section_64
0114   SECTION_TYPE = 0x000000ffu,           // SECTION_TYPE
0115   SECTION_ATTRIBUTES = 0xffffff00u,     // SECTION_ATTRIBUTES
0116   SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
0117   SECTION_ATTRIBUTES_SYS = 0x00ffff00u  // SECTION_ATTRIBUTES_SYS
0118 };
0119 
0120 /// These are the section type and attributes fields.  A MachO section can
0121 /// have only one Type, but can have any of the attributes specified.
0122 enum SectionType : uint32_t {
0123   // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
0124   // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
0125 
0126   /// S_REGULAR - Regular section.
0127   S_REGULAR = 0x00u,
0128   /// S_ZEROFILL - Zero fill on demand section.
0129   S_ZEROFILL = 0x01u,
0130   /// S_CSTRING_LITERALS - Section with literal C strings.
0131   S_CSTRING_LITERALS = 0x02u,
0132   /// S_4BYTE_LITERALS - Section with 4 byte literals.
0133   S_4BYTE_LITERALS = 0x03u,
0134   /// S_8BYTE_LITERALS - Section with 8 byte literals.
0135   S_8BYTE_LITERALS = 0x04u,
0136   /// S_LITERAL_POINTERS - Section with pointers to literals.
0137   S_LITERAL_POINTERS = 0x05u,
0138   /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
0139   S_NON_LAZY_SYMBOL_POINTERS = 0x06u,
0140   /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
0141   S_LAZY_SYMBOL_POINTERS = 0x07u,
0142   /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
0143   /// the Reserved2 field.
0144   S_SYMBOL_STUBS = 0x08u,
0145   /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
0146   /// initialization.
0147   S_MOD_INIT_FUNC_POINTERS = 0x09u,
0148   /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
0149   /// termination.
0150   S_MOD_TERM_FUNC_POINTERS = 0x0au,
0151   /// S_COALESCED - Section contains symbols that are to be coalesced.
0152   S_COALESCED = 0x0bu,
0153   /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
0154   /// gigabytes).
0155   S_GB_ZEROFILL = 0x0cu,
0156   /// S_INTERPOSING - Section with only pairs of function pointers for
0157   /// interposing.
0158   S_INTERPOSING = 0x0du,
0159   /// S_16BYTE_LITERALS - Section with only 16 byte literals.
0160   S_16BYTE_LITERALS = 0x0eu,
0161   /// S_DTRACE_DOF - Section contains DTrace Object Format.
0162   S_DTRACE_DOF = 0x0fu,
0163   /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
0164   /// lazy loaded dylibs.
0165   S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
0166   /// S_THREAD_LOCAL_REGULAR - Thread local data section.
0167   S_THREAD_LOCAL_REGULAR = 0x11u,
0168   /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
0169   S_THREAD_LOCAL_ZEROFILL = 0x12u,
0170   /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
0171   /// structure data.
0172   S_THREAD_LOCAL_VARIABLES = 0x13u,
0173   /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
0174   /// local structures.
0175   S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
0176   /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
0177   /// variable initialization pointers to functions.
0178   S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
0179   /// S_INIT_FUNC_OFFSETS - Section with 32-bit offsets to initializer
0180   /// functions.
0181   S_INIT_FUNC_OFFSETS = 0x16u,
0182 
0183   LAST_KNOWN_SECTION_TYPE = S_INIT_FUNC_OFFSETS
0184 };
0185 
0186 enum : uint32_t {
0187   // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
0188   // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
0189 
0190   /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
0191   /// instructions.
0192   S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
0193   /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
0194   /// in a ranlib table of contents.
0195   S_ATTR_NO_TOC = 0x40000000u,
0196   /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
0197   /// in files with the MY_DYLDLINK flag.
0198   S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
0199   /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
0200   S_ATTR_NO_DEAD_STRIP = 0x10000000u,
0201   /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
0202   S_ATTR_LIVE_SUPPORT = 0x08000000u,
0203   /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
0204   /// dyld.
0205   S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
0206   /// S_ATTR_DEBUG - A debug section.
0207   S_ATTR_DEBUG = 0x02000000u,
0208 
0209   // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
0210   // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
0211 
0212   /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
0213   S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
0214   /// S_ATTR_EXT_RELOC - Section has external relocation entries.
0215   S_ATTR_EXT_RELOC = 0x00000200u,
0216   /// S_ATTR_LOC_RELOC - Section has local relocation entries.
0217   S_ATTR_LOC_RELOC = 0x00000100u,
0218 
0219   // Constant masks for the value of an indirect symbol in an indirect
0220   // symbol table
0221   INDIRECT_SYMBOL_LOCAL = 0x80000000u,
0222   INDIRECT_SYMBOL_ABS = 0x40000000u
0223 };
0224 
0225 enum DataRegionType {
0226   // Constants for the "kind" field in a data_in_code_entry structure
0227   DICE_KIND_DATA = 1u,
0228   DICE_KIND_JUMP_TABLE8 = 2u,
0229   DICE_KIND_JUMP_TABLE16 = 3u,
0230   DICE_KIND_JUMP_TABLE32 = 4u,
0231   DICE_KIND_ABS_JUMP_TABLE32 = 5u
0232 };
0233 
0234 enum RebaseType {
0235   REBASE_TYPE_POINTER = 1u,
0236   REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
0237   REBASE_TYPE_TEXT_PCREL32 = 3u
0238 };
0239 
0240 enum { REBASE_OPCODE_MASK = 0xF0u, REBASE_IMMEDIATE_MASK = 0x0Fu };
0241 
0242 enum RebaseOpcode {
0243   REBASE_OPCODE_DONE = 0x00u,
0244   REBASE_OPCODE_SET_TYPE_IMM = 0x10u,
0245   REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
0246   REBASE_OPCODE_ADD_ADDR_ULEB = 0x30u,
0247   REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40u,
0248   REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50u,
0249   REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60u,
0250   REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70u,
0251   REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
0252 };
0253 
0254 enum BindType {
0255   BIND_TYPE_POINTER = 1u,
0256   BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
0257   BIND_TYPE_TEXT_PCREL32 = 3u
0258 };
0259 
0260 enum BindSpecialDylib {
0261   BIND_SPECIAL_DYLIB_SELF = 0,
0262   BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
0263   BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2,
0264   BIND_SPECIAL_DYLIB_WEAK_LOOKUP = -3
0265 };
0266 
0267 enum {
0268   BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1u,
0269   BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
0270 
0271   BIND_OPCODE_MASK = 0xF0u,
0272   BIND_IMMEDIATE_MASK = 0x0Fu
0273 };
0274 
0275 enum BindOpcode {
0276   BIND_OPCODE_DONE = 0x00u,
0277   BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10u,
0278   BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20u,
0279   BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30u,
0280   BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
0281   BIND_OPCODE_SET_TYPE_IMM = 0x50u,
0282   BIND_OPCODE_SET_ADDEND_SLEB = 0x60u,
0283   BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
0284   BIND_OPCODE_ADD_ADDR_ULEB = 0x80u,
0285   BIND_OPCODE_DO_BIND = 0x90u,
0286   BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0u,
0287   BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
0288   BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
0289 };
0290 
0291 enum {
0292   EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03u,
0293   EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04u,
0294   EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08u,
0295   EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10u
0296 };
0297 
0298 enum ExportSymbolKind {
0299   EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00u,
0300   EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01u,
0301   EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE = 0x02u
0302 };
0303 
0304 enum {
0305   // Constant masks for the "n_type" field in llvm::MachO::nlist and
0306   // llvm::MachO::nlist_64
0307   N_STAB = 0xe0,
0308   N_PEXT = 0x10,
0309   N_TYPE = 0x0e,
0310   N_EXT = 0x01
0311 };
0312 
0313 enum NListType : uint8_t {
0314   // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
0315   // llvm::MachO::nlist_64
0316   N_UNDF = 0x0u,
0317   N_ABS = 0x2u,
0318   N_SECT = 0xeu,
0319   N_PBUD = 0xcu,
0320   N_INDR = 0xau
0321 };
0322 
0323 enum SectionOrdinal {
0324   // Constants for the "n_sect" field in llvm::MachO::nlist and
0325   // llvm::MachO::nlist_64
0326   NO_SECT = 0u,
0327   MAX_SECT = 0xffu
0328 };
0329 
0330 enum {
0331   // Constant masks for the "n_desc" field in llvm::MachO::nlist and
0332   // llvm::MachO::nlist_64
0333   // The low 3 bits are the for the REFERENCE_TYPE.
0334   REFERENCE_TYPE = 0x7,
0335   REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
0336   REFERENCE_FLAG_UNDEFINED_LAZY = 1,
0337   REFERENCE_FLAG_DEFINED = 2,
0338   REFERENCE_FLAG_PRIVATE_DEFINED = 3,
0339   REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
0340   REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
0341   // Flag bits (some overlap with the library ordinal bits).
0342   N_ARM_THUMB_DEF = 0x0008u,
0343   REFERENCED_DYNAMICALLY = 0x0010u,
0344   N_NO_DEAD_STRIP = 0x0020u,
0345   N_WEAK_REF = 0x0040u,
0346   N_WEAK_DEF = 0x0080u,
0347   N_SYMBOL_RESOLVER = 0x0100u,
0348   N_ALT_ENTRY = 0x0200u,
0349   N_COLD_FUNC = 0x0400u,
0350   // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
0351   // as these are in the top 8 bits.
0352   SELF_LIBRARY_ORDINAL = 0x0,
0353   MAX_LIBRARY_ORDINAL = 0xfd,
0354   DYNAMIC_LOOKUP_ORDINAL = 0xfe,
0355   EXECUTABLE_ORDINAL = 0xff
0356 };
0357 
0358 enum StabType {
0359   // Constant values for the "n_type" field in llvm::MachO::nlist and
0360   // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
0361   N_GSYM = 0x20u,
0362   N_FNAME = 0x22u,
0363   N_FUN = 0x24u,
0364   N_STSYM = 0x26u,
0365   N_LCSYM = 0x28u,
0366   N_BNSYM = 0x2Eu,
0367   N_PC = 0x30u,
0368   N_AST = 0x32u,
0369   N_OPT = 0x3Cu,
0370   N_RSYM = 0x40u,
0371   N_SLINE = 0x44u,
0372   N_ENSYM = 0x4Eu,
0373   N_SSYM = 0x60u,
0374   N_SO = 0x64u,
0375   N_OSO = 0x66u,
0376   N_LIB = 0x68u,
0377   N_LSYM = 0x80u,
0378   N_BINCL = 0x82u,
0379   N_SOL = 0x84u,
0380   N_PARAMS = 0x86u,
0381   N_VERSION = 0x88u,
0382   N_OLEVEL = 0x8Au,
0383   N_PSYM = 0xA0u,
0384   N_EINCL = 0xA2u,
0385   N_ENTRY = 0xA4u,
0386   N_LBRAC = 0xC0u,
0387   N_EXCL = 0xC2u,
0388   N_RBRAC = 0xE0u,
0389   N_BCOMM = 0xE2u,
0390   N_ECOMM = 0xE4u,
0391   N_ECOML = 0xE8u,
0392   N_LENG = 0xFEu
0393 };
0394 
0395 enum : uint32_t {
0396   // Constant values for the r_symbolnum field in an
0397   // llvm::MachO::relocation_info structure when r_extern is 0.
0398   R_ABS = 0,
0399 
0400   // Constant bits for the r_address field in an
0401   // llvm::MachO::relocation_info structure.
0402   R_SCATTERED = 0x80000000
0403 };
0404 
0405 enum RelocationInfoType {
0406   // Constant values for the r_type field in an
0407   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
0408   // structure.
0409   GENERIC_RELOC_INVALID = 0xff,
0410   GENERIC_RELOC_VANILLA = 0,
0411   GENERIC_RELOC_PAIR = 1,
0412   GENERIC_RELOC_SECTDIFF = 2,
0413   GENERIC_RELOC_PB_LA_PTR = 3,
0414   GENERIC_RELOC_LOCAL_SECTDIFF = 4,
0415   GENERIC_RELOC_TLV = 5,
0416 
0417   // Constant values for the r_type field in a PowerPC architecture
0418   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
0419   // structure.
0420   PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
0421   PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
0422   PPC_RELOC_BR14 = 2,
0423   PPC_RELOC_BR24 = 3,
0424   PPC_RELOC_HI16 = 4,
0425   PPC_RELOC_LO16 = 5,
0426   PPC_RELOC_HA16 = 6,
0427   PPC_RELOC_LO14 = 7,
0428   PPC_RELOC_SECTDIFF = 8,
0429   PPC_RELOC_PB_LA_PTR = 9,
0430   PPC_RELOC_HI16_SECTDIFF = 10,
0431   PPC_RELOC_LO16_SECTDIFF = 11,
0432   PPC_RELOC_HA16_SECTDIFF = 12,
0433   PPC_RELOC_JBSR = 13,
0434   PPC_RELOC_LO14_SECTDIFF = 14,
0435   PPC_RELOC_LOCAL_SECTDIFF = 15,
0436 
0437   // Constant values for the r_type field in an ARM architecture
0438   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
0439   // structure.
0440   ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
0441   ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
0442   ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
0443   ARM_RELOC_LOCAL_SECTDIFF = 3,
0444   ARM_RELOC_PB_LA_PTR = 4,
0445   ARM_RELOC_BR24 = 5,
0446   ARM_THUMB_RELOC_BR22 = 6,
0447   ARM_THUMB_32BIT_BRANCH = 7, // obsolete
0448   ARM_RELOC_HALF = 8,
0449   ARM_RELOC_HALF_SECTDIFF = 9,
0450 
0451   // Constant values for the r_type field in an ARM64 architecture
0452   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
0453   // structure.
0454 
0455   // For pointers.
0456   ARM64_RELOC_UNSIGNED = 0,
0457   // Must be followed by an ARM64_RELOC_UNSIGNED
0458   ARM64_RELOC_SUBTRACTOR = 1,
0459   // A B/BL instruction with 26-bit displacement.
0460   ARM64_RELOC_BRANCH26 = 2,
0461   // PC-rel distance to page of target.
0462   ARM64_RELOC_PAGE21 = 3,
0463   // Offset within page, scaled by r_length.
0464   ARM64_RELOC_PAGEOFF12 = 4,
0465   // PC-rel distance to page of GOT slot.
0466   ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
0467   // Offset within page of GOT slot, scaled by r_length.
0468   ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
0469   // For pointers to GOT slots.
0470   ARM64_RELOC_POINTER_TO_GOT = 7,
0471   // PC-rel distance to page of TLVP slot.
0472   ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
0473   // Offset within page of TLVP slot, scaled by r_length.
0474   ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
0475   // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
0476   ARM64_RELOC_ADDEND = 10,
0477   // An authenticated pointer.
0478   ARM64_RELOC_AUTHENTICATED_POINTER = 11,
0479 
0480   // Constant values for the r_type field in an x86_64 architecture
0481   // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
0482   // structure
0483   X86_64_RELOC_UNSIGNED = 0,
0484   X86_64_RELOC_SIGNED = 1,
0485   X86_64_RELOC_BRANCH = 2,
0486   X86_64_RELOC_GOT_LOAD = 3,
0487   X86_64_RELOC_GOT = 4,
0488   X86_64_RELOC_SUBTRACTOR = 5,
0489   X86_64_RELOC_SIGNED_1 = 6,
0490   X86_64_RELOC_SIGNED_2 = 7,
0491   X86_64_RELOC_SIGNED_4 = 8,
0492   X86_64_RELOC_TLV = 9
0493 };
0494 
0495 // Values for segment_command.initprot.
0496 // From <mach/vm_prot.h>
0497 enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
0498 
0499 // Values for platform field in build_version_command.
0500 enum PlatformType {
0501 #define PLATFORM(platform, id, name, build_name, target, tapi_target,          \
0502                  marketing)                                                    \
0503   PLATFORM_##platform = id,
0504 #include "MachO.def"
0505 };
0506 
0507 // Values for tools enum in build_tool_version.
0508 enum { TOOL_CLANG = 1, TOOL_SWIFT = 2, TOOL_LD = 3, TOOL_LLD = 4 };
0509 
0510 // Structs from <mach-o/loader.h>
0511 
0512 struct mach_header {
0513   uint32_t magic;
0514   uint32_t cputype;
0515   uint32_t cpusubtype;
0516   uint32_t filetype;
0517   uint32_t ncmds;
0518   uint32_t sizeofcmds;
0519   uint32_t flags;
0520 };
0521 
0522 struct mach_header_64 {
0523   uint32_t magic;
0524   uint32_t cputype;
0525   uint32_t cpusubtype;
0526   uint32_t filetype;
0527   uint32_t ncmds;
0528   uint32_t sizeofcmds;
0529   uint32_t flags;
0530   uint32_t reserved;
0531 };
0532 
0533 struct load_command {
0534   uint32_t cmd;
0535   uint32_t cmdsize;
0536 };
0537 
0538 struct segment_command {
0539   uint32_t cmd;
0540   uint32_t cmdsize;
0541   char segname[16];
0542   uint32_t vmaddr;
0543   uint32_t vmsize;
0544   uint32_t fileoff;
0545   uint32_t filesize;
0546   uint32_t maxprot;
0547   uint32_t initprot;
0548   uint32_t nsects;
0549   uint32_t flags;
0550 };
0551 
0552 struct segment_command_64 {
0553   uint32_t cmd;
0554   uint32_t cmdsize;
0555   char segname[16];
0556   uint64_t vmaddr;
0557   uint64_t vmsize;
0558   uint64_t fileoff;
0559   uint64_t filesize;
0560   uint32_t maxprot;
0561   uint32_t initprot;
0562   uint32_t nsects;
0563   uint32_t flags;
0564 };
0565 
0566 struct section {
0567   char sectname[16];
0568   char segname[16];
0569   uint32_t addr;
0570   uint32_t size;
0571   uint32_t offset;
0572   uint32_t align;
0573   uint32_t reloff;
0574   uint32_t nreloc;
0575   uint32_t flags;
0576   uint32_t reserved1;
0577   uint32_t reserved2;
0578 };
0579 
0580 struct section_64 {
0581   char sectname[16];
0582   char segname[16];
0583   uint64_t addr;
0584   uint64_t size;
0585   uint32_t offset;
0586   uint32_t align;
0587   uint32_t reloff;
0588   uint32_t nreloc;
0589   uint32_t flags;
0590   uint32_t reserved1;
0591   uint32_t reserved2;
0592   uint32_t reserved3;
0593 };
0594 
0595 inline bool isVirtualSection(uint8_t type) {
0596   return (type == MachO::S_ZEROFILL || type == MachO::S_GB_ZEROFILL ||
0597           type == MachO::S_THREAD_LOCAL_ZEROFILL);
0598 }
0599 
0600 struct fvmlib {
0601   uint32_t name;
0602   uint32_t minor_version;
0603   uint32_t header_addr;
0604 };
0605 
0606 // The fvmlib_command is obsolete and no longer supported.
0607 struct fvmlib_command {
0608   uint32_t cmd;
0609   uint32_t cmdsize;
0610   struct fvmlib fvmlib;
0611 };
0612 
0613 struct dylib {
0614   uint32_t name;
0615   uint32_t timestamp;
0616   uint32_t current_version;
0617   uint32_t compatibility_version;
0618 };
0619 
0620 struct dylib_command {
0621   uint32_t cmd;
0622   uint32_t cmdsize;
0623   struct dylib dylib;
0624 };
0625 
0626 struct sub_framework_command {
0627   uint32_t cmd;
0628   uint32_t cmdsize;
0629   uint32_t umbrella;
0630 };
0631 
0632 struct sub_client_command {
0633   uint32_t cmd;
0634   uint32_t cmdsize;
0635   uint32_t client;
0636 };
0637 
0638 struct sub_umbrella_command {
0639   uint32_t cmd;
0640   uint32_t cmdsize;
0641   uint32_t sub_umbrella;
0642 };
0643 
0644 struct sub_library_command {
0645   uint32_t cmd;
0646   uint32_t cmdsize;
0647   uint32_t sub_library;
0648 };
0649 
0650 // The prebound_dylib_command is obsolete and no longer supported.
0651 struct prebound_dylib_command {
0652   uint32_t cmd;
0653   uint32_t cmdsize;
0654   uint32_t name;
0655   uint32_t nmodules;
0656   uint32_t linked_modules;
0657 };
0658 
0659 struct dylinker_command {
0660   uint32_t cmd;
0661   uint32_t cmdsize;
0662   uint32_t name;
0663 };
0664 
0665 struct thread_command {
0666   uint32_t cmd;
0667   uint32_t cmdsize;
0668 };
0669 
0670 struct routines_command {
0671   uint32_t cmd;
0672   uint32_t cmdsize;
0673   uint32_t init_address;
0674   uint32_t init_module;
0675   uint32_t reserved1;
0676   uint32_t reserved2;
0677   uint32_t reserved3;
0678   uint32_t reserved4;
0679   uint32_t reserved5;
0680   uint32_t reserved6;
0681 };
0682 
0683 struct routines_command_64 {
0684   uint32_t cmd;
0685   uint32_t cmdsize;
0686   uint64_t init_address;
0687   uint64_t init_module;
0688   uint64_t reserved1;
0689   uint64_t reserved2;
0690   uint64_t reserved3;
0691   uint64_t reserved4;
0692   uint64_t reserved5;
0693   uint64_t reserved6;
0694 };
0695 
0696 struct symtab_command {
0697   uint32_t cmd;
0698   uint32_t cmdsize;
0699   uint32_t symoff;
0700   uint32_t nsyms;
0701   uint32_t stroff;
0702   uint32_t strsize;
0703 };
0704 
0705 struct dysymtab_command {
0706   uint32_t cmd;
0707   uint32_t cmdsize;
0708   uint32_t ilocalsym;
0709   uint32_t nlocalsym;
0710   uint32_t iextdefsym;
0711   uint32_t nextdefsym;
0712   uint32_t iundefsym;
0713   uint32_t nundefsym;
0714   uint32_t tocoff;
0715   uint32_t ntoc;
0716   uint32_t modtaboff;
0717   uint32_t nmodtab;
0718   uint32_t extrefsymoff;
0719   uint32_t nextrefsyms;
0720   uint32_t indirectsymoff;
0721   uint32_t nindirectsyms;
0722   uint32_t extreloff;
0723   uint32_t nextrel;
0724   uint32_t locreloff;
0725   uint32_t nlocrel;
0726 };
0727 
0728 struct dylib_table_of_contents {
0729   uint32_t symbol_index;
0730   uint32_t module_index;
0731 };
0732 
0733 struct dylib_module {
0734   uint32_t module_name;
0735   uint32_t iextdefsym;
0736   uint32_t nextdefsym;
0737   uint32_t irefsym;
0738   uint32_t nrefsym;
0739   uint32_t ilocalsym;
0740   uint32_t nlocalsym;
0741   uint32_t iextrel;
0742   uint32_t nextrel;
0743   uint32_t iinit_iterm;
0744   uint32_t ninit_nterm;
0745   uint32_t objc_module_info_addr;
0746   uint32_t objc_module_info_size;
0747 };
0748 
0749 struct dylib_module_64 {
0750   uint32_t module_name;
0751   uint32_t iextdefsym;
0752   uint32_t nextdefsym;
0753   uint32_t irefsym;
0754   uint32_t nrefsym;
0755   uint32_t ilocalsym;
0756   uint32_t nlocalsym;
0757   uint32_t iextrel;
0758   uint32_t nextrel;
0759   uint32_t iinit_iterm;
0760   uint32_t ninit_nterm;
0761   uint32_t objc_module_info_size;
0762   uint64_t objc_module_info_addr;
0763 };
0764 
0765 struct dylib_reference {
0766   uint32_t isym : 24, flags : 8;
0767 };
0768 
0769 // The twolevel_hints_command is obsolete and no longer supported.
0770 struct twolevel_hints_command {
0771   uint32_t cmd;
0772   uint32_t cmdsize;
0773   uint32_t offset;
0774   uint32_t nhints;
0775 };
0776 
0777 // The twolevel_hints_command is obsolete and no longer supported.
0778 struct twolevel_hint {
0779   uint32_t isub_image : 8, itoc : 24;
0780 };
0781 
0782 // The prebind_cksum_command is obsolete and no longer supported.
0783 struct prebind_cksum_command {
0784   uint32_t cmd;
0785   uint32_t cmdsize;
0786   uint32_t cksum;
0787 };
0788 
0789 struct uuid_command {
0790   uint32_t cmd;
0791   uint32_t cmdsize;
0792   uint8_t uuid[16];
0793 };
0794 
0795 struct rpath_command {
0796   uint32_t cmd;
0797   uint32_t cmdsize;
0798   uint32_t path;
0799 };
0800 
0801 struct linkedit_data_command {
0802   uint32_t cmd;
0803   uint32_t cmdsize;
0804   uint32_t dataoff;
0805   uint32_t datasize;
0806 };
0807 
0808 struct data_in_code_entry {
0809   uint32_t offset;
0810   uint16_t length;
0811   uint16_t kind;
0812 };
0813 
0814 struct source_version_command {
0815   uint32_t cmd;
0816   uint32_t cmdsize;
0817   uint64_t version;
0818 };
0819 
0820 struct encryption_info_command {
0821   uint32_t cmd;
0822   uint32_t cmdsize;
0823   uint32_t cryptoff;
0824   uint32_t cryptsize;
0825   uint32_t cryptid;
0826 };
0827 
0828 struct encryption_info_command_64 {
0829   uint32_t cmd;
0830   uint32_t cmdsize;
0831   uint32_t cryptoff;
0832   uint32_t cryptsize;
0833   uint32_t cryptid;
0834   uint32_t pad;
0835 };
0836 
0837 struct version_min_command {
0838   uint32_t cmd;     // LC_VERSION_MIN_MACOSX or
0839                     // LC_VERSION_MIN_IPHONEOS
0840   uint32_t cmdsize; // sizeof(struct version_min_command)
0841   uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
0842   uint32_t sdk;     // X.Y.Z is encoded in nibbles xxxx.yy.zz
0843 };
0844 
0845 struct note_command {
0846   uint32_t cmd;        // LC_NOTE
0847   uint32_t cmdsize;    // sizeof(struct note_command)
0848   char data_owner[16]; // owner name for this LC_NOTE
0849   uint64_t offset;     // file offset of this data
0850   uint64_t size;       // length of data region
0851 };
0852 
0853 struct build_tool_version {
0854   uint32_t tool;    // enum for the tool
0855   uint32_t version; // version of the tool
0856 };
0857 
0858 struct build_version_command {
0859   uint32_t cmd;      // LC_BUILD_VERSION
0860   uint32_t cmdsize;  // sizeof(struct build_version_command) +
0861                      // ntools * sizeof(struct build_tool_version)
0862   uint32_t platform; // platform
0863   uint32_t minos;    // X.Y.Z is encoded in nibbles xxxx.yy.zz
0864   uint32_t sdk;      // X.Y.Z is encoded in nibbles xxxx.yy.zz
0865   uint32_t ntools;   // number of tool entries following this
0866 };
0867 
0868 struct dyld_env_command {
0869   uint32_t cmd;
0870   uint32_t cmdsize;
0871   uint32_t name;
0872 };
0873 
0874 struct dyld_info_command {
0875   uint32_t cmd;
0876   uint32_t cmdsize;
0877   uint32_t rebase_off;
0878   uint32_t rebase_size;
0879   uint32_t bind_off;
0880   uint32_t bind_size;
0881   uint32_t weak_bind_off;
0882   uint32_t weak_bind_size;
0883   uint32_t lazy_bind_off;
0884   uint32_t lazy_bind_size;
0885   uint32_t export_off;
0886   uint32_t export_size;
0887 };
0888 
0889 struct linker_option_command {
0890   uint32_t cmd;
0891   uint32_t cmdsize;
0892   uint32_t count;
0893 };
0894 
0895 union lc_str {
0896   uint32_t offset;
0897 };
0898 
0899 struct fileset_entry_command {
0900   uint32_t cmd;
0901   uint32_t cmdsize;
0902   uint64_t vmaddr;
0903   uint64_t fileoff;
0904   union lc_str entry_id;
0905   uint32_t reserved;
0906 };
0907 
0908 // The symseg_command is obsolete and no longer supported.
0909 struct symseg_command {
0910   uint32_t cmd;
0911   uint32_t cmdsize;
0912   uint32_t offset;
0913   uint32_t size;
0914 };
0915 
0916 // The ident_command is obsolete and no longer supported.
0917 struct ident_command {
0918   uint32_t cmd;
0919   uint32_t cmdsize;
0920 };
0921 
0922 // The fvmfile_command is obsolete and no longer supported.
0923 struct fvmfile_command {
0924   uint32_t cmd;
0925   uint32_t cmdsize;
0926   uint32_t name;
0927   uint32_t header_addr;
0928 };
0929 
0930 struct tlv_descriptor_32 {
0931   uint32_t thunk;
0932   uint32_t key;
0933   uint32_t offset;
0934 };
0935 
0936 struct tlv_descriptor_64 {
0937   uint64_t thunk;
0938   uint64_t key;
0939   uint64_t offset;
0940 };
0941 
0942 struct tlv_descriptor {
0943   uintptr_t thunk;
0944   uintptr_t key;
0945   uintptr_t offset;
0946 };
0947 
0948 struct entry_point_command {
0949   uint32_t cmd;
0950   uint32_t cmdsize;
0951   uint64_t entryoff;
0952   uint64_t stacksize;
0953 };
0954 
0955 // Structs from <mach-o/fat.h>
0956 struct fat_header {
0957   uint32_t magic;
0958   uint32_t nfat_arch;
0959 };
0960 
0961 struct fat_arch {
0962   uint32_t cputype;
0963   uint32_t cpusubtype;
0964   uint32_t offset;
0965   uint32_t size;
0966   uint32_t align;
0967 };
0968 
0969 struct fat_arch_64 {
0970   uint32_t cputype;
0971   uint32_t cpusubtype;
0972   uint64_t offset;
0973   uint64_t size;
0974   uint32_t align;
0975   uint32_t reserved;
0976 };
0977 
0978 // Structs from <mach-o/reloc.h>
0979 struct relocation_info {
0980   int32_t r_address;
0981   uint32_t r_symbolnum : 24, r_pcrel : 1, r_length : 2, r_extern : 1,
0982       r_type : 4;
0983 };
0984 
0985 struct scattered_relocation_info {
0986 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
0987   uint32_t r_scattered : 1, r_pcrel : 1, r_length : 2, r_type : 4,
0988       r_address : 24;
0989 #else
0990   uint32_t r_address : 24, r_type : 4, r_length : 2, r_pcrel : 1,
0991       r_scattered : 1;
0992 #endif
0993   int32_t r_value;
0994 };
0995 
0996 // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
0997 struct any_relocation_info {
0998   uint32_t r_word0, r_word1;
0999 };
1000 
1001 // Structs from <mach-o/nlist.h>
1002 struct nlist_base {
1003   uint32_t n_strx;
1004   uint8_t n_type;
1005   uint8_t n_sect;
1006   uint16_t n_desc;
1007 };
1008 
1009 struct nlist {
1010   uint32_t n_strx;
1011   uint8_t n_type;
1012   uint8_t n_sect;
1013   int16_t n_desc;
1014   uint32_t n_value;
1015 };
1016 
1017 struct nlist_64 {
1018   uint32_t n_strx;
1019   uint8_t n_type;
1020   uint8_t n_sect;
1021   uint16_t n_desc;
1022   uint64_t n_value;
1023 };
1024 
1025 // Values for dyld_chained_fixups_header::imports_format.
1026 enum ChainedImportFormat {
1027   DYLD_CHAINED_IMPORT = 1,
1028   DYLD_CHAINED_IMPORT_ADDEND = 2,
1029   DYLD_CHAINED_IMPORT_ADDEND64 = 3,
1030 };
1031 
1032 // Values for dyld_chained_fixups_header::symbols_format.
1033 enum {
1034   DYLD_CHAINED_SYMBOL_UNCOMPRESSED = 0,
1035   DYLD_CHAINED_SYMBOL_ZLIB = 1,
1036 };
1037 
1038 // Values for dyld_chained_starts_in_segment::page_start.
1039 enum {
1040   DYLD_CHAINED_PTR_START_NONE = 0xFFFF,
1041   DYLD_CHAINED_PTR_START_MULTI = 0x8000, // page which has multiple starts
1042   DYLD_CHAINED_PTR_START_LAST = 0x8000,  // last chain_start for a given page
1043 };
1044 
1045 // Values for dyld_chained_starts_in_segment::pointer_format.
1046 enum {
1047   DYLD_CHAINED_PTR_ARM64E = 1,
1048   DYLD_CHAINED_PTR_64 = 2,
1049   DYLD_CHAINED_PTR_32 = 3,
1050   DYLD_CHAINED_PTR_32_CACHE = 4,
1051   DYLD_CHAINED_PTR_32_FIRMWARE = 5,
1052   DYLD_CHAINED_PTR_64_OFFSET = 6,
1053   DYLD_CHAINED_PTR_ARM64E_KERNEL = 7,
1054   DYLD_CHAINED_PTR_64_KERNEL_CACHE = 8,
1055   DYLD_CHAINED_PTR_ARM64E_USERLAND = 9,
1056   DYLD_CHAINED_PTR_ARM64E_FIRMWARE = 10,
1057   DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE = 11,
1058   DYLD_CHAINED_PTR_ARM64E_USERLAND24 = 12,
1059 };
1060 
1061 /// Structs for dyld chained fixups.
1062 /// dyld_chained_fixups_header is the data pointed to by LC_DYLD_CHAINED_FIXUPS
1063 /// load command.
1064 struct dyld_chained_fixups_header {
1065   uint32_t fixups_version; ///< 0
1066   uint32_t starts_offset;  ///< Offset of dyld_chained_starts_in_image.
1067   uint32_t imports_offset; ///< Offset of imports table in chain_data.
1068   uint32_t symbols_offset; ///< Offset of symbol strings in chain_data.
1069   uint32_t imports_count;  ///< Number of imported symbol names.
1070   uint32_t imports_format; ///< DYLD_CHAINED_IMPORT*
1071   uint32_t symbols_format; ///< 0 => uncompressed, 1 => zlib compressed
1072 };
1073 
1074 /// dyld_chained_starts_in_image is embedded in LC_DYLD_CHAINED_FIXUPS payload.
1075 /// Each seg_info_offset entry is the offset into this struct for that
1076 /// segment followed by pool of dyld_chain_starts_in_segment data.
1077 struct dyld_chained_starts_in_image {
1078   uint32_t seg_count;
1079   uint32_t seg_info_offset[1];
1080 };
1081 
1082 struct dyld_chained_starts_in_segment {
1083   uint32_t size;              ///< Size of this, including chain_starts entries
1084   uint16_t page_size;         ///< Page size in bytes (0x1000 or 0x4000)
1085   uint16_t pointer_format;    ///< DYLD_CHAINED_PTR*
1086   uint64_t segment_offset;    ///< VM offset from the __TEXT segment
1087   uint32_t max_valid_pointer; ///< Values beyond this are not pointers on 32-bit
1088   uint16_t page_count;        ///< Length of the page_start array
1089   uint16_t page_start[1];     ///< Page offset of first fixup on each page, or
1090                               ///< DYLD_CHAINED_PTR_START_NONE if no fixups
1091 };
1092 
1093 // DYLD_CHAINED_IMPORT
1094 struct dyld_chained_import {
1095   uint32_t lib_ordinal : 8;
1096   uint32_t weak_import : 1;
1097   uint32_t name_offset : 23;
1098 };
1099 
1100 // DYLD_CHAINED_IMPORT_ADDEND
1101 struct dyld_chained_import_addend {
1102   uint32_t lib_ordinal : 8;
1103   uint32_t weak_import : 1;
1104   uint32_t name_offset : 23;
1105   int32_t addend;
1106 };
1107 
1108 // DYLD_CHAINED_IMPORT_ADDEND64
1109 struct dyld_chained_import_addend64 {
1110   uint64_t lib_ordinal : 16;
1111   uint64_t weak_import : 1;
1112   uint64_t reserved : 15;
1113   uint64_t name_offset : 32;
1114   uint64_t addend;
1115 };
1116 
1117 // The `bind` field (most significant bit) of the encoded fixup determines
1118 // whether it is dyld_chained_ptr_64_bind or dyld_chained_ptr_64_rebase.
1119 
1120 // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
1121 struct dyld_chained_ptr_64_bind {
1122   uint64_t ordinal : 24;
1123   uint64_t addend : 8;
1124   uint64_t reserved : 19;
1125   uint64_t next : 12;
1126   uint64_t bind : 1; // set to 1
1127 };
1128 
1129 // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
1130 struct dyld_chained_ptr_64_rebase {
1131   uint64_t target : 36;
1132   uint64_t high8 : 8;
1133   uint64_t reserved : 7;
1134   uint64_t next : 12;
1135   uint64_t bind : 1; // set to 0
1136 };
1137 
1138 // Byte order swapping functions for MachO structs
1139 
1140 inline void swapStruct(fat_header &mh) {
1141   sys::swapByteOrder(mh.magic);
1142   sys::swapByteOrder(mh.nfat_arch);
1143 }
1144 
1145 inline void swapStruct(fat_arch &mh) {
1146   sys::swapByteOrder(mh.cputype);
1147   sys::swapByteOrder(mh.cpusubtype);
1148   sys::swapByteOrder(mh.offset);
1149   sys::swapByteOrder(mh.size);
1150   sys::swapByteOrder(mh.align);
1151 }
1152 
1153 inline void swapStruct(fat_arch_64 &mh) {
1154   sys::swapByteOrder(mh.cputype);
1155   sys::swapByteOrder(mh.cpusubtype);
1156   sys::swapByteOrder(mh.offset);
1157   sys::swapByteOrder(mh.size);
1158   sys::swapByteOrder(mh.align);
1159   sys::swapByteOrder(mh.reserved);
1160 }
1161 
1162 inline void swapStruct(mach_header &mh) {
1163   sys::swapByteOrder(mh.magic);
1164   sys::swapByteOrder(mh.cputype);
1165   sys::swapByteOrder(mh.cpusubtype);
1166   sys::swapByteOrder(mh.filetype);
1167   sys::swapByteOrder(mh.ncmds);
1168   sys::swapByteOrder(mh.sizeofcmds);
1169   sys::swapByteOrder(mh.flags);
1170 }
1171 
1172 inline void swapStruct(mach_header_64 &H) {
1173   sys::swapByteOrder(H.magic);
1174   sys::swapByteOrder(H.cputype);
1175   sys::swapByteOrder(H.cpusubtype);
1176   sys::swapByteOrder(H.filetype);
1177   sys::swapByteOrder(H.ncmds);
1178   sys::swapByteOrder(H.sizeofcmds);
1179   sys::swapByteOrder(H.flags);
1180   sys::swapByteOrder(H.reserved);
1181 }
1182 
1183 inline void swapStruct(load_command &lc) {
1184   sys::swapByteOrder(lc.cmd);
1185   sys::swapByteOrder(lc.cmdsize);
1186 }
1187 
1188 inline void swapStruct(symtab_command &lc) {
1189   sys::swapByteOrder(lc.cmd);
1190   sys::swapByteOrder(lc.cmdsize);
1191   sys::swapByteOrder(lc.symoff);
1192   sys::swapByteOrder(lc.nsyms);
1193   sys::swapByteOrder(lc.stroff);
1194   sys::swapByteOrder(lc.strsize);
1195 }
1196 
1197 inline void swapStruct(segment_command_64 &seg) {
1198   sys::swapByteOrder(seg.cmd);
1199   sys::swapByteOrder(seg.cmdsize);
1200   sys::swapByteOrder(seg.vmaddr);
1201   sys::swapByteOrder(seg.vmsize);
1202   sys::swapByteOrder(seg.fileoff);
1203   sys::swapByteOrder(seg.filesize);
1204   sys::swapByteOrder(seg.maxprot);
1205   sys::swapByteOrder(seg.initprot);
1206   sys::swapByteOrder(seg.nsects);
1207   sys::swapByteOrder(seg.flags);
1208 }
1209 
1210 inline void swapStruct(segment_command &seg) {
1211   sys::swapByteOrder(seg.cmd);
1212   sys::swapByteOrder(seg.cmdsize);
1213   sys::swapByteOrder(seg.vmaddr);
1214   sys::swapByteOrder(seg.vmsize);
1215   sys::swapByteOrder(seg.fileoff);
1216   sys::swapByteOrder(seg.filesize);
1217   sys::swapByteOrder(seg.maxprot);
1218   sys::swapByteOrder(seg.initprot);
1219   sys::swapByteOrder(seg.nsects);
1220   sys::swapByteOrder(seg.flags);
1221 }
1222 
1223 inline void swapStruct(section_64 &sect) {
1224   sys::swapByteOrder(sect.addr);
1225   sys::swapByteOrder(sect.size);
1226   sys::swapByteOrder(sect.offset);
1227   sys::swapByteOrder(sect.align);
1228   sys::swapByteOrder(sect.reloff);
1229   sys::swapByteOrder(sect.nreloc);
1230   sys::swapByteOrder(sect.flags);
1231   sys::swapByteOrder(sect.reserved1);
1232   sys::swapByteOrder(sect.reserved2);
1233 }
1234 
1235 inline void swapStruct(section &sect) {
1236   sys::swapByteOrder(sect.addr);
1237   sys::swapByteOrder(sect.size);
1238   sys::swapByteOrder(sect.offset);
1239   sys::swapByteOrder(sect.align);
1240   sys::swapByteOrder(sect.reloff);
1241   sys::swapByteOrder(sect.nreloc);
1242   sys::swapByteOrder(sect.flags);
1243   sys::swapByteOrder(sect.reserved1);
1244   sys::swapByteOrder(sect.reserved2);
1245 }
1246 
1247 inline void swapStruct(dyld_info_command &info) {
1248   sys::swapByteOrder(info.cmd);
1249   sys::swapByteOrder(info.cmdsize);
1250   sys::swapByteOrder(info.rebase_off);
1251   sys::swapByteOrder(info.rebase_size);
1252   sys::swapByteOrder(info.bind_off);
1253   sys::swapByteOrder(info.bind_size);
1254   sys::swapByteOrder(info.weak_bind_off);
1255   sys::swapByteOrder(info.weak_bind_size);
1256   sys::swapByteOrder(info.lazy_bind_off);
1257   sys::swapByteOrder(info.lazy_bind_size);
1258   sys::swapByteOrder(info.export_off);
1259   sys::swapByteOrder(info.export_size);
1260 }
1261 
1262 inline void swapStruct(dylib_command &d) {
1263   sys::swapByteOrder(d.cmd);
1264   sys::swapByteOrder(d.cmdsize);
1265   sys::swapByteOrder(d.dylib.name);
1266   sys::swapByteOrder(d.dylib.timestamp);
1267   sys::swapByteOrder(d.dylib.current_version);
1268   sys::swapByteOrder(d.dylib.compatibility_version);
1269 }
1270 
1271 inline void swapStruct(sub_framework_command &s) {
1272   sys::swapByteOrder(s.cmd);
1273   sys::swapByteOrder(s.cmdsize);
1274   sys::swapByteOrder(s.umbrella);
1275 }
1276 
1277 inline void swapStruct(sub_umbrella_command &s) {
1278   sys::swapByteOrder(s.cmd);
1279   sys::swapByteOrder(s.cmdsize);
1280   sys::swapByteOrder(s.sub_umbrella);
1281 }
1282 
1283 inline void swapStruct(sub_library_command &s) {
1284   sys::swapByteOrder(s.cmd);
1285   sys::swapByteOrder(s.cmdsize);
1286   sys::swapByteOrder(s.sub_library);
1287 }
1288 
1289 inline void swapStruct(sub_client_command &s) {
1290   sys::swapByteOrder(s.cmd);
1291   sys::swapByteOrder(s.cmdsize);
1292   sys::swapByteOrder(s.client);
1293 }
1294 
1295 inline void swapStruct(routines_command &r) {
1296   sys::swapByteOrder(r.cmd);
1297   sys::swapByteOrder(r.cmdsize);
1298   sys::swapByteOrder(r.init_address);
1299   sys::swapByteOrder(r.init_module);
1300   sys::swapByteOrder(r.reserved1);
1301   sys::swapByteOrder(r.reserved2);
1302   sys::swapByteOrder(r.reserved3);
1303   sys::swapByteOrder(r.reserved4);
1304   sys::swapByteOrder(r.reserved5);
1305   sys::swapByteOrder(r.reserved6);
1306 }
1307 
1308 inline void swapStruct(routines_command_64 &r) {
1309   sys::swapByteOrder(r.cmd);
1310   sys::swapByteOrder(r.cmdsize);
1311   sys::swapByteOrder(r.init_address);
1312   sys::swapByteOrder(r.init_module);
1313   sys::swapByteOrder(r.reserved1);
1314   sys::swapByteOrder(r.reserved2);
1315   sys::swapByteOrder(r.reserved3);
1316   sys::swapByteOrder(r.reserved4);
1317   sys::swapByteOrder(r.reserved5);
1318   sys::swapByteOrder(r.reserved6);
1319 }
1320 
1321 inline void swapStruct(thread_command &t) {
1322   sys::swapByteOrder(t.cmd);
1323   sys::swapByteOrder(t.cmdsize);
1324 }
1325 
1326 inline void swapStruct(dylinker_command &d) {
1327   sys::swapByteOrder(d.cmd);
1328   sys::swapByteOrder(d.cmdsize);
1329   sys::swapByteOrder(d.name);
1330 }
1331 
1332 inline void swapStruct(uuid_command &u) {
1333   sys::swapByteOrder(u.cmd);
1334   sys::swapByteOrder(u.cmdsize);
1335 }
1336 
1337 inline void swapStruct(rpath_command &r) {
1338   sys::swapByteOrder(r.cmd);
1339   sys::swapByteOrder(r.cmdsize);
1340   sys::swapByteOrder(r.path);
1341 }
1342 
1343 inline void swapStruct(source_version_command &s) {
1344   sys::swapByteOrder(s.cmd);
1345   sys::swapByteOrder(s.cmdsize);
1346   sys::swapByteOrder(s.version);
1347 }
1348 
1349 inline void swapStruct(entry_point_command &e) {
1350   sys::swapByteOrder(e.cmd);
1351   sys::swapByteOrder(e.cmdsize);
1352   sys::swapByteOrder(e.entryoff);
1353   sys::swapByteOrder(e.stacksize);
1354 }
1355 
1356 inline void swapStruct(encryption_info_command &e) {
1357   sys::swapByteOrder(e.cmd);
1358   sys::swapByteOrder(e.cmdsize);
1359   sys::swapByteOrder(e.cryptoff);
1360   sys::swapByteOrder(e.cryptsize);
1361   sys::swapByteOrder(e.cryptid);
1362 }
1363 
1364 inline void swapStruct(encryption_info_command_64 &e) {
1365   sys::swapByteOrder(e.cmd);
1366   sys::swapByteOrder(e.cmdsize);
1367   sys::swapByteOrder(e.cryptoff);
1368   sys::swapByteOrder(e.cryptsize);
1369   sys::swapByteOrder(e.cryptid);
1370   sys::swapByteOrder(e.pad);
1371 }
1372 
1373 inline void swapStruct(dysymtab_command &dst) {
1374   sys::swapByteOrder(dst.cmd);
1375   sys::swapByteOrder(dst.cmdsize);
1376   sys::swapByteOrder(dst.ilocalsym);
1377   sys::swapByteOrder(dst.nlocalsym);
1378   sys::swapByteOrder(dst.iextdefsym);
1379   sys::swapByteOrder(dst.nextdefsym);
1380   sys::swapByteOrder(dst.iundefsym);
1381   sys::swapByteOrder(dst.nundefsym);
1382   sys::swapByteOrder(dst.tocoff);
1383   sys::swapByteOrder(dst.ntoc);
1384   sys::swapByteOrder(dst.modtaboff);
1385   sys::swapByteOrder(dst.nmodtab);
1386   sys::swapByteOrder(dst.extrefsymoff);
1387   sys::swapByteOrder(dst.nextrefsyms);
1388   sys::swapByteOrder(dst.indirectsymoff);
1389   sys::swapByteOrder(dst.nindirectsyms);
1390   sys::swapByteOrder(dst.extreloff);
1391   sys::swapByteOrder(dst.nextrel);
1392   sys::swapByteOrder(dst.locreloff);
1393   sys::swapByteOrder(dst.nlocrel);
1394 }
1395 
1396 inline void swapStruct(any_relocation_info &reloc) {
1397   sys::swapByteOrder(reloc.r_word0);
1398   sys::swapByteOrder(reloc.r_word1);
1399 }
1400 
1401 inline void swapStruct(nlist_base &S) {
1402   sys::swapByteOrder(S.n_strx);
1403   sys::swapByteOrder(S.n_desc);
1404 }
1405 
1406 inline void swapStruct(nlist &sym) {
1407   sys::swapByteOrder(sym.n_strx);
1408   sys::swapByteOrder(sym.n_desc);
1409   sys::swapByteOrder(sym.n_value);
1410 }
1411 
1412 inline void swapStruct(nlist_64 &sym) {
1413   sys::swapByteOrder(sym.n_strx);
1414   sys::swapByteOrder(sym.n_desc);
1415   sys::swapByteOrder(sym.n_value);
1416 }
1417 
1418 inline void swapStruct(linkedit_data_command &C) {
1419   sys::swapByteOrder(C.cmd);
1420   sys::swapByteOrder(C.cmdsize);
1421   sys::swapByteOrder(C.dataoff);
1422   sys::swapByteOrder(C.datasize);
1423 }
1424 
1425 inline void swapStruct(linker_option_command &C) {
1426   sys::swapByteOrder(C.cmd);
1427   sys::swapByteOrder(C.cmdsize);
1428   sys::swapByteOrder(C.count);
1429 }
1430 
1431 inline void swapStruct(fileset_entry_command &C) {
1432   sys::swapByteOrder(C.cmd);
1433   sys::swapByteOrder(C.cmdsize);
1434   sys::swapByteOrder(C.vmaddr);
1435   sys::swapByteOrder(C.fileoff);
1436   sys::swapByteOrder(C.entry_id.offset);
1437   sys::swapByteOrder(C.reserved);
1438 }
1439 
1440 inline void swapStruct(version_min_command &C) {
1441   sys::swapByteOrder(C.cmd);
1442   sys::swapByteOrder(C.cmdsize);
1443   sys::swapByteOrder(C.version);
1444   sys::swapByteOrder(C.sdk);
1445 }
1446 
1447 inline void swapStruct(note_command &C) {
1448   sys::swapByteOrder(C.cmd);
1449   sys::swapByteOrder(C.cmdsize);
1450   sys::swapByteOrder(C.offset);
1451   sys::swapByteOrder(C.size);
1452 }
1453 
1454 inline void swapStruct(build_version_command &C) {
1455   sys::swapByteOrder(C.cmd);
1456   sys::swapByteOrder(C.cmdsize);
1457   sys::swapByteOrder(C.platform);
1458   sys::swapByteOrder(C.minos);
1459   sys::swapByteOrder(C.sdk);
1460   sys::swapByteOrder(C.ntools);
1461 }
1462 
1463 inline void swapStruct(build_tool_version &C) {
1464   sys::swapByteOrder(C.tool);
1465   sys::swapByteOrder(C.version);
1466 }
1467 
1468 inline void swapStruct(data_in_code_entry &C) {
1469   sys::swapByteOrder(C.offset);
1470   sys::swapByteOrder(C.length);
1471   sys::swapByteOrder(C.kind);
1472 }
1473 
1474 inline void swapStruct(uint32_t &C) { sys::swapByteOrder(C); }
1475 
1476 // The prebind_cksum_command is obsolete and no longer supported.
1477 inline void swapStruct(prebind_cksum_command &C) {
1478   sys::swapByteOrder(C.cmd);
1479   sys::swapByteOrder(C.cmdsize);
1480   sys::swapByteOrder(C.cksum);
1481 }
1482 
1483 // The twolevel_hints_command is obsolete and no longer supported.
1484 inline void swapStruct(twolevel_hints_command &C) {
1485   sys::swapByteOrder(C.cmd);
1486   sys::swapByteOrder(C.cmdsize);
1487   sys::swapByteOrder(C.offset);
1488   sys::swapByteOrder(C.nhints);
1489 }
1490 
1491 // The prebound_dylib_command is obsolete and no longer supported.
1492 inline void swapStruct(prebound_dylib_command &C) {
1493   sys::swapByteOrder(C.cmd);
1494   sys::swapByteOrder(C.cmdsize);
1495   sys::swapByteOrder(C.name);
1496   sys::swapByteOrder(C.nmodules);
1497   sys::swapByteOrder(C.linked_modules);
1498 }
1499 
1500 // The fvmfile_command is obsolete and no longer supported.
1501 inline void swapStruct(fvmfile_command &C) {
1502   sys::swapByteOrder(C.cmd);
1503   sys::swapByteOrder(C.cmdsize);
1504   sys::swapByteOrder(C.name);
1505   sys::swapByteOrder(C.header_addr);
1506 }
1507 
1508 // The symseg_command is obsolete and no longer supported.
1509 inline void swapStruct(symseg_command &C) {
1510   sys::swapByteOrder(C.cmd);
1511   sys::swapByteOrder(C.cmdsize);
1512   sys::swapByteOrder(C.offset);
1513   sys::swapByteOrder(C.size);
1514 }
1515 
1516 // The ident_command is obsolete and no longer supported.
1517 inline void swapStruct(ident_command &C) {
1518   sys::swapByteOrder(C.cmd);
1519   sys::swapByteOrder(C.cmdsize);
1520 }
1521 
1522 inline void swapStruct(fvmlib &C) {
1523   sys::swapByteOrder(C.name);
1524   sys::swapByteOrder(C.minor_version);
1525   sys::swapByteOrder(C.header_addr);
1526 }
1527 
1528 // The fvmlib_command is obsolete and no longer supported.
1529 inline void swapStruct(fvmlib_command &C) {
1530   sys::swapByteOrder(C.cmd);
1531   sys::swapByteOrder(C.cmdsize);
1532   swapStruct(C.fvmlib);
1533 }
1534 
1535 // Get/Set functions from <mach-o/nlist.h>
1536 
1537 inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
1538   return (((n_desc) >> 8u) & 0xffu);
1539 }
1540 
1541 inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
1542   n_desc = (((n_desc)&0x00ff) | (((ordinal)&0xff) << 8));
1543 }
1544 
1545 inline uint8_t GET_COMM_ALIGN(uint16_t n_desc) {
1546   return (n_desc >> 8u) & 0x0fu;
1547 }
1548 
1549 inline void SET_COMM_ALIGN(uint16_t &n_desc, uint8_t align) {
1550   n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
1551 }
1552 
1553 // Enums from <mach/machine.h>
1554 enum : uint32_t {
1555   // Capability bits used in the definition of cpu_type.
1556   CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
1557   CPU_ARCH_ABI64 = 0x01000000, // 64 bit ABI
1558   CPU_ARCH_ABI64_32 = 0x02000000, // ILP32 ABI on 64-bit hardware
1559 };
1560 
1561 // Constants for the cputype field.
1562 enum CPUType {
1563   CPU_TYPE_ANY = -1,
1564   CPU_TYPE_X86 = 7,
1565   CPU_TYPE_I386 = CPU_TYPE_X86,
1566   CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
1567   /* CPU_TYPE_MIPS      = 8, */
1568   CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
1569   CPU_TYPE_ARM = 12,
1570   CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
1571   CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32,
1572   CPU_TYPE_SPARC = 14,
1573   CPU_TYPE_POWERPC = 18,
1574   CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
1575 };
1576 
1577 enum : uint32_t {
1578   // Capability bits used in the definition of cpusubtype.
1579   CPU_SUBTYPE_MASK = 0xff000000,  // Mask for architecture bits
1580   CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
1581 
1582   // Special CPU subtype constants.
1583   CPU_SUBTYPE_MULTIPLE = ~0u
1584 };
1585 
1586 // Constants for the cpusubtype field.
1587 enum CPUSubTypeX86 {
1588   CPU_SUBTYPE_I386_ALL = 3,
1589   CPU_SUBTYPE_386 = 3,
1590   CPU_SUBTYPE_486 = 4,
1591   CPU_SUBTYPE_486SX = 0x84,
1592   CPU_SUBTYPE_586 = 5,
1593   CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
1594   CPU_SUBTYPE_PENTPRO = 0x16,
1595   CPU_SUBTYPE_PENTII_M3 = 0x36,
1596   CPU_SUBTYPE_PENTII_M5 = 0x56,
1597   CPU_SUBTYPE_CELERON = 0x67,
1598   CPU_SUBTYPE_CELERON_MOBILE = 0x77,
1599   CPU_SUBTYPE_PENTIUM_3 = 0x08,
1600   CPU_SUBTYPE_PENTIUM_3_M = 0x18,
1601   CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
1602   CPU_SUBTYPE_PENTIUM_M = 0x09,
1603   CPU_SUBTYPE_PENTIUM_4 = 0x0a,
1604   CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
1605   CPU_SUBTYPE_ITANIUM = 0x0b,
1606   CPU_SUBTYPE_ITANIUM_2 = 0x1b,
1607   CPU_SUBTYPE_XEON = 0x0c,
1608   CPU_SUBTYPE_XEON_MP = 0x1c,
1609 
1610   CPU_SUBTYPE_X86_ALL = 3,
1611   CPU_SUBTYPE_X86_64_ALL = 3,
1612   CPU_SUBTYPE_X86_ARCH1 = 4,
1613   CPU_SUBTYPE_X86_64_H = 8
1614 };
1615 inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
1616   return Family | (Model << 4);
1617 }
1618 inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
1619   return ((int)ST) & 0x0f;
1620 }
1621 inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) { return ((int)ST) >> 4; }
1622 enum { CPU_SUBTYPE_INTEL_FAMILY_MAX = 15, CPU_SUBTYPE_INTEL_MODEL_ALL = 0 };
1623 
1624 enum CPUSubTypeARM {
1625   CPU_SUBTYPE_ARM_ALL = 0,
1626   CPU_SUBTYPE_ARM_V4T = 5,
1627   CPU_SUBTYPE_ARM_V6 = 6,
1628   CPU_SUBTYPE_ARM_V5 = 7,
1629   CPU_SUBTYPE_ARM_V5TEJ = 7,
1630   CPU_SUBTYPE_ARM_XSCALE = 8,
1631   CPU_SUBTYPE_ARM_V7 = 9,
1632   //  unused  ARM_V7F     = 10,
1633   CPU_SUBTYPE_ARM_V7S = 11,
1634   CPU_SUBTYPE_ARM_V7K = 12,
1635   CPU_SUBTYPE_ARM_V6M = 14,
1636   CPU_SUBTYPE_ARM_V7M = 15,
1637   CPU_SUBTYPE_ARM_V7EM = 16
1638 };
1639 
1640 enum CPUSubTypeARM64 : uint32_t {
1641   CPU_SUBTYPE_ARM64_ALL = 0,
1642   CPU_SUBTYPE_ARM64_V8 = 1,
1643   CPU_SUBTYPE_ARM64E = 2,
1644 
1645   // arm64e uses the capability bits to encode ptrauth ABI information.
1646   // Bit 63 marks the binary as Versioned.
1647   CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK = 0x80000000U,
1648   // Bit 62 marks the binary as using a kernel ABI.
1649   CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK = 0x40000000U,
1650   // Bits [59:56] hold the 4-bit ptrauth ABI version.
1651   CPU_SUBTYPE_ARM64E_PTRAUTH_MASK = 0x0f000000U,
1652 };
1653 
1654 inline unsigned CPU_SUBTYPE_ARM64E_PTRAUTH_VERSION(uint32_t ST) {
1655   return (ST & CPU_SUBTYPE_ARM64E_PTRAUTH_MASK) >> 24;
1656 }
1657 
1658 inline uint32_t
1659 CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(unsigned PtrAuthABIVersion,
1660                                         bool PtrAuthKernelABIVersion) {
1661   assert((PtrAuthABIVersion <= 0xF) &&
1662          "ptrauth abi version must fit in 4 bits");
1663   return CPU_SUBTYPE_ARM64E | CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK |
1664          (PtrAuthKernelABIVersion
1665               ? (uint32_t)CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK
1666               : 0) |
1667          (PtrAuthABIVersion << 24);
1668 }
1669 
1670 inline bool CPU_SUBTYPE_ARM64E_IS_VERSIONED_PTRAUTH_ABI(uint32_t ST) {
1671   return ST & CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI_MASK;
1672 }
1673 
1674 inline bool CPU_SUBTYPE_ARM64E_IS_KERNEL_PTRAUTH_ABI(uint32_t ST) {
1675   return ST & CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI_MASK;
1676 }
1677 
1678 enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
1679 
1680 enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
1681 
1682 enum CPUSubTypePowerPC {
1683   CPU_SUBTYPE_POWERPC_ALL = 0,
1684   CPU_SUBTYPE_POWERPC_601 = 1,
1685   CPU_SUBTYPE_POWERPC_602 = 2,
1686   CPU_SUBTYPE_POWERPC_603 = 3,
1687   CPU_SUBTYPE_POWERPC_603e = 4,
1688   CPU_SUBTYPE_POWERPC_603ev = 5,
1689   CPU_SUBTYPE_POWERPC_604 = 6,
1690   CPU_SUBTYPE_POWERPC_604e = 7,
1691   CPU_SUBTYPE_POWERPC_620 = 8,
1692   CPU_SUBTYPE_POWERPC_750 = 9,
1693   CPU_SUBTYPE_POWERPC_7400 = 10,
1694   CPU_SUBTYPE_POWERPC_7450 = 11,
1695   CPU_SUBTYPE_POWERPC_970 = 100,
1696 
1697   CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
1698   CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
1699 };
1700 
1701 Expected<uint32_t> getCPUType(const Triple &T);
1702 Expected<uint32_t> getCPUSubType(const Triple &T);
1703 Expected<uint32_t> getCPUSubType(const Triple &T, unsigned PtrAuthABIVersion,
1704                                  bool PtrAuthKernelABIVersion);
1705 
1706 struct x86_thread_state32_t {
1707   uint32_t eax;
1708   uint32_t ebx;
1709   uint32_t ecx;
1710   uint32_t edx;
1711   uint32_t edi;
1712   uint32_t esi;
1713   uint32_t ebp;
1714   uint32_t esp;
1715   uint32_t ss;
1716   uint32_t eflags;
1717   uint32_t eip;
1718   uint32_t cs;
1719   uint32_t ds;
1720   uint32_t es;
1721   uint32_t fs;
1722   uint32_t gs;
1723 };
1724 
1725 struct x86_thread_state64_t {
1726   uint64_t rax;
1727   uint64_t rbx;
1728   uint64_t rcx;
1729   uint64_t rdx;
1730   uint64_t rdi;
1731   uint64_t rsi;
1732   uint64_t rbp;
1733   uint64_t rsp;
1734   uint64_t r8;
1735   uint64_t r9;
1736   uint64_t r10;
1737   uint64_t r11;
1738   uint64_t r12;
1739   uint64_t r13;
1740   uint64_t r14;
1741   uint64_t r15;
1742   uint64_t rip;
1743   uint64_t rflags;
1744   uint64_t cs;
1745   uint64_t fs;
1746   uint64_t gs;
1747 };
1748 
1749 enum x86_fp_control_precis {
1750   x86_FP_PREC_24B = 0,
1751   x86_FP_PREC_53B = 2,
1752   x86_FP_PREC_64B = 3
1753 };
1754 
1755 enum x86_fp_control_rc {
1756   x86_FP_RND_NEAR = 0,
1757   x86_FP_RND_DOWN = 1,
1758   x86_FP_RND_UP = 2,
1759   x86_FP_CHOP = 3
1760 };
1761 
1762 struct fp_control_t {
1763   unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
1764       precis : 1, : 2, pc : 2, rc : 2, : 1, : 3;
1765 };
1766 
1767 struct fp_status_t {
1768   unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
1769       precis : 1, stkflt : 1, errsumm : 1, c0 : 1, c1 : 1, c2 : 1, tos : 3,
1770       c3 : 1, busy : 1;
1771 };
1772 
1773 struct mmst_reg_t {
1774   char mmst_reg[10];
1775   char mmst_rsrv[6];
1776 };
1777 
1778 struct xmm_reg_t {
1779   char xmm_reg[16];
1780 };
1781 
1782 struct x86_float_state64_t {
1783   int32_t fpu_reserved[2];
1784   fp_control_t fpu_fcw;
1785   fp_status_t fpu_fsw;
1786   uint8_t fpu_ftw;
1787   uint8_t fpu_rsrv1;
1788   uint16_t fpu_fop;
1789   uint32_t fpu_ip;
1790   uint16_t fpu_cs;
1791   uint16_t fpu_rsrv2;
1792   uint32_t fpu_dp;
1793   uint16_t fpu_ds;
1794   uint16_t fpu_rsrv3;
1795   uint32_t fpu_mxcsr;
1796   uint32_t fpu_mxcsrmask;
1797   mmst_reg_t fpu_stmm0;
1798   mmst_reg_t fpu_stmm1;
1799   mmst_reg_t fpu_stmm2;
1800   mmst_reg_t fpu_stmm3;
1801   mmst_reg_t fpu_stmm4;
1802   mmst_reg_t fpu_stmm5;
1803   mmst_reg_t fpu_stmm6;
1804   mmst_reg_t fpu_stmm7;
1805   xmm_reg_t fpu_xmm0;
1806   xmm_reg_t fpu_xmm1;
1807   xmm_reg_t fpu_xmm2;
1808   xmm_reg_t fpu_xmm3;
1809   xmm_reg_t fpu_xmm4;
1810   xmm_reg_t fpu_xmm5;
1811   xmm_reg_t fpu_xmm6;
1812   xmm_reg_t fpu_xmm7;
1813   xmm_reg_t fpu_xmm8;
1814   xmm_reg_t fpu_xmm9;
1815   xmm_reg_t fpu_xmm10;
1816   xmm_reg_t fpu_xmm11;
1817   xmm_reg_t fpu_xmm12;
1818   xmm_reg_t fpu_xmm13;
1819   xmm_reg_t fpu_xmm14;
1820   xmm_reg_t fpu_xmm15;
1821   char fpu_rsrv4[6 * 16];
1822   uint32_t fpu_reserved1;
1823 };
1824 
1825 struct x86_exception_state64_t {
1826   uint16_t trapno;
1827   uint16_t cpu;
1828   uint32_t err;
1829   uint64_t faultvaddr;
1830 };
1831 
1832 inline void swapStruct(x86_thread_state32_t &x) {
1833   sys::swapByteOrder(x.eax);
1834   sys::swapByteOrder(x.ebx);
1835   sys::swapByteOrder(x.ecx);
1836   sys::swapByteOrder(x.edx);
1837   sys::swapByteOrder(x.edi);
1838   sys::swapByteOrder(x.esi);
1839   sys::swapByteOrder(x.ebp);
1840   sys::swapByteOrder(x.esp);
1841   sys::swapByteOrder(x.ss);
1842   sys::swapByteOrder(x.eflags);
1843   sys::swapByteOrder(x.eip);
1844   sys::swapByteOrder(x.cs);
1845   sys::swapByteOrder(x.ds);
1846   sys::swapByteOrder(x.es);
1847   sys::swapByteOrder(x.fs);
1848   sys::swapByteOrder(x.gs);
1849 }
1850 
1851 inline void swapStruct(x86_thread_state64_t &x) {
1852   sys::swapByteOrder(x.rax);
1853   sys::swapByteOrder(x.rbx);
1854   sys::swapByteOrder(x.rcx);
1855   sys::swapByteOrder(x.rdx);
1856   sys::swapByteOrder(x.rdi);
1857   sys::swapByteOrder(x.rsi);
1858   sys::swapByteOrder(x.rbp);
1859   sys::swapByteOrder(x.rsp);
1860   sys::swapByteOrder(x.r8);
1861   sys::swapByteOrder(x.r9);
1862   sys::swapByteOrder(x.r10);
1863   sys::swapByteOrder(x.r11);
1864   sys::swapByteOrder(x.r12);
1865   sys::swapByteOrder(x.r13);
1866   sys::swapByteOrder(x.r14);
1867   sys::swapByteOrder(x.r15);
1868   sys::swapByteOrder(x.rip);
1869   sys::swapByteOrder(x.rflags);
1870   sys::swapByteOrder(x.cs);
1871   sys::swapByteOrder(x.fs);
1872   sys::swapByteOrder(x.gs);
1873 }
1874 
1875 inline void swapStruct(x86_float_state64_t &x) {
1876   sys::swapByteOrder(x.fpu_reserved[0]);
1877   sys::swapByteOrder(x.fpu_reserved[1]);
1878   // TODO swap: fp_control_t fpu_fcw;
1879   // TODO swap: fp_status_t fpu_fsw;
1880   sys::swapByteOrder(x.fpu_fop);
1881   sys::swapByteOrder(x.fpu_ip);
1882   sys::swapByteOrder(x.fpu_cs);
1883   sys::swapByteOrder(x.fpu_rsrv2);
1884   sys::swapByteOrder(x.fpu_dp);
1885   sys::swapByteOrder(x.fpu_ds);
1886   sys::swapByteOrder(x.fpu_rsrv3);
1887   sys::swapByteOrder(x.fpu_mxcsr);
1888   sys::swapByteOrder(x.fpu_mxcsrmask);
1889   sys::swapByteOrder(x.fpu_reserved1);
1890 }
1891 
1892 inline void swapStruct(x86_exception_state64_t &x) {
1893   sys::swapByteOrder(x.trapno);
1894   sys::swapByteOrder(x.cpu);
1895   sys::swapByteOrder(x.err);
1896   sys::swapByteOrder(x.faultvaddr);
1897 }
1898 
1899 struct x86_state_hdr_t {
1900   uint32_t flavor;
1901   uint32_t count;
1902 };
1903 
1904 struct x86_thread_state_t {
1905   x86_state_hdr_t tsh;
1906   union {
1907     x86_thread_state64_t ts64;
1908     x86_thread_state32_t ts32;
1909   } uts;
1910 };
1911 
1912 struct x86_float_state_t {
1913   x86_state_hdr_t fsh;
1914   union {
1915     x86_float_state64_t fs64;
1916   } ufs;
1917 };
1918 
1919 struct x86_exception_state_t {
1920   x86_state_hdr_t esh;
1921   union {
1922     x86_exception_state64_t es64;
1923   } ues;
1924 };
1925 
1926 inline void swapStruct(x86_state_hdr_t &x) {
1927   sys::swapByteOrder(x.flavor);
1928   sys::swapByteOrder(x.count);
1929 }
1930 
1931 enum X86ThreadFlavors {
1932   x86_THREAD_STATE32 = 1,
1933   x86_FLOAT_STATE32 = 2,
1934   x86_EXCEPTION_STATE32 = 3,
1935   x86_THREAD_STATE64 = 4,
1936   x86_FLOAT_STATE64 = 5,
1937   x86_EXCEPTION_STATE64 = 6,
1938   x86_THREAD_STATE = 7,
1939   x86_FLOAT_STATE = 8,
1940   x86_EXCEPTION_STATE = 9,
1941   x86_DEBUG_STATE32 = 10,
1942   x86_DEBUG_STATE64 = 11,
1943   x86_DEBUG_STATE = 12
1944 };
1945 
1946 inline void swapStruct(x86_thread_state_t &x) {
1947   swapStruct(x.tsh);
1948   if (x.tsh.flavor == x86_THREAD_STATE64)
1949     swapStruct(x.uts.ts64);
1950 }
1951 
1952 inline void swapStruct(x86_float_state_t &x) {
1953   swapStruct(x.fsh);
1954   if (x.fsh.flavor == x86_FLOAT_STATE64)
1955     swapStruct(x.ufs.fs64);
1956 }
1957 
1958 inline void swapStruct(x86_exception_state_t &x) {
1959   swapStruct(x.esh);
1960   if (x.esh.flavor == x86_EXCEPTION_STATE64)
1961     swapStruct(x.ues.es64);
1962 }
1963 
1964 const uint32_t x86_THREAD_STATE32_COUNT =
1965     sizeof(x86_thread_state32_t) / sizeof(uint32_t);
1966 
1967 const uint32_t x86_THREAD_STATE64_COUNT =
1968     sizeof(x86_thread_state64_t) / sizeof(uint32_t);
1969 const uint32_t x86_FLOAT_STATE64_COUNT =
1970     sizeof(x86_float_state64_t) / sizeof(uint32_t);
1971 const uint32_t x86_EXCEPTION_STATE64_COUNT =
1972     sizeof(x86_exception_state64_t) / sizeof(uint32_t);
1973 
1974 const uint32_t x86_THREAD_STATE_COUNT =
1975     sizeof(x86_thread_state_t) / sizeof(uint32_t);
1976 const uint32_t x86_FLOAT_STATE_COUNT =
1977     sizeof(x86_float_state_t) / sizeof(uint32_t);
1978 const uint32_t x86_EXCEPTION_STATE_COUNT =
1979     sizeof(x86_exception_state_t) / sizeof(uint32_t);
1980 
1981 struct arm_thread_state32_t {
1982   uint32_t r[13];
1983   uint32_t sp;
1984   uint32_t lr;
1985   uint32_t pc;
1986   uint32_t cpsr;
1987 };
1988 
1989 inline void swapStruct(arm_thread_state32_t &x) {
1990   for (int i = 0; i < 13; i++)
1991     sys::swapByteOrder(x.r[i]);
1992   sys::swapByteOrder(x.sp);
1993   sys::swapByteOrder(x.lr);
1994   sys::swapByteOrder(x.pc);
1995   sys::swapByteOrder(x.cpsr);
1996 }
1997 
1998 struct arm_thread_state64_t {
1999   uint64_t x[29];
2000   uint64_t fp;
2001   uint64_t lr;
2002   uint64_t sp;
2003   uint64_t pc;
2004   uint32_t cpsr;
2005   uint32_t pad;
2006 };
2007 
2008 inline void swapStruct(arm_thread_state64_t &x) {
2009   for (int i = 0; i < 29; i++)
2010     sys::swapByteOrder(x.x[i]);
2011   sys::swapByteOrder(x.fp);
2012   sys::swapByteOrder(x.lr);
2013   sys::swapByteOrder(x.sp);
2014   sys::swapByteOrder(x.pc);
2015   sys::swapByteOrder(x.cpsr);
2016 }
2017 
2018 struct arm_state_hdr_t {
2019   uint32_t flavor;
2020   uint32_t count;
2021 };
2022 
2023 struct arm_thread_state_t {
2024   arm_state_hdr_t tsh;
2025   union {
2026     arm_thread_state32_t ts32;
2027   } uts;
2028 };
2029 
2030 inline void swapStruct(arm_state_hdr_t &x) {
2031   sys::swapByteOrder(x.flavor);
2032   sys::swapByteOrder(x.count);
2033 }
2034 
2035 enum ARMThreadFlavors {
2036   ARM_THREAD_STATE = 1,
2037   ARM_VFP_STATE = 2,
2038   ARM_EXCEPTION_STATE = 3,
2039   ARM_DEBUG_STATE = 4,
2040   ARN_THREAD_STATE_NONE = 5,
2041   ARM_THREAD_STATE64 = 6,
2042   ARM_EXCEPTION_STATE64 = 7
2043 };
2044 
2045 inline void swapStruct(arm_thread_state_t &x) {
2046   swapStruct(x.tsh);
2047   if (x.tsh.flavor == ARM_THREAD_STATE)
2048     swapStruct(x.uts.ts32);
2049 }
2050 
2051 const uint32_t ARM_THREAD_STATE_COUNT =
2052     sizeof(arm_thread_state32_t) / sizeof(uint32_t);
2053 
2054 const uint32_t ARM_THREAD_STATE64_COUNT =
2055     sizeof(arm_thread_state64_t) / sizeof(uint32_t);
2056 
2057 struct ppc_thread_state32_t {
2058   uint32_t srr0;
2059   uint32_t srr1;
2060   uint32_t r0;
2061   uint32_t r1;
2062   uint32_t r2;
2063   uint32_t r3;
2064   uint32_t r4;
2065   uint32_t r5;
2066   uint32_t r6;
2067   uint32_t r7;
2068   uint32_t r8;
2069   uint32_t r9;
2070   uint32_t r10;
2071   uint32_t r11;
2072   uint32_t r12;
2073   uint32_t r13;
2074   uint32_t r14;
2075   uint32_t r15;
2076   uint32_t r16;
2077   uint32_t r17;
2078   uint32_t r18;
2079   uint32_t r19;
2080   uint32_t r20;
2081   uint32_t r21;
2082   uint32_t r22;
2083   uint32_t r23;
2084   uint32_t r24;
2085   uint32_t r25;
2086   uint32_t r26;
2087   uint32_t r27;
2088   uint32_t r28;
2089   uint32_t r29;
2090   uint32_t r30;
2091   uint32_t r31;
2092   uint32_t ct;
2093   uint32_t xer;
2094   uint32_t lr;
2095   uint32_t ctr;
2096   uint32_t mq;
2097   uint32_t vrsave;
2098 };
2099 
2100 inline void swapStruct(ppc_thread_state32_t &x) {
2101   sys::swapByteOrder(x.srr0);
2102   sys::swapByteOrder(x.srr1);
2103   sys::swapByteOrder(x.r0);
2104   sys::swapByteOrder(x.r1);
2105   sys::swapByteOrder(x.r2);
2106   sys::swapByteOrder(x.r3);
2107   sys::swapByteOrder(x.r4);
2108   sys::swapByteOrder(x.r5);
2109   sys::swapByteOrder(x.r6);
2110   sys::swapByteOrder(x.r7);
2111   sys::swapByteOrder(x.r8);
2112   sys::swapByteOrder(x.r9);
2113   sys::swapByteOrder(x.r10);
2114   sys::swapByteOrder(x.r11);
2115   sys::swapByteOrder(x.r12);
2116   sys::swapByteOrder(x.r13);
2117   sys::swapByteOrder(x.r14);
2118   sys::swapByteOrder(x.r15);
2119   sys::swapByteOrder(x.r16);
2120   sys::swapByteOrder(x.r17);
2121   sys::swapByteOrder(x.r18);
2122   sys::swapByteOrder(x.r19);
2123   sys::swapByteOrder(x.r20);
2124   sys::swapByteOrder(x.r21);
2125   sys::swapByteOrder(x.r22);
2126   sys::swapByteOrder(x.r23);
2127   sys::swapByteOrder(x.r24);
2128   sys::swapByteOrder(x.r25);
2129   sys::swapByteOrder(x.r26);
2130   sys::swapByteOrder(x.r27);
2131   sys::swapByteOrder(x.r28);
2132   sys::swapByteOrder(x.r29);
2133   sys::swapByteOrder(x.r30);
2134   sys::swapByteOrder(x.r31);
2135   sys::swapByteOrder(x.ct);
2136   sys::swapByteOrder(x.xer);
2137   sys::swapByteOrder(x.lr);
2138   sys::swapByteOrder(x.ctr);
2139   sys::swapByteOrder(x.mq);
2140   sys::swapByteOrder(x.vrsave);
2141 }
2142 
2143 struct ppc_state_hdr_t {
2144   uint32_t flavor;
2145   uint32_t count;
2146 };
2147 
2148 struct ppc_thread_state_t {
2149   ppc_state_hdr_t tsh;
2150   union {
2151     ppc_thread_state32_t ts32;
2152   } uts;
2153 };
2154 
2155 inline void swapStruct(ppc_state_hdr_t &x) {
2156   sys::swapByteOrder(x.flavor);
2157   sys::swapByteOrder(x.count);
2158 }
2159 
2160 enum PPCThreadFlavors {
2161   PPC_THREAD_STATE = 1,
2162   PPC_FLOAT_STATE = 2,
2163   PPC_EXCEPTION_STATE = 3,
2164   PPC_VECTOR_STATE = 4,
2165   PPC_THREAD_STATE64 = 5,
2166   PPC_EXCEPTION_STATE64 = 6,
2167   PPC_THREAD_STATE_NONE = 7
2168 };
2169 
2170 inline void swapStruct(ppc_thread_state_t &x) {
2171   swapStruct(x.tsh);
2172   if (x.tsh.flavor == PPC_THREAD_STATE)
2173     swapStruct(x.uts.ts32);
2174 }
2175 
2176 const uint32_t PPC_THREAD_STATE_COUNT =
2177     sizeof(ppc_thread_state32_t) / sizeof(uint32_t);
2178 
2179 // Define a union of all load command structs
2180 #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
2181 
2182 LLVM_PACKED_START
2183 union alignas(4) macho_load_command {
2184 #include "llvm/BinaryFormat/MachO.def"
2185 };
2186 LLVM_PACKED_END
2187 
2188 inline void swapStruct(dyld_chained_fixups_header &C) {
2189   sys::swapByteOrder(C.fixups_version);
2190   sys::swapByteOrder(C.starts_offset);
2191   sys::swapByteOrder(C.imports_offset);
2192   sys::swapByteOrder(C.symbols_offset);
2193   sys::swapByteOrder(C.imports_count);
2194   sys::swapByteOrder(C.imports_format);
2195   sys::swapByteOrder(C.symbols_format);
2196 }
2197 
2198 inline void swapStruct(dyld_chained_starts_in_image &C) {
2199   sys::swapByteOrder(C.seg_count);
2200   // getStructOrErr() cannot copy the variable-length seg_info_offset array.
2201   // Its elements must be byte swapped manually.
2202 }
2203 
2204 inline void swapStruct(dyld_chained_starts_in_segment &C) {
2205   sys::swapByteOrder(C.size);
2206   sys::swapByteOrder(C.page_size);
2207   sys::swapByteOrder(C.pointer_format);
2208   sys::swapByteOrder(C.segment_offset);
2209   sys::swapByteOrder(C.max_valid_pointer);
2210   sys::swapByteOrder(C.page_count);
2211   // seg_info_offset entries must be byte swapped manually.
2212 }
2213 
2214 /* code signing attributes of a process */
2215 
2216 enum CodeSignAttrs {
2217   CS_VALID = 0x00000001,          /* dynamically valid */
2218   CS_ADHOC = 0x00000002,          /* ad hoc signed */
2219   CS_GET_TASK_ALLOW = 0x00000004, /* has get-task-allow entitlement */
2220   CS_INSTALLER = 0x00000008,      /* has installer entitlement */
2221 
2222   CS_FORCED_LV =
2223       0x00000010, /* Library Validation required by Hardened System Policy */
2224   CS_INVALID_ALLOWED = 0x00000020, /* (macOS Only) Page invalidation allowed by
2225                                       task port policy */
2226 
2227   CS_HARD = 0x00000100,             /* don't load invalid pages */
2228   CS_KILL = 0x00000200,             /* kill process if it becomes invalid */
2229   CS_CHECK_EXPIRATION = 0x00000400, /* force expiration checking */
2230   CS_RESTRICT = 0x00000800,         /* tell dyld to treat restricted */
2231 
2232   CS_ENFORCEMENT = 0x00001000, /* require enforcement */
2233   CS_REQUIRE_LV = 0x00002000,  /* require library validation */
2234   CS_ENTITLEMENTS_VALIDATED =
2235       0x00004000, /* code signature permits restricted entitlements */
2236   CS_NVRAM_UNRESTRICTED =
2237       0x00008000, /* has com.apple.rootless.restricted-nvram-variables.heritable
2238                      entitlement */
2239 
2240   CS_RUNTIME = 0x00010000,       /* Apply hardened runtime policies */
2241   CS_LINKER_SIGNED = 0x00020000, /* Automatically signed by the linker */
2242 
2243   CS_ALLOWED_MACHO =
2244       (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | CS_RESTRICT |
2245        CS_ENFORCEMENT | CS_REQUIRE_LV | CS_RUNTIME | CS_LINKER_SIGNED),
2246 
2247   CS_EXEC_SET_HARD = 0x00100000, /* set CS_HARD on any exec'ed process */
2248   CS_EXEC_SET_KILL = 0x00200000, /* set CS_KILL on any exec'ed process */
2249   CS_EXEC_SET_ENFORCEMENT =
2250       0x00400000, /* set CS_ENFORCEMENT on any exec'ed process */
2251   CS_EXEC_INHERIT_SIP =
2252       0x00800000, /* set CS_INSTALLER on any exec'ed process */
2253 
2254   CS_KILLED = 0x01000000, /* was killed by kernel for invalidity */
2255   CS_DYLD_PLATFORM =
2256       0x02000000, /* dyld used to load this is a platform binary */
2257   CS_PLATFORM_BINARY = 0x04000000, /* this is a platform binary */
2258   CS_PLATFORM_PATH =
2259       0x08000000, /* platform binary by the fact of path (osx only) */
2260 
2261   CS_DEBUGGED = 0x10000000, /* process is currently or has previously been
2262                 debugged and allowed to run with invalid pages */
2263   CS_SIGNED = 0x20000000, /* process has a signature (may have gone invalid) */
2264   CS_DEV_CODE =
2265       0x40000000, /* code is dev signed, cannot be loaded into prod signed code
2266                      (will go away with rdar://problem/28322552) */
2267   CS_DATAVAULT_CONTROLLER =
2268       0x80000000, /* has Data Vault controller entitlement */
2269 
2270   CS_ENTITLEMENT_FLAGS = (CS_GET_TASK_ALLOW | CS_INSTALLER |
2271                           CS_DATAVAULT_CONTROLLER | CS_NVRAM_UNRESTRICTED),
2272 };
2273 
2274 /* executable segment flags */
2275 
2276 enum CodeSignExecSegFlags {
2277 
2278   CS_EXECSEG_MAIN_BINARY = 0x1,     /* executable segment denotes main binary */
2279   CS_EXECSEG_ALLOW_UNSIGNED = 0x10, /* allow unsigned pages (for debugging) */
2280   CS_EXECSEG_DEBUGGER = 0x20,       /* main binary is debugger */
2281   CS_EXECSEG_JIT = 0x40,            /* JIT enabled */
2282   CS_EXECSEG_SKIP_LV = 0x80,        /* OBSOLETE: skip library validation */
2283   CS_EXECSEG_CAN_LOAD_CDHASH = 0x100, /* can bless cdhash for execution */
2284   CS_EXECSEG_CAN_EXEC_CDHASH = 0x200, /* can execute blessed cdhash */
2285 
2286 };
2287 
2288 /* Magic numbers used by Code Signing */
2289 
2290 enum CodeSignMagic {
2291   CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
2292   CSMAGIC_REQUIREMENTS =
2293       0xfade0c01, /* Requirements vector (internal requirements) */
2294   CSMAGIC_CODEDIRECTORY = 0xfade0c02,      /* CodeDirectory blob */
2295   CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
2296   CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
2297   CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171,  /* embedded entitlements */
2298   CSMAGIC_DETACHED_SIGNATURE =
2299       0xfade0cc1, /* multi-arch collection of embedded signatures */
2300   CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
2301 
2302   CS_SUPPORTSSCATTER = 0x20100,
2303   CS_SUPPORTSTEAMID = 0x20200,
2304   CS_SUPPORTSCODELIMIT64 = 0x20300,
2305   CS_SUPPORTSEXECSEG = 0x20400,
2306   CS_SUPPORTSRUNTIME = 0x20500,
2307   CS_SUPPORTSLINKAGE = 0x20600,
2308 
2309   CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
2310   CSSLOT_INFOSLOT = 1,
2311   CSSLOT_REQUIREMENTS = 2,
2312   CSSLOT_RESOURCEDIR = 3,
2313   CSSLOT_APPLICATION = 4,
2314   CSSLOT_ENTITLEMENTS = 5,
2315 
2316   CSSLOT_ALTERNATE_CODEDIRECTORIES =
2317       0x1000, /* first alternate CodeDirectory, if any */
2318   CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5, /* max number of alternate CD slots */
2319   CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT =
2320       CSSLOT_ALTERNATE_CODEDIRECTORIES +
2321       CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
2322 
2323   CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
2324   CSSLOT_IDENTIFICATIONSLOT = 0x10001,
2325   CSSLOT_TICKETSLOT = 0x10002,
2326 
2327   CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
2328   CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
2329 
2330   CS_HASHTYPE_SHA1 = 1,
2331   CS_HASHTYPE_SHA256 = 2,
2332   CS_HASHTYPE_SHA256_TRUNCATED = 3,
2333   CS_HASHTYPE_SHA384 = 4,
2334 
2335   CS_SHA1_LEN = 20,
2336   CS_SHA256_LEN = 32,
2337   CS_SHA256_TRUNCATED_LEN = 20,
2338 
2339   CS_CDHASH_LEN = 20,    /* always - larger hashes are truncated */
2340   CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
2341 
2342   /*
2343    * Currently only to support Legacy VPN plugins, and Mac App Store
2344    * but intended to replace all the various platform code, dev code etc. bits.
2345    */
2346   CS_SIGNER_TYPE_UNKNOWN = 0,
2347   CS_SIGNER_TYPE_LEGACYVPN = 5,
2348   CS_SIGNER_TYPE_MAC_APP_STORE = 6,
2349 
2350   CS_SUPPL_SIGNER_TYPE_UNKNOWN = 0,
2351   CS_SUPPL_SIGNER_TYPE_TRUSTCACHE = 7,
2352   CS_SUPPL_SIGNER_TYPE_LOCAL = 8,
2353 };
2354 
2355 struct CS_CodeDirectory {
2356   uint32_t magic;         /* magic number (CSMAGIC_CODEDIRECTORY) */
2357   uint32_t length;        /* total length of CodeDirectory blob */
2358   uint32_t version;       /* compatibility version */
2359   uint32_t flags;         /* setup and mode flags */
2360   uint32_t hashOffset;    /* offset of hash slot element at index zero */
2361   uint32_t identOffset;   /* offset of identifier string */
2362   uint32_t nSpecialSlots; /* number of special hash slots */
2363   uint32_t nCodeSlots;    /* number of ordinary (code) hash slots */
2364   uint32_t codeLimit;     /* limit to main image signature range */
2365   uint8_t hashSize;       /* size of each hash in bytes */
2366   uint8_t hashType;       /* type of hash (cdHashType* constants) */
2367   uint8_t platform;       /* platform identifier; zero if not platform binary */
2368   uint8_t pageSize;       /* log2(page size in bytes); 0 => infinite */
2369   uint32_t spare2;        /* unused (must be zero) */
2370 
2371   /* Version 0x20100 */
2372   uint32_t scatterOffset; /* offset of optional scatter vector */
2373 
2374   /* Version 0x20200 */
2375   uint32_t teamOffset; /* offset of optional team identifier */
2376 
2377   /* Version 0x20300 */
2378   uint32_t spare3;      /* unused (must be zero) */
2379   uint64_t codeLimit64; /* limit to main image signature range, 64 bits */
2380 
2381   /* Version 0x20400 */
2382   uint64_t execSegBase;  /* offset of executable segment */
2383   uint64_t execSegLimit; /* limit of executable segment */
2384   uint64_t execSegFlags; /* executable segment flags */
2385 };
2386 
2387 static_assert(sizeof(CS_CodeDirectory) == 88);
2388 
2389 struct CS_BlobIndex {
2390   uint32_t type;   /* type of entry */
2391   uint32_t offset; /* offset of entry */
2392 };
2393 
2394 struct CS_SuperBlob {
2395   uint32_t magic;  /* magic number */
2396   uint32_t length; /* total length of SuperBlob */
2397   uint32_t count;  /* number of index entries following */
2398   /* followed by Blobs in no particular order as indicated by index offsets */
2399 };
2400 
2401 enum SecCSDigestAlgorithm {
2402   kSecCodeSignatureNoHash = 0,     /* null value */
2403   kSecCodeSignatureHashSHA1 = 1,   /* SHA-1 */
2404   kSecCodeSignatureHashSHA256 = 2, /* SHA-256 */
2405   kSecCodeSignatureHashSHA256Truncated =
2406       3,                           /* SHA-256 truncated to first 20 bytes */
2407   kSecCodeSignatureHashSHA384 = 4, /* SHA-384 */
2408   kSecCodeSignatureHashSHA512 = 5, /* SHA-512 */
2409 };
2410 
2411 enum LinkerOptimizationHintKind {
2412   LOH_ARM64_ADRP_ADRP = 1,
2413   LOH_ARM64_ADRP_LDR = 2,
2414   LOH_ARM64_ADRP_ADD_LDR = 3,
2415   LOH_ARM64_ADRP_LDR_GOT_LDR = 4,
2416   LOH_ARM64_ADRP_ADD_STR = 5,
2417   LOH_ARM64_ADRP_LDR_GOT_STR = 6,
2418   LOH_ARM64_ADRP_ADD = 7,
2419   LOH_ARM64_ADRP_LDR_GOT = 8,
2420 };
2421 
2422 } // end namespace MachO
2423 } // end namespace llvm
2424 
2425 #endif