1 //===-- llvm/Support/MachO.h - The MachO file format ------------*- C -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // University of Illinois/NCSA
6 // Open Source License
7 //
8 // Copyright (c) 2003-2010 University of Illinois at Urbana-Champaign.
9 // All rights reserved.
10 //
11 // Developed by:
12 //
13 //    LLVM Team
14 //
15 //    University of Illinois at Urbana-Champaign
16 //
17 //    http://llvm.org
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining a copy
20 // of this software and associated documentation files (the "Software"), to
21 // deal with the Software without restriction, including without limitation the
22 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
23 // sell copies of the Software, and to permit persons to whom the Software is
24 // furnished to do so, subject to the following conditions:
25 //
26 // Redistributions of source code must retain the above copyright notice, this
27 // list of conditions and the following disclaimers.  Redistributions in binary
28 // form must reproduce the above copyright notice, this list of conditions and
29 // the following disclaimers in the documentation and/or other materials
30 // provided with the distribution. Neither the names of LLVM Team, Universitys
31 // of Illinois at Urbana-Champaign, nor the names of its contributors may be
32 // used to endorse or promote products derived from this Software without
33 // specific prior written permission.
34 //
35 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
40 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
41 // WITH THE SOFTWARE.
42 //
43 //===----------------------------------------------------------------------===//
44 //
45 // This file defines manifest constants for the MachO object file format.
46 //
47 //===----------------------------------------------------------------------===//
48 
49 #ifndef LLVM_SUPPORT_MACHO_H
50 #define LLVM_SUPPORT_MACHO_H
51 
52 #include <stdint.h>
53 
54 // Enums from <mach-o/loader.h>
55 enum {
56 	// Constants for the "magic" field in llvm::MachO::mach_header and
57 	// llvm::MachO::mach_header_64
58 	MH_MAGIC    = 0xFEEDFACEu,
59 	MH_CIGAM    = 0xCEFAEDFEu,
60 	MH_MAGIC_64 = 0xFEEDFACFu,
61 	MH_CIGAM_64 = 0xCFFAEDFEu,
62 	FAT_MAGIC   = 0xCAFEBABEu,
63 	FAT_CIGAM   = 0xBEBAFECAu
64 };
65 
66 enum HeaderFileType {
67 	// Constants for the "filetype" field in llvm::MachO::mach_header and
68 	// llvm::MachO::mach_header_64
69 	MH_OBJECT      = 0x1u,
70 	MH_EXECUTE     = 0x2u,
71 	MH_FVMLIB      = 0x3u,
72 	MH_CORE        = 0x4u,
73 	MH_PRELOAD     = 0x5u,
74 	MH_DYLIB       = 0x6u,
75 	MH_DYLINKER    = 0x7u,
76 	MH_BUNDLE      = 0x8u,
77 	MH_DYLIB_STUB  = 0x9u,
78 	MH_DSYM        = 0xAu,
79 	MH_KEXT_BUNDLE = 0xBu,
80 	MH_FILESET     = 0xCu
81 };
82 
83 enum {
84 	// Constant bits for the "flags" field in llvm::MachO::mach_header and
85 	// llvm::MachO::mach_header_64
86 	MH_NOUNDEFS                = 0x00000001u,
87 	MH_INCRLINK                = 0x00000002u,
88 	MH_DYLDLINK                = 0x00000004u,
89 	MH_BINDATLOAD              = 0x00000008u,
90 	MH_PREBOUND                = 0x00000010u,
91 	MH_SPLIT_SEGS              = 0x00000020u,
92 	MH_LAZY_INIT               = 0x00000040u,
93 	MH_TWOLEVEL                = 0x00000080u,
94 	MH_FORCE_FLAT              = 0x00000100u,
95 	MH_NOMULTIDEFS             = 0x00000200u,
96 	MH_NOFIXPREBINDING         = 0x00000400u,
97 	MH_PREBINDABLE             = 0x00000800u,
98 	MH_ALLMODSBOUND            = 0x00001000u,
99 	MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
100 	MH_CANONICAL               = 0x00004000u,
101 	MH_WEAK_DEFINES            = 0x00008000u,
102 	MH_BINDS_TO_WEAK           = 0x00010000u,
103 	MH_ALLOW_STACK_EXECUTION   = 0x00020000u,
104 	MH_ROOT_SAFE               = 0x00040000u,
105 	MH_SETUID_SAFE             = 0x00080000u,
106 	MH_NO_REEXPORTED_DYLIBS    = 0x00100000u,
107 	MH_PIE                     = 0x00200000u,
108 	MH_DEAD_STRIPPABLE_DYLIB   = 0x00400000u,
109 	MH_HAS_TLV_DESCRIPTORS     = 0x00800000u,
110 	MH_NO_HEAP_EXECUTION       = 0x01000000u,
111 	MH_APP_EXTENSION_SAFE      = 0x02000000u
112 };
113 
114 enum {
115 	// Flags for the "cmd" field in llvm::MachO::load_command
116 	LC_REQ_DYLD    = 0x80000000u
117 };
118 
119 enum LoadCommandType {
120 	// Constants for the "cmd" field in llvm::MachO::load_command
121 	LC_SEGMENT              = 0x00000001u,
122 	LC_SYMTAB               = 0x00000002u,
123 	LC_SYMSEG               = 0x00000003u,
124 	LC_THREAD               = 0x00000004u,
125 	LC_UNIXTHREAD           = 0x00000005u,
126 	LC_LOADFVMLIB           = 0x00000006u,
127 	LC_IDFVMLIB             = 0x00000007u,
128 	LC_IDENT                = 0x00000008u,
129 	LC_FVMFILE              = 0x00000009u,
130 	LC_PREPAGE              = 0x0000000Au,
131 	LC_DYSYMTAB             = 0x0000000Bu,
132 	LC_LOAD_DYLIB           = 0x0000000Cu,
133 	LC_ID_DYLIB             = 0x0000000Du,
134 	LC_LOAD_DYLINKER        = 0x0000000Eu,
135 	LC_ID_DYLINKER          = 0x0000000Fu,
136 	LC_PREBOUND_DYLIB       = 0x00000010u,
137 	LC_ROUTINES             = 0x00000011u,
138 	LC_SUB_FRAMEWORK        = 0x00000012u,
139 	LC_SUB_UMBRELLA         = 0x00000013u,
140 	LC_SUB_CLIENT           = 0x00000014u,
141 	LC_SUB_LIBRARY          = 0x00000015u,
142 	LC_TWOLEVEL_HINTS       = 0x00000016u,
143 	LC_PREBIND_CKSUM        = 0x00000017u,
144 	LC_LOAD_WEAK_DYLIB      = 0x80000018u,
145 	LC_SEGMENT_64           = 0x00000019u,
146 	LC_ROUTINES_64          = 0x0000001Au,
147 	LC_UUID                 = 0x0000001Bu,
148 	LC_RPATH                = 0x8000001Cu,
149 	LC_CODE_SIGNATURE       = 0x0000001Du,
150 	LC_SEGMENT_SPLIT_INFO   = 0x0000001Eu,
151 	LC_REEXPORT_DYLIB       = 0x8000001Fu,
152 	LC_LAZY_LOAD_DYLIB      = 0x00000020u,
153 	LC_ENCRYPTION_INFO      = 0x00000021u,
154 	LC_DYLD_INFO            = 0x00000022u,
155 	LC_DYLD_INFO_ONLY       = 0x80000022u,
156 	LC_LOAD_UPWARD_DYLIB    = 0x80000023u,
157 	LC_VERSION_MIN_MACOSX   = 0x00000024u,
158 	LC_VERSION_MIN_IPHONEOS = 0x00000025u,
159 	LC_FUNCTION_STARTS      = 0x00000026u,
160 	LC_DYLD_ENVIRONMENT     = 0x00000027u,
161 	LC_MAIN                 = 0x80000028u,
162 	LC_DATA_IN_CODE         = 0x00000029u,
163 	LC_SOURCE_VERSION       = 0x0000002Au,
164 	LC_DYLIB_CODE_SIGN_DRS  = 0x0000002Bu,
165 	LC_ENCRYPTION_INFO_64   = 0x0000002Cu,
166 	LC_LINKER_OPTION        = 0x0000002Du,
167 	LC_LINKER_OPTIMIZATION_HINT = 0x0000002Eu,
168 	LC_VERSION_MIN_TVOS     = 0x0000002Fu,
169 	LC_VERSION_MIN_WATCHOS  = 0x00000030u,
170 	LC_NOTE                 = 0x00000031u,
171 	LC_BUILD_VERSION        = 0x00000032u,
172 	LC_DYLD_EXPORTS_TRIE    = 0x80000033u,
173 	LC_DYLD_CHAINED_FIXUPS  = 0x80000034u,
174 	LC_KEXT  = 0x80000035u, /* TODO: get the right name */
175 /*
176 Load command 9
177        cmd LC_BUILD_VERSION
178    cmdsize 32
179   platform macos
180        sdk 10.14
181      minos 10.14
182     ntools 1
183       tool ld
184    version 409.11
185 */
186 };
187 
188 enum {
189 	// Constant bits for the "flags" field in llvm::MachO::segment_command
190 	SG_HIGHVM              = 0x1u,
191 	SG_FVMLIB              = 0x2u,
192 	SG_NORELOC             = 0x4u,
193 	SG_PROTECTED_VERSION_1 = 0x8u,
194 
195 
196 	// Constant masks for the "flags" field in llvm::MachO::section and
197 	// llvm::MachO::section_64
198 	SECTION_TYPE           = 0x000000ffu, // SECTION_TYPE
199 	SECTION_ATTRIBUTES     = 0xffffff00u, // SECTION_ATTRIBUTES
200 	SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
201 	SECTION_ATTRIBUTES_SYS = 0x00ffff00u  // SECTION_ATTRIBUTES_SYS
202 };
203 
204 /// These are the section type and attributes fields.  A MachO section can
205 /// have only one Type, but can have any of the attributes specified.
206 enum SectionType {
207 	// Constant masks for the "flags[7:0]" field in llvm::MachO::section and
208 	// llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
209 
210 	/// S_REGULAR - Regular section.
211 	S_REGULAR                             = 0x00u,
212 	/// S_ZEROFILL - Zero fill on demand section.
213 	S_ZEROFILL                            = 0x01u,
214 	/// S_CSTRING_LITERALS - Section with literal C strings.
215 	S_CSTRING_LITERALS                    = 0x02u,
216 	/// S_4BYTE_LITERALS - Section with 4 byte literals.
217 	S_4BYTE_LITERALS                      = 0x03u,
218 	/// S_8BYTE_LITERALS - Section with 8 byte literals.
219 	S_8BYTE_LITERALS                      = 0x04u,
220 	/// S_LITERAL_POINTERS - Section with pointers to literals.
221 	S_LITERAL_POINTERS                    = 0x05u,
222 	/// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
223 	S_NON_LAZY_SYMBOL_POINTERS            = 0x06u,
224 	/// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
225 	S_LAZY_SYMBOL_POINTERS                = 0x07u,
226 	/// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
227 	/// the Reserved2 field.
228 	S_SYMBOL_STUBS                        = 0x08u,
229 	/// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
230 	/// initialization.
231 	S_MOD_INIT_FUNC_POINTERS              = 0x09u,
232 	/// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
233 	/// termination.
234 	S_MOD_TERM_FUNC_POINTERS              = 0x0au,
235 	/// S_COALESCED - Section contains symbols that are to be coalesced.
236 	S_COALESCED                           = 0x0bu,
237 	/// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
238 	/// gigabytes).
239 	S_GB_ZEROFILL                         = 0x0cu,
240 	/// S_INTERPOSING - Section with only pairs of function pointers for
241 	/// interposing.
242 	S_INTERPOSING                         = 0x0du,
243 	/// S_16BYTE_LITERALS - Section with only 16 byte literals.
244 	S_16BYTE_LITERALS                     = 0x0eu,
245 	/// S_DTRACE_DOF - Section contains DTrace Object Format.
246 	S_DTRACE_DOF                          = 0x0fu,
247 	/// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
248 	/// lazy loaded dylibs.
249 	S_LAZY_DYLIB_SYMBOL_POINTERS          = 0x10u,
250 	/// S_THREAD_LOCAL_REGULAR - Thread local data section.
251 	S_THREAD_LOCAL_REGULAR                = 0x11u,
252 	/// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
253 	S_THREAD_LOCAL_ZEROFILL               = 0x12u,
254 	/// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
255 	/// structure data.
256 	S_THREAD_LOCAL_VARIABLES              = 0x13u,
257 	/// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
258 	/// local structures.
259 	S_THREAD_LOCAL_VARIABLE_POINTERS      = 0x14u,
260 	/// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
261 	/// variable initialization pointers to functions.
262 	S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
263 
264 	LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
265 };
266 
267 enum {
268 	// Constant masks for the "flags[31:24]" field in llvm::MachO::section and
269 	// llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
270 
271 	/// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
272 	/// instructions.
273 	S_ATTR_PURE_INSTRUCTIONS   = 0x80000000u,
274 	/// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
275 	/// in a ranlib table of contents.
276 	S_ATTR_NO_TOC              = 0x40000000u,
277 	/// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
278 	/// in files with the MY_DYLDLINK flag.
279 	S_ATTR_STRIP_STATIC_SYMS   = 0x20000000u,
280 	/// S_ATTR_NO_DEAD_STRIP - No dead stripping.
281 	S_ATTR_NO_DEAD_STRIP       = 0x10000000u,
282 	/// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
283 	S_ATTR_LIVE_SUPPORT        = 0x08000000u,
284 	/// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
285 	/// dyld.
286 	S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
287 	/// S_ATTR_DEBUG - A debug section.
288 	S_ATTR_DEBUG               = 0x02000000u,
289 
290 	// Constant masks for the "flags[23:8]" field in llvm::MachO::section and
291 	// llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
292 
293 	/// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
294 	S_ATTR_SOME_INSTRUCTIONS   = 0x00000400u,
295 	/// S_ATTR_EXT_RELOC - Section has external relocation entries.
296 	S_ATTR_EXT_RELOC           = 0x00000200u,
297 	/// S_ATTR_LOC_RELOC - Section has local relocation entries.
298 	S_ATTR_LOC_RELOC           = 0x00000100u,
299 
300 	// Constant masks for the value of an indirect symbol in an indirect
301 	// symbol table
302 	INDIRECT_SYMBOL_LOCAL = 0x80000000u,
303 	INDIRECT_SYMBOL_ABS   = 0x40000000u
304 };
305 
306 enum DataRegionType {
307 	// Constants for the "kind" field in a data_in_code_entry structure
308 	DICE_KIND_DATA             = 1u,
309 	DICE_KIND_JUMP_TABLE8      = 2u,
310 	DICE_KIND_JUMP_TABLE16     = 3u,
311 	DICE_KIND_JUMP_TABLE32     = 4u,
312 	DICE_KIND_ABS_JUMP_TABLE32 = 5u
313 };
314 
315 enum RebaseType {
316 	REBASE_TYPE_POINTER         = 1u,
317 	REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
318 	REBASE_TYPE_TEXT_PCREL32    = 3u
319 };
320 
321 enum {
322 	REBASE_OPCODE_MASK    = 0xF0u,
323 	REBASE_IMMEDIATE_MASK = 0x0Fu
324 };
325 
326 enum RebaseOpcode {
327 	REBASE_OPCODE_DONE                               = 0x00u,
328 	REBASE_OPCODE_SET_TYPE_IMM                       = 0x10u,
329 	REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB        = 0x20u,
330 	REBASE_OPCODE_ADD_ADDR_ULEB                      = 0x30u,
331 	REBASE_OPCODE_ADD_ADDR_IMM_SCALED                = 0x40u,
332 	REBASE_OPCODE_DO_REBASE_IMM_TIMES                = 0x50u,
333 	REBASE_OPCODE_DO_REBASE_ULEB_TIMES               = 0x60u,
334 	REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB            = 0x70u,
335 	REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
336 };
337 
338 enum BindType {
339 	BIND_TYPE_POINTER         = 1u,
340 	BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
341 	BIND_TYPE_TEXT_PCREL32    = 3u
342 };
343 
344 enum BindSpecialDylib {
345 	BIND_SPECIAL_DYLIB_SELF            =  0,
346 	BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
347 	BIND_SPECIAL_DYLIB_FLAT_LOOKUP     = -2
348 };
349 
350 enum {
351 	BIND_SYMBOL_FLAGS_WEAK_IMPORT         = 0x1u,
352 	BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
353 
354 	BIND_OPCODE_MASK                      = 0xF0u,
355 	BIND_IMMEDIATE_MASK                   = 0x0Fu
356 };
357 
358 enum BindOpcode {
359 	BIND_OPCODE_DONE                             = 0x00u,
360 	BIND_OPCODE_SET_DYLIB_ORDINAL_IMM            = 0x10u,
361 	BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB           = 0x20u,
362 	BIND_OPCODE_SET_DYLIB_SPECIAL_IMM            = 0x30u,
363 	BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM    = 0x40u,
364 	BIND_OPCODE_SET_TYPE_IMM                     = 0x50u,
365 	BIND_OPCODE_SET_ADDEND_SLEB                  = 0x60u,
366 	BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB      = 0x70u,
367 	BIND_OPCODE_ADD_ADDR_ULEB                    = 0x80u,
368 	BIND_OPCODE_DO_BIND                          = 0x90u,
369 	BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB            = 0xA0u,
370 	BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED      = 0xB0u,
371 	BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u,
372 	BIND_OPCODE_THREADED                         = 0xD0u
373 };
374 
375 enum BindSubOpcode {
376 	BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00,
377 	BIND_SUBOPCODE_THREADED_APPLY                            = 0x01,
378 };
379 
380 enum {
381 	EXPORT_SYMBOL_FLAGS_KIND_MASK           = 0x03u,
382 	EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION     = 0x04u,
383 	EXPORT_SYMBOL_FLAGS_REEXPORT            = 0x08u,
384 	EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER   = 0x10u
385 };
386 
387 enum ExportSymbolKind {
388 	EXPORT_SYMBOL_FLAGS_KIND_REGULAR        = 0x00u,
389 	EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL   = 0x01u,
390 	EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE       = 0x02u
391 };
392 
393 
394 enum {
395 	// Constant masks for the "n_type" field in llvm::MachO::nlist and
396 	// llvm::MachO::nlist_64
397 	N_STAB = 0xe0,
398 	N_PEXT = 0x10,
399 	N_TYPE = 0x0e,
400 	N_EXT  = 0x01
401 };
402 
403 enum NListType {
404 	// Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
405 	// llvm::MachO::nlist_64
406 	N_UNDF = 0x0u,
407 	N_ABS  = 0x2u,
408 	N_SECT = 0xeu,
409 	N_PBUD = 0xcu,
410 	N_INDR = 0xau
411 };
412 
413 enum SectionOrdinal {
414 	// Constants for the "n_sect" field in llvm::MachO::nlist and
415 	// llvm::MachO::nlist_64
416 	NO_SECT  = 0u,
417 	MAX_SECT = 0xffu
418 };
419 
420 enum {
421 	// Constant masks for the "n_desc" field in llvm::MachO::nlist and
422 	// llvm::MachO::nlist_64
423 	// The low 3 bits are the for the REFERENCE_TYPE.
424 	REFERENCE_TYPE                            = 0x7,
425 	REFERENCE_FLAG_UNDEFINED_NON_LAZY         = 0,
426 	REFERENCE_FLAG_UNDEFINED_LAZY             = 1,
427 	REFERENCE_FLAG_DEFINED                    = 2,
428 	REFERENCE_FLAG_PRIVATE_DEFINED            = 3,
429 	REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
430 	REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY     = 5,
431 	// Flag bits (some overlap with the library ordinal bits).
432 	N_ARM_THUMB_DEF   = 0x0008u,
433 	REFERENCED_DYNAMICALLY = 0x0010u,
434 	N_NO_DEAD_STRIP   = 0x0020u,
435 	N_WEAK_REF        = 0x0040u,
436 	N_WEAK_DEF        = 0x0080u,
437 	N_SYMBOL_RESOLVER = 0x0100u,
438 	N_ALT_ENTRY       = 0x0200u,
439 	// For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
440 	// as these are in the top 8 bits.
441 	SELF_LIBRARY_ORDINAL   = 0x0,
442 	MAX_LIBRARY_ORDINAL    = 0xfd,
443 	DYNAMIC_LOOKUP_ORDINAL = 0xfe,
444 	EXECUTABLE_ORDINAL     = 0xff
445 };
446 
447 enum StabType {
448 	// Constant values for the "n_type" field in llvm::MachO::nlist and
449 	// llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
450 	N_GSYM    = 0x20u,
451 	N_FNAME   = 0x22u,
452 	N_FUN     = 0x24u,
453 	N_STSYM   = 0x26u,
454 	N_LCSYM   = 0x28u,
455 	N_BNSYM   = 0x2Eu,
456 	N_PC      = 0x30u,
457 	N_AST     = 0x32u,
458 	N_OPT     = 0x3Cu,
459 	N_RSYM    = 0x40u,
460 	N_SLINE   = 0x44u,
461 	N_ENSYM   = 0x4Eu,
462 	N_SSYM    = 0x60u,
463 	N_SO      = 0x64u,
464 	N_OSO     = 0x66u,
465 	N_LSYM    = 0x80u,
466 	N_BINCL   = 0x82u,
467 	N_SOL     = 0x84u,
468 	N_PARAMS  = 0x86u,
469 	N_VERSION = 0x88u,
470 	N_OLEVEL  = 0x8Au,
471 	N_PSYM    = 0xA0u,
472 	N_EINCL   = 0xA2u,
473 	N_ENTRY   = 0xA4u,
474 	N_LBRAC   = 0xC0u,
475 	N_EXCL    = 0xC2u,
476 	N_RBRAC   = 0xE0u,
477 	N_BCOMM   = 0xE2u,
478 	N_ECOMM   = 0xE4u,
479 	N_ECOML   = 0xE8u,
480 	N_LENG    = 0xFEu
481 };
482 
483 enum {
484 	// Constant values for the r_symbolnum field in an
485 	// llvm::MachO::relocation_info structure when r_extern is 0.
486 	R_ABS = 0,
487 
488 	// Constant bits for the r_address field in an
489 	// llvm::MachO::relocation_info structure.
490 	R_SCATTERED = 0x80000000
491 };
492 
493 enum RelocationInfoType {
494 	// Constant values for the r_type field in an
495 	// llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
496 	// structure.
497 	GENERIC_RELOC_VANILLA        = 0,
498 	GENERIC_RELOC_PAIR           = 1,
499 	GENERIC_RELOC_SECTDIFF       = 2,
500 	GENERIC_RELOC_PB_LA_PTR      = 3,
501 	GENERIC_RELOC_LOCAL_SECTDIFF = 4,
502 	GENERIC_RELOC_TLV            = 5,
503 
504 	// Constant values for the r_type field in a PowerPC architecture
505 	// llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
506 	// structure.
507 	PPC_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
508 	PPC_RELOC_PAIR               = GENERIC_RELOC_PAIR,
509 	PPC_RELOC_BR14               = 2,
510 	PPC_RELOC_BR24               = 3,
511 	PPC_RELOC_HI16               = 4,
512 	PPC_RELOC_LO16               = 5,
513 	PPC_RELOC_HA16               = 6,
514 	PPC_RELOC_LO14               = 7,
515 	PPC_RELOC_SECTDIFF           = 8,
516 	PPC_RELOC_PB_LA_PTR          = 9,
517 	PPC_RELOC_HI16_SECTDIFF      = 10,
518 	PPC_RELOC_LO16_SECTDIFF      = 11,
519 	PPC_RELOC_HA16_SECTDIFF      = 12,
520 	PPC_RELOC_JBSR               = 13,
521 	PPC_RELOC_LO14_SECTDIFF      = 14,
522 	PPC_RELOC_LOCAL_SECTDIFF     = 15,
523 
524 	// Constant values for the r_type field in an ARM architecture
525 	// llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
526 	// structure.
527 	ARM_RELOC_VANILLA            = GENERIC_RELOC_VANILLA,
528 	ARM_RELOC_PAIR               = GENERIC_RELOC_PAIR,
529 	ARM_RELOC_SECTDIFF           = GENERIC_RELOC_SECTDIFF,
530 	ARM_RELOC_LOCAL_SECTDIFF     = 3,
531 	ARM_RELOC_PB_LA_PTR          = 4,
532 	ARM_RELOC_BR24               = 5,
533 	ARM_THUMB_RELOC_BR22         = 6,
534 	ARM_THUMB_32BIT_BRANCH       = 7, // obsolete
535 	ARM_RELOC_HALF               = 8,
536 	ARM_RELOC_HALF_SECTDIFF      = 9,
537 
538 	// Constant values for the r_type field in an ARM64 architecture
539 	// llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
540 	// structure.
541 
542 	// For pointers.
543 	ARM64_RELOC_UNSIGNED            = 0,
544 	// Must be followed by an ARM64_RELOC_UNSIGNED
545 	ARM64_RELOC_SUBTRACTOR          = 1,
546 	// A B/BL instruction with 26-bit displacement.
547 	ARM64_RELOC_BRANCH26            = 2,
548 	// PC-rel distance to page of target.
549 	ARM64_RELOC_PAGE21              = 3,
550 	// Offset within page, scaled by r_length.
551 	ARM64_RELOC_PAGEOFF12           = 4,
552 	// PC-rel distance to page of GOT slot.
553 	ARM64_RELOC_GOT_LOAD_PAGE21     = 5,
554 	// Offset within page of GOT slot, scaled by r_length.
555 	ARM64_RELOC_GOT_LOAD_PAGEOFF12  = 6,
556 	// For pointers to GOT slots.
557 	ARM64_RELOC_POINTER_TO_GOT      = 7,
558 	// PC-rel distance to page of TLVP slot.
559 	ARM64_RELOC_TLVP_LOAD_PAGE21    = 8,
560 	// Offset within page of TLVP slot, scaled by r_length.
561 	ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
562 	// Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
563 	ARM64_RELOC_ADDEND              = 10,
564 
565 
566 	// Constant values for the r_type field in an x86_64 architecture
567 	// llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
568 	// structure
569 	X86_64_RELOC_UNSIGNED        = 0,
570 	X86_64_RELOC_SIGNED          = 1,
571 	X86_64_RELOC_BRANCH          = 2,
572 	X86_64_RELOC_GOT_LOAD        = 3,
573 	X86_64_RELOC_GOT             = 4,
574 	X86_64_RELOC_SUBTRACTOR      = 5,
575 	X86_64_RELOC_SIGNED_1        = 6,
576 	X86_64_RELOC_SIGNED_2        = 7,
577 	X86_64_RELOC_SIGNED_4        = 8,
578 	X86_64_RELOC_TLV             = 9
579 };
580 
581 // Values for segment_command.initprot.
582 // From <mach/vm_prot.h>
583 enum {
584 	VM_PROT_READ    = 0x1,
585 	VM_PROT_WRITE   = 0x2,
586 	VM_PROT_EXECUTE = 0x4
587 };
588 
589 
590 // Structs from <mach-o/loader.h>
591 
592 struct mach_header {
593 	uint32_t magic;
594 	uint32_t cputype;
595 	uint32_t cpusubtype;
596 	uint32_t filetype;
597 	uint32_t ncmds;
598 	uint32_t sizeofcmds;
599 	uint32_t flags;
600 };
601 
602 struct mach_header_64 {
603 	uint32_t magic;
604 	uint32_t cputype;
605 	uint32_t cpusubtype;
606 	uint32_t filetype;
607 	uint32_t ncmds;
608 	uint32_t sizeofcmds;
609 	uint32_t flags;
610 	uint32_t reserved;
611 };
612 
613 struct load_command {
614 	uint32_t cmd;
615 	uint32_t cmdsize;
616 };
617 
618 struct segment_command {
619 	uint32_t cmd;
620 	uint32_t cmdsize;
621 	char segname[16];
622 	uint32_t vmaddr;
623 	uint32_t vmsize;
624 	uint32_t fileoff;
625 	uint32_t filesize;
626 	uint32_t maxprot;
627 	uint32_t initprot;
628 	uint32_t nsects;
629 	uint32_t flags;
630 };
631 
632 struct segment_command_64 {
633 	uint32_t cmd;
634 	uint32_t cmdsize;
635 	char segname[16];
636 	uint64_t vmaddr;
637 	uint64_t vmsize;
638 	uint64_t fileoff;
639 	uint64_t filesize;
640 	uint32_t maxprot;
641 	uint32_t initprot;
642 	uint32_t nsects;
643 	uint32_t flags;
644 };
645 
646 struct section {
647 	char sectname[16];
648 	char segname[16];
649 	uint32_t addr;
650 	uint32_t size;
651 	uint32_t offset;
652 	uint32_t align;
653 	uint32_t reloff;
654 	uint32_t nreloc;
655 	uint32_t flags;
656 	uint32_t reserved1;
657 	uint32_t reserved2;
658 };
659 
660 struct section_64 {
661 	char sectname[16];
662 	char segname[16];
663 	uint64_t addr;
664 	uint64_t size;
665 	uint32_t offset;
666 	uint32_t align;
667 	uint32_t reloff;
668 	uint32_t nreloc;
669 	uint32_t flags;
670 	uint32_t reserved1;
671 	uint32_t reserved2;
672 	uint32_t reserved3;
673 };
674 
675 struct fvmlib {
676 	uint32_t name;
677 	uint32_t minor_version;
678 	uint32_t header_addr;
679 };
680 
681 struct fvmlib_command {
682 	uint32_t  cmd;
683 	uint32_t cmdsize;
684 	struct fvmlib fvmlib;
685 };
686 
687 struct dylib {
688 	uint32_t name;
689 	uint32_t timestamp;
690 	uint32_t current_version;
691 	uint32_t compatibility_version;
692 };
693 
694 struct dylib_command {
695 	uint32_t cmd;
696 	uint32_t cmdsize;
697 	struct dylib dylib;
698 };
699 
700 struct sub_framework_command {
701 	uint32_t cmd;
702 	uint32_t cmdsize;
703 	uint32_t umbrella;
704 };
705 
706 struct sub_client_command {
707 	uint32_t cmd;
708 	uint32_t cmdsize;
709 	uint32_t client;
710 };
711 
712 struct sub_umbrella_command {
713 	uint32_t cmd;
714 	uint32_t cmdsize;
715 	uint32_t sub_umbrella;
716 };
717 
718 struct sub_library_command {
719 	uint32_t cmd;
720 	uint32_t cmdsize;
721 	uint32_t sub_library;
722 };
723 
724 struct prebound_dylib_command {
725 	uint32_t cmd;
726 	uint32_t cmdsize;
727 	uint32_t name;
728 	uint32_t nmodules;
729 	uint32_t linked_modules;
730 };
731 
732 struct dylinker_command {
733 	uint32_t cmd;
734 	uint32_t cmdsize;
735 	uint32_t name;
736 };
737 
738 struct thread_command {
739 	uint32_t cmd;
740 	uint32_t cmdsize;
741 };
742 
743 struct routines_command {
744 	uint32_t cmd;
745 	uint32_t cmdsize;
746 	uint32_t init_address;
747 	uint32_t init_module;
748 	uint32_t reserved1;
749 	uint32_t reserved2;
750 	uint32_t reserved3;
751 	uint32_t reserved4;
752 	uint32_t reserved5;
753 	uint32_t reserved6;
754 };
755 
756 struct routines_command_64 {
757 	uint32_t cmd;
758 	uint32_t cmdsize;
759 	uint64_t init_address;
760 	uint64_t init_module;
761 	uint64_t reserved1;
762 	uint64_t reserved2;
763 	uint64_t reserved3;
764 	uint64_t reserved4;
765 	uint64_t reserved5;
766 	uint64_t reserved6;
767 };
768 
769 struct symtab_command {
770 	uint32_t cmd;
771 	uint32_t cmdsize;
772 	uint32_t symoff;
773 	uint32_t nsyms;
774 	uint32_t stroff;
775 	uint32_t strsize;
776 };
777 
778 struct dysymtab_command {
779 	uint32_t cmd;
780 	uint32_t cmdsize;
781 	uint32_t ilocalsym;
782 	uint32_t nlocalsym;
783 	uint32_t iextdefsym;
784 	uint32_t nextdefsym;
785 	uint32_t iundefsym;
786 	uint32_t nundefsym;
787 	uint32_t tocoff;
788 	uint32_t ntoc;
789 	uint32_t modtaboff;
790 	uint32_t nmodtab;
791 	uint32_t extrefsymoff;
792 	uint32_t nextrefsyms;
793 	uint32_t indirectsymoff;
794 	uint32_t nindirectsyms;
795 	uint32_t extreloff;
796 	uint32_t nextrel;
797 	uint32_t locreloff;
798 	uint32_t nlocrel;
799 };
800 
801 struct dylib_table_of_contents {
802 	uint32_t symbol_index;
803 	uint32_t module_index;
804 };
805 
806 struct dylib_module {
807 	uint32_t module_name;
808 	uint32_t iextdefsym;
809 	uint32_t nextdefsym;
810 	uint32_t irefsym;
811 	uint32_t nrefsym;
812 	uint32_t ilocalsym;
813 	uint32_t nlocalsym;
814 	uint32_t iextrel;
815 	uint32_t nextrel;
816 	uint32_t iinit_iterm;
817 	uint32_t ninit_nterm;
818 	uint32_t objc_module_info_addr;
819 	uint32_t objc_module_info_size;
820 };
821 
822 struct dylib_module_64 {
823 	uint32_t module_name;
824 	uint32_t iextdefsym;
825 	uint32_t nextdefsym;
826 	uint32_t irefsym;
827 	uint32_t nrefsym;
828 	uint32_t ilocalsym;
829 	uint32_t nlocalsym;
830 	uint32_t iextrel;
831 	uint32_t nextrel;
832 	uint32_t iinit_iterm;
833 	uint32_t ninit_nterm;
834 	uint32_t objc_module_info_size;
835 	uint64_t objc_module_info_addr;
836 };
837 
838 struct dylib_reference {
839 	uint32_t isym:24,
840 	flags:8;
841 };
842 
843 
844 struct twolevel_hints_command {
845 	uint32_t cmd;
846 	uint32_t cmdsize;
847 	uint32_t offset;
848 	uint32_t nhints;
849 };
850 
851 struct twolevel_hint {
852 	uint32_t isub_image:8,
853 	itoc:24;
854 };
855 
856 struct prebind_cksum_command {
857 	uint32_t cmd;
858 	uint32_t cmdsize;
859 	uint32_t cksum;
860 };
861 
862 struct uuid_command {
863 	uint32_t cmd;
864 	uint32_t cmdsize;
865 	uint8_t uuid[16];
866 };
867 
868 struct rpath_command {
869 	uint32_t cmd;
870 	uint32_t cmdsize;
871 	uint32_t path;
872 };
873 
874 struct linkedit_data_command {
875 	uint32_t cmd;
876 	uint32_t cmdsize;
877 	uint32_t dataoff;
878 	uint32_t datasize;
879 };
880 
881 struct data_in_code_entry {
882 	uint32_t offset;
883 	uint16_t length;
884 	uint16_t kind;
885 };
886 
887 struct source_version_command {
888 	uint32_t cmd;
889 	uint32_t cmdsize;
890 	uint64_t version;
891 };
892 
893 struct encryption_info_command {
894 	uint32_t cmd;
895 	uint32_t cmdsize;
896 	uint32_t cryptoff;
897 	uint32_t cryptsize;
898 	uint32_t cryptid;
899 };
900 
901 struct encryption_info_command_64 {
902 	uint32_t cmd;
903 	uint32_t cmdsize;
904 	uint32_t cryptoff;
905 	uint32_t cryptsize;
906 	uint32_t cryptid;
907 	uint32_t pad;
908 };
909 
910 struct version_min_command {
911 	uint32_t cmd;       // LC_VERSION_MIN_MACOSX or
912 	// LC_VERSION_MIN_IPHONEOS
913 	uint32_t cmdsize;   // sizeof(struct version_min_command)
914 	uint32_t version;   // X.Y.Z is encoded in nibbles xxxx.yy.zz
915 	uint32_t sdk;       // X.Y.Z is encoded in nibbles xxxx.yy.zz
916 };
917 
918 struct dyld_info_command {
919 	uint32_t cmd;
920 	uint32_t cmdsize;
921 	uint32_t rebase_off;
922 	uint32_t rebase_size;
923 	uint32_t bind_off;
924 	uint32_t bind_size;
925 	uint32_t weak_bind_off;
926 	uint32_t weak_bind_size;
927 	uint32_t lazy_bind_off;
928 	uint32_t lazy_bind_size;
929 	uint32_t export_off;
930 	uint32_t export_size;
931 };
932 
933 struct linker_option_command {
934 	uint32_t cmd;
935 	uint32_t cmdsize;
936 	uint32_t count;
937 };
938 
939 struct symseg_command {
940 	uint32_t cmd;
941 	uint32_t cmdsize;
942 	uint32_t offset;
943 	uint32_t size;
944 };
945 
946 struct ident_command {
947 	uint32_t cmd;
948 	uint32_t cmdsize;
949 };
950 
951 struct fvmfile_command {
952 	uint32_t cmd;
953 	uint32_t cmdsize;
954 	uint32_t name;
955 	uint32_t header_addr;
956 };
957 
958 struct tlv_descriptor_32 {
959 	uint32_t thunk;
960 	uint32_t key;
961 	uint32_t offset;
962 };
963 
964 struct tlv_descriptor_64 {
965 	uint64_t thunk;
966 	uint64_t key;
967 	uint64_t offset;
968 };
969 
970 struct tlv_descriptor {
971 	uintptr_t thunk;
972 	uintptr_t key;
973 	uintptr_t offset;
974 };
975 
976 struct entry_point_command {
977 	uint32_t cmd;
978 	uint32_t cmdsize;
979 	uint64_t entryoff;
980 	uint64_t stacksize;
981 };
982 
983 
984 // Structs from <mach-o/fat.h>
985 struct fat_header {
986 	uint32_t magic;
987 	uint32_t nfat_arch;
988 };
989 
990 struct fat_arch {
991 	uint32_t cputype;
992 	uint32_t cpusubtype;
993 	uint32_t offset;
994 	uint32_t size;
995 	uint32_t align;
996 };
997 
998 // Structs from <mach-o/reloc.h>
999 struct relocation_info {
1000 	int32_t r_address;
1001 	uint32_t r_symbolnum:24,
1002 	r_pcrel:1,
1003 	r_length:2,
1004 	r_extern:1,
1005 	r_type:4;
1006 };
1007 
1008 struct scattered_relocation_info {
1009 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
1010 	uint32_t r_scattered:1,
1011 	r_pcrel:1,
1012 	r_length:2,
1013 	r_type:4,
1014 	r_address:24;
1015 #else
1016 	uint32_t r_address:24,
1017 	r_type:4,
1018 	r_length:2,
1019 	r_pcrel:1,
1020 	r_scattered:1;
1021 #endif
1022 	int32_t r_value;
1023 };
1024 
1025 // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
1026 struct any_relocation_info {
1027 	uint32_t r_word0, r_word1;
1028 };
1029 
1030 // Structs from <mach-o/nlist.h>
1031 struct nlist_base {
1032 	uint32_t n_strx;
1033 	uint8_t n_type;
1034 	uint8_t n_sect;
1035 	uint16_t n_desc;
1036 };
1037 
1038 struct nlist {
1039 	uint32_t n_strx;
1040 	uint8_t n_type;
1041 	uint8_t n_sect;
1042 	int16_t n_desc;
1043 	uint32_t n_value;
1044 };
1045 
1046 struct nlist_64 {
1047 	uint32_t n_strx;
1048 	uint8_t n_type;
1049 	uint8_t n_sect;
1050 	uint16_t n_desc;
1051 	uint64_t n_value;
1052 };
1053 
1054 // Get/Set functions from <mach-o/nlist.h>
1055 
GET_LIBRARY_ORDINAL(uint16_t n_desc)1056 static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
1057 	return (((n_desc) >> 8u) & 0xffu);
1058 }
1059 
SET_LIBRARY_ORDINAL(uint16_t * n_desc,uint8_t ordinal)1060 static inline void SET_LIBRARY_ORDINAL(uint16_t *n_desc, uint8_t ordinal) {
1061 	*n_desc = (((*n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
1062 }
1063 
GET_COMM_ALIGN(uint16_t n_desc)1064 static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc) {
1065 	return (n_desc >> 8u) & 0x0fu;
1066 }
1067 
SET_COMM_ALIGN(uint16_t * n_desc,uint8_t align)1068 static inline void SET_COMM_ALIGN (uint16_t *n_desc, uint8_t align) {
1069 	*n_desc = ((*n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
1070 }
1071 
1072 // Enums from <mach/machine.h>
1073 enum {
1074 	// Capability bits used in the definition of cpu_type.
1075 	CPU_ARCH_MASK  = 0xff000000,   // Mask for architecture bits
1076 	CPU_ARCH_ABI64 = 0x01000000,   // 64 bit ABI
1077 	CPU_ARCH_ABI32 = 0x02000000    // Used for ARM64_32 (new Apple Watch)
1078 };
1079 
1080 // Constants for the cputype field.
1081 enum CPUType {
1082 	CPU_TYPE_ANY       = -1,
1083 	CPU_TYPE_VAX       = 1,
1084 	CPU_TYPE_MC680x0   = 6,
1085 	CPU_TYPE_X86       = 7,
1086 	CPU_TYPE_I386      = CPU_TYPE_X86,
1087 	CPU_TYPE_X86_64    = CPU_TYPE_X86 | CPU_ARCH_ABI64,
1088 	CPU_TYPE_MIPS      = 8,
1089 	CPU_TYPE_MC98000   = 10, // Old Motorola PowerPC
1090 	CPU_TYPE_HPPA      = 11,
1091 	CPU_TYPE_ARM       = 12,
1092 	CPU_TYPE_ARM64     = CPU_TYPE_ARM | CPU_ARCH_ABI64,
1093 	CPU_TYPE_ARM64_32  = CPU_TYPE_ARM | CPU_ARCH_ABI32,
1094 	CPU_TYPE_MC88000   = 13,
1095 	CPU_TYPE_SPARC     = 14,
1096 	CPU_TYPE_I860      = 15,
1097 	CPU_TYPE_POWERPC   = 18,
1098 	CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
1099 };
1100 
1101 enum {
1102 	// Capability bits used in the definition of cpusubtype.
1103 	CPU_SUBTYPE_MASK  = 0xff000000,   // Mask for architecture bits
1104 	CPU_SUBTYPE_LIB64 = 0x80000000,   // 64 bit libraries
1105 
1106 	// Special CPU subtype constants.
1107 	CPU_SUBTYPE_MULTIPLE = ~0u
1108 };
1109 
1110 // Constants for the cpusubtype field.
1111 enum CPUSubTypeX86 {
1112 	CPU_SUBTYPE_I386_ALL       = 3,
1113 	CPU_SUBTYPE_386            = 3,
1114 	CPU_SUBTYPE_486            = 4,
1115 	CPU_SUBTYPE_486SX          = 0x84,
1116 	CPU_SUBTYPE_586            = 5,
1117 	CPU_SUBTYPE_PENT           = CPU_SUBTYPE_586,
1118 	CPU_SUBTYPE_PENTPRO        = 0x16,
1119 	CPU_SUBTYPE_PENTII_M3      = 0x36,
1120 	CPU_SUBTYPE_PENTII_M5      = 0x56,
1121 	CPU_SUBTYPE_CELERON        = 0x67,
1122 	CPU_SUBTYPE_CELERON_MOBILE = 0x77,
1123 	CPU_SUBTYPE_PENTIUM_3      = 0x08,
1124 	CPU_SUBTYPE_PENTIUM_3_M    = 0x18,
1125 	CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
1126 	CPU_SUBTYPE_PENTIUM_M      = 0x09,
1127 	CPU_SUBTYPE_PENTIUM_4      = 0x0a,
1128 	CPU_SUBTYPE_PENTIUM_4_M    = 0x1a,
1129 	CPU_SUBTYPE_ITANIUM        = 0x0b,
1130 	CPU_SUBTYPE_ITANIUM_2      = 0x1b,
1131 	CPU_SUBTYPE_XEON           = 0x0c,
1132 	CPU_SUBTYPE_XEON_MP        = 0x1c,
1133 
1134 	CPU_SUBTYPE_X86_ALL     = 3,
1135 	CPU_SUBTYPE_X86_64_ALL  = 3,
1136 	CPU_SUBTYPE_X86_ARCH1   = 4,
1137 	CPU_SUBTYPE_X86_64_H    = 8
1138 };
CPU_SUBTYPE_INTEL(int Family,int Model)1139 static inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
1140 	return Family | (Model << 4);
1141 }
CPU_SUBTYPE_INTEL_FAMILY(enum CPUSubTypeX86 ST)1142 static inline int CPU_SUBTYPE_INTEL_FAMILY(enum CPUSubTypeX86 ST) {
1143 	return ((int)ST) & 0x0f;
1144 }
CPU_SUBTYPE_INTEL_MODEL(enum CPUSubTypeX86 ST)1145 static inline int CPU_SUBTYPE_INTEL_MODEL(enum CPUSubTypeX86 ST) {
1146 	return ((int)ST) >> 4;
1147 }
1148 enum {
1149 	CPU_SUBTYPE_INTEL_FAMILY_MAX = 15,
1150 	CPU_SUBTYPE_INTEL_MODEL_ALL  = 0
1151 };
1152 
1153 enum CPUSubTypeARM {
1154 	CPU_SUBTYPE_ARM_ALL     = 0,
1155 	CPU_SUBTYPE_ARM_V4T     = 5,
1156 	CPU_SUBTYPE_ARM_V6      = 6,
1157 	CPU_SUBTYPE_ARM_V5      = 7,
1158 	CPU_SUBTYPE_ARM_V5TEJ   = 7,
1159 	CPU_SUBTYPE_ARM_XSCALE  = 8,
1160 	CPU_SUBTYPE_ARM_V7      = 9,
1161 	CPU_SUBTYPE_ARM_V7F     = 10,
1162 	CPU_SUBTYPE_ARM_V7S     = 11,
1163 	CPU_SUBTYPE_ARM_V7K     = 12,
1164 	CPU_SUBTYPE_ARM_V6M     = 14,
1165 	CPU_SUBTYPE_ARM_V7M     = 15,
1166 	CPU_SUBTYPE_ARM_V7EM    = 16
1167 };
1168 
1169 enum CPUSubTypeARM64 {
1170 	CPU_SUBTYPE_ARM64_ALL   = 0,
1171 	CPU_SUBTYPE_ARM64_V8    = 1,
1172 	CPU_SUBTYPE_ARM64E      = 2
1173 };
1174 
1175 enum CPUSubTypeSPARC {
1176 	CPU_SUBTYPE_SPARC_ALL   = 0
1177 };
1178 
1179 enum CPUSubTypePowerPC {
1180 	CPU_SUBTYPE_POWERPC_ALL   = 0,
1181 	CPU_SUBTYPE_POWERPC_601   = 1,
1182 	CPU_SUBTYPE_POWERPC_602   = 2,
1183 	CPU_SUBTYPE_POWERPC_603   = 3,
1184 	CPU_SUBTYPE_POWERPC_603e  = 4,
1185 	CPU_SUBTYPE_POWERPC_603ev = 5,
1186 	CPU_SUBTYPE_POWERPC_604   = 6,
1187 	CPU_SUBTYPE_POWERPC_604e  = 7,
1188 	CPU_SUBTYPE_POWERPC_620   = 8,
1189 	CPU_SUBTYPE_POWERPC_750   = 9,
1190 	CPU_SUBTYPE_POWERPC_7400  = 10,
1191 	CPU_SUBTYPE_POWERPC_7450  = 11,
1192 	CPU_SUBTYPE_POWERPC_970   = 100,
1193 
1194 	CPU_SUBTYPE_MC98000_ALL   = CPU_SUBTYPE_POWERPC_ALL,
1195 	CPU_SUBTYPE_MC98601       = CPU_SUBTYPE_POWERPC_601
1196 };
1197 
1198 enum CPUSubTypeVAX {
1199 	CPU_SUBTYPE_VAX_ALL       = 0,
1200 	CPU_SUBTYPE_VAX780        = 1,
1201 	CPU_SUBTYPE_VAX785        = 2,
1202 	CPU_SUBTYPE_VAX750        = 3,
1203 	CPU_SUBTYPE_VAX730        = 4,
1204 	CPU_SUBTYPE_UVAXI         = 5,
1205 	CPU_SUBTYPE_UVAXII        = 6,
1206 	CPU_SUBTYPE_VAX8200       = 7,
1207 	CPU_SUBTYPE_VAX8500       = 8,
1208 	CPU_SUBTYPE_VAX8600       = 9,
1209 	CPU_SUBTYPE_VAX8650       = 10,
1210 	CPU_SUBTYPE_VAX8800       = 11,
1211 	CPU_SUBTYPE_UVAXIII       = 12
1212 };
1213 
1214 enum CPUSubTypeMIPS {
1215 	CPU_SUBTYPE_MIPS_ALL     = 0,
1216 	CPU_SUBTYPE_MIPS_R2300   = 1,
1217 	CPU_SUBTYPE_MIPS_R2600   = 2,
1218 	CPU_SUBTYPE_MIPS_R2800   = 3,
1219 	CPU_SUBTYPE_MIPS_R2000a  = 4,
1220 	CPU_SUBTYPE_MIPS_R2000   = 5,
1221 	CPU_SUBTYPE_MIPS_R3000a  = 6,
1222 	CPU_SUBTYPE_MIPS_R3000   = 7
1223 };
1224 
1225 enum CPUSubTypeMC680x0 {
1226 	CPU_SUBTYPE_MC680x0_ALL   = 1,
1227 	CPU_SUBTYPE_MC68030       = 1,
1228 	CPU_SUBTYPE_MC68040       = 2,
1229 	CPU_SUBTYPE_MC68030_ONLY  = 3
1230 };
1231 
1232 enum CPUSubTypeMC88000 {
1233 	CPU_SUBTYPE_MC88000_ALL   = 0,
1234 	CPU_SUBTYPE_MC88100       = 1,
1235 	CPU_SUBTYPE_MC88110       = 2
1236 };
1237 
1238 enum CPUSubTypeHPPA {
1239 	CPU_SUBTYPE_HPPA_ALL      = 0,
1240 	CPU_SUBTYPE_HPPA_7100     = 0,
1241 	CPU_SUBTYPE_HPPA_7100LC   = 1
1242 };
1243 
1244 enum CPUSubTypeI860 {
1245 	CPU_SUBTYPE_I860_ALL      = 0,
1246 	CPU_SUBTYPE_I860_860      = 1
1247 };
1248 
1249 
1250 struct x86_thread_state64_t {
1251 	uint64_t rax;
1252 	uint64_t rbx;
1253 	uint64_t rcx;
1254 	uint64_t rdx;
1255 	uint64_t rdi;
1256 	uint64_t rsi;
1257 	uint64_t rbp;
1258 	uint64_t rsp;
1259 	uint64_t r8;
1260 	uint64_t r9;
1261 	uint64_t r10;
1262 	uint64_t r11;
1263 	uint64_t r12;
1264 	uint64_t r13;
1265 	uint64_t r14;
1266 	uint64_t r15;
1267 	uint64_t rip;
1268 	uint64_t rflags;
1269 	uint64_t cs;
1270 	uint64_t fs;
1271 	uint64_t gs;
1272 };
1273 
1274 enum x86_fp_control_precis {
1275 	x86_FP_PREC_24B = 0,
1276 	x86_FP_PREC_53B = 2,
1277 	x86_FP_PREC_64B = 3
1278 };
1279 
1280 enum x86_fp_control_rc {
1281 	x86_FP_RND_NEAR = 0,
1282 	x86_FP_RND_DOWN = 1,
1283 	x86_FP_RND_UP = 2,
1284 	x86_FP_CHOP = 3
1285 };
1286 
1287 struct fp_control_t {
1288 	unsigned short
1289 	invalid :1,
1290 	denorm  :1,
1291 	zdiv    :1,
1292 	ovrfl   :1,
1293 	undfl   :1,
1294 	precis  :1,
1295 	:2,
1296 	pc      :2,
1297 	rc      :2,
1298 	:1,
1299 	:3;
1300 };
1301 
1302 struct fp_status_t {
1303 	unsigned short
1304 	invalid :1,
1305 	denorm  :1,
1306 	zdiv    :1,
1307 	ovrfl   :1,
1308 	undfl   :1,
1309 	precis  :1,
1310 	stkflt  :1,
1311 	errsumm :1,
1312 	c0      :1,
1313 	c1      :1,
1314 	c2      :1,
1315 	tos     :3,
1316 	c3      :1,
1317 	busy    :1;
1318 };
1319 
1320 struct mmst_reg_t {
1321 	char mmst_reg[10];
1322 	char mmst_rsrv[6];
1323 };
1324 
1325 struct xmm_reg_t {
1326 	char xmm_reg[16];
1327 };
1328 
1329 struct x86_float_state64_t {
1330 	int32_t fpu_reserved[2];
1331 	struct fp_control_t fpu_fcw;
1332 	struct fp_status_t fpu_fsw;
1333 	uint8_t fpu_ftw;
1334 	uint8_t fpu_rsrv1;
1335 	uint16_t fpu_fop;
1336 	uint32_t fpu_ip;
1337 	uint16_t fpu_cs;
1338 	uint16_t fpu_rsrv2;
1339 	uint32_t fpu_dp;
1340 	uint16_t fpu_ds;
1341 	uint16_t fpu_rsrv3;
1342 	uint32_t fpu_mxcsr;
1343 	uint32_t fpu_mxcsrmask;
1344 	struct mmst_reg_t fpu_stmm0;
1345 	struct mmst_reg_t fpu_stmm1;
1346 	struct mmst_reg_t fpu_stmm2;
1347 	struct mmst_reg_t fpu_stmm3;
1348 	struct mmst_reg_t fpu_stmm4;
1349 	struct mmst_reg_t fpu_stmm5;
1350 	struct mmst_reg_t fpu_stmm6;
1351 	struct mmst_reg_t fpu_stmm7;
1352 	struct xmm_reg_t fpu_xmm0;
1353 	struct xmm_reg_t fpu_xmm1;
1354 	struct xmm_reg_t fpu_xmm2;
1355 	struct xmm_reg_t fpu_xmm3;
1356 	struct xmm_reg_t fpu_xmm4;
1357 	struct xmm_reg_t fpu_xmm5;
1358 	struct xmm_reg_t fpu_xmm6;
1359 	struct xmm_reg_t fpu_xmm7;
1360 	struct xmm_reg_t fpu_xmm8;
1361 	struct xmm_reg_t fpu_xmm9;
1362 	struct xmm_reg_t fpu_xmm10;
1363 	struct xmm_reg_t fpu_xmm11;
1364 	struct xmm_reg_t fpu_xmm12;
1365 	struct xmm_reg_t fpu_xmm13;
1366 	struct xmm_reg_t fpu_xmm14;
1367 	struct xmm_reg_t fpu_xmm15;
1368 	char fpu_rsrv4[6*16];
1369 	uint32_t fpu_reserved1;
1370 };
1371 
1372 struct x86_exception_state64_t {
1373 	uint16_t trapno;
1374 	uint16_t cpu;
1375 	uint32_t err;
1376 	uint64_t faultvaddr;
1377 };
1378 
1379 struct x86_state_hdr_t {
1380 	uint32_t flavor;
1381 	uint32_t count;
1382 };
1383 
1384 struct x86_thread_state_t {
1385 	struct x86_state_hdr_t tsh;
1386 	union {
1387 		struct x86_thread_state64_t ts64;
1388 	} uts;
1389 };
1390 
1391 struct x86_float_state_t {
1392 	struct x86_state_hdr_t fsh;
1393 	union {
1394 		struct x86_float_state64_t fs64;
1395 	} ufs;
1396 };
1397 
1398 struct x86_exception_state_t {
1399 	struct x86_state_hdr_t esh;
1400 	union {
1401 		struct x86_exception_state64_t es64;
1402 	} ues;
1403 };
1404 
1405 enum X86ThreadFlavors {
1406 	x86_THREAD_STATE32    = 1,
1407 	x86_FLOAT_STATE32     = 2,
1408 	x86_EXCEPTION_STATE32 = 3,
1409 	x86_THREAD_STATE64    = 4,
1410 	x86_FLOAT_STATE64     = 5,
1411 	x86_EXCEPTION_STATE64 = 6,
1412 	x86_THREAD_STATE      = 7,
1413 	x86_FLOAT_STATE       = 8,
1414 	x86_EXCEPTION_STATE   = 9,
1415 	x86_DEBUG_STATE32     = 10,
1416 	x86_DEBUG_STATE64     = 11,
1417 	x86_DEBUG_STATE       = 12
1418 };
1419 
1420 #define x86_THREAD_STATE64_COUNT \
1421 sizeof(struct x86_thread_state64_t) / sizeof(uint32_t);
1422 #define x86_FLOAT_STATE64_COUNT \
1423 sizeof(struct x86_float_state64_t) / sizeof(uint32_t);
1424 #define x86_EXCEPTION_STATE64_COUNT \
1425 sizeof(struct x86_exception_state64_t) / sizeof(uint32_t);
1426 
1427 #define x86_THREAD_STATE_COUNT \
1428 sizeof(struct x86_thread_state_t) / sizeof(uint32_t);
1429 #define x86_FLOAT_STATE_COUNT \
1430 sizeof(struct x86_float_state_t) / sizeof(uint32_t);
1431 #define x86_EXCEPTION_STATE_COUNT \
1432 sizeof(struct x86_exception_state_t) / sizeof(uint32_t);
1433 
1434 #define EXPORT_SYMBOL_FLAGS_KIND_MASK 0x03
1435 #define EXPORT_SYMBOL_FLAGS_KIND_REGULAR 0x00
1436 #define EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL 0x01
1437 #define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE 0x02
1438 #define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION 0x04
1439 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
1440 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
1441 
1442 struct dyld_chained_fixups_header
1443 {
1444 	uint32_t fixups_version;
1445 	uint32_t starts_offset;
1446 	uint32_t imports_offset;
1447 	uint32_t symbols_offset;
1448 	uint32_t imports_count;
1449 	uint32_t imports_format;
1450 	uint32_t symbols_format;
1451 };
1452 
1453 struct dyld_chained_starts_in_image {
1454 	uint32_t seg_count;
1455 };
1456 
1457 struct dyld_chained_starts_in_segment {
1458 	uint32_t size;
1459 	uint16_t page_size;
1460 	uint16_t pointer_format;
1461 	uint64_t segment_offset;
1462 	uint32_t max_valid_pointer;
1463 	uint16_t page_count;
1464 };
1465 
1466 struct r_dyld_chained_starts_in_segment {
1467 	uint32_t size;
1468 	uint16_t page_size;
1469 	uint16_t pointer_format;
1470 	uint64_t segment_offset;
1471 	uint32_t max_valid_pointer;
1472 	uint16_t page_count;
1473 	ut16 * page_start;
1474 };
1475 
1476 enum {
1477 	DYLD_CHAINED_PTR_START_NONE   = 0xFFFF,
1478 	DYLD_CHAINED_PTR_START_MULTI  = 0x8000,
1479 	DYLD_CHAINED_PTR_START_LAST   = 0x8000,
1480 };
1481 
1482 enum {
1483 	DYLD_CHAINED_PTR_ARM64E      = 1,
1484 	DYLD_CHAINED_PTR_64          = 2,
1485 	DYLD_CHAINED_PTR_32          = 3,
1486 	DYLD_CHAINED_PTR_32_CACHE    = 4,
1487 	DYLD_CHAINED_PTR_32_FIRMWARE = 5,
1488 	DYLD_CHAINED_PTR_ARM64E_KERNEL = 7,
1489 	DYLD_CHAINED_PTR_64_KERNEL_CACHE = 8,
1490 };
1491 
1492 struct dyld_chained_ptr_arm64e_rebase {
1493 	uint64_t target : 43,
1494 		high8 : 8,
1495 		next : 11,
1496 		bind : 1, // == 0
1497 		auth : 1; // == 0
1498 };
1499 
1500 struct dyld_chained_ptr_arm64e_bind {
1501 	uint64_t ordinal : 16,
1502 		zero : 16,
1503 		addend : 19,
1504 		next : 11,
1505 		bind : 1, // == 1
1506 		auth : 1; // == 0
1507 };
1508 
1509 struct dyld_chained_ptr_arm64e_auth_rebase {
1510 	uint64_t target : 32,
1511 		diversity : 16,
1512 		addrDiv : 1,
1513 		key : 2,
1514 		next : 11,
1515 		bind : 1, // == 0
1516 		auth : 1; // == 1
1517 };
1518 
1519 struct dyld_chained_ptr_arm64e_auth_bind {
1520 	uint64_t ordinal : 16,
1521 		zero : 16,
1522 		diversity : 16,
1523 		addrDiv : 1,
1524 		key : 2,
1525 		next : 11,
1526 		bind : 1, // == 1
1527 		auth : 1; // == 1
1528 };
1529 
1530 /* WARNING: this is guesswork based on trial and error */
1531 struct dyld_chained_ptr_arm64e_cache_rebase {
1532 	uint64_t target : 43,
1533 		high8 : 8,
1534 		next : 12,
1535 		auth : 1; // == 0
1536 };
1537 
1538 struct dyld_chained_ptr_arm64e_cache_auth_rebase {
1539 	uint64_t target : 32,
1540 		diversity : 16,
1541 		addrDiv : 1,
1542 		key : 2,
1543 		next : 12,
1544 		auth : 1; // == 1
1545 };
1546 
1547 #endif
1548