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