1 /* 2 * Copyright 2002-2019 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _ELF_H 6 #define _ELF_H 7 8 9 #include <BeBuild.h> 10 #include <ByteOrder.h> 11 #include <SupportDefs.h> 12 13 14 typedef uint32 Elf32_Addr; 15 typedef uint16 Elf32_Half; 16 typedef uint32 Elf32_Off; 17 typedef int32 Elf32_Sword; 18 typedef uint32 Elf32_Word; 19 20 typedef Elf32_Half Elf32_Versym; 21 22 typedef uint64 Elf64_Addr; 23 typedef uint64 Elf64_Off; 24 typedef uint16 Elf64_Half; 25 typedef uint32 Elf64_Word; 26 typedef int32 Elf64_Sword; 27 typedef uint64 Elf64_Xword; 28 typedef int64 Elf64_Sxword; 29 30 typedef Elf64_Half Elf64_Versym; 31 32 #if B_HAIKU_32_BIT 33 #define Elf_Addr Elf32_Addr 34 #define Elf_Phdr Elf32_Phdr 35 #define Elf_Half Elf32_Half 36 #elif B_HAIKU_64_BIT 37 #define Elf_Addr Elf64_Addr 38 #define Elf_Phdr Elf64_Phdr 39 #define Elf_Half Elf64_Half 40 #endif 41 42 43 /*** ELF header ***/ 44 45 #define EI_NIDENT 16 46 47 typedef struct { 48 uint8 e_ident[EI_NIDENT]; 49 Elf32_Half e_type; 50 Elf32_Half e_machine; 51 Elf32_Word e_version; 52 Elf32_Addr e_entry; 53 Elf32_Off e_phoff; 54 Elf32_Off e_shoff; 55 Elf32_Word e_flags; 56 Elf32_Half e_ehsize; 57 Elf32_Half e_phentsize; 58 Elf32_Half e_phnum; 59 Elf32_Half e_shentsize; 60 Elf32_Half e_shnum; 61 Elf32_Half e_shstrndx; 62 63 #ifdef __cplusplus 64 bool IsHostEndian() const; 65 #endif 66 } Elf32_Ehdr; 67 68 typedef struct { 69 uint8 e_ident[EI_NIDENT]; 70 Elf64_Half e_type; 71 Elf64_Half e_machine; 72 Elf64_Word e_version; 73 Elf64_Addr e_entry; 74 Elf64_Off e_phoff; 75 Elf64_Off e_shoff; 76 Elf64_Word e_flags; 77 Elf64_Half e_ehsize; 78 Elf64_Half e_phentsize; 79 Elf64_Half e_phnum; 80 Elf64_Half e_shentsize; 81 Elf64_Half e_shnum; 82 Elf64_Half e_shstrndx; 83 84 #ifdef __cplusplus 85 bool IsHostEndian() const; 86 #endif 87 } Elf64_Ehdr; 88 89 /* e_ident[] indices */ 90 #define EI_MAG0 0 91 #define EI_MAG1 1 92 #define EI_MAG2 2 93 #define EI_MAG3 3 94 #define EI_CLASS 4 95 #define EI_DATA 5 96 #define EI_VERSION 6 97 #define EI_PAD 7 98 99 /* Values for the magic number bytes. */ 100 #define ELFMAG0 0x7f 101 #define ELFMAG1 'E' 102 #define ELFMAG2 'L' 103 #define ELFMAG3 'F' 104 #define ELFMAG "\x7f""ELF" 105 #define SELFMAG 4 106 107 /* e_ident */ 108 #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ 109 (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ 110 (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ 111 (ehdr).e_ident[EI_MAG3] == ELFMAG3) 112 113 /* e_type (Object file type) */ 114 #define ET_NONE 0 /* No file type */ 115 #define ET_REL 1 /* Relocatable file */ 116 #define ET_EXEC 2 /* Executable file */ 117 #define ET_DYN 3 /* Shared-object file */ 118 #define ET_CORE 4 /* Core file */ 119 #define ET_LOOS 0xfe00 /* OS-specific range start */ 120 #define ET_HIOS 0xfeff /* OS-specific range end */ 121 #define ET_LOPROC 0xff00 /* Processor-specific range start */ 122 #define ET_HIPROC 0xffff /* Processor-specific range end */ 123 124 /* e_machine (Architecture) */ 125 #define EM_NONE 0 /* No machine */ 126 #define EM_M32 1 /* AT&T WE 32100 */ 127 #define EM_SPARC 2 /* Sparc */ 128 #define EM_386 3 /* Intel 80386 */ 129 #define EM_68K 4 /* Motorola m68k family */ 130 #define EM_88K 5 /* Motorola m88k family */ 131 #define EM_486 6 /* Intel 80486, Reserved for future use */ 132 #define EM_860 7 /* Intel 80860 */ 133 #define EM_MIPS 8 /* MIPS R3000 (officially, big-endian only) */ 134 #define EM_S370 9 /* IBM System/370 */ 135 #define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian, Deprecated */ 136 #define EM_PARISC 15 /* HPPA */ 137 #define EM_VPP550 17 /* Fujitsu VPP500 */ 138 #define EM_SPARC32PLUS 18 /* Sun "v8plus" */ 139 #define EM_960 19 /* Intel 80960 */ 140 #define EM_PPC 20 /* PowerPC */ 141 #define EM_PPC64 21 /* 64-bit PowerPC */ 142 #define EM_S390 22 /* IBM S/390 */ 143 #define EM_V800 36 /* NEC V800 series */ 144 #define EM_FR20 37 /* Fujitsu FR20 */ 145 #define EM_RH32 38 /* TRW RH32 */ 146 #define EM_MCORE 39 /* Motorola M*Core */ 147 #define EM_RCE 39 /* Old name for MCore */ 148 #define EM_ARM 40 /* ARM */ 149 #define EM_OLD_ALPHA 41 /* Digital Alpha */ 150 #define EM_SH 42 /* Renesas / SuperH SH */ 151 #define EM_SPARCV9 43 /* SPARC v9 64-bit */ 152 #define EM_TRICORE 44 /* Siemens Tricore embedded processor */ 153 #define EM_ARC 45 /* ARC Cores */ 154 #define EM_H8_300 46 /* Renesas H8/300 */ 155 #define EM_H8_300H 47 /* Renesas H8/300H */ 156 #define EM_H8S 48 /* Renesas H8S */ 157 #define EM_H8_500 49 /* Renesas H8/500 */ 158 #define EM_IA_64 50 /* Intel IA-64 Processor */ 159 #define EM_MIPS_X 51 /* Stanford MIPS-X */ 160 #define EM_COLDFIRE 52 /* Motorola Coldfire */ 161 #define EM_68HC12 53 /* Motorola M68HC12 */ 162 #define EM_MMA 54 /* Fujitsu Multimedia Accelerator */ 163 #define EM_PCP 55 /* Siemens PCP */ 164 #define EM_NCPU 56 /* Sony nCPU embedded RISC processor */ 165 #define EM_NDR1 57 /* Denso NDR1 microprocesspr */ 166 #define EM_STARCORE 58 /* Motorola Star*Core processor */ 167 #define EM_ME16 59 /* Toyota ME16 processor */ 168 #define EM_ST100 60 /* STMicroelectronics ST100 processor */ 169 #define EM_TINYJ 61 /* Advanced Logic Corp. TinyJ embedded processor */ 170 #define EM_X86_64 62 /* Advanced Micro Devices X86-64 processor */ 171 #define EM_PDSP 63 /* Sony DSP Processor */ 172 #define EM_FX66 66 /* Siemens FX66 microcontroller */ 173 #define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ 174 #define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */ 175 #define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ 176 #define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ 177 #define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ 178 #define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ 179 #define EM_SVX 73 /* Silicon Graphics SVx */ 180 #define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ 181 #define EM_VAX 75 /* Digital VAX */ 182 #define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ 183 #define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */ 184 #define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ 185 #define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ 186 #define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */ 187 #define EM_HUANY 81 /* Harvard University machine-independent object files */ 188 #define EM_PRISM 82 /* SiTera Prism */ 189 #define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ 190 #define EM_FR30 84 /* Fujitsu FR30 */ 191 #define EM_D10V 85 /* Mitsubishi D10V */ 192 #define EM_D30V 86 /* Mitsubishi D30V */ 193 #define EM_V850 87 /* NEC v850 */ 194 #define EM_M32R 88 /* Mitsubishi M32R */ 195 #define EM_MN10300 89 /* Matsushita MN10300 */ 196 #define EM_MN10200 90 /* Matsushita MN10200 */ 197 #define EM_PJ 91 /* picoJava */ 198 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ 199 #define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */ 200 #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ 201 #define EM_VIDCORE 95 /* Alphamosaic VideoCore */ 202 #define EM_CR 103 /* Nat. Semi. CompactRISC microprocessor */ 203 #define EM_BLACKFIN 106 /* Analog Devices Blackfin (DSP) processor */ 204 #define EM_ARCA 109 /* Arca RISC Microprocessor */ 205 #define EM_VIDCORE3 137 /* Broadcom VideoCore III */ 206 #define EM_ARM64 183 /* ARM 64 bit */ 207 #define EM_AARCH64 EM_ARM64 208 #define EM_AVR32 185 /* AVR-32 */ 209 #define EM_STM8 186 /* ST STM8S */ 210 #define EM_CUDA 190 /* Nvidia CUDA */ 211 #define EM_VIDCORE5 198 /* Broadcom VideoCore V */ 212 #define EM_NORC 218 /* Nanoradio Optimized RISC */ 213 #define EM_AMDGPU 224 /* AMD GPU */ 214 #define EM_RISCV 243 /* RISC-V */ 215 #define EM_BPF 247 /* Linux kernel bpf virtual machine */ 216 217 #define EM_ALPHA 0x9026 /* Digital Alpha (nonstandard, but the value 218 everyone uses) */ 219 220 /* architecture class (EI_CLASS) */ 221 #define ELFCLASSNONE 0 222 #define ELFCLASS32 1 223 #define ELFCLASS64 2 224 225 /* endian (EI_DATA) */ 226 #define ELFDATANONE 0 /* invalid */ 227 #define ELFDATA2LSB 1 /* little endian */ 228 #define ELFDATA2MSB 2 /* big endian */ 229 230 /* ELF version (EI_VERSION) */ 231 #define EV_NONE 0 /* invalid */ 232 #define EV_CURRENT 1 /* current version */ 233 234 /*** section header ***/ 235 236 typedef struct { 237 Elf32_Word sh_name; 238 Elf32_Word sh_type; 239 Elf32_Word sh_flags; 240 Elf32_Addr sh_addr; 241 Elf32_Off sh_offset; 242 Elf32_Word sh_size; 243 Elf32_Word sh_link; 244 Elf32_Word sh_info; 245 Elf32_Word sh_addralign; 246 Elf32_Word sh_entsize; 247 } Elf32_Shdr; 248 249 typedef struct { 250 Elf64_Word sh_name; 251 Elf64_Word sh_type; 252 Elf64_Xword sh_flags; 253 Elf64_Addr sh_addr; 254 Elf64_Off sh_offset; 255 Elf64_Xword sh_size; 256 Elf64_Word sh_link; 257 Elf64_Word sh_info; 258 Elf64_Xword sh_addralign; 259 Elf64_Xword sh_entsize; 260 } Elf64_Shdr; 261 262 /* special section indices */ 263 #define SHN_UNDEF 0 264 #define SHN_LORESERVE 0xff00 265 #define SHN_LOPROC 0xff00 266 #define SHN_HIPROC 0xff1f 267 #define SHN_ABS 0xfff1 268 #define SHN_COMMON 0xfff2 269 #define SHN_HIRESERVE 0xffff 270 271 /* section header type */ 272 #define SHT_NULL 0 273 #define SHT_PROGBITS 1 274 #define SHT_SYMTAB 2 275 #define SHT_STRTAB 3 276 #define SHT_RELA 4 277 #define SHT_HASH 5 278 #define SHT_DYNAMIC 6 279 #define SHT_NOTE 7 280 #define SHT_NOBITS 8 281 #define SHT_REL 9 282 #define SHT_SHLIB 10 283 #define SHT_DYNSYM 11 284 #define SHT_INIT_ARRAY 14 285 #define SHT_FINI_ARRAY 15 286 287 #define SHT_GNU_verdef 0x6ffffffd /* version definition section */ 288 #define SHT_GNU_verneed 0x6ffffffe /* version needs section */ 289 #define SHT_GNU_versym 0x6fffffff /* version symbol table */ 290 291 #define SHT_LOPROC 0x70000000 292 #define SHT_HIPROC 0x7fffffff 293 #define SHT_LOUSER 0x80000000 294 #define SHT_HIUSER 0xffffffff 295 296 /* section header flags */ 297 #define SHF_WRITE 1 298 #define SHF_ALLOC 2 299 #define SHF_EXECINSTR 4 300 301 #define SHF_MASKPROC 0xf0000000 302 303 304 /*** program header ***/ 305 306 typedef struct { 307 Elf32_Word p_type; 308 Elf32_Off p_offset; /* offset from the beginning of the file of the segment */ 309 Elf32_Addr p_vaddr; /* virtual address for the segment in memory */ 310 Elf32_Addr p_paddr; 311 Elf32_Word p_filesz; /* the size of the segment in the file */ 312 Elf32_Word p_memsz; /* the size of the segment in memory */ 313 Elf32_Word p_flags; 314 Elf32_Word p_align; 315 316 #ifdef __cplusplus 317 bool IsReadWrite() const; 318 bool IsExecutable() const; 319 #endif 320 } Elf32_Phdr; 321 322 typedef struct { 323 Elf64_Word p_type; 324 Elf64_Word p_flags; 325 Elf64_Off p_offset; /* offset from the beginning of the file of the segment */ 326 Elf64_Addr p_vaddr; /* virtual address for the segment in memory */ 327 Elf64_Addr p_paddr; 328 Elf64_Xword p_filesz; /* the size of the segment in the file */ 329 Elf64_Xword p_memsz; /* the size of the segment in memory */ 330 Elf64_Xword p_align; 331 332 #ifdef __cplusplus 333 bool IsReadWrite() const; 334 bool IsExecutable() const; 335 #endif 336 } Elf64_Phdr; 337 338 /* program header segment types */ 339 #define PT_NULL 0 340 #define PT_LOAD 1 341 #define PT_DYNAMIC 2 342 #define PT_INTERP 3 343 #define PT_NOTE 4 344 #define PT_SHLIB 5 345 #define PT_PHDR 6 346 #define PT_TLS 7 347 #define PT_EH_FRAME 0x6474e550 348 #define PT_STACK 0x6474e551 349 #define PT_RELRO 0x6474e552 350 351 #define PT_LOPROC 0x70000000 352 #define PT_ARM_UNWIND 0x70000001 353 #define PT_RISCV_ATTRIBUTES 0x70000003 354 #define PT_HIPROC 0x7fffffff 355 356 /* program header segment flags */ 357 #define PF_EXECUTE 0x1 358 #define PF_WRITE 0x2 359 #define PF_READ 0x4 360 #define PF_PROTECTION_MASK (PF_EXECUTE | PF_WRITE | PF_READ) 361 362 #define PF_MASKPROC 0xf0000000 363 364 365 /* symbol table entry */ 366 367 typedef struct { 368 Elf32_Word st_name; 369 Elf32_Addr st_value; 370 Elf32_Word st_size; 371 uint8 st_info; 372 uint8 st_other; 373 Elf32_Half st_shndx; 374 375 #ifdef __cplusplus 376 uint8 Bind() const; 377 uint8 Type() const; 378 void SetInfo(uint8 bind, uint8 type); 379 #endif 380 } Elf32_Sym; 381 382 typedef struct { 383 Elf64_Word st_name; 384 uint8 st_info; 385 uint8 st_other; 386 Elf64_Half st_shndx; 387 Elf64_Addr st_value; 388 Elf64_Xword st_size; 389 390 #ifdef __cplusplus 391 uint8 Bind() const; 392 uint8 Type() const; 393 void SetInfo(uint8 bind, uint8 type); 394 #endif 395 } Elf64_Sym; 396 397 #define ELF32_ST_BIND(i) ((i) >> 4) 398 #define ELF32_ST_TYPE(i) ((i) & 0xf) 399 #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf)) 400 401 #define ELF64_ST_BIND(i) ((i) >> 4) 402 #define ELF64_ST_TYPE(i) ((i) & 0xf) 403 #define ELF64_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf)) 404 405 /* symbol types */ 406 #define STT_NOTYPE 0 407 #define STT_OBJECT 1 408 #define STT_FUNC 2 409 #define STT_SECTION 3 410 #define STT_FILE 4 411 #define STT_TLS 6 412 #define STT_LOPROC 13 413 #define STT_HIPROC 15 414 415 /* symbol binding */ 416 #define STB_LOCAL 0 417 #define STB_GLOBAL 1 418 #define STB_WEAK 2 419 #define STB_LOPROC 13 420 #define STB_HIPROC 15 421 422 /* special symbol indices */ 423 #define STN_UNDEF 0 424 425 /* relocation types */ 426 427 #define R_386_NONE 0 428 #define R_386_32 1 /* add symbol value */ 429 #define R_386_PC32 2 /* add PC relative symbol value */ 430 #define R_386_GOT32 3 /* add PC relative GOT offset */ 431 #define R_386_PLT32 4 /* add PC relative PLT offset */ 432 #define R_386_COPY 5 /* copy data from shared object */ 433 #define R_386_GLOB_DAT 6 /* set GOT entry to data address */ 434 #define R_386_JMP_SLOT 7 /* set GOT entry to code address */ 435 #define R_386_RELATIVE 8 /* add load address of shared object */ 436 #define R_386_GOTOFF 9 /* add GOT relative symbol address */ 437 #define R_386_GOTPC 10 /* add PC relative GOT table address */ 438 #define R_386_TLS_DTPMOD32 35 439 #define R_386_TLS_DTPOFF32 36 440 441 442 /* relocation table entry */ 443 444 typedef struct { 445 Elf32_Addr r_offset; 446 Elf32_Word r_info; 447 448 #ifdef __cplusplus 449 uint8 SymbolIndex() const; 450 uint8 Type() const; 451 #endif 452 } Elf32_Rel; 453 454 typedef struct { 455 Elf64_Addr r_offset; 456 Elf64_Xword r_info; 457 458 #ifdef __cplusplus 459 uint8 SymbolIndex() const; 460 uint8 Type() const; 461 #endif 462 } Elf64_Rel; 463 464 #ifdef __cplusplus 465 typedef struct Elf32_Rela : public Elf32_Rel { 466 #else 467 typedef struct { 468 Elf32_Addr r_offset; 469 Elf32_Word r_info; 470 #endif 471 Elf32_Sword r_addend; 472 } Elf32_Rela; 473 474 #ifdef __cplusplus 475 typedef struct Elf64_Rela : public Elf64_Rel { 476 #else 477 typedef struct { 478 Elf64_Addr r_offset; 479 Elf64_Xword r_info; 480 #endif 481 Elf64_Sxword r_addend; 482 } Elf64_Rela; 483 484 #define ELF32_R_SYM(i) ((i) >> 8) 485 #define ELF32_R_TYPE(i) ((unsigned char)(i)) 486 #define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t)) 487 488 #define ELF64_R_SYM(i) ((i) >> 32) 489 #define ELF64_R_TYPE(i) ((i) & 0xffffffffL) 490 #define ELF64_R_INFO(s, t) ((((Elf64_Xword)(s)) << 32) + ((t) & 0xffffffffL)) 491 492 493 /* dynamic section entry */ 494 495 typedef struct { 496 Elf32_Sword d_tag; 497 union { 498 Elf32_Word d_val; 499 Elf32_Addr d_ptr; 500 } d_un; 501 } Elf32_Dyn; 502 503 typedef struct { 504 Elf64_Sxword d_tag; 505 union { 506 Elf64_Xword d_val; 507 Elf64_Addr d_ptr; 508 } d_un; 509 } Elf64_Dyn; 510 511 /* dynamic entry type */ 512 #define DT_NULL 0 513 #define DT_NEEDED 1 514 #define DT_PLTRELSZ 2 515 #define DT_PLTGOT 3 516 #define DT_HASH 4 517 #define DT_STRTAB 5 518 #define DT_SYMTAB 6 519 #define DT_RELA 7 520 #define DT_RELASZ 8 521 #define DT_RELAENT 9 522 #define DT_STRSZ 10 523 #define DT_SYMENT 11 524 #define DT_INIT 12 525 #define DT_FINI 13 526 #define DT_SONAME 14 527 #define DT_RPATH 15 528 #define DT_SYMBOLIC 16 529 #define DT_REL 17 530 #define DT_RELSZ 18 531 #define DT_RELENT 19 532 #define DT_PLTREL 20 533 #define DT_DEBUG 21 534 #define DT_TEXTREL 22 535 #define DT_JMPREL 23 536 #define DT_BIND_NOW 24 /* no lazy binding */ 537 #define DT_INIT_ARRAY 25 /* init function array */ 538 #define DT_FINI_ARRAY 26 /* termination function array */ 539 #define DT_INIT_ARRAYSZ 27 /* init function array size */ 540 #define DT_FINI_ARRAYSZ 28 /* termination function array size */ 541 #define DT_RUNPATH 29 /* library search path (supersedes DT_RPATH) */ 542 #define DT_FLAGS 30 /* flags (see below) */ 543 #define DT_ENCODING 32 544 #define DT_PREINIT_ARRAY 32 /* preinitialization array */ 545 #define DT_PREINIT_ARRAYSZ 33 /* preinitialization array size */ 546 547 #define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table */ 548 549 #define DT_VERSYM 0x6ffffff0 /* symbol version table */ 550 #define DT_VERDEF 0x6ffffffc /* version definition table */ 551 #define DT_VERDEFNUM 0x6ffffffd /* number of version definitions */ 552 #define DT_VERNEED 0x6ffffffe /* table with needed versions */ 553 #define DT_VERNEEDNUM 0x6fffffff /* number of needed versions */ 554 555 #define DT_LOPROC 0x70000000 556 #define DT_HIPROC 0x7fffffff 557 558 /* DT_FLAGS values */ 559 #define DF_ORIGIN 0x01 560 #define DF_SYMBOLIC 0x02 561 #define DF_TEXTREL 0x04 562 #define DF_BIND_NOW 0x08 563 #define DF_STATIC_TLS 0x10 564 565 566 /* version definition section */ 567 568 typedef struct { 569 Elf32_Half vd_version; /* version revision */ 570 Elf32_Half vd_flags; /* version information flags */ 571 Elf32_Half vd_ndx; /* version index as specified in the 572 symbol version table */ 573 Elf32_Half vd_cnt; /* number of associated verdaux entries */ 574 Elf32_Word vd_hash; /* version name hash value */ 575 Elf32_Word vd_aux; /* byte offset to verdaux array */ 576 Elf32_Word vd_next; /* byte offset to next verdef entry */ 577 } Elf32_Verdef; 578 579 typedef struct { 580 Elf64_Half vd_version; /* version revision */ 581 Elf64_Half vd_flags; /* version information flags */ 582 Elf64_Half vd_ndx; /* version index as specified in the 583 symbol version table */ 584 Elf64_Half vd_cnt; /* number of associated verdaux entries */ 585 Elf64_Word vd_hash; /* version name hash value */ 586 Elf64_Word vd_aux; /* byte offset to verdaux array */ 587 Elf64_Word vd_next; /* byte offset to next verdef entry */ 588 } Elf64_Verdef; 589 590 /* values for vd_version (version revision) */ 591 #define VER_DEF_NONE 0 /* no version */ 592 #define VER_DEF_CURRENT 1 /* current version */ 593 #define VER_DEF_NUM 2 /* given version number */ 594 595 /* values for vd_flags (version information flags) */ 596 #define VER_FLG_BASE 0x1 /* version definition of file itself */ 597 #define VER_FLG_WEAK 0x2 /* weak version identifier */ 598 599 /* values for versym symbol index */ 600 #define VER_NDX_LOCAL 0 /* symbol is local */ 601 #define VER_NDX_GLOBAL 1 /* symbol is global/unversioned */ 602 #define VER_NDX_INITIAL 2 /* initial version -- that's the one given 603 to symbols when a library becomes 604 versioned; handled by the linker (and 605 runtime loader) similar to 606 VER_NDX_GLOBAL */ 607 #define VER_NDX_LORESERVE 0xff00 /* beginning of reserved entries */ 608 #define VER_NDX_ELIMINATE 0xff01 /* symbol is to be eliminated */ 609 610 #define VER_NDX_FLAG_HIDDEN 0x8000 /* flag: version is hidden */ 611 #define VER_NDX_MASK 0x7fff /* mask to get the actual version index */ 612 #define VER_NDX(x) ((x) & VER_NDX_MASK) 613 614 615 /* auxiliary version information */ 616 617 typedef struct { 618 Elf32_Word vda_name; /* string table offset to version or dependency 619 name */ 620 Elf32_Word vda_next; /* byte offset to next verdaux entry */ 621 } Elf32_Verdaux; 622 623 typedef struct { 624 Elf64_Word vda_name; /* string table offset to version or dependency 625 name */ 626 Elf64_Word vda_next; /* byte offset to next verdaux entry */ 627 } Elf64_Verdaux; 628 629 630 /* version dependency section */ 631 632 typedef struct { 633 Elf32_Half vn_version; /* version of structure */ 634 Elf32_Half vn_cnt; /* number of associated vernaux entries */ 635 Elf32_Word vn_file; /* byte offset to file name for this 636 dependency */ 637 Elf32_Word vn_aux; /* byte offset to vernaux array */ 638 Elf32_Word vn_next; /* byte offset to next verneed entry */ 639 } Elf32_Verneed; 640 641 typedef struct { 642 Elf64_Half vn_version; /* version of structure */ 643 Elf64_Half vn_cnt; /* number of associated vernaux entries */ 644 Elf64_Word vn_file; /* byte offset to file name for this 645 dependency */ 646 Elf64_Word vn_aux; /* byte offset to vernaux array */ 647 Elf64_Word vn_next; /* byte offset to next verneed entry */ 648 } Elf64_Verneed; 649 650 /* values for vn_version (version revision) */ 651 #define VER_NEED_NONE 0 /* no version */ 652 #define VER_NEED_CURRENT 1 /* current version */ 653 #define VER_NEED_NUM 2 /* given version number */ 654 655 656 /* auxiliary needed version information */ 657 658 typedef struct { 659 Elf32_Word vna_hash; /* dependency name hash value */ 660 Elf32_Half vna_flags; /* dependency specific information flags */ 661 Elf32_Half vna_other; /* version index as specified in the symbol 662 version table */ 663 Elf32_Word vna_name; /* string table offset to dependency name */ 664 Elf32_Word vna_next; /* byte offset to next vernaux entry */ 665 } Elf32_Vernaux; 666 667 typedef struct { 668 Elf64_Word vna_hash; /* dependency name hash value */ 669 Elf64_Half vna_flags; /* dependency specific information flags */ 670 Elf64_Half vna_other; /* version index as specified in the symbol 671 version table */ 672 Elf64_Word vna_name; /* string table offset to dependency name */ 673 Elf64_Word vna_next; /* byte offset to next vernaux entry */ 674 } Elf64_Vernaux; 675 676 /* values for vna_flags */ 677 #define VER_FLG_WEAK 0x2 /* weak version identifier */ 678 679 680 /*** core files ***/ 681 682 /* note section header */ 683 684 typedef struct { 685 Elf32_Word n_namesz; /* length of the note's name */ 686 Elf32_Word n_descsz; /* length of the note's descriptor */ 687 Elf32_Word n_type; /* note type */ 688 } Elf32_Nhdr; 689 690 typedef struct { 691 Elf64_Word n_namesz; /* length of the note's name */ 692 Elf64_Word n_descsz; /* length of the note's descriptor */ 693 Elf64_Word n_type; /* note type */ 694 } Elf64_Nhdr; 695 696 /* values for note name */ 697 #define ELF_NOTE_CORE "CORE" 698 #define ELF_NOTE_HAIKU "Haiku" 699 700 /* values for note type (n_type) */ 701 /* ELF_NOTE_CORE/... */ 702 #define NT_FILE 0x46494c45 /* mapped files */ 703 704 /* ELF_NOTE_HAIKU/... */ 705 #define NT_TEAM 0x7465616d /* team */ 706 #define NT_AREAS 0x61726561 /* areas */ 707 #define NT_IMAGES 0x696d6167 /* images */ 708 #define NT_THREADS 0x74687264 /* threads */ 709 #define NT_SYMBOLS 0x73796d73 /* symbols */ 710 711 /* NT_TEAM: uint32 entrySize; Elf32_Note_Team; char[] args */ 712 typedef struct { 713 int32 nt_id; /* team ID */ 714 int32 nt_uid; /* team owner ID */ 715 int32 nt_gid; /* team group ID */ 716 } Elf32_Note_Team; 717 718 typedef Elf32_Note_Team Elf64_Note_Team; 719 720 /* NT_AREAS: 721 * uint32 count; 722 * uint32 entrySize; 723 * Elf32_Note_Area_Entry[count]; 724 * char[] names 725 */ 726 typedef struct { 727 int32 na_id; /* area ID */ 728 uint32 na_lock; /* lock type (B_NO_LOCK, ...) */ 729 uint32 na_protection; /* protection (B_READ_AREA | ...) */ 730 uint32 na_base; /* area base address */ 731 uint32 na_size; /* area size */ 732 uint32 na_ram_size; /* physical memory used */ 733 } Elf32_Note_Area_Entry; 734 735 /* NT_AREAS: 736 * uint32 count; 737 * uint32 entrySize; 738 * Elf64_Note_Area_Entry[count]; 739 * char[] names 740 */ 741 typedef struct { 742 int32 na_id; /* area ID */ 743 uint32 na_lock; /* lock type (B_NO_LOCK, ...) */ 744 uint32 na_protection; /* protection (B_READ_AREA | ...) */ 745 uint32 na_pad1; 746 uint64 na_base; /* area base address */ 747 uint64 na_size; /* area size */ 748 uint64 na_ram_size; /* physical memory used */ 749 } Elf64_Note_Area_Entry; 750 751 /* NT_IMAGES: 752 * uint32 count; 753 * uint32 entrySize; 754 * Elf32_Note_Image_Entry[count]; 755 * char[] names 756 */ 757 typedef struct { 758 int32 ni_id; /* image ID */ 759 int32 ni_type; /* image type (B_APP_IMAGE, ...) */ 760 uint32 ni_init_routine; /* address of init function */ 761 uint32 ni_term_routine; /* address of termination function */ 762 int32 ni_device; /* device ID of mapped file */ 763 int64 ni_node; /* node ID of mapped file */ 764 uint32 ni_text_base; /* base address of text segment */ 765 uint32 ni_text_size; /* size of text segment */ 766 int32 ni_text_delta; /* delta of the text segment relative to 767 load address specified in the ELF file */ 768 uint32 ni_data_base; /* base address of data segment */ 769 uint32 ni_data_size; /* size of data segment */ 770 uint32 ni_symbol_table; /* address of dynamic symbol table */ 771 uint32 ni_symbol_hash; /* address of dynamic symbol hash */ 772 uint32 ni_string_table; /* address of dynamic string table */ 773 } Elf32_Note_Image_Entry; 774 775 /* NT_IMAGES: 776 * uint32 count; 777 * uint32 entrySize; 778 * Elf64_Note_Image_Entry[count]; 779 * char[] names 780 */ 781 typedef struct { 782 int32 ni_id; /* image ID */ 783 int32 ni_type; /* image type (B_APP_IMAGE, ...) */ 784 uint64 ni_init_routine; /* address of init function */ 785 uint64 ni_term_routine; /* address of termination function */ 786 uint32 ni_pad1; 787 int32 ni_device; /* device ID of mapped file */ 788 int64 ni_node; /* node ID of mapped file */ 789 uint64 ni_text_base; /* base address of text segment */ 790 uint64 ni_text_size; /* size of text segment */ 791 int64 ni_text_delta; /* delta of the text segment relative to 792 load address specified in the ELF file */ 793 uint64 ni_data_base; /* base address of data segment */ 794 uint64 ni_data_size; /* size of data segment */ 795 uint64 ni_symbol_table; /* address of dynamic symbol table */ 796 uint64 ni_symbol_hash; /* address of dynamic symbol hash */ 797 uint64 ni_string_table; /* address of dynamic string table */ 798 } Elf64_Note_Image_Entry; 799 800 /* NT_THREADS: 801 * uint32 count; 802 * uint32 entrySize; 803 * uint32 cpuStateSize; 804 * {Elf32_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count]; 805 * char[] names 806 */ 807 typedef struct { 808 int32 nth_id; /* thread ID */ 809 int32 nth_state; /* thread state (B_THREAD_RUNNING, ...) */ 810 int32 nth_priority; /* thread priority */ 811 uint32 nth_stack_base; /* thread stack base address */ 812 uint32 nth_stack_end; /* thread stack end address */ 813 } Elf32_Note_Thread_Entry; 814 815 /* NT_THREADS: 816 * uint32 count; 817 * uint32 entrySize; 818 * uint32 cpuStateSize; 819 * {Elf64_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count]; 820 * char[] names 821 */ 822 typedef struct { 823 int32 nth_id; /* thread ID */ 824 int32 nth_state; /* thread state (B_THREAD_RUNNING, ...) */ 825 int32 nth_priority; /* thread priority */ 826 uint32 nth_pad1; 827 uint64 nth_stack_base; /* thread stack base address */ 828 uint64 nth_stack_end; /* thread stack end address */ 829 } Elf64_Note_Thread_Entry; 830 831 /* NT_SYMBOLS: 832 * int32 imageId; 833 * uint32 symbolCount; 834 * uint32 entrySize; 835 * Elf{32,64}_Sym[count]; 836 * char[] strings 837 */ 838 839 840 /*** inline functions ***/ 841 842 #ifdef __cplusplus 843 844 inline bool 845 Elf32_Ehdr::IsHostEndian() const 846 { 847 #if B_HOST_IS_LENDIAN 848 return e_ident[EI_DATA] == ELFDATA2LSB; 849 #elif B_HOST_IS_BENDIAN 850 return e_ident[EI_DATA] == ELFDATA2MSB; 851 #endif 852 } 853 854 855 inline bool 856 Elf64_Ehdr::IsHostEndian() const 857 { 858 #if B_HOST_IS_LENDIAN 859 return e_ident[EI_DATA] == ELFDATA2LSB; 860 #elif B_HOST_IS_BENDIAN 861 return e_ident[EI_DATA] == ELFDATA2MSB; 862 #endif 863 } 864 865 866 inline bool 867 Elf32_Phdr::IsReadWrite() const 868 { 869 return !(~p_flags & (PF_READ | PF_WRITE)); 870 } 871 872 873 inline bool 874 Elf32_Phdr::IsExecutable() const 875 { 876 return (p_flags & PF_EXECUTE) != 0; 877 } 878 879 880 inline bool 881 Elf64_Phdr::IsReadWrite() const 882 { 883 return !(~p_flags & (PF_READ | PF_WRITE)); 884 } 885 886 887 inline bool 888 Elf64_Phdr::IsExecutable() const 889 { 890 return (p_flags & PF_EXECUTE) != 0; 891 } 892 893 894 inline uint8 895 Elf32_Sym::Bind() const 896 { 897 return ELF32_ST_BIND(st_info); 898 } 899 900 901 inline uint8 902 Elf32_Sym::Type() const 903 { 904 return ELF32_ST_TYPE(st_info); 905 } 906 907 908 inline void 909 Elf32_Sym::SetInfo(uint8 bind, uint8 type) 910 { 911 st_info = ELF32_ST_INFO(bind, type); 912 } 913 914 915 inline uint8 916 Elf64_Sym::Bind() const 917 { 918 return ELF64_ST_BIND(st_info); 919 } 920 921 922 inline uint8 923 Elf64_Sym::Type() const 924 { 925 return ELF64_ST_TYPE(st_info); 926 } 927 928 929 inline void 930 Elf64_Sym::SetInfo(uint8 bind, uint8 type) 931 { 932 st_info = ELF64_ST_INFO(bind, type); 933 } 934 935 936 inline uint8 937 Elf32_Rel::SymbolIndex() const 938 { 939 return ELF32_R_SYM(r_info); 940 } 941 942 943 inline uint8 944 Elf32_Rel::Type() const 945 { 946 return ELF32_R_TYPE(r_info); 947 } 948 949 950 inline uint8 951 Elf64_Rel::SymbolIndex() const 952 { 953 return ELF64_R_SYM(r_info); 954 } 955 956 957 inline uint8 958 Elf64_Rel::Type() const 959 { 960 return ELF64_R_TYPE(r_info); 961 } 962 963 #endif /* __cplusplus */ 964 965 966 #endif /* _ELF_H */ 967