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