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