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