| 1 |
/* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 |
/* Copyright 1996-2018 The NASM Authors - All Rights Reserved */ |
| 3 |
|
| 4 |
#ifndef OUTPUT_MACHO_H |
| 5 |
#define OUTPUT_MACHO_H |
| 6 |
|
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* Magics */ |
| 10 |
#define MH_MAGIC 0xfeedface |
| 11 |
#define MH_MAGIC_64 0xfeedfacf |
| 12 |
|
| 13 |
/* File types */ |
| 14 |
#define MH_OBJECT 0x1 |
| 15 |
|
| 16 |
/* CPUs */ |
| 17 |
#define CPU_ARCH_MASK 0xff000000 |
| 18 |
#define CPU_ARCH_ABI64 0x01000000 |
| 19 |
#define CPU_TYPE_X86 7 |
| 20 |
#define CPU_TYPE_I386 CPU_TYPE_X86 |
| 21 |
#define CPU_TYPE_X86_64 (CPU_TYPE_X86 | CPU_ARCH_ABI64) |
| 22 |
|
| 23 |
#define CPU_SUBTYPE_MASK 0xff000000 |
| 24 |
#define CPU_SUBTYPE_I386_ALL 3 |
| 25 |
|
| 26 |
/* Header flags */ |
| 27 |
#define MH_SUBSECTIONS_VIA_SYMBOLS 0x00002000 |
| 28 |
|
| 29 |
/* Load commands */ |
| 30 |
#define LC_SEGMENT 0x1 |
| 31 |
#define LC_SEGMENT_64 0x19 |
| 32 |
#define LC_SYMTAB 0x2 |
| 33 |
#define LC_BUILD_VERSION 0x32 |
| 34 |
|
| 35 |
/* Symbol type bits */ |
| 36 |
#define N_STAB 0xe0 |
| 37 |
#define N_PEXT 0x10 |
| 38 |
#define N_TYPE 0x0e |
| 39 |
#define N_EXT 0x01 |
| 40 |
|
| 41 |
/* To mask with N_TYPE */ |
| 42 |
#define N_UNDF 0x00 |
| 43 |
#define N_ABS 0x02 |
| 44 |
#define N_INDR 0x0a |
| 45 |
#define N_PBUD 0x0c |
| 46 |
#define N_SECT 0x0e |
| 47 |
|
| 48 |
/* Section ordinals */ |
| 49 |
#define NO_SECT 0x00 |
| 50 |
#define MAX_SECT 0xff |
| 51 |
|
| 52 |
/* Section bits */ |
| 53 |
#define SECTION_TYPE 0x000000ff |
| 54 |
#define SECTION_ATTRIBUTES 0xffffff00 |
| 55 |
#define SECTION_ATTRIBUTES_USR 0xff000000 |
| 56 |
#define SECTION_ATTRIBUTES_SYS 0x00ffff00 |
| 57 |
|
| 58 |
#define S_REGULAR 0x00 |
| 59 |
#define S_ZEROFILL 0x01 |
| 60 |
#define S_CSTRING_LITERALS 0x02 |
| 61 |
#define S_4BYTE_LITERALS 0x03 |
| 62 |
#define S_8BYTE_LITERALS 0x04 |
| 63 |
#define S_LITERAL_POINTERS 0x05 |
| 64 |
#define S_NON_LAZY_SYMBOL_POINTERS 0x06 |
| 65 |
#define S_LAZY_SYMBOL_POINTERS 0x07 |
| 66 |
#define S_SYMBOL_STUBS 0x08 |
| 67 |
#define S_MOD_INIT_FUNC_POINTERS 0x09 |
| 68 |
#define S_MOD_TERM_FUNC_POINTERS 0x0a |
| 69 |
#define S_COALESCED 0x0b |
| 70 |
#define S_GB_ZEROFILL 0x0c |
| 71 |
#define S_INTERPOSING 0x0d |
| 72 |
#define S_16BYTE_LITERALS 0x0e |
| 73 |
#define S_DTRACE_DOF 0x0f |
| 74 |
#define S_LAZY_DYLIB_SYMBOL_POINTERS 0x10 |
| 75 |
#define S_THREAD_LOCAL_REGULAR 0x11 |
| 76 |
#define S_THREAD_LOCAL_ZEROFILL 0x12 |
| 77 |
#define S_THREAD_LOCAL_VARIABLES 0x13 |
| 78 |
#define S_THREAD_LOCAL_VARIABLE_POINTERS 0x14 |
| 79 |
#define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS 0x15 |
| 80 |
|
| 81 |
#define S_ATTR_PURE_INSTRUCTIONS 0x80000000 |
| 82 |
#define S_ATTR_NO_TOC 0x40000000 |
| 83 |
#define S_ATTR_STRIP_STATIC_SYMS 0x20000000 |
| 84 |
#define S_ATTR_NO_DEAD_STRIP 0x10000000 |
| 85 |
#define S_ATTR_LIVE_SUPPORT 0x08000000 |
| 86 |
#define S_ATTR_SELF_MODIFYING_CODE 0x04000000 |
| 87 |
#define S_ATTR_DEBUG 0x02000000 |
| 88 |
|
| 89 |
#define S_ATTR_SOME_INSTRUCTIONS 0x00000400 |
| 90 |
#define S_ATTR_EXT_RELOC 0x00000200 |
| 91 |
#define S_ATTR_LOC_RELOC 0x00000100 |
| 92 |
#define INDIRECT_SYMBOL_LOCAL 0x80000000 |
| 93 |
#define INDIRECT_SYMBOL_ABS 0x40000000 |
| 94 |
|
| 95 |
/* Relocation info type */ |
| 96 |
#define GENERIC_RELOC_VANILLA 0 |
| 97 |
#define GENERIC_RELOC_PAIR 1 |
| 98 |
#define GENERIC_RELOC_SECTDIFF 2 |
| 99 |
#define GENERIC_RELOC_PB_LA_PTR 3 |
| 100 |
#define GENERIC_RELOC_LOCAL_SECTDIFF 4 |
| 101 |
#define GENERIC_RELOC_TLV 5 |
| 102 |
|
| 103 |
#define X86_64_RELOC_UNSIGNED 0 |
| 104 |
#define X86_64_RELOC_SIGNED 1 |
| 105 |
#define X86_64_RELOC_BRANCH 2 |
| 106 |
#define X86_64_RELOC_GOT_LOAD 3 |
| 107 |
#define X86_64_RELOC_GOT 4 |
| 108 |
#define X86_64_RELOC_SUBTRACTOR 5 |
| 109 |
#define X86_64_RELOC_SIGNED_1 6 |
| 110 |
#define X86_64_RELOC_SIGNED_2 7 |
| 111 |
#define X86_64_RELOC_SIGNED_4 8 |
| 112 |
#define X86_64_RELOC_TLV 9 |
| 113 |
|
| 114 |
/* Relocation info */ |
| 115 |
#define R_ABS 0 |
| 116 |
#define R_SCATTERED 0x80000000 |
| 117 |
|
| 118 |
/* VM permission constants */ |
| 119 |
#define VM_PROT_NONE 0x00 |
| 120 |
#define VM_PROT_READ 0x01 |
| 121 |
#define VM_PROT_WRITE 0x02 |
| 122 |
#define VM_PROT_EXECUTE 0x04 |
| 123 |
|
| 124 |
/* Platforms */ |
| 125 |
/* X-macro X(_platform, _id, _name) */ |
| 126 |
#define MACHO_ALL_PLATFORMS \ |
| 127 |
X(PLATFORM_UNKNOWN, 0, "unknown") \ |
| 128 |
X(PLATFORM_MACOS, 1, "macos") \ |
| 129 |
X(PLATFORM_IOS, 2, "ios") \ |
| 130 |
X(PLATFORM_TVOS, 3, "tvos") \ |
| 131 |
X(PLATFORM_WATCHOS, 4, "watchos") \ |
| 132 |
X(PLATFORM_BRIDGEOS, 5, "bridgeos") \ |
| 133 |
X(PLATFORM_MACCATALYST, 6, "macCatalyst") \ |
| 134 |
X(PLATFORM_IOSSIMULATOR, 7, "iossimulator") \ |
| 135 |
X(PLATFORM_TVOSSIMULATOR, 8, "tvossimulator") \ |
| 136 |
X(PLATFORM_WATCHOSSIMULATOR, 9, "watchossimulator") \ |
| 137 |
X(PLATFORM_DRIVERKIT, 10, "driverkit") \ |
| 138 |
X(PLATFORM_XROS, 11, "xros") \ |
| 139 |
X(PLATFORM_XROS_SIMULATOR, 12, "xrsimulator") \ |
| 140 |
/* end */ |
| 141 |
|
| 142 |
typedef struct { |
| 143 |
uint32_t magic; |
| 144 |
uint32_t cputype; |
| 145 |
uint32_t cpusubtype; |
| 146 |
uint32_t filetype; |
| 147 |
uint32_t ncmds; |
| 148 |
uint32_t sizeofcmds; |
| 149 |
uint32_t flags; |
| 150 |
} macho_header_t; |
| 151 |
|
| 152 |
typedef struct { |
| 153 |
uint32_t magic; |
| 154 |
uint32_t cputype; |
| 155 |
uint32_t cpusubtype; |
| 156 |
uint32_t filetype; |
| 157 |
uint32_t ncmds; |
| 158 |
uint32_t sizeofcmds; |
| 159 |
uint32_t flags; |
| 160 |
uint32_t reserved; |
| 161 |
} macho_header_64_t; |
| 162 |
|
| 163 |
typedef struct { |
| 164 |
uint32_t cmd; |
| 165 |
uint32_t cmdsize; |
| 166 |
} macho_load_command_t; |
| 167 |
|
| 168 |
typedef struct { |
| 169 |
uint32_t cmd; |
| 170 |
uint32_t cmdsize; |
| 171 |
char segname[16]; |
| 172 |
uint32_t vmaddr; |
| 173 |
uint32_t vmsize; |
| 174 |
uint32_t fileoff; |
| 175 |
uint32_t filesize; |
| 176 |
uint32_t maxprot; |
| 177 |
uint32_t initprot; |
| 178 |
uint32_t nsects; |
| 179 |
uint32_t flags; |
| 180 |
} macho_segment_command_t; |
| 181 |
|
| 182 |
typedef struct { |
| 183 |
uint32_t cmd; |
| 184 |
uint32_t cmdsize; |
| 185 |
char segname[16]; |
| 186 |
uint64_t vmaddr; |
| 187 |
uint64_t vmsize; |
| 188 |
uint64_t fileoff; |
| 189 |
uint64_t filesize; |
| 190 |
uint32_t maxprot; |
| 191 |
uint32_t initprot; |
| 192 |
uint32_t nsects; |
| 193 |
uint32_t flags; |
| 194 |
} macho_segment_command_64_t; |
| 195 |
|
| 196 |
typedef struct { |
| 197 |
char sectname[16]; |
| 198 |
char segname[16]; |
| 199 |
uint32_t addr; |
| 200 |
uint32_t size; |
| 201 |
uint32_t offset; |
| 202 |
uint32_t align; |
| 203 |
uint32_t reloff; |
| 204 |
uint32_t nreloc; |
| 205 |
uint32_t flags; |
| 206 |
uint32_t reserved1; |
| 207 |
uint32_t reserved2; |
| 208 |
} macho_section_t; |
| 209 |
|
| 210 |
typedef struct { |
| 211 |
char sectname[16]; |
| 212 |
char segname[16]; |
| 213 |
uint64_t addr; |
| 214 |
uint64_t size; |
| 215 |
uint32_t offset; |
| 216 |
uint32_t align; |
| 217 |
uint32_t reloff; |
| 218 |
uint32_t nreloc; |
| 219 |
uint32_t flags; |
| 220 |
uint32_t reserved1; |
| 221 |
uint32_t reserved2; |
| 222 |
uint32_t reserved3; |
| 223 |
} macho_section_64_t; |
| 224 |
|
| 225 |
typedef struct { |
| 226 |
uint32_t cmd; |
| 227 |
uint32_t cmdsize; |
| 228 |
uint32_t symoff; |
| 229 |
uint32_t nsyms; |
| 230 |
uint32_t stroff; |
| 231 |
uint32_t strsize; |
| 232 |
} macho_symtab_command_t; |
| 233 |
|
| 234 |
typedef struct { |
| 235 |
int32_t r_address; |
| 236 |
union { |
| 237 |
struct { |
| 238 |
uint32_t r_symbolnum: 24, |
| 239 |
r_pcrel: 1, |
| 240 |
r_length: 2, |
| 241 |
r_extern: 1, |
| 242 |
r_type: 4; |
| 243 |
} s; |
| 244 |
uint32_t r_raw; |
| 245 |
} u; |
| 246 |
} macho_relocation_info_t; |
| 247 |
|
| 248 |
typedef struct nlist_base { |
| 249 |
uint32_t n_strx; |
| 250 |
uint8_t n_type; |
| 251 |
uint8_t n_sect; |
| 252 |
uint16_t n_desc; |
| 253 |
} macho_nlist_base_t; |
| 254 |
|
| 255 |
typedef struct nlist { |
| 256 |
uint32_t n_strx; |
| 257 |
uint8_t n_type; |
| 258 |
uint8_t n_sect; |
| 259 |
int16_t n_desc; |
| 260 |
uint32_t n_value; |
| 261 |
} macho_nlist_t; |
| 262 |
|
| 263 |
typedef struct { |
| 264 |
uint32_t n_strx; |
| 265 |
uint8_t n_type; |
| 266 |
uint8_t n_sect; |
| 267 |
uint16_t n_desc; |
| 268 |
uint64_t n_value; |
| 269 |
} macho_nlist_64_t; |
| 270 |
|
| 271 |
/* Adapted from LLVM include/llvm/BinaryFormat/MachO.h */ |
| 272 |
typedef struct { |
| 273 |
uint32_t tool; |
| 274 |
uint32_t version; |
| 275 |
} macho_build_tool_version_t; |
| 276 |
|
| 277 |
typedef struct { |
| 278 |
uint32_t cmd; |
| 279 |
uint32_t cmdsize; |
| 280 |
uint32_t platform; |
| 281 |
uint32_t minos; /* x.y.z is 0xXXXXYYZZ */ |
| 282 |
uint32_t sdk; /* x.y.z is 0xXXXXYYZZ */ |
| 283 |
uint32_t ntools; |
| 284 |
/* ntools macho_build_tool_version_t follow this */ |
| 285 |
} macho_build_version_command_t; |
| 286 |
|
| 287 |
#endif /* OUTPUT_MACHO_H */ |