diff --git a/llvm/test/tools/llvm-readobj/ELF/addrsig.test b/llvm/test/tools/llvm-readobj/ELF/addrsig.test index 1234a42a839a..c4793aae7b73 100644 --- a/llvm/test/tools/llvm-readobj/ELF/addrsig.test +++ b/llvm/test/tools/llvm-readobj/ELF/addrsig.test @@ -58,7 +58,7 @@ Sections: # INVALID-INDEX: Addrsig [ # INVALID-INDEX-NEXT: Sym: foo (1) -# INVALID-INDEX-NEXT: warning: '[[FILE]]': unable to get symbol from section [index 2]: invalid symbol index (255) +# INVALID-INDEX-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 255: unable to get symbol from section [index 2]: invalid symbol index (255) # INVALID-INDEX-NEXT: Sym: (255) # INVALID-INDEX-NEXT: Sym: bar (2) # INVALID-INDEX-NEXT: ] diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 3d51515682d8..b284aae84470 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -343,7 +343,7 @@ public: const Elf_Sym *FirstSym) const; Expected getSymbolSectionName(const Elf_Sym *Symbol, unsigned SectionIndex) const; - Expected getStaticSymbolName(uint32_t Index) const; + std::string getStaticSymbolName(uint32_t Index) const; StringRef getDynamicString(uint64_t Value) const; Expected getSymbolVersionByIndex(uint32_t VersionSymbolIndex, bool &IsDefault) const; @@ -1131,21 +1131,27 @@ static std::string maybeDemangle(StringRef Name) { } template -Expected -ELFDumper::getStaticSymbolName(uint32_t Index) const { +std::string ELFDumper::getStaticSymbolName(uint32_t Index) const { + auto Warn = [&](Error E) -> std::string { + this->reportUniqueWarning( + createError("unable to read the name of symbol with index " + + Twine(Index) + ": " + toString(std::move(E)))); + return ""; + }; + const ELFFile *Obj = ObjF->getELFFile(); Expected SymOrErr = Obj->getSymbol(DotSymtabSec, Index); if (!SymOrErr) - return SymOrErr.takeError(); + return Warn(SymOrErr.takeError()); Expected StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec); if (!StrTabOrErr) - return StrTabOrErr.takeError(); + return Warn(StrTabOrErr.takeError()); Expected NameOrErr = (*SymOrErr)->getName(*StrTabOrErr); if (!NameOrErr) - return NameOrErr.takeError(); + return Warn(NameOrErr.takeError()); return maybeDemangle(*NameOrErr); } @@ -6575,21 +6581,12 @@ void LLVMStyle::printCGProfile(const ELFFile *Obj) { return; } - auto GetSymName = [&](uint32_t Index) -> std::string { - if (Expected NameOrErr = - this->dumper()->getStaticSymbolName(Index)) - return *NameOrErr; - else - this->reportUniqueWarning( - createError("unable to read the name of symbol with index " + - Twine(Index) + ": " + toString(NameOrErr.takeError()))); - return ""; - }; - for (const Elf_CGProfile &CGPE : *CGProfileOrErr) { DictScope D(W, "CGProfileEntry"); - W.printNumber("From", GetSymName(CGPE.cgp_from), CGPE.cgp_from); - W.printNumber("To", GetSymName(CGPE.cgp_to), CGPE.cgp_to); + W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from), + CGPE.cgp_from); + W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to), + CGPE.cgp_to); W.printNumber("Weight", CGPE.cgp_weight); } }