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.

llvm-svn: 246656
This commit is contained in:
Rafael Espindola 2015-09-02 15:07:39 +00:00
parent 8475466860
commit 7d250f773f
4 changed files with 50 additions and 45 deletions

View File

@ -88,8 +88,8 @@ public:
/// \brief Get the symbol table section and symbol for a given relocation. /// \brief Get the symbol table section and symbol for a given relocation.
template <class RelT> template <class RelT>
std::pair<const Elf_Shdr *, const Elf_Sym *> const Elf_Sym *getRelocationSymbol(const RelT *Rel,
getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const; const Elf_Shdr *SymTab) const;
ELFFile(StringRef Object, std::error_code &EC); ELFFile(StringRef Object, std::error_code &EC);
@ -290,17 +290,13 @@ void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
template <class ELFT> template <class ELFT>
template <class RelT> template <class RelT>
std::pair<const typename ELFFile<ELFT>::Elf_Shdr *, const typename ELFFile<ELFT>::Elf_Sym *
const typename ELFFile<ELFT>::Elf_Sym *> ELFFile<ELFT>::getRelocationSymbol(const RelT *Rel,
ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const { const Elf_Shdr *SymTab) const {
if (!Sec->sh_link) uint32_t Index = Rel->getSymbol(isMips64EL());
return std::make_pair(nullptr, nullptr); if (Index == 0)
ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection(Sec->sh_link); return nullptr;
if (std::error_code EC = SymTableOrErr.getError()) return getEntry<Elf_Sym>(SymTab, Index);
report_fatal_error(EC.message());
const Elf_Shdr *SymTable = *SymTableOrErr;
return std::make_pair(
SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol(isMips64EL())));
} }
template <class ELFT> template <class ELFT>

View File

@ -375,6 +375,10 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex) if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
continue; continue;
ErrorOr<const Elf_Shdr *> SymTabOrErr = ELF->getSection(Sec.sh_link);
error(SymTabOrErr.getError());
const Elf_Shdr *SymTab = *SymTabOrErr;
for (const Elf_Rel &R : ELF->rels(&Sec)) { for (const Elf_Rel &R : ELF->rels(&Sec)) {
if (R.r_offset != static_cast<unsigned>(IndexTableOffset)) if (R.r_offset != static_cast<unsigned>(IndexTableOffset))
continue; continue;
@ -384,11 +388,10 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
RelA.r_info = R.r_info; RelA.r_info = R.r_info;
RelA.r_addend = 0; RelA.r_addend = 0;
std::pair<const Elf_Shdr *, const Elf_Sym *> Symbol = const Elf_Sym *Symbol = ELF->getRelocationSymbol(&RelA, SymTab);
ELF->getRelocationSymbol(&Sec, &RelA);
ErrorOr<const Elf_Shdr *> Ret = ErrorOr<const Elf_Shdr *> Ret =
ELF->getSection(Symbol.second, Symbol.first, ShndxTable); ELF->getSection(Symbol, SymTab, ShndxTable);
if (std::error_code EC = Ret.getError()) if (std::error_code EC = Ret.getError())
report_fatal_error(EC.message()); report_fatal_error(EC.message());
return *Ret; return *Ret;

View File

@ -100,7 +100,7 @@ private:
StringRef StrTable, bool IsDynamic); StringRef StrTable, bool IsDynamic);
void printRelocations(const Elf_Shdr *Sec); 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); void printValue(uint64_t Type, uint64_t Value);
const Elf_Rela *dyn_rela_begin() const; const Elf_Rela *dyn_rela_begin() const;
@ -1095,6 +1095,10 @@ void ELFDumper<ELFT>::printDynamicRelocations() {
template <class ELFT> template <class ELFT>
void ELFDumper<ELFT>::printRelocations(const Elf_Shdr *Sec) { void ELFDumper<ELFT>::printRelocations(const Elf_Shdr *Sec) {
ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj->getSection(Sec->sh_link);
error(SymTabOrErr.getError());
const Elf_Shdr *SymTab = *SymTabOrErr;
switch (Sec->sh_type) { switch (Sec->sh_type) {
case ELF::SHT_REL: case ELF::SHT_REL:
for (const Elf_Rel &R : Obj->rels(Sec)) { for (const Elf_Rel &R : Obj->rels(Sec)) {
@ -1102,35 +1106,32 @@ void ELFDumper<ELFT>::printRelocations(const Elf_Shdr *Sec) {
Rela.r_offset = R.r_offset; Rela.r_offset = R.r_offset;
Rela.r_info = R.r_info; Rela.r_info = R.r_info;
Rela.r_addend = 0; Rela.r_addend = 0;
printRelocation(Sec, Rela); printRelocation(Rela, SymTab);
} }
break; break;
case ELF::SHT_RELA: case ELF::SHT_RELA:
for (const Elf_Rela &R : Obj->relas(Sec)) for (const Elf_Rela &R : Obj->relas(Sec))
printRelocation(Sec, R); printRelocation(R, SymTab);
break; break;
} }
} }
template <class ELFT> template <class ELFT>
void ELFDumper<ELFT>::printRelocation(const Elf_Shdr *Sec, Elf_Rela Rel) { void ELFDumper<ELFT>::printRelocation(Elf_Rela Rel, const Elf_Shdr *SymTab) {
SmallString<32> RelocName; SmallString<32> RelocName;
Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName); Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
StringRef TargetName; StringRef TargetName;
std::pair<const Elf_Shdr *, const Elf_Sym *> Sym = const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab);
Obj->getRelocationSymbol(Sec, &Rel); if (Sym && Sym->getType() == ELF::STT_SECTION) {
if (Sym.second && Sym.second->getType() == ELF::STT_SECTION) { ErrorOr<const Elf_Shdr *> Sec = Obj->getSection(Sym, SymTab, ShndxTable);
ErrorOr<const Elf_Shdr *> Sec =
Obj->getSection(Sym.second, Sym.first, ShndxTable);
error(Sec.getError()); error(Sec.getError());
ErrorOr<StringRef> SecName = Obj->getSectionName(*Sec); ErrorOr<StringRef> SecName = Obj->getSectionName(*Sec);
if (SecName) if (SecName)
TargetName = SecName.get(); TargetName = SecName.get();
} else if (Sym.first) { } else if (Sym) {
const Elf_Shdr *SymTable = Sym.first; ErrorOr<StringRef> StrTableOrErr = Obj->getStringTableForSymtab(*SymTab);
ErrorOr<StringRef> StrTableOrErr = Obj->getStringTableForSymtab(*SymTable);
error(StrTableOrErr.getError()); error(StrTableOrErr.getError());
TargetName = errorOrDefault(Sym.second->getName(*StrTableOrErr)); TargetName = errorOrDefault(Sym->getName(*StrTableOrErr));
} }
if (opts::ExpandRelocs) { if (opts::ExpandRelocs) {
@ -1767,7 +1768,8 @@ template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
ErrorOr<const Elf_Shdr *> SymTableOrErr = ErrorOr<const Elf_Shdr *> SymTableOrErr =
Obj->getSection(PLTRelShdr->sh_link); Obj->getSection(PLTRelShdr->sh_link);
error(SymTableOrErr.getError()); error(SymTableOrErr.getError());
ErrorOr<StringRef> StrTable = Obj->getStringTableForSymtab(**SymTableOrErr); const Elf_Shdr *SymTable = *SymTableOrErr;
ErrorOr<StringRef> StrTable = Obj->getStringTableForSymtab(*SymTable);
error(StrTable.getError()); error(StrTable.getError());
const GOTEntry *PLTBegin = makeGOTIter(*PLT, 0); const GOTEntry *PLTBegin = makeGOTIter(*PLT, 0);
@ -1789,8 +1791,7 @@ template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr), for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr),
*RE = Obj->rel_end(PLTRelShdr); *RE = Obj->rel_end(PLTRelShdr);
RI != RE && It != PLTEnd; ++RI, ++It) { RI != RE && It != PLTEnd; ++RI, ++It) {
const Elf_Sym *Sym = const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
} }
break; break;
@ -1798,8 +1799,7 @@ template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr), for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr),
*RE = Obj->rela_end(PLTRelShdr); *RE = Obj->rela_end(PLTRelShdr);
RI != RE && It != PLTEnd; ++RI, ++It) { RI != RE && It != PLTEnd; ++RI, ++It) {
const Elf_Sym *Sym = const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable);
Obj->getRelocationSymbol(&*PLTRelShdr, &*RI).second;
printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, *StrTable, Sym);
} }
break; break;

