[llvm-readobj] - Refactor ELFDumper<ELFT>::getStaticSymbolName.

This is a followup for D83129.
It is possible to make `getStaticSymbolName` report warnings inside
and return the "<?>" on a error. This allows to encapsulate errors handling
and slightly simplifies the logic in callers code.

Differential revision: https://reviews.llvm.org/D83208
This commit is contained in:
Georgii Rymar 2020-07-06 13:45:49 +03:00
parent 2d9bd448c9
commit 2953ac0975
2 changed files with 17 additions and 20 deletions

View File

@ -58,7 +58,7 @@ Sections:
# INVALID-INDEX: Addrsig [ # INVALID-INDEX: Addrsig [
# INVALID-INDEX-NEXT: Sym: foo (1) # 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: <?> (255)
# INVALID-INDEX-NEXT: Sym: bar (2) # INVALID-INDEX-NEXT: Sym: bar (2)
# INVALID-INDEX-NEXT: ] # INVALID-INDEX-NEXT: ]

View File

@ -343,7 +343,7 @@ public:
const Elf_Sym *FirstSym) const; const Elf_Sym *FirstSym) const;
Expected<StringRef> getSymbolSectionName(const Elf_Sym *Symbol, Expected<StringRef> getSymbolSectionName(const Elf_Sym *Symbol,
unsigned SectionIndex) const; unsigned SectionIndex) const;
Expected<std::string> getStaticSymbolName(uint32_t Index) const; std::string getStaticSymbolName(uint32_t Index) const;
StringRef getDynamicString(uint64_t Value) const; StringRef getDynamicString(uint64_t Value) const;
Expected<StringRef> getSymbolVersionByIndex(uint32_t VersionSymbolIndex, Expected<StringRef> getSymbolVersionByIndex(uint32_t VersionSymbolIndex,
bool &IsDefault) const; bool &IsDefault) const;
@ -1131,21 +1131,27 @@ static std::string maybeDemangle(StringRef Name) {
} }
template <typename ELFT> template <typename ELFT>
Expected<std::string> std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
ELFDumper<ELFT>::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<ELFT> *Obj = ObjF->getELFFile(); const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Expected<const typename ELFT::Sym *> SymOrErr = Expected<const typename ELFT::Sym *> SymOrErr =
Obj->getSymbol(DotSymtabSec, Index); Obj->getSymbol(DotSymtabSec, Index);
if (!SymOrErr) if (!SymOrErr)
return SymOrErr.takeError(); return Warn(SymOrErr.takeError());
Expected<StringRef> StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec); Expected<StringRef> StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec);
if (!StrTabOrErr) if (!StrTabOrErr)
return StrTabOrErr.takeError(); return Warn(StrTabOrErr.takeError());
Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr); Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr);
if (!NameOrErr) if (!NameOrErr)
return NameOrErr.takeError(); return Warn(NameOrErr.takeError());
return maybeDemangle(*NameOrErr); return maybeDemangle(*NameOrErr);
} }
@ -6575,21 +6581,12 @@ void LLVMStyle<ELFT>::printCGProfile(const ELFFile<ELFT> *Obj) {
return; return;
} }
auto GetSymName = [&](uint32_t Index) -> std::string {
if (Expected<std::string> 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) { for (const Elf_CGProfile &CGPE : *CGProfileOrErr) {
DictScope D(W, "CGProfileEntry"); DictScope D(W, "CGProfileEntry");
W.printNumber("From", GetSymName(CGPE.cgp_from), CGPE.cgp_from); W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from),
W.printNumber("To", GetSymName(CGPE.cgp_to), CGPE.cgp_to); CGPE.cgp_from);
W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to),
CGPE.cgp_to);
W.printNumber("Weight", CGPE.cgp_weight); W.printNumber("Weight", CGPE.cgp_weight);
} }
} }