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);
440 } // End namespace elfcpp.
442 // Include internal details after defining the types.
443 #include "elfcpp_internal.h"
448 // The offset of the ELF file header in the ELF file.
450 const int file_header_offset
= 0;
452 // ELF structure sizes.
457 // Size of ELF file header.
458 static const int ehdr_size
= sizeof(internal::Ehdr_data
<size
>);
459 // Size of ELF section header.
460 static const int shdr_size
= sizeof(internal::Shdr_data
<size
>);
461 // Size of ELF symbol table entry.
462 static const int sym_size
= sizeof(internal::Sym_data
<size
>);
465 // Accessor class for the ELF file header.
467 template<int size
, bool big_endian
>
471 Ehdr(const unsigned char* p
)
472 : p_(reinterpret_cast<const internal::Ehdr_data
<size
>*>(p
))
477 { return this->p_
->e_ident
; }
481 { return internal::convert_half
<big_endian
>(this->p_
->e_type
); }
484 get_e_machine() const
485 { return internal::convert_half
<big_endian
>(this->p_
->e_machine
); }
488 get_e_version() const
489 { return internal::convert_word
<big_endian
>(this->p_
->e_version
); }
491 typename Elf_types
<size
>::Elf_Addr
493 { return internal::convert_addr
<size
, big_endian
>(this->p_
->e_entry
); }
495 typename Elf_types
<size
>::Elf_Off
497 { return internal::convert_off
<size
, big_endian
>(this->p_
->e_phoff
); }
499 typename Elf_types
<size
>::Elf_Off
501 { return internal::convert_off
<size
, big_endian
>(this->p_
->e_shoff
); }
505 { return internal::convert_word
<big_endian
>(this->p_
->e_flags
); }
509 { return internal::convert_half
<big_endian
>(this->p_
->e_ehsize
); }
512 get_e_phentsize() const
513 { return internal::convert_half
<big_endian
>(this->p_
->e_phentsize
); }
517 { return internal::convert_half
<big_endian
>(this->p_
->e_phnum
); }
520 get_e_shentsize() const
521 { return internal::convert_half
<big_endian
>(this->p_
->e_shentsize
); }
525 { return internal::convert_half
<big_endian
>(this->p_
->e_shnum
); }
528 get_e_shstrndx() const
529 { return internal::convert_half
<big_endian
>(this->p_
->e_shstrndx
); }
532 const internal::Ehdr_data
<size
>* p_
;
535 // Accessor class for an ELF section header.
537 template<int size
, bool big_endian
>
541 Shdr(const unsigned char* p
)
542 : p_(reinterpret_cast<const internal::Shdr_data
<size
>*>(p
))
547 { return internal::convert_word
<big_endian
>(this->p_
->sh_name
); }
551 { return internal::convert_word
<big_endian
>(this->p_
->sh_type
); }
553 typename Elf_types
<size
>::Elf_WXword
555 { return internal::convert_wxword
<size
, big_endian
>(this->p_
->sh_flags
); }
557 typename Elf_types
<size
>::Elf_Addr
559 { return internal::convert_addr
<size
, big_endian
>(this->p_
->sh_addr
); }
561 typename Elf_types
<size
>::Elf_Off
562 get_sh_offset() const
563 { return internal::convert_off
<size
, big_endian
>(this->p_
->sh_offset
); }
565 typename Elf_types
<size
>::Elf_WXword
567 { return internal::convert_wxword
<size
, big_endian
>(this->p_
->sh_size
); }
571 { return internal::convert_word
<big_endian
>(this->p_
->sh_link
); }
575 { return internal::convert_word
<big_endian
>(this->p_
->sh_info
); }
577 typename Elf_types
<size
>::Elf_WXword
578 get_sh_addralign() const
580 internal::convert_wxword
<size
, big_endian
>(this->p_
->sh_addralign
); }
582 typename Elf_types
<size
>::Elf_WXword
583 get_sh_entsize() const
584 { return internal::convert_wxword
<size
, big_endian
>(this->p_
->sh_entsize
); }
587 const internal::Shdr_data
<size
>* p_
;
590 // Accessor class for an ELF symbol table entry.
592 template<int size
, bool big_endian
>
596 Sym(const unsigned char* p
)
597 : p_(reinterpret_cast<const internal::Sym_data
<size
>*>(p
))
602 { return internal::convert_word
<big_endian
>(this->p_
->st_name
); }
604 typename Elf_types
<size
>::Elf_Addr
606 { return internal::convert_addr
<size
, big_endian
>(this->p_
->st_value
); }
608 typename Elf_types
<size
>::Elf_WXword
610 { return internal::convert_wxword
<size
, big_endian
>(this->p_
->st_size
); }
614 { return this->p_
->st_info
; }
618 { return elf_st_bind(this->get_st_info()); }
622 { return elf_st_type(this->get_st_info()); }
626 { return this->p_
->st_other
; }
629 get_st_visibility() const
630 { return elf_st_visibility(this->get_st_other()); }
633 get_st_nonvis() const
634 { return elf_st_nonvis(this->get_st_other()); }
638 { return internal::convert_half
<big_endian
>(this->p_
->st_shndx
); }
641 const internal::Sym_data
<size
>* p_
;
644 } // End namespace elfcpp.
646 #endif // !defined(ELFPCP_H)