diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h index 7ec475279396..a2ea7bc111e1 100644 --- a/llvm/include/llvm/Object/ELF.h +++ b/llvm/include/llvm/Object/ELF.h @@ -234,6 +234,10 @@ public: const T *getEntry(uint32_t Section, uint32_t Entry) const; template const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; + + const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; } + const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; } + ErrorOr getStringTable(const Elf_Shdr *Section) const; const char *getDynamicString(uintX_t Offset) const; ErrorOr getSymbolVersion(const Elf_Shdr *section, diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h index 5738d128b08e..4966983822cb 100644 --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -244,13 +244,25 @@ protected: } const Elf_Sym *toELFSymIter(DataRefImpl Sym) const { - return reinterpret_cast(Sym.p & ~uintptr_t(1)); + return EF.template getEntry(Sym.d.a, Sym.d.b); } - DataRefImpl toDRI(const Elf_Sym *Sym, bool IsDynamic) const { + DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const { DataRefImpl DRI; - DRI.p = - reinterpret_cast(Sym) | static_cast(IsDynamic); + if (!SymTable) { + DRI.d.a = 0; + DRI.d.b = 0; + return DRI; + } + uint32_t Type = SymTable->sh_type; + assert(Type == ELF::SHT_SYMTAB || Type == ELF::SHT_DYNSYM); + + uintptr_t SHT = reinterpret_cast(EF.section_begin()); + unsigned SymTableIndex = + (reinterpret_cast(SymTable) - SHT) / sizeof(Elf_Shdr); + + DRI.d.a = SymTableIndex; + DRI.d.b = SymbolNum; return DRI; } @@ -343,14 +355,16 @@ typedef ELFObjectFile> ELF64BEObjectFile; template void ELFObjectFile::moveSymbolNext(DataRefImpl &Sym) const { - const Elf_Sym *S = toELFSymIter(Sym); - Sym = toDRI(++S, Sym.p & 1); + ++Sym.d.b; } template ErrorOr ELFObjectFile::getSymbolName(DataRefImpl Sym) const { const Elf_Sym *ESym = toELFSymIter(Sym); - return EF.getSymbolName(ESym, Sym.p & 1); + const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a); + const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link); + StringRef SymTable = *EF.getStringTable(StringTableSec); + return ESym->getName(SymTable); } template @@ -481,8 +495,7 @@ uint32_t ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { Result |= SymbolRef::SF_FormatSpecific; if (EF.getHeader()->e_machine == ELF::EM_ARM) { - ErrorOr NameOrErr = EF.getSymbolName(ESym, Sym.p & 1); - if (NameOrErr) { + if (ErrorOr NameOrErr = getSymbolName(Sym)) { StringRef Name = *NameOrErr; if (Name.startswith("$d") || Name.startswith("$t") || Name.startswith("$a")) @@ -669,9 +682,9 @@ ELFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { bool IsDyn = Rel.d.b & 1; DataRefImpl SymbolData; if (IsDyn) - SymbolData = toDRI(EF.dynamic_symbol_begin() + symbolIdx, true); + SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx); else - SymbolData = toDRI(EF.symbol_begin() + symbolIdx, false); + SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx); return symbol_iterator(SymbolRef(SymbolData, this)); } @@ -768,26 +781,30 @@ ELFObjectFile::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC) template basic_symbol_iterator ELFObjectFile::symbol_begin_impl() const { - DataRefImpl Sym = toDRI(EF.symbol_begin(), false); + DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0); return basic_symbol_iterator(SymbolRef(Sym, this)); } template basic_symbol_iterator ELFObjectFile::symbol_end_impl() const { - DataRefImpl Sym = toDRI(EF.symbol_end(), false); + const Elf_Shdr *SymTab = EF.getDotSymtabSec(); + if (!SymTab) + return symbol_begin_impl(); + DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); return basic_symbol_iterator(SymbolRef(Sym, this)); } template elf_symbol_iterator ELFObjectFile::dynamic_symbol_begin() const { - DataRefImpl Sym = toDRI(EF.dynamic_symbol_begin(), true); + DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0); return symbol_iterator(SymbolRef(Sym, this)); } template elf_symbol_iterator ELFObjectFile::dynamic_symbol_end() const { - DataRefImpl Sym = toDRI(EF.dynamic_symbol_end(), true); - return symbol_iterator(SymbolRef(Sym, this)); + const Elf_Shdr *SymTab = EF.getDotDynSymSec(); + DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); + return basic_symbol_iterator(SymbolRef(Sym, this)); } template