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;
42 typedef uint64_t Elf_Addr;
43 typedef uint64_t Elf_Off;
44 typedef uint64_t Elf_WXword;
47 // Offsets within the Ehdr e_ident field.
49 const int EI_MAG0 = 0;
50 const int EI_MAG1 = 1;
51 const int EI_MAG2 = 2;
52 const int EI_MAG3 = 3;
53 const int EI_CLASS = 4;
54 const int EI_DATA = 5;
55 const int EI_VERSION = 6;
56 const int EI_OSABI = 7;
57 const int EI_ABIVERSION = 8;
59 const int EI_NIDENT = 16;
61 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
63 const int ELFMAG0 = 0x7f;
64 const int ELFMAG1 = 'E';
65 const int ELFMAG2 = 'L';
66 const int ELFMAG3 = 'F';
68 // The valid values found in Ehdr e_ident[EI_CLASS].
77 // The valid values found in Ehdr e_ident[EI_DATA].
86 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
94 // The valid values found in Ehdr e_ident[EI_OSABI].
101 // ELFOSABI_LINUX is not listed in the ELF standard.
103 // ELFOSABI_HURD is not listed in the ELF standard.
105 ELFOSABI_SOLARIS = 6,
108 ELFOSABI_FREEBSD = 9,
110 ELFOSABI_MODESTO = 11,
111 ELFOSABI_OPENBSD = 12,
112 ELFOSABI_OPENVMS = 13,
115 // A GNU extension for the ARM.
117 // A GNU extension for the MSP.
118 ELFOSABI_STANDALONE = 255
121 // The valid values found in the Ehdr e_type field.
136 // The valid values found in the Ehdr e_machine field.
146 // 6 used to be EM_486
151 // 11 was the old Sparc V9 ABI.
152 // 12 through 14 are reserved.
155 // Some old PowerPC object files use 17.
162 // 23 through 35 are served.
226 // Some old picoJava object files use 99 (EM_PJ is correct).
239 EM_ALTERA_NIOS2 = 113,
247 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
250 EM_XSTORMY16 = 0xad45,
257 // Old AVR objects used 0x1057 (EM_AVR is correct).
258 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
259 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
260 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
261 // Old D10V objects used 0x7650 (EM_D10V is correct).
262 // Old D30V objects used 0x7676 (EM_D30V is correct).
263 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
264 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
265 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
266 // Old M32R objects used 0x9041 (EM_M32R is correct).
267 // Old V850 objects used 0x9080 (EM_V850 is correct).
268 // Old S/390 objects used 0xa390 (EM_S390 is correct).
269 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
270 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
271 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
274 // Special section indices.
279 SHN_LORESERVE = 0xff00,
287 SHN_HIRESERVE = 0xffff
290 // The valid values found in the Shdr sh_type field.
308 SHT_PREINIT_ARRAY = 16,
310 SHT_SYMTAB_SHNDX = 18,
311 SHT_LOOS = 0x60000000,
312 SHT_HIOS = 0x6fffffff,
313 SHT_LOPROC = 0x70000000,
314 SHT_HIPROC = 0x7fffffff,
315 SHT_LOUSER = 0x80000000,
316 SHT_HIUSER = 0xffffffff,
317 // The remaining values are not in the standard.
318 // List of prelink dependencies.
319 SHT_GNU_LIBLIST = 0x6ffffff7,
320 // Versions defined by file.
321 SHT_SUNW_verdef = 0x6ffffffd,
322 SHT_GNU_verdef = 0x6ffffffd,
323 // Versions needed by file.
324 SHT_SUNW_verneed = 0x6ffffffe,
325 SHT_GNU_verneed = 0x6ffffffe,
327 SHT_SUNW_versym = 0x6fffffff,
328 SHT_GNU_versym = 0x6fffffff,
331 // The valid bit flags found in the Shdr sh_flags field.
340 SHF_INFO_LINK = 0x40,
341 SHF_LINK_ORDER = 0x80,
342 SHF_OS_NONCONFORMING = 0x100,
345 SHF_MASKOS = 0x0ff00000,
346 SHF_MASKPROC = 0xf0000000
349 // Bit flags which appear in the first 32-bit word of the section data
350 // of a SHT_GROUP section.
355 GRP_MASKOS = 0x0ff00000,
356 GRP_MASKPROC = 0xf0000000
359 // The valid values found in the Phdr p_type field.
371 PT_LOOS = 0x60000000,
372 PT_HIOS = 0x6fffffff,
373 PT_LOPROC = 0x70000000,
374 PT_HIPROC = 0x7fffffff,
375 // The remaining values are not in the standard.
376 // Frame unwind information.
377 PT_GNU_EH_FRAME = 0x6474e550,
378 PT_SUNW_EH_FRAME = 0x6474e550,
380 PT_GNU_STACK = 0x6474e551,
381 // Read only after relocation.
382 PT_GNU_RELRO = 0x6474e552
385 // The valid bit flags found in the Phdr p_flags field.
392 PF_MASKOS = 0x0ff00000,
393 PF_MASKPROC = 0xf0000000
396 // Symbol binding from Sym st_info field.
409 // Symbol types from Sym st_info field.
427 elf_st_bind(unsigned char info)
429 return static_cast<STB>(info >> 4);
433 elf_st_type(unsigned char info)
435 return static_cast<STT>(info & 0xf);
439 elf_st_info(STB bind, STT type)
441 return ((static_cast<unsigned char>(bind) << 4)
442 + (static_cast<unsigned char>(type) & 0xf));
445 // Symbol visibility from Sym st_other field.
456 elf_st_visibility(unsigned char other)
458 return static_cast<STV>(other & 0x3);
462 elf_st_nonvis(unsigned char other)
464 return static_cast<STV>(other >> 2);
468 elf_st_other(STV vis, unsigned char nonvis)
470 return ((nonvis << 2)
471 + (static_cast<unsigned char>(vis) & 3));
474 } // End namespace elfcpp.
476 // Include internal details after defining the types.
477 #include "elfcpp_internal.h"
482 // The offset of the ELF file header in the ELF file.
484 const int file_header_offset = 0;
486 // ELF structure sizes.
491 // Size of ELF file header.
492 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
493 // Size of ELF section header.
494 static const int shdr_size = sizeof(internal::Shdr_data<size>);
495 // Size of ELF symbol table entry.
496 static const int sym_size = sizeof(internal::Sym_data<size>);
499 // Given the address of an Elf_Word, return the value.
501 template<bool big_endian>
503 read_elf_word(const Elf_Word* p)
505 return internal::convert_word<big_endian>(*p);
508 // Accessor class for the ELF file header.
510 template<int size, bool big_endian>
514 Ehdr(const unsigned char* p)
515 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
520 { return this->p_->e_ident; }
524 { return internal::convert_half<big_endian>(this->p_->e_type); }
527 get_e_machine() const
528 { return internal::convert_half<big_endian>(this->p_->e_machine); }
531 get_e_version() const
532 { return internal::convert_word<big_endian>(this->p_->e_version); }
534 typename Elf_types<size>::Elf_Addr
536 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
538 typename Elf_types<size>::Elf_Off
540 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
542 typename Elf_types<size>::Elf_Off
544 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
548 { return internal::convert_word<big_endian>(this->p_->e_flags); }
552 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
555 get_e_phentsize() const
556 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
560 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
563 get_e_shentsize() const
564 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
568 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
571 get_e_shstrndx() const
572 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
575 const internal::Ehdr_data<size>* p_;
578 // Accessor class for an ELF section header.
580 template<int size, bool big_endian>
584 Shdr(const unsigned char* p)
585 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
590 { return internal::convert_word<big_endian>(this->p_->sh_name); }
594 { return internal::convert_word<big_endian>(this->p_->sh_type); }
596 typename Elf_types<size>::Elf_WXword
598 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
600 typename Elf_types<size>::Elf_Addr
602 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
604 typename Elf_types<size>::Elf_Off
605 get_sh_offset() const
606 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
608 typename Elf_types<size>::Elf_WXword
610 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
614 { return internal::convert_word<big_endian>(this->p_->sh_link); }
618 { return internal::convert_word<big_endian>(this->p_->sh_info); }
620 typename Elf_types<size>::Elf_WXword
621 get_sh_addralign() const
623 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
625 typename Elf_types<size>::Elf_WXword
626 get_sh_entsize() const
627 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
630 const internal::Shdr_data<size>* p_;
633 // Accessor class for an ELF segment header.
635 template<int size, bool big_endian>
639 Phdr(const unsigned char* p)
640 : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p))
645 { return internal::convert_word<big_endian>(this->p_->p_type); }
647 typename Elf_types<size>::Elf_Off
649 { return internal::convert_off<size, big_endian>(this->p_->p_offset); }
651 typename Elf_types<size>::Elf_Addr
653 { return internal::convert_addr<size, big_endian>(this->p_->p_vaddr); }
655 typename Elf_types<size>::Elf_Addr
657 { return internal::convert_addr<size, big_endian>(this->p_->p_paddr); }
659 typename Elf_types<size>::Elf_WXword
661 { return internal::convert_wxword<size, big_endian>(this->p_->p_filesz); }
663 typename Elf_types<size>::Elf_WXword
665 { return internal::convert_wxword<size, big_endian>(this->p_->p_memsz); }
669 { return internal::convert_word<big_endian>(this->p_->p_flags); }
671 typename Elf_types<size>::Elf_WXword
673 { return internal::convert_wxword<size, big_endian>(this->p_->p_align); }
676 const internal::Phdr_data<size>* p_;
679 // Accessor class for an ELF symbol table entry.
681 template<int size, bool big_endian>
685 Sym(const unsigned char* p)
686 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
691 { return internal::convert_word<big_endian>(this->p_->st_name); }
693 typename Elf_types<size>::Elf_Addr
695 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
697 typename Elf_types<size>::Elf_WXword
699 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
703 { return this->p_->st_info; }
707 { return elf_st_bind(this->get_st_info()); }
711 { return elf_st_type(this->get_st_info()); }
715 { return this->p_->st_other; }
718 get_st_visibility() const
719 { return elf_st_visibility(this->get_st_other()); }
722 get_st_nonvis() const
723 { return elf_st_nonvis(this->get_st_other()); }
727 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
730 const internal::Sym_data<size>* p_;
733 // Writer class for an ELF symbol table entry.
735 template<int size, bool big_endian>
739 Sym_write(unsigned char* p)
740 : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
744 put_st_name(Elf_Word v)
745 { this->p_->st_name = internal::convert_word<big_endian>(v); }
748 put_st_value(typename Elf_types<size>::Elf_Addr v)
749 { this->p_->st_value = internal::convert_addr<size, big_endian>(v); }
752 put_st_size(typename Elf_types<size>::Elf_WXword v)
753 { this->p_->st_size = internal::convert_wxword<size, big_endian>(v); }
756 put_st_info(unsigned char v)
757 { this->p_->st_info = v; }
760 put_st_info(STB bind, STT type)
761 { this->p_->st_info = elf_st_info(bind, type); }
764 put_st_other(unsigned char v)
765 { this->p_->st_other = v; }
768 put_st_other(STV vis, unsigned char nonvis)
769 { this->p_->st_other = elf_st_other(vis, nonvis); }
772 put_st_shndx(Elf_Half v)
773 { this->p_->st_shndx = internal::convert_half<big_endian>(v); }
775 Sym<size, big_endian>
777 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
780 internal::Sym_data<size>* p_;
783 } // End namespace elfcpp.
785 #endif // !defined(ELFPCP_H)