1 // elfcpp.h -- main header file for elfcpp -*- C++ -*-
3 // This is the external interface for elfcpp.
8 #include "elfcpp_config.h"
17 // These types are always the same size.
19 typedef uint16_t Elf_Half;
20 typedef uint32_t Elf_Word;
21 typedef int32_t Elf_Sword;
22 typedef uint64_t Elf_Xword;
23 typedef int64_t Elf_Sxword;
25 // These types vary in size depending on the ELF file class. The
26 // template parameter should be 32 or 64.
31 // Dummy types which should not be used.
32 typedef unsigned char Elf_Addr;
33 typedef unsigned char Elf_Off;
34 // WXword is for fields which are Elf32_Word and Elf64_Xword.
35 typedef unsigned char Elf_WXword;
41 typedef uint32_t Elf_Addr;
42 typedef uint32_t Elf_Off;
43 typedef uint32_t Elf_WXword;
49 typedef uint64_t Elf_Addr;
50 typedef uint64_t Elf_Off;
51 typedef uint64_t Elf_WXword;
52 static const int ehdr_size;
53 static const int shdr_size;
54 static const int sym_size;
57 // Offsets within the Ehdr e_ident field.
59 const int EI_MAG0 = 0;
60 const int EI_MAG1 = 1;
61 const int EI_MAG2 = 2;
62 const int EI_MAG3 = 3;
63 const int EI_CLASS = 4;
64 const int EI_DATA = 5;
65 const int EI_VERSION = 6;
66 const int EI_OSABI = 7;
67 const int EI_ABIVERSION = 8;
69 const int EI_NIDENT = 16;
71 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
73 const int ELFMAG0 = 0x7f;
74 const int ELFMAG1 = 'E';
75 const int ELFMAG2 = 'L';
76 const int ELFMAG3 = 'F';
78 // The valid values found in Ehdr e_ident[EI_CLASS].
87 // The valid values found in Ehdr e_ident[EI_DATA].
96 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
104 // The valid values found in Ehdr e_ident[EI_OSABI].
111 // ELFOSABI_LINUX is not listed in the ELF standard.
113 // ELFOSABI_HURD is not listed in the ELF standard.
115 ELFOSABI_SOLARIS = 6,
118 ELFOSABI_FREEBSD = 9,
120 ELFOSABI_MODESTO = 11,
121 ELFOSABI_OPENBSD = 12,
122 ELFOSABI_OPENVMS = 13,
125 // A GNU extension for the ARM.
127 // A GNU extension for the MSP.
128 ELFOSABI_STANDALONE = 255
131 // The valid values found in the Ehdr e_type field.
146 // The valid values found in the Ehdr e_machine field.
156 // 6 used to be EM_486
161 // 11 was the old Sparc V9 ABI.
162 // 12 through 14 are reserved.
165 // Some old PowerPC object files use 17.
172 // 23 through 35 are served.
236 // Some old picoJava object files use 99 (EM_PJ is correct).
249 EM_ALTERA_NIOS2 = 113,
257 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
260 EM_XSTORMY16 = 0xad45,
267 // Old AVR objects used 0x1057 (EM_AVR is correct).
268 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
269 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
270 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
271 // Old D10V objects used 0x7650 (EM_D10V is correct).
272 // Old D30V objects used 0x7676 (EM_D30V is correct).
273 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
274 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
275 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
276 // Old M32R objects used 0x9041 (EM_M32R is correct).
277 // Old V850 objects used 0x9080 (EM_V850 is correct).
278 // Old S/390 objects used 0xa390 (EM_S390 is correct).
279 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
280 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
281 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
284 // Special section indices.
289 SHN_LORESERVE = 0xff00,
297 SHN_HIRESERVE = 0xffff
300 // The valid values found in the Shdr sh_type field.
318 SHT_PREINIT_ARRAY = 16,
320 SHT_SYMTAB_SHNDX = 18,
321 SHT_LOOS = 0x60000000,
322 SHT_HIOS = 0x6fffffff,
323 SHT_LOPROC = 0x70000000,
324 SHT_HIPROC = 0x7fffffff,
325 SHT_LOUSER = 0x80000000,
326 SHT_HIUSER = 0xffffffff,
327 // The remaining values are not in the standard.
328 // List of prelink dependencies.
329 SHT_GNU_LIBLIST = 0x6ffffff7,
330 // Versions defined by file.
331 SHT_SUNW_verdef = 0x6ffffffd,
332 SHT_GNU_verdef = 0x6ffffffd,
333 // Versions needed by file.
334 SHT_SUNW_verneed = 0x6ffffffe,
335 SHT_GNU_verneed = 0x6ffffffe,
337 SHT_SUNW_versym = 0x6fffffff,
338 SHT_GNU_versym = 0x6fffffff,
341 // The valid bit flags found in the Shdr sh_flags field.
350 SHF_INFO_LINK = 0x40,
351 SHF_LINK_ORDER = 0x80,
352 SHF_OS_NONCONFORMING = 0x100,
355 SHF_MASKOS = 0x0ff00000,
356 SHF_MASKPROC = 0xf0000000
359 // Bit flags which appear in the first 32-bit word of the section data
360 // of a SHT_GROUP section.
365 GRP_MASKOS = 0x0ff00000,
366 GRP_MASKPROC = 0xf0000000
369 // Symbol binding from Sym st_info field.
382 // Symbol types from Sym st_info field.
400 elf_st_bind(unsigned char info)
402 return static_cast<STB>(info >> 4);
406 elf_st_type(unsigned char info)
408 return static_cast<STT>(info & 0xf);
412 elf_st_info(STB bind, STT type)
414 return ((static_cast<unsigned char>(bind) << 4)
415 + (static_cast<unsigned char>(type) & 0xf));
418 // Symbol visibility from Sym st_other field.
429 elf_st_visibility(unsigned char other)
431 return static_cast<STV>(other & 0x3);
435 elf_st_nonvis(unsigned char other)
437 return static_cast<STV>(other >> 2);
441 elf_st_other(STV vis, unsigned char nonvis)
443 return ((nonvis << 2)
444 + (static_cast<unsigned char>(vis) & 3));
447 } // End namespace elfcpp.
449 // Include internal details after defining the types.
450 #include "elfcpp_internal.h"
455 // The offset of the ELF file header in the ELF file.
457 const int file_header_offset = 0;
459 // ELF structure sizes.
464 // Size of ELF file header.
465 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
466 // Size of ELF section header.
467 static const int shdr_size = sizeof(internal::Shdr_data<size>);
468 // Size of ELF symbol table entry.
469 static const int sym_size = sizeof(internal::Sym_data<size>);
472 // Accessor class for the ELF file header.
474 template<int size, bool big_endian>
478 Ehdr(const unsigned char* p)
479 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
484 { return this->p_->e_ident; }
488 { return internal::convert_half<big_endian>(this->p_->e_type); }
491 get_e_machine() const
492 { return internal::convert_half<big_endian>(this->p_->e_machine); }
495 get_e_version() const
496 { return internal::convert_word<big_endian>(this->p_->e_version); }
498 typename Elf_types<size>::Elf_Addr
500 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
502 typename Elf_types<size>::Elf_Off
504 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
506 typename Elf_types<size>::Elf_Off
508 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
512 { return internal::convert_word<big_endian>(this->p_->e_flags); }
516 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
519 get_e_phentsize() const
520 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
524 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
527 get_e_shentsize() const
528 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
532 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
535 get_e_shstrndx() const
536 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
539 const internal::Ehdr_data<size>* p_;
542 // Accessor class for an ELF section header.
544 template<int size, bool big_endian>
548 Shdr(const unsigned char* p)
549 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
554 { return internal::convert_word<big_endian>(this->p_->sh_name); }
558 { return internal::convert_word<big_endian>(this->p_->sh_type); }
560 typename Elf_types<size>::Elf_WXword
562 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
564 typename Elf_types<size>::Elf_Addr
566 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
568 typename Elf_types<size>::Elf_Off
569 get_sh_offset() const
570 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
572 typename Elf_types<size>::Elf_WXword
574 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
578 { return internal::convert_word<big_endian>(this->p_->sh_link); }
582 { return internal::convert_word<big_endian>(this->p_->sh_info); }
584 typename Elf_types<size>::Elf_WXword
585 get_sh_addralign() const
587 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
589 typename Elf_types<size>::Elf_WXword
590 get_sh_entsize() const
591 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
594 const internal::Shdr_data<size>* p_;
597 // Accessor class for an ELF symbol table entry.
599 template<int size, bool big_endian>
603 Sym(const unsigned char* p)
604 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
609 { return internal::convert_word<big_endian>(this->p_->st_name); }
611 typename Elf_types<size>::Elf_Addr
613 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
615 typename Elf_types<size>::Elf_WXword
617 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
621 { return this->p_->st_info; }
625 { return elf_st_bind(this->get_st_info()); }
629 { return elf_st_type(this->get_st_info()); }
633 { return this->p_->st_other; }
636 get_st_visibility() const
637 { return elf_st_visibility(this->get_st_other()); }
640 get_st_nonvis() const
641 { return elf_st_nonvis(this->get_st_other()); }
645 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
648 const internal::Sym_data<size>* p_;
651 // Writer class for an ELF symbol table entry.
653 template<int size, bool big_endian>
657 Sym_write(unsigned char* p)
658 : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
662 put_st_name(Elf_Word v)
663 { this->p_->st_name = internal::convert_word<big_endian>(v); }
666 put_st_value(typename Elf_types<size>::Elf_Addr v)
667 { this->p_->st_value = internal::convert_addr<size, big_endian>(v); }
670 put_st_size(typename Elf_types<size>::Elf_WXword v)
671 { this->p_->st_size = internal::convert_wxword<size, big_endian>(v); }
674 put_st_info(unsigned char v)
675 { this->p_->st_info = v; }
678 put_st_info(STB bind, STT type)
679 { this->p_->st_info = elf_st_info(bind, type); }
682 put_st_other(unsigned char v)
683 { this->p_->st_other = v; }
686 put_st_other(STV vis, unsigned char nonvis)
687 { this->p_->st_other = elf_st_other(vis, nonvis); }
690 put_st_shndx(Elf_Half v)
691 { this->p_->st_shndx = internal::convert_half<big_endian>(v); }
693 Sym<size, big_endian>
695 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
698 internal::Sym_data<size>* p_;
701 } // End namespace elfcpp.
703 #endif // !defined(ELFPCP_H)