14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
43 inline std::pair<unsigned char, unsigned char>
56 typedef typename std::conditional<ELFT::Is64Bits,
72 : EntitySize(EntSize), Current(Start) {}
75 assert(Current &&
"Attempted to dereference an invalid iterator!");
76 return *
reinterpret_cast<pointer>(Current);
80 assert(Current &&
"Attempted to dereference an invalid iterator!");
81 return reinterpret_cast<pointer>(Current);
85 return Current == Other.Current;
89 return !(*
this ==
Other);
93 assert(Current &&
"Attempted to increment an invalid iterator!");
94 Current += EntitySize;
99 assert(Current &&
"Attempted to increment an invalid iterator!");
100 Current += (n * EntitySize);
105 assert(Current &&
"Attempted to subtract an invalid iterator!");
106 Current -= (n * EntitySize);
117 assert(EntitySize == Other.EntitySize &&
118 "Subtracting iterators of different EntitySize!");
119 return (Current - Other.Current) / EntitySize;
122 const char *
get()
const {
return Current; }
152 template <
typename T>
157 return static_cast<T *
>(
P);
170 const uint8_t *base()
const {
171 return reinterpret_cast<const uint8_t *
>(Buf.
data());
175 const Elf_Shdr *SectionHeaderTable =
nullptr;
178 const Elf_Shdr *dot_symtab_sec =
nullptr;
179 const Elf_Shdr *DotDynSymSec =
nullptr;
180 const Elf_Hash *HashTable =
nullptr;
182 const Elf_Shdr *SymbolTableSectionHeaderIndex =
nullptr;
185 const Elf_Shdr *dot_gnu_version_sec =
nullptr;
186 const Elf_Shdr *dot_gnu_version_r_sec =
nullptr;
187 const Elf_Shdr *dot_gnu_version_d_sec =
nullptr;
190 struct DynRegionInfo {
191 DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
200 DynRegionInfo DynamicRegion;
201 DynRegionInfo DynHashRegion;
202 DynRegionInfo DynStrRegion;
203 DynRegionInfo DynRelaRegion;
207 mutable const char *dt_soname =
nullptr;
211 class VersionMapEntry :
public PointerIntPair<const void*, 1> {
215 VersionMapEntry() : PointerIntPair<
const void*, 1>(nullptr, 0) { }
221 bool isVerdef()
const {
return !
isNull() &&
getInt() == 0; }
222 bool isVernaux()
const {
return !
isNull() &&
getInt() == 1; }
230 mutable SmallVector<VersionMapEntry, 16> VersionMap;
231 void LoadVersionDefs(
const Elf_Shdr *sec)
const;
232 void LoadVersionNeeds(
const Elf_Shdr *ec)
const;
233 void LoadVersionMap()
const;
235 void scanDynamicTable();
240 template <
typename T>
251 bool &IsDefault)
const;
259 template <
class RelT>
260 std::pair<const Elf_Shdr *, const Elf_Sym *>
299 if (DotDynSymSec->sh_entsize !=
sizeof(
Elf_Sym))
301 return reinterpret_cast<const Elf_Sym *
>(base() + DotDynSymSec->sh_offset);
307 return reinterpret_cast<const Elf_Sym *
>(base() + DotDynSymSec->sh_offset +
308 DotDynSymSec->sh_size);
316 if (DynRelaRegion.Addr)
318 (
const char *)DynRelaRegion.Addr);
323 if (DynRelaRegion.Addr)
325 DynRelaRegion.EntSize,
326 (
const char *)DynRelaRegion.Addr + DynRelaRegion.Size);
332 (
const char *)(base() + sec->sh_offset));
338 (
const char *)(base() + sec->sh_offset + sec->sh_size));
343 (
const char *)(base() + sec->sh_offset));
348 (
const char *)(base() + sec->sh_offset + sec->sh_size));
356 (
const char*)base() + Header->
e_phoff);
361 (
const char*)base() +
390 template <
class ELFT>
392 unsigned vd_size = sec->sh_size;
393 unsigned vd_count = sec->sh_info;
394 const char *sec_start = (
const char*)base() + sec->sh_offset;
395 const char *sec_end = sec_start + vd_size;
397 const char *p = sec_start;
398 for (
unsigned i = 0; i < vd_count; i++) {
399 if (p +
sizeof(Elf_Verdef) > sec_end)
401 "version definitions.");
402 const Elf_Verdef *vd =
reinterpret_cast<const Elf_Verdef *
>(p);
406 if (index >= VersionMap.size())
407 VersionMap.resize(index + 1);
408 VersionMap[index] = VersionMapEntry(vd);
415 template <
class ELFT>
417 unsigned vn_size = sec->sh_size;
418 unsigned vn_count = sec->sh_info;
419 const char *sec_start = (
const char *)base() + sec->sh_offset;
420 const char *sec_end = sec_start + vn_size;
422 const char *p = sec_start;
423 for (
unsigned i = 0; i < vn_count; i++) {
424 if (p +
sizeof(Elf_Verneed) > sec_end)
426 "version needed records.");
427 const Elf_Verneed *vn =
reinterpret_cast<const Elf_Verneed *
>(p);
431 const char *paux = p + vn->vn_aux;
432 for (
unsigned j = 0; j < vn->vn_cnt; j++) {
433 if (paux +
sizeof(Elf_Vernaux) > sec_end)
435 "version needed records.");
436 const Elf_Vernaux *vna =
reinterpret_cast<const Elf_Vernaux *
>(paux);
438 if (index >= VersionMap.size())
439 VersionMap.resize(index + 1);
440 VersionMap[index] = VersionMapEntry(vna);
441 paux += vna->vna_next;
447 template <
class ELFT>
448 void ELFFile<ELFT>::LoadVersionMap()
const {
450 if (!DotDynSymSec || !dot_gnu_version_sec)
454 if (VersionMap.size() > 0)
459 VersionMap.push_back(VersionMapEntry());
460 VersionMap.push_back(VersionMapEntry());
462 if (dot_gnu_version_d_sec)
463 LoadVersionDefs(dot_gnu_version_d_sec);
465 if (dot_gnu_version_r_sec)
466 LoadVersionNeeds(dot_gnu_version_r_sec);
469 template <
class ELFT>
473 return ExtendedSymbolTable.lookup(symb);
476 template <
class ELFT>
479 uint32_t Index = symb->st_shndx;
481 return getSection(ExtendedSymbolTable.lookup(symb));
484 return getSection(symb->st_shndx);
487 template <
class ELFT>
490 return &*(symbol_begin() + Index);
493 template <
class ELFT>
496 if (Sec->sh_offset + Sec->sh_size > Buf.size())
498 const uint8_t *Start = base() + Sec->sh_offset;
502 template <
class ELFT>
507 template <
class ELFT>
510 if (!isMipsELF64()) {
520 uint8_t Type1 = (Type >> 0) & 0xFF;
521 uint8_t
Type2 = (Type >> 8) & 0xFF;
522 uint8_t
Type3 = (Type >> 16) & 0xFF;
528 Name = getRelocationTypeName(Type2);
532 Name = getRelocationTypeName(Type3);
538 template <
class ELFT>
539 template <
class RelT>
540 std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
544 return std::make_pair(
nullptr,
nullptr);
546 if (std::error_code EC = SymTableOrErr.
getError())
548 const Elf_Shdr *SymTable = *SymTableOrErr;
549 return std::make_pair(
550 SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
553 template <
class ELFT>
555 assert(Header &&
"Header not initialized!");
557 assert(SectionHeaderTable &&
"SectionHeaderTable not initialized!");
558 return SectionHeaderTable->sh_size;
560 return Header->e_shnum;
563 template <
class ELFT>
567 return SectionHeaderTable->sh_link;
568 if (Header->e_shstrndx >= getNumSections())
571 return Header->e_shstrndx;
574 template <
class ELFT>
577 const uint64_t FileSize = Buf.
size();
585 Header =
reinterpret_cast<const Elf_Ehdr *
>(base());
592 const uint64_t SectionTableOffset = Header->
e_shoff;
594 if (SectionTableOffset +
sizeof(
Elf_Shdr) > FileSize) {
602 reinterpret_cast<const Elf_Shdr *
>(base() + SectionTableOffset);
605 if (SectionTableOffset + SectionTableSize > FileSize) {
614 switch (Sec.sh_type) {
620 HashTable =
reinterpret_cast<const Elf_Hash *
>(base() + Sec.sh_offset);
623 if (SymbolTableSectionHeaderIndex) {
628 SymbolTableSectionHeaderIndex = &Sec;
631 if (dot_symtab_sec) {
636 dot_symtab_sec = &Sec;
643 DotStrtab = *SymtabOrErr;
658 DynStrRegion.Addr = SymtabOrErr->data();
659 DynStrRegion.Size = SymtabOrErr->size();
660 DynStrRegion.EntSize = 1;
664 if (DynamicRegion.Addr) {
669 DynamicRegion.Addr = base() + Sec.sh_offset;
670 DynamicRegion.Size = Sec.sh_size;
671 DynamicRegion.EntSize = Sec.sh_entsize;
674 if (dot_gnu_version_sec !=
nullptr) {
679 dot_gnu_version_sec = &Sec;
682 if (dot_gnu_version_d_sec !=
nullptr) {
687 dot_gnu_version_d_sec = &Sec;
690 if (dot_gnu_version_r_sec !=
nullptr) {
695 dot_gnu_version_r_sec = &Sec;
702 if ((EC = StrTabSecOrErr.
getError()))
708 DotShstrtab = *SymtabOrErr;
711 if (SymbolTableSectionHeaderIndex) {
712 const Elf_Word *ShndxTable =
reinterpret_cast<const Elf_Word*
>(base() +
713 SymbolTableSectionHeaderIndex->sh_offset);
716 ExtendedSymbolTable[&S] = *ShndxTable;
723 EC = std::error_code();
726 template <
class ELFT>
733 typename LoadMapT::Allocator Alloc;
736 std::unique_ptr<LoadMapT> LoadMap(
new LoadMapT(Alloc));
738 for (Elf_Phdr_Iter PhdrI = program_header_begin(),
739 PhdrE = program_header_end();
740 PhdrI != PhdrE; ++PhdrI) {
742 DynamicRegion.Addr = base() + PhdrI->p_offset;
743 DynamicRegion.Size = PhdrI->p_filesz;
744 DynamicRegion.EntSize =
sizeof(Elf_Dyn);
749 if (PhdrI->p_filesz == 0)
751 LoadMap->insert(PhdrI->p_vaddr, PhdrI->p_vaddr + PhdrI->p_filesz,
755 auto toMappedAddr = [&](uint64_t VAddr) ->
const uint8_t * {
756 auto I = LoadMap->find(VAddr);
757 if (
I == LoadMap->end())
759 return this->base() +
I.value() + (VAddr -
I.start());
762 for (Elf_Dyn_Iter DynI = dynamic_table_begin(), DynE = dynamic_table_end();
763 DynI != DynE; ++DynI) {
764 switch (DynI->d_tag) {
769 reinterpret_cast<const Elf_Hash *
>(toMappedAddr(DynI->getPtr()));
772 if (!DynStrRegion.Addr)
773 DynStrRegion.Addr = toMappedAddr(DynI->getPtr());
776 if (!DynStrRegion.Size)
777 DynStrRegion.Size = DynI->getVal();
780 if (!DynRelaRegion.Addr)
781 DynRelaRegion.Addr = toMappedAddr(DynI->getPtr());
784 DynRelaRegion.Size = DynI->getVal();
787 DynRelaRegion.EntSize = DynI->getVal();
792 template <
class ELFT>
794 if (Header->e_shentsize !=
sizeof(
Elf_Shdr))
796 "Invalid section header entry size (e_shentsize) in ELF header");
797 return reinterpret_cast<const Elf_Shdr *
>(base() + Header->e_shoff);
800 template <
class ELFT>
802 return section_begin() + getNumSections();
805 template <
class ELFT>
809 if (dot_symtab_sec->sh_entsize !=
sizeof(
Elf_Sym))
811 return reinterpret_cast<const Elf_Sym *
>(base() + dot_symtab_sec->sh_offset);
814 template <
class ELFT>
818 return reinterpret_cast<const Elf_Sym *
>(base() + dot_symtab_sec->sh_offset +
819 dot_symtab_sec->sh_size);
822 template <
class ELFT>
825 if (DynamicRegion.Addr)
827 (
const char *)DynamicRegion.Addr);
831 template <
class ELFT>
834 if (!DynamicRegion.Addr)
837 (
const char *)DynamicRegion.Addr + DynamicRegion.Size);
852 template <
class ELFT>
857 for (
const auto &Entry : dynamic_table())
859 dt_soname = getDynamicString(Entry.getVal());
866 template <
class ELFT>
867 template <
typename T>
870 if (std::error_code EC = Sec.
getError())
872 return getEntry<T>(*Sec, Entry);
875 template <
class ELFT>
876 template <
typename T>
878 uint32_t Entry)
const {
879 return reinterpret_cast<const T *
>(base() + Section->sh_offset +
880 (Entry * Section->sh_entsize));
883 template <
class ELFT>
886 assert(SectionHeaderTable &&
"SectionHeaderTable not initialized!");
887 if (Index >= getNumSections())
890 return reinterpret_cast<const Elf_Shdr *
>(
891 reinterpret_cast<const char *
>(SectionHeaderTable) +
892 (Index * Header->e_shentsize));
895 template <
class ELFT>
900 uint64_t Offset = Section->sh_offset;
901 uint64_t Size = Section->sh_size;
902 if (Offset + Size > Buf.size())
904 StringRef Data((
const char *)base() + Section->sh_offset, Size);
905 if (Data[Size - 1] !=
'\0')
910 template <
class ELFT>
912 if (Offset >= DynStrRegion.Size)
914 return (
const char *)DynStrRegion.Addr + Offset;
917 template <
class ELFT>
920 return Symb->
getName(DotStrtab);
923 template <
class ELFT>
926 return StringRef(getDynamicString(Symb->st_name));
929 template <
class ELFT>
931 bool IsDynamic)
const {
933 return getDynamicSymbolName(Symb);
934 return getStaticSymbolName(Symb);
937 template <
class ELFT>
940 uint32_t Offset = Section->sh_name;
941 if (Offset >= DotShstrtab.size())
943 return StringRef(DotShstrtab.data() + Offset);
946 template <
class ELFT>
949 bool &IsDefault)
const {
953 if (std::error_code EC = StrTabOrErr.
getError())
955 StrTab = *StrTabOrErr;
958 if (section != DotDynSymSec && section !=
nullptr) {
966 size_t atpos = Name.
find(
'@');
972 if (atpos < Name.
size() && Name[atpos] ==
'@') {
978 return Name.
substr(atpos);
982 if (!dot_gnu_version_sec) {
990 (
reinterpret_cast<uintptr_t
>(symb) - DotDynSymSec->sh_offset -
991 reinterpret_cast<uintptr_t>(base())) /
995 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
1007 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
1009 const VersionMapEntry &entry = VersionMap[version_index];
1013 if (entry.isVerdef()) {
1015 name_offset = entry.getVerdef()->getAux()->vda_name;
1017 name_offset = entry.getVernaux()->vna_name;
1021 if (entry.isVerdef()) {
1027 if (name_offset >= DynStrRegion.Size)
1029 return StringRef(getDynamicString(name_offset));
1037 for (
unsigned i = 0, j = symbolName.
size(); i < j; i++) {
1038 h = (h << 4) + symbolName[i];
1039 g = h & 0xf0000000L;
std::conditional< ELFT::Is64Bits, uint64_t, uint32_t >::type uintX_t
Elf_Rel_Iter rel_end(const Elf_Shdr *sec) const
std::error_code getError() const
const Elf_Sym * symbol_end() const
Represents either an error or a value T.
ErrorOr< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr *Sec) const
StringRef getRelocationTypeName(uint32_t Type) const
Elf_Dyn_Iter dynamic_table_end(bool NULLEnd=false) const
Iterate over constant sized entities.
size_t size() const
size - Get the string size.
ErrorOr< StringRef > getSymbolName(const Elf_Sym *Symb, bool IsDynamic) const
Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section (.gnu.version_d).
const Elf_Sym * symbol_begin() const
unsigned char getDataEncoding() const
ELFEntityIterator(uintX_t EntSize, const char *Start)
ELFFile< ELFType< support::big, true > > ELF64BEFile
Elf_Rela_Iter dyn_rela_end() const
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
ELFFile< ELFType< support::little, false > > ELF32LEFile
Elf_Rela_Iter rela_end(const Elf_Shdr *sec) const
ErrorOr< StringRef > getSectionName(const Elf_Shdr *Section) const
const Elf_Shdr * getDotDynSymSec() const
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Elf_Versym_Impl< ELFT > Elf_Versym
ELF::Elf64_Word getExtendedSymbolTableIndex(const Elf_Sym *symb) const
Elf_Verdaux_Impl< ELFT > Elf_Verdaux
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
ELFEntityIterator< const Elf_Rela > Elf_Rela_Iter
Elf_Phdr_Iter program_header_end() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
Elf_Ehdr_Impl< ELFT > Elf_Ehdr
const Elf_Shdr * section_end() const
Elf_Dyn_Iter dynamic_table_begin() const
unsigned char getFileClass() const
Elf_Rel_Impl< ELFT, false > Elf_Rel
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Elf_Sym_Range dynamic_symbols() const
Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed section (.gnu.version_r).
ELFEntityIterator & operator-(difference_type n)
ErrorOr< const Elf_Shdr * > getSection(const Elf_Sym *symb) const
iterator_range< const Elf_Shdr * > Elf_Shdr_Range
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Elf_Phdr_Impl< ELFT > Elf_Phdr
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
COFF::MachineTypes Machine
Elf_Verdef_Impl< ELFT > Elf_Verdef
Elf_Rel_Impl< ELFT, true > Elf_Rela
uintX_t getStringTableIndex() const
Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef section (...
iterator_range< const Elf_Sym * > Elf_Sym_Range
ErrorOr< StringRef > getName(StringRef StrTab) const
Elf_Dyn_Range dynamic_table(bool NULLEnd=false) const
Elf_Hash_Impl< ELFT > Elf_Hash
ErrorOr< StringRef > getSymbolVersion(const Elf_Shdr *section, const Elf_Sym *Symb, bool &IsDefault) const
The instances of the Type class are immutable: once they are created, they are never changed...
uint64_t getNumSections() const
ELFEntityIterator & operator++()
static unsigned elf_hash(StringRef &symbolName)
This function returns the hash value for a symbol in the .dynsym section Name of the API remains cons...
bool operator!=(const ELFEntityIterator &Other)
const char * getDynamicString(uintX_t Offset) const
ELFFile(StringRef Object, std::error_code &EC)
Elf_Sym_Impl< ELFT > Elf_Sym
const Elf_Sym * getSymbol(uint32_t index) const
static const void * getAsVoidPointer(T *P)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
ELFEntityIterator()
Default construct iterator.
ELFEntityIterator & operator+(difference_type n)
Elf_Sym_Range symbols() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Elf_Phdr_Iter program_header_begin() const
ErrorOr< StringRef > getStringTable(const Elf_Shdr *Section) const
const void * getPointer() const
const T * getEntry(uint32_t Section, uint32_t Entry) const
ELFEntityIterator< const Elf_Dyn > Elf_Dyn_Iter
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Archive files are 2 byte aligned, so we need this for PointerIntPair to work.
Elf_Dyn_Impl< ELFT > Elf_Dyn
A range adaptor for a pair of iterators.
const Elf_Hash * getHashTable() const
iterator_range< Elf_Dyn_Iter > Elf_Dyn_Range
ELFFile< ELFType< support::little, true > > ELF64LEFile
const Elf_Shdr * section_begin() const
Elf_Versym: This is the structure of entries in the SHT_GNU_versym section (.gnu.version).
ptrdiff_t difference_type
ELFEntityIterator< const Elf_Rel > Elf_Rel_Iter
bool operator==(const ELFEntityIterator &Other)
void VerifyStrTab(const Elf_Shdr *sh) const
const Elf_Sym * dynamic_symbol_end() const
Elf_Rel_Iter rel_begin(const Elf_Shdr *sec) const
Elf_Verneed_Impl< ELFT > Elf_Verneed
ErrorOr< StringRef > getDynamicSymbolName(const Elf_Sym *Symb) const
Provides ErrorOr<T> smart pointer.
const Elf_Shdr * getDotSymtabSec() const
static T * getFromVoidPointer(const void *P)
Elf_Vernaux_Impl< ELFT > Elf_Vernaux
Elf_Rela_Iter dyn_rela_begin() const
std::forward_iterator_tag iterator_category
Elf_Shdr_Range sections() const
std::pair< const Elf_Shdr *, const Elf_Sym * > getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const
Get the symbol table section and symbol for a given relocation.
Elf_Shdr_Impl< ELFT > Elf_Shdr
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
ErrorOr< StringRef > getStaticSymbolName(const Elf_Sym *Symb) const
const Elf_Sym * dynamic_symbol_begin() const
StringRef getLoadName() const
const Elf_Ehdr * getHeader() const
uintX_t getEntSize() const
ELFEntityIterator< const Elf_Phdr > Elf_Phdr_Iter
Iterate over program header table.
Elf_Rela_Iter rela_begin(const Elf_Shdr *sec) const
Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed section (.gnu.version_r).
ELFFile< ELFType< support::big, false > > ELF32BEFile