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