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