Coverage Report

Created: 2019-03-24 22:13

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