View File

@ -34,7 +34,7 @@ class ELFDumper {
std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr, std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
ELFYAML::RelocationSection &S); ELFYAML::RelocationSection &S);
template <class RelT> template <class RelT>
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); ELFYAML::Relocation &R);
ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr); ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
@ -201,18 +201,14 @@ ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
template <class ELFT> template <class ELFT>
template <class RelT> template <class RelT>
std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr, std::error_code ELFDumper<ELFT>::dumpRelocation(const RelT *Rel,
const RelT *Rel, const Elf_Shdr *SymTab,
ELFYAML::Relocation &R) { ELFYAML::Relocation &R) {
R.Type = Rel->getType(Obj.isMips64EL()); R.Type = Rel->getType(Obj.isMips64EL());
R.Offset = Rel->r_offset; R.Offset = Rel->r_offset;
R.Addend = 0; R.Addend = 0;
auto NamePair = Obj.getRelocationSymbol(Shdr, Rel); const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
if (!NamePair.first)
return obj2yaml_error::success;
const Elf_Shdr *SymTab = NamePair.first;
ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link); ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
if (std::error_code EC = StrTabSec.getError()) if (std::error_code EC = StrTabSec.getError())
return EC; return EC;
@ -221,7 +217,7 @@ std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
return EC; return EC;
StringRef StrTab = *StrTabOrErr; StringRef StrTab = *StrTabOrErr;
ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab); ErrorOr<StringRef> NameOrErr = Sym->getName(StrTab);
if (std::error_code EC = NameOrErr.getError()) if (std::error_code EC = NameOrErr.getError())
return EC; return EC;
R.Symbol = NameOrErr.get(); R.Symbol = NameOrErr.get();
@ -283,9 +279,14 @@ ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC; return EC;
ErrorOr<const Elf_Shdr *> 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) { for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
ELFYAML::Relocation R; ELFYAML::Relocation R;
if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
return EC; return EC;
S->Relocations.push_back(R); S->Relocations.push_back(R);
} }
@ -302,10 +303,15 @@ ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC; return EC;
ErrorOr<const Elf_Shdr *> 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; for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
++RI) { ++RI) {
ELFYAML::Relocation R; ELFYAML::Relocation R;
if (std::error_code EC = dumpRelocation(Shdr, &*RI, R)) if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
return EC; return EC;
R.Addend = RI->r_addend; R.Addend = RI->r_addend;
S->Relocations.push_back(R); S->Relocations.push_back(R);