| 1 | //! Numeric constants mirrored from Apple's `<mach-o/loader.h>`, `<mach-o/nlist.h>`, |
| 2 | //! `<mach-o/reloc.h>`, and `<mach-o/arm64/reloc.h>`. Duplicated from afs-as |
| 3 | //! rather than cross-crate coupled so each submodule remains independent. |
| 4 | //! |
| 5 | //! Sprint 0 lands the baseline set the reader needs in Sprint 1; later sprints |
| 6 | //! extend this module as new load commands / section flags / relocation types |
| 7 | //! come into scope. |
| 8 | |
| 9 | // Magic and file types |
| 10 | pub const MH_MAGIC_64: u32 = 0xFEEDFACF; |
| 11 | pub const CPU_TYPE_ARM64: u32 = 0x0100_000C; |
| 12 | pub const CPU_SUBTYPE_ARM64_ALL: u32 = 0; |
| 13 | |
| 14 | pub const MH_OBJECT: u32 = 1; |
| 15 | pub const MH_EXECUTE: u32 = 2; |
| 16 | pub const MH_DYLIB: u32 = 6; |
| 17 | |
| 18 | // Mach header flags |
| 19 | pub const MH_NOUNDEFS: u32 = 0x1; |
| 20 | pub const MH_DYLDLINK: u32 = 0x4; |
| 21 | pub const MH_TWOLEVEL: u32 = 0x80; |
| 22 | pub const MH_SUBSECTIONS_VIA_SYMBOLS: u32 = 0x2000; |
| 23 | pub const MH_PIE: u32 = 0x0020_0000; |
| 24 | pub const MH_HAS_TLV_DESCRIPTORS: u32 = 0x0080_0000; |
| 25 | |
| 26 | // Load command kinds (subset afs-as emits plus the writer will need) |
| 27 | pub const LC_REQ_DYLD: u32 = 0x8000_0000; |
| 28 | |
| 29 | pub const LC_SEGMENT_64: u32 = 0x19; |
| 30 | pub const LC_SYMTAB: u32 = 0x02; |
| 31 | pub const LC_DYSYMTAB: u32 = 0x0B; |
| 32 | pub const LC_LOAD_DYLIB: u32 = 0x0C; |
| 33 | pub const LC_ID_DYLIB: u32 = 0x0D; |
| 34 | pub const LC_LOAD_DYLINKER: u32 = 0x0E; |
| 35 | pub const LC_LOAD_WEAK_DYLIB: u32 = 0x18 | LC_REQ_DYLD; |
| 36 | pub const LC_REEXPORT_DYLIB: u32 = 0x1F | LC_REQ_DYLD; |
| 37 | pub const LC_UUID: u32 = 0x1B; |
| 38 | pub const LC_RPATH: u32 = 0x1C | LC_REQ_DYLD; |
| 39 | pub const LC_CODE_SIGNATURE: u32 = 0x1D; |
| 40 | pub const LC_FUNCTION_STARTS: u32 = 0x26; |
| 41 | pub const LC_DATA_IN_CODE: u32 = 0x29; |
| 42 | pub const LC_SOURCE_VERSION: u32 = 0x2A; |
| 43 | pub const LC_LINKER_OPTIMIZATION_HINT: u32 = 0x2E; |
| 44 | pub const LC_BUILD_VERSION: u32 = 0x32; |
| 45 | pub const LC_DYLD_INFO_ONLY: u32 = 0x22 | LC_REQ_DYLD; |
| 46 | pub const LC_DYLD_CHAINED_FIXUPS: u32 = 0x34 | LC_REQ_DYLD; |
| 47 | pub const LC_DYLD_EXPORTS_TRIE: u32 = 0x33 | LC_REQ_DYLD; |
| 48 | pub const LC_MAIN: u32 = 0x28 | LC_REQ_DYLD; |
| 49 | pub const LC_LOAD_UPWARD_DYLIB: u32 = 0x23 | LC_REQ_DYLD; |
| 50 | |
| 51 | // Segment flags |
| 52 | pub const SG_READ_ONLY: u32 = 0x10; |
| 53 | |
| 54 | // Section type nibble (flags & 0xff) |
| 55 | pub const SECTION_TYPE_MASK: u32 = 0x0000_00ff; |
| 56 | pub const S_REGULAR: u32 = 0x0; |
| 57 | pub const S_ZEROFILL: u32 = 0x1; |
| 58 | pub const S_CSTRING_LITERALS: u32 = 0x2; |
| 59 | pub const S_4BYTE_LITERALS: u32 = 0x3; |
| 60 | pub const S_8BYTE_LITERALS: u32 = 0x4; |
| 61 | pub const S_LITERAL_POINTERS: u32 = 0x5; |
| 62 | pub const S_NON_LAZY_SYMBOL_POINTERS: u32 = 0x6; |
| 63 | pub const S_LAZY_SYMBOL_POINTERS: u32 = 0x7; |
| 64 | pub const S_SYMBOL_STUBS: u32 = 0x8; |
| 65 | pub const S_MOD_INIT_FUNC_POINTERS: u32 = 0x9; |
| 66 | pub const S_MOD_TERM_FUNC_POINTERS: u32 = 0xA; |
| 67 | pub const S_COALESCED: u32 = 0xB; |
| 68 | pub const S_GB_ZEROFILL: u32 = 0xC; |
| 69 | pub const S_INTERPOSING: u32 = 0xD; |
| 70 | pub const S_16BYTE_LITERALS: u32 = 0xE; |
| 71 | pub const S_THREAD_LOCAL_REGULAR: u32 = 0x11; |
| 72 | pub const S_THREAD_LOCAL_ZEROFILL: u32 = 0x12; |
| 73 | pub const S_THREAD_LOCAL_VARIABLES: u32 = 0x13; |
| 74 | pub const S_THREAD_LOCAL_VARIABLE_POINTERS: u32 = 0x14; |
| 75 | pub const S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: u32 = 0x15; |
| 76 | |
| 77 | // Section attribute bits (high byte of flags) |
| 78 | pub const S_ATTR_PURE_INSTRUCTIONS: u32 = 0x8000_0000; |
| 79 | pub const S_ATTR_NO_TOC: u32 = 0x4000_0000; |
| 80 | pub const S_ATTR_STRIP_STATIC_SYMS: u32 = 0x2000_0000; |
| 81 | pub const S_ATTR_NO_DEAD_STRIP: u32 = 0x1000_0000; |
| 82 | pub const S_ATTR_LIVE_SUPPORT: u32 = 0x0800_0000; |
| 83 | pub const S_ATTR_SELF_MODIFYING_CODE: u32 = 0x0400_0000; |
| 84 | pub const S_ATTR_DEBUG: u32 = 0x0200_0000; |
| 85 | pub const S_ATTR_SOME_INSTRUCTIONS: u32 = 0x0000_0400; |
| 86 | pub const S_ATTR_EXT_RELOC: u32 = 0x0000_0200; |
| 87 | pub const S_ATTR_LOC_RELOC: u32 = 0x0000_0100; |
| 88 | |
| 89 | // data_in_code_entry kinds |
| 90 | pub const DICE_KIND_DATA: u16 = 1; |
| 91 | pub const DICE_KIND_JUMP_TABLE8: u16 = 2; |
| 92 | pub const DICE_KIND_JUMP_TABLE16: u16 = 3; |
| 93 | pub const DICE_KIND_JUMP_TABLE32: u16 = 4; |
| 94 | pub const DICE_KIND_ABS_JUMP_TABLE32: u16 = 5; |
| 95 | |
| 96 | // nlist_64 n_type |
| 97 | pub const N_STAB: u8 = 0xe0; |
| 98 | pub const N_PEXT: u8 = 0x10; |
| 99 | pub const N_TYPE: u8 = 0x0e; |
| 100 | pub const N_EXT: u8 = 0x01; |
| 101 | |
| 102 | pub const N_UNDF: u8 = 0x0; |
| 103 | pub const N_ABS: u8 = 0x2; |
| 104 | pub const N_SECT: u8 = 0xe; |
| 105 | pub const N_INDR: u8 = 0xa; |
| 106 | pub const NO_SECT: u8 = 0; |
| 107 | |
| 108 | // nlist_64 n_desc bits |
| 109 | pub const REFERENCED_DYNAMICALLY: u16 = 0x0010; |
| 110 | pub const N_NO_DEAD_STRIP: u16 = 0x0020; |
| 111 | pub const N_WEAK_REF: u16 = 0x0040; |
| 112 | pub const N_WEAK_DEF: u16 = 0x0080; |
| 113 | /// Symbol is an alternate entry point into the atom defined by the |
| 114 | /// preceding symbol in the same section. Folded into that atom's |
| 115 | /// `alt_entries` during atomization. |
| 116 | pub const N_ALT_ENTRY: u16 = 0x0200; |
| 117 | |
| 118 | // Indirect symbol table sentinels (<mach-o/loader.h>) |
| 119 | pub const INDIRECT_SYMBOL_LOCAL: u32 = 0x8000_0000; |
| 120 | pub const INDIRECT_SYMBOL_ABS: u32 = 0x4000_0000; |
| 121 | |
| 122 | // ARM64 relocation kinds |
| 123 | pub const ARM64_RELOC_UNSIGNED: u8 = 0; |
| 124 | pub const ARM64_RELOC_SUBTRACTOR: u8 = 1; |
| 125 | pub const ARM64_RELOC_BRANCH26: u8 = 2; |
| 126 | pub const ARM64_RELOC_PAGE21: u8 = 3; |
| 127 | pub const ARM64_RELOC_PAGEOFF12: u8 = 4; |
| 128 | pub const ARM64_RELOC_GOT_LOAD_PAGE21: u8 = 5; |
| 129 | pub const ARM64_RELOC_GOT_LOAD_PAGEOFF12: u8 = 6; |
| 130 | pub const ARM64_RELOC_POINTER_TO_GOT: u8 = 7; |
| 131 | pub const ARM64_RELOC_TLVP_LOAD_PAGE21: u8 = 8; |
| 132 | pub const ARM64_RELOC_TLVP_LOAD_PAGEOFF12: u8 = 9; |
| 133 | pub const ARM64_RELOC_ADDEND: u8 = 10; |
| 134 | |
| 135 | // Platforms for LC_BUILD_VERSION |
| 136 | pub const PLATFORM_MACOS: u32 = 1; |
| 137 | pub const PLATFORM_IOS: u32 = 2; |
| 138 | |
| 139 | // Export trie terminal-node flags (<mach-o/loader.h>) |
| 140 | pub const EXPORT_SYMBOL_FLAGS_KIND_MASK: u64 = 0x03; |
| 141 | pub const EXPORT_SYMBOL_FLAGS_KIND_REGULAR: u64 = 0x00; |
| 142 | pub const EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL: u64 = 0x01; |
| 143 | pub const EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE: u64 = 0x02; |
| 144 | pub const EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u64 = 0x04; |
| 145 | pub const EXPORT_SYMBOL_FLAGS_REEXPORT: u64 = 0x08; |
| 146 | pub const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: u64 = 0x10; |
| 147 | |
| 148 | // Classic dyld bind opcodes / flags (<mach-o/loader.h>) |
| 149 | pub const REBASE_TYPE_POINTER: u8 = 1; |
| 150 | |
| 151 | pub const REBASE_OPCODE_MASK: u8 = 0xF0; |
| 152 | pub const REBASE_IMMEDIATE_MASK: u8 = 0x0F; |
| 153 | pub const REBASE_OPCODE_DONE: u8 = 0x00; |
| 154 | pub const REBASE_OPCODE_SET_TYPE_IMM: u8 = 0x10; |
| 155 | pub const REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x20; |
| 156 | pub const REBASE_OPCODE_ADD_ADDR_ULEB: u8 = 0x30; |
| 157 | pub const REBASE_OPCODE_ADD_ADDR_IMM_SCALED: u8 = 0x40; |
| 158 | pub const REBASE_OPCODE_DO_REBASE_IMM_TIMES: u8 = 0x50; |
| 159 | pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES: u8 = 0x60; |
| 160 | pub const REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: u8 = 0x70; |
| 161 | pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: u8 = 0x80; |
| 162 | |
| 163 | // Classic dyld bind opcodes / flags (<mach-o/loader.h>) |
| 164 | pub const BIND_TYPE_POINTER: u8 = 1; |
| 165 | |
| 166 | pub const BIND_SYMBOL_FLAGS_WEAK_IMPORT: u8 = 0x1; |
| 167 | |
| 168 | pub const BIND_OPCODE_MASK: u8 = 0xF0; |
| 169 | pub const BIND_IMMEDIATE_MASK: u8 = 0x0F; |
| 170 | pub const BIND_OPCODE_DONE: u8 = 0x00; |
| 171 | pub const BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: u8 = 0x10; |
| 172 | pub const BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: u8 = 0x20; |
| 173 | pub const BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: u8 = 0x30; |
| 174 | pub const BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: u8 = 0x40; |
| 175 | pub const BIND_OPCODE_SET_TYPE_IMM: u8 = 0x50; |
| 176 | pub const BIND_OPCODE_SET_ADDEND_SLEB: u8 = 0x60; |
| 177 | pub const BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x70; |
| 178 | pub const BIND_OPCODE_ADD_ADDR_ULEB: u8 = 0x80; |
| 179 | pub const BIND_OPCODE_DO_BIND: u8 = 0x90; |
| 180 | pub const BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: u8 = 0xA0; |
| 181 | pub const BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: u8 = 0xB0; |
| 182 | pub const BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: u8 = 0xC0; |
| 183 |