1 //===-- llvm/Support/MachO.h - The MachO file format ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines manifest constants for the MachO object file format.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MACHO_H
15 #define LLVM_SUPPORT_MACHO_H
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/Host.h"
23 // Enums from <mach-o/loader.h>
25 // Constants for the "magic" field in llvm::MachO::mach_header and
26 // llvm::MachO::mach_header_64
27 MH_MAGIC = 0xFEEDFACEu,
28 MH_CIGAM = 0xCEFAEDFEu,
29 MH_MAGIC_64 = 0xFEEDFACFu,
30 MH_CIGAM_64 = 0xCFFAEDFEu,
31 FAT_MAGIC = 0xCAFEBABEu,
32 FAT_CIGAM = 0xBEBAFECAu
36 // Constants for the "filetype" field in llvm::MachO::mach_header and
37 // llvm::MachO::mach_header_64
52 // Constant bits for the "flags" field in llvm::MachO::mach_header and
53 // llvm::MachO::mach_header_64
54 MH_NOUNDEFS = 0x00000001u,
55 MH_INCRLINK = 0x00000002u,
56 MH_DYLDLINK = 0x00000004u,
57 MH_BINDATLOAD = 0x00000008u,
58 MH_PREBOUND = 0x00000010u,
59 MH_SPLIT_SEGS = 0x00000020u,
60 MH_LAZY_INIT = 0x00000040u,
61 MH_TWOLEVEL = 0x00000080u,
62 MH_FORCE_FLAT = 0x00000100u,
63 MH_NOMULTIDEFS = 0x00000200u,
64 MH_NOFIXPREBINDING = 0x00000400u,
65 MH_PREBINDABLE = 0x00000800u,
66 MH_ALLMODSBOUND = 0x00001000u,
67 MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
68 MH_CANONICAL = 0x00004000u,
69 MH_WEAK_DEFINES = 0x00008000u,
70 MH_BINDS_TO_WEAK = 0x00010000u,
71 MH_ALLOW_STACK_EXECUTION = 0x00020000u,
72 MH_ROOT_SAFE = 0x00040000u,
73 MH_SETUID_SAFE = 0x00080000u,
74 MH_NO_REEXPORTED_DYLIBS = 0x00100000u,
76 MH_DEAD_STRIPPABLE_DYLIB = 0x00400000u
80 // Flags for the "cmd" field in llvm::MachO::load_command
81 LC_REQ_DYLD = 0x80000000u
84 enum LoadCommandType : uint32_t {
85 // Constants for the "cmd" field in llvm::MachO::load_command
86 LC_SEGMENT = 0x00000001u,
87 LC_SYMTAB = 0x00000002u,
88 LC_SYMSEG = 0x00000003u,
89 LC_THREAD = 0x00000004u,
90 LC_UNIXTHREAD = 0x00000005u,
91 LC_LOADFVMLIB = 0x00000006u,
92 LC_IDFVMLIB = 0x00000007u,
93 LC_IDENT = 0x00000008u,
94 LC_FVMFILE = 0x00000009u,
95 LC_PREPAGE = 0x0000000Au,
96 LC_DYSYMTAB = 0x0000000Bu,
97 LC_LOAD_DYLIB = 0x0000000Cu,
98 LC_ID_DYLIB = 0x0000000Du,
99 LC_LOAD_DYLINKER = 0x0000000Eu,
100 LC_ID_DYLINKER = 0x0000000Fu,
101 LC_PREBOUND_DYLIB = 0x00000010u,
102 LC_ROUTINES = 0x00000011u,
103 LC_SUB_FRAMEWORK = 0x00000012u,
104 LC_SUB_UMBRELLA = 0x00000013u,
105 LC_SUB_CLIENT = 0x00000014u,
106 LC_SUB_LIBRARY = 0x00000015u,
107 LC_TWOLEVEL_HINTS = 0x00000016u,
108 LC_PREBIND_CKSUM = 0x00000017u,
109 LC_LOAD_WEAK_DYLIB = 0x80000018u,
110 LC_SEGMENT_64 = 0x00000019u,
111 LC_ROUTINES_64 = 0x0000001Au,
112 LC_UUID = 0x0000001Bu,
113 LC_RPATH = 0x8000001Cu,
114 LC_CODE_SIGNATURE = 0x0000001Du,
115 LC_SEGMENT_SPLIT_INFO = 0x0000001Eu,
116 LC_REEXPORT_DYLIB = 0x8000001Fu,
117 LC_LAZY_LOAD_DYLIB = 0x00000020u,
118 LC_ENCRYPTION_INFO = 0x00000021u,
119 LC_DYLD_INFO = 0x00000022u,
120 LC_DYLD_INFO_ONLY = 0x80000022u,
121 LC_LOAD_UPWARD_DYLIB = 0x80000023u,
122 LC_VERSION_MIN_MACOSX = 0x00000024u,
123 LC_VERSION_MIN_IPHONEOS = 0x00000025u,
124 LC_FUNCTION_STARTS = 0x00000026u,
125 LC_DYLD_ENVIRONMENT = 0x00000027u,
126 LC_MAIN = 0x80000028u,
127 LC_DATA_IN_CODE = 0x00000029u,
128 LC_SOURCE_VERSION = 0x0000002Au,
129 LC_DYLIB_CODE_SIGN_DRS = 0x0000002Bu,
131 LC_LINKER_OPTIONS = 0x0000002Du,
132 LC_LINKER_OPTIMIZATION_HINT = 0x0000002Eu
136 // Constant bits for the "flags" field in llvm::MachO::segment_command
140 SG_PROTECTED_VERSION_1 = 0x8u,
143 // Constant masks for the "flags" field in llvm::MachO::section and
144 // llvm::MachO::section_64
145 SECTION_TYPE = 0x000000ffu, // SECTION_TYPE
146 SECTION_ATTRIBUTES = 0xffffff00u, // SECTION_ATTRIBUTES
147 SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
148 SECTION_ATTRIBUTES_SYS = 0x00ffff00u // SECTION_ATTRIBUTES_SYS
151 /// These are the section type and attributes fields. A MachO section can
152 /// have only one Type, but can have any of the attributes specified.
153 enum SectionType : uint32_t {
154 // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
155 // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
157 /// S_REGULAR - Regular section.
159 /// S_ZEROFILL - Zero fill on demand section.
161 /// S_CSTRING_LITERALS - Section with literal C strings.
162 S_CSTRING_LITERALS = 0x02u,
163 /// S_4BYTE_LITERALS - Section with 4 byte literals.
164 S_4BYTE_LITERALS = 0x03u,
165 /// S_8BYTE_LITERALS - Section with 8 byte literals.
166 S_8BYTE_LITERALS = 0x04u,
167 /// S_LITERAL_POINTERS - Section with pointers to literals.
168 S_LITERAL_POINTERS = 0x05u,
169 /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
170 S_NON_LAZY_SYMBOL_POINTERS = 0x06u,
171 /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
172 S_LAZY_SYMBOL_POINTERS = 0x07u,
173 /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
174 /// the Reserved2 field.
175 S_SYMBOL_STUBS = 0x08u,
176 /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
178 S_MOD_INIT_FUNC_POINTERS = 0x09u,
179 /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
181 S_MOD_TERM_FUNC_POINTERS = 0x0au,
182 /// S_COALESCED - Section contains symbols that are to be coalesced.
184 /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
186 S_GB_ZEROFILL = 0x0cu,
187 /// S_INTERPOSING - Section with only pairs of function pointers for
189 S_INTERPOSING = 0x0du,
190 /// S_16BYTE_LITERALS - Section with only 16 byte literals.
191 S_16BYTE_LITERALS = 0x0eu,
192 /// S_DTRACE_DOF - Section contains DTrace Object Format.
193 S_DTRACE_DOF = 0x0fu,
194 /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
195 /// lazy loaded dylibs.
196 S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
197 /// S_THREAD_LOCAL_REGULAR - Thread local data section.
198 S_THREAD_LOCAL_REGULAR = 0x11u,
199 /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
200 S_THREAD_LOCAL_ZEROFILL = 0x12u,
201 /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
203 S_THREAD_LOCAL_VARIABLES = 0x13u,
204 /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
205 /// local structures.
206 S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
207 /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
208 /// variable initialization pointers to functions.
209 S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
211 LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
215 // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
216 // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
218 /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
220 S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
221 /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
222 /// in a ranlib table of contents.
223 S_ATTR_NO_TOC = 0x40000000u,
224 /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
225 /// in files with the MY_DYLDLINK flag.
226 S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
227 /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
228 S_ATTR_NO_DEAD_STRIP = 0x10000000u,
229 /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
230 S_ATTR_LIVE_SUPPORT = 0x08000000u,
231 /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
233 S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
234 /// S_ATTR_DEBUG - A debug section.
235 S_ATTR_DEBUG = 0x02000000u,
237 // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
238 // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
240 /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
241 S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
242 /// S_ATTR_EXT_RELOC - Section has external relocation entries.
243 S_ATTR_EXT_RELOC = 0x00000200u,
244 /// S_ATTR_LOC_RELOC - Section has local relocation entries.
245 S_ATTR_LOC_RELOC = 0x00000100u,
247 // Constant masks for the value of an indirect symbol in an indirect
249 INDIRECT_SYMBOL_LOCAL = 0x80000000u,
250 INDIRECT_SYMBOL_ABS = 0x40000000u
253 enum DataRegionType {
254 // Constants for the "kind" field in a data_in_code_entry structure
256 DICE_KIND_JUMP_TABLE8 = 2u,
257 DICE_KIND_JUMP_TABLE16 = 3u,
258 DICE_KIND_JUMP_TABLE32 = 4u,
259 DICE_KIND_ABS_JUMP_TABLE32 = 5u
263 REBASE_TYPE_POINTER = 1u,
264 REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
265 REBASE_TYPE_TEXT_PCREL32 = 3u
269 REBASE_OPCODE_MASK = 0xF0u,
270 REBASE_IMMEDIATE_MASK = 0x0Fu
274 REBASE_OPCODE_DONE = 0x00u,
275 REBASE_OPCODE_SET_TYPE_IMM = 0x10u,
276 REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
277 REBASE_OPCODE_ADD_ADDR_ULEB = 0x30u,
278 REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40u,
279 REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50u,
280 REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60u,
281 REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70u,
282 REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
286 BIND_TYPE_POINTER = 1u,
287 BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
288 BIND_TYPE_TEXT_PCREL32 = 3u
291 enum BindSpecialDylib {
292 BIND_SPECIAL_DYLIB_SELF = 0,
293 BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
294 BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2
298 BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1u,
299 BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
301 BIND_OPCODE_MASK = 0xF0u,
302 BIND_IMMEDIATE_MASK = 0x0Fu
306 BIND_OPCODE_DONE = 0x00u,
307 BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10u,
308 BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20u,
309 BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30u,
310 BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
311 BIND_OPCODE_SET_TYPE_IMM = 0x50u,
312 BIND_OPCODE_SET_ADDEND_SLEB = 0x60u,
313 BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
314 BIND_OPCODE_ADD_ADDR_ULEB = 0x80u,
315 BIND_OPCODE_DO_BIND = 0x90u,
316 BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0u,
317 BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
318 BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
322 EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03u,
323 EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04u,
324 EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08u,
325 EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10u
328 enum ExportSymbolKind {
329 EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00u,
330 EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01u
335 // Constant masks for the "n_type" field in llvm::MachO::nlist and
336 // llvm::MachO::nlist_64
344 // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
345 // llvm::MachO::nlist_64
353 enum SectionOrdinal {
354 // Constants for the "n_sect" field in llvm::MachO::nlist and
355 // llvm::MachO::nlist_64
361 // Constant masks for the "n_desc" field in llvm::MachO::nlist and
362 // llvm::MachO::nlist_64
363 // The low 3 bits are the for the REFERENCE_TYPE.
364 REFERENCE_TYPE = 0x7,
365 REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
366 REFERENCE_FLAG_UNDEFINED_LAZY = 1,
367 REFERENCE_FLAG_DEFINED = 2,
368 REFERENCE_FLAG_PRIVATE_DEFINED = 3,
369 REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
370 REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
371 // Flag bits (some overlap with the library ordinal bits).
372 N_ARM_THUMB_DEF = 0x0008u,
373 REFERENCED_DYNAMICALLY = 0x0010u,
374 N_NO_DEAD_STRIP = 0x0020u,
375 N_WEAK_REF = 0x0040u,
376 N_WEAK_DEF = 0x0080u,
377 N_SYMBOL_RESOLVER = 0x0100u,
378 N_ALT_ENTRY = 0x0200u,
379 // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
380 // as these are in the top 8 bits.
381 SELF_LIBRARY_ORDINAL = 0x0,
382 MAX_LIBRARY_ORDINAL = 0xfd,
383 DYNAMIC_LOOKUP_ORDINAL = 0xfe,
384 EXECUTABLE_ORDINAL = 0xff
388 // Constant values for the "n_type" field in llvm::MachO::nlist and
389 // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
424 // Constant values for the r_symbolnum field in an
425 // llvm::MachO::relocation_info structure when r_extern is 0.
428 // Constant bits for the r_address field in an
429 // llvm::MachO::relocation_info structure.
430 R_SCATTERED = 0x80000000
433 enum RelocationInfoType {
434 // Constant values for the r_type field in an
435 // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
437 GENERIC_RELOC_VANILLA = 0,
438 GENERIC_RELOC_PAIR = 1,
439 GENERIC_RELOC_SECTDIFF = 2,
440 GENERIC_RELOC_PB_LA_PTR = 3,
441 GENERIC_RELOC_LOCAL_SECTDIFF = 4,
442 GENERIC_RELOC_TLV = 5,
444 // Constant values for the r_type field in a PowerPC architecture
445 // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
447 PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
448 PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
455 PPC_RELOC_SECTDIFF = 8,
456 PPC_RELOC_PB_LA_PTR = 9,
457 PPC_RELOC_HI16_SECTDIFF = 10,
458 PPC_RELOC_LO16_SECTDIFF = 11,
459 PPC_RELOC_HA16_SECTDIFF = 12,
461 PPC_RELOC_LO14_SECTDIFF = 14,
462 PPC_RELOC_LOCAL_SECTDIFF = 15,
464 // Constant values for the r_type field in an ARM architecture
465 // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
467 ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
468 ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
469 ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
470 ARM_RELOC_LOCAL_SECTDIFF = 3,
471 ARM_RELOC_PB_LA_PTR = 4,
473 ARM_THUMB_RELOC_BR22 = 6,
474 ARM_THUMB_32BIT_BRANCH = 7, // obsolete
476 ARM_RELOC_HALF_SECTDIFF = 9,
478 // Constant values for the r_type field in an ARM64 architecture
479 // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
483 ARM64_RELOC_UNSIGNED = 0,
484 // Must be followed by an ARM64_RELOC_UNSIGNED
485 ARM64_RELOC_SUBTRACTOR = 1,
486 // A B/BL instruction with 26-bit displacement.
487 ARM64_RELOC_BRANCH26 = 2,
488 // PC-rel distance to page of target.
489 ARM64_RELOC_PAGE21 = 3,
490 // Offset within page, scaled by r_length.
491 ARM64_RELOC_PAGEOFF12 = 4,
492 // PC-rel distance to page of GOT slot.
493 ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
494 // Offset within page of GOT slot, scaled by r_length.
495 ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
496 // For pointers to GOT slots.
497 ARM64_RELOC_POINTER_TO_GOT = 7,
498 // PC-rel distance to page of TLVP slot.
499 ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
500 // Offset within page of TLVP slot, scaled by r_length.
501 ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
502 // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
503 ARM64_RELOC_ADDEND = 10,
506 // Constant values for the r_type field in an x86_64 architecture
507 // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
509 X86_64_RELOC_UNSIGNED = 0,
510 X86_64_RELOC_SIGNED = 1,
511 X86_64_RELOC_BRANCH = 2,
512 X86_64_RELOC_GOT_LOAD = 3,
513 X86_64_RELOC_GOT = 4,
514 X86_64_RELOC_SUBTRACTOR = 5,
515 X86_64_RELOC_SIGNED_1 = 6,
516 X86_64_RELOC_SIGNED_2 = 7,
517 X86_64_RELOC_SIGNED_4 = 8,
521 // Values for segment_command.initprot.
522 // From <mach/vm_prot.h>
526 VM_PROT_EXECUTE = 0x4
530 // Structs from <mach-o/loader.h>
542 struct mach_header_64 {
553 struct load_command {
558 struct segment_command {
572 struct segment_command_64 {
617 uint32_t minor_version;
618 uint32_t header_addr;
621 struct fvmlib_command {
624 struct fvmlib fvmlib;
630 uint32_t current_version;
631 uint32_t compatibility_version;
634 struct dylib_command {
640 struct sub_framework_command {
646 struct sub_client_command {
652 struct sub_umbrella_command {
655 uint32_t sub_umbrella;
658 struct sub_library_command {
661 uint32_t sub_library;
664 struct prebound_dylib_command {
669 uint32_t linked_modules;
672 struct dylinker_command {
678 struct thread_command {
683 struct routines_command {
686 uint32_t init_address;
687 uint32_t init_module;
696 struct routines_command_64 {
699 uint64_t init_address;
700 uint64_t init_module;
709 struct symtab_command {
718 struct dysymtab_command {
731 uint32_t extrefsymoff;
732 uint32_t nextrefsyms;
733 uint32_t indirectsymoff;
734 uint32_t nindirectsyms;
741 struct dylib_table_of_contents {
742 uint32_t symbol_index;
743 uint32_t module_index;
746 struct dylib_module {
747 uint32_t module_name;
756 uint32_t iinit_iterm;
757 uint32_t ninit_nterm;
758 uint32_t objc_module_info_addr;
759 uint32_t objc_module_info_size;
762 struct dylib_module_64 {
763 uint32_t module_name;
772 uint32_t iinit_iterm;
773 uint32_t ninit_nterm;
774 uint32_t objc_module_info_size;
775 uint64_t objc_module_info_addr;
778 struct dylib_reference {
784 struct twolevel_hints_command {
791 struct twolevel_hint {
792 uint32_t isub_image:8,
796 struct prebind_cksum_command {
802 struct uuid_command {
808 struct rpath_command {
814 struct linkedit_data_command {
821 struct data_in_code_entry {
827 struct source_version_command {
833 struct encryption_info_command {
841 struct version_min_command {
842 uint32_t cmd; // LC_VERSION_MIN_MACOSX or
843 // LC_VERSION_MIN_IPHONEOS
844 uint32_t cmdsize; // sizeof(struct version_min_command)
845 uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
849 struct dyld_info_command {
853 uint32_t rebase_size;
856 uint32_t weak_bind_off;
857 uint32_t weak_bind_size;
858 uint32_t lazy_bind_off;
859 uint32_t lazy_bind_size;
861 uint32_t export_size;
864 struct linker_options_command {
870 struct symseg_command {
877 struct ident_command {
882 struct fvmfile_command {
886 uint32_t header_addr;
889 struct tlv_descriptor_32 {
895 struct tlv_descriptor_64 {
901 struct tlv_descriptor {
907 struct entry_point_command {
915 // Structs from <mach-o/fat.h>
929 // Structs from <mach-o/reloc.h>
930 struct relocation_info {
932 uint32_t r_symbolnum:24,
939 struct scattered_relocation_info {
940 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
941 uint32_t r_scattered:1,
947 uint32_t r_address:24,
956 // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
957 struct any_relocation_info {
958 uint32_t r_word0, r_word1;
961 // Structs from <mach-o/nlist.h>
986 // Byte order swapping functions for MachO structs
988 inline void swapStruct(mach_header &mh) {
989 sys::swapByteOrder(mh.magic);
990 sys::swapByteOrder(mh.cputype);
991 sys::swapByteOrder(mh.cpusubtype);
992 sys::swapByteOrder(mh.filetype);
993 sys::swapByteOrder(mh.ncmds);
994 sys::swapByteOrder(mh.sizeofcmds);
995 sys::swapByteOrder(mh.flags);
998 inline void swapStruct(mach_header_64 &H) {
999 sys::swapByteOrder(H.magic);
1000 sys::swapByteOrder(H.cputype);
1001 sys::swapByteOrder(H.cpusubtype);
1002 sys::swapByteOrder(H.filetype);
1003 sys::swapByteOrder(H.ncmds);
1004 sys::swapByteOrder(H.sizeofcmds);
1005 sys::swapByteOrder(H.flags);
1006 sys::swapByteOrder(H.reserved);
1009 inline void swapStruct(load_command &lc) {
1010 sys::swapByteOrder(lc.cmd);
1011 sys::swapByteOrder(lc.cmdsize);
1014 inline void swapStruct(symtab_command &lc) {
1015 sys::swapByteOrder(lc.cmd);
1016 sys::swapByteOrder(lc.cmdsize);
1017 sys::swapByteOrder(lc.symoff);
1018 sys::swapByteOrder(lc.nsyms);
1019 sys::swapByteOrder(lc.stroff);
1020 sys::swapByteOrder(lc.strsize);
1023 inline void swapStruct(segment_command_64 &seg) {
1024 sys::swapByteOrder(seg.cmd);
1025 sys::swapByteOrder(seg.cmdsize);
1026 sys::swapByteOrder(seg.vmaddr);
1027 sys::swapByteOrder(seg.vmsize);
1028 sys::swapByteOrder(seg.fileoff);
1029 sys::swapByteOrder(seg.filesize);
1030 sys::swapByteOrder(seg.maxprot);
1031 sys::swapByteOrder(seg.initprot);
1032 sys::swapByteOrder(seg.nsects);
1033 sys::swapByteOrder(seg.flags);
1036 inline void swapStruct(segment_command &seg) {
1037 sys::swapByteOrder(seg.cmd);
1038 sys::swapByteOrder(seg.cmdsize);
1039 sys::swapByteOrder(seg.vmaddr);
1040 sys::swapByteOrder(seg.vmsize);
1041 sys::swapByteOrder(seg.fileoff);
1042 sys::swapByteOrder(seg.filesize);
1043 sys::swapByteOrder(seg.maxprot);
1044 sys::swapByteOrder(seg.initprot);
1045 sys::swapByteOrder(seg.nsects);
1046 sys::swapByteOrder(seg.flags);
1049 inline void swapStruct(section_64 §) {
1050 sys::swapByteOrder(sect.addr);
1051 sys::swapByteOrder(sect.size);
1052 sys::swapByteOrder(sect.offset);
1053 sys::swapByteOrder(sect.align);
1054 sys::swapByteOrder(sect.reloff);
1055 sys::swapByteOrder(sect.nreloc);
1056 sys::swapByteOrder(sect.flags);
1057 sys::swapByteOrder(sect.reserved1);
1058 sys::swapByteOrder(sect.reserved2);
1061 inline void swapStruct(section §) {
1062 sys::swapByteOrder(sect.addr);
1063 sys::swapByteOrder(sect.size);
1064 sys::swapByteOrder(sect.offset);
1065 sys::swapByteOrder(sect.align);
1066 sys::swapByteOrder(sect.reloff);
1067 sys::swapByteOrder(sect.nreloc);
1068 sys::swapByteOrder(sect.flags);
1069 sys::swapByteOrder(sect.reserved1);
1070 sys::swapByteOrder(sect.reserved2);
1073 inline void swapStruct(dyld_info_command &info) {
1074 sys::swapByteOrder(info.cmd);
1075 sys::swapByteOrder(info.cmdsize);
1076 sys::swapByteOrder(info.rebase_off);
1077 sys::swapByteOrder(info.rebase_size);
1078 sys::swapByteOrder(info.bind_off);
1079 sys::swapByteOrder(info.bind_size);
1080 sys::swapByteOrder(info.weak_bind_off);
1081 sys::swapByteOrder(info.weak_bind_size);
1082 sys::swapByteOrder(info.lazy_bind_off);
1083 sys::swapByteOrder(info.lazy_bind_size);
1084 sys::swapByteOrder(info.export_off);
1085 sys::swapByteOrder(info.export_size);
1088 inline void swapStruct(dylib_command &d) {
1089 sys::swapByteOrder(d.cmd);
1090 sys::swapByteOrder(d.cmdsize);
1091 sys::swapByteOrder(d.dylib.name);
1092 sys::swapByteOrder(d.dylib.timestamp);
1093 sys::swapByteOrder(d.dylib.current_version);
1094 sys::swapByteOrder(d.dylib.compatibility_version);
1097 inline void swapStruct(dylinker_command &d) {
1098 sys::swapByteOrder(d.cmd);
1099 sys::swapByteOrder(d.cmdsize);
1100 sys::swapByteOrder(d.name);
1103 inline void swapStruct(entry_point_command &e) {
1104 sys::swapByteOrder(e.cmd);
1105 sys::swapByteOrder(e.cmdsize);
1106 sys::swapByteOrder(e.entryoff);
1107 sys::swapByteOrder(e.stacksize);
1110 inline void swapStruct(dysymtab_command &dst) {
1111 sys::swapByteOrder(dst.cmd);
1112 sys::swapByteOrder(dst.cmdsize);
1113 sys::swapByteOrder(dst.ilocalsym);
1114 sys::swapByteOrder(dst.nlocalsym);
1115 sys::swapByteOrder(dst.iextdefsym);
1116 sys::swapByteOrder(dst.nextdefsym);
1117 sys::swapByteOrder(dst.iundefsym);
1118 sys::swapByteOrder(dst.nundefsym);
1119 sys::swapByteOrder(dst.tocoff);
1120 sys::swapByteOrder(dst.ntoc);
1121 sys::swapByteOrder(dst.modtaboff);
1122 sys::swapByteOrder(dst.nmodtab);
1123 sys::swapByteOrder(dst.extrefsymoff);
1124 sys::swapByteOrder(dst.nextrefsyms);
1125 sys::swapByteOrder(dst.indirectsymoff);
1126 sys::swapByteOrder(dst.nindirectsyms);
1127 sys::swapByteOrder(dst.extreloff);
1128 sys::swapByteOrder(dst.nextrel);
1129 sys::swapByteOrder(dst.locreloff);
1130 sys::swapByteOrder(dst.nlocrel);
1133 inline void swapStruct(any_relocation_info &reloc) {
1134 sys::swapByteOrder(reloc.r_word0);
1135 sys::swapByteOrder(reloc.r_word1);
1138 inline void swapStruct(nlist_base &S) {
1139 sys::swapByteOrder(S.n_strx);
1140 sys::swapByteOrder(S.n_desc);
1143 inline void swapStruct(nlist &sym) {
1144 sys::swapByteOrder(sym.n_strx);
1145 sys::swapByteOrder(sym.n_desc);
1146 sys::swapByteOrder(sym.n_value);
1149 inline void swapStruct(nlist_64 &sym) {
1150 sys::swapByteOrder(sym.n_strx);
1151 sys::swapByteOrder(sym.n_desc);
1152 sys::swapByteOrder(sym.n_value);
1155 inline void swapStruct(linkedit_data_command &C) {
1156 sys::swapByteOrder(C.cmd);
1157 sys::swapByteOrder(C.cmdsize);
1158 sys::swapByteOrder(C.dataoff);
1159 sys::swapByteOrder(C.datasize);
1162 inline void swapStruct(linker_options_command &C) {
1163 sys::swapByteOrder(C.cmd);
1164 sys::swapByteOrder(C.cmdsize);
1165 sys::swapByteOrder(C.count);
1168 inline void swapStruct(version_min_command&C) {
1169 sys::swapByteOrder(C.cmd);
1170 sys::swapByteOrder(C.cmdsize);
1171 sys::swapByteOrder(C.version);
1172 sys::swapByteOrder(C.reserved);
1175 inline void swapStruct(data_in_code_entry &C) {
1176 sys::swapByteOrder(C.offset);
1177 sys::swapByteOrder(C.length);
1178 sys::swapByteOrder(C.kind);
1181 inline void swapStruct(uint32_t &C) {
1182 sys::swapByteOrder(C);
1185 // Get/Set functions from <mach-o/nlist.h>
1187 static inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
1188 return (((n_desc) >> 8u) & 0xffu);
1191 static inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
1192 n_desc = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8));
1195 static inline uint8_t GET_COMM_ALIGN (uint16_t n_desc) {
1196 return (n_desc >> 8u) & 0x0fu;
1199 static inline void SET_COMM_ALIGN (uint16_t &n_desc, uint8_t align) {
1200 n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
1203 // Enums from <mach/machine.h>
1205 // Capability bits used in the definition of cpu_type.
1206 CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
1207 CPU_ARCH_ABI64 = 0x01000000 // 64 bit ABI
1210 // Constants for the cputype field.
1214 CPU_TYPE_I386 = CPU_TYPE_X86,
1215 CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
1216 /* CPU_TYPE_MIPS = 8, */
1217 CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
1219 CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
1220 CPU_TYPE_SPARC = 14,
1221 CPU_TYPE_POWERPC = 18,
1222 CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
1226 // Capability bits used in the definition of cpusubtype.
1227 CPU_SUBTYPE_MASK = 0xff000000, // Mask for architecture bits
1228 CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
1230 // Special CPU subtype constants.
1231 CPU_SUBTYPE_MULTIPLE = ~0u
1234 // Constants for the cpusubtype field.
1235 enum CPUSubTypeX86 {
1236 CPU_SUBTYPE_I386_ALL = 3,
1237 CPU_SUBTYPE_386 = 3,
1238 CPU_SUBTYPE_486 = 4,
1239 CPU_SUBTYPE_486SX = 0x84,
1240 CPU_SUBTYPE_586 = 5,
1241 CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
1242 CPU_SUBTYPE_PENTPRO = 0x16,
1243 CPU_SUBTYPE_PENTII_M3 = 0x36,
1244 CPU_SUBTYPE_PENTII_M5 = 0x56,
1245 CPU_SUBTYPE_CELERON = 0x67,
1246 CPU_SUBTYPE_CELERON_MOBILE = 0x77,
1247 CPU_SUBTYPE_PENTIUM_3 = 0x08,
1248 CPU_SUBTYPE_PENTIUM_3_M = 0x18,
1249 CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
1250 CPU_SUBTYPE_PENTIUM_M = 0x09,
1251 CPU_SUBTYPE_PENTIUM_4 = 0x0a,
1252 CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
1253 CPU_SUBTYPE_ITANIUM = 0x0b,
1254 CPU_SUBTYPE_ITANIUM_2 = 0x1b,
1255 CPU_SUBTYPE_XEON = 0x0c,
1256 CPU_SUBTYPE_XEON_MP = 0x1c,
1258 CPU_SUBTYPE_X86_ALL = 3,
1259 CPU_SUBTYPE_X86_64_ALL = 3,
1260 CPU_SUBTYPE_X86_ARCH1 = 4,
1261 CPU_SUBTYPE_X86_64_H = 8
1263 static inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
1264 return Family | (Model << 4);
1266 static inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
1267 return ((int)ST) & 0x0f;
1269 static inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) {
1270 return ((int)ST) >> 4;
1273 CPU_SUBTYPE_INTEL_FAMILY_MAX = 15,
1274 CPU_SUBTYPE_INTEL_MODEL_ALL = 0
1277 enum CPUSubTypeARM {
1278 CPU_SUBTYPE_ARM_ALL = 0,
1279 CPU_SUBTYPE_ARM_V4T = 5,
1280 CPU_SUBTYPE_ARM_V6 = 6,
1281 CPU_SUBTYPE_ARM_V5 = 7,
1282 CPU_SUBTYPE_ARM_V5TEJ = 7,
1283 CPU_SUBTYPE_ARM_XSCALE = 8,
1284 CPU_SUBTYPE_ARM_V7 = 9,
1285 // unused ARM_V7F = 10,
1286 CPU_SUBTYPE_ARM_V7S = 11,
1287 CPU_SUBTYPE_ARM_V7K = 12,
1288 CPU_SUBTYPE_ARM_V6M = 14,
1289 CPU_SUBTYPE_ARM_V7M = 15,
1290 CPU_SUBTYPE_ARM_V7EM = 16
1293 enum CPUSubTypeARM64 {
1294 CPU_SUBTYPE_ARM64_ALL = 0
1297 enum CPUSubTypeSPARC {
1298 CPU_SUBTYPE_SPARC_ALL = 0
1301 enum CPUSubTypePowerPC {
1302 CPU_SUBTYPE_POWERPC_ALL = 0,
1303 CPU_SUBTYPE_POWERPC_601 = 1,
1304 CPU_SUBTYPE_POWERPC_602 = 2,
1305 CPU_SUBTYPE_POWERPC_603 = 3,
1306 CPU_SUBTYPE_POWERPC_603e = 4,
1307 CPU_SUBTYPE_POWERPC_603ev = 5,
1308 CPU_SUBTYPE_POWERPC_604 = 6,
1309 CPU_SUBTYPE_POWERPC_604e = 7,
1310 CPU_SUBTYPE_POWERPC_620 = 8,
1311 CPU_SUBTYPE_POWERPC_750 = 9,
1312 CPU_SUBTYPE_POWERPC_7400 = 10,
1313 CPU_SUBTYPE_POWERPC_7450 = 11,
1314 CPU_SUBTYPE_POWERPC_970 = 100,
1316 CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
1317 CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
1319 } // end namespace MachO
1320 } // end namespace llvm