Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -114,6 +114,13 @@ template class DumpStyle; +template struct RelSymbol { + RelSymbol(const typename ELFT::Sym *S, const std::string &N) + : Sym(S), Name(N) {} + const typename ELFT::Sym *Sym; + std::string Name; +}; + /// Represents a contiguous uniform range in the file. We cannot just create a /// range directly because when creating one of these from the .dynamic table /// the size, entity size and virtual address are different entries in arbitrary @@ -364,8 +371,8 @@ getVersionDependencies(const Elf_Shdr *Sec) const; template - Expected> - getRelocationTarget(const Elf_Shdr *SymTab, const RelTy &R) const; + Expected> getRelocationTarget(const Elf_Shdr *SymTab, + const RelTy &R) const; std::function WarningHandler; void reportUniqueWarning(Error Err) const; @@ -873,8 +880,7 @@ void printRelRelaReloc(unsigned SecIndex, const Elf_Shdr *SymTab, const RelTy &R, unsigned RelIndex); template - void printRelRelaReloc(const Elf_Sym *Sym, StringRef SymbolName, - const RelTy &R); + void printRelRelaReloc(const RelTy &R, const RelSymbol &RelSym); void printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First, Optional StrTable, bool IsDynamic, bool NonVisibilityBitsUsed) override; @@ -1053,7 +1059,7 @@ template template -Expected> +Expected> ELFDumper::getRelocationTarget(const Elf_Shdr *SymTab, const RelTy &R) const { const ELFFile *Obj = ObjF->getELFFile(); @@ -1062,7 +1068,7 @@ return SymOrErr.takeError(); const Elf_Sym *Sym = *SymOrErr; if (!Sym) - return std::make_pair(nullptr, ""); + return RelSymbol(nullptr, ""); // The st_name field of a STT_SECTION is usually 0 (empty string). // This code block returns the section name. @@ -1073,12 +1079,12 @@ return SecOrErr.takeError(); // A section symbol describes the section at index 0. if (*SecOrErr == nullptr) - return std::make_pair(Sym, ""); + return RelSymbol(Sym, ""); Expected NameOrErr = Obj->getSectionName(*SecOrErr); if (!NameOrErr) return NameOrErr.takeError(); - return std::make_pair(Sym, NameOrErr->str()); + return RelSymbol(Sym, NameOrErr->str()); } Expected StrTableOrErr = Obj->getStringTableForSymtab(*SymTab); @@ -1087,7 +1093,7 @@ std::string SymbolName = getFullSymbolName(Sym, *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM); - return std::make_pair(Sym, SymbolName); + return RelSymbol(Sym, SymbolName); } static std::string maybeDemangle(StringRef Name) { @@ -3621,14 +3627,14 @@ void GNUStyle::printRelRelaReloc(unsigned SecIndex, const Elf_Shdr *SymTab, const RelTy &R, unsigned RelIndex) { - Expected> Target = + Expected> Target = this->dumper()->getRelocationTarget(SymTab, R); if (!Target) this->reportUniqueWarning(createError( "unable to print relocation " + Twine(RelIndex) + " in section " + Twine(SecIndex) + ": " + toString(Target.takeError()))); else - printRelRelaReloc(/*Sym=*/Target->first, /*Name=*/Target->second, R); + printRelRelaReloc(R, *Target); } template @@ -3643,8 +3649,8 @@ template template -void GNUStyle::printRelRelaReloc(const Elf_Sym *Sym, StringRef SymbolName, - const RelTy &R) { +void GNUStyle::printRelRelaReloc(const RelTy &R, + const RelSymbol &RelSym) { // First two fields are bit width dependent. The rest of them are fixed width. unsigned Bias = ELFT::Is64Bits ? 8 : 0; Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias}; @@ -3657,17 +3663,18 @@ this->Obj.getRelocationTypeName(R.getType(this->Obj.isMips64EL()), RelocName); Fields[2].Str = RelocName.c_str(); - if (Sym) - Fields[3].Str = to_string(format_hex_no_prefix(Sym->getValue(), Width)); + if (RelSym.Sym) + Fields[3].Str = + to_string(format_hex_no_prefix(RelSym.Sym->getValue(), Width)); - Fields[4].Str = std::string(SymbolName); + Fields[4].Str = std::string(RelSym.Name); for (const Field &F : Fields) printField(F); std::string Addend; if (Optional A = getAddend(R)) { int64_t RelAddend = *A; - if (!SymbolName.empty()) { + if (!RelSym.Name.empty()) { if (RelAddend < 0) { Addend = " - "; RelAddend = std::abs(RelAddend); @@ -4347,10 +4354,6 @@ } namespace { -template struct RelSymbol { - const typename ELFT::Sym *Sym; - std::string Name; -}; template RelSymbol getSymbolForReloc(const ELFFile &Obj, StringRef FileName, @@ -4392,9 +4395,8 @@ template template void GNUStyle::printDynamicRelocation(const RelTy &R) { - RelSymbol S = - getSymbolForReloc(this->Obj, this->FileName, this->dumper(), R); - printRelRelaReloc(S.Sym, S.Name, R); + printRelRelaReloc( + R, getSymbolForReloc(this->Obj, this->FileName, this->dumper(), R)); } template @@ -6166,7 +6168,7 @@ void LLVMStyle::printRelRelaReloc(unsigned SecIndex, const RelTy &Rel, unsigned RelIndex, const Elf_Shdr *SymTab) { - Expected> Target = + Expected> Target = this->dumper()->getRelocationTarget(SymTab, Rel); if (!Target) { this->reportUniqueWarning(createError( @@ -6175,7 +6177,7 @@ return; } - std::string TargetName = Target->second; + std::string TargetName = Target->Name; SmallString<32> RelocName; this->Obj.getRelocationTypeName(Rel.getType(this->Obj.isMips64EL()), RelocName);