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