1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-02-01 05:01:59 +01:00

[llvm-readelf/obj] - Cleanup the code. NFCI.

This:
1) Replaces pointers with references in many places.
2) Adds few TODOs about fixing possible unhandled errors (in ARMEHABIPrinter.h).
3) Replaces `auto`s with actual types.
4) Removes excessive arguments.
5) Adds `const ELFFile<ELFT> &Obj;` member to `ELFDumper` to simplify the code.

Differential revision: https://reviews.llvm.org/D88097
This commit is contained in:
Georgii Rymar 2020-09-19 19:53:51 +03:00
parent 7d764f4ed6
commit 3d8119e026
6 changed files with 404 additions and 451 deletions

View File

@ -327,7 +327,7 @@ class PrinterContext {
typedef typename ET::Word Elf_Word;
ScopedPrinter &SW;
const object::ELFFile<ET> *ELF;
const object::ELFFile<ET> &ELF;
StringRef FileName;
const Elf_Shdr *Symtab;
ArrayRef<Elf_Word> ShndxTable;
@ -351,7 +351,7 @@ class PrinterContext {
void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const;
public:
PrinterContext(ScopedPrinter &SW, const object::ELFFile<ET> *ELF,
PrinterContext(ScopedPrinter &SW, const object::ELFFile<ET> &ELF,
StringRef FileName, const Elf_Shdr *Symtab)
: SW(SW), ELF(ELF), FileName(FileName), Symtab(Symtab) {}
@ -367,12 +367,12 @@ PrinterContext<ET>::FunctionAtAddress(unsigned Section,
uint64_t Address) const {
if (!Symtab)
return inconvertibleErrorCode();
auto StrTableOrErr = ELF->getStringTableForSymtab(*Symtab);
auto StrTableOrErr = ELF.getStringTableForSymtab(*Symtab);
if (!StrTableOrErr)
reportError(StrTableOrErr.takeError(), FileName);
StringRef StrTable = *StrTableOrErr;
for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF->symbols(Symtab)))
for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF.symbols(Symtab)))
if (Sym.st_shndx == Section && Sym.st_value == Address &&
Sym.getType() == ELF::STT_FUNC) {
auto NameOrErr = Sym.getName(StrTable);
@ -398,16 +398,16 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
/// handling table. Use this symbol to recover the actual exception handling
/// table.
for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF->sections())) {
for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) {
if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
continue;
auto SymTabOrErr = ELF->getSection(Sec.sh_link);
auto SymTabOrErr = ELF.getSection(Sec.sh_link);
if (!SymTabOrErr)
reportError(SymTabOrErr.takeError(), FileName);
const Elf_Shdr *SymTab = *SymTabOrErr;
for (const Elf_Rel &R : unwrapOrError(FileName, ELF->rels(Sec))) {
for (const Elf_Rel &R : unwrapOrError(FileName, ELF.rels(Sec))) {
if (R.r_offset != static_cast<unsigned>(IndexTableOffset))
continue;
@ -417,9 +417,9 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
RelA.r_addend = 0;
const Elf_Sym *Symbol =
unwrapOrError(FileName, ELF->getRelocationSymbol(RelA, SymTab));
unwrapOrError(FileName, ELF.getRelocationSymbol(RelA, SymTab));
auto Ret = ELF->getSection(*Symbol, SymTab, ShndxTable);
auto Ret = ELF.getSection(*Symbol, SymTab, ShndxTable);
if (!Ret)
report_fatal_error(errorToErrorCode(Ret.takeError()).message());
return *Ret;
@ -432,7 +432,8 @@ template <typename ET>
void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr *IT,
const Elf_Shdr *EHT,
uint64_t TableEntryOffset) const {
Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(*EHT);
// TODO: handle failure.
Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(*EHT);
if (!Contents)
return;
@ -499,7 +500,8 @@ void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry,
template <typename ET>
void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
const Elf_Shdr *IT) const {
Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(*IT);
// TODO: handle failure.
Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(*IT);
if (!Contents)
return;
@ -553,7 +555,8 @@ void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
if (EHT)
if (auto Name = ELF->getSectionName(*EHT))
// TODO: handle failure.
if (Expected<StringRef> Name = ELF.getSectionName(*EHT))
SW.printString("ExceptionHandlingTable", *Name);
uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr);
@ -570,12 +573,13 @@ void PrinterContext<ET>::PrintUnwindInformation() const {
DictScope UI(SW, "UnwindInformation");
int SectionIndex = 0;
for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF->sections())) {
for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) {
if (Sec.sh_type == ELF::SHT_ARM_EXIDX) {
DictScope UIT(SW, "UnwindIndexTable");
SW.printNumber("SectionIndex", SectionIndex);
if (auto SectionName = ELF->getSectionName(Sec))
// TODO: handle failure.
if (Expected<StringRef> SectionName = ELF.getSectionName(Sec))
SW.printString("SectionName", *SectionName);
SW.printHex("SectionOffset", Sec.sh_offset);

View File

@ -33,13 +33,13 @@ template <typename ELFT> class PrinterContext {
using Elf_Phdr = typename ELFT::Phdr;
ScopedPrinter &W;
const object::ELFObjectFile<ELFT> *ObjF;
const object::ELFObjectFile<ELFT> &ObjF;
void printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const;
void printEHFrame(const Elf_Shdr *EHFrameShdr) const;
public:
PrinterContext(ScopedPrinter &W, const object::ELFObjectFile<ELFT> *ObjF)
PrinterContext(ScopedPrinter &W, const object::ELFObjectFile<ELFT> &ObjF)
: W(W), ObjF(ObjF) {}
void printUnwindInformation() const;
@ -47,11 +47,11 @@ public:
template <class ELFT>
static const typename ELFT::Shdr *
findSectionByAddress(const object::ELFObjectFile<ELFT> *ObjF, uint64_t Addr) {
findSectionByAddress(const object::ELFObjectFile<ELFT> &ObjF, uint64_t Addr) {
Expected<typename ELFT::ShdrRange> SectionsOrErr =
ObjF->getELFFile()->sections();
ObjF.getELFFile()->sections();
if (!SectionsOrErr)
reportError(SectionsOrErr.takeError(), ObjF->getFileName());
reportError(SectionsOrErr.takeError(), ObjF.getFileName());
for (const typename ELFT::Shdr &Shdr : *SectionsOrErr)
if (Shdr.sh_addr == Addr)
@ -61,11 +61,11 @@ findSectionByAddress(const object::ELFObjectFile<ELFT> *ObjF, uint64_t Addr) {
template <typename ELFT>
void PrinterContext<ELFT>::printUnwindInformation() const {
const object::ELFFile<ELFT> *Obj = ObjF->getELFFile();
const object::ELFFile<ELFT> &Obj = *ObjF.getELFFile();
Expected<typename ELFT::PhdrRange> PhdrsOrErr = Obj->program_headers();
Expected<typename ELFT::PhdrRange> PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr)
reportError(PhdrsOrErr.takeError(), ObjF->getFileName());
reportError(PhdrsOrErr.takeError(), ObjF.getFileName());
for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
if (Phdr.p_type != ELF::PT_GNU_EH_FRAME)
@ -74,20 +74,19 @@ void PrinterContext<ELFT>::printUnwindInformation() const {
if (Phdr.p_memsz != Phdr.p_filesz)
reportError(object::createError(
"p_memsz does not match p_filesz for GNU_EH_FRAME"),
ObjF->getFileName());
ObjF.getFileName());
printEHFrameHdr(&Phdr);
break;
}
Expected<typename ELFT::ShdrRange> SectionsOrErr =
ObjF->getELFFile()->sections();
Expected<typename ELFT::ShdrRange> SectionsOrErr = Obj.sections();
if (!SectionsOrErr)
reportError(SectionsOrErr.takeError(), ObjF->getFileName());
reportError(SectionsOrErr.takeError(), ObjF.getFileName());
for (const Elf_Shdr &Shdr : *SectionsOrErr) {
Expected<StringRef> NameOrErr = Obj->getSectionName(Shdr);
Expected<StringRef> NameOrErr = Obj.getSectionName(Shdr);
if (!NameOrErr)
reportError(NameOrErr.takeError(), ObjF->getFileName());
reportError(NameOrErr.takeError(), ObjF.getFileName());
if (*NameOrErr == ".eh_frame")
printEHFrame(&Shdr);
}
@ -101,18 +100,18 @@ void PrinterContext<ELFT>::printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const {
W.startLine() << format("Offset: 0x%" PRIx64 "\n", (uint64_t)EHFramePHdr->p_offset);
W.startLine() << format("Size: 0x%" PRIx64 "\n", (uint64_t)EHFramePHdr->p_memsz);
const object::ELFFile<ELFT> *Obj = ObjF->getELFFile();
const object::ELFFile<ELFT> &Obj = *ObjF.getELFFile();
if (const Elf_Shdr *EHFrameHdr =
findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) {
Expected<StringRef> NameOrErr = Obj->getSectionName(*EHFrameHdr);
Expected<StringRef> NameOrErr = Obj.getSectionName(*EHFrameHdr);
if (!NameOrErr)
reportError(NameOrErr.takeError(), ObjF->getFileName());
reportError(NameOrErr.takeError(), ObjF.getFileName());
W.printString("Corresponding Section", *NameOrErr);
}
Expected<ArrayRef<uint8_t>> Content = Obj->getSegmentContents(*EHFramePHdr);
Expected<ArrayRef<uint8_t>> Content = Obj.getSegmentContents(*EHFramePHdr);
if (!Content)
reportError(Content.takeError(), ObjF->getFileName());
reportError(Content.takeError(), ObjF.getFileName());
DataExtractor DE(*Content,
ELFT::TargetEndianness == support::endianness::little,
@ -126,25 +125,25 @@ void PrinterContext<ELFT>::printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const {
if (Version != 1)
reportError(
object::createError("only version 1 of .eh_frame_hdr is supported"),
ObjF->getFileName());
ObjF.getFileName());
uint64_t EHFramePtrEnc = DE.getU8(&Offset);
W.startLine() << format("eh_frame_ptr_enc: 0x%" PRIx64 "\n", EHFramePtrEnc);
if (EHFramePtrEnc != (dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4))
reportError(object::createError("unexpected encoding eh_frame_ptr_enc"),
ObjF->getFileName());
ObjF.getFileName());
uint64_t FDECountEnc = DE.getU8(&Offset);
W.startLine() << format("fde_count_enc: 0x%" PRIx64 "\n", FDECountEnc);
if (FDECountEnc != dwarf::DW_EH_PE_udata4)
reportError(object::createError("unexpected encoding fde_count_enc"),
ObjF->getFileName());
ObjF.getFileName());
uint64_t TableEnc = DE.getU8(&Offset);
W.startLine() << format("table_enc: 0x%" PRIx64 "\n", TableEnc);
if (TableEnc != (dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4))
reportError(object::createError("unexpected encoding table_enc"),
ObjF->getFileName());
ObjF.getFileName());
auto EHFramePtr = DE.getSigned(&Offset, 4) + EHFrameHdrAddress + 4;
W.startLine() << format("eh_frame_ptr: 0x%" PRIx64 "\n", EHFramePtr);
@ -164,7 +163,7 @@ void PrinterContext<ELFT>::printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const {
if (InitialPC < PrevPC)
reportError(object::createError("initial_location is out of order"),
ObjF->getFileName());
ObjF.getFileName());
PrevPC = InitialPC;
++NumEntries;
@ -181,20 +180,20 @@ void PrinterContext<ELFT>::printEHFrame(const Elf_Shdr *EHFrameShdr) const {
W.indent();
Expected<ArrayRef<uint8_t>> DataOrErr =
ObjF->getELFFile()->getSectionContents(*EHFrameShdr);
ObjF.getELFFile()->getSectionContents(*EHFrameShdr);
if (!DataOrErr)
reportError(DataOrErr.takeError(), ObjF->getFileName());
reportError(DataOrErr.takeError(), ObjF.getFileName());
// Construct DWARFDataExtractor to handle relocations ("PC Begin" fields).
std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(*ObjF, nullptr);
std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(ObjF, nullptr);
DWARFDataExtractor DE(DICtx->getDWARFObj(),
DICtx->getDWARFObj().getEHFrameSection(),
ELFT::TargetEndianness == support::endianness::little,
ELFT::Is64Bits ? 8 : 4);
DWARFDebugFrame EHFrame(Triple::ArchType(ObjF->getArch()), /*IsEH=*/true,
DWARFDebugFrame EHFrame(Triple::ArchType(ObjF.getArch()), /*IsEH=*/true,
/*EHFrameAddress=*/Address);
if (Error E = EHFrame.parse(DE))
reportError(std::move(E), ObjF->getFileName());
reportError(std::move(E), ObjF.getFileName());
for (const dwarf::FrameEntry &Entry : EHFrame) {
if (const dwarf::CIE *CIE = dyn_cast<dwarf::CIE>(&Entry)) {

File diff suppressed because it is too large Load Diff

View File

@ -37,7 +37,7 @@ static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
}
static std::vector<object::SectionRef>
getSectionRefsByNameOrIndex(const object::ObjectFile *Obj,
getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections) {
std::vector<object::SectionRef> Ret;
std::map<std::string, bool> SecNames;
@ -50,9 +50,9 @@ getSectionRefsByNameOrIndex(const object::ObjectFile *Obj,
SecNames.emplace(std::string(Section), false);
}
SecIndex = Obj->isELF() ? 0 : 1;
for (object::SectionRef SecRef : Obj->sections()) {
StringRef SecName = unwrapOrError(Obj->getFileName(), SecRef.getName());
SecIndex = Obj.isELF() ? 0 : 1;
for (object::SectionRef SecRef : Obj.sections()) {
StringRef SecName = unwrapOrError(Obj.getFileName(), SecRef.getName());
auto NameIt = SecNames.find(std::string(SecName));
if (NameIt != SecNames.end())
NameIt->second = true;
@ -68,24 +68,23 @@ getSectionRefsByNameOrIndex(const object::ObjectFile *Obj,
if (!S.second)
reportWarning(
createError(formatv("could not find section '{0}'", S.first).str()),
Obj->getFileName());
Obj.getFileName());
for (std::pair<unsigned, bool> S : SecIndices)
if (!S.second)
reportWarning(
createError(formatv("could not find section {0}", S.first).str()),
Obj->getFileName());
Obj.getFileName());
return Ret;
}
void ObjDumper::printSectionsAsString(const object::ObjectFile *Obj,
void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections) {
bool First = true;
for (object::SectionRef Section :
getSectionRefsByNameOrIndex(Obj, Sections)) {
StringRef SectionName =
unwrapOrError(Obj->getFileName(), Section.getName());
StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
if (!First)
W.startLine() << '\n';
@ -93,7 +92,7 @@ void ObjDumper::printSectionsAsString(const object::ObjectFile *Obj,
W.startLine() << "String dump of section '" << SectionName << "':\n";
StringRef SectionContent =
unwrapOrError(Obj->getFileName(), Section.getContents());
unwrapOrError(Obj.getFileName(), Section.getContents());
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *CurrentWord = SecContent;
@ -114,13 +113,12 @@ void ObjDumper::printSectionsAsString(const object::ObjectFile *Obj,
}
}
void ObjDumper::printSectionsAsHex(const object::ObjectFile *Obj,
void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections) {
bool First = true;
for (object::SectionRef Section :
getSectionRefsByNameOrIndex(Obj, Sections)) {
StringRef SectionName =
unwrapOrError(Obj->getFileName(), Section.getName());
StringRef SectionName = unwrapOrError(Obj.getFileName(), Section.getName());
if (!First)
W.startLine() << '\n';
@ -128,7 +126,7 @@ void ObjDumper::printSectionsAsHex(const object::ObjectFile *Obj,
W.startLine() << "Hex dump of section '" << SectionName << "':\n";
StringRef SectionContent =
unwrapOrError(Obj->getFileName(), Section.getContents());
unwrapOrError(Obj.getFileName(), Section.getContents());
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *SecEnd = SecContent + SectionContent.size();

View File

@ -61,7 +61,7 @@ public:
virtual void printNeededLibraries() { }
virtual void printSectionAsHex(StringRef SectionName) {}
virtual void printHashTable() { }
virtual void printGnuHashTable(const object::ObjectFile *Obj) {}
virtual void printGnuHashTable() {}
virtual void printHashSymbols() {}
virtual void printLoadName() {}
virtual void printVersionInfo() {}
@ -100,9 +100,9 @@ public:
virtual void printStackMap() const = 0;
void printSectionsAsString(const object::ObjectFile *Obj,
void printSectionsAsString(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections);
void printSectionsAsHex(const object::ObjectFile *Obj,
void printSectionsAsHex(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections);
protected:

View File

@ -495,13 +495,13 @@ static void dumpObject(const ObjectFile &Obj, ScopedPrinter &Writer,
if (opts::Symbols || opts::DynamicSymbols)
Dumper->printSymbols(opts::Symbols, opts::DynamicSymbols);
if (!opts::StringDump.empty())
Dumper->printSectionsAsString(&Obj, opts::StringDump);
Dumper->printSectionsAsString(Obj, opts::StringDump);
if (!opts::HexDump.empty())
Dumper->printSectionsAsHex(&Obj, opts::HexDump);
Dumper->printSectionsAsHex(Obj, opts::HexDump);
if (opts::HashTable)
Dumper->printHashTable();
if (opts::GnuHashTable)
Dumper->printGnuHashTable(&Obj);
Dumper->printGnuHashTable();
if (opts::VersionInfo)
Dumper->printVersionInfo();
if (Obj.isELF()) {