mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-22 07:24:47 +00:00
Don't iterate over the program headers in the constructor of ELFFile.
Not every program needs this information. In particular, it is necessary and sufficient for a static linker to scan the section table. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242833 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -157,23 +157,22 @@ public:
|
||||
|
||||
typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
|
||||
|
||||
const uint8_t *base() const {
|
||||
return reinterpret_cast<const uint8_t *>(Buf.data());
|
||||
}
|
||||
|
||||
private:
|
||||
typedef SmallVector<const Elf_Shdr *, 2> Sections_t;
|
||||
typedef DenseMap<unsigned, unsigned> IndexMap_t;
|
||||
|
||||
StringRef Buf;
|
||||
|
||||
const uint8_t *base() const {
|
||||
return reinterpret_cast<const uint8_t *>(Buf.data());
|
||||
}
|
||||
|
||||
const Elf_Ehdr *Header;
|
||||
const Elf_Shdr *SectionHeaderTable = nullptr;
|
||||
StringRef DotShstrtab; // Section header string table.
|
||||
StringRef DotStrtab; // Symbol header string table.
|
||||
const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
|
||||
const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
|
||||
const Elf_Hash *HashTable = nullptr;
|
||||
|
||||
const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
|
||||
DenseMap<const Elf_Sym *, ELF::Elf64_Word> ExtendedSymbolTable;
|
||||
@@ -182,24 +181,6 @@ private:
|
||||
const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r
|
||||
const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d
|
||||
|
||||
/// \brief Represents a region described by entries in the .dynamic table.
|
||||
struct DynRegionInfo {
|
||||
DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
|
||||
/// \brief Address in current address space.
|
||||
const void *Addr;
|
||||
/// \brief Size in bytes of the region.
|
||||
uintX_t Size;
|
||||
/// \brief Size of each entity in the region.
|
||||
uintX_t EntSize;
|
||||
};
|
||||
|
||||
DynRegionInfo DynamicRegion;
|
||||
DynRegionInfo DynStrRegion;
|
||||
DynRegionInfo DynRelaRegion;
|
||||
|
||||
// SONAME entry in dynamic string table
|
||||
StringRef DTSoname;
|
||||
|
||||
// Records for each version index the corresponding Verdef or Vernaux entry.
|
||||
// This is filled the first time LoadVersionMap() is called.
|
||||
class VersionMapEntry : public PointerIntPair<const void*, 1> {
|
||||
@@ -226,8 +207,6 @@ private:
|
||||
void LoadVersionNeeds(const Elf_Shdr *ec) const;
|
||||
void LoadVersionMap() const;
|
||||
|
||||
void scanDynamicTable();
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
const T *getEntry(uint32_t Section, uint32_t Entry) const;
|
||||
@@ -236,16 +215,11 @@ public:
|
||||
|
||||
const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
|
||||
const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
|
||||
const Elf_Hash *getHashTable() const { return HashTable; }
|
||||
StringRef getDynamicStringTable() const {
|
||||
return StringRef((const char *)DynStrRegion.Addr, DynStrRegion.Size);
|
||||
}
|
||||
|
||||
ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
|
||||
ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
|
||||
|
||||
const char *getDynamicString(uintX_t Offset) const;
|
||||
ErrorOr<StringRef> getSymbolVersion(const Elf_Sym *Symb,
|
||||
ErrorOr<StringRef> getSymbolVersion(StringRef StrTab, const Elf_Sym *Symb,
|
||||
bool &IsDefault) const;
|
||||
void VerifyStrTab(const Elf_Shdr *sh) const;
|
||||
|
||||
@@ -283,12 +257,6 @@ public:
|
||||
return make_range(symbol_begin(), symbol_end());
|
||||
}
|
||||
|
||||
const Elf_Dyn *dynamic_table_begin() const;
|
||||
const Elf_Dyn *dynamic_table_end() const;
|
||||
Elf_Dyn_Range dynamic_table() const {
|
||||
return make_range(dynamic_table_begin(), dynamic_table_end());
|
||||
}
|
||||
|
||||
const Elf_Sym *dynamic_symbol_begin() const {
|
||||
if (!DotDynSymSec)
|
||||
return nullptr;
|
||||
@@ -308,25 +276,8 @@ public:
|
||||
return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
|
||||
}
|
||||
|
||||
const Elf_Rela *dyn_rela_begin() const {
|
||||
if (DynRelaRegion.Size && DynRelaRegion.EntSize != sizeof(Elf_Rela))
|
||||
report_fatal_error("Invalid relocation entry size");
|
||||
return reinterpret_cast<const Elf_Rela *>(DynRelaRegion.Addr);
|
||||
}
|
||||
|
||||
const Elf_Rela *dyn_rela_end() const {
|
||||
uint64_t Size = DynRelaRegion.Size;
|
||||
if (Size % sizeof(Elf_Rela))
|
||||
report_fatal_error("Invalid relocation table size");
|
||||
return dyn_rela_begin() + Size / sizeof(Elf_Rela);
|
||||
}
|
||||
|
||||
typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
|
||||
|
||||
Elf_Rela_Range dyn_relas() const {
|
||||
return make_range(dyn_rela_begin(), dyn_rela_end());
|
||||
}
|
||||
|
||||
const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
|
||||
if (sec->sh_entsize != sizeof(Elf_Rela))
|
||||
report_fatal_error("Invalid relocation entry size");
|
||||
@@ -389,7 +340,6 @@ public:
|
||||
|
||||
ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
|
||||
ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
|
||||
StringRef getLoadName() const;
|
||||
};
|
||||
|
||||
typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
|
||||
@@ -596,10 +546,8 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
|
||||
|
||||
Header = reinterpret_cast<const Elf_Ehdr *>(base());
|
||||
|
||||
if (Header->e_shoff == 0) {
|
||||
scanDynamicTable();
|
||||
if (Header->e_shoff == 0)
|
||||
return;
|
||||
}
|
||||
|
||||
const uint64_t SectionTableOffset = Header->e_shoff;
|
||||
|
||||
@@ -624,13 +572,6 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
|
||||
|
||||
for (const Elf_Shdr &Sec : sections()) {
|
||||
switch (Sec.sh_type) {
|
||||
case ELF::SHT_HASH:
|
||||
if (HashTable) {
|
||||
EC = object_error::parse_failed;
|
||||
return;
|
||||
}
|
||||
HashTable = reinterpret_cast<const Elf_Hash *>(base() + Sec.sh_offset);
|
||||
break;
|
||||
case ELF::SHT_SYMTAB_SHNDX:
|
||||
if (SymbolTableSectionHeaderIndex) {
|
||||
// More than one .symtab_shndx!
|
||||
@@ -708,8 +649,6 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
|
||||
}
|
||||
}
|
||||
|
||||
scanDynamicTable();
|
||||
|
||||
EC = std::error_code();
|
||||
}
|
||||
|
||||
@@ -718,68 +657,6 @@ static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
|
||||
return VAddr < Phdr->p_vaddr;
|
||||
}
|
||||
|
||||
template <class ELFT> void ELFFile<ELFT>::scanDynamicTable() {
|
||||
SmallVector<const Elf_Phdr *, 4> LoadSegments;
|
||||
for (const Elf_Phdr &Phdr : program_headers()) {
|
||||
if (Phdr.p_type == ELF::PT_DYNAMIC) {
|
||||
DynamicRegion.Addr = base() + Phdr.p_offset;
|
||||
DynamicRegion.Size = Phdr.p_filesz;
|
||||
continue;
|
||||
}
|
||||
if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
|
||||
continue;
|
||||
LoadSegments.push_back(&Phdr);
|
||||
}
|
||||
|
||||
auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
|
||||
const Elf_Phdr **I = std::upper_bound(
|
||||
LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr<ELFT>);
|
||||
if (I == LoadSegments.begin())
|
||||
report_fatal_error("Virtual address is not in any segment");
|
||||
--I;
|
||||
const Elf_Phdr &Phdr = **I;
|
||||
uint64_t Delta = VAddr - Phdr.p_vaddr;
|
||||
if (Delta >= Phdr.p_filesz)
|
||||
report_fatal_error("Virtual address is not in any segment");
|
||||
return this->base() + Phdr.p_offset + Delta;
|
||||
};
|
||||
|
||||
uint64_t SONameOffset = 0;
|
||||
for (const Elf_Dyn &Dyn : dynamic_table()) {
|
||||
switch (Dyn.d_tag) {
|
||||
case ELF::DT_HASH:
|
||||
if (HashTable)
|
||||
continue;
|
||||
HashTable =
|
||||
reinterpret_cast<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
|
||||
break;
|
||||
case ELF::DT_STRTAB:
|
||||
if (!DynStrRegion.Addr)
|
||||
DynStrRegion.Addr = toMappedAddr(Dyn.getPtr());
|
||||
break;
|
||||
case ELF::DT_STRSZ:
|
||||
if (!DynStrRegion.Size)
|
||||
DynStrRegion.Size = Dyn.getVal();
|
||||
break;
|
||||
case ELF::DT_RELA:
|
||||
if (!DynRelaRegion.Addr)
|
||||
DynRelaRegion.Addr = toMappedAddr(Dyn.getPtr());
|
||||
break;
|
||||
case ELF::DT_RELASZ:
|
||||
DynRelaRegion.Size = Dyn.getVal();
|
||||
break;
|
||||
case ELF::DT_RELAENT:
|
||||
DynRelaRegion.EntSize = Dyn.getVal();
|
||||
break;
|
||||
case ELF::DT_SONAME:
|
||||
SONameOffset = Dyn.getVal();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (SONameOffset)
|
||||
DTSoname = getDynamicString(SONameOffset);
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
|
||||
if (Header->e_shentsize != sizeof(Elf_Shdr))
|
||||
@@ -810,27 +687,6 @@ const typename ELFFile<ELFT>::Elf_Sym *ELFFile<ELFT>::symbol_end() const {
|
||||
dot_symtab_sec->sh_size);
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
const typename ELFFile<ELFT>::Elf_Dyn *
|
||||
ELFFile<ELFT>::dynamic_table_begin() const {
|
||||
return reinterpret_cast<const Elf_Dyn *>(DynamicRegion.Addr);
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
const typename ELFFile<ELFT>::Elf_Dyn *
|
||||
ELFFile<ELFT>::dynamic_table_end() const {
|
||||
uint64_t Size = DynamicRegion.Size;
|
||||
if (Size % sizeof(Elf_Dyn))
|
||||
report_fatal_error("Invalid dynamic table size");
|
||||
|
||||
return dynamic_table_begin() + Size / sizeof(Elf_Dyn);
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
StringRef ELFFile<ELFT>::getLoadName() const {
|
||||
return DTSoname;
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
template <typename T>
|
||||
const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
|
||||
@@ -886,13 +742,6 @@ ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec) const {
|
||||
return getStringTable(*SectionOrErr);
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
const char *ELFFile<ELFT>::getDynamicString(uintX_t Offset) const {
|
||||
if (Offset >= DynStrRegion.Size)
|
||||
return nullptr;
|
||||
return (const char *)DynStrRegion.Addr + Offset;
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
ErrorOr<StringRef>
|
||||
ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
|
||||
@@ -903,7 +752,8 @@ ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
ErrorOr<StringRef> ELFFile<ELFT>::getSymbolVersion(const Elf_Sym *symb,
|
||||
ErrorOr<StringRef> ELFFile<ELFT>::getSymbolVersion(StringRef StrTab,
|
||||
const Elf_Sym *symb,
|
||||
bool &IsDefault) const {
|
||||
// This is a dynamic symbol. Look in the GNU symbol version table.
|
||||
if (!dot_gnu_version_sec) {
|
||||
@@ -951,9 +801,9 @@ ErrorOr<StringRef> ELFFile<ELFT>::getSymbolVersion(const Elf_Sym *symb,
|
||||
IsDefault = false;
|
||||
}
|
||||
|
||||
if (name_offset >= DynStrRegion.Size)
|
||||
if (name_offset >= StrTab.size())
|
||||
return object_error::parse_failed;
|
||||
return StringRef(getDynamicString(name_offset));
|
||||
return StringRef(StrTab.data() + name_offset);
|
||||
}
|
||||
|
||||
/// This function returns the hash value for a symbol in the .dynsym section
|
||||
|
Reference in New Issue
Block a user