Coverage Report

Created: 2018-09-25 17:16

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