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.
34 typedef uint32_t Elf_Addr;
35 typedef uint32_t Elf_Off;
36 typedef uint32_t Elf_WXword;
37 typedef int32_t Elf_Swxword;
43 typedef uint64_t Elf_Addr;
44 typedef uint64_t Elf_Off;
45 typedef uint64_t Elf_WXword;
46 typedef int64_t Elf_Swxword;
49 // Offsets within the Ehdr e_ident field.
51 const int EI_MAG0 = 0;
52 const int EI_MAG1 = 1;
53 const int EI_MAG2 = 2;
54 const int EI_MAG3 = 3;
55 const int EI_CLASS = 4;
56 const int EI_DATA = 5;
57 const int EI_VERSION = 6;
58 const int EI_OSABI = 7;
59 const int EI_ABIVERSION = 8;
61 const int EI_NIDENT = 16;
63 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
65 const int ELFMAG0 = 0x7f;
66 const int ELFMAG1 = 'E';
67 const int ELFMAG2 = 'L';
68 const int ELFMAG3 = 'F';
70 // The valid values found in Ehdr e_ident[EI_CLASS].
79 // The valid values found in Ehdr e_ident[EI_DATA].
88 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
96 // The valid values found in Ehdr e_ident[EI_OSABI].
103 // ELFOSABI_LINUX is not listed in the ELF standard.
105 // ELFOSABI_HURD is not listed in the ELF standard.
107 ELFOSABI_SOLARIS = 6,
110 ELFOSABI_FREEBSD = 9,
112 ELFOSABI_MODESTO = 11,
113 ELFOSABI_OPENBSD = 12,
114 ELFOSABI_OPENVMS = 13,
117 // A GNU extension for the ARM.
119 // A GNU extension for the MSP.
120 ELFOSABI_STANDALONE = 255
123 // The valid values found in the Ehdr e_type field.
138 // The valid values found in the Ehdr e_machine field.
148 // 6 used to be EM_486
153 // 11 was the old Sparc V9 ABI.
154 // 12 through 14 are reserved.
157 // Some old PowerPC object files use 17.
164 // 23 through 35 are served.
228 // Some old picoJava object files use 99 (EM_PJ is correct).
241 EM_ALTERA_NIOS2 = 113,
249 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
252 EM_XSTORMY16 = 0xad45,
259 // Old AVR objects used 0x1057 (EM_AVR is correct).
260 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
261 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
262 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
263 // Old D10V objects used 0x7650 (EM_D10V is correct).
264 // Old D30V objects used 0x7676 (EM_D30V is correct).
265 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
266 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
267 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
268 // Old M32R objects used 0x9041 (EM_M32R is correct).
269 // Old V850 objects used 0x9080 (EM_V850 is correct).
270 // Old S/390 objects used 0xa390 (EM_S390 is correct).
271 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
272 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
273 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
276 // Special section indices.
281 SHN_LORESERVE = 0xff00,
289 SHN_HIRESERVE = 0xffff
292 // The valid values found in the Shdr sh_type field.
310 SHT_PREINIT_ARRAY = 16,
312 SHT_SYMTAB_SHNDX = 18,
313 SHT_LOOS = 0x60000000,
314 SHT_HIOS = 0x6fffffff,
315 SHT_LOPROC = 0x70000000,
316 SHT_HIPROC = 0x7fffffff,
317 SHT_LOUSER = 0x80000000,
318 SHT_HIUSER = 0xffffffff,
319 // The remaining values are not in the standard.
320 // List of prelink dependencies.
321 SHT_GNU_LIBLIST = 0x6ffffff7,
322 // Versions defined by file.
323 SHT_SUNW_verdef = 0x6ffffffd,
324 SHT_GNU_verdef = 0x6ffffffd,
325 // Versions needed by file.
326 SHT_SUNW_verneed = 0x6ffffffe,
327 SHT_GNU_verneed = 0x6ffffffe,
329 SHT_SUNW_versym = 0x6fffffff,
330 SHT_GNU_versym = 0x6fffffff,
333 // The valid bit flags found in the Shdr sh_flags field.
342 SHF_INFO_LINK = 0x40,
343 SHF_LINK_ORDER = 0x80,
344 SHF_OS_NONCONFORMING = 0x100,
347 SHF_MASKOS = 0x0ff00000,
348 SHF_MASKPROC = 0xf0000000
351 // Bit flags which appear in the first 32-bit word of the section data
352 // of a SHT_GROUP section.
357 GRP_MASKOS = 0x0ff00000,
358 GRP_MASKPROC = 0xf0000000
361 // The valid values found in the Phdr p_type field.
373 PT_LOOS = 0x60000000,
374 PT_HIOS = 0x6fffffff,
375 PT_LOPROC = 0x70000000,
376 PT_HIPROC = 0x7fffffff,
377 // The remaining values are not in the standard.
378 // Frame unwind information.
379 PT_GNU_EH_FRAME = 0x6474e550,
380 PT_SUNW_EH_FRAME = 0x6474e550,
382 PT_GNU_STACK = 0x6474e551,
383 // Read only after relocation.
384 PT_GNU_RELRO = 0x6474e552
387 // The valid bit flags found in the Phdr p_flags field.
394 PF_MASKOS = 0x0ff00000,
395 PF_MASKPROC = 0xf0000000
398 // Symbol binding from Sym st_info field.
411 // Symbol types from Sym st_info field.
429 elf_st_bind(unsigned char info)
431 return static_cast<STB>(info >> 4);
435 elf_st_type(unsigned char info)
437 return static_cast<STT>(info & 0xf);
441 elf_st_info(STB bind, STT type)
443 return ((static_cast<unsigned char>(bind) << 4)
444 + (static_cast<unsigned char>(type) & 0xf));
447 // Symbol visibility from Sym st_other field.
458 elf_st_visibility(unsigned char other)
460 return static_cast<STV>(other & 0x3);
464 elf_st_nonvis(unsigned char other)
466 return static_cast<STV>(other >> 2);
470 elf_st_other(STV vis, unsigned char nonvis)
472 return ((nonvis << 2)
473 + (static_cast<unsigned char>(vis) & 3));
476 // Reloc information from Rel/Rela r_info field.
480 elf_r_sym(typename Elf_types<size>::Elf_WXword);
484 elf_r_sym<32>(Elf_Word v)
491 elf_r_sym<64>(Elf_Xword v)
498 elf_r_type(typename Elf_types<size>::Elf_WXword);
502 elf_r_type<32>(Elf_Word v)
509 elf_r_type<64>(Elf_Xword v)
511 return v & 0xffffffff;
515 typename Elf_types<size>::Elf_WXword
516 elf_r_info(unsigned int s, unsigned int t);
520 elf_r_info<32>(unsigned int s, unsigned int t)
522 return (s << 8) + (t & 0xff);
527 elf_r_info<64>(unsigned int s, unsigned int t)
529 return (static_cast<Elf_Xword>(s) << 32) + (t & 0xffffffff);
532 } // End namespace elfcpp.
534 // Include internal details after defining the types.
535 #include "elfcpp_internal.h"
540 // The offset of the ELF file header in the ELF file.
542 const int file_header_offset = 0;
544 // ELF structure sizes.
549 // Size of ELF file header.
550 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
551 // Size of ELF segment header.
552 static const int phdr_size = sizeof(internal::Phdr_data<size>);
553 // Size of ELF section header.
554 static const int shdr_size = sizeof(internal::Shdr_data<size>);
555 // Size of ELF symbol table entry.
556 static const int sym_size = sizeof(internal::Sym_data<size>);
557 // Sizes of ELF reloc entries.
558 static const int rel_size = sizeof(internal::Rel_data<size>);
559 static const int rela_size = sizeof(internal::Rela_data<size>);
562 // Given the address of an Elf_Word, return the value.
564 template<bool big_endian>
566 read_elf_word(const Elf_Word* p)
568 return internal::convert_word<big_endian>(*p);
571 // Store an Elf_Word into an address.
573 template<bool big_endian>
575 write_elf_word(Elf_Word* p, Elf_Word v)
577 *p = internal::convert_word<big_endian>(v);
580 // Accessor class for the ELF file header.
582 template<int size, bool big_endian>
586 Ehdr(const unsigned char* p)
587 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
592 { return this->p_->e_ident; }
596 { return internal::convert_half<big_endian>(this->p_->e_type); }
599 get_e_machine() const
600 { return internal::convert_half<big_endian>(this->p_->e_machine); }
603 get_e_version() const
604 { return internal::convert_word<big_endian>(this->p_->e_version); }
606 typename Elf_types<size>::Elf_Addr
608 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
610 typename Elf_types<size>::Elf_Off
612 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
614 typename Elf_types<size>::Elf_Off
616 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
620 { return internal::convert_word<big_endian>(this->p_->e_flags); }
624 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
627 get_e_phentsize() const
628 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
632 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
635 get_e_shentsize() const
636 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
640 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
643 get_e_shstrndx() const
644 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
647 const internal::Ehdr_data<size>* p_;
650 // Write class for the ELF file header.
652 template<int size, bool big_endian>
656 Ehdr_write(unsigned char* p)
657 : p_(reinterpret_cast<internal::Ehdr_data<size>*>(p))
661 put_e_ident(const unsigned char v[EI_NIDENT]) const
662 { memcpy(this->p_->e_ident, v, EI_NIDENT); }
665 put_e_type(Elf_Half v)
666 { this->p_->e_type = internal::convert_half<big_endian>(v); }
669 put_e_machine(Elf_Half v)
670 { this->p_->e_machine = internal::convert_half<big_endian>(v); }
673 put_e_version(Elf_Word v)
674 { this->p_->e_version = internal::convert_word<big_endian>(v); }
677 put_e_entry(typename Elf_types<size>::Elf_Addr v)
678 { this->p_->e_entry = internal::convert_addr<size, big_endian>(v); }
681 put_e_phoff(typename Elf_types<size>::Elf_Off v)
682 { this->p_->e_phoff = internal::convert_off<size, big_endian>(v); }
685 put_e_shoff(typename Elf_types<size>::Elf_Off v)
686 { this->p_->e_shoff = internal::convert_off<size, big_endian>(v); }
689 put_e_flags(Elf_Word v)
690 { this->p_->e_flags = internal::convert_word<big_endian>(v); }
693 put_e_ehsize(Elf_Half v)
694 { this->p_->e_ehsize = internal::convert_half<big_endian>(v); }
697 put_e_phentsize(Elf_Half v)
698 { this->p_->e_phentsize = internal::convert_half<big_endian>(v); }
701 put_e_phnum(Elf_Half v)
702 { this->p_->e_phnum = internal::convert_half<big_endian>(v); }
705 put_e_shentsize(Elf_Half v)
706 { this->p_->e_shentsize = internal::convert_half<big_endian>(v); }
709 put_e_shnum(Elf_Half v)
710 { this->p_->e_shnum = internal::convert_half<big_endian>(v); }
713 put_e_shstrndx(Elf_Half v)
714 { this->p_->e_shstrndx = internal::convert_half<big_endian>(v); }
717 internal::Ehdr_data<size>* p_;
720 // Accessor class for an ELF section header.
722 template<int size, bool big_endian>
726 Shdr(const unsigned char* p)
727 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
732 { return internal::convert_word<big_endian>(this->p_->sh_name); }
736 { return internal::convert_word<big_endian>(this->p_->sh_type); }
738 typename Elf_types<size>::Elf_WXword
740 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
742 typename Elf_types<size>::Elf_Addr
744 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
746 typename Elf_types<size>::Elf_Off
747 get_sh_offset() const
748 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
750 typename Elf_types<size>::Elf_WXword
752 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
756 { return internal::convert_word<big_endian>(this->p_->sh_link); }
760 { return internal::convert_word<big_endian>(this->p_->sh_info); }
762 typename Elf_types<size>::Elf_WXword
763 get_sh_addralign() const
765 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
767 typename Elf_types<size>::Elf_WXword
768 get_sh_entsize() const
769 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
772 const internal::Shdr_data<size>* p_;
775 // Write class for an ELF section header.
777 template<int size, bool big_endian>
781 Shdr_write(unsigned char* p)
782 : p_(reinterpret_cast<internal::Shdr_data<size>*>(p))
786 put_sh_name(Elf_Word v)
787 { this->p_->sh_name = internal::convert_word<big_endian>(v); }
790 put_sh_type(Elf_Word v)
791 { this->p_->sh_type = internal::convert_word<big_endian>(v); }
794 put_sh_flags(typename Elf_types<size>::Elf_WXword v)
795 { this->p_->sh_flags = internal::convert_wxword<size, big_endian>(v); }
798 put_sh_addr(typename Elf_types<size>::Elf_Addr v)
799 { this->p_->sh_addr = internal::convert_addr<size, big_endian>(v); }
802 put_sh_offset(typename Elf_types<size>::Elf_Off v)
803 { this->p_->sh_offset = internal::convert_off<size, big_endian>(v); }
806 put_sh_size(typename Elf_types<size>::Elf_WXword v)
807 { this->p_->sh_size = internal::convert_wxword<size, big_endian>(v); }
810 put_sh_link(Elf_Word v)
811 { this->p_->sh_link = internal::convert_word<big_endian>(v); }
814 put_sh_info(Elf_Word v)
815 { this->p_->sh_info = internal::convert_word<big_endian>(v); }
818 put_sh_addralign(typename Elf_types<size>::Elf_WXword v)
819 { this->p_->sh_addralign = internal::convert_wxword<size, big_endian>(v); }
822 put_sh_entsize(typename Elf_types<size>::Elf_WXword v)
823 { this->p_->sh_entsize = internal::convert_wxword<size, big_endian>(v); }
826 internal::Shdr_data<size>* p_;
829 // Accessor class for an ELF segment header.
831 template<int size, bool big_endian>
835 Phdr(const unsigned char* p)
836 : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p))
841 { return internal::convert_word<big_endian>(this->p_->p_type); }
843 typename Elf_types<size>::Elf_Off
845 { return internal::convert_off<size, big_endian>(this->p_->p_offset); }
847 typename Elf_types<size>::Elf_Addr
849 { return internal::convert_addr<size, big_endian>(this->p_->p_vaddr); }
851 typename Elf_types<size>::Elf_Addr
853 { return internal::convert_addr<size, big_endian>(this->p_->p_paddr); }
855 typename Elf_types<size>::Elf_WXword
857 { return internal::convert_wxword<size, big_endian>(this->p_->p_filesz); }
859 typename Elf_types<size>::Elf_WXword
861 { return internal::convert_wxword<size, big_endian>(this->p_->p_memsz); }
865 { return internal::convert_word<big_endian>(this->p_->p_flags); }
867 typename Elf_types<size>::Elf_WXword
869 { return internal::convert_wxword<size, big_endian>(this->p_->p_align); }
872 const internal::Phdr_data<size>* p_;
875 // Write class for an ELF segment header.
877 template<int size, bool big_endian>
881 Phdr_write(unsigned char* p)
882 : p_(reinterpret_cast<internal::Phdr_data<size>*>(p))
886 put_p_type(Elf_Word v)
887 { this->p_->p_type = internal::convert_word<big_endian>(v); }
890 put_p_offset(typename Elf_types<size>::Elf_Off v)
891 { this->p_->p_offset = internal::convert_off<size, big_endian>(v); }
894 put_p_vaddr(typename Elf_types<size>::Elf_Addr v)
895 { this->p_->p_vaddr = internal::convert_addr<size, big_endian>(v); }
898 put_p_paddr(typename Elf_types<size>::Elf_Addr v)
899 { this->p_->p_paddr = internal::convert_addr<size, big_endian>(v); }
902 put_p_filesz(typename Elf_types<size>::Elf_WXword v)
903 { this->p_->p_filesz = internal::convert_wxword<size, big_endian>(v); }
906 put_p_memsz(typename Elf_types<size>::Elf_WXword v)
907 { this->p_->p_memsz = internal::convert_wxword<size, big_endian>(v); }
910 put_p_flags(Elf_Word v)
911 { this->p_->p_flags = internal::convert_word<big_endian>(v); }
914 put_p_align(typename Elf_types<size>::Elf_WXword v)
915 { this->p_->p_align = internal::convert_wxword<size, big_endian>(v); }
918 internal::Phdr_data<size>* p_;
921 // Accessor class for an ELF symbol table entry.
923 template<int size, bool big_endian>
927 Sym(const unsigned char* p)
928 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
933 { return internal::convert_word<big_endian>(this->p_->st_name); }
935 typename Elf_types<size>::Elf_Addr
937 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
939 typename Elf_types<size>::Elf_WXword
941 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
945 { return this->p_->st_info; }
949 { return elf_st_bind(this->get_st_info()); }
953 { return elf_st_type(this->get_st_info()); }
957 { return this->p_->st_other; }
960 get_st_visibility() const
961 { return elf_st_visibility(this->get_st_other()); }
964 get_st_nonvis() const
965 { return elf_st_nonvis(this->get_st_other()); }
969 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
972 const internal::Sym_data<size>* p_;
975 // Writer class for an ELF symbol table entry.
977 template<int size, bool big_endian>
981 Sym_write(unsigned char* p)
982 : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
986 put_st_name(Elf_Word v)
987 { this->p_->st_name = internal::convert_word<big_endian>(v); }
990 put_st_value(typename Elf_types<size>::Elf_Addr v)
991 { this->p_->st_value = internal::convert_addr<size, big_endian>(v); }
994 put_st_size(typename Elf_types<size>::Elf_WXword v)
995 { this->p_->st_size = internal::convert_wxword<size, big_endian>(v); }
998 put_st_info(unsigned char v)
999 { this->p_->st_info = v; }
1002 put_st_info(STB bind, STT type)
1003 { this->p_->st_info = elf_st_info(bind, type); }
1006 put_st_other(unsigned char v)
1007 { this->p_->st_other = v; }
1010 put_st_other(STV vis, unsigned char nonvis)
1011 { this->p_->st_other = elf_st_other(vis, nonvis); }
1014 put_st_shndx(Elf_Half v)
1015 { this->p_->st_shndx = internal::convert_half<big_endian>(v); }
1017 Sym<size, big_endian>
1019 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
1022 internal::Sym_data<size>* p_;
1025 // Accessor classes for Elf relocation table entries.
1027 template<int size, bool big_endian>
1031 Rel(const unsigned char* p)
1032 : p_(reinterpret_cast<const internal::Rel_data<size>*>(p))
1035 typename Elf_types<size>::Elf_Addr
1036 get_r_offset() const
1037 { return internal::convert_addr<size, big_endian>(this->p_->r_offset); }
1039 typename Elf_types<size>::Elf_WXword
1041 { return internal::convert_wxword<size, big_endian>(this->p_->r_info); }
1044 const internal::Rel_data<size>* p_;
1047 template<int size, bool big_endian>
1051 Rela(const unsigned char* p)
1052 : p_(reinterpret_cast<const internal::Rela_data<size>*>(p))
1055 typename Elf_types<size>::Elf_Addr
1056 get_r_offset() const
1057 { return internal::convert_addr<size, big_endian>(this->p_->r_offset); }
1059 typename Elf_types<size>::Elf_WXword
1061 { return internal::convert_wxword<size, big_endian>(this->p_->r_info); }
1063 typename Elf_types<size>::Elf_Swxword
1064 get_r_addend() const
1065 { return internal::convert_swxword<size, big_endian>(this->p_->r_addend); }
1068 const internal::Rela_data<size>* p_;
1071 } // End namespace elfcpp.
1073 #endif // !defined(ELFPCP_H)