1 // elfcpp.h -- main header file for elfcpp -*- C++ -*-
3 // This is the external interface for elfcpp.
8 #include "elfcpp_config.h"
9 #include "elfcpp_swap.h"
18 // These types are always the same size.
20 typedef uint16_t Elf_Half
;
21 typedef uint32_t Elf_Word
;
22 typedef int32_t Elf_Sword
;
23 typedef uint64_t Elf_Xword
;
24 typedef int64_t Elf_Sxword
;
26 // These types vary in size depending on the ELF file class. The
27 // template parameter should be 32 or 64.
35 typedef uint32_t Elf_Addr
;
36 typedef uint32_t Elf_Off
;
37 typedef uint32_t Elf_WXword
;
38 typedef int32_t Elf_Swxword
;
44 typedef uint64_t Elf_Addr
;
45 typedef uint64_t Elf_Off
;
46 typedef uint64_t Elf_WXword
;
47 typedef int64_t Elf_Swxword
;
50 // Offsets within the Ehdr e_ident field.
52 const int EI_MAG0
= 0;
53 const int EI_MAG1
= 1;
54 const int EI_MAG2
= 2;
55 const int EI_MAG3
= 3;
56 const int EI_CLASS
= 4;
57 const int EI_DATA
= 5;
58 const int EI_VERSION
= 6;
59 const int EI_OSABI
= 7;
60 const int EI_ABIVERSION
= 8;
62 const int EI_NIDENT
= 16;
64 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
66 const int ELFMAG0
= 0x7f;
67 const int ELFMAG1
= 'E';
68 const int ELFMAG2
= 'L';
69 const int ELFMAG3
= 'F';
71 // The valid values found in Ehdr e_ident[EI_CLASS].
80 // The valid values found in Ehdr e_ident[EI_DATA].
89 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
97 // The valid values found in Ehdr e_ident[EI_OSABI].
104 // ELFOSABI_LINUX is not listed in the ELF standard.
106 // ELFOSABI_HURD is not listed in the ELF standard.
108 ELFOSABI_SOLARIS
= 6,
111 ELFOSABI_FREEBSD
= 9,
113 ELFOSABI_MODESTO
= 11,
114 ELFOSABI_OPENBSD
= 12,
115 ELFOSABI_OPENVMS
= 13,
118 // A GNU extension for the ARM.
120 // A GNU extension for the MSP.
121 ELFOSABI_STANDALONE
= 255
124 // The valid values found in the Ehdr e_type field.
139 // The valid values found in the Ehdr e_machine field.
149 // 6 used to be EM_486
154 // 11 was the old Sparc V9 ABI.
155 // 12 through 14 are reserved.
158 // Some old PowerPC object files use 17.
165 // 23 through 35 are served.
229 // Some old picoJava object files use 99 (EM_PJ is correct).
242 EM_ALTERA_NIOS2
= 113,
250 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
253 EM_XSTORMY16
= 0xad45,
260 // Old AVR objects used 0x1057 (EM_AVR is correct).
261 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
262 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
263 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
264 // Old D10V objects used 0x7650 (EM_D10V is correct).
265 // Old D30V objects used 0x7676 (EM_D30V is correct).
266 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
267 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
268 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
269 // Old M32R objects used 0x9041 (EM_M32R is correct).
270 // Old V850 objects used 0x9080 (EM_V850 is correct).
271 // Old S/390 objects used 0xa390 (EM_S390 is correct).
272 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
273 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
274 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
277 // Special section indices.
282 SHN_LORESERVE
= 0xff00,
290 SHN_HIRESERVE
= 0xffff
293 // The valid values found in the Shdr sh_type field.
311 SHT_PREINIT_ARRAY
= 16,
313 SHT_SYMTAB_SHNDX
= 18,
314 SHT_LOOS
= 0x60000000,
315 SHT_HIOS
= 0x6fffffff,
316 SHT_LOPROC
= 0x70000000,
317 SHT_HIPROC
= 0x7fffffff,
318 SHT_LOUSER
= 0x80000000,
319 SHT_HIUSER
= 0xffffffff,
320 // The remaining values are not in the standard.
321 // List of prelink dependencies.
322 SHT_GNU_LIBLIST
= 0x6ffffff7,
323 // Versions defined by file.
324 SHT_SUNW_verdef
= 0x6ffffffd,
325 SHT_GNU_verdef
= 0x6ffffffd,
326 // Versions needed by file.
327 SHT_SUNW_verneed
= 0x6ffffffe,
328 SHT_GNU_verneed
= 0x6ffffffe,
330 SHT_SUNW_versym
= 0x6fffffff,
331 SHT_GNU_versym
= 0x6fffffff,
334 // The valid bit flags found in the Shdr sh_flags field.
343 SHF_INFO_LINK
= 0x40,
344 SHF_LINK_ORDER
= 0x80,
345 SHF_OS_NONCONFORMING
= 0x100,
348 SHF_MASKOS
= 0x0ff00000,
349 SHF_MASKPROC
= 0xf0000000
352 // Bit flags which appear in the first 32-bit word of the section data
353 // of a SHT_GROUP section.
358 GRP_MASKOS
= 0x0ff00000,
359 GRP_MASKPROC
= 0xf0000000
362 // The valid values found in the Phdr p_type field.
374 PT_LOOS
= 0x60000000,
375 PT_HIOS
= 0x6fffffff,
376 PT_LOPROC
= 0x70000000,
377 PT_HIPROC
= 0x7fffffff,
378 // The remaining values are not in the standard.
379 // Frame unwind information.
380 PT_GNU_EH_FRAME
= 0x6474e550,
381 PT_SUNW_EH_FRAME
= 0x6474e550,
383 PT_GNU_STACK
= 0x6474e551,
384 // Read only after relocation.
385 PT_GNU_RELRO
= 0x6474e552
388 // The valid bit flags found in the Phdr p_flags field.
395 PF_MASKOS
= 0x0ff00000,
396 PF_MASKPROC
= 0xf0000000
399 // Symbol binding from Sym st_info field.
412 // Symbol types from Sym st_info field.
430 elf_st_bind(unsigned char info
)
432 return static_cast<STB
>(info
>> 4);
436 elf_st_type(unsigned char info
)
438 return static_cast<STT
>(info
& 0xf);
442 elf_st_info(STB bind
, STT type
)
444 return ((static_cast<unsigned char>(bind
) << 4)
445 + (static_cast<unsigned char>(type
) & 0xf));
448 // Symbol visibility from Sym st_other field.
459 elf_st_visibility(unsigned char other
)
461 return static_cast<STV
>(other
& 0x3);
465 elf_st_nonvis(unsigned char other
)
467 return static_cast<STV
>(other
>> 2);
471 elf_st_other(STV vis
, unsigned char nonvis
)
473 return ((nonvis
<< 2)
474 + (static_cast<unsigned char>(vis
) & 3));
477 // Reloc information from Rel/Rela r_info field.
481 elf_r_sym(typename Elf_types
<size
>::Elf_WXword
);
485 elf_r_sym
<32>(Elf_Word v
)
492 elf_r_sym
<64>(Elf_Xword v
)
499 elf_r_type(typename Elf_types
<size
>::Elf_WXword
);
503 elf_r_type
<32>(Elf_Word v
)
510 elf_r_type
<64>(Elf_Xword v
)
512 return v
& 0xffffffff;
516 typename Elf_types
<size
>::Elf_WXword
517 elf_r_info(unsigned int s
, unsigned int t
);
521 elf_r_info
<32>(unsigned int s
, unsigned int t
)
523 return (s
<< 8) + (t
& 0xff);
528 elf_r_info
<64>(unsigned int s
, unsigned int t
)
530 return (static_cast<Elf_Xword
>(s
) << 32) + (t
& 0xffffffff);
533 } // End namespace elfcpp.
535 // Include internal details after defining the types.
536 #include "elfcpp_internal.h"
541 // The offset of the ELF file header in the ELF file.
543 const int file_header_offset
= 0;
545 // ELF structure sizes.
550 // Size of ELF file header.
551 static const int ehdr_size
= sizeof(internal::Ehdr_data
<size
>);
552 // Size of ELF segment header.
553 static const int phdr_size
= sizeof(internal::Phdr_data
<size
>);
554 // Size of ELF section header.
555 static const int shdr_size
= sizeof(internal::Shdr_data
<size
>);
556 // Size of ELF symbol table entry.
557 static const int sym_size
= sizeof(internal::Sym_data
<size
>);
558 // Sizes of ELF reloc entries.
559 static const int rel_size
= sizeof(internal::Rel_data
<size
>);
560 static const int rela_size
= sizeof(internal::Rela_data
<size
>);
563 // Accessor class for the ELF file header.
565 template<int size
, bool big_endian
>
569 Ehdr(const unsigned char* p
)
570 : p_(reinterpret_cast<const internal::Ehdr_data
<size
>*>(p
))
575 { return this->p_
->e_ident
; }
579 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_type
); }
582 get_e_machine() const
583 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_machine
); }
586 get_e_version() const
587 { return Convert
<32, big_endian
>::convert_host(this->p_
->e_version
); }
589 typename Elf_types
<size
>::Elf_Addr
591 { return Convert
<size
, big_endian
>::convert_host(this->p_
->e_entry
); }
593 typename Elf_types
<size
>::Elf_Off
595 { return Convert
<size
, big_endian
>::convert_host(this->p_
->e_phoff
); }
597 typename Elf_types
<size
>::Elf_Off
599 { return Convert
<size
, big_endian
>::convert_host(this->p_
->e_shoff
); }
603 { return Convert
<32, big_endian
>::convert_host(this->p_
->e_flags
); }
607 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_ehsize
); }
610 get_e_phentsize() const
611 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_phentsize
); }
615 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_phnum
); }
618 get_e_shentsize() const
619 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_shentsize
); }
623 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_shnum
); }
626 get_e_shstrndx() const
627 { return Convert
<16, big_endian
>::convert_host(this->p_
->e_shstrndx
); }
630 const internal::Ehdr_data
<size
>* p_
;
633 // Write class for the ELF file header.
635 template<int size
, bool big_endian
>
639 Ehdr_write(unsigned char* p
)
640 : p_(reinterpret_cast<internal::Ehdr_data
<size
>*>(p
))
644 put_e_ident(const unsigned char v
[EI_NIDENT
]) const
645 { memcpy(this->p_
->e_ident
, v
, EI_NIDENT
); }
648 put_e_type(Elf_Half v
)
649 { this->p_
->e_type
= Convert
<16, big_endian
>::convert_host(v
); }
652 put_e_machine(Elf_Half v
)
653 { this->p_
->e_machine
= Convert
<16, big_endian
>::convert_host(v
); }
656 put_e_version(Elf_Word v
)
657 { this->p_
->e_version
= Convert
<32, big_endian
>::convert_host(v
); }
660 put_e_entry(typename Elf_types
<size
>::Elf_Addr v
)
661 { this->p_
->e_entry
= Convert
<size
, big_endian
>::convert_host(v
); }
664 put_e_phoff(typename Elf_types
<size
>::Elf_Off v
)
665 { this->p_
->e_phoff
= Convert
<size
, big_endian
>::convert_host(v
); }
668 put_e_shoff(typename Elf_types
<size
>::Elf_Off v
)
669 { this->p_
->e_shoff
= Convert
<size
, big_endian
>::convert_host(v
); }
672 put_e_flags(Elf_Word v
)
673 { this->p_
->e_flags
= Convert
<32, big_endian
>::convert_host(v
); }
676 put_e_ehsize(Elf_Half v
)
677 { this->p_
->e_ehsize
= Convert
<16, big_endian
>::convert_host(v
); }
680 put_e_phentsize(Elf_Half v
)
681 { this->p_
->e_phentsize
= Convert
<16, big_endian
>::convert_host(v
); }
684 put_e_phnum(Elf_Half v
)
685 { this->p_
->e_phnum
= Convert
<16, big_endian
>::convert_host(v
); }
688 put_e_shentsize(Elf_Half v
)
689 { this->p_
->e_shentsize
= Convert
<16, big_endian
>::convert_host(v
); }
692 put_e_shnum(Elf_Half v
)
693 { this->p_
->e_shnum
= Convert
<16, big_endian
>::convert_host(v
); }
696 put_e_shstrndx(Elf_Half v
)
697 { this->p_
->e_shstrndx
= Convert
<16, big_endian
>::convert_host(v
); }
700 internal::Ehdr_data
<size
>* p_
;
703 // Accessor class for an ELF section header.
705 template<int size
, bool big_endian
>
709 Shdr(const unsigned char* p
)
710 : p_(reinterpret_cast<const internal::Shdr_data
<size
>*>(p
))
715 { return Convert
<32, big_endian
>::convert_host(this->p_
->sh_name
); }
719 { return Convert
<32, big_endian
>::convert_host(this->p_
->sh_type
); }
721 typename Elf_types
<size
>::Elf_WXword
723 { return Convert
<size
, big_endian
>::convert_host(this->p_
->sh_flags
); }
725 typename Elf_types
<size
>::Elf_Addr
727 { return Convert
<size
, big_endian
>::convert_host(this->p_
->sh_addr
); }
729 typename Elf_types
<size
>::Elf_Off
730 get_sh_offset() const
731 { return Convert
<size
, big_endian
>::convert_host(this->p_
->sh_offset
); }
733 typename Elf_types
<size
>::Elf_WXword
735 { return Convert
<size
, big_endian
>::convert_host(this->p_
->sh_size
); }
739 { return Convert
<32, big_endian
>::convert_host(this->p_
->sh_link
); }
743 { return Convert
<32, big_endian
>::convert_host(this->p_
->sh_info
); }
745 typename Elf_types
<size
>::Elf_WXword
746 get_sh_addralign() const
748 Convert
<size
, big_endian
>::convert_host(this->p_
->sh_addralign
); }
750 typename Elf_types
<size
>::Elf_WXword
751 get_sh_entsize() const
752 { return Convert
<size
, big_endian
>::convert_host(this->p_
->sh_entsize
); }
755 const internal::Shdr_data
<size
>* p_
;
758 // Write class for an ELF section header.
760 template<int size
, bool big_endian
>
764 Shdr_write(unsigned char* p
)
765 : p_(reinterpret_cast<internal::Shdr_data
<size
>*>(p
))
769 put_sh_name(Elf_Word v
)
770 { this->p_
->sh_name
= Convert
<32, big_endian
>::convert_host(v
); }
773 put_sh_type(Elf_Word v
)
774 { this->p_
->sh_type
= Convert
<32, big_endian
>::convert_host(v
); }
777 put_sh_flags(typename Elf_types
<size
>::Elf_WXword v
)
778 { this->p_
->sh_flags
= Convert
<size
, big_endian
>::convert_host(v
); }
781 put_sh_addr(typename Elf_types
<size
>::Elf_Addr v
)
782 { this->p_
->sh_addr
= Convert
<size
, big_endian
>::convert_host(v
); }
785 put_sh_offset(typename Elf_types
<size
>::Elf_Off v
)
786 { this->p_
->sh_offset
= Convert
<size
, big_endian
>::convert_host(v
); }
789 put_sh_size(typename Elf_types
<size
>::Elf_WXword v
)
790 { this->p_
->sh_size
= Convert
<size
, big_endian
>::convert_host(v
); }
793 put_sh_link(Elf_Word v
)
794 { this->p_
->sh_link
= Convert
<32, big_endian
>::convert_host(v
); }
797 put_sh_info(Elf_Word v
)
798 { this->p_
->sh_info
= Convert
<32, big_endian
>::convert_host(v
); }
801 put_sh_addralign(typename Elf_types
<size
>::Elf_WXword v
)
802 { this->p_
->sh_addralign
= Convert
<size
, big_endian
>::convert_host(v
); }
805 put_sh_entsize(typename Elf_types
<size
>::Elf_WXword v
)
806 { this->p_
->sh_entsize
= Convert
<size
, big_endian
>::convert_host(v
); }
809 internal::Shdr_data
<size
>* p_
;
812 // Accessor class for an ELF segment header.
814 template<int size
, bool big_endian
>
818 Phdr(const unsigned char* p
)
819 : p_(reinterpret_cast<const internal::Phdr_data
<size
>*>(p
))
824 { return Convert
<32, big_endian
>::convert_host(this->p_
->p_type
); }
826 typename Elf_types
<size
>::Elf_Off
828 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_offset
); }
830 typename Elf_types
<size
>::Elf_Addr
832 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_vaddr
); }
834 typename Elf_types
<size
>::Elf_Addr
836 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_paddr
); }
838 typename Elf_types
<size
>::Elf_WXword
840 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_filesz
); }
842 typename Elf_types
<size
>::Elf_WXword
844 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_memsz
); }
848 { return Convert
<32, big_endian
>::convert_host(this->p_
->p_flags
); }
850 typename Elf_types
<size
>::Elf_WXword
852 { return Convert
<size
, big_endian
>::convert_host(this->p_
->p_align
); }
855 const internal::Phdr_data
<size
>* p_
;
858 // Write class for an ELF segment header.
860 template<int size
, bool big_endian
>
864 Phdr_write(unsigned char* p
)
865 : p_(reinterpret_cast<internal::Phdr_data
<size
>*>(p
))
869 put_p_type(Elf_Word v
)
870 { this->p_
->p_type
= Convert
<32, big_endian
>::convert_host(v
); }
873 put_p_offset(typename Elf_types
<size
>::Elf_Off v
)
874 { this->p_
->p_offset
= Convert
<size
, big_endian
>::convert_host(v
); }
877 put_p_vaddr(typename Elf_types
<size
>::Elf_Addr v
)
878 { this->p_
->p_vaddr
= Convert
<size
, big_endian
>::convert_host(v
); }
881 put_p_paddr(typename Elf_types
<size
>::Elf_Addr v
)
882 { this->p_
->p_paddr
= Convert
<size
, big_endian
>::convert_host(v
); }
885 put_p_filesz(typename Elf_types
<size
>::Elf_WXword v
)
886 { this->p_
->p_filesz
= Convert
<size
, big_endian
>::convert_host(v
); }
889 put_p_memsz(typename Elf_types
<size
>::Elf_WXword v
)
890 { this->p_
->p_memsz
= Convert
<size
, big_endian
>::convert_host(v
); }
893 put_p_flags(Elf_Word v
)
894 { this->p_
->p_flags
= Convert
<32, big_endian
>::convert_host(v
); }
897 put_p_align(typename Elf_types
<size
>::Elf_WXword v
)
898 { this->p_
->p_align
= Convert
<size
, big_endian
>::convert_host(v
); }
901 internal::Phdr_data
<size
>* p_
;
904 // Accessor class for an ELF symbol table entry.
906 template<int size
, bool big_endian
>
910 Sym(const unsigned char* p
)
911 : p_(reinterpret_cast<const internal::Sym_data
<size
>*>(p
))
916 { return Convert
<32, big_endian
>::convert_host(this->p_
->st_name
); }
918 typename Elf_types
<size
>::Elf_Addr
920 { return Convert
<size
, big_endian
>::convert_host(this->p_
->st_value
); }
922 typename Elf_types
<size
>::Elf_WXword
924 { return Convert
<size
, big_endian
>::convert_host(this->p_
->st_size
); }
928 { return this->p_
->st_info
; }
932 { return elf_st_bind(this->get_st_info()); }
936 { return elf_st_type(this->get_st_info()); }
940 { return this->p_
->st_other
; }
943 get_st_visibility() const
944 { return elf_st_visibility(this->get_st_other()); }
947 get_st_nonvis() const
948 { return elf_st_nonvis(this->get_st_other()); }
952 { return Convert
<16, big_endian
>::convert_host(this->p_
->st_shndx
); }
955 const internal::Sym_data
<size
>* p_
;
958 // Writer class for an ELF symbol table entry.
960 template<int size
, bool big_endian
>
964 Sym_write(unsigned char* p
)
965 : p_(reinterpret_cast<internal::Sym_data
<size
>*>(p
))
969 put_st_name(Elf_Word v
)
970 { this->p_
->st_name
= Convert
<32, big_endian
>::convert_host(v
); }
973 put_st_value(typename Elf_types
<size
>::Elf_Addr v
)
974 { this->p_
->st_value
= Convert
<size
, big_endian
>::convert_host(v
); }
977 put_st_size(typename Elf_types
<size
>::Elf_WXword v
)
978 { this->p_
->st_size
= Convert
<size
, big_endian
>::convert_host(v
); }
981 put_st_info(unsigned char v
)
982 { this->p_
->st_info
= v
; }
985 put_st_info(STB bind
, STT type
)
986 { this->p_
->st_info
= elf_st_info(bind
, type
); }
989 put_st_other(unsigned char v
)
990 { this->p_
->st_other
= v
; }
993 put_st_other(STV vis
, unsigned char nonvis
)
994 { this->p_
->st_other
= elf_st_other(vis
, nonvis
); }
997 put_st_shndx(Elf_Half v
)
998 { this->p_
->st_shndx
= Convert
<16, big_endian
>::convert_host(v
); }
1000 Sym
<size
, big_endian
>
1002 { return Sym
<size
, big_endian
>(reinterpret_cast<unsigned char*>(this->p_
)); }
1005 internal::Sym_data
<size
>* p_
;
1008 // Accessor classes for Elf relocation table entries.
1010 template<int size
, bool big_endian
>
1014 Rel(const unsigned char* p
)
1015 : p_(reinterpret_cast<const internal::Rel_data
<size
>*>(p
))
1018 typename Elf_types
<size
>::Elf_Addr
1019 get_r_offset() const
1020 { return Convert
<size
, big_endian
>::convert_host(this->p_
->r_offset
); }
1022 typename Elf_types
<size
>::Elf_WXword
1024 { return Convert
<size
, big_endian
>::convert_host(this->p_
->r_info
); }
1027 const internal::Rel_data
<size
>* p_
;
1030 template<int size
, bool big_endian
>
1034 Rela(const unsigned char* p
)
1035 : p_(reinterpret_cast<const internal::Rela_data
<size
>*>(p
))
1038 typename Elf_types
<size
>::Elf_Addr
1039 get_r_offset() const
1040 { return Convert
<size
, big_endian
>::convert_host(this->p_
->r_offset
); }
1042 typename Elf_types
<size
>::Elf_WXword
1044 { return Convert
<size
, big_endian
>::convert_host(this->p_
->r_info
); }
1046 typename Elf_types
<size
>::Elf_Swxword
1047 get_r_addend() const
1048 { return Convert
<size
, big_endian
>::convert_host(this->p_
->r_addend
); }
1051 const internal::Rela_data
<size
>* p_
;
1054 } // End namespace elfcpp.
1056 #endif // !defined(ELFPCP_H)