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