From 3c4c9334b22f59d9ec8728491993ac731e09801a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 2 Sep 2015 15:07:39 +0000 Subject: [PATCH] Pass a symbol table to getRelocationSymbol instead of returning one. This removes a report_fatal_error from library and avoids checking a section property for every section entry. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246656 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 22 +++++++---------- tools/llvm-readobj/ARMEHABIPrinter.h | 9 ++++--- tools/llvm-readobj/ELFDumper.cpp | 36 ++++++++++++++-------------- tools/obj2yaml/elf2yaml.cpp | 28 +++++++++++++--------- 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 092ac6db0bd..e4f49ca0eb3 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -88,8 +88,8 @@ public: /// \brief Get the symbol table section and symbol for a given relocation. template - std::pair - getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const; + const Elf_Sym *getRelocationSymbol(const RelT *Rel, + const Elf_Shdr *SymTab) const; ELFFile(StringRef Object, std::error_code &EC); @@ -290,17 +290,13 @@ void ELFFile::getRelocationTypeName(uint32_t Type, template template -std::pair::Elf_Shdr *, - const typename ELFFile::Elf_Sym *> -ELFFile::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const { - if (!Sec->sh_link) - return std::make_pair(nullptr, nullptr); - ErrorOr SymTableOrErr = getSection(Sec->sh_link); - if (std::error_code EC = SymTableOrErr.getError()) - report_fatal_error(EC.message()); - const Elf_Shdr *SymTable = *SymTableOrErr; - return std::make_pair( - SymTable, getEntry(SymTable, Rel->getSymbol(isMips64EL()))); +const typename ELFFile::Elf_Sym * +ELFFile::getRelocationSymbol(const RelT *Rel, + const Elf_Shdr *SymTab) const { + uint32_t Index = Rel->getSymbol(isMips64EL()); + if (Index == 0) + return nullptr; + return getEntry(SymTab, Index); } template diff --git a/tools/llvm-readobj/ARMEHABIPrinter.h b/tools/llvm-readobj/ARMEHABIPrinter.h index 39bcdaae651..beb5fd4ea04 100644 --- a/tools/llvm-readobj/ARMEHABIPrinter.h +++ b/tools/llvm-readobj/ARMEHABIPrinter.h @@ -375,6 +375,10 @@ PrinterContext::FindExceptionTable(unsigned IndexSectionIndex, if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex) continue; + ErrorOr SymTabOrErr = ELF->getSection(Sec.sh_link); + error(SymTabOrErr.getError()); + const Elf_Shdr *SymTab = *SymTabOrErr; + for (const Elf_Rel &R : ELF->rels(&Sec)) { if (R.r_offset != static_cast(IndexTableOffset)) continue; @@ -384,11 +388,10 @@ PrinterContext::FindExceptionTable(unsigned IndexSectionIndex, RelA.r_info = R.r_info; RelA.r_addend = 0; - std::pair Symbol = - ELF->getRelocationSymbol(&Sec, &RelA); + const Elf_Sym *Symbol = ELF->getRelocationSymbol(&RelA, SymTab); ErrorOr Ret = - ELF->getSection(Symbol.second, Symbol.first, ShndxTable); + ELF->getSection(Symbol, SymTab, ShndxTable); if (std::error_code EC = Ret.getError()) report_fatal_error(EC.message()); return *Ret; diff --git a/tools/llvm-readobj/ELFDumper.cpp b/tools/llvm-readobj/ELFDumper.cpp index 0115c1af5b2..31e200e436c 100644 --- a/tools/llvm-readobj/ELFDumper.cpp +++ b/tools/llvm-readobj/ELFDumper.cpp @@ -100,7 +100,7 @@ private: StringRef StrTable, bool IsDynamic); void printRelocations(const Elf_Shdr *Sec); - void printRelocation(const Elf_Shdr *Sec, Elf_Rela Rel); + void printRelocation(Elf_Rela Rel, const Elf_Shdr *SymTab); void printValue(uint64_t Type, uint64_t Value); const Elf_Rela *dyn_rela_begin() const; @@ -1095,6 +1095,10 @@ void ELFDumper::printDynamicRelocations() { template void ELFDumper::printRelocations(const Elf_Shdr *Sec) { + ErrorOr SymTabOrErr = Obj->getSection(Sec->sh_link); + error(SymTabOrErr.getError()); + const Elf_Shdr *SymTab = *SymTabOrErr; + switch (Sec->sh_type) { case ELF::SHT_REL: for (const Elf_Rel &R : Obj->rels(Sec)) { @@ -1102,35 +1106,32 @@ void ELFDumper::printRelocations(const Elf_Shdr *Sec) { Rela.r_offset = R.r_offset; Rela.r_info = R.r_info; Rela.r_addend = 0; - printRelocation(Sec, Rela); + printRelocation(Rela, SymTab); } break; case ELF::SHT_RELA: for (const Elf_Rela &R : Obj->relas(Sec)) - printRelocation(Sec, R); + printRelocation(R, SymTab); break; } } template -void ELFDumper::printRelocation(const Elf_Shdr *Sec, Elf_Rela Rel) { +void ELFDumper::printRelocation(Elf_Rela Rel, const Elf_Shdr *SymTab) { SmallString<32> RelocName; Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName); StringRef TargetName; - std::pair Sym = - Obj->getRelocationSymbol(Sec, &Rel); - if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) { - ErrorOr Sec = - Obj->getSection(Sym.second, Sym.first, ShndxTable); + const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab); + if (Sym && Sym->getType() == ELF::STT_SECTION) { + ErrorOr Sec = Obj->getSection(Sym, SymTab, ShndxTable); error(Sec.getError()); ErrorOr SecName = Obj->getSectionName(*Sec); if (SecName) TargetName = SecName.get(); - } else if (Sym.first) { - const Elf_Shdr *SymTable = Sym.first; - ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*SymTable); + } else if (Sym) { + ErrorOr StrTableOrErr = Obj->getStringTableForSymtab(*SymTab); error(StrTableOrErr.getError()); - TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr)); + TargetName = errorOrDefault(Sym->getName(*StrTableOrErr)); } if (opts::ExpandRelocs) { @@ -1767,7 +1768,8 @@ template void MipsGOTParser::parsePLT() { ErrorOr SymTableOrErr = Obj->getSection(PLTRelShdr->sh_link); error(SymTableOrErr.getError()); - ErrorOr StrTable = Obj->getStringTableForSymtab(**SymTableOrErr); + const Elf_Shdr *SymTable = *SymTableOrErr; + ErrorOr StrTable = Obj->getStringTableForSymtab(*SymTable); error(StrTable.getError()); const GOTEntry *PLTBegin = makeGOTIter(*PLT, 0); @@ -1789,8 +1791,7 @@ template void MipsGOTParser::parsePLT() { for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr), *RE = Obj->rel_end(PLTRelShdr); RI != RE && It != PLTEnd; ++RI, ++It) { - const Elf_Sym *Sym = - Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second; + const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym); } break; @@ -1798,8 +1799,7 @@ template void MipsGOTParser::parsePLT() { for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr), *RE = Obj->rela_end(PLTRelShdr); RI != RE && It != PLTEnd; ++RI, ++It) { - const Elf_Sym *Sym = - Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second; + const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym); } break; diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index 6aea437aba7..f2b01380734 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -34,7 +34,7 @@ class ELFDumper { std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S); template - std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel, + std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, ELFYAML::Relocation &R); ErrorOr dumpRelSection(const Elf_Shdr *Shdr); @@ -201,18 +201,14 @@ ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, template template -std::error_code ELFDumper::dumpRelocation(const Elf_Shdr *Shdr, - const RelT *Rel, +std::error_code ELFDumper::dumpRelocation(const RelT *Rel, + const Elf_Shdr *SymTab, ELFYAML::Relocation &R) { R.Type = Rel->getType(Obj.isMips64EL()); R.Offset = Rel->r_offset; R.Addend = 0; - auto NamePair = Obj.getRelocationSymbol(Shdr, Rel); - if (!NamePair.first) - return obj2yaml_error::success; - - const Elf_Shdr *SymTab = NamePair.first; + const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab); ErrorOr StrTabSec = Obj.getSection(SymTab->sh_link); if (std::error_code EC = StrTabSec.getError()) return EC; @@ -221,7 +217,7 @@ std::error_code ELFDumper::dumpRelocation(const Elf_Shdr *Shdr, return EC; StringRef StrTab = *StrTabOrErr; - ErrorOr NameOrErr = NamePair.second->getName(StrTab); + ErrorOr NameOrErr = Sym->getName(StrTab); if (std::error_code EC = NameOrErr.getError()) return EC; R.Symbol = NameOrErr.get(); @@ -283,9 +279,14 @@ ELFDumper::dumpRelSection(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) return EC; + ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link); + if (std::error_code EC = SymTabOrErr.getError()) + return EC; + const Elf_Shdr *SymTab = *SymTabOrErr; + for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) { ELFYAML::Relocation R; - if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) + if (std::error_code EC = dumpRelocation(&*RI, SymTab, R)) return EC; S->Relocations.push_back(R); } @@ -302,10 +303,15 @@ ELFDumper::dumpRelaSection(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) return EC; + ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link); + if (std::error_code EC = SymTabOrErr.getError()) + return EC; + const Elf_Shdr *SymTab = *SymTabOrErr; + for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE; ++RI) { ELFYAML::Relocation R; - if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) + if (std::error_code EC = dumpRelocation(&*RI, SymTab, R)) return EC; R.Addend = RI->r_addend; S->Relocations.push_back(R); -- 2.34.1