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