1 //! Mach-O definitions. 2 //! 3 //! These definitions are independent of read/write support, although we do implement 4 //! some traits useful for those. 5 //! 6 //! This module is based heavily on header files from MacOSX11.1.sdk. 7 8 #![allow(missing_docs)] 9 10 use crate::endian::{BigEndian, Endian, U64Bytes, U16, U32, U64}; 11 use crate::pod::Pod; 12 13 // Definitions from "/usr/include/mach/machine.h". 14 15 /* 16 * Capability bits used in the definition of cpu_type. 17 */ 18 19 /// mask for architecture bits 20 pub const CPU_ARCH_MASK: u32 = 0xff00_0000; 21 /// 64 bit ABI 22 pub const CPU_ARCH_ABI64: u32 = 0x0100_0000; 23 /// ABI for 64-bit hardware with 32-bit types; LP32 24 pub const CPU_ARCH_ABI64_32: u32 = 0x0200_0000; 25 26 /* 27 * Machine types known by all. 28 */ 29 30 pub const CPU_TYPE_ANY: u32 = !0; 31 32 pub const CPU_TYPE_VAX: u32 = 1; 33 pub const CPU_TYPE_MC680X0: u32 = 6; 34 pub const CPU_TYPE_X86: u32 = 7; 35 pub const CPU_TYPE_X86_64: u32 = CPU_TYPE_X86 | CPU_ARCH_ABI64; 36 pub const CPU_TYPE_MIPS: u32 = 8; 37 pub const CPU_TYPE_MC98000: u32 = 10; 38 pub const CPU_TYPE_HPPA: u32 = 11; 39 pub const CPU_TYPE_ARM: u32 = 12; 40 pub const CPU_TYPE_ARM64: u32 = CPU_TYPE_ARM | CPU_ARCH_ABI64; 41 pub const CPU_TYPE_ARM64_32: u32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32; 42 pub const CPU_TYPE_MC88000: u32 = 13; 43 pub const CPU_TYPE_SPARC: u32 = 14; 44 pub const CPU_TYPE_I860: u32 = 15; 45 pub const CPU_TYPE_ALPHA: u32 = 16; 46 pub const CPU_TYPE_POWERPC: u32 = 18; 47 pub const CPU_TYPE_POWERPC64: u32 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64; 48 49 /* 50 * Capability bits used in the definition of cpu_subtype. 51 */ 52 /// mask for feature flags 53 pub const CPU_SUBTYPE_MASK: u32 = 0xff00_0000; 54 /// 64 bit libraries 55 pub const CPU_SUBTYPE_LIB64: u32 = 0x8000_0000; 56 /// pointer authentication with versioned ABI 57 pub const CPU_SUBTYPE_PTRAUTH_ABI: u32 = 0x8000_0000; 58 59 /// When selecting a slice, ANY will pick the slice with the best 60 /// grading for the selected cpu_type_t, unlike the "ALL" subtypes, 61 /// which are the slices that can run on any hardware for that cpu type. 62 pub const CPU_SUBTYPE_ANY: u32 = !0; 63 64 /* 65 * Object files that are hand-crafted to run on any 66 * implementation of an architecture are tagged with 67 * CPU_SUBTYPE_MULTIPLE. This functions essentially the same as 68 * the "ALL" subtype of an architecture except that it allows us 69 * to easily find object files that may need to be modified 70 * whenever a new implementation of an architecture comes out. 71 * 72 * It is the responsibility of the implementor to make sure the 73 * software handles unsupported implementations elegantly. 74 */ 75 pub const CPU_SUBTYPE_MULTIPLE: u32 = !0; 76 pub const CPU_SUBTYPE_LITTLE_ENDIAN: u32 = 0; 77 pub const CPU_SUBTYPE_BIG_ENDIAN: u32 = 1; 78 79 /* 80 * VAX subtypes (these do *not* necessary conform to the actual cpu 81 * ID assigned by DEC available via the SID register). 82 */ 83 84 pub const CPU_SUBTYPE_VAX_ALL: u32 = 0; 85 pub const CPU_SUBTYPE_VAX780: u32 = 1; 86 pub const CPU_SUBTYPE_VAX785: u32 = 2; 87 pub const CPU_SUBTYPE_VAX750: u32 = 3; 88 pub const CPU_SUBTYPE_VAX730: u32 = 4; 89 pub const CPU_SUBTYPE_UVAXI: u32 = 5; 90 pub const CPU_SUBTYPE_UVAXII: u32 = 6; 91 pub const CPU_SUBTYPE_VAX8200: u32 = 7; 92 pub const CPU_SUBTYPE_VAX8500: u32 = 8; 93 pub const CPU_SUBTYPE_VAX8600: u32 = 9; 94 pub const CPU_SUBTYPE_VAX8650: u32 = 10; 95 pub const CPU_SUBTYPE_VAX8800: u32 = 11; 96 pub const CPU_SUBTYPE_UVAXIII: u32 = 12; 97 98 /* 99 * 680x0 subtypes 100 * 101 * The subtype definitions here are unusual for historical reasons. 102 * NeXT used to consider 68030 code as generic 68000 code. For 103 * backwards compatability: 104 * 105 * CPU_SUBTYPE_MC68030 symbol has been preserved for source code 106 * compatability. 107 * 108 * CPU_SUBTYPE_MC680x0_ALL has been defined to be the same 109 * subtype as CPU_SUBTYPE_MC68030 for binary comatability. 110 * 111 * CPU_SUBTYPE_MC68030_ONLY has been added to allow new object 112 * files to be tagged as containing 68030-specific instructions. 113 */ 114 115 pub const CPU_SUBTYPE_MC680X0_ALL: u32 = 1; 116 // compat 117 pub const CPU_SUBTYPE_MC68030: u32 = 1; 118 pub const CPU_SUBTYPE_MC68040: u32 = 2; 119 pub const CPU_SUBTYPE_MC68030_ONLY: u32 = 3; 120 121 /* 122 * I386 subtypes 123 */ 124 125 #[inline] 126 pub const fn cpu_subtype_intel(f: u32, m: u32) -> u32 { 127 f + (m << 4) 128 } 129 130 pub const CPU_SUBTYPE_I386_ALL: u32 = cpu_subtype_intel(3, 0); 131 pub const CPU_SUBTYPE_386: u32 = cpu_subtype_intel(3, 0); 132 pub const CPU_SUBTYPE_486: u32 = cpu_subtype_intel(4, 0); 133 pub const CPU_SUBTYPE_486SX: u32 = cpu_subtype_intel(4, 8); 134 pub const CPU_SUBTYPE_586: u32 = cpu_subtype_intel(5, 0); 135 pub const CPU_SUBTYPE_PENT: u32 = cpu_subtype_intel(5, 0); 136 pub const CPU_SUBTYPE_PENTPRO: u32 = cpu_subtype_intel(6, 1); 137 pub const CPU_SUBTYPE_PENTII_M3: u32 = cpu_subtype_intel(6, 3); 138 pub const CPU_SUBTYPE_PENTII_M5: u32 = cpu_subtype_intel(6, 5); 139 pub const CPU_SUBTYPE_CELERON: u32 = cpu_subtype_intel(7, 6); 140 pub const CPU_SUBTYPE_CELERON_MOBILE: u32 = cpu_subtype_intel(7, 7); 141 pub const CPU_SUBTYPE_PENTIUM_3: u32 = cpu_subtype_intel(8, 0); 142 pub const CPU_SUBTYPE_PENTIUM_3_M: u32 = cpu_subtype_intel(8, 1); 143 pub const CPU_SUBTYPE_PENTIUM_3_XEON: u32 = cpu_subtype_intel(8, 2); 144 pub const CPU_SUBTYPE_PENTIUM_M: u32 = cpu_subtype_intel(9, 0); 145 pub const CPU_SUBTYPE_PENTIUM_4: u32 = cpu_subtype_intel(10, 0); 146 pub const CPU_SUBTYPE_PENTIUM_4_M: u32 = cpu_subtype_intel(10, 1); 147 pub const CPU_SUBTYPE_ITANIUM: u32 = cpu_subtype_intel(11, 0); 148 pub const CPU_SUBTYPE_ITANIUM_2: u32 = cpu_subtype_intel(11, 1); 149 pub const CPU_SUBTYPE_XEON: u32 = cpu_subtype_intel(12, 0); 150 pub const CPU_SUBTYPE_XEON_MP: u32 = cpu_subtype_intel(12, 1); 151 152 #[inline] 153 pub const fn cpu_subtype_intel_family(x: u32) -> u32 { 154 x & 15 155 } 156 pub const CPU_SUBTYPE_INTEL_FAMILY_MAX: u32 = 15; 157 158 #[inline] 159 pub const fn cpu_subtype_intel_model(x: u32) -> u32 { 160 x >> 4 161 } 162 pub const CPU_SUBTYPE_INTEL_MODEL_ALL: u32 = 0; 163 164 /* 165 * X86 subtypes. 166 */ 167 168 pub const CPU_SUBTYPE_X86_ALL: u32 = 3; 169 pub const CPU_SUBTYPE_X86_64_ALL: u32 = 3; 170 pub const CPU_SUBTYPE_X86_ARCH1: u32 = 4; 171 /// Haswell feature subset 172 pub const CPU_SUBTYPE_X86_64_H: u32 = 8; 173 174 /* 175 * Mips subtypes. 176 */ 177 178 pub const CPU_SUBTYPE_MIPS_ALL: u32 = 0; 179 pub const CPU_SUBTYPE_MIPS_R2300: u32 = 1; 180 pub const CPU_SUBTYPE_MIPS_R2600: u32 = 2; 181 pub const CPU_SUBTYPE_MIPS_R2800: u32 = 3; 182 /// pmax 183 pub const CPU_SUBTYPE_MIPS_R2000A: u32 = 4; 184 pub const CPU_SUBTYPE_MIPS_R2000: u32 = 5; 185 /// 3max 186 pub const CPU_SUBTYPE_MIPS_R3000A: u32 = 6; 187 pub const CPU_SUBTYPE_MIPS_R3000: u32 = 7; 188 189 /* 190 * MC98000 (PowerPC) subtypes 191 */ 192 pub const CPU_SUBTYPE_MC98000_ALL: u32 = 0; 193 pub const CPU_SUBTYPE_MC98601: u32 = 1; 194 195 /* 196 * HPPA subtypes for Hewlett-Packard HP-PA family of 197 * risc processors. Port by NeXT to 700 series. 198 */ 199 200 pub const CPU_SUBTYPE_HPPA_ALL: u32 = 0; 201 pub const CPU_SUBTYPE_HPPA_7100LC: u32 = 1; 202 203 /* 204 * MC88000 subtypes. 205 */ 206 pub const CPU_SUBTYPE_MC88000_ALL: u32 = 0; 207 pub const CPU_SUBTYPE_MC88100: u32 = 1; 208 pub const CPU_SUBTYPE_MC88110: u32 = 2; 209 210 /* 211 * SPARC subtypes 212 */ 213 pub const CPU_SUBTYPE_SPARC_ALL: u32 = 0; 214 215 /* 216 * I860 subtypes 217 */ 218 pub const CPU_SUBTYPE_I860_ALL: u32 = 0; 219 pub const CPU_SUBTYPE_I860_860: u32 = 1; 220 221 /* 222 * PowerPC subtypes 223 */ 224 pub const CPU_SUBTYPE_POWERPC_ALL: u32 = 0; 225 pub const CPU_SUBTYPE_POWERPC_601: u32 = 1; 226 pub const CPU_SUBTYPE_POWERPC_602: u32 = 2; 227 pub const CPU_SUBTYPE_POWERPC_603: u32 = 3; 228 pub const CPU_SUBTYPE_POWERPC_603E: u32 = 4; 229 pub const CPU_SUBTYPE_POWERPC_603EV: u32 = 5; 230 pub const CPU_SUBTYPE_POWERPC_604: u32 = 6; 231 pub const CPU_SUBTYPE_POWERPC_604E: u32 = 7; 232 pub const CPU_SUBTYPE_POWERPC_620: u32 = 8; 233 pub const CPU_SUBTYPE_POWERPC_750: u32 = 9; 234 pub const CPU_SUBTYPE_POWERPC_7400: u32 = 10; 235 pub const CPU_SUBTYPE_POWERPC_7450: u32 = 11; 236 pub const CPU_SUBTYPE_POWERPC_970: u32 = 100; 237 238 /* 239 * ARM subtypes 240 */ 241 pub const CPU_SUBTYPE_ARM_ALL: u32 = 0; 242 pub const CPU_SUBTYPE_ARM_V4T: u32 = 5; 243 pub const CPU_SUBTYPE_ARM_V6: u32 = 6; 244 pub const CPU_SUBTYPE_ARM_V5TEJ: u32 = 7; 245 pub const CPU_SUBTYPE_ARM_XSCALE: u32 = 8; 246 /// ARMv7-A and ARMv7-R 247 pub const CPU_SUBTYPE_ARM_V7: u32 = 9; 248 /// Cortex A9 249 pub const CPU_SUBTYPE_ARM_V7F: u32 = 10; 250 /// Swift 251 pub const CPU_SUBTYPE_ARM_V7S: u32 = 11; 252 pub const CPU_SUBTYPE_ARM_V7K: u32 = 12; 253 pub const CPU_SUBTYPE_ARM_V8: u32 = 13; 254 /// Not meant to be run under xnu 255 pub const CPU_SUBTYPE_ARM_V6M: u32 = 14; 256 /// Not meant to be run under xnu 257 pub const CPU_SUBTYPE_ARM_V7M: u32 = 15; 258 /// Not meant to be run under xnu 259 pub const CPU_SUBTYPE_ARM_V7EM: u32 = 16; 260 /// Not meant to be run under xnu 261 pub const CPU_SUBTYPE_ARM_V8M: u32 = 17; 262 263 /* 264 * ARM64 subtypes 265 */ 266 pub const CPU_SUBTYPE_ARM64_ALL: u32 = 0; 267 pub const CPU_SUBTYPE_ARM64_V8: u32 = 1; 268 pub const CPU_SUBTYPE_ARM64E: u32 = 2; 269 270 /* 271 * ARM64_32 subtypes 272 */ 273 pub const CPU_SUBTYPE_ARM64_32_ALL: u32 = 0; 274 pub const CPU_SUBTYPE_ARM64_32_V8: u32 = 1; 275 276 // Definitions from "/usr/include/mach/vm_prot.h". 277 278 /// read permission 279 pub const VM_PROT_READ: u32 = 0x01; 280 /// write permission 281 pub const VM_PROT_WRITE: u32 = 0x02; 282 /// execute permission 283 pub const VM_PROT_EXECUTE: u32 = 0x04; 284 285 // Definitions from https://opensource.apple.com/source/dyld/dyld-210.2.3/launch-cache/dyld_cache_format.h.auto.html 286 287 /// The dyld cache header, containing only the fields which are present 288 /// in all versions of dyld caches (dyld-95.3 and up). 289 /// Many more fields exist in later dyld versions, but we currently do 290 /// not need to parse those. 291 /// Corresponds to struct dyld_cache_header from dyld_cache_format.h. 292 #[derive(Debug, Clone, Copy)] 293 #[repr(C)] 294 pub struct DyldCacheHeader<E: Endian> { 295 /// e.g. "dyld_v0 i386" 296 pub magic: [u8; 16], 297 /// file offset to first dyld_cache_mapping_info 298 pub mapping_offset: U32<E>, 299 /// number of dyld_cache_mapping_info entries 300 pub mapping_count: U32<E>, 301 /// file offset to first dyld_cache_image_info 302 pub images_offset: U32<E>, 303 /// number of dyld_cache_image_info entries 304 pub images_count: U32<E>, 305 /// base address of dyld when cache was built 306 pub dyld_base_address: U64<E>, 307 } 308 309 /// Corresponds to struct dyld_cache_mapping_info from dyld_cache_format.h. 310 #[derive(Debug, Clone, Copy)] 311 #[repr(C)] 312 pub struct DyldCacheMappingInfo<E: Endian> { 313 /// 314 pub address: U64<E>, 315 /// 316 pub size: U64<E>, 317 /// 318 pub file_offset: U64<E>, 319 /// 320 pub max_prot: U32<E>, 321 /// 322 pub init_prot: U32<E>, 323 } 324 325 /// Corresponds to struct dyld_cache_image_info from dyld_cache_format.h. 326 #[derive(Debug, Clone, Copy)] 327 #[repr(C)] 328 pub struct DyldCacheImageInfo<E: Endian> { 329 /// 330 pub address: U64<E>, 331 /// 332 pub mod_time: U64<E>, 333 /// 334 pub inode: U64<E>, 335 /// 336 pub path_file_offset: U32<E>, 337 /// 338 pub pad: U32<E>, 339 } 340 341 // Definitions from "/usr/include/mach-o/loader.h". 342 343 /* 344 * This header file describes the structures of the file format for "fat" 345 * architecture specific file (wrapper design). At the begining of the file 346 * there is one `FatHeader` structure followed by a number of `FatArch*` 347 * structures. For each architecture in the file, specified by a pair of 348 * cputype and cpusubtype, the `FatHeader` describes the file offset, file 349 * size and alignment in the file of the architecture specific member. 350 * The padded bytes in the file to place each member on it's specific alignment 351 * are defined to be read as zeros and can be left as "holes" if the file system 352 * can support them as long as they read as zeros. 353 * 354 * All structures defined here are always written and read to/from disk 355 * in big-endian order. 356 */ 357 358 pub const FAT_MAGIC: u32 = 0xcafe_babe; 359 /// NXSwapLong(FAT_MAGIC) 360 pub const FAT_CIGAM: u32 = 0xbeba_feca; 361 362 #[derive(Debug, Clone, Copy)] 363 #[repr(C)] 364 pub struct FatHeader { 365 /// FAT_MAGIC or FAT_MAGIC_64 366 pub magic: U32<BigEndian>, 367 /// number of structs that follow 368 pub nfat_arch: U32<BigEndian>, 369 } 370 371 #[derive(Debug, Clone, Copy)] 372 #[repr(C)] 373 pub struct FatArch32 { 374 /// cpu specifier (int) 375 pub cputype: U32<BigEndian>, 376 /// machine specifier (int) 377 pub cpusubtype: U32<BigEndian>, 378 /// file offset to this object file 379 pub offset: U32<BigEndian>, 380 /// size of this object file 381 pub size: U32<BigEndian>, 382 /// alignment as a power of 2 383 pub align: U32<BigEndian>, 384 } 385 386 /* 387 * The support for the 64-bit fat file format described here is a work in 388 * progress and not yet fully supported in all the Apple Developer Tools. 389 * 390 * When a slice is greater than 4mb or an offset to a slice is greater than 4mb 391 * then the 64-bit fat file format is used. 392 */ 393 pub const FAT_MAGIC_64: u32 = 0xcafe_babf; 394 /// NXSwapLong(FAT_MAGIC_64) 395 pub const FAT_CIGAM_64: u32 = 0xbfba_feca; 396 397 #[derive(Debug, Clone, Copy)] 398 #[repr(C)] 399 pub struct FatArch64 { 400 /// cpu specifier (int) 401 pub cputype: U32<BigEndian>, 402 /// machine specifier (int) 403 pub cpusubtype: U32<BigEndian>, 404 /// file offset to this object file 405 pub offset: U64<BigEndian>, 406 /// size of this object file 407 pub size: U64<BigEndian>, 408 /// alignment as a power of 2 409 pub align: U32<BigEndian>, 410 /// reserved 411 pub reserved: U32<BigEndian>, 412 } 413 414 // Definitions from "/usr/include/mach-o/loader.h". 415 416 /// The 32-bit mach header. 417 /// 418 /// Appears at the very beginning of the object file for 32-bit architectures. 419 #[derive(Debug, Clone, Copy)] 420 #[repr(C)] 421 pub struct MachHeader32<E: Endian> { 422 /// mach magic number identifier 423 pub magic: U32<BigEndian>, 424 /// cpu specifier 425 pub cputype: U32<E>, 426 /// machine specifier 427 pub cpusubtype: U32<E>, 428 /// type of file 429 pub filetype: U32<E>, 430 /// number of load commands 431 pub ncmds: U32<E>, 432 /// the size of all the load commands 433 pub sizeofcmds: U32<E>, 434 /// flags 435 pub flags: U32<E>, 436 } 437 438 // Values for `MachHeader32::magic`. 439 /// the mach magic number 440 pub const MH_MAGIC: u32 = 0xfeed_face; 441 /// NXSwapInt(MH_MAGIC) 442 pub const MH_CIGAM: u32 = 0xcefa_edfe; 443 444 /// The 64-bit mach header. 445 /// 446 /// Appears at the very beginning of object files for 64-bit architectures. 447 #[derive(Debug, Clone, Copy)] 448 #[repr(C)] 449 pub struct MachHeader64<E: Endian> { 450 /// mach magic number identifier 451 pub magic: U32<BigEndian>, 452 /// cpu specifier 453 pub cputype: U32<E>, 454 /// machine specifier 455 pub cpusubtype: U32<E>, 456 /// type of file 457 pub filetype: U32<E>, 458 /// number of load commands 459 pub ncmds: U32<E>, 460 /// the size of all the load commands 461 pub sizeofcmds: U32<E>, 462 /// flags 463 pub flags: U32<E>, 464 /// reserved 465 pub reserved: U32<E>, 466 } 467 468 // Values for `MachHeader64::magic`. 469 /// the 64-bit mach magic number 470 pub const MH_MAGIC_64: u32 = 0xfeed_facf; 471 /// NXSwapInt(MH_MAGIC_64) 472 pub const MH_CIGAM_64: u32 = 0xcffa_edfe; 473 474 /* 475 * The layout of the file depends on the filetype. For all but the MH_OBJECT 476 * file type the segments are padded out and aligned on a segment alignment 477 * boundary for efficient demand pageing. The MH_EXECUTE, MH_FVMLIB, MH_DYLIB, 478 * MH_DYLINKER and MH_BUNDLE file types also have the headers included as part 479 * of their first segment. 480 * 481 * The file type MH_OBJECT is a compact format intended as output of the 482 * assembler and input (and possibly output) of the link editor (the .o 483 * format). All sections are in one unnamed segment with no segment padding. 484 * This format is used as an executable format when the file is so small the 485 * segment padding greatly increases its size. 486 * 487 * The file type MH_PRELOAD is an executable format intended for things that 488 * are not executed under the kernel (proms, stand alones, kernels, etc). The 489 * format can be executed under the kernel but may demand paged it and not 490 * preload it before execution. 491 * 492 * A core file is in MH_CORE format and can be any in an arbritray legal 493 * Mach-O file. 494 */ 495 496 // Values for `MachHeader*::filetype`. 497 /// relocatable object file 498 pub const MH_OBJECT: u32 = 0x1; 499 /// demand paged executable file 500 pub const MH_EXECUTE: u32 = 0x2; 501 /// fixed VM shared library file 502 pub const MH_FVMLIB: u32 = 0x3; 503 /// core file 504 pub const MH_CORE: u32 = 0x4; 505 /// preloaded executable file 506 pub const MH_PRELOAD: u32 = 0x5; 507 /// dynamically bound shared library 508 pub const MH_DYLIB: u32 = 0x6; 509 /// dynamic link editor 510 pub const MH_DYLINKER: u32 = 0x7; 511 /// dynamically bound bundle file 512 pub const MH_BUNDLE: u32 = 0x8; 513 /// shared library stub for static linking only, no section contents 514 pub const MH_DYLIB_STUB: u32 = 0x9; 515 /// companion file with only debug sections 516 pub const MH_DSYM: u32 = 0xa; 517 /// x86_64 kexts 518 pub const MH_KEXT_BUNDLE: u32 = 0xb; 519 /// set of mach-o's 520 pub const MH_FILESET: u32 = 0xc; 521 522 // Values for `MachHeader*::flags`. 523 /// the object file has no undefined references 524 pub const MH_NOUNDEFS: u32 = 0x1; 525 /// the object file is the output of an incremental link against a base file and can't be link edited again 526 pub const MH_INCRLINK: u32 = 0x2; 527 /// the object file is input for the dynamic linker and can't be staticly link edited again 528 pub const MH_DYLDLINK: u32 = 0x4; 529 /// the object file's undefined references are bound by the dynamic linker when loaded. 530 pub const MH_BINDATLOAD: u32 = 0x8; 531 /// the file has its dynamic undefined references prebound. 532 pub const MH_PREBOUND: u32 = 0x10; 533 /// the file has its read-only and read-write segments split 534 pub const MH_SPLIT_SEGS: u32 = 0x20; 535 /// the shared library init routine is to be run lazily via catching memory faults to its writeable segments (obsolete) 536 pub const MH_LAZY_INIT: u32 = 0x40; 537 /// the image is using two-level name space bindings 538 pub const MH_TWOLEVEL: u32 = 0x80; 539 /// the executable is forcing all images to use flat name space bindings 540 pub const MH_FORCE_FLAT: u32 = 0x100; 541 /// this umbrella guarantees no multiple defintions of symbols in its sub-images so the two-level namespace hints can always be used. 542 pub const MH_NOMULTIDEFS: u32 = 0x200; 543 /// do not have dyld notify the prebinding agent about this executable 544 pub const MH_NOFIXPREBINDING: u32 = 0x400; 545 /// the binary is not prebound but can have its prebinding redone. only used when MH_PREBOUND is not set. 546 pub const MH_PREBINDABLE: u32 = 0x800; 547 /// indicates that this binary binds to all two-level namespace modules of its dependent libraries. only used when MH_PREBINDABLE and MH_TWOLEVEL are both set. 548 pub const MH_ALLMODSBOUND: u32 = 0x1000; 549 /// safe to divide up the sections into sub-sections via symbols for dead code stripping 550 pub const MH_SUBSECTIONS_VIA_SYMBOLS: u32 = 0x2000; 551 /// the binary has been canonicalized via the unprebind operation 552 pub const MH_CANONICAL: u32 = 0x4000; 553 /// the final linked image contains external weak symbols 554 pub const MH_WEAK_DEFINES: u32 = 0x8000; 555 /// the final linked image uses weak symbols 556 pub const MH_BINDS_TO_WEAK: u32 = 0x10000; 557 /// When this bit is set, all stacks in the task will be given stack execution privilege. Only used in MH_EXECUTE filetypes. 558 pub const MH_ALLOW_STACK_EXECUTION: u32 = 0x20000; 559 /// When this bit is set, the binary declares it is safe for use in processes with uid zero 560 pub const MH_ROOT_SAFE: u32 = 0x40000; 561 /// When this bit is set, the binary declares it is safe for use in processes when issetugid() is true 562 pub const MH_SETUID_SAFE: u32 = 0x80000; 563 /// When this bit is set on a dylib, the static linker does not need to examine dependent dylibs to see if any are re-exported 564 pub const MH_NO_REEXPORTED_DYLIBS: u32 = 0x10_0000; 565 /// When this bit is set, the OS will load the main executable at a random address. Only used in MH_EXECUTE filetypes. 566 pub const MH_PIE: u32 = 0x20_0000; 567 /// Only for use on dylibs. When linking against a dylib that has this bit set, the static linker will automatically not create a LC_LOAD_DYLIB load command to the dylib if no symbols are being referenced from the dylib. 568 pub const MH_DEAD_STRIPPABLE_DYLIB: u32 = 0x40_0000; 569 /// Contains a section of type S_THREAD_LOCAL_VARIABLES 570 pub const MH_HAS_TLV_DESCRIPTORS: u32 = 0x80_0000; 571 /// When this bit is set, the OS will run the main executable with a non-executable heap even on platforms (e.g. i386) that don't require it. Only used in MH_EXECUTE filetypes. 572 pub const MH_NO_HEAP_EXECUTION: u32 = 0x100_0000; 573 /// The code was linked for use in an application extension. 574 pub const MH_APP_EXTENSION_SAFE: u32 = 0x0200_0000; 575 /// The external symbols listed in the nlist symbol table do not include all the symbols listed in the dyld info. 576 pub const MH_NLIST_OUTOFSYNC_WITH_DYLDINFO: u32 = 0x0400_0000; 577 /// Allow LC_MIN_VERSION_MACOS and LC_BUILD_VERSION load commands with 578 /// the platforms macOS, iOSMac, iOSSimulator, tvOSSimulator and watchOSSimulator. 579 pub const MH_SIM_SUPPORT: u32 = 0x0800_0000; 580 /// Only for use on dylibs. When this bit is set, the dylib is part of the dyld 581 /// shared cache, rather than loose in the filesystem. 582 pub const MH_DYLIB_IN_CACHE: u32 = 0x8000_0000; 583 584 /// Common fields at the start of every load command. 585 /// 586 /// The load commands directly follow the mach_header. The total size of all 587 /// of the commands is given by the sizeofcmds field in the mach_header. All 588 /// load commands must have as their first two fields `cmd` and `cmdsize`. The `cmd` 589 /// field is filled in with a constant for that command type. Each command type 590 /// has a structure specifically for it. The `cmdsize` field is the size in bytes 591 /// of the particular load command structure plus anything that follows it that 592 /// is a part of the load command (i.e. section structures, strings, etc.). To 593 /// advance to the next load command the `cmdsize` can be added to the offset or 594 /// pointer of the current load command. The `cmdsize` for 32-bit architectures 595 /// MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple 596 /// of 8 bytes (these are forever the maximum alignment of any load commands). 597 /// The padded bytes must be zero. All tables in the object file must also 598 /// follow these rules so the file can be memory mapped. Otherwise the pointers 599 /// to these tables will not work well or at all on some machines. With all 600 /// padding zeroed like objects will compare byte for byte. 601 #[derive(Debug, Clone, Copy)] 602 #[repr(C)] 603 pub struct LoadCommand<E: Endian> { 604 /// Type of load command. 605 /// 606 /// One of the `LC_*` constants. 607 pub cmd: U32<E>, 608 /// Total size of command in bytes. 609 pub cmdsize: U32<E>, 610 } 611 612 /* 613 * After MacOS X 10.1 when a new load command is added that is required to be 614 * understood by the dynamic linker for the image to execute properly the 615 * LC_REQ_DYLD bit will be or'ed into the load command constant. If the dynamic 616 * linker sees such a load command it it does not understand will issue a 617 * "unknown load command required for execution" error and refuse to use the 618 * image. Other load commands without this bit that are not understood will 619 * simply be ignored. 620 */ 621 pub const LC_REQ_DYLD: u32 = 0x8000_0000; 622 623 /* Constants for the cmd field of all load commands, the type */ 624 /// segment of this file to be mapped 625 pub const LC_SEGMENT: u32 = 0x1; 626 /// link-edit stab symbol table info 627 pub const LC_SYMTAB: u32 = 0x2; 628 /// link-edit gdb symbol table info (obsolete) 629 pub const LC_SYMSEG: u32 = 0x3; 630 /// thread 631 pub const LC_THREAD: u32 = 0x4; 632 /// unix thread (includes a stack) 633 pub const LC_UNIXTHREAD: u32 = 0x5; 634 /// load a specified fixed VM shared library 635 pub const LC_LOADFVMLIB: u32 = 0x6; 636 /// fixed VM shared library identification 637 pub const LC_IDFVMLIB: u32 = 0x7; 638 /// object identification info (obsolete) 639 pub const LC_IDENT: u32 = 0x8; 640 /// fixed VM file inclusion (internal use) 641 pub const LC_FVMFILE: u32 = 0x9; 642 /// prepage command (internal use) 643 pub const LC_PREPAGE: u32 = 0xa; 644 /// dynamic link-edit symbol table info 645 pub const LC_DYSYMTAB: u32 = 0xb; 646 /// load a dynamically linked shared library 647 pub const LC_LOAD_DYLIB: u32 = 0xc; 648 /// dynamically linked shared lib ident 649 pub const LC_ID_DYLIB: u32 = 0xd; 650 /// load a dynamic linker 651 pub const LC_LOAD_DYLINKER: u32 = 0xe; 652 /// dynamic linker identification 653 pub const LC_ID_DYLINKER: u32 = 0xf; 654 /// modules prebound for a dynamically linked shared library 655 pub const LC_PREBOUND_DYLIB: u32 = 0x10; 656 /// image routines 657 pub const LC_ROUTINES: u32 = 0x11; 658 /// sub framework 659 pub const LC_SUB_FRAMEWORK: u32 = 0x12; 660 /// sub umbrella 661 pub const LC_SUB_UMBRELLA: u32 = 0x13; 662 /// sub client 663 pub const LC_SUB_CLIENT: u32 = 0x14; 664 /// sub library 665 pub const LC_SUB_LIBRARY: u32 = 0x15; 666 /// two-level namespace lookup hints 667 pub const LC_TWOLEVEL_HINTS: u32 = 0x16; 668 /// prebind checksum 669 pub const LC_PREBIND_CKSUM: u32 = 0x17; 670 /// load a dynamically linked shared library that is allowed to be missing 671 /// (all symbols are weak imported). 672 pub const LC_LOAD_WEAK_DYLIB: u32 = 0x18 | LC_REQ_DYLD; 673 /// 64-bit segment of this file to be mapped 674 pub const LC_SEGMENT_64: u32 = 0x19; 675 /// 64-bit image routines 676 pub const LC_ROUTINES_64: u32 = 0x1a; 677 /// the uuid 678 pub const LC_UUID: u32 = 0x1b; 679 /// runpath additions 680 pub const LC_RPATH: u32 = 0x1c | LC_REQ_DYLD; 681 /// local of code signature 682 pub const LC_CODE_SIGNATURE: u32 = 0x1d; 683 /// local of info to split segments 684 pub const LC_SEGMENT_SPLIT_INFO: u32 = 0x1e; 685 /// load and re-export dylib 686 pub const LC_REEXPORT_DYLIB: u32 = 0x1f | LC_REQ_DYLD; 687 /// delay load of dylib until first use 688 pub const LC_LAZY_LOAD_DYLIB: u32 = 0x20; 689 /// encrypted segment information 690 pub const LC_ENCRYPTION_INFO: u32 = 0x21; 691 /// compressed dyld information 692 pub const LC_DYLD_INFO: u32 = 0x22; 693 /// compressed dyld information only 694 pub const LC_DYLD_INFO_ONLY: u32 = 0x22 | LC_REQ_DYLD; 695 /// load upward dylib 696 pub const LC_LOAD_UPWARD_DYLIB: u32 = 0x23 | LC_REQ_DYLD; 697 /// build for MacOSX min OS version 698 pub const LC_VERSION_MIN_MACOSX: u32 = 0x24; 699 /// build for iPhoneOS min OS version 700 pub const LC_VERSION_MIN_IPHONEOS: u32 = 0x25; 701 /// compressed table of function start addresses 702 pub const LC_FUNCTION_STARTS: u32 = 0x26; 703 /// string for dyld to treat like environment variable 704 pub const LC_DYLD_ENVIRONMENT: u32 = 0x27; 705 /// replacement for LC_UNIXTHREAD 706 pub const LC_MAIN: u32 = 0x28 | LC_REQ_DYLD; 707 /// table of non-instructions in __text 708 pub const LC_DATA_IN_CODE: u32 = 0x29; 709 /// source version used to build binary 710 pub const LC_SOURCE_VERSION: u32 = 0x2A; 711 /// Code signing DRs copied from linked dylibs 712 pub const LC_DYLIB_CODE_SIGN_DRS: u32 = 0x2B; 713 /// 64-bit encrypted segment information 714 pub const LC_ENCRYPTION_INFO_64: u32 = 0x2C; 715 /// linker options in MH_OBJECT files 716 pub const LC_LINKER_OPTION: u32 = 0x2D; 717 /// optimization hints in MH_OBJECT files 718 pub const LC_LINKER_OPTIMIZATION_HINT: u32 = 0x2E; 719 /// build for AppleTV min OS version 720 pub const LC_VERSION_MIN_TVOS: u32 = 0x2F; 721 /// build for Watch min OS version 722 pub const LC_VERSION_MIN_WATCHOS: u32 = 0x30; 723 /// arbitrary data included within a Mach-O file 724 pub const LC_NOTE: u32 = 0x31; 725 /// build for platform min OS version 726 pub const LC_BUILD_VERSION: u32 = 0x32; 727 /// used with `LinkeditDataCommand`, payload is trie 728 pub const LC_DYLD_EXPORTS_TRIE: u32 = 0x33 | LC_REQ_DYLD; 729 /// used with `LinkeditDataCommand` 730 pub const LC_DYLD_CHAINED_FIXUPS: u32 = 0x34 | LC_REQ_DYLD; 731 /// used with `FilesetEntryCommand` 732 pub const LC_FILESET_ENTRY: u32 = 0x35 | LC_REQ_DYLD; 733 734 /// A variable length string in a load command. 735 /// 736 /// The strings are stored just after the load command structure and 737 /// the offset is from the start of the load command structure. The size 738 /// of the string is reflected in the `cmdsize` field of the load command. 739 /// Once again any padded bytes to bring the `cmdsize` field to a multiple 740 /// of 4 bytes must be zero. 741 #[derive(Debug, Clone, Copy)] 742 #[repr(C)] 743 pub struct LcStr<E: Endian> { 744 /// offset to the string 745 pub offset: U32<E>, 746 } 747 748 /// 32-bit segment load command. 749 /// 750 /// The segment load command indicates that a part of this file is to be 751 /// mapped into the task's address space. The size of this segment in memory, 752 /// vmsize, maybe equal to or larger than the amount to map from this file, 753 /// filesize. The file is mapped starting at fileoff to the beginning of 754 /// the segment in memory, vmaddr. The rest of the memory of the segment, 755 /// if any, is allocated zero fill on demand. The segment's maximum virtual 756 /// memory protection and initial virtual memory protection are specified 757 /// by the maxprot and initprot fields. If the segment has sections then the 758 /// `Section32` structures directly follow the segment command and their size is 759 /// reflected in `cmdsize`. 760 #[derive(Debug, Clone, Copy)] 761 #[repr(C)] 762 pub struct SegmentCommand32<E: Endian> { 763 /// LC_SEGMENT 764 pub cmd: U32<E>, 765 /// includes sizeof section structs 766 pub cmdsize: U32<E>, 767 /// segment name 768 pub segname: [u8; 16], 769 /// memory address of this segment 770 pub vmaddr: U32<E>, 771 /// memory size of this segment 772 pub vmsize: U32<E>, 773 /// file offset of this segment 774 pub fileoff: U32<E>, 775 /// amount to map from the file 776 pub filesize: U32<E>, 777 /// maximum VM protection 778 pub maxprot: U32<E>, 779 /// initial VM protection 780 pub initprot: U32<E>, 781 /// number of sections in segment 782 pub nsects: U32<E>, 783 /// flags 784 pub flags: U32<E>, 785 } 786 787 /// 64-bit segment load command. 788 /// 789 /// The 64-bit segment load command indicates that a part of this file is to be 790 /// mapped into a 64-bit task's address space. If the 64-bit segment has 791 /// sections then `Section64` structures directly follow the 64-bit segment 792 /// command and their size is reflected in `cmdsize`. 793 #[derive(Debug, Clone, Copy)] 794 #[repr(C)] 795 pub struct SegmentCommand64<E: Endian> { 796 /// LC_SEGMENT_64 797 pub cmd: U32<E>, 798 /// includes sizeof section_64 structs 799 pub cmdsize: U32<E>, 800 /// segment name 801 pub segname: [u8; 16], 802 /// memory address of this segment 803 pub vmaddr: U64<E>, 804 /// memory size of this segment 805 pub vmsize: U64<E>, 806 /// file offset of this segment 807 pub fileoff: U64<E>, 808 /// amount to map from the file 809 pub filesize: U64<E>, 810 /// maximum VM protection 811 pub maxprot: U32<E>, 812 /// initial VM protection 813 pub initprot: U32<E>, 814 /// number of sections in segment 815 pub nsects: U32<E>, 816 /// flags 817 pub flags: U32<E>, 818 } 819 820 // Values for `SegmentCommand*::flags`. 821 /// the file contents for this segment is for the high part of the VM space, the low part is zero filled (for stacks in core files) 822 pub const SG_HIGHVM: u32 = 0x1; 823 /// this segment is the VM that is allocated by a fixed VM library, for overlap checking in the link editor 824 pub const SG_FVMLIB: u32 = 0x2; 825 /// this segment has nothing that was relocated in it and nothing relocated to it, that is it maybe safely replaced without relocation 826 pub const SG_NORELOC: u32 = 0x4; 827 /// This segment is protected. If the segment starts at file offset 0, the first page of the segment is not protected. All other pages of the segment are protected. 828 pub const SG_PROTECTED_VERSION_1: u32 = 0x8; 829 /// This segment is made read-only after fixups 830 pub const SG_READ_ONLY: u32 = 0x10; 831 832 /* 833 * A segment is made up of zero or more sections. Non-MH_OBJECT files have 834 * all of their segments with the proper sections in each, and padded to the 835 * specified segment alignment when produced by the link editor. The first 836 * segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header 837 * and load commands of the object file before its first section. The zero 838 * fill sections are always last in their segment (in all formats). This 839 * allows the zeroed segment padding to be mapped into memory where zero fill 840 * sections might be. The gigabyte zero fill sections, those with the section 841 * type S_GB_ZEROFILL, can only be in a segment with sections of this type. 842 * These segments are then placed after all other segments. 843 * 844 * The MH_OBJECT format has all of its sections in one segment for 845 * compactness. There is no padding to a specified segment boundary and the 846 * mach_header and load commands are not part of the segment. 847 * 848 * Sections with the same section name, sectname, going into the same segment, 849 * segname, are combined by the link editor. The resulting section is aligned 850 * to the maximum alignment of the combined sections and is the new section's 851 * alignment. The combined sections are aligned to their original alignment in 852 * the combined section. Any padded bytes to get the specified alignment are 853 * zeroed. 854 * 855 * The format of the relocation entries referenced by the reloff and nreloc 856 * fields of the section structure for mach object files is described in the 857 * header file <reloc.h>. 858 */ 859 /// 32-bit section. 860 #[derive(Debug, Clone, Copy)] 861 #[repr(C)] 862 pub struct Section32<E: Endian> { 863 /// name of this section 864 pub sectname: [u8; 16], 865 /// segment this section goes in 866 pub segname: [u8; 16], 867 /// memory address of this section 868 pub addr: U32<E>, 869 /// size in bytes of this section 870 pub size: U32<E>, 871 /// file offset of this section 872 pub offset: U32<E>, 873 /// section alignment (power of 2) 874 pub align: U32<E>, 875 /// file offset of relocation entries 876 pub reloff: U32<E>, 877 /// number of relocation entries 878 pub nreloc: U32<E>, 879 /// flags (section type and attributes) 880 pub flags: U32<E>, 881 /// reserved (for offset or index) 882 pub reserved1: U32<E>, 883 /// reserved (for count or sizeof) 884 pub reserved2: U32<E>, 885 } 886 887 /// 64-bit section. 888 #[derive(Debug, Clone, Copy)] 889 #[repr(C)] 890 pub struct Section64<E: Endian> { 891 /// name of this section 892 pub sectname: [u8; 16], 893 /// segment this section goes in 894 pub segname: [u8; 16], 895 /// memory address of this section 896 pub addr: U64<E>, 897 /// size in bytes of this section 898 pub size: U64<E>, 899 /// file offset of this section 900 pub offset: U32<E>, 901 /// section alignment (power of 2) 902 pub align: U32<E>, 903 /// file offset of relocation entries 904 pub reloff: U32<E>, 905 /// number of relocation entries 906 pub nreloc: U32<E>, 907 /// flags (section type and attributes) 908 pub flags: U32<E>, 909 /// reserved (for offset or index) 910 pub reserved1: U32<E>, 911 /// reserved (for count or sizeof) 912 pub reserved2: U32<E>, 913 /// reserved 914 pub reserved3: U32<E>, 915 } 916 917 /* 918 * The flags field of a section structure is separated into two parts a section 919 * type and section attributes. The section types are mutually exclusive (it 920 * can only have one type) but the section attributes are not (it may have more 921 * than one attribute). 922 */ 923 /// 256 section types 924 pub const SECTION_TYPE: u32 = 0x0000_00ff; 925 /// 24 section attributes 926 pub const SECTION_ATTRIBUTES: u32 = 0xffff_ff00; 927 928 /* Constants for the type of a section */ 929 /// regular section 930 pub const S_REGULAR: u32 = 0x0; 931 /// zero fill on demand section 932 pub const S_ZEROFILL: u32 = 0x1; 933 /// section with only literal C strings 934 pub const S_CSTRING_LITERALS: u32 = 0x2; 935 /// section with only 4 byte literals 936 pub const S_4BYTE_LITERALS: u32 = 0x3; 937 /// section with only 8 byte literals 938 pub const S_8BYTE_LITERALS: u32 = 0x4; 939 /// section with only pointers to literals 940 pub const S_LITERAL_POINTERS: u32 = 0x5; 941 /* 942 * For the two types of symbol pointers sections and the symbol stubs section 943 * they have indirect symbol table entries. For each of the entries in the 944 * section the indirect symbol table entries, in corresponding order in the 945 * indirect symbol table, start at the index stored in the reserved1 field 946 * of the section structure. Since the indirect symbol table entries 947 * correspond to the entries in the section the number of indirect symbol table 948 * entries is inferred from the size of the section divided by the size of the 949 * entries in the section. For symbol pointers sections the size of the entries 950 * in the section is 4 bytes and for symbol stubs sections the byte size of the 951 * stubs is stored in the reserved2 field of the section structure. 952 */ 953 /// section with only non-lazy symbol pointers 954 pub const S_NON_LAZY_SYMBOL_POINTERS: u32 = 0x6; 955 /// section with only lazy symbol pointers 956 pub const S_LAZY_SYMBOL_POINTERS: u32 = 0x7; 957 /// section with only symbol stubs, byte size of stub in the reserved2 field 958 pub const S_SYMBOL_STUBS: u32 = 0x8; 959 /// section with only function pointers for initialization 960 pub const S_MOD_INIT_FUNC_POINTERS: u32 = 0x9; 961 /// section with only function pointers for termination 962 pub const S_MOD_TERM_FUNC_POINTERS: u32 = 0xa; 963 /// section contains symbols that are to be coalesced 964 pub const S_COALESCED: u32 = 0xb; 965 /// zero fill on demand section (that can be larger than 4 gigabytes) 966 pub const S_GB_ZEROFILL: u32 = 0xc; 967 /// section with only pairs of function pointers for interposing 968 pub const S_INTERPOSING: u32 = 0xd; 969 /// section with only 16 byte literals 970 pub const S_16BYTE_LITERALS: u32 = 0xe; 971 /// section contains DTrace Object Format 972 pub const S_DTRACE_DOF: u32 = 0xf; 973 /// section with only lazy symbol pointers to lazy loaded dylibs 974 pub const S_LAZY_DYLIB_SYMBOL_POINTERS: u32 = 0x10; 975 /* 976 * Section types to support thread local variables 977 */ 978 /// template of initial values for TLVs 979 pub const S_THREAD_LOCAL_REGULAR: u32 = 0x11; 980 /// template of initial values for TLVs 981 pub const S_THREAD_LOCAL_ZEROFILL: u32 = 0x12; 982 /// TLV descriptors 983 pub const S_THREAD_LOCAL_VARIABLES: u32 = 0x13; 984 /// pointers to TLV descriptors 985 pub const S_THREAD_LOCAL_VARIABLE_POINTERS: u32 = 0x14; 986 /// functions to call to initialize TLV values 987 pub const S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: u32 = 0x15; 988 /// 32-bit offsets to initializers 989 pub const S_INIT_FUNC_OFFSETS: u32 = 0x16; 990 991 /* 992 * Constants for the section attributes part of the flags field of a section 993 * structure. 994 */ 995 /// User setable attributes 996 pub const SECTION_ATTRIBUTES_USR: u32 = 0xff00_0000; 997 /// section contains only true machine instructions 998 pub const S_ATTR_PURE_INSTRUCTIONS: u32 = 0x8000_0000; 999 /// section contains coalesced symbols that are not to be in a ranlib table of contents 1000 pub const S_ATTR_NO_TOC: u32 = 0x4000_0000; 1001 /// ok to strip static symbols in this section in files with the MH_DYLDLINK flag 1002 pub const S_ATTR_STRIP_STATIC_SYMS: u32 = 0x2000_0000; 1003 /// no dead stripping 1004 pub const S_ATTR_NO_DEAD_STRIP: u32 = 0x1000_0000; 1005 /// blocks are live if they reference live blocks 1006 pub const S_ATTR_LIVE_SUPPORT: u32 = 0x0800_0000; 1007 /// Used with i386 code stubs written on by dyld 1008 pub const S_ATTR_SELF_MODIFYING_CODE: u32 = 0x0400_0000; 1009 /* 1010 * If a segment contains any sections marked with S_ATTR_DEBUG then all 1011 * sections in that segment must have this attribute. No section other than 1012 * a section marked with this attribute may reference the contents of this 1013 * section. A section with this attribute may contain no symbols and must have 1014 * a section type S_REGULAR. The static linker will not copy section contents 1015 * from sections with this attribute into its output file. These sections 1016 * generally contain DWARF debugging info. 1017 */ 1018 /// a debug section 1019 pub const S_ATTR_DEBUG: u32 = 0x0200_0000; 1020 /// system setable attributes 1021 pub const SECTION_ATTRIBUTES_SYS: u32 = 0x00ff_ff00; 1022 /// section contains some machine instructions 1023 pub const S_ATTR_SOME_INSTRUCTIONS: u32 = 0x0000_0400; 1024 /// section has external relocation entries 1025 pub const S_ATTR_EXT_RELOC: u32 = 0x0000_0200; 1026 /// section has local relocation entries 1027 pub const S_ATTR_LOC_RELOC: u32 = 0x0000_0100; 1028 1029 /* 1030 * The names of segments and sections in them are mostly meaningless to the 1031 * link-editor. But there are few things to support traditional UNIX 1032 * executables that require the link-editor and assembler to use some names 1033 * agreed upon by convention. 1034 * 1035 * The initial protection of the "__TEXT" segment has write protection turned 1036 * off (not writeable). 1037 * 1038 * The link-editor will allocate common symbols at the end of the "__common" 1039 * section in the "__DATA" segment. It will create the section and segment 1040 * if needed. 1041 */ 1042 1043 /* The currently known segment names and the section names in those segments */ 1044 1045 /// the pagezero segment which has no protections and catches NULL references for MH_EXECUTE files 1046 pub const SEG_PAGEZERO: &str = "__PAGEZERO"; 1047 1048 /// the tradition UNIX text segment 1049 pub const SEG_TEXT: &str = "__TEXT"; 1050 /// the real text part of the text section no headers, and no padding 1051 pub const SECT_TEXT: &str = "__text"; 1052 /// the fvmlib initialization section 1053 pub const SECT_FVMLIB_INIT0: &str = "__fvmlib_init0"; 1054 /// the section following the fvmlib initialization section 1055 pub const SECT_FVMLIB_INIT1: &str = "__fvmlib_init1"; 1056 1057 /// the tradition UNIX data segment 1058 pub const SEG_DATA: &str = "__DATA"; 1059 /// the real initialized data section no padding, no bss overlap 1060 pub const SECT_DATA: &str = "__data"; 1061 /// the real uninitialized data section no padding 1062 pub const SECT_BSS: &str = "__bss"; 1063 /// the section common symbols are allocated in by the link editor 1064 pub const SECT_COMMON: &str = "__common"; 1065 1066 /// objective-C runtime segment 1067 pub const SEG_OBJC: &str = "__OBJC"; 1068 /// symbol table 1069 pub const SECT_OBJC_SYMBOLS: &str = "__symbol_table"; 1070 /// module information 1071 pub const SECT_OBJC_MODULES: &str = "__module_info"; 1072 /// string table 1073 pub const SECT_OBJC_STRINGS: &str = "__selector_strs"; 1074 /// string table 1075 pub const SECT_OBJC_REFS: &str = "__selector_refs"; 1076 1077 /// the icon segment 1078 pub const SEG_ICON: &str = "__ICON"; 1079 /// the icon headers 1080 pub const SECT_ICON_HEADER: &str = "__header"; 1081 /// the icons in tiff format 1082 pub const SECT_ICON_TIFF: &str = "__tiff"; 1083 1084 /// the segment containing all structs created and maintained by the link editor. Created with -seglinkedit option to ld(1) for MH_EXECUTE and FVMLIB file types only 1085 pub const SEG_LINKEDIT: &str = "__LINKEDIT"; 1086 1087 /// the segment overlapping with linkedit containing linking information 1088 pub const SEG_LINKINFO: &str = "__LINKINFO"; 1089 1090 /// the unix stack segment 1091 pub const SEG_UNIXSTACK: &str = "__UNIXSTACK"; 1092 1093 /// the segment for the self (dyld) modifing code stubs that has read, write and execute permissions 1094 pub const SEG_IMPORT: &str = "__IMPORT"; 1095 1096 /* 1097 * Fixed virtual memory shared libraries are identified by two things. The 1098 * target pathname (the name of the library as found for execution), and the 1099 * minor version number. The address of where the headers are loaded is in 1100 * header_addr. (THIS IS OBSOLETE and no longer supported). 1101 */ 1102 #[derive(Debug, Clone, Copy)] 1103 #[repr(C)] 1104 pub struct Fvmlib<E: Endian> { 1105 /// library's target pathname 1106 pub name: LcStr<E>, 1107 /// library's minor version number 1108 pub minor_version: U32<E>, 1109 /// library's header address 1110 pub header_addr: U32<E>, 1111 } 1112 1113 /* 1114 * A fixed virtual shared library (filetype == MH_FVMLIB in the mach header) 1115 * contains a `FvmlibCommand` (cmd == LC_IDFVMLIB) to identify the library. 1116 * An object that uses a fixed virtual shared library also contains a 1117 * `FvmlibCommand` (cmd == LC_LOADFVMLIB) for each library it uses. 1118 * (THIS IS OBSOLETE and no longer supported). 1119 */ 1120 #[derive(Debug, Clone, Copy)] 1121 #[repr(C)] 1122 pub struct FvmlibCommand<E: Endian> { 1123 /// LC_IDFVMLIB or LC_LOADFVMLIB 1124 pub cmd: U32<E>, 1125 /// includes pathname string 1126 pub cmdsize: U32<E>, 1127 /// the library identification 1128 pub fvmlib: Fvmlib<E>, 1129 } 1130 1131 /* 1132 * Dynamicly linked shared libraries are identified by two things. The 1133 * pathname (the name of the library as found for execution), and the 1134 * compatibility version number. The pathname must match and the compatibility 1135 * number in the user of the library must be greater than or equal to the 1136 * library being used. The time stamp is used to record the time a library was 1137 * built and copied into user so it can be use to determined if the library used 1138 * at runtime is exactly the same as used to built the program. 1139 */ 1140 #[derive(Debug, Clone, Copy)] 1141 #[repr(C)] 1142 pub struct Dylib<E: Endian> { 1143 /// library's path name 1144 pub name: LcStr<E>, 1145 /// library's build time stamp 1146 pub timestamp: U32<E>, 1147 /// library's current version number 1148 pub current_version: U32<E>, 1149 /// library's compatibility vers number 1150 pub compatibility_version: U32<E>, 1151 } 1152 1153 /* 1154 * A dynamically linked shared library (filetype == MH_DYLIB in the mach header) 1155 * contains a `DylibCommand` (cmd == LC_ID_DYLIB) to identify the library. 1156 * An object that uses a dynamically linked shared library also contains a 1157 * `DylibCommand` (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or 1158 * LC_REEXPORT_DYLIB) for each library it uses. 1159 */ 1160 #[derive(Debug, Clone, Copy)] 1161 #[repr(C)] 1162 pub struct DylibCommand<E: Endian> { 1163 /// LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB, LC_REEXPORT_DYLIB 1164 pub cmd: U32<E>, 1165 /// includes pathname string 1166 pub cmdsize: U32<E>, 1167 /// the library identification 1168 pub dylib: Dylib<E>, 1169 } 1170 1171 /* 1172 * A dynamically linked shared library may be a subframework of an umbrella 1173 * framework. If so it will be linked with "-umbrella umbrella_name" where 1174 * Where "umbrella_name" is the name of the umbrella framework. A subframework 1175 * can only be linked against by its umbrella framework or other subframeworks 1176 * that are part of the same umbrella framework. Otherwise the static link 1177 * editor produces an error and states to link against the umbrella framework. 1178 * The name of the umbrella framework for subframeworks is recorded in the 1179 * following structure. 1180 */ 1181 #[derive(Debug, Clone, Copy)] 1182 #[repr(C)] 1183 pub struct SubFrameworkCommand<E: Endian> { 1184 /// LC_SUB_FRAMEWORK 1185 pub cmd: U32<E>, 1186 /// includes umbrella string 1187 pub cmdsize: U32<E>, 1188 /// the umbrella framework name 1189 pub umbrella: LcStr<E>, 1190 } 1191 1192 /* 1193 * For dynamically linked shared libraries that are subframework of an umbrella 1194 * framework they can allow clients other than the umbrella framework or other 1195 * subframeworks in the same umbrella framework. To do this the subframework 1196 * is built with "-allowable_client client_name" and an LC_SUB_CLIENT load 1197 * command is created for each -allowable_client flag. The client_name is 1198 * usually a framework name. It can also be a name used for bundles clients 1199 * where the bundle is built with "-client_name client_name". 1200 */ 1201 #[derive(Debug, Clone, Copy)] 1202 #[repr(C)] 1203 pub struct SubClientCommand<E: Endian> { 1204 /// LC_SUB_CLIENT 1205 pub cmd: U32<E>, 1206 /// includes client string 1207 pub cmdsize: U32<E>, 1208 /// the client name 1209 pub client: LcStr<E>, 1210 } 1211 1212 /* 1213 * A dynamically linked shared library may be a sub_umbrella of an umbrella 1214 * framework. If so it will be linked with "-sub_umbrella umbrella_name" where 1215 * Where "umbrella_name" is the name of the sub_umbrella framework. When 1216 * staticly linking when -twolevel_namespace is in effect a twolevel namespace 1217 * umbrella framework will only cause its subframeworks and those frameworks 1218 * listed as sub_umbrella frameworks to be implicited linked in. Any other 1219 * dependent dynamic libraries will not be linked it when -twolevel_namespace 1220 * is in effect. The primary library recorded by the static linker when 1221 * resolving a symbol in these libraries will be the umbrella framework. 1222 * Zero or more sub_umbrella frameworks may be use by an umbrella framework. 1223 * The name of a sub_umbrella framework is recorded in the following structure. 1224 */ 1225 #[derive(Debug, Clone, Copy)] 1226 #[repr(C)] 1227 pub struct SubUmbrellaCommand<E: Endian> { 1228 /// LC_SUB_UMBRELLA 1229 pub cmd: U32<E>, 1230 /// includes sub_umbrella string 1231 pub cmdsize: U32<E>, 1232 /// the sub_umbrella framework name 1233 pub sub_umbrella: LcStr<E>, 1234 } 1235 1236 /* 1237 * A dynamically linked shared library may be a sub_library of another shared 1238 * library. If so it will be linked with "-sub_library library_name" where 1239 * Where "library_name" is the name of the sub_library shared library. When 1240 * staticly linking when -twolevel_namespace is in effect a twolevel namespace 1241 * shared library will only cause its subframeworks and those frameworks 1242 * listed as sub_umbrella frameworks and libraries listed as sub_libraries to 1243 * be implicited linked in. Any other dependent dynamic libraries will not be 1244 * linked it when -twolevel_namespace is in effect. The primary library 1245 * recorded by the static linker when resolving a symbol in these libraries 1246 * will be the umbrella framework (or dynamic library). Zero or more sub_library 1247 * shared libraries may be use by an umbrella framework or (or dynamic library). 1248 * The name of a sub_library framework is recorded in the following structure. 1249 * For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc". 1250 */ 1251 #[derive(Debug, Clone, Copy)] 1252 #[repr(C)] 1253 pub struct SubLibraryCommand<E: Endian> { 1254 /// LC_SUB_LIBRARY 1255 pub cmd: U32<E>, 1256 /// includes sub_library string 1257 pub cmdsize: U32<E>, 1258 /// the sub_library name 1259 pub sub_library: LcStr<E>, 1260 } 1261 1262 /* 1263 * A program (filetype == MH_EXECUTE) that is 1264 * prebound to its dynamic libraries has one of these for each library that 1265 * the static linker used in prebinding. It contains a bit vector for the 1266 * modules in the library. The bits indicate which modules are bound (1) and 1267 * which are not (0) from the library. The bit for module 0 is the low bit 1268 * of the first byte. So the bit for the Nth module is: 1269 * (linked_modules[N/8] >> N%8) & 1 1270 */ 1271 #[derive(Debug, Clone, Copy)] 1272 #[repr(C)] 1273 pub struct PreboundDylibCommand<E: Endian> { 1274 /// LC_PREBOUND_DYLIB 1275 pub cmd: U32<E>, 1276 /// includes strings 1277 pub cmdsize: U32<E>, 1278 /// library's path name 1279 pub name: LcStr<E>, 1280 /// number of modules in library 1281 pub nmodules: U32<E>, 1282 /// bit vector of linked modules 1283 pub linked_modules: LcStr<E>, 1284 } 1285 1286 /* 1287 * A program that uses a dynamic linker contains a `DylinkerCommand` to identify 1288 * the name of the dynamic linker (LC_LOAD_DYLINKER). And a dynamic linker 1289 * contains a `DylinkerCommand` to identify the dynamic linker (LC_ID_DYLINKER). 1290 * A file can have at most one of these. 1291 * This struct is also used for the LC_DYLD_ENVIRONMENT load command and 1292 * contains string for dyld to treat like environment variable. 1293 */ 1294 #[derive(Debug, Clone, Copy)] 1295 #[repr(C)] 1296 pub struct DylinkerCommand<E: Endian> { 1297 /// LC_ID_DYLINKER, LC_LOAD_DYLINKER or LC_DYLD_ENVIRONMENT 1298 pub cmd: U32<E>, 1299 /// includes pathname string 1300 pub cmdsize: U32<E>, 1301 /// dynamic linker's path name 1302 pub name: LcStr<E>, 1303 } 1304 1305 /* 1306 * Thread commands contain machine-specific data structures suitable for 1307 * use in the thread state primitives. The machine specific data structures 1308 * follow the struct `ThreadCommand` as follows. 1309 * Each flavor of machine specific data structure is preceded by an uint32_t 1310 * constant for the flavor of that data structure, an uint32_t that is the 1311 * count of uint32_t's of the size of the state data structure and then 1312 * the state data structure follows. This triple may be repeated for many 1313 * flavors. The constants for the flavors, counts and state data structure 1314 * definitions are expected to be in the header file <machine/thread_status.h>. 1315 * These machine specific data structures sizes must be multiples of 1316 * 4 bytes. The `cmdsize` reflects the total size of the `ThreadCommand` 1317 * and all of the sizes of the constants for the flavors, counts and state 1318 * data structures. 1319 * 1320 * For executable objects that are unix processes there will be one 1321 * `ThreadCommand` (cmd == LC_UNIXTHREAD) created for it by the link-editor. 1322 * This is the same as a LC_THREAD, except that a stack is automatically 1323 * created (based on the shell's limit for the stack size). Command arguments 1324 * and environment variables are copied onto that stack. 1325 */ 1326 #[derive(Debug, Clone, Copy)] 1327 #[repr(C)] 1328 pub struct ThreadCommand<E: Endian> { 1329 /// LC_THREAD or LC_UNIXTHREAD 1330 pub cmd: U32<E>, 1331 /// total size of this command 1332 pub cmdsize: U32<E>, 1333 /* uint32_t flavor flavor of thread state */ 1334 /* uint32_t count count of uint32_t's in thread state */ 1335 /* struct XXX_thread_state state thread state for this flavor */ 1336 /* ... */ 1337 } 1338 1339 /* 1340 * The routines command contains the address of the dynamic shared library 1341 * initialization routine and an index into the module table for the module 1342 * that defines the routine. Before any modules are used from the library the 1343 * dynamic linker fully binds the module that defines the initialization routine 1344 * and then calls it. This gets called before any module initialization 1345 * routines (used for C++ static constructors) in the library. 1346 */ 1347 #[derive(Debug, Clone, Copy)] 1348 #[repr(C)] 1349 pub struct RoutinesCommand32<E: Endian> { 1350 /* for 32-bit architectures */ 1351 /// LC_ROUTINES 1352 pub cmd: U32<E>, 1353 /// total size of this command 1354 pub cmdsize: U32<E>, 1355 /// address of initialization routine 1356 pub init_address: U32<E>, 1357 /// index into the module table that the init routine is defined in 1358 pub init_module: U32<E>, 1359 pub reserved1: U32<E>, 1360 pub reserved2: U32<E>, 1361 pub reserved3: U32<E>, 1362 pub reserved4: U32<E>, 1363 pub reserved5: U32<E>, 1364 pub reserved6: U32<E>, 1365 } 1366 1367 /* 1368 * The 64-bit routines command. Same use as above. 1369 */ 1370 #[derive(Debug, Clone, Copy)] 1371 #[repr(C)] 1372 pub struct RoutinesCommand64<E: Endian> { 1373 /* for 64-bit architectures */ 1374 /// LC_ROUTINES_64 1375 pub cmd: U32<E>, 1376 /// total size of this command 1377 pub cmdsize: U32<E>, 1378 /// address of initialization routine 1379 pub init_address: U64<E>, 1380 /// index into the module table that the init routine is defined in 1381 pub init_module: U64<E>, 1382 pub reserved1: U64<E>, 1383 pub reserved2: U64<E>, 1384 pub reserved3: U64<E>, 1385 pub reserved4: U64<E>, 1386 pub reserved5: U64<E>, 1387 pub reserved6: U64<E>, 1388 } 1389 1390 /* 1391 * The `SymtabCommand` contains the offsets and sizes of the link-edit 4.3BSD 1392 * "stab" style symbol table information as described in the header files 1393 * <nlist.h> and <stab.h>. 1394 */ 1395 #[derive(Debug, Clone, Copy)] 1396 #[repr(C)] 1397 pub struct SymtabCommand<E: Endian> { 1398 /// LC_SYMTAB 1399 pub cmd: U32<E>, 1400 /// sizeof(struct SymtabCommand) 1401 pub cmdsize: U32<E>, 1402 /// symbol table offset 1403 pub symoff: U32<E>, 1404 /// number of symbol table entries 1405 pub nsyms: U32<E>, 1406 /// string table offset 1407 pub stroff: U32<E>, 1408 /// string table size in bytes 1409 pub strsize: U32<E>, 1410 } 1411 1412 /* 1413 * This is the second set of the symbolic information which is used to support 1414 * the data structures for the dynamically link editor. 1415 * 1416 * The original set of symbolic information in the `SymtabCommand` which contains 1417 * the symbol and string tables must also be present when this load command is 1418 * present. When this load command is present the symbol table is organized 1419 * into three groups of symbols: 1420 * local symbols (static and debugging symbols) - grouped by module 1421 * defined external symbols - grouped by module (sorted by name if not lib) 1422 * undefined external symbols (sorted by name if MH_BINDATLOAD is not set, 1423 * and in order the were seen by the static 1424 * linker if MH_BINDATLOAD is set) 1425 * In this load command there are offsets and counts to each of the three groups 1426 * of symbols. 1427 * 1428 * This load command contains a the offsets and sizes of the following new 1429 * symbolic information tables: 1430 * table of contents 1431 * module table 1432 * reference symbol table 1433 * indirect symbol table 1434 * The first three tables above (the table of contents, module table and 1435 * reference symbol table) are only present if the file is a dynamically linked 1436 * shared library. For executable and object modules, which are files 1437 * containing only one module, the information that would be in these three 1438 * tables is determined as follows: 1439 * table of contents - the defined external symbols are sorted by name 1440 * module table - the file contains only one module so everything in the 1441 * file is part of the module. 1442 * reference symbol table - is the defined and undefined external symbols 1443 * 1444 * For dynamically linked shared library files this load command also contains 1445 * offsets and sizes to the pool of relocation entries for all sections 1446 * separated into two groups: 1447 * external relocation entries 1448 * local relocation entries 1449 * For executable and object modules the relocation entries continue to hang 1450 * off the section structures. 1451 */ 1452 #[derive(Debug, Clone, Copy)] 1453 #[repr(C)] 1454 pub struct DysymtabCommand<E: Endian> { 1455 /// LC_DYSYMTAB 1456 pub cmd: U32<E>, 1457 /// sizeof(struct DysymtabCommand) 1458 pub cmdsize: U32<E>, 1459 1460 /* 1461 * The symbols indicated by symoff and nsyms of the LC_SYMTAB load command 1462 * are grouped into the following three groups: 1463 * local symbols (further grouped by the module they are from) 1464 * defined external symbols (further grouped by the module they are from) 1465 * undefined symbols 1466 * 1467 * The local symbols are used only for debugging. The dynamic binding 1468 * process may have to use them to indicate to the debugger the local 1469 * symbols for a module that is being bound. 1470 * 1471 * The last two groups are used by the dynamic binding process to do the 1472 * binding (indirectly through the module table and the reference symbol 1473 * table when this is a dynamically linked shared library file). 1474 */ 1475 /// index to local symbols 1476 pub ilocalsym: U32<E>, 1477 /// number of local symbols 1478 pub nlocalsym: U32<E>, 1479 1480 /// index to externally defined symbols 1481 pub iextdefsym: U32<E>, 1482 /// number of externally defined symbols 1483 pub nextdefsym: U32<E>, 1484 1485 /// index to undefined symbols 1486 pub iundefsym: U32<E>, 1487 /// number of undefined symbols 1488 pub nundefsym: U32<E>, 1489 1490 /* 1491 * For the for the dynamic binding process to find which module a symbol 1492 * is defined in the table of contents is used (analogous to the ranlib 1493 * structure in an archive) which maps defined external symbols to modules 1494 * they are defined in. This exists only in a dynamically linked shared 1495 * library file. For executable and object modules the defined external 1496 * symbols are sorted by name and is use as the table of contents. 1497 */ 1498 /// file offset to table of contents 1499 pub tocoff: U32<E>, 1500 /// number of entries in table of contents 1501 pub ntoc: U32<E>, 1502 1503 /* 1504 * To support dynamic binding of "modules" (whole object files) the symbol 1505 * table must reflect the modules that the file was created from. This is 1506 * done by having a module table that has indexes and counts into the merged 1507 * tables for each module. The module structure that these two entries 1508 * refer to is described below. This exists only in a dynamically linked 1509 * shared library file. For executable and object modules the file only 1510 * contains one module so everything in the file belongs to the module. 1511 */ 1512 /// file offset to module table 1513 pub modtaboff: U32<E>, 1514 /// number of module table entries 1515 pub nmodtab: U32<E>, 1516 1517 /* 1518 * To support dynamic module binding the module structure for each module 1519 * indicates the external references (defined and undefined) each module 1520 * makes. For each module there is an offset and a count into the 1521 * reference symbol table for the symbols that the module references. 1522 * This exists only in a dynamically linked shared library file. For 1523 * executable and object modules the defined external symbols and the 1524 * undefined external symbols indicates the external references. 1525 */ 1526 /// offset to referenced symbol table 1527 pub extrefsymoff: U32<E>, 1528 /// number of referenced symbol table entries 1529 pub nextrefsyms: U32<E>, 1530 1531 /* 1532 * The sections that contain "symbol pointers" and "routine stubs" have 1533 * indexes and (implied counts based on the size of the section and fixed 1534 * size of the entry) into the "indirect symbol" table for each pointer 1535 * and stub. For every section of these two types the index into the 1536 * indirect symbol table is stored in the section header in the field 1537 * reserved1. An indirect symbol table entry is simply a 32bit index into 1538 * the symbol table to the symbol that the pointer or stub is referring to. 1539 * The indirect symbol table is ordered to match the entries in the section. 1540 */ 1541 /// file offset to the indirect symbol table 1542 pub indirectsymoff: U32<E>, 1543 /// number of indirect symbol table entries 1544 pub nindirectsyms: U32<E>, 1545 1546 /* 1547 * To support relocating an individual module in a library file quickly the 1548 * external relocation entries for each module in the library need to be 1549 * accessed efficiently. Since the relocation entries can't be accessed 1550 * through the section headers for a library file they are separated into 1551 * groups of local and external entries further grouped by module. In this 1552 * case the presents of this load command who's extreloff, nextrel, 1553 * locreloff and nlocrel fields are non-zero indicates that the relocation 1554 * entries of non-merged sections are not referenced through the section 1555 * structures (and the reloff and nreloc fields in the section headers are 1556 * set to zero). 1557 * 1558 * Since the relocation entries are not accessed through the section headers 1559 * this requires the r_address field to be something other than a section 1560 * offset to identify the item to be relocated. In this case r_address is 1561 * set to the offset from the vmaddr of the first LC_SEGMENT command. 1562 * For MH_SPLIT_SEGS images r_address is set to the the offset from the 1563 * vmaddr of the first read-write LC_SEGMENT command. 1564 * 1565 * The relocation entries are grouped by module and the module table 1566 * entries have indexes and counts into them for the group of external 1567 * relocation entries for that the module. 1568 * 1569 * For sections that are merged across modules there must not be any 1570 * remaining external relocation entries for them (for merged sections 1571 * remaining relocation entries must be local). 1572 */ 1573 /// offset to external relocation entries 1574 pub extreloff: U32<E>, 1575 /// number of external relocation entries 1576 pub nextrel: U32<E>, 1577 1578 /* 1579 * All the local relocation entries are grouped together (they are not 1580 * grouped by their module since they are only used if the object is moved 1581 * from it staticly link edited address). 1582 */ 1583 /// offset to local relocation entries 1584 pub locreloff: U32<E>, 1585 /// number of local relocation entries 1586 pub nlocrel: U32<E>, 1587 } 1588 1589 /* 1590 * An indirect symbol table entry is simply a 32bit index into the symbol table 1591 * to the symbol that the pointer or stub is refering to. Unless it is for a 1592 * non-lazy symbol pointer section for a defined symbol which strip(1) as 1593 * removed. In which case it has the value INDIRECT_SYMBOL_LOCAL. If the 1594 * symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that. 1595 */ 1596 pub const INDIRECT_SYMBOL_LOCAL: u32 = 0x8000_0000; 1597 pub const INDIRECT_SYMBOL_ABS: u32 = 0x4000_0000; 1598 1599 /* a table of contents entry */ 1600 #[derive(Debug, Clone, Copy)] 1601 #[repr(C)] 1602 pub struct DylibTableOfContents<E: Endian> { 1603 /// the defined external symbol (index into the symbol table) 1604 pub symbol_index: U32<E>, 1605 /// index into the module table this symbol is defined in 1606 pub module_index: U32<E>, 1607 } 1608 1609 /* a module table entry */ 1610 #[derive(Debug, Clone, Copy)] 1611 #[repr(C)] 1612 pub struct DylibModule32<E: Endian> { 1613 /// the module name (index into string table) 1614 pub module_name: U32<E>, 1615 1616 /// index into externally defined symbols 1617 pub iextdefsym: U32<E>, 1618 /// number of externally defined symbols 1619 pub nextdefsym: U32<E>, 1620 /// index into reference symbol table 1621 pub irefsym: U32<E>, 1622 /// number of reference symbol table entries 1623 pub nrefsym: U32<E>, 1624 /// index into symbols for local symbols 1625 pub ilocalsym: U32<E>, 1626 /// number of local symbols 1627 pub nlocalsym: U32<E>, 1628 1629 /// index into external relocation entries 1630 pub iextrel: U32<E>, 1631 /// number of external relocation entries 1632 pub nextrel: U32<E>, 1633 1634 /// low 16 bits are the index into the init section, high 16 bits are the index into the term section 1635 pub iinit_iterm: U32<E>, 1636 /// low 16 bits are the number of init section entries, high 16 bits are the number of term section entries 1637 pub ninit_nterm: U32<E>, 1638 1639 /// for this module address of the start of the (__OBJC,__module_info) section 1640 pub objc_module_info_addr: U32<E>, 1641 /// for this module size of the (__OBJC,__module_info) section 1642 pub objc_module_info_size: U32<E>, 1643 } 1644 1645 /* a 64-bit module table entry */ 1646 #[derive(Debug, Clone, Copy)] 1647 #[repr(C)] 1648 pub struct DylibModule64<E: Endian> { 1649 /// the module name (index into string table) 1650 pub module_name: U32<E>, 1651 1652 /// index into externally defined symbols 1653 pub iextdefsym: U32<E>, 1654 /// number of externally defined symbols 1655 pub nextdefsym: U32<E>, 1656 /// index into reference symbol table 1657 pub irefsym: U32<E>, 1658 /// number of reference symbol table entries 1659 pub nrefsym: U32<E>, 1660 /// index into symbols for local symbols 1661 pub ilocalsym: U32<E>, 1662 /// number of local symbols 1663 pub nlocalsym: U32<E>, 1664 1665 /// index into external relocation entries 1666 pub iextrel: U32<E>, 1667 /// number of external relocation entries 1668 pub nextrel: U32<E>, 1669 1670 /// low 16 bits are the index into the init section, high 16 bits are the index into the term section 1671 pub iinit_iterm: U32<E>, 1672 /// low 16 bits are the number of init section entries, high 16 bits are the number of term section entries 1673 pub ninit_nterm: U32<E>, 1674 1675 /// for this module size of the (__OBJC,__module_info) section 1676 pub objc_module_info_size: U32<E>, 1677 /// for this module address of the start of the (__OBJC,__module_info) section 1678 pub objc_module_info_addr: U64<E>, 1679 } 1680 1681 /* 1682 * The entries in the reference symbol table are used when loading the module 1683 * (both by the static and dynamic link editors) and if the module is unloaded 1684 * or replaced. Therefore all external symbols (defined and undefined) are 1685 * listed in the module's reference table. The flags describe the type of 1686 * reference that is being made. The constants for the flags are defined in 1687 * <mach-o/nlist.h> as they are also used for symbol table entries. 1688 */ 1689 #[derive(Debug, Clone, Copy)] 1690 #[repr(C)] 1691 pub struct DylibReference<E: Endian> { 1692 /* TODO: 1693 uint32_t isym:24, /* index into the symbol table */ 1694 flags:8; /* flags to indicate the type of reference */ 1695 */ 1696 pub bitfield: U32<E>, 1697 } 1698 1699 /* 1700 * The TwolevelHintsCommand contains the offset and number of hints in the 1701 * two-level namespace lookup hints table. 1702 */ 1703 #[derive(Debug, Clone, Copy)] 1704 #[repr(C)] 1705 pub struct TwolevelHintsCommand<E: Endian> { 1706 /// LC_TWOLEVEL_HINTS 1707 pub cmd: U32<E>, 1708 /// sizeof(struct TwolevelHintsCommand) 1709 pub cmdsize: U32<E>, 1710 /// offset to the hint table 1711 pub offset: U32<E>, 1712 /// number of hints in the hint table 1713 pub nhints: U32<E>, 1714 } 1715 1716 /* 1717 * The entries in the two-level namespace lookup hints table are TwolevelHint 1718 * structs. These provide hints to the dynamic link editor where to start 1719 * looking for an undefined symbol in a two-level namespace image. The 1720 * isub_image field is an index into the sub-images (sub-frameworks and 1721 * sub-umbrellas list) that made up the two-level image that the undefined 1722 * symbol was found in when it was built by the static link editor. If 1723 * isub-image is 0 the the symbol is expected to be defined in library and not 1724 * in the sub-images. If isub-image is non-zero it is an index into the array 1725 * of sub-images for the umbrella with the first index in the sub-images being 1726 * 1. The array of sub-images is the ordered list of sub-images of the umbrella 1727 * that would be searched for a symbol that has the umbrella recorded as its 1728 * primary library. The table of contents index is an index into the 1729 * library's table of contents. This is used as the starting point of the 1730 * binary search or a directed linear search. 1731 */ 1732 #[derive(Debug, Clone, Copy)] 1733 #[repr(C)] 1734 pub struct TwolevelHint<E: Endian> { 1735 /* TODO: 1736 uint32_t 1737 isub_image:8, /* index into the sub images */ 1738 itoc:24; /* index into the table of contents */ 1739 */ 1740 pub bitfield: U32<E>, 1741 } 1742 1743 /* 1744 * The PrebindCksumCommand contains the value of the original check sum for 1745 * prebound files or zero. When a prebound file is first created or modified 1746 * for other than updating its prebinding information the value of the check sum 1747 * is set to zero. When the file has it prebinding re-done and if the value of 1748 * the check sum is zero the original check sum is calculated and stored in 1749 * cksum field of this load command in the output file. If when the prebinding 1750 * is re-done and the cksum field is non-zero it is left unchanged from the 1751 * input file. 1752 */ 1753 #[derive(Debug, Clone, Copy)] 1754 #[repr(C)] 1755 pub struct PrebindCksumCommand<E: Endian> { 1756 /// LC_PREBIND_CKSUM 1757 pub cmd: U32<E>, 1758 /// sizeof(struct PrebindCksumCommand) 1759 pub cmdsize: U32<E>, 1760 /// the check sum or zero 1761 pub cksum: U32<E>, 1762 } 1763 1764 /* 1765 * The uuid load command contains a single 128-bit unique random number that 1766 * identifies an object produced by the static link editor. 1767 */ 1768 #[derive(Debug, Clone, Copy)] 1769 #[repr(C)] 1770 pub struct UuidCommand<E: Endian> { 1771 /// LC_UUID 1772 pub cmd: U32<E>, 1773 /// sizeof(struct UuidCommand) 1774 pub cmdsize: U32<E>, 1775 /// the 128-bit uuid 1776 pub uuid: [u8; 16], 1777 } 1778 1779 /* 1780 * The RpathCommand contains a path which at runtime should be added to 1781 * the current run path used to find @rpath prefixed dylibs. 1782 */ 1783 #[derive(Debug, Clone, Copy)] 1784 #[repr(C)] 1785 pub struct RpathCommand<E: Endian> { 1786 /// LC_RPATH 1787 pub cmd: U32<E>, 1788 /// includes string 1789 pub cmdsize: U32<E>, 1790 /// path to add to run path 1791 pub path: LcStr<E>, 1792 } 1793 1794 /* 1795 * The LinkeditDataCommand contains the offsets and sizes of a blob 1796 * of data in the __LINKEDIT segment. 1797 */ 1798 #[derive(Debug, Clone, Copy)] 1799 #[repr(C)] 1800 pub struct LinkeditDataCommand<E: Endian> { 1801 /// `LC_CODE_SIGNATURE`, `LC_SEGMENT_SPLIT_INFO`, `LC_FUNCTION_STARTS`, 1802 /// `LC_DATA_IN_CODE`, `LC_DYLIB_CODE_SIGN_DRS`, `LC_LINKER_OPTIMIZATION_HINT`, 1803 /// `LC_DYLD_EXPORTS_TRIE`, or `LC_DYLD_CHAINED_FIXUPS`. 1804 pub cmd: U32<E>, 1805 /// sizeof(struct LinkeditDataCommand) 1806 pub cmdsize: U32<E>, 1807 /// file offset of data in __LINKEDIT segment 1808 pub dataoff: U32<E>, 1809 /// file size of data in __LINKEDIT segment 1810 pub datasize: U32<E>, 1811 } 1812 1813 #[derive(Debug, Clone, Copy)] 1814 #[repr(C)] 1815 pub struct FilesetEntryCommand<E: Endian> { 1816 // LC_FILESET_ENTRY 1817 pub cmd: U32<E>, 1818 /// includes id string 1819 pub cmdsize: U32<E>, 1820 /// memory address of the dylib 1821 pub vmaddr: U64<E>, 1822 /// file offset of the dylib 1823 pub fileoff: U64<E>, 1824 /// contained entry id 1825 pub entry_id: LcStr<E>, 1826 /// entry_id is 32-bits long, so this is the reserved padding 1827 pub reserved: U32<E>, 1828 } 1829 1830 /* 1831 * The EncryptionInfoCommand32 contains the file offset and size of an 1832 * of an encrypted segment. 1833 */ 1834 #[derive(Debug, Clone, Copy)] 1835 #[repr(C)] 1836 pub struct EncryptionInfoCommand32<E: Endian> { 1837 /// LC_ENCRYPTION_INFO 1838 pub cmd: U32<E>, 1839 /// sizeof(struct EncryptionInfoCommand32) 1840 pub cmdsize: U32<E>, 1841 /// file offset of encrypted range 1842 pub cryptoff: U32<E>, 1843 /// file size of encrypted range 1844 pub cryptsize: U32<E>, 1845 /// which enryption system, 0 means not-encrypted yet 1846 pub cryptid: U32<E>, 1847 } 1848 1849 /* 1850 * The EncryptionInfoCommand64 contains the file offset and size of an 1851 * of an encrypted segment (for use in x86_64 targets). 1852 */ 1853 #[derive(Debug, Clone, Copy)] 1854 #[repr(C)] 1855 pub struct EncryptionInfoCommand64<E: Endian> { 1856 /// LC_ENCRYPTION_INFO_64 1857 pub cmd: U32<E>, 1858 /// sizeof(struct EncryptionInfoCommand64) 1859 pub cmdsize: U32<E>, 1860 /// file offset of encrypted range 1861 pub cryptoff: U32<E>, 1862 /// file size of encrypted range 1863 pub cryptsize: U32<E>, 1864 /// which enryption system, 0 means not-encrypted yet 1865 pub cryptid: U32<E>, 1866 /// padding to make this struct's size a multiple of 8 bytes 1867 pub pad: U32<E>, 1868 } 1869 1870 /* 1871 * The VersionMinCommand contains the min OS version on which this 1872 * binary was built to run. 1873 */ 1874 #[derive(Debug, Clone, Copy)] 1875 #[repr(C)] 1876 pub struct VersionMinCommand<E: Endian> { 1877 /// LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS or LC_VERSION_MIN_WATCHOS or LC_VERSION_MIN_TVOS 1878 pub cmd: U32<E>, 1879 /// sizeof(struct VersionMinCommand) 1880 pub cmdsize: U32<E>, 1881 /// X.Y.Z is encoded in nibbles xxxx.yy.zz 1882 pub version: U32<E>, 1883 /// X.Y.Z is encoded in nibbles xxxx.yy.zz 1884 pub sdk: U32<E>, 1885 } 1886 1887 /* 1888 * The BuildVersionCommand contains the min OS version on which this 1889 * binary was built to run for its platform. The list of known platforms and 1890 * tool values following it. 1891 */ 1892 #[derive(Debug, Clone, Copy)] 1893 #[repr(C)] 1894 pub struct BuildVersionCommand<E: Endian> { 1895 /// LC_BUILD_VERSION 1896 pub cmd: U32<E>, 1897 /// sizeof(struct BuildVersionCommand) plus ntools * sizeof(struct BuildToolVersion) 1898 pub cmdsize: U32<E>, 1899 /// platform 1900 pub platform: U32<E>, 1901 /// X.Y.Z is encoded in nibbles xxxx.yy.zz 1902 pub minos: U32<E>, 1903 /// X.Y.Z is encoded in nibbles xxxx.yy.zz 1904 pub sdk: U32<E>, 1905 /// number of tool entries following this 1906 pub ntools: U32<E>, 1907 } 1908 1909 #[derive(Debug, Clone, Copy)] 1910 #[repr(C)] 1911 pub struct BuildToolVersion<E: Endian> { 1912 /// enum for the tool 1913 pub tool: U32<E>, 1914 /// version number of the tool 1915 pub version: U32<E>, 1916 } 1917 1918 /* Known values for the platform field above. */ 1919 pub const PLATFORM_MACOS: u32 = 1; 1920 pub const PLATFORM_IOS: u32 = 2; 1921 pub const PLATFORM_TVOS: u32 = 3; 1922 pub const PLATFORM_WATCHOS: u32 = 4; 1923 pub const PLATFORM_BRIDGEOS: u32 = 5; 1924 pub const PLATFORM_MACCATALYST: u32 = 6; 1925 pub const PLATFORM_IOSSIMULATOR: u32 = 7; 1926 pub const PLATFORM_TVOSSIMULATOR: u32 = 8; 1927 pub const PLATFORM_WATCHOSSIMULATOR: u32 = 9; 1928 pub const PLATFORM_DRIVERKIT: u32 = 10; 1929 1930 /* Known values for the tool field above. */ 1931 pub const TOOL_CLANG: u32 = 1; 1932 pub const TOOL_SWIFT: u32 = 2; 1933 pub const TOOL_LD: u32 = 3; 1934 1935 /* 1936 * The DyldInfoCommand contains the file offsets and sizes of 1937 * the new compressed form of the information dyld needs to 1938 * load the image. This information is used by dyld on Mac OS X 1939 * 10.6 and later. All information pointed to by this command 1940 * is encoded using byte streams, so no endian swapping is needed 1941 * to interpret it. 1942 */ 1943 #[derive(Debug, Clone, Copy)] 1944 #[repr(C)] 1945 pub struct DyldInfoCommand<E: Endian> { 1946 /// LC_DYLD_INFO or LC_DYLD_INFO_ONLY 1947 pub cmd: U32<E>, 1948 /// sizeof(struct DyldInfoCommand) 1949 pub cmdsize: U32<E>, 1950 1951 /* 1952 * Dyld rebases an image whenever dyld loads it at an address different 1953 * from its preferred address. The rebase information is a stream 1954 * of byte sized opcodes whose symbolic names start with REBASE_OPCODE_. 1955 * Conceptually the rebase information is a table of tuples: 1956 * <seg-index, seg-offset, type> 1957 * The opcodes are a compressed way to encode the table by only 1958 * encoding when a column changes. In addition simple patterns 1959 * like "every n'th offset for m times" can be encoded in a few 1960 * bytes. 1961 */ 1962 /// file offset to rebase info 1963 pub rebase_off: U32<E>, 1964 /// size of rebase info 1965 pub rebase_size: U32<E>, 1966 1967 /* 1968 * Dyld binds an image during the loading process, if the image 1969 * requires any pointers to be initialized to symbols in other images. 1970 * The bind information is a stream of byte sized 1971 * opcodes whose symbolic names start with BIND_OPCODE_. 1972 * Conceptually the bind information is a table of tuples: 1973 * <seg-index, seg-offset, type, symbol-library-ordinal, symbol-name, addend> 1974 * The opcodes are a compressed way to encode the table by only 1975 * encoding when a column changes. In addition simple patterns 1976 * like for runs of pointers initialzed to the same value can be 1977 * encoded in a few bytes. 1978 */ 1979 /// file offset to binding info 1980 pub bind_off: U32<E>, 1981 /// size of binding info 1982 pub bind_size: U32<E>, 1983 1984 /* 1985 * Some C++ programs require dyld to unique symbols so that all 1986 * images in the process use the same copy of some code/data. 1987 * This step is done after binding. The content of the weak_bind 1988 * info is an opcode stream like the bind_info. But it is sorted 1989 * alphabetically by symbol name. This enable dyld to walk 1990 * all images with weak binding information in order and look 1991 * for collisions. If there are no collisions, dyld does 1992 * no updating. That means that some fixups are also encoded 1993 * in the bind_info. For instance, all calls to "operator new" 1994 * are first bound to libstdc++.dylib using the information 1995 * in bind_info. Then if some image overrides operator new 1996 * that is detected when the weak_bind information is processed 1997 * and the call to operator new is then rebound. 1998 */ 1999 /// file offset to weak binding info 2000 pub weak_bind_off: U32<E>, 2001 /// size of weak binding info 2002 pub weak_bind_size: U32<E>, 2003 2004 /* 2005 * Some uses of external symbols do not need to be bound immediately. 2006 * Instead they can be lazily bound on first use. The lazy_bind 2007 * are contains a stream of BIND opcodes to bind all lazy symbols. 2008 * Normal use is that dyld ignores the lazy_bind section when 2009 * loading an image. Instead the static linker arranged for the 2010 * lazy pointer to initially point to a helper function which 2011 * pushes the offset into the lazy_bind area for the symbol 2012 * needing to be bound, then jumps to dyld which simply adds 2013 * the offset to lazy_bind_off to get the information on what 2014 * to bind. 2015 */ 2016 /// file offset to lazy binding info 2017 pub lazy_bind_off: U32<E>, 2018 /// size of lazy binding infs 2019 pub lazy_bind_size: U32<E>, 2020 2021 /* 2022 * The symbols exported by a dylib are encoded in a trie. This 2023 * is a compact representation that factors out common prefixes. 2024 * It also reduces LINKEDIT pages in RAM because it encodes all 2025 * information (name, address, flags) in one small, contiguous range. 2026 * The export area is a stream of nodes. The first node sequentially 2027 * is the start node for the trie. 2028 * 2029 * Nodes for a symbol start with a uleb128 that is the length of 2030 * the exported symbol information for the string so far. 2031 * If there is no exported symbol, the node starts with a zero byte. 2032 * If there is exported info, it follows the length. 2033 * 2034 * First is a uleb128 containing flags. Normally, it is followed by 2035 * a uleb128 encoded offset which is location of the content named 2036 * by the symbol from the mach_header for the image. If the flags 2037 * is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is 2038 * a uleb128 encoded library ordinal, then a zero terminated 2039 * UTF8 string. If the string is zero length, then the symbol 2040 * is re-export from the specified dylib with the same name. 2041 * If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following 2042 * the flags is two uleb128s: the stub offset and the resolver offset. 2043 * The stub is used by non-lazy pointers. The resolver is used 2044 * by lazy pointers and must be called to get the actual address to use. 2045 * 2046 * After the optional exported symbol information is a byte of 2047 * how many edges (0-255) that this node has leaving it, 2048 * followed by each edge. 2049 * Each edge is a zero terminated UTF8 of the addition chars 2050 * in the symbol, followed by a uleb128 offset for the node that 2051 * edge points to. 2052 * 2053 */ 2054 /// file offset to lazy binding info 2055 pub export_off: U32<E>, 2056 /// size of lazy binding infs 2057 pub export_size: U32<E>, 2058 } 2059 2060 /* 2061 * The following are used to encode rebasing information 2062 */ 2063 pub const REBASE_TYPE_POINTER: u8 = 1; 2064 pub const REBASE_TYPE_TEXT_ABSOLUTE32: u8 = 2; 2065 pub const REBASE_TYPE_TEXT_PCREL32: u8 = 3; 2066 2067 pub const REBASE_OPCODE_MASK: u8 = 0xF0; 2068 pub const REBASE_IMMEDIATE_MASK: u8 = 0x0F; 2069 pub const REBASE_OPCODE_DONE: u8 = 0x00; 2070 pub const REBASE_OPCODE_SET_TYPE_IMM: u8 = 0x10; 2071 pub const REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x20; 2072 pub const REBASE_OPCODE_ADD_ADDR_ULEB: u8 = 0x30; 2073 pub const REBASE_OPCODE_ADD_ADDR_IMM_SCALED: u8 = 0x40; 2074 pub const REBASE_OPCODE_DO_REBASE_IMM_TIMES: u8 = 0x50; 2075 pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES: u8 = 0x60; 2076 pub const REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: u8 = 0x70; 2077 pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: u8 = 0x80; 2078 2079 /* 2080 * The following are used to encode binding information 2081 */ 2082 pub const BIND_TYPE_POINTER: u8 = 1; 2083 pub const BIND_TYPE_TEXT_ABSOLUTE32: u8 = 2; 2084 pub const BIND_TYPE_TEXT_PCREL32: u8 = 3; 2085 2086 pub const BIND_SPECIAL_DYLIB_SELF: i8 = 0; 2087 pub const BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: i8 = -1; 2088 pub const BIND_SPECIAL_DYLIB_FLAT_LOOKUP: i8 = -2; 2089 pub const BIND_SPECIAL_DYLIB_WEAK_LOOKUP: i8 = -3; 2090 2091 pub const BIND_SYMBOL_FLAGS_WEAK_IMPORT: u8 = 0x1; 2092 pub const BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION: u8 = 0x8; 2093 2094 pub const BIND_OPCODE_MASK: u8 = 0xF0; 2095 pub const BIND_IMMEDIATE_MASK: u8 = 0x0F; 2096 pub const BIND_OPCODE_DONE: u8 = 0x00; 2097 pub const BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: u8 = 0x10; 2098 pub const BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: u8 = 0x20; 2099 pub const BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: u8 = 0x30; 2100 pub const BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: u8 = 0x40; 2101 pub const BIND_OPCODE_SET_TYPE_IMM: u8 = 0x50; 2102 pub const BIND_OPCODE_SET_ADDEND_SLEB: u8 = 0x60; 2103 pub const BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x70; 2104 pub const BIND_OPCODE_ADD_ADDR_ULEB: u8 = 0x80; 2105 pub const BIND_OPCODE_DO_BIND: u8 = 0x90; 2106 pub const BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: u8 = 0xA0; 2107 pub const BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: u8 = 0xB0; 2108 pub const BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: u8 = 0xC0; 2109 pub const BIND_OPCODE_THREADED: u8 = 0xD0; 2110 pub const BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB: u8 = 0x00; 2111 pub const BIND_SUBOPCODE_THREADED_APPLY: u8 = 0x01; 2112 2113 /* 2114 * The following are used on the flags byte of a terminal node 2115 * in the export information. 2116 */ 2117 pub const EXPORT_SYMBOL_FLAGS_KIND_MASK: u32 = 0x03; 2118 pub const EXPORT_SYMBOL_FLAGS_KIND_REGULAR: u32 = 0x00; 2119 pub const EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL: u32 = 0x01; 2120 pub const EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE: u32 = 0x02; 2121 pub const EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u32 = 0x04; 2122 pub const EXPORT_SYMBOL_FLAGS_REEXPORT: u32 = 0x08; 2123 pub const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: u32 = 0x10; 2124 2125 /* 2126 * The LinkerOptionCommand contains linker options embedded in object files. 2127 */ 2128 #[derive(Debug, Clone, Copy)] 2129 #[repr(C)] 2130 pub struct LinkerOptionCommand<E: Endian> { 2131 /// LC_LINKER_OPTION only used in MH_OBJECT filetypes 2132 pub cmd: U32<E>, 2133 pub cmdsize: U32<E>, 2134 /// number of strings 2135 pub count: U32<E>, 2136 /* concatenation of zero terminated UTF8 strings. 2137 Zero filled at end to align */ 2138 } 2139 2140 /* 2141 * The SymsegCommand contains the offset and size of the GNU style 2142 * symbol table information as described in the header file <symseg.h>. 2143 * The symbol roots of the symbol segments must also be aligned properly 2144 * in the file. So the requirement of keeping the offsets aligned to a 2145 * multiple of a 4 bytes translates to the length field of the symbol 2146 * roots also being a multiple of a long. Also the padding must again be 2147 * zeroed. (THIS IS OBSOLETE and no longer supported). 2148 */ 2149 #[derive(Debug, Clone, Copy)] 2150 #[repr(C)] 2151 pub struct SymsegCommand<E: Endian> { 2152 /// LC_SYMSEG 2153 pub cmd: U32<E>, 2154 /// sizeof(struct SymsegCommand) 2155 pub cmdsize: U32<E>, 2156 /// symbol segment offset 2157 pub offset: U32<E>, 2158 /// symbol segment size in bytes 2159 pub size: U32<E>, 2160 } 2161 2162 /* 2163 * The IdentCommand contains a free format string table following the 2164 * IdentCommand structure. The strings are null terminated and the size of 2165 * the command is padded out with zero bytes to a multiple of 4 bytes/ 2166 * (THIS IS OBSOLETE and no longer supported). 2167 */ 2168 #[derive(Debug, Clone, Copy)] 2169 #[repr(C)] 2170 pub struct IdentCommand<E: Endian> { 2171 /// LC_IDENT 2172 pub cmd: U32<E>, 2173 /// strings that follow this command 2174 pub cmdsize: U32<E>, 2175 } 2176 2177 /* 2178 * The FvmfileCommand contains a reference to a file to be loaded at the 2179 * specified virtual address. (Presently, this command is reserved for 2180 * internal use. The kernel ignores this command when loading a program into 2181 * memory). 2182 */ 2183 #[derive(Debug, Clone, Copy)] 2184 #[repr(C)] 2185 pub struct FvmfileCommand<E: Endian> { 2186 /// LC_FVMFILE 2187 pub cmd: U32<E>, 2188 /// includes pathname string 2189 pub cmdsize: U32<E>, 2190 /// files pathname 2191 pub name: LcStr<E>, 2192 /// files virtual address 2193 pub header_addr: U32<E>, 2194 } 2195 2196 /* 2197 * The EntryPointCommand is a replacement for thread_command. 2198 * It is used for main executables to specify the location (file offset) 2199 * of main(). If -stack_size was used at link time, the stacksize 2200 * field will contain the stack size need for the main thread. 2201 */ 2202 #[derive(Debug, Clone, Copy)] 2203 #[repr(C)] 2204 pub struct EntryPointCommand<E: Endian> { 2205 /// LC_MAIN only used in MH_EXECUTE filetypes 2206 pub cmd: U32<E>, 2207 /// 24 2208 pub cmdsize: U32<E>, 2209 /// file (__TEXT) offset of main() 2210 pub entryoff: U64<E>, 2211 /// if not zero, initial stack size 2212 pub stacksize: U64<E>, 2213 } 2214 2215 /* 2216 * The SourceVersionCommand is an optional load command containing 2217 * the version of the sources used to build the binary. 2218 */ 2219 #[derive(Debug, Clone, Copy)] 2220 #[repr(C)] 2221 pub struct SourceVersionCommand<E: Endian> { 2222 /// LC_SOURCE_VERSION 2223 pub cmd: U32<E>, 2224 /// 16 2225 pub cmdsize: U32<E>, 2226 /// A.B.C.D.E packed as a24.b10.c10.d10.e10 2227 pub version: U64<E>, 2228 } 2229 2230 /* 2231 * The LC_DATA_IN_CODE load commands uses a LinkeditDataCommand 2232 * to point to an array of DataInCodeEntry entries. Each entry 2233 * describes a range of data in a code section. 2234 */ 2235 #[derive(Debug, Clone, Copy)] 2236 #[repr(C)] 2237 pub struct DataInCodeEntry<E: Endian> { 2238 /// from mach_header to start of data range 2239 pub offset: U32<E>, 2240 /// number of bytes in data range 2241 pub length: U16<E>, 2242 /// a DICE_KIND_* value 2243 pub kind: U16<E>, 2244 } 2245 pub const DICE_KIND_DATA: u32 = 0x0001; 2246 pub const DICE_KIND_JUMP_TABLE8: u32 = 0x0002; 2247 pub const DICE_KIND_JUMP_TABLE16: u32 = 0x0003; 2248 pub const DICE_KIND_JUMP_TABLE32: u32 = 0x0004; 2249 pub const DICE_KIND_ABS_JUMP_TABLE32: u32 = 0x0005; 2250 2251 /* 2252 * Sections of type S_THREAD_LOCAL_VARIABLES contain an array 2253 * of TlvDescriptor structures. 2254 */ 2255 /* TODO: 2256 #[derive(Debug, Clone, Copy)] 2257 #[repr(C)] 2258 pub struct TlvDescriptor<E: Endian> 2259 { 2260 void* (*thunk)(struct TlvDescriptor*); 2261 unsigned long key; 2262 unsigned long offset; 2263 } 2264 */ 2265 2266 /* 2267 * LC_NOTE commands describe a region of arbitrary data included in a Mach-O 2268 * file. Its initial use is to record extra data in MH_CORE files. 2269 */ 2270 #[derive(Debug, Clone, Copy)] 2271 #[repr(C)] 2272 pub struct NoteCommand<E: Endian> { 2273 /// LC_NOTE 2274 pub cmd: U32<E>, 2275 /// sizeof(struct NoteCommand) 2276 pub cmdsize: U32<E>, 2277 /// owner name for this LC_NOTE 2278 pub data_owner: [u8; 16], 2279 /// file offset of this data 2280 pub offset: U64<E>, 2281 /// length of data region 2282 pub size: U64<E>, 2283 } 2284 2285 // Definitions from "/usr/include/mach-o/nlist.h". 2286 2287 #[derive(Debug, Clone, Copy)] 2288 #[repr(C)] 2289 pub struct Nlist32<E: Endian> { 2290 /// index into the string table 2291 pub n_strx: U32<E>, 2292 /// type flag, see below 2293 pub n_type: u8, 2294 /// section number or NO_SECT 2295 pub n_sect: u8, 2296 /// see <mach-o/stab.h> 2297 pub n_desc: U16<E>, 2298 /// value of this symbol (or stab offset) 2299 pub n_value: U32<E>, 2300 } 2301 2302 /* 2303 * This is the symbol table entry structure for 64-bit architectures. 2304 */ 2305 #[derive(Debug, Clone, Copy)] 2306 #[repr(C)] 2307 pub struct Nlist64<E: Endian> { 2308 /// index into the string table 2309 pub n_strx: U32<E>, 2310 /// type flag, see below 2311 pub n_type: u8, 2312 /// section number or NO_SECT 2313 pub n_sect: u8, 2314 /// see <mach-o/stab.h> 2315 pub n_desc: U16<E>, 2316 /// value of this symbol (or stab offset) 2317 // Note: 4 byte alignment has been observed in practice. 2318 pub n_value: U64Bytes<E>, 2319 } 2320 2321 /* 2322 * Symbols with a index into the string table of zero (n_un.n_strx == 0) are 2323 * defined to have a null, "", name. Therefore all string indexes to non null 2324 * names must not have a zero string index. This is bit historical information 2325 * that has never been well documented. 2326 */ 2327 2328 /* 2329 * The n_type field really contains four fields: 2330 * unsigned char N_STAB:3, 2331 * N_PEXT:1, 2332 * N_TYPE:3, 2333 * N_EXT:1; 2334 * which are used via the following masks. 2335 */ 2336 /// if any of these bits set, a symbolic debugging entry 2337 pub const N_STAB: u8 = 0xe0; 2338 /// private external symbol bit 2339 pub const N_PEXT: u8 = 0x10; 2340 /// mask for the type bits 2341 pub const N_TYPE: u8 = 0x0e; 2342 /// external symbol bit, set for external symbols 2343 pub const N_EXT: u8 = 0x01; 2344 2345 /* 2346 * Only symbolic debugging entries have some of the N_STAB bits set and if any 2347 * of these bits are set then it is a symbolic debugging entry (a stab). In 2348 * which case then the values of the n_type field (the entire field) are given 2349 * in <mach-o/stab.h> 2350 */ 2351 2352 /* 2353 * Values for N_TYPE bits of the n_type field. 2354 */ 2355 /// undefined, n_sect == NO_SECT 2356 pub const N_UNDF: u8 = 0x0; 2357 /// absolute, n_sect == NO_SECT 2358 pub const N_ABS: u8 = 0x2; 2359 /// defined in section number n_sect 2360 pub const N_SECT: u8 = 0xe; 2361 /// prebound undefined (defined in a dylib) 2362 pub const N_PBUD: u8 = 0xc; 2363 /// indirect 2364 pub const N_INDR: u8 = 0xa; 2365 2366 /* 2367 * If the type is N_INDR then the symbol is defined to be the same as another 2368 * symbol. In this case the n_value field is an index into the string table 2369 * of the other symbol's name. When the other symbol is defined then they both 2370 * take on the defined type and value. 2371 */ 2372 2373 /* 2374 * If the type is N_SECT then the n_sect field contains an ordinal of the 2375 * section the symbol is defined in. The sections are numbered from 1 and 2376 * refer to sections in order they appear in the load commands for the file 2377 * they are in. This means the same ordinal may very well refer to different 2378 * sections in different files. 2379 * 2380 * The n_value field for all symbol table entries (including N_STAB's) gets 2381 * updated by the link editor based on the value of it's n_sect field and where 2382 * the section n_sect references gets relocated. If the value of the n_sect 2383 * field is NO_SECT then it's n_value field is not changed by the link editor. 2384 */ 2385 /// symbol is not in any section 2386 pub const NO_SECT: u8 = 0; 2387 /// 1 thru 255 inclusive 2388 pub const MAX_SECT: u8 = 255; 2389 2390 /* 2391 * Common symbols are represented by undefined (N_UNDF) external (N_EXT) types 2392 * who's values (n_value) are non-zero. In which case the value of the n_value 2393 * field is the size (in bytes) of the common symbol. The n_sect field is set 2394 * to NO_SECT. The alignment of a common symbol may be set as a power of 2 2395 * between 2^1 and 2^15 as part of the n_desc field using the macros below. If 2396 * the alignment is not set (a value of zero) then natural alignment based on 2397 * the size is used. 2398 */ 2399 /* TODO: 2400 #define GET_COMM_ALIGN(n_desc) (((n_desc) >> 8) & 0x0f) 2401 #define SET_COMM_ALIGN(n_desc,align) \ 2402 (n_desc) = (((n_desc) & 0xf0ff) | (((align) & 0x0f) << 8)) 2403 */ 2404 2405 /* 2406 * To support the lazy binding of undefined symbols in the dynamic link-editor, 2407 * the undefined symbols in the symbol table (the nlist structures) are marked 2408 * with the indication if the undefined reference is a lazy reference or 2409 * non-lazy reference. If both a non-lazy reference and a lazy reference is 2410 * made to the same symbol the non-lazy reference takes precedence. A reference 2411 * is lazy only when all references to that symbol are made through a symbol 2412 * pointer in a lazy symbol pointer section. 2413 * 2414 * The implementation of marking nlist structures in the symbol table for 2415 * undefined symbols will be to use some of the bits of the n_desc field as a 2416 * reference type. The mask REFERENCE_TYPE will be applied to the n_desc field 2417 * of an nlist structure for an undefined symbol to determine the type of 2418 * undefined reference (lazy or non-lazy). 2419 * 2420 * The constants for the REFERENCE FLAGS are propagated to the reference table 2421 * in a shared library file. In that case the constant for a defined symbol, 2422 * REFERENCE_FLAG_DEFINED, is also used. 2423 */ 2424 /* Reference type bits of the n_desc field of undefined symbols */ 2425 pub const REFERENCE_TYPE: u16 = 0x7; 2426 /* types of references */ 2427 pub const REFERENCE_FLAG_UNDEFINED_NON_LAZY: u16 = 0; 2428 pub const REFERENCE_FLAG_UNDEFINED_LAZY: u16 = 1; 2429 pub const REFERENCE_FLAG_DEFINED: u16 = 2; 2430 pub const REFERENCE_FLAG_PRIVATE_DEFINED: u16 = 3; 2431 pub const REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY: u16 = 4; 2432 pub const REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY: u16 = 5; 2433 2434 /* 2435 * To simplify stripping of objects that use are used with the dynamic link 2436 * editor, the static link editor marks the symbols defined an object that are 2437 * referenced by a dynamicly bound object (dynamic shared libraries, bundles). 2438 * With this marking strip knows not to strip these symbols. 2439 */ 2440 pub const REFERENCED_DYNAMICALLY: u16 = 0x0010; 2441 2442 /* 2443 * For images created by the static link editor with the -twolevel_namespace 2444 * option in effect the flags field of the mach header is marked with 2445 * MH_TWOLEVEL. And the binding of the undefined references of the image are 2446 * determined by the static link editor. Which library an undefined symbol is 2447 * bound to is recorded by the static linker in the high 8 bits of the n_desc 2448 * field using the SET_LIBRARY_ORDINAL macro below. The ordinal recorded 2449 * references the libraries listed in the Mach-O's LC_LOAD_DYLIB, 2450 * LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_LOAD_UPWARD_DYLIB, and 2451 * LC_LAZY_LOAD_DYLIB, etc. load commands in the order they appear in the 2452 * headers. The library ordinals start from 1. 2453 * For a dynamic library that is built as a two-level namespace image the 2454 * undefined references from module defined in another use the same nlist struct 2455 * an in that case SELF_LIBRARY_ORDINAL is used as the library ordinal. For 2456 * defined symbols in all images they also must have the library ordinal set to 2457 * SELF_LIBRARY_ORDINAL. The EXECUTABLE_ORDINAL refers to the executable 2458 * image for references from plugins that refer to the executable that loads 2459 * them. 2460 * 2461 * The DYNAMIC_LOOKUP_ORDINAL is for undefined symbols in a two-level namespace 2462 * image that are looked up by the dynamic linker with flat namespace semantics. 2463 * This ordinal was added as a feature in Mac OS X 10.3 by reducing the 2464 * value of MAX_LIBRARY_ORDINAL by one. So it is legal for existing binaries 2465 * or binaries built with older tools to have 0xfe (254) dynamic libraries. In 2466 * this case the ordinal value 0xfe (254) must be treated as a library ordinal 2467 * for compatibility. 2468 */ 2469 /* TODO: 2470 #define GET_LIBRARY_ORDINAL(n_desc) (((n_desc) >> 8) & 0xff) 2471 #define SET_LIBRARY_ORDINAL(n_desc,ordinal) \ 2472 (n_desc) = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8)) 2473 */ 2474 pub const SELF_LIBRARY_ORDINAL: u8 = 0x0; 2475 pub const MAX_LIBRARY_ORDINAL: u8 = 0xfd; 2476 pub const DYNAMIC_LOOKUP_ORDINAL: u8 = 0xfe; 2477 pub const EXECUTABLE_ORDINAL: u8 = 0xff; 2478 2479 /* 2480 * The bit 0x0020 of the n_desc field is used for two non-overlapping purposes 2481 * and has two different symbolic names, N_NO_DEAD_STRIP and N_DESC_DISCARDED. 2482 */ 2483 2484 /* 2485 * The N_NO_DEAD_STRIP bit of the n_desc field only ever appears in a 2486 * relocatable .o file (MH_OBJECT filetype). And is used to indicate to the 2487 * static link editor it is never to dead strip the symbol. 2488 */ 2489 /// symbol is not to be dead stripped 2490 pub const N_NO_DEAD_STRIP: u16 = 0x0020; 2491 2492 /* 2493 * The N_DESC_DISCARDED bit of the n_desc field never appears in linked image. 2494 * But is used in very rare cases by the dynamic link editor to mark an in 2495 * memory symbol as discared and longer used for linking. 2496 */ 2497 /// symbol is discarded 2498 pub const N_DESC_DISCARDED: u16 = 0x0020; 2499 2500 /* 2501 * The N_WEAK_REF bit of the n_desc field indicates to the dynamic linker that 2502 * the undefined symbol is allowed to be missing and is to have the address of 2503 * zero when missing. 2504 */ 2505 /// symbol is weak referenced 2506 pub const N_WEAK_REF: u16 = 0x0040; 2507 2508 /* 2509 * The N_WEAK_DEF bit of the n_desc field indicates to the static and dynamic 2510 * linkers that the symbol definition is weak, allowing a non-weak symbol to 2511 * also be used which causes the weak definition to be discared. Currently this 2512 * is only supported for symbols in coalesed sections. 2513 */ 2514 /// coalesed symbol is a weak definition 2515 pub const N_WEAK_DEF: u16 = 0x0080; 2516 2517 /* 2518 * The N_REF_TO_WEAK bit of the n_desc field indicates to the dynamic linker 2519 * that the undefined symbol should be resolved using flat namespace searching. 2520 */ 2521 /// reference to a weak symbol 2522 pub const N_REF_TO_WEAK: u16 = 0x0080; 2523 2524 /* 2525 * The N_ARM_THUMB_DEF bit of the n_desc field indicates that the symbol is 2526 * a defintion of a Thumb function. 2527 */ 2528 /// symbol is a Thumb function (ARM) 2529 pub const N_ARM_THUMB_DEF: u16 = 0x0008; 2530 2531 /* 2532 * The N_SYMBOL_RESOLVER bit of the n_desc field indicates that the 2533 * that the function is actually a resolver function and should 2534 * be called to get the address of the real function to use. 2535 * This bit is only available in .o files (MH_OBJECT filetype) 2536 */ 2537 pub const N_SYMBOL_RESOLVER: u16 = 0x0100; 2538 2539 /* 2540 * The N_ALT_ENTRY bit of the n_desc field indicates that the 2541 * symbol is pinned to the previous content. 2542 */ 2543 pub const N_ALT_ENTRY: u16 = 0x0200; 2544 2545 // Definitions from "/usr/include/mach-o/stab.h". 2546 2547 /* 2548 * This file gives definitions supplementing <nlist.h> for permanent symbol 2549 * table entries of Mach-O files. Modified from the BSD definitions. The 2550 * modifications from the original definitions were changing what the values of 2551 * what was the n_other field (an unused field) which is now the n_sect field. 2552 * These modifications are required to support symbols in an arbitrary number of 2553 * sections not just the three sections (text, data and bss) in a BSD file. 2554 * The values of the defined constants have NOT been changed. 2555 * 2556 * These must have one of the N_STAB bits on. The n_value fields are subject 2557 * to relocation according to the value of their n_sect field. So for types 2558 * that refer to things in sections the n_sect field must be filled in with the 2559 * proper section ordinal. For types that are not to have their n_value field 2560 * relocatated the n_sect field must be NO_SECT. 2561 */ 2562 2563 /* 2564 * Symbolic debugger symbols. The comments give the conventional use for 2565 * 2566 * .stabs "n_name", n_type, n_sect, n_desc, n_value 2567 * 2568 * where n_type is the defined constant and not listed in the comment. Other 2569 * fields not listed are zero. n_sect is the section ordinal the entry is 2570 * refering to. 2571 */ 2572 /// global symbol: name,,NO_SECT,type,0 2573 pub const N_GSYM: u8 = 0x20; 2574 /// procedure name (f77 kludge): name,,NO_SECT,0,0 2575 pub const N_FNAME: u8 = 0x22; 2576 /// procedure: name,,n_sect,linenumber,address 2577 pub const N_FUN: u8 = 0x24; 2578 /// static symbol: name,,n_sect,type,address 2579 pub const N_STSYM: u8 = 0x26; 2580 /// .lcomm symbol: name,,n_sect,type,address 2581 pub const N_LCSYM: u8 = 0x28; 2582 /// begin nsect sym: 0,,n_sect,0,address 2583 pub const N_BNSYM: u8 = 0x2e; 2584 /// AST file path: name,,NO_SECT,0,0 2585 pub const N_AST: u8 = 0x32; 2586 /// emitted with gcc2_compiled and in gcc source 2587 pub const N_OPT: u8 = 0x3c; 2588 /// register sym: name,,NO_SECT,type,register 2589 pub const N_RSYM: u8 = 0x40; 2590 /// src line: 0,,n_sect,linenumber,address 2591 pub const N_SLINE: u8 = 0x44; 2592 /// end nsect sym: 0,,n_sect,0,address 2593 pub const N_ENSYM: u8 = 0x4e; 2594 /// structure elt: name,,NO_SECT,type,struct_offset 2595 pub const N_SSYM: u8 = 0x60; 2596 /// source file name: name,,n_sect,0,address 2597 pub const N_SO: u8 = 0x64; 2598 /// object file name: name,,0,0,st_mtime 2599 pub const N_OSO: u8 = 0x66; 2600 /// local sym: name,,NO_SECT,type,offset 2601 pub const N_LSYM: u8 = 0x80; 2602 /// include file beginning: name,,NO_SECT,0,sum 2603 pub const N_BINCL: u8 = 0x82; 2604 /// #included file name: name,,n_sect,0,address 2605 pub const N_SOL: u8 = 0x84; 2606 /// compiler parameters: name,,NO_SECT,0,0 2607 pub const N_PARAMS: u8 = 0x86; 2608 /// compiler version: name,,NO_SECT,0,0 2609 pub const N_VERSION: u8 = 0x88; 2610 /// compiler -O level: name,,NO_SECT,0,0 2611 pub const N_OLEVEL: u8 = 0x8A; 2612 /// parameter: name,,NO_SECT,type,offset 2613 pub const N_PSYM: u8 = 0xa0; 2614 /// include file end: name,,NO_SECT,0,0 2615 pub const N_EINCL: u8 = 0xa2; 2616 /// alternate entry: name,,n_sect,linenumber,address 2617 pub const N_ENTRY: u8 = 0xa4; 2618 /// left bracket: 0,,NO_SECT,nesting level,address 2619 pub const N_LBRAC: u8 = 0xc0; 2620 /// deleted include file: name,,NO_SECT,0,sum 2621 pub const N_EXCL: u8 = 0xc2; 2622 /// right bracket: 0,,NO_SECT,nesting level,address 2623 pub const N_RBRAC: u8 = 0xe0; 2624 /// begin common: name,,NO_SECT,0,0 2625 pub const N_BCOMM: u8 = 0xe2; 2626 /// end common: name,,n_sect,0,0 2627 pub const N_ECOMM: u8 = 0xe4; 2628 /// end common (local name): 0,,n_sect,0,address 2629 pub const N_ECOML: u8 = 0xe8; 2630 /// second stab entry with length information 2631 pub const N_LENG: u8 = 0xfe; 2632 2633 /* 2634 * for the berkeley pascal compiler, pc(1): 2635 */ 2636 /// global pascal symbol: name,,NO_SECT,subtype,line 2637 pub const N_PC: u8 = 0x30; 2638 2639 // Definitions from "/usr/include/mach-o/reloc.h". 2640 2641 /// A relocation entry. 2642 /// 2643 /// Mach-O relocations have plain and scattered variants, with the 2644 /// meaning of the fields depending on the variant. 2645 /// 2646 /// This type provides functions for determining whether the relocation 2647 /// is scattered, and for accessing the fields of each variant. 2648 #[derive(Debug, Clone, Copy)] 2649 #[repr(C)] 2650 pub struct Relocation<E: Endian> { 2651 pub r_word0: U32<E>, 2652 pub r_word1: U32<E>, 2653 } 2654 2655 impl<E: Endian> Relocation<E> { 2656 /// Determine whether this is a scattered relocation. 2657 #[inline] 2658 pub fn r_scattered(self, endian: E, cputype: u32) -> bool { 2659 if cputype == CPU_TYPE_X86_64 { 2660 false 2661 } else { 2662 self.r_word0.get(endian) & R_SCATTERED != 0 2663 } 2664 } 2665 2666 /// Return the fields of a plain relocation. 2667 pub fn info(self, endian: E) -> RelocationInfo { 2668 let r_address = self.r_word0.get(endian); 2669 let r_word1 = self.r_word1.get(endian); 2670 if endian.is_little_endian() { 2671 RelocationInfo { 2672 r_address, 2673 r_symbolnum: r_word1 & 0x00ff_ffff, 2674 r_pcrel: ((r_word1 >> 24) & 0x1) != 0, 2675 r_length: ((r_word1 >> 25) & 0x3) as u8, 2676 r_extern: ((r_word1 >> 27) & 0x1) != 0, 2677 r_type: (r_word1 >> 28) as u8, 2678 } 2679 } else { 2680 RelocationInfo { 2681 r_address, 2682 r_symbolnum: r_word1 >> 8, 2683 r_pcrel: ((r_word1 >> 7) & 0x1) != 0, 2684 r_length: ((r_word1 >> 5) & 0x3) as u8, 2685 r_extern: ((r_word1 >> 4) & 0x1) != 0, 2686 r_type: (r_word1 & 0xf) as u8, 2687 } 2688 } 2689 } 2690 2691 /// Return the fields of a scattered relocation. 2692 pub fn scattered_info(self, endian: E) -> ScatteredRelocationInfo { 2693 let r_word0 = self.r_word0.get(endian); 2694 let r_value = self.r_word1.get(endian); 2695 ScatteredRelocationInfo { 2696 r_address: r_word0 & 0x00ff_ffff, 2697 r_type: ((r_word0 >> 24) & 0xf) as u8, 2698 r_length: ((r_word0 >> 28) & 0x3) as u8, 2699 r_pcrel: ((r_word0 >> 30) & 0x1) != 0, 2700 r_value, 2701 } 2702 } 2703 } 2704 2705 /* 2706 * Format of a relocation entry of a Mach-O file. Modified from the 4.3BSD 2707 * format. The modifications from the original format were changing the value 2708 * of the r_symbolnum field for "local" (r_extern == 0) relocation entries. 2709 * This modification is required to support symbols in an arbitrary number of 2710 * sections not just the three sections (text, data and bss) in a 4.3BSD file. 2711 * Also the last 4 bits have had the r_type tag added to them. 2712 */ 2713 2714 #[derive(Debug, Clone, Copy)] 2715 pub struct RelocationInfo { 2716 /// offset in the section to what is being relocated 2717 pub r_address: u32, 2718 /// symbol index if r_extern == 1 or section ordinal if r_extern == 0 2719 pub r_symbolnum: u32, 2720 /// was relocated pc relative already 2721 pub r_pcrel: bool, 2722 /// 0=byte, 1=word, 2=long, 3=quad 2723 pub r_length: u8, 2724 /// does not include value of sym referenced 2725 pub r_extern: bool, 2726 /// if not 0, machine specific relocation type 2727 pub r_type: u8, 2728 } 2729 2730 impl RelocationInfo { 2731 /// Combine the fields into a `Relocation`. 2732 pub fn relocation<E: Endian>(self, endian: E) -> Relocation<E> { 2733 let r_word0 = U32::new(endian, self.r_address); 2734 let r_word1 = U32::new( 2735 endian, 2736 if endian.is_little_endian() { 2737 self.r_symbolnum & 0x00ff_ffff 2738 | u32::from(self.r_pcrel) << 24 2739 | u32::from(self.r_length & 0x3) << 25 2740 | u32::from(self.r_extern) << 27 2741 | u32::from(self.r_type) << 28 2742 } else { 2743 self.r_symbolnum >> 8 2744 | u32::from(self.r_pcrel) << 7 2745 | u32::from(self.r_length & 0x3) << 5 2746 | u32::from(self.r_extern) << 4 2747 | u32::from(self.r_type) & 0xf 2748 }, 2749 ); 2750 Relocation { r_word0, r_word1 } 2751 } 2752 } 2753 2754 /// absolute relocation type for Mach-O files 2755 pub const R_ABS: u8 = 0; 2756 2757 /* 2758 * The r_address is not really the address as it's name indicates but an offset. 2759 * In 4.3BSD a.out objects this offset is from the start of the "segment" for 2760 * which relocation entry is for (text or data). For Mach-O object files it is 2761 * also an offset but from the start of the "section" for which the relocation 2762 * entry is for. See comments in <mach-o/loader.h> about the r_address feild 2763 * in images for used with the dynamic linker. 2764 * 2765 * In 4.3BSD a.out objects if r_extern is zero then r_symbolnum is an ordinal 2766 * for the segment the symbol being relocated is in. These ordinals are the 2767 * symbol types N_TEXT, N_DATA, N_BSS or N_ABS. In Mach-O object files these 2768 * ordinals refer to the sections in the object file in the order their section 2769 * structures appear in the headers of the object file they are in. The first 2770 * section has the ordinal 1, the second 2, and so on. This means that the 2771 * same ordinal in two different object files could refer to two different 2772 * sections. And further could have still different ordinals when combined 2773 * by the link-editor. The value R_ABS is used for relocation entries for 2774 * absolute symbols which need no further relocation. 2775 */ 2776 2777 /* 2778 * For RISC machines some of the references are split across two instructions 2779 * and the instruction does not contain the complete value of the reference. 2780 * In these cases a second, or paired relocation entry, follows each of these 2781 * relocation entries, using a PAIR r_type, which contains the other part of the 2782 * reference not contained in the instruction. This other part is stored in the 2783 * pair's r_address field. The exact number of bits of the other part of the 2784 * reference store in the r_address field is dependent on the particular 2785 * relocation type for the particular architecture. 2786 */ 2787 2788 /* 2789 * To make scattered loading by the link editor work correctly "local" 2790 * relocation entries can't be used when the item to be relocated is the value 2791 * of a symbol plus an offset (where the resulting expresion is outside the 2792 * block the link editor is moving, a blocks are divided at symbol addresses). 2793 * In this case. where the item is a symbol value plus offset, the link editor 2794 * needs to know more than just the section the symbol was defined. What is 2795 * needed is the actual value of the symbol without the offset so it can do the 2796 * relocation correctly based on where the value of the symbol got relocated to 2797 * not the value of the expression (with the offset added to the symbol value). 2798 * So for the NeXT 2.0 release no "local" relocation entries are ever used when 2799 * there is a non-zero offset added to a symbol. The "external" and "local" 2800 * relocation entries remain unchanged. 2801 * 2802 * The implemention is quite messy given the compatibility with the existing 2803 * relocation entry format. The ASSUMPTION is that a section will never be 2804 * bigger than 2**24 - 1 (0x00ffffff or 16,777,215) bytes. This assumption 2805 * allows the r_address (which is really an offset) to fit in 24 bits and high 2806 * bit of the r_address field in the relocation_info structure to indicate 2807 * it is really a scattered_relocation_info structure. Since these are only 2808 * used in places where "local" relocation entries are used and not where 2809 * "external" relocation entries are used the r_extern field has been removed. 2810 * 2811 * For scattered loading to work on a RISC machine where some of the references 2812 * are split across two instructions the link editor needs to be assured that 2813 * each reference has a unique 32 bit reference (that more than one reference is 2814 * NOT sharing the same high 16 bits for example) so it move each referenced 2815 * item independent of each other. Some compilers guarantees this but the 2816 * compilers don't so scattered loading can be done on those that do guarantee 2817 * this. 2818 */ 2819 2820 /// Bit set in `Relocation::r_word0` for scattered relocations. 2821 pub const R_SCATTERED: u32 = 0x8000_0000; 2822 2823 #[derive(Debug, Clone, Copy)] 2824 pub struct ScatteredRelocationInfo { 2825 /// offset in the section to what is being relocated 2826 pub r_address: u32, 2827 /// if not 0, machine specific relocation type 2828 pub r_type: u8, 2829 /// 0=byte, 1=word, 2=long, 3=quad 2830 pub r_length: u8, 2831 /// was relocated pc relative already 2832 pub r_pcrel: bool, 2833 /// the value the item to be relocated is refering to (without any offset added) 2834 pub r_value: u32, 2835 } 2836 2837 impl ScatteredRelocationInfo { 2838 /// Combine the fields into a `Relocation`. 2839 pub fn relocation<E: Endian>(self, endian: E) -> Relocation<E> { 2840 let r_word0 = U32::new( 2841 endian, 2842 self.r_address & 0x00ff_ffff 2843 | u32::from(self.r_type & 0xf) << 24 2844 | u32::from(self.r_length & 0x3) << 28 2845 | u32::from(self.r_pcrel) << 30 2846 | R_SCATTERED, 2847 ); 2848 let r_word1 = U32::new(endian, self.r_value); 2849 Relocation { r_word0, r_word1 } 2850 } 2851 } 2852 2853 /* 2854 * Relocation types used in a generic implementation. Relocation entries for 2855 * normal things use the generic relocation as discribed above and their r_type 2856 * is GENERIC_RELOC_VANILLA (a value of zero). 2857 * 2858 * Another type of generic relocation, GENERIC_RELOC_SECTDIFF, is to support 2859 * the difference of two symbols defined in different sections. That is the 2860 * expression "symbol1 - symbol2 + constant" is a relocatable expression when 2861 * both symbols are defined in some section. For this type of relocation the 2862 * both relocations entries are scattered relocation entries. The value of 2863 * symbol1 is stored in the first relocation entry's r_value field and the 2864 * value of symbol2 is stored in the pair's r_value field. 2865 * 2866 * A special case for a prebound lazy pointer is needed to beable to set the 2867 * value of the lazy pointer back to its non-prebound state. This is done 2868 * using the GENERIC_RELOC_PB_LA_PTR r_type. This is a scattered relocation 2869 * entry where the r_value feild is the value of the lazy pointer not prebound. 2870 */ 2871 /// generic relocation as discribed above 2872 pub const GENERIC_RELOC_VANILLA: u8 = 0; 2873 /// Only follows a GENERIC_RELOC_SECTDIFF 2874 pub const GENERIC_RELOC_PAIR: u8 = 1; 2875 pub const GENERIC_RELOC_SECTDIFF: u8 = 2; 2876 /// prebound lazy pointer 2877 pub const GENERIC_RELOC_PB_LA_PTR: u8 = 3; 2878 pub const GENERIC_RELOC_LOCAL_SECTDIFF: u8 = 4; 2879 /// thread local variables 2880 pub const GENERIC_RELOC_TLV: u8 = 5; 2881 2882 // Definitions from "/usr/include/mach-o/arm/reloc.h". 2883 2884 /* 2885 * Relocation types used in the arm implementation. Relocation entries for 2886 * things other than instructions use the same generic relocation as discribed 2887 * in <mach-o/reloc.h> and their r_type is ARM_RELOC_VANILLA, one of the 2888 * *_SECTDIFF or the *_PB_LA_PTR types. The rest of the relocation types are 2889 * for instructions. Since they are for instructions the r_address field 2890 * indicates the 32 bit instruction that the relocation is to be preformed on. 2891 */ 2892 /// generic relocation as discribed above 2893 pub const ARM_RELOC_VANILLA: u8 = 0; 2894 /// the second relocation entry of a pair 2895 pub const ARM_RELOC_PAIR: u8 = 1; 2896 /// a PAIR follows with subtract symbol value 2897 pub const ARM_RELOC_SECTDIFF: u8 = 2; 2898 /// like ARM_RELOC_SECTDIFF, but the symbol referenced was local. 2899 pub const ARM_RELOC_LOCAL_SECTDIFF: u8 = 3; 2900 /// prebound lazy pointer 2901 pub const ARM_RELOC_PB_LA_PTR: u8 = 4; 2902 /// 24 bit branch displacement (to a word address) 2903 pub const ARM_RELOC_BR24: u8 = 5; 2904 /// 22 bit branch displacement (to a half-word address) 2905 pub const ARM_THUMB_RELOC_BR22: u8 = 6; 2906 /// obsolete - a thumb 32-bit branch instruction possibly needing page-spanning branch workaround 2907 pub const ARM_THUMB_32BIT_BRANCH: u8 = 7; 2908 2909 /* 2910 * For these two r_type relocations they always have a pair following them 2911 * and the r_length bits are used differently. The encoding of the 2912 * r_length is as follows: 2913 * low bit of r_length: 2914 * 0 - :lower16: for movw instructions 2915 * 1 - :upper16: for movt instructions 2916 * high bit of r_length: 2917 * 0 - arm instructions 2918 * 1 - thumb instructions 2919 * the other half of the relocated expression is in the following pair 2920 * relocation entry in the the low 16 bits of r_address field. 2921 */ 2922 pub const ARM_RELOC_HALF: u8 = 8; 2923 pub const ARM_RELOC_HALF_SECTDIFF: u8 = 9; 2924 2925 // Definitions from "/usr/include/mach-o/arm64/reloc.h". 2926 2927 /* 2928 * Relocation types used in the arm64 implementation. 2929 */ 2930 /// for pointers 2931 pub const ARM64_RELOC_UNSIGNED: u8 = 0; 2932 /// must be followed by a ARM64_RELOC_UNSIGNED 2933 pub const ARM64_RELOC_SUBTRACTOR: u8 = 1; 2934 /// a B/BL instruction with 26-bit displacement 2935 pub const ARM64_RELOC_BRANCH26: u8 = 2; 2936 /// pc-rel distance to page of target 2937 pub const ARM64_RELOC_PAGE21: u8 = 3; 2938 /// offset within page, scaled by r_length 2939 pub const ARM64_RELOC_PAGEOFF12: u8 = 4; 2940 /// pc-rel distance to page of GOT slot 2941 pub const ARM64_RELOC_GOT_LOAD_PAGE21: u8 = 5; 2942 /// offset within page of GOT slot, scaled by r_length 2943 pub const ARM64_RELOC_GOT_LOAD_PAGEOFF12: u8 = 6; 2944 /// for pointers to GOT slots 2945 pub const ARM64_RELOC_POINTER_TO_GOT: u8 = 7; 2946 /// pc-rel distance to page of TLVP slot 2947 pub const ARM64_RELOC_TLVP_LOAD_PAGE21: u8 = 8; 2948 /// offset within page of TLVP slot, scaled by r_length 2949 pub const ARM64_RELOC_TLVP_LOAD_PAGEOFF12: u8 = 9; 2950 /// must be followed by PAGE21 or PAGEOFF12 2951 pub const ARM64_RELOC_ADDEND: u8 = 10; 2952 2953 // An arm64e authenticated pointer. 2954 // 2955 // Represents a pointer to a symbol (like ARM64_RELOC_UNSIGNED). 2956 // Additionally, the resulting pointer is signed. The signature is 2957 // specified in the target location: the addend is restricted to the lower 2958 // 32 bits (instead of the full 64 bits for ARM64_RELOC_UNSIGNED): 2959 // 2960 // |63|62|61-51|50-49| 48 |47 - 32|31 - 0| 2961 // | 1| 0| 0 | key | addr | discriminator | addend | 2962 // 2963 // The key is one of: 2964 // IA: 00 IB: 01 2965 // DA: 10 DB: 11 2966 // 2967 // The discriminator field is used as extra signature diversification. 2968 // 2969 // The addr field indicates whether the target address should be blended 2970 // into the discriminator. 2971 // 2972 pub const ARM64_RELOC_AUTHENTICATED_POINTER: u8 = 11; 2973 2974 // Definitions from "/usr/include/mach-o/ppc/reloc.h". 2975 2976 /* 2977 * Relocation types used in the ppc implementation. Relocation entries for 2978 * things other than instructions use the same generic relocation as discribed 2979 * above and their r_type is RELOC_VANILLA. The rest of the relocation types 2980 * are for instructions. Since they are for instructions the r_address field 2981 * indicates the 32 bit instruction that the relocation is to be preformed on. 2982 * The fields r_pcrel and r_length are ignored for non-RELOC_VANILLA r_types 2983 * except for PPC_RELOC_BR14. 2984 * 2985 * For PPC_RELOC_BR14 if the r_length is the unused value 3, then the branch was 2986 * statically predicted setting or clearing the Y-bit based on the sign of the 2987 * displacement or the opcode. If this is the case the static linker must flip 2988 * the value of the Y-bit if the sign of the displacement changes for non-branch 2989 * always conditions. 2990 */ 2991 /// generic relocation as discribed above 2992 pub const PPC_RELOC_VANILLA: u8 = 0; 2993 /// the second relocation entry of a pair 2994 pub const PPC_RELOC_PAIR: u8 = 1; 2995 /// 14 bit branch displacement (to a word address) 2996 pub const PPC_RELOC_BR14: u8 = 2; 2997 /// 24 bit branch displacement (to a word address) 2998 pub const PPC_RELOC_BR24: u8 = 3; 2999 /// a PAIR follows with the low half 3000 pub const PPC_RELOC_HI16: u8 = 4; 3001 /// a PAIR follows with the high half 3002 pub const PPC_RELOC_LO16: u8 = 5; 3003 /// Same as the RELOC_HI16 except the low 16 bits and the high 16 bits are added together 3004 /// with the low 16 bits sign extened first. This means if bit 15 of the low 16 bits is 3005 /// set the high 16 bits stored in the instruction will be adjusted. 3006 pub const PPC_RELOC_HA16: u8 = 6; 3007 /// Same as the LO16 except that the low 2 bits are not stored in the instruction and are 3008 /// always zero. This is used in double word load/store instructions. 3009 pub const PPC_RELOC_LO14: u8 = 7; 3010 /// a PAIR follows with subtract symbol value 3011 pub const PPC_RELOC_SECTDIFF: u8 = 8; 3012 /// prebound lazy pointer 3013 pub const PPC_RELOC_PB_LA_PTR: u8 = 9; 3014 /// section difference forms of above. a PAIR 3015 pub const PPC_RELOC_HI16_SECTDIFF: u8 = 10; 3016 /// follows these with subtract symbol value 3017 pub const PPC_RELOC_LO16_SECTDIFF: u8 = 11; 3018 pub const PPC_RELOC_HA16_SECTDIFF: u8 = 12; 3019 pub const PPC_RELOC_JBSR: u8 = 13; 3020 pub const PPC_RELOC_LO14_SECTDIFF: u8 = 14; 3021 /// like PPC_RELOC_SECTDIFF, but the symbol referenced was local. 3022 pub const PPC_RELOC_LOCAL_SECTDIFF: u8 = 15; 3023 3024 // Definitions from "/usr/include/mach-o/x86_64/reloc.h". 3025 3026 /* 3027 * Relocations for x86_64 are a bit different than for other architectures in 3028 * Mach-O: Scattered relocations are not used. Almost all relocations produced 3029 * by the compiler are external relocations. An external relocation has the 3030 * r_extern bit set to 1 and the r_symbolnum field contains the symbol table 3031 * index of the target label. 3032 * 3033 * When the assembler is generating relocations, if the target label is a local 3034 * label (begins with 'L'), then the previous non-local label in the same 3035 * section is used as the target of the external relocation. An addend is used 3036 * with the distance from that non-local label to the target label. Only when 3037 * there is no previous non-local label in the section is an internal 3038 * relocation used. 3039 * 3040 * The addend (i.e. the 4 in _foo+4) is encoded in the instruction (Mach-O does 3041 * not have RELA relocations). For PC-relative relocations, the addend is 3042 * stored directly in the instruction. This is different from other Mach-O 3043 * architectures, which encode the addend minus the current section offset. 3044 * 3045 * The relocation types are: 3046 * 3047 * X86_64_RELOC_UNSIGNED // for absolute addresses 3048 * X86_64_RELOC_SIGNED // for signed 32-bit displacement 3049 * X86_64_RELOC_BRANCH // a CALL/JMP instruction with 32-bit displacement 3050 * X86_64_RELOC_GOT_LOAD // a MOVQ load of a GOT entry 3051 * X86_64_RELOC_GOT // other GOT references 3052 * X86_64_RELOC_SUBTRACTOR // must be followed by a X86_64_RELOC_UNSIGNED 3053 * 3054 * The following are sample assembly instructions, followed by the relocation 3055 * and section content they generate in an object file: 3056 * 3057 * call _foo 3058 * r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3059 * E8 00 00 00 00 3060 * 3061 * call _foo+4 3062 * r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3063 * E8 04 00 00 00 3064 * 3065 * movq _foo@GOTPCREL(%rip), %rax 3066 * r_type=X86_64_RELOC_GOT_LOAD, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3067 * 48 8B 05 00 00 00 00 3068 * 3069 * pushq _foo@GOTPCREL(%rip) 3070 * r_type=X86_64_RELOC_GOT, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3071 * FF 35 00 00 00 00 3072 * 3073 * movl _foo(%rip), %eax 3074 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3075 * 8B 05 00 00 00 00 3076 * 3077 * movl _foo+4(%rip), %eax 3078 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3079 * 8B 05 04 00 00 00 3080 * 3081 * movb $0x12, _foo(%rip) 3082 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3083 * C6 05 FF FF FF FF 12 3084 * 3085 * movl $0x12345678, _foo(%rip) 3086 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo 3087 * C7 05 FC FF FF FF 78 56 34 12 3088 * 3089 * .quad _foo 3090 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3091 * 00 00 00 00 00 00 00 00 3092 * 3093 * .quad _foo+4 3094 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3095 * 04 00 00 00 00 00 00 00 3096 * 3097 * .quad _foo - _bar 3098 * r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_bar 3099 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3100 * 00 00 00 00 00 00 00 00 3101 * 3102 * .quad _foo - _bar + 4 3103 * r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_bar 3104 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3105 * 04 00 00 00 00 00 00 00 3106 * 3107 * .long _foo - _bar 3108 * r_type=X86_64_RELOC_SUBTRACTOR, r_length=2, r_extern=1, r_pcrel=0, r_symbolnum=_bar 3109 * r_type=X86_64_RELOC_UNSIGNED, r_length=2, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3110 * 00 00 00 00 3111 * 3112 * lea L1(%rip), %rax 3113 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_prev 3114 * 48 8d 05 12 00 00 00 3115 * // assumes _prev is the first non-local label 0x12 bytes before L1 3116 * 3117 * lea L0(%rip), %rax 3118 * r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3 3119 * 48 8d 05 56 00 00 00 3120 * // assumes L0 is in third section and there is no previous non-local label. 3121 * // The rip-relative-offset of 0x00000056 is L0-address_of_next_instruction. 3122 * // address_of_next_instruction is the address of the relocation + 4. 3123 * 3124 * add $6,L0(%rip) 3125 * r_type=X86_64_RELOC_SIGNED_1, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3 3126 * 83 05 18 00 00 00 06 3127 * // assumes L0 is in third section and there is no previous non-local label. 3128 * // The rip-relative-offset of 0x00000018 is L0-address_of_next_instruction. 3129 * // address_of_next_instruction is the address of the relocation + 4 + 1. 3130 * // The +1 comes from SIGNED_1. This is used because the relocation is not 3131 * // at the end of the instruction. 3132 * 3133 * .quad L1 3134 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev 3135 * 12 00 00 00 00 00 00 00 3136 * // assumes _prev is the first non-local label 0x12 bytes before L1 3137 * 3138 * .quad L0 3139 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=0, r_pcrel=0, r_symbolnum=3 3140 * 56 00 00 00 00 00 00 00 3141 * // assumes L0 is in third section, has an address of 0x00000056 in .o 3142 * // file, and there is no previous non-local label 3143 * 3144 * .quad _foo - . 3145 * r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev 3146 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3147 * EE FF FF FF FF FF FF FF 3148 * // assumes _prev is the first non-local label 0x12 bytes before this 3149 * // .quad 3150 * 3151 * .quad _foo - L1 3152 * r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev 3153 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo 3154 * EE FF FF FF FF FF FF FF 3155 * // assumes _prev is the first non-local label 0x12 bytes before L1 3156 * 3157 * .quad L1 - _prev 3158 * // No relocations. This is an assembly time constant. 3159 * 12 00 00 00 00 00 00 00 3160 * // assumes _prev is the first non-local label 0x12 bytes before L1 3161 * 3162 * 3163 * 3164 * In final linked images, there are only two valid relocation kinds: 3165 * 3166 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_pcrel=0, r_extern=1, r_symbolnum=sym_index 3167 * This tells dyld to add the address of a symbol to a pointer sized (8-byte) 3168 * piece of data (i.e on disk the 8-byte piece of data contains the addend). The 3169 * r_symbolnum contains the index into the symbol table of the target symbol. 3170 * 3171 * r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_pcrel=0, r_extern=0, r_symbolnum=0 3172 * This tells dyld to adjust the pointer sized (8-byte) piece of data by the amount 3173 * the containing image was loaded from its base address (e.g. slide). 3174 * 3175 */ 3176 /// for absolute addresses 3177 pub const X86_64_RELOC_UNSIGNED: u8 = 0; 3178 /// for signed 32-bit displacement 3179 pub const X86_64_RELOC_SIGNED: u8 = 1; 3180 /// a CALL/JMP instruction with 32-bit displacement 3181 pub const X86_64_RELOC_BRANCH: u8 = 2; 3182 /// a MOVQ load of a GOT entry 3183 pub const X86_64_RELOC_GOT_LOAD: u8 = 3; 3184 /// other GOT references 3185 pub const X86_64_RELOC_GOT: u8 = 4; 3186 /// must be followed by a X86_64_RELOC_UNSIGNED 3187 pub const X86_64_RELOC_SUBTRACTOR: u8 = 5; 3188 /// for signed 32-bit displacement with a -1 addend 3189 pub const X86_64_RELOC_SIGNED_1: u8 = 6; 3190 /// for signed 32-bit displacement with a -2 addend 3191 pub const X86_64_RELOC_SIGNED_2: u8 = 7; 3192 /// for signed 32-bit displacement with a -4 addend 3193 pub const X86_64_RELOC_SIGNED_4: u8 = 8; 3194 /// for thread local variables 3195 pub const X86_64_RELOC_TLV: u8 = 9; 3196 3197 unsafe_impl_pod!(FatHeader, FatArch32, FatArch64,); 3198 unsafe_impl_endian_pod!( 3199 DyldCacheHeader, 3200 DyldCacheMappingInfo, 3201 DyldCacheImageInfo, 3202 MachHeader32, 3203 MachHeader64, 3204 LoadCommand, 3205 LcStr, 3206 SegmentCommand32, 3207 SegmentCommand64, 3208 Section32, 3209 Section64, 3210 Fvmlib, 3211 FvmlibCommand, 3212 Dylib, 3213 DylibCommand, 3214 SubFrameworkCommand, 3215 SubClientCommand, 3216 SubUmbrellaCommand, 3217 SubLibraryCommand, 3218 PreboundDylibCommand, 3219 DylinkerCommand, 3220 ThreadCommand, 3221 RoutinesCommand32, 3222 RoutinesCommand64, 3223 SymtabCommand, 3224 DysymtabCommand, 3225 DylibTableOfContents, 3226 DylibModule32, 3227 DylibModule64, 3228 DylibReference, 3229 TwolevelHintsCommand, 3230 TwolevelHint, 3231 PrebindCksumCommand, 3232 UuidCommand, 3233 RpathCommand, 3234 LinkeditDataCommand, 3235 FilesetEntryCommand, 3236 EncryptionInfoCommand32, 3237 EncryptionInfoCommand64, 3238 VersionMinCommand, 3239 BuildVersionCommand, 3240 BuildToolVersion, 3241 DyldInfoCommand, 3242 LinkerOptionCommand, 3243 SymsegCommand, 3244 IdentCommand, 3245 FvmfileCommand, 3246 EntryPointCommand, 3247 SourceVersionCommand, 3248 DataInCodeEntry, 3249 //TlvDescriptor, 3250 NoteCommand, 3251 Nlist32, 3252 Nlist64, 3253 Relocation, 3254 ); 3255