1 // elfcpp.h -- main header file for elfcpp -*- C++ -*- 2 3 // Copyright (C) 2006-2015 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant@google.com>. 5 6 // This file is part of elfcpp. 7 8 // This program is free software; you can redistribute it and/or 9 // modify it under the terms of the GNU Library General Public License 10 // as published by the Free Software Foundation; either version 2, or 11 // (at your option) any later version. 12 13 // In addition to the permissions in the GNU Library General Public 14 // License, the Free Software Foundation gives you unlimited 15 // permission to link the compiled version of this file into 16 // combinations with other programs, and to distribute those 17 // combinations without any restriction coming from the use of this 18 // file. (The Library Public License restrictions do apply in other 19 // respects; for example, they cover modification of the file, and 20 // distribution when not linked into a combined executable.) 21 22 // This program is distributed in the hope that it will be useful, but 23 // WITHOUT ANY WARRANTY; without even the implied warranty of 24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 // Library General Public License for more details. 26 27 // You should have received a copy of the GNU Library General Public 28 // License along with this program; if not, write to the Free Software 29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 30 // 02110-1301, USA. 31 32 // This is the external interface for elfcpp. 33 34 #ifndef ELFCPP_H 35 #define ELFCPP_H 36 37 #include "elfcpp_swap.h" 38 39 #include <stdint.h> 40 41 namespace elfcpp 42 { 43 44 // Basic ELF types. 45 46 // These types are always the same size. 47 48 typedef uint16_t Elf_Half; 49 typedef uint32_t Elf_Word; 50 typedef int32_t Elf_Sword; 51 typedef uint64_t Elf_Xword; 52 typedef int64_t Elf_Sxword; 53 54 // These types vary in size depending on the ELF file class. The 55 // template parameter should be 32 or 64. 56 57 template<int size> 58 struct Elf_types; 59 60 template<> 61 struct Elf_types<32> 62 { 63 typedef uint32_t Elf_Addr; 64 typedef uint32_t Elf_Off; 65 typedef uint32_t Elf_WXword; 66 typedef int32_t Elf_Swxword; 67 }; 68 69 template<> 70 struct Elf_types<64> 71 { 72 typedef uint64_t Elf_Addr; 73 typedef uint64_t Elf_Off; 74 typedef uint64_t Elf_WXword; 75 typedef int64_t Elf_Swxword; 76 }; 77 78 // Offsets within the Ehdr e_ident field. 79 80 const int EI_MAG0 = 0; 81 const int EI_MAG1 = 1; 82 const int EI_MAG2 = 2; 83 const int EI_MAG3 = 3; 84 const int EI_CLASS = 4; 85 const int EI_DATA = 5; 86 const int EI_VERSION = 6; 87 const int EI_OSABI = 7; 88 const int EI_ABIVERSION = 8; 89 const int EI_PAD = 9; 90 const int EI_NIDENT = 16; 91 92 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3]. 93 94 const int ELFMAG0 = 0x7f; 95 const int ELFMAG1 = 'E'; 96 const int ELFMAG2 = 'L'; 97 const int ELFMAG3 = 'F'; 98 99 // The valid values found in Ehdr e_ident[EI_CLASS]. 100 101 enum 102 { 103 ELFCLASSNONE = 0, 104 ELFCLASS32 = 1, 105 ELFCLASS64 = 2 106 }; 107 108 // The valid values found in Ehdr e_ident[EI_DATA]. 109 110 enum 111 { 112 ELFDATANONE = 0, 113 ELFDATA2LSB = 1, 114 ELFDATA2MSB = 2 115 }; 116 117 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version. 118 119 enum 120 { 121 EV_NONE = 0, 122 EV_CURRENT = 1 123 }; 124 125 // The valid values found in Ehdr e_ident[EI_OSABI]. 126 127 enum ELFOSABI 128 { 129 ELFOSABI_NONE = 0, 130 ELFOSABI_HPUX = 1, 131 ELFOSABI_NETBSD = 2, 132 ELFOSABI_GNU = 3, 133 // ELFOSABI_LINUX is an alias for ELFOSABI_GNU. 134 ELFOSABI_LINUX = 3, 135 ELFOSABI_SOLARIS = 6, 136 ELFOSABI_AIX = 7, 137 ELFOSABI_IRIX = 8, 138 ELFOSABI_FREEBSD = 9, 139 ELFOSABI_TRU64 = 10, 140 ELFOSABI_MODESTO = 11, 141 ELFOSABI_OPENBSD = 12, 142 ELFOSABI_OPENVMS = 13, 143 ELFOSABI_NSK = 14, 144 ELFOSABI_AROS = 15, 145 // A GNU extension for the ARM. 146 ELFOSABI_ARM = 97, 147 // A GNU extension for the MSP. 148 ELFOSABI_STANDALONE = 255 149 }; 150 151 // The valid values found in the Ehdr e_type field. 152 153 enum ET 154 { 155 ET_NONE = 0, 156 ET_REL = 1, 157 ET_EXEC = 2, 158 ET_DYN = 3, 159 ET_CORE = 4, 160 ET_LOOS = 0xfe00, 161 ET_HIOS = 0xfeff, 162 ET_LOPROC = 0xff00, 163 ET_HIPROC = 0xffff 164 }; 165 166 // The valid values found in the Ehdr e_machine field. 167 168 enum EM 169 { 170 EM_NONE = 0, 171 EM_M32 = 1, 172 EM_SPARC = 2, 173 EM_386 = 3, 174 EM_68K = 4, 175 EM_88K = 5, 176 EM_IAMCU = 6, 177 EM_860 = 7, 178 EM_MIPS = 8, 179 EM_S370 = 9, 180 EM_MIPS_RS3_LE = 10, 181 // 11 was the old Sparc V9 ABI. 182 // 12 through 14 are reserved. 183 EM_PARISC = 15, 184 // 16 is reserved. 185 // Some old PowerPC object files use 17. 186 EM_VPP500 = 17, 187 EM_SPARC32PLUS = 18, 188 EM_960 = 19, 189 EM_PPC = 20, 190 EM_PPC64 = 21, 191 EM_S390 = 22, 192 // 23 through 35 are served. 193 EM_V800 = 36, 194 EM_FR20 = 37, 195 EM_RH32 = 38, 196 EM_RCE = 39, 197 EM_ARM = 40, 198 EM_ALPHA = 41, 199 EM_SH = 42, 200 EM_SPARCV9 = 43, 201 EM_TRICORE = 44, 202 EM_ARC = 45, 203 EM_H8_300 = 46, 204 EM_H8_300H = 47, 205 EM_H8S = 48, 206 EM_H8_500 = 49, 207 EM_IA_64 = 50, 208 EM_MIPS_X = 51, 209 EM_COLDFIRE = 52, 210 EM_68HC12 = 53, 211 EM_MMA = 54, 212 EM_PCP = 55, 213 EM_NCPU = 56, 214 EM_NDR1 = 57, 215 EM_STARCORE = 58, 216 EM_ME16 = 59, 217 EM_ST100 = 60, 218 EM_TINYJ = 61, 219 EM_X86_64 = 62, 220 EM_PDSP = 63, 221 EM_PDP10 = 64, 222 EM_PDP11 = 65, 223 EM_FX66 = 66, 224 EM_ST9PLUS = 67, 225 EM_ST7 = 68, 226 EM_68HC16 = 69, 227 EM_68HC11 = 70, 228 EM_68HC08 = 71, 229 EM_68HC05 = 72, 230 EM_SVX = 73, 231 EM_ST19 = 74, 232 EM_VAX = 75, 233 EM_CRIS = 76, 234 EM_JAVELIN = 77, 235 EM_FIREPATH = 78, 236 EM_ZSP = 79, 237 EM_MMIX = 80, 238 EM_HUANY = 81, 239 EM_PRISM = 82, 240 EM_AVR = 83, 241 EM_FR30 = 84, 242 EM_D10V = 85, 243 EM_D30V = 86, 244 EM_V850 = 87, 245 EM_M32R = 88, 246 EM_MN10300 = 89, 247 EM_MN10200 = 90, 248 EM_PJ = 91, 249 EM_OR1K = 92, 250 EM_ARC_A5 = 93, 251 EM_XTENSA = 94, 252 EM_VIDEOCORE = 95, 253 EM_TMM_GPP = 96, 254 EM_NS32K = 97, 255 EM_TPC = 98, 256 // Some old picoJava object files use 99 (EM_PJ is correct). 257 EM_SNP1K = 99, 258 EM_ST200 = 100, 259 EM_IP2K = 101, 260 EM_MAX = 102, 261 EM_CR = 103, 262 EM_F2MC16 = 104, 263 EM_MSP430 = 105, 264 EM_BLACKFIN = 106, 265 EM_SE_C33 = 107, 266 EM_SEP = 108, 267 EM_ARCA = 109, 268 EM_UNICORE = 110, 269 EM_ALTERA_NIOS2 = 113, 270 EM_CRX = 114, 271 EM_AARCH64 = 183, 272 EM_TILEGX = 191, 273 // The Morph MT. 274 EM_MT = 0x2530, 275 // DLX. 276 EM_DLX = 0x5aa5, 277 // FRV. 278 EM_FRV = 0x5441, 279 // Infineon Technologies 16-bit microcontroller with C166-V2 core. 280 EM_X16X = 0x4688, 281 // Xstorym16 282 EM_XSTORMY16 = 0xad45, 283 // Renesas M32C 284 EM_M32C = 0xfeb0, 285 // Vitesse IQ2000 286 EM_IQ2000 = 0xfeba, 287 // NIOS 288 EM_NIOS32 = 0xfebb 289 // Old AVR objects used 0x1057 (EM_AVR is correct). 290 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct). 291 // Old FR30 objects used 0x3330 (EM_FR30 is correct). 292 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OR1K is correct). 293 // Old D10V objects used 0x7650 (EM_D10V is correct). 294 // Old D30V objects used 0x7676 (EM_D30V is correct). 295 // Old IP2X objects used 0x8217 (EM_IP2K is correct). 296 // Old PowerPC objects used 0x9025 (EM_PPC is correct). 297 // Old Alpha objects used 0x9026 (EM_ALPHA is correct). 298 // Old M32R objects used 0x9041 (EM_M32R is correct). 299 // Old V850 objects used 0x9080 (EM_V850 is correct). 300 // Old S/390 objects used 0xa390 (EM_S390 is correct). 301 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct). 302 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct). 303 // Old MN10200 objects used 0xdead (EM_MN10200 is correct). 304 }; 305 306 // A special value found in the Ehdr e_phnum field. 307 308 enum 309 { 310 // Number of program segments stored in sh_info field of first 311 // section headre. 312 PN_XNUM = 0xffff 313 }; 314 315 // Special section indices. 316 317 enum 318 { 319 SHN_UNDEF = 0, 320 SHN_LORESERVE = 0xff00, 321 SHN_LOPROC = 0xff00, 322 SHN_HIPROC = 0xff1f, 323 SHN_LOOS = 0xff20, 324 SHN_HIOS = 0xff3f, 325 SHN_ABS = 0xfff1, 326 SHN_COMMON = 0xfff2, 327 SHN_XINDEX = 0xffff, 328 SHN_HIRESERVE = 0xffff, 329 330 // Provide for initial and final section ordering in conjunction 331 // with the SHF_LINK_ORDER and SHF_ORDERED section flags. 332 SHN_BEFORE = 0xff00, 333 SHN_AFTER = 0xff01, 334 335 // x86_64 specific large common symbol. 336 SHN_X86_64_LCOMMON = 0xff02 337 }; 338 339 // The valid values found in the Shdr sh_type field. 340 341 enum SHT 342 { 343 SHT_NULL = 0, 344 SHT_PROGBITS = 1, 345 SHT_SYMTAB = 2, 346 SHT_STRTAB = 3, 347 SHT_RELA = 4, 348 SHT_HASH = 5, 349 SHT_DYNAMIC = 6, 350 SHT_NOTE = 7, 351 SHT_NOBITS = 8, 352 SHT_REL = 9, 353 SHT_SHLIB = 10, 354 SHT_DYNSYM = 11, 355 SHT_INIT_ARRAY = 14, 356 SHT_FINI_ARRAY = 15, 357 SHT_PREINIT_ARRAY = 16, 358 SHT_GROUP = 17, 359 SHT_SYMTAB_SHNDX = 18, 360 SHT_LOOS = 0x60000000, 361 SHT_HIOS = 0x6fffffff, 362 SHT_LOPROC = 0x70000000, 363 SHT_HIPROC = 0x7fffffff, 364 SHT_LOUSER = 0x80000000, 365 SHT_HIUSER = 0xffffffff, 366 // The remaining values are not in the standard. 367 // Incremental build data. 368 SHT_GNU_INCREMENTAL_INPUTS = 0x6fff4700, 369 SHT_GNU_INCREMENTAL_SYMTAB = 0x6fff4701, 370 SHT_GNU_INCREMENTAL_RELOCS = 0x6fff4702, 371 SHT_GNU_INCREMENTAL_GOT_PLT = 0x6fff4703, 372 // Object attributes. 373 SHT_GNU_ATTRIBUTES = 0x6ffffff5, 374 // GNU style dynamic hash table. 375 SHT_GNU_HASH = 0x6ffffff6, 376 // List of prelink dependencies. 377 SHT_GNU_LIBLIST = 0x6ffffff7, 378 // Versions defined by file. 379 SHT_SUNW_verdef = 0x6ffffffd, 380 SHT_GNU_verdef = 0x6ffffffd, 381 // Versions needed by file. 382 SHT_SUNW_verneed = 0x6ffffffe, 383 SHT_GNU_verneed = 0x6ffffffe, 384 // Symbol versions, 385 SHT_SUNW_versym = 0x6fffffff, 386 SHT_GNU_versym = 0x6fffffff, 387 388 SHT_SPARC_GOTDATA = 0x70000000, 389 390 // ARM-specific section types. 391 // Exception Index table. 392 SHT_ARM_EXIDX = 0x70000001, 393 // BPABI DLL dynamic linking pre-emption map. 394 SHT_ARM_PREEMPTMAP = 0x70000002, 395 // Object file compatibility attributes. 396 SHT_ARM_ATTRIBUTES = 0x70000003, 397 // Support for debugging overlaid programs. 398 SHT_ARM_DEBUGOVERLAY = 0x70000004, 399 SHT_ARM_OVERLAYSECTION = 0x70000005, 400 401 // x86_64 unwind information. 402 SHT_X86_64_UNWIND = 0x70000001, 403 404 // MIPS-specific section types. 405 // Section contains register usage information. 406 SHT_MIPS_REGINFO = 0x70000006, 407 // Section contains miscellaneous options. 408 SHT_MIPS_OPTIONS = 0x7000000d, 409 410 // AARCH64-specific section type. 411 SHT_AARCH64_ATTRIBUTES = 0x70000003, 412 413 // Link editor is to sort the entries in this section based on the 414 // address specified in the associated symbol table entry. 415 SHT_ORDERED = 0x7fffffff 416 }; 417 418 // The valid bit flags found in the Shdr sh_flags field. 419 420 enum SHF 421 { 422 SHF_WRITE = 0x1, 423 SHF_ALLOC = 0x2, 424 SHF_EXECINSTR = 0x4, 425 SHF_MERGE = 0x10, 426 SHF_STRINGS = 0x20, 427 SHF_INFO_LINK = 0x40, 428 SHF_LINK_ORDER = 0x80, 429 SHF_OS_NONCONFORMING = 0x100, 430 SHF_GROUP = 0x200, 431 SHF_TLS = 0x400, 432 SHF_COMPRESSED = 0x800, 433 SHF_MASKOS = 0x0ff00000, 434 SHF_MASKPROC = 0xf0000000, 435 436 // Indicates this section requires ordering in relation to 437 // other sections of the same type. Ordered sections are 438 // combined within the section pointed to by the sh_link entry. 439 // The sh_info values SHN_BEFORE and SHN_AFTER imply that the 440 // sorted section is to precede or follow, respectively, all 441 // other sections in the set being ordered. 442 SHF_ORDERED = 0x40000000, 443 // This section is excluded from input to the link-edit of an 444 // executable or shared object. This flag is ignored if SHF_ALLOC 445 // is also set, or if relocations exist against the section. 446 SHF_EXCLUDE = 0x80000000, 447 448 // Section with data that is GP relative addressable. 449 SHF_MIPS_GPREL = 0x10000000, 450 451 // x86_64 specific large section. 452 SHF_X86_64_LARGE = 0x10000000 453 }; 454 455 // Values which appear in the first Elf_WXword of the section data 456 // of a SHF_COMPRESSED section. 457 enum 458 { 459 ELFCOMPRESS_ZLIB = 1, 460 ELFCOMPRESS_LOOS = 0x60000000, 461 ELFCOMPRESS_HIOS = 0x6fffffff, 462 ELFCOMPRESS_LOPROC = 0x70000000, 463 ELFCOMPRESS_HIPROC = 0x7fffffff, 464 }; 465 466 // Bit flags which appear in the first 32-bit word of the section data 467 // of a SHT_GROUP section. 468 469 enum 470 { 471 GRP_COMDAT = 0x1, 472 GRP_MASKOS = 0x0ff00000, 473 GRP_MASKPROC = 0xf0000000 474 }; 475 476 // The valid values found in the Phdr p_type field. 477 478 enum PT 479 { 480 PT_NULL = 0, 481 PT_LOAD = 1, 482 PT_DYNAMIC = 2, 483 PT_INTERP = 3, 484 PT_NOTE = 4, 485 PT_SHLIB = 5, 486 PT_PHDR = 6, 487 PT_TLS = 7, 488 PT_LOOS = 0x60000000, 489 PT_HIOS = 0x6fffffff, 490 PT_LOPROC = 0x70000000, 491 PT_HIPROC = 0x7fffffff, 492 // The remaining values are not in the standard. 493 // Frame unwind information. 494 PT_GNU_EH_FRAME = 0x6474e550, 495 PT_SUNW_EH_FRAME = 0x6474e550, 496 // Stack flags. 497 PT_GNU_STACK = 0x6474e551, 498 // Read only after relocation. 499 PT_GNU_RELRO = 0x6474e552, 500 // Platform architecture compatibility information 501 PT_ARM_ARCHEXT = 0x70000000, 502 // Exception unwind tables 503 PT_ARM_EXIDX = 0x70000001, 504 // Register usage information. Identifies one .reginfo section. 505 PT_MIPS_REGINFO =0x70000000, 506 // Runtime procedure table. 507 PT_MIPS_RTPROC = 0x70000001, 508 // .MIPS.options section. 509 PT_MIPS_OPTIONS = 0x70000002, 510 // .MIPS.abiflags section. 511 PT_MIPS_ABIFLAGS = 0x70000003, 512 // Platform architecture compatibility information 513 PT_AARCH64_ARCHEXT = 0x70000000, 514 // Exception unwind tables 515 PT_AARCH64_UNWIND = 0x70000001 516 }; 517 518 // The valid bit flags found in the Phdr p_flags field. 519 520 enum PF 521 { 522 PF_X = 0x1, 523 PF_W = 0x2, 524 PF_R = 0x4, 525 PF_MASKOS = 0x0ff00000, 526 PF_MASKPROC = 0xf0000000 527 }; 528 529 // Symbol binding from Sym st_info field. 530 531 enum STB 532 { 533 STB_LOCAL = 0, 534 STB_GLOBAL = 1, 535 STB_WEAK = 2, 536 STB_LOOS = 10, 537 STB_GNU_UNIQUE = 10, 538 STB_HIOS = 12, 539 STB_LOPROC = 13, 540 STB_HIPROC = 15 541 }; 542 543 // Symbol types from Sym st_info field. 544 545 enum STT 546 { 547 STT_NOTYPE = 0, 548 STT_OBJECT = 1, 549 STT_FUNC = 2, 550 STT_SECTION = 3, 551 STT_FILE = 4, 552 STT_COMMON = 5, 553 STT_TLS = 6, 554 555 // GNU extension: symbol value points to a function which is called 556 // at runtime to determine the final value of the symbol. 557 STT_GNU_IFUNC = 10, 558 559 STT_LOOS = 10, 560 STT_HIOS = 12, 561 STT_LOPROC = 13, 562 STT_HIPROC = 15, 563 564 // The section type that must be used for register symbols on 565 // Sparc. These symbols initialize a global register. 566 STT_SPARC_REGISTER = 13, 567 568 // ARM: a THUMB function. This is not defined in ARM ELF Specification but 569 // used by the GNU tool-chain. 570 STT_ARM_TFUNC = 13 571 }; 572 573 inline STB 574 elf_st_bind(unsigned char info) 575 { 576 return static_cast<STB>(info >> 4); 577 } 578 579 inline STT 580 elf_st_type(unsigned char info) 581 { 582 return static_cast<STT>(info & 0xf); 583 } 584 585 inline unsigned char 586 elf_st_info(STB bind, STT type) 587 { 588 return ((static_cast<unsigned char>(bind) << 4) 589 + (static_cast<unsigned char>(type) & 0xf)); 590 } 591 592 // Symbol visibility from Sym st_other field. 593 594 enum STV 595 { 596 STV_DEFAULT = 0, 597 STV_INTERNAL = 1, 598 STV_HIDDEN = 2, 599 STV_PROTECTED = 3 600 }; 601 602 inline STV 603 elf_st_visibility(unsigned char other) 604 { 605 return static_cast<STV>(other & 0x3); 606 } 607 608 inline unsigned char 609 elf_st_nonvis(unsigned char other) 610 { 611 return static_cast<STV>(other >> 2); 612 } 613 614 inline unsigned char 615 elf_st_other(STV vis, unsigned char nonvis) 616 { 617 return ((nonvis << 2) 618 + (static_cast<unsigned char>(vis) & 3)); 619 } 620 621 // Reloc information from Rel/Rela r_info field. 622 623 template<int size> 624 unsigned int 625 elf_r_sym(typename Elf_types<size>::Elf_WXword); 626 627 template<> 628 inline unsigned int 629 elf_r_sym<32>(Elf_Word v) 630 { 631 return v >> 8; 632 } 633 634 template<> 635 inline unsigned int 636 elf_r_sym<64>(Elf_Xword v) 637 { 638 return v >> 32; 639 } 640 641 template<int size> 642 unsigned int 643 elf_r_type(typename Elf_types<size>::Elf_WXword); 644 645 template<> 646 inline unsigned int 647 elf_r_type<32>(Elf_Word v) 648 { 649 return v & 0xff; 650 } 651 652 template<> 653 inline unsigned int 654 elf_r_type<64>(Elf_Xword v) 655 { 656 return v & 0xffffffff; 657 } 658 659 template<int size> 660 typename Elf_types<size>::Elf_WXword 661 elf_r_info(unsigned int s, unsigned int t); 662 663 template<> 664 inline Elf_Word 665 elf_r_info<32>(unsigned int s, unsigned int t) 666 { 667 return (s << 8) + (t & 0xff); 668 } 669 670 template<> 671 inline Elf_Xword 672 elf_r_info<64>(unsigned int s, unsigned int t) 673 { 674 return (static_cast<Elf_Xword>(s) << 32) + (t & 0xffffffff); 675 } 676 677 // Dynamic tags found in the PT_DYNAMIC segment. 678 679 enum DT 680 { 681 DT_NULL = 0, 682 DT_NEEDED = 1, 683 DT_PLTRELSZ = 2, 684 DT_PLTGOT = 3, 685 DT_HASH = 4, 686 DT_STRTAB = 5, 687 DT_SYMTAB = 6, 688 DT_RELA = 7, 689 DT_RELASZ = 8, 690 DT_RELAENT = 9, 691 DT_STRSZ = 10, 692 DT_SYMENT = 11, 693 DT_INIT = 12, 694 DT_FINI = 13, 695 DT_SONAME = 14, 696 DT_RPATH = 15, 697 DT_SYMBOLIC = 16, 698 DT_REL = 17, 699 DT_RELSZ = 18, 700 DT_RELENT = 19, 701 DT_PLTREL = 20, 702 DT_DEBUG = 21, 703 DT_TEXTREL = 22, 704 DT_JMPREL = 23, 705 DT_BIND_NOW = 24, 706 DT_INIT_ARRAY = 25, 707 DT_FINI_ARRAY = 26, 708 DT_INIT_ARRAYSZ = 27, 709 DT_FINI_ARRAYSZ = 28, 710 DT_RUNPATH = 29, 711 DT_FLAGS = 30, 712 713 // This is used to mark a range of dynamic tags. It is not really 714 // a tag value. 715 DT_ENCODING = 32, 716 717 DT_PREINIT_ARRAY = 32, 718 DT_PREINIT_ARRAYSZ = 33, 719 DT_LOOS = 0x6000000d, 720 DT_HIOS = 0x6ffff000, 721 DT_LOPROC = 0x70000000, 722 DT_HIPROC = 0x7fffffff, 723 724 // The remaining values are extensions used by GNU or Solaris. 725 DT_VALRNGLO = 0x6ffffd00, 726 DT_GNU_PRELINKED = 0x6ffffdf5, 727 DT_GNU_CONFLICTSZ = 0x6ffffdf6, 728 DT_GNU_LIBLISTSZ = 0x6ffffdf7, 729 DT_CHECKSUM = 0x6ffffdf8, 730 DT_PLTPADSZ = 0x6ffffdf9, 731 DT_MOVEENT = 0x6ffffdfa, 732 DT_MOVESZ = 0x6ffffdfb, 733 DT_FEATURE = 0x6ffffdfc, 734 DT_POSFLAG_1 = 0x6ffffdfd, 735 DT_SYMINSZ = 0x6ffffdfe, 736 DT_SYMINENT = 0x6ffffdff, 737 DT_VALRNGHI = 0x6ffffdff, 738 739 DT_ADDRRNGLO = 0x6ffffe00, 740 DT_GNU_HASH = 0x6ffffef5, 741 DT_TLSDESC_PLT = 0x6ffffef6, 742 DT_TLSDESC_GOT = 0x6ffffef7, 743 DT_GNU_CONFLICT = 0x6ffffef8, 744 DT_GNU_LIBLIST = 0x6ffffef9, 745 DT_CONFIG = 0x6ffffefa, 746 DT_DEPAUDIT = 0x6ffffefb, 747 DT_AUDIT = 0x6ffffefc, 748 DT_PLTPAD = 0x6ffffefd, 749 DT_MOVETAB = 0x6ffffefe, 750 DT_SYMINFO = 0x6ffffeff, 751 DT_ADDRRNGHI = 0x6ffffeff, 752 753 DT_RELACOUNT = 0x6ffffff9, 754 DT_RELCOUNT = 0x6ffffffa, 755 DT_FLAGS_1 = 0x6ffffffb, 756 DT_VERDEF = 0x6ffffffc, 757 DT_VERDEFNUM = 0x6ffffffd, 758 DT_VERNEED = 0x6ffffffe, 759 DT_VERNEEDNUM = 0x6fffffff, 760 761 DT_VERSYM = 0x6ffffff0, 762 763 // Specify the value of _GLOBAL_OFFSET_TABLE_. 764 DT_PPC_GOT = 0x70000000, 765 766 // Specify the start of the .glink section. 767 DT_PPC64_GLINK = 0x70000000, 768 769 // Specify the start and size of the .opd section. 770 DT_PPC64_OPD = 0x70000001, 771 DT_PPC64_OPDSZ = 0x70000002, 772 773 // The index of an STT_SPARC_REGISTER symbol within the DT_SYMTAB 774 // symbol table. One dynamic entry exists for every STT_SPARC_REGISTER 775 // symbol in the symbol table. 776 DT_SPARC_REGISTER = 0x70000001, 777 778 // MIPS specific dynamic array tags. 779 // 32 bit version number for runtime linker interface. 780 DT_MIPS_RLD_VERSION = 0x70000001, 781 // Time stamp. 782 DT_MIPS_TIME_STAMP = 0x70000002, 783 // Checksum of external strings and common sizes. 784 DT_MIPS_ICHECKSUM = 0x70000003, 785 // Index of version string in string table. 786 DT_MIPS_IVERSION = 0x70000004, 787 // 32 bits of flags. 788 DT_MIPS_FLAGS = 0x70000005, 789 // Base address of the segment. 790 DT_MIPS_BASE_ADDRESS = 0x70000006, 791 // ??? 792 DT_MIPS_MSYM = 0x70000007, 793 // Address of .conflict section. 794 DT_MIPS_CONFLICT = 0x70000008, 795 // Address of .liblist section. 796 DT_MIPS_LIBLIST = 0x70000009, 797 // Number of local global offset table entries. 798 DT_MIPS_LOCAL_GOTNO = 0x7000000a, 799 // Number of entries in the .conflict section. 800 DT_MIPS_CONFLICTNO = 0x7000000b, 801 // Number of entries in the .liblist section. 802 DT_MIPS_LIBLISTNO = 0x70000010, 803 // Number of entries in the .dynsym section. 804 DT_MIPS_SYMTABNO = 0x70000011, 805 // Index of first external dynamic symbol not referenced locally. 806 DT_MIPS_UNREFEXTNO = 0x70000012, 807 // Index of first dynamic symbol in global offset table. 808 DT_MIPS_GOTSYM = 0x70000013, 809 // Number of page table entries in global offset table. 810 DT_MIPS_HIPAGENO = 0x70000014, 811 // Address of run time loader map, used for debugging. 812 DT_MIPS_RLD_MAP = 0x70000016, 813 // Delta C++ class definition. 814 DT_MIPS_DELTA_CLASS = 0x70000017, 815 // Number of entries in DT_MIPS_DELTA_CLASS. 816 DT_MIPS_DELTA_CLASS_NO = 0x70000018, 817 // Delta C++ class instances. 818 DT_MIPS_DELTA_INSTANCE = 0x70000019, 819 // Number of entries in DT_MIPS_DELTA_INSTANCE. 820 DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a, 821 // Delta relocations. 822 DT_MIPS_DELTA_RELOC = 0x7000001b, 823 // Number of entries in DT_MIPS_DELTA_RELOC. 824 DT_MIPS_DELTA_RELOC_NO = 0x7000001c, 825 // Delta symbols that Delta relocations refer to. 826 DT_MIPS_DELTA_SYM = 0x7000001d, 827 // Number of entries in DT_MIPS_DELTA_SYM. 828 DT_MIPS_DELTA_SYM_NO = 0x7000001e, 829 // Delta symbols that hold class declarations. 830 DT_MIPS_DELTA_CLASSSYM = 0x70000020, 831 // Number of entries in DT_MIPS_DELTA_CLASSSYM. 832 DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021, 833 // Flags indicating information about C++ flavor. 834 DT_MIPS_CXX_FLAGS = 0x70000022, 835 // Pixie information (???). 836 DT_MIPS_PIXIE_INIT = 0x70000023, 837 // Address of .MIPS.symlib 838 DT_MIPS_SYMBOL_LIB = 0x70000024, 839 // The GOT index of the first PTE for a segment 840 DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025, 841 // The GOT index of the first PTE for a local symbol 842 DT_MIPS_LOCAL_GOTIDX = 0x70000026, 843 // The GOT index of the first PTE for a hidden symbol 844 DT_MIPS_HIDDEN_GOTIDX = 0x70000027, 845 // The GOT index of the first PTE for a protected symbol 846 DT_MIPS_PROTECTED_GOTIDX = 0x70000028, 847 // Address of `.MIPS.options'. 848 DT_MIPS_OPTIONS = 0x70000029, 849 // Address of `.interface'. 850 DT_MIPS_INTERFACE = 0x7000002a, 851 // ??? 852 DT_MIPS_DYNSTR_ALIGN = 0x7000002b, 853 // Size of the .interface section. 854 DT_MIPS_INTERFACE_SIZE = 0x7000002c, 855 // Size of rld_text_resolve function stored in the GOT. 856 DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d, 857 // Default suffix of DSO to be added by rld on dlopen() calls. 858 DT_MIPS_PERF_SUFFIX = 0x7000002e, 859 // Size of compact relocation section (O32). 860 DT_MIPS_COMPACT_SIZE = 0x7000002f, 861 // GP value for auxiliary GOTs. 862 DT_MIPS_GP_VALUE = 0x70000030, 863 // Address of auxiliary .dynamic. 864 DT_MIPS_AUX_DYNAMIC = 0x70000031, 865 // Address of the base of the PLTGOT. 866 DT_MIPS_PLTGOT = 0x70000032, 867 // Points to the base of a writable PLT. 868 DT_MIPS_RWPLT = 0x70000034, 869 870 DT_AUXILIARY = 0x7ffffffd, 871 DT_USED = 0x7ffffffe, 872 DT_FILTER = 0x7fffffff 873 }; 874 875 // Flags found in the DT_FLAGS dynamic element. 876 877 enum DF 878 { 879 DF_ORIGIN = 0x1, 880 DF_SYMBOLIC = 0x2, 881 DF_TEXTREL = 0x4, 882 DF_BIND_NOW = 0x8, 883 DF_STATIC_TLS = 0x10 884 }; 885 886 // Flags found in the DT_FLAGS_1 dynamic element. 887 888 enum DF_1 889 { 890 DF_1_NOW = 0x1, 891 DF_1_GLOBAL = 0x2, 892 DF_1_GROUP = 0x4, 893 DF_1_NODELETE = 0x8, 894 DF_1_LOADFLTR = 0x10, 895 DF_1_INITFIRST = 0x20, 896 DF_1_NOOPEN = 0x40, 897 DF_1_ORIGIN = 0x80, 898 DF_1_DIRECT = 0x100, 899 DF_1_TRANS = 0x200, 900 DF_1_INTERPOSE = 0x400, 901 DF_1_NODEFLIB = 0x800, 902 DF_1_NODUMP = 0x1000, 903 DF_1_CONLFAT = 0x2000 904 }; 905 906 // Version numbers which appear in the vd_version field of a Verdef 907 // structure. 908 909 const int VER_DEF_NONE = 0; 910 const int VER_DEF_CURRENT = 1; 911 912 // Version numbers which appear in the vn_version field of a Verneed 913 // structure. 914 915 const int VER_NEED_NONE = 0; 916 const int VER_NEED_CURRENT = 1; 917 918 // Bit flags which appear in vd_flags of Verdef and vna_flags of 919 // Vernaux. 920 921 const int VER_FLG_BASE = 0x1; 922 const int VER_FLG_WEAK = 0x2; 923 const int VER_FLG_INFO = 0x4; 924 925 // Special constants found in the SHT_GNU_versym entries. 926 927 const int VER_NDX_LOCAL = 0; 928 const int VER_NDX_GLOBAL = 1; 929 930 // A SHT_GNU_versym section holds 16-bit words. This bit is set if 931 // the symbol is hidden and can only be seen when referenced using an 932 // explicit version number. This is a GNU extension. 933 934 const int VERSYM_HIDDEN = 0x8000; 935 936 // This is the mask for the rest of the data in a word read from a 937 // SHT_GNU_versym section. 938 939 const int VERSYM_VERSION = 0x7fff; 940 941 // Note descriptor type codes for notes in a non-core file with an 942 // empty name. 943 944 enum 945 { 946 // A version string. 947 NT_VERSION = 1, 948 // An architecture string. 949 NT_ARCH = 2 950 }; 951 952 // Note descriptor type codes for notes in a non-core file with the 953 // name "GNU". 954 955 enum 956 { 957 // The minimum ABI level. This is used by the dynamic linker to 958 // describe the minimal kernel version on which a shared library may 959 // be used. Th value should be four words. Word 0 is an OS 960 // descriptor (see below). Word 1 is the major version of the ABI. 961 // Word 2 is the minor version. Word 3 is the subminor version. 962 NT_GNU_ABI_TAG = 1, 963 // Hardware capabilities information. Word 0 is the number of 964 // entries. Word 1 is a bitmask of enabled entries. The rest of 965 // the descriptor is a series of entries, where each entry is a 966 // single byte followed by a nul terminated string. The byte gives 967 // the bit number to test if enabled in the bitmask. 968 NT_GNU_HWCAP = 2, 969 // The build ID as set by the linker's --build-id option. The 970 // format of the descriptor depends on the build ID style. 971 NT_GNU_BUILD_ID = 3, 972 // The version of gold used to link. Th descriptor is just a 973 // string. 974 NT_GNU_GOLD_VERSION = 4 975 }; 976 977 // The OS values which may appear in word 0 of a NT_GNU_ABI_TAG note. 978 979 enum 980 { 981 ELF_NOTE_OS_LINUX = 0, 982 ELF_NOTE_OS_GNU = 1, 983 ELF_NOTE_OS_SOLARIS2 = 2, 984 ELF_NOTE_OS_FREEBSD = 3, 985 ELF_NOTE_OS_NETBSD = 4, 986 ELF_NOTE_OS_SYLLABLE = 5 987 }; 988 989 } // End namespace elfcpp. 990 991 // Include internal details after defining the types. 992 #include "elfcpp_internal.h" 993 994 namespace elfcpp 995 { 996 997 // The offset of the ELF file header in the ELF file. 998 999 const int file_header_offset = 0; 1000 1001 // ELF structure sizes. 1002 1003 template<int size> 1004 struct Elf_sizes 1005 { 1006 // Size of ELF file header. 1007 static const int ehdr_size = sizeof(internal::Ehdr_data<size>); 1008 // Size of ELF segment header. 1009 static const int phdr_size = sizeof(internal::Phdr_data<size>); 1010 // Size of ELF section header. 1011 static const int shdr_size = sizeof(internal::Shdr_data<size>); 1012 // Size of ELF compression header. 1013 static const int chdr_size = sizeof(internal::Chdr_data<size>); 1014 // Size of ELF symbol table entry. 1015 static const int sym_size = sizeof(internal::Sym_data<size>); 1016 // Sizes of ELF reloc entries. 1017 static const int rel_size = sizeof(internal::Rel_data<size>); 1018 static const int rela_size = sizeof(internal::Rela_data<size>); 1019 // Size of ELF dynamic entry. 1020 static const int dyn_size = sizeof(internal::Dyn_data<size>); 1021 // Size of ELF version structures. 1022 static const int verdef_size = sizeof(internal::Verdef_data); 1023 static const int verdaux_size = sizeof(internal::Verdaux_data); 1024 static const int verneed_size = sizeof(internal::Verneed_data); 1025 static const int vernaux_size = sizeof(internal::Vernaux_data); 1026 }; 1027 1028 // Accessor class for the ELF file header. 1029 1030 template<int size, bool big_endian> 1031 class Ehdr 1032 { 1033 public: 1034 Ehdr(const unsigned char* p) 1035 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p)) 1036 { } 1037 1038 template<typename File> 1039 Ehdr(File* file, typename File::Location loc) 1040 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>( 1041 file->view(loc.file_offset, loc.data_size).data())) 1042 { } 1043 1044 const unsigned char* 1045 get_e_ident() const 1046 { return this->p_->e_ident; } 1047 1048 Elf_Half 1049 get_e_type() const 1050 { return Convert<16, big_endian>::convert_host(this->p_->e_type); } 1051 1052 Elf_Half 1053 get_e_machine() const 1054 { return Convert<16, big_endian>::convert_host(this->p_->e_machine); } 1055 1056 Elf_Word 1057 get_e_version() const 1058 { return Convert<32, big_endian>::convert_host(this->p_->e_version); } 1059 1060 typename Elf_types<size>::Elf_Addr 1061 get_e_entry() const 1062 { return Convert<size, big_endian>::convert_host(this->p_->e_entry); } 1063 1064 typename Elf_types<size>::Elf_Off 1065 get_e_phoff() const 1066 { return Convert<size, big_endian>::convert_host(this->p_->e_phoff); } 1067 1068 typename Elf_types<size>::Elf_Off 1069 get_e_shoff() const 1070 { return Convert<size, big_endian>::convert_host(this->p_->e_shoff); } 1071 1072 Elf_Word 1073 get_e_flags() const 1074 { return Convert<32, big_endian>::convert_host(this->p_->e_flags); } 1075 1076 Elf_Half 1077 get_e_ehsize() const 1078 { return Convert<16, big_endian>::convert_host(this->p_->e_ehsize); } 1079 1080 Elf_Half 1081 get_e_phentsize() const 1082 { return Convert<16, big_endian>::convert_host(this->p_->e_phentsize); } 1083 1084 Elf_Half 1085 get_e_phnum() const 1086 { return Convert<16, big_endian>::convert_host(this->p_->e_phnum); } 1087 1088 Elf_Half 1089 get_e_shentsize() const 1090 { return Convert<16, big_endian>::convert_host(this->p_->e_shentsize); } 1091 1092 Elf_Half 1093 get_e_shnum() const 1094 { return Convert<16, big_endian>::convert_host(this->p_->e_shnum); } 1095 1096 Elf_Half 1097 get_e_shstrndx() const 1098 { return Convert<16, big_endian>::convert_host(this->p_->e_shstrndx); } 1099 1100 private: 1101 const internal::Ehdr_data<size>* p_; 1102 }; 1103 1104 // Write class for the ELF file header. 1105 1106 template<int size, bool big_endian> 1107 class Ehdr_write 1108 { 1109 public: 1110 Ehdr_write(unsigned char* p) 1111 : p_(reinterpret_cast<internal::Ehdr_data<size>*>(p)) 1112 { } 1113 1114 void 1115 put_e_ident(const unsigned char v[EI_NIDENT]) const 1116 { memcpy(this->p_->e_ident, v, EI_NIDENT); } 1117 1118 void 1119 put_e_type(Elf_Half v) 1120 { this->p_->e_type = Convert<16, big_endian>::convert_host(v); } 1121 1122 void 1123 put_e_machine(Elf_Half v) 1124 { this->p_->e_machine = Convert<16, big_endian>::convert_host(v); } 1125 1126 void 1127 put_e_version(Elf_Word v) 1128 { this->p_->e_version = Convert<32, big_endian>::convert_host(v); } 1129 1130 void 1131 put_e_entry(typename Elf_types<size>::Elf_Addr v) 1132 { this->p_->e_entry = Convert<size, big_endian>::convert_host(v); } 1133 1134 void 1135 put_e_phoff(typename Elf_types<size>::Elf_Off v) 1136 { this->p_->e_phoff = Convert<size, big_endian>::convert_host(v); } 1137 1138 void 1139 put_e_shoff(typename Elf_types<size>::Elf_Off v) 1140 { this->p_->e_shoff = Convert<size, big_endian>::convert_host(v); } 1141 1142 void 1143 put_e_flags(Elf_Word v) 1144 { this->p_->e_flags = Convert<32, big_endian>::convert_host(v); } 1145 1146 void 1147 put_e_ehsize(Elf_Half v) 1148 { this->p_->e_ehsize = Convert<16, big_endian>::convert_host(v); } 1149 1150 void 1151 put_e_phentsize(Elf_Half v) 1152 { this->p_->e_phentsize = Convert<16, big_endian>::convert_host(v); } 1153 1154 void 1155 put_e_phnum(Elf_Half v) 1156 { this->p_->e_phnum = Convert<16, big_endian>::convert_host(v); } 1157 1158 void 1159 put_e_shentsize(Elf_Half v) 1160 { this->p_->e_shentsize = Convert<16, big_endian>::convert_host(v); } 1161 1162 void 1163 put_e_shnum(Elf_Half v) 1164 { this->p_->e_shnum = Convert<16, big_endian>::convert_host(v); } 1165 1166 void 1167 put_e_shstrndx(Elf_Half v) 1168 { this->p_->e_shstrndx = Convert<16, big_endian>::convert_host(v); } 1169 1170 private: 1171 internal::Ehdr_data<size>* p_; 1172 }; 1173 1174 // Accessor class for an ELF section header. 1175 1176 template<int size, bool big_endian> 1177 class Shdr 1178 { 1179 public: 1180 Shdr(const unsigned char* p) 1181 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p)) 1182 { } 1183 1184 template<typename File> 1185 Shdr(File* file, typename File::Location loc) 1186 : p_(reinterpret_cast<const internal::Shdr_data<size>*>( 1187 file->view(loc.file_offset, loc.data_size).data())) 1188 { } 1189 1190 Elf_Word 1191 get_sh_name() const 1192 { return Convert<32, big_endian>::convert_host(this->p_->sh_name); } 1193 1194 Elf_Word 1195 get_sh_type() const 1196 { return Convert<32, big_endian>::convert_host(this->p_->sh_type); } 1197 1198 typename Elf_types<size>::Elf_WXword 1199 get_sh_flags() const 1200 { return Convert<size, big_endian>::convert_host(this->p_->sh_flags); } 1201 1202 typename Elf_types<size>::Elf_Addr 1203 get_sh_addr() const 1204 { return Convert<size, big_endian>::convert_host(this->p_->sh_addr); } 1205 1206 typename Elf_types<size>::Elf_Off 1207 get_sh_offset() const 1208 { return Convert<size, big_endian>::convert_host(this->p_->sh_offset); } 1209 1210 typename Elf_types<size>::Elf_WXword 1211 get_sh_size() const 1212 { return Convert<size, big_endian>::convert_host(this->p_->sh_size); } 1213 1214 Elf_Word 1215 get_sh_link() const 1216 { return Convert<32, big_endian>::convert_host(this->p_->sh_link); } 1217 1218 Elf_Word 1219 get_sh_info() const 1220 { return Convert<32, big_endian>::convert_host(this->p_->sh_info); } 1221 1222 typename Elf_types<size>::Elf_WXword 1223 get_sh_addralign() const 1224 { return 1225 Convert<size, big_endian>::convert_host(this->p_->sh_addralign); } 1226 1227 typename Elf_types<size>::Elf_WXword 1228 get_sh_entsize() const 1229 { return Convert<size, big_endian>::convert_host(this->p_->sh_entsize); } 1230 1231 private: 1232 const internal::Shdr_data<size>* p_; 1233 }; 1234 1235 // Write class for an ELF section header. 1236 1237 template<int size, bool big_endian> 1238 class Shdr_write 1239 { 1240 public: 1241 Shdr_write(unsigned char* p) 1242 : p_(reinterpret_cast<internal::Shdr_data<size>*>(p)) 1243 { } 1244 1245 void 1246 put_sh_name(Elf_Word v) 1247 { this->p_->sh_name = Convert<32, big_endian>::convert_host(v); } 1248 1249 void 1250 put_sh_type(Elf_Word v) 1251 { this->p_->sh_type = Convert<32, big_endian>::convert_host(v); } 1252 1253 void 1254 put_sh_flags(typename Elf_types<size>::Elf_WXword v) 1255 { this->p_->sh_flags = Convert<size, big_endian>::convert_host(v); } 1256 1257 void 1258 put_sh_addr(typename Elf_types<size>::Elf_Addr v) 1259 { this->p_->sh_addr = Convert<size, big_endian>::convert_host(v); } 1260 1261 void 1262 put_sh_offset(typename Elf_types<size>::Elf_Off v) 1263 { this->p_->sh_offset = Convert<size, big_endian>::convert_host(v); } 1264 1265 void 1266 put_sh_size(typename Elf_types<size>::Elf_WXword v) 1267 { this->p_->sh_size = Convert<size, big_endian>::convert_host(v); } 1268 1269 void 1270 put_sh_link(Elf_Word v) 1271 { this->p_->sh_link = Convert<32, big_endian>::convert_host(v); } 1272 1273 void 1274 put_sh_info(Elf_Word v) 1275 { this->p_->sh_info = Convert<32, big_endian>::convert_host(v); } 1276 1277 void 1278 put_sh_addralign(typename Elf_types<size>::Elf_WXword v) 1279 { this->p_->sh_addralign = Convert<size, big_endian>::convert_host(v); } 1280 1281 void 1282 put_sh_entsize(typename Elf_types<size>::Elf_WXword v) 1283 { this->p_->sh_entsize = Convert<size, big_endian>::convert_host(v); } 1284 1285 private: 1286 internal::Shdr_data<size>* p_; 1287 }; 1288 1289 // Accessor class for an ELF compression header. 1290 1291 template<int size, bool big_endian> 1292 class Chdr 1293 { 1294 public: 1295 Chdr(const unsigned char* p) 1296 : p_(reinterpret_cast<const internal::Chdr_data<size>*>(p)) 1297 { } 1298 1299 template<typename File> 1300 Chdr(File* file, typename File::Location loc) 1301 : p_(reinterpret_cast<const internal::Chdr_data<size>*>( 1302 file->view(loc.file_offset, loc.data_size).data())) 1303 { } 1304 1305 Elf_Word 1306 get_ch_type() const 1307 { return Convert<size, big_endian>::convert_host(this->p_->ch_type); } 1308 1309 typename Elf_types<size>::Elf_WXword 1310 get_ch_size() const 1311 { return Convert<size, big_endian>::convert_host(this->p_->ch_size); } 1312 1313 typename Elf_types<size>::Elf_WXword 1314 get_ch_addralign() const 1315 { return 1316 Convert<size, big_endian>::convert_host(this->p_->ch_addralign); } 1317 1318 private: 1319 const internal::Chdr_data<size>* p_; 1320 }; 1321 1322 // Write class for an ELF compression header. 1323 1324 template<int size, bool big_endian> 1325 class Chdr_write 1326 { 1327 public: 1328 Chdr_write(unsigned char* p) 1329 : p_(reinterpret_cast<internal::Chdr_data<size>*>(p)) 1330 { } 1331 1332 void 1333 put_ch_type(typename Elf_types<size>::Elf_WXword v) 1334 { this->p_->ch_type = Convert<size, big_endian>::convert_host(v); } 1335 1336 void 1337 put_ch_size(typename Elf_types<size>::Elf_WXword v) 1338 { this->p_->ch_size = Convert<size, big_endian>::convert_host(v); } 1339 1340 void 1341 put_ch_addralign(typename Elf_types<size>::Elf_WXword v) 1342 { this->p_->ch_addralign = Convert<size, big_endian>::convert_host(v); } 1343 1344 private: 1345 internal::Chdr_data<size>* p_; 1346 }; 1347 1348 // Accessor class for an ELF segment header. 1349 1350 template<int size, bool big_endian> 1351 class Phdr 1352 { 1353 public: 1354 Phdr(const unsigned char* p) 1355 : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p)) 1356 { } 1357 1358 template<typename File> 1359 Phdr(File* file, typename File::Location loc) 1360 : p_(reinterpret_cast<internal::Phdr_data<size>*>( 1361 file->view(loc.file_offset, loc.data_size).data())) 1362 { } 1363 1364 Elf_Word 1365 get_p_type() const 1366 { return Convert<32, big_endian>::convert_host(this->p_->p_type); } 1367 1368 typename Elf_types<size>::Elf_Off 1369 get_p_offset() const 1370 { return Convert<size, big_endian>::convert_host(this->p_->p_offset); } 1371 1372 typename Elf_types<size>::Elf_Addr 1373 get_p_vaddr() const 1374 { return Convert<size, big_endian>::convert_host(this->p_->p_vaddr); } 1375 1376 typename Elf_types<size>::Elf_Addr 1377 get_p_paddr() const 1378 { return Convert<size, big_endian>::convert_host(this->p_->p_paddr); } 1379 1380 typename Elf_types<size>::Elf_WXword 1381 get_p_filesz() const 1382 { return Convert<size, big_endian>::convert_host(this->p_->p_filesz); } 1383 1384 typename Elf_types<size>::Elf_WXword 1385 get_p_memsz() const 1386 { return Convert<size, big_endian>::convert_host(this->p_->p_memsz); } 1387 1388 Elf_Word 1389 get_p_flags() const 1390 { return Convert<32, big_endian>::convert_host(this->p_->p_flags); } 1391 1392 typename Elf_types<size>::Elf_WXword 1393 get_p_align() const 1394 { return Convert<size, big_endian>::convert_host(this->p_->p_align); } 1395 1396 private: 1397 const internal::Phdr_data<size>* p_; 1398 }; 1399 1400 // Write class for an ELF segment header. 1401 1402 template<int size, bool big_endian> 1403 class Phdr_write 1404 { 1405 public: 1406 Phdr_write(unsigned char* p) 1407 : p_(reinterpret_cast<internal::Phdr_data<size>*>(p)) 1408 { } 1409 1410 void 1411 put_p_type(Elf_Word v) 1412 { this->p_->p_type = Convert<32, big_endian>::convert_host(v); } 1413 1414 void 1415 put_p_offset(typename Elf_types<size>::Elf_Off v) 1416 { this->p_->p_offset = Convert<size, big_endian>::convert_host(v); } 1417 1418 void 1419 put_p_vaddr(typename Elf_types<size>::Elf_Addr v) 1420 { this->p_->p_vaddr = Convert<size, big_endian>::convert_host(v); } 1421 1422 void 1423 put_p_paddr(typename Elf_types<size>::Elf_Addr v) 1424 { this->p_->p_paddr = Convert<size, big_endian>::convert_host(v); } 1425 1426 void 1427 put_p_filesz(typename Elf_types<size>::Elf_WXword v) 1428 { this->p_->p_filesz = Convert<size, big_endian>::convert_host(v); } 1429 1430 void 1431 put_p_memsz(typename Elf_types<size>::Elf_WXword v) 1432 { this->p_->p_memsz = Convert<size, big_endian>::convert_host(v); } 1433 1434 void 1435 put_p_flags(Elf_Word v) 1436 { this->p_->p_flags = Convert<32, big_endian>::convert_host(v); } 1437 1438 void 1439 put_p_align(typename Elf_types<size>::Elf_WXword v) 1440 { this->p_->p_align = Convert<size, big_endian>::convert_host(v); } 1441 1442 private: 1443 internal::Phdr_data<size>* p_; 1444 }; 1445 1446 // Accessor class for an ELF symbol table entry. 1447 1448 template<int size, bool big_endian> 1449 class Sym 1450 { 1451 public: 1452 Sym(const unsigned char* p) 1453 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p)) 1454 { } 1455 1456 template<typename File> 1457 Sym(File* file, typename File::Location loc) 1458 : p_(reinterpret_cast<const internal::Sym_data<size>*>( 1459 file->view(loc.file_offset, loc.data_size).data())) 1460 { } 1461 1462 Elf_Word 1463 get_st_name() const 1464 { return Convert<32, big_endian>::convert_host(this->p_->st_name); } 1465 1466 typename Elf_types<size>::Elf_Addr 1467 get_st_value() const 1468 { return Convert<size, big_endian>::convert_host(this->p_->st_value); } 1469 1470 typename Elf_types<size>::Elf_WXword 1471 get_st_size() const 1472 { return Convert<size, big_endian>::convert_host(this->p_->st_size); } 1473 1474 unsigned char 1475 get_st_info() const 1476 { return this->p_->st_info; } 1477 1478 STB 1479 get_st_bind() const 1480 { return elf_st_bind(this->get_st_info()); } 1481 1482 STT 1483 get_st_type() const 1484 { return elf_st_type(this->get_st_info()); } 1485 1486 unsigned char 1487 get_st_other() const 1488 { return this->p_->st_other; } 1489 1490 STV 1491 get_st_visibility() const 1492 { return elf_st_visibility(this->get_st_other()); } 1493 1494 unsigned char 1495 get_st_nonvis() const 1496 { return elf_st_nonvis(this->get_st_other()); } 1497 1498 Elf_Half 1499 get_st_shndx() const 1500 { return Convert<16, big_endian>::convert_host(this->p_->st_shndx); } 1501 1502 private: 1503 const internal::Sym_data<size>* p_; 1504 }; 1505 1506 // Writer class for an ELF symbol table entry. 1507 1508 template<int size, bool big_endian> 1509 class Sym_write 1510 { 1511 public: 1512 Sym_write(unsigned char* p) 1513 : p_(reinterpret_cast<internal::Sym_data<size>*>(p)) 1514 { } 1515 1516 void 1517 put_st_name(Elf_Word v) 1518 { this->p_->st_name = Convert<32, big_endian>::convert_host(v); } 1519 1520 void 1521 put_st_value(typename Elf_types<size>::Elf_Addr v) 1522 { this->p_->st_value = Convert<size, big_endian>::convert_host(v); } 1523 1524 void 1525 put_st_size(typename Elf_types<size>::Elf_WXword v) 1526 { this->p_->st_size = Convert<size, big_endian>::convert_host(v); } 1527 1528 void 1529 put_st_info(unsigned char v) 1530 { this->p_->st_info = v; } 1531 1532 void 1533 put_st_info(STB bind, STT type) 1534 { this->p_->st_info = elf_st_info(bind, type); } 1535 1536 void 1537 put_st_other(unsigned char v) 1538 { this->p_->st_other = v; } 1539 1540 void 1541 put_st_other(STV vis, unsigned char nonvis) 1542 { this->p_->st_other = elf_st_other(vis, nonvis); } 1543 1544 void 1545 put_st_shndx(Elf_Half v) 1546 { this->p_->st_shndx = Convert<16, big_endian>::convert_host(v); } 1547 1548 Sym<size, big_endian> 1549 sym() 1550 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); } 1551 1552 private: 1553 internal::Sym_data<size>* p_; 1554 }; 1555 1556 // Accessor classes for an ELF REL relocation entry. 1557 1558 template<int size, bool big_endian> 1559 class Rel 1560 { 1561 public: 1562 Rel(const unsigned char* p) 1563 : p_(reinterpret_cast<const internal::Rel_data<size>*>(p)) 1564 { } 1565 1566 template<typename File> 1567 Rel(File* file, typename File::Location loc) 1568 : p_(reinterpret_cast<const internal::Rel_data<size>*>( 1569 file->view(loc.file_offset, loc.data_size).data())) 1570 { } 1571 1572 typename Elf_types<size>::Elf_Addr 1573 get_r_offset() const 1574 { return Convert<size, big_endian>::convert_host(this->p_->r_offset); } 1575 1576 typename Elf_types<size>::Elf_WXword 1577 get_r_info() const 1578 { return Convert<size, big_endian>::convert_host(this->p_->r_info); } 1579 1580 private: 1581 const internal::Rel_data<size>* p_; 1582 }; 1583 1584 // Writer class for an ELF Rel relocation. 1585 1586 template<int size, bool big_endian> 1587 class Rel_write 1588 { 1589 public: 1590 Rel_write(unsigned char* p) 1591 : p_(reinterpret_cast<internal::Rel_data<size>*>(p)) 1592 { } 1593 1594 void 1595 put_r_offset(typename Elf_types<size>::Elf_Addr v) 1596 { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); } 1597 1598 void 1599 put_r_info(typename Elf_types<size>::Elf_WXword v) 1600 { this->p_->r_info = Convert<size, big_endian>::convert_host(v); } 1601 1602 private: 1603 internal::Rel_data<size>* p_; 1604 }; 1605 1606 // Accessor class for an ELF Rela relocation. 1607 1608 template<int size, bool big_endian> 1609 class Rela 1610 { 1611 public: 1612 Rela(const unsigned char* p) 1613 : p_(reinterpret_cast<const internal::Rela_data<size>*>(p)) 1614 { } 1615 1616 template<typename File> 1617 Rela(File* file, typename File::Location loc) 1618 : p_(reinterpret_cast<const internal::Rela_data<size>*>( 1619 file->view(loc.file_offset, loc.data_size).data())) 1620 { } 1621 1622 typename Elf_types<size>::Elf_Addr 1623 get_r_offset() const 1624 { return Convert<size, big_endian>::convert_host(this->p_->r_offset); } 1625 1626 typename Elf_types<size>::Elf_WXword 1627 get_r_info() const 1628 { return Convert<size, big_endian>::convert_host(this->p_->r_info); } 1629 1630 typename Elf_types<size>::Elf_Swxword 1631 get_r_addend() const 1632 { return Convert<size, big_endian>::convert_host(this->p_->r_addend); } 1633 1634 private: 1635 const internal::Rela_data<size>* p_; 1636 }; 1637 1638 // Writer class for an ELF Rela relocation. 1639 1640 template<int size, bool big_endian> 1641 class Rela_write 1642 { 1643 public: 1644 Rela_write(unsigned char* p) 1645 : p_(reinterpret_cast<internal::Rela_data<size>*>(p)) 1646 { } 1647 1648 void 1649 put_r_offset(typename Elf_types<size>::Elf_Addr v) 1650 { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); } 1651 1652 void 1653 put_r_info(typename Elf_types<size>::Elf_WXword v) 1654 { this->p_->r_info = Convert<size, big_endian>::convert_host(v); } 1655 1656 void 1657 put_r_addend(typename Elf_types<size>::Elf_Swxword v) 1658 { this->p_->r_addend = Convert<size, big_endian>::convert_host(v); } 1659 1660 private: 1661 internal::Rela_data<size>* p_; 1662 }; 1663 1664 // Accessor classes for entries in the ELF SHT_DYNAMIC section aka 1665 // PT_DYNAMIC segment. 1666 1667 template<int size, bool big_endian> 1668 class Dyn 1669 { 1670 public: 1671 Dyn(const unsigned char* p) 1672 : p_(reinterpret_cast<const internal::Dyn_data<size>*>(p)) 1673 { } 1674 1675 template<typename File> 1676 Dyn(File* file, typename File::Location loc) 1677 : p_(reinterpret_cast<const internal::Dyn_data<size>*>( 1678 file->view(loc.file_offset, loc.data_size).data())) 1679 { } 1680 1681 typename Elf_types<size>::Elf_Swxword 1682 get_d_tag() const 1683 { return Convert<size, big_endian>::convert_host(this->p_->d_tag); } 1684 1685 typename Elf_types<size>::Elf_WXword 1686 get_d_val() const 1687 { return Convert<size, big_endian>::convert_host(this->p_->d_val); } 1688 1689 typename Elf_types<size>::Elf_Addr 1690 get_d_ptr() const 1691 { return Convert<size, big_endian>::convert_host(this->p_->d_val); } 1692 1693 private: 1694 const internal::Dyn_data<size>* p_; 1695 }; 1696 1697 // Write class for an entry in the SHT_DYNAMIC section. 1698 1699 template<int size, bool big_endian> 1700 class Dyn_write 1701 { 1702 public: 1703 Dyn_write(unsigned char* p) 1704 : p_(reinterpret_cast<internal::Dyn_data<size>*>(p)) 1705 { } 1706 1707 void 1708 put_d_tag(typename Elf_types<size>::Elf_Swxword v) 1709 { this->p_->d_tag = Convert<size, big_endian>::convert_host(v); } 1710 1711 void 1712 put_d_val(typename Elf_types<size>::Elf_WXword v) 1713 { this->p_->d_val = Convert<size, big_endian>::convert_host(v); } 1714 1715 void 1716 put_d_ptr(typename Elf_types<size>::Elf_Addr v) 1717 { this->p_->d_val = Convert<size, big_endian>::convert_host(v); } 1718 1719 private: 1720 internal::Dyn_data<size>* p_; 1721 }; 1722 1723 // Accessor classes for entries in the ELF SHT_GNU_verdef section. 1724 1725 template<int size, bool big_endian> 1726 class Verdef 1727 { 1728 public: 1729 Verdef(const unsigned char* p) 1730 : p_(reinterpret_cast<const internal::Verdef_data*>(p)) 1731 { } 1732 1733 template<typename File> 1734 Verdef(File* file, typename File::Location loc) 1735 : p_(reinterpret_cast<const internal::Verdef_data*>( 1736 file->view(loc.file_offset, loc.data_size).data())) 1737 { } 1738 1739 Elf_Half 1740 get_vd_version() const 1741 { return Convert<16, big_endian>::convert_host(this->p_->vd_version); } 1742 1743 Elf_Half 1744 get_vd_flags() const 1745 { return Convert<16, big_endian>::convert_host(this->p_->vd_flags); } 1746 1747 Elf_Half 1748 get_vd_ndx() const 1749 { return Convert<16, big_endian>::convert_host(this->p_->vd_ndx); } 1750 1751 Elf_Half 1752 get_vd_cnt() const 1753 { return Convert<16, big_endian>::convert_host(this->p_->vd_cnt); } 1754 1755 Elf_Word 1756 get_vd_hash() const 1757 { return Convert<32, big_endian>::convert_host(this->p_->vd_hash); } 1758 1759 Elf_Word 1760 get_vd_aux() const 1761 { return Convert<32, big_endian>::convert_host(this->p_->vd_aux); } 1762 1763 Elf_Word 1764 get_vd_next() const 1765 { return Convert<32, big_endian>::convert_host(this->p_->vd_next); } 1766 1767 private: 1768 const internal::Verdef_data* p_; 1769 }; 1770 1771 template<int size, bool big_endian> 1772 class Verdef_write 1773 { 1774 public: 1775 Verdef_write(unsigned char* p) 1776 : p_(reinterpret_cast<internal::Verdef_data*>(p)) 1777 { } 1778 1779 void 1780 set_vd_version(Elf_Half v) 1781 { this->p_->vd_version = Convert<16, big_endian>::convert_host(v); } 1782 1783 void 1784 set_vd_flags(Elf_Half v) 1785 { this->p_->vd_flags = Convert<16, big_endian>::convert_host(v); } 1786 1787 void 1788 set_vd_ndx(Elf_Half v) 1789 { this->p_->vd_ndx = Convert<16, big_endian>::convert_host(v); } 1790 1791 void 1792 set_vd_cnt(Elf_Half v) 1793 { this->p_->vd_cnt = Convert<16, big_endian>::convert_host(v); } 1794 1795 void 1796 set_vd_hash(Elf_Word v) 1797 { this->p_->vd_hash = Convert<32, big_endian>::convert_host(v); } 1798 1799 void 1800 set_vd_aux(Elf_Word v) 1801 { this->p_->vd_aux = Convert<32, big_endian>::convert_host(v); } 1802 1803 void 1804 set_vd_next(Elf_Word v) 1805 { this->p_->vd_next = Convert<32, big_endian>::convert_host(v); } 1806 1807 private: 1808 internal::Verdef_data* p_; 1809 }; 1810 1811 // Accessor classes for auxiliary entries in the ELF SHT_GNU_verdef 1812 // section. 1813 1814 template<int size, bool big_endian> 1815 class Verdaux 1816 { 1817 public: 1818 Verdaux(const unsigned char* p) 1819 : p_(reinterpret_cast<const internal::Verdaux_data*>(p)) 1820 { } 1821 1822 template<typename File> 1823 Verdaux(File* file, typename File::Location loc) 1824 : p_(reinterpret_cast<const internal::Verdaux_data*>( 1825 file->view(loc.file_offset, loc.data_size).data())) 1826 { } 1827 1828 Elf_Word 1829 get_vda_name() const 1830 { return Convert<32, big_endian>::convert_host(this->p_->vda_name); } 1831 1832 Elf_Word 1833 get_vda_next() const 1834 { return Convert<32, big_endian>::convert_host(this->p_->vda_next); } 1835 1836 private: 1837 const internal::Verdaux_data* p_; 1838 }; 1839 1840 template<int size, bool big_endian> 1841 class Verdaux_write 1842 { 1843 public: 1844 Verdaux_write(unsigned char* p) 1845 : p_(reinterpret_cast<internal::Verdaux_data*>(p)) 1846 { } 1847 1848 void 1849 set_vda_name(Elf_Word v) 1850 { this->p_->vda_name = Convert<32, big_endian>::convert_host(v); } 1851 1852 void 1853 set_vda_next(Elf_Word v) 1854 { this->p_->vda_next = Convert<32, big_endian>::convert_host(v); } 1855 1856 private: 1857 internal::Verdaux_data* p_; 1858 }; 1859 1860 // Accessor classes for entries in the ELF SHT_GNU_verneed section. 1861 1862 template<int size, bool big_endian> 1863 class Verneed 1864 { 1865 public: 1866 Verneed(const unsigned char* p) 1867 : p_(reinterpret_cast<const internal::Verneed_data*>(p)) 1868 { } 1869 1870 template<typename File> 1871 Verneed(File* file, typename File::Location loc) 1872 : p_(reinterpret_cast<const internal::Verneed_data*>( 1873 file->view(loc.file_offset, loc.data_size).data())) 1874 { } 1875 1876 Elf_Half 1877 get_vn_version() const 1878 { return Convert<16, big_endian>::convert_host(this->p_->vn_version); } 1879 1880 Elf_Half 1881 get_vn_cnt() const 1882 { return Convert<16, big_endian>::convert_host(this->p_->vn_cnt); } 1883 1884 Elf_Word 1885 get_vn_file() const 1886 { return Convert<32, big_endian>::convert_host(this->p_->vn_file); } 1887 1888 Elf_Word 1889 get_vn_aux() const 1890 { return Convert<32, big_endian>::convert_host(this->p_->vn_aux); } 1891 1892 Elf_Word 1893 get_vn_next() const 1894 { return Convert<32, big_endian>::convert_host(this->p_->vn_next); } 1895 1896 private: 1897 const internal::Verneed_data* p_; 1898 }; 1899 1900 template<int size, bool big_endian> 1901 class Verneed_write 1902 { 1903 public: 1904 Verneed_write(unsigned char* p) 1905 : p_(reinterpret_cast<internal::Verneed_data*>(p)) 1906 { } 1907 1908 void 1909 set_vn_version(Elf_Half v) 1910 { this->p_->vn_version = Convert<16, big_endian>::convert_host(v); } 1911 1912 void 1913 set_vn_cnt(Elf_Half v) 1914 { this->p_->vn_cnt = Convert<16, big_endian>::convert_host(v); } 1915 1916 void 1917 set_vn_file(Elf_Word v) 1918 { this->p_->vn_file = Convert<32, big_endian>::convert_host(v); } 1919 1920 void 1921 set_vn_aux(Elf_Word v) 1922 { this->p_->vn_aux = Convert<32, big_endian>::convert_host(v); } 1923 1924 void 1925 set_vn_next(Elf_Word v) 1926 { this->p_->vn_next = Convert<32, big_endian>::convert_host(v); } 1927 1928 private: 1929 internal::Verneed_data* p_; 1930 }; 1931 1932 // Accessor classes for auxiliary entries in the ELF SHT_GNU_verneed 1933 // section. 1934 1935 template<int size, bool big_endian> 1936 class Vernaux 1937 { 1938 public: 1939 Vernaux(const unsigned char* p) 1940 : p_(reinterpret_cast<const internal::Vernaux_data*>(p)) 1941 { } 1942 1943 template<typename File> 1944 Vernaux(File* file, typename File::Location loc) 1945 : p_(reinterpret_cast<const internal::Vernaux_data*>( 1946 file->view(loc.file_offset, loc.data_size).data())) 1947 { } 1948 1949 Elf_Word 1950 get_vna_hash() const 1951 { return Convert<32, big_endian>::convert_host(this->p_->vna_hash); } 1952 1953 Elf_Half 1954 get_vna_flags() const 1955 { return Convert<16, big_endian>::convert_host(this->p_->vna_flags); } 1956 1957 Elf_Half 1958 get_vna_other() const 1959 { return Convert<16, big_endian>::convert_host(this->p_->vna_other); } 1960 1961 Elf_Word 1962 get_vna_name() const 1963 { return Convert<32, big_endian>::convert_host(this->p_->vna_name); } 1964 1965 Elf_Word 1966 get_vna_next() const 1967 { return Convert<32, big_endian>::convert_host(this->p_->vna_next); } 1968 1969 private: 1970 const internal::Vernaux_data* p_; 1971 }; 1972 1973 template<int size, bool big_endian> 1974 class Vernaux_write 1975 { 1976 public: 1977 Vernaux_write(unsigned char* p) 1978 : p_(reinterpret_cast<internal::Vernaux_data*>(p)) 1979 { } 1980 1981 void 1982 set_vna_hash(Elf_Word v) 1983 { this->p_->vna_hash = Convert<32, big_endian>::convert_host(v); } 1984 1985 void 1986 set_vna_flags(Elf_Half v) 1987 { this->p_->vna_flags = Convert<16, big_endian>::convert_host(v); } 1988 1989 void 1990 set_vna_other(Elf_Half v) 1991 { this->p_->vna_other = Convert<16, big_endian>::convert_host(v); } 1992 1993 void 1994 set_vna_name(Elf_Word v) 1995 { this->p_->vna_name = Convert<32, big_endian>::convert_host(v); } 1996 1997 void 1998 set_vna_next(Elf_Word v) 1999 { this->p_->vna_next = Convert<32, big_endian>::convert_host(v); } 2000 2001 private: 2002 internal::Vernaux_data* p_; 2003 }; 2004 2005 } // End namespace elfcpp. 2006 2007 #endif // !defined(ELFPCP_H) 2008