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