diff --git a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h --- a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h +++ b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h @@ -327,7 +327,7 @@ typedef typename ET::Word Elf_Word; ScopedPrinter &SW; - const object::ELFFile *ELF; + const object::ELFFile &ELF; StringRef FileName; const Elf_Shdr *Symtab; ArrayRef ShndxTable; @@ -351,7 +351,7 @@ void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const; public: - PrinterContext(ScopedPrinter &SW, const object::ELFFile *ELF, + PrinterContext(ScopedPrinter &SW, const object::ELFFile &ELF, StringRef FileName, const Elf_Shdr *Symtab) : SW(SW), ELF(ELF), FileName(FileName), Symtab(Symtab) {} @@ -367,12 +367,12 @@ 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 @@ /// 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(IndexTableOffset)) continue; @@ -417,9 +417,9 @@ 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 @@ void PrinterContext::PrintExceptionTable(const Elf_Shdr *IT, const Elf_Shdr *EHT, uint64_t TableEntryOffset) const { - Expected> Contents = ELF->getSectionContents(*EHT); + // TODO: handle failure. + Expected> Contents = ELF.getSectionContents(*EHT); if (!Contents) return; @@ -499,7 +500,8 @@ template void PrinterContext::PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *IT) const { - Expected> Contents = ELF->getSectionContents(*IT); + // TODO: handle failure. + Expected> Contents = ELF.getSectionContents(*IT); if (!Contents) return; @@ -553,7 +555,8 @@ FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4); if (EHT) - if (auto Name = ELF->getSectionName(*EHT)) + // TODO: handle failure. + if (Expected Name = ELF.getSectionName(*EHT)) SW.printString("ExceptionHandlingTable", *Name); uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr); @@ -570,12 +573,13 @@ 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 SectionName = ELF.getSectionName(Sec)) SW.printString("SectionName", *SectionName); SW.printHex("SectionOffset", Sec.sh_offset); diff --git a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h --- a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h +++ b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h @@ -33,13 +33,13 @@ using Elf_Phdr = typename ELFT::Phdr; ScopedPrinter &W; - const object::ELFObjectFile *ObjF; + const object::ELFObjectFile &ObjF; void printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const; void printEHFrame(const Elf_Shdr *EHFrameShdr) const; public: - PrinterContext(ScopedPrinter &W, const object::ELFObjectFile *ObjF) + PrinterContext(ScopedPrinter &W, const object::ELFObjectFile &ObjF) : W(W), ObjF(ObjF) {} void printUnwindInformation() const; @@ -47,11 +47,11 @@ template static const typename ELFT::Shdr * -findSectionByAddress(const object::ELFObjectFile *ObjF, uint64_t Addr) { +findSectionByAddress(const object::ELFObjectFile &ObjF, uint64_t Addr) { Expected 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 @@ template void PrinterContext::printUnwindInformation() const { - const object::ELFFile *Obj = ObjF->getELFFile(); + const object::ELFFile &Obj = *ObjF.getELFFile(); - Expected PhdrsOrErr = Obj->program_headers(); + Expected 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 @@ 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 SectionsOrErr = - ObjF->getELFFile()->sections(); + Expected SectionsOrErr = Obj.sections(); if (!SectionsOrErr) - reportError(SectionsOrErr.takeError(), ObjF->getFileName()); + reportError(SectionsOrErr.takeError(), ObjF.getFileName()); for (const Elf_Shdr &Shdr : *SectionsOrErr) { - Expected NameOrErr = Obj->getSectionName(Shdr); + Expected 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 @@ 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 *Obj = ObjF->getELFFile(); + const object::ELFFile &Obj = *ObjF.getELFFile(); if (const Elf_Shdr *EHFrameHdr = findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) { - Expected NameOrErr = Obj->getSectionName(*EHFrameHdr); + Expected NameOrErr = Obj.getSectionName(*EHFrameHdr); if (!NameOrErr) - reportError(NameOrErr.takeError(), ObjF->getFileName()); + reportError(NameOrErr.takeError(), ObjF.getFileName()); W.printString("Corresponding Section", *NameOrErr); } - Expected> Content = Obj->getSegmentContents(*EHFramePHdr); + Expected> 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 @@ 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 @@ if (InitialPC < PrevPC) reportError(object::createError("initial_location is out of order"), - ObjF->getFileName()); + ObjF.getFileName()); PrevPC = InitialPC; ++NumEntries; @@ -181,20 +180,20 @@ W.indent(); Expected> 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 DICtx = DWARFContext::create(*ObjF, nullptr); + std::unique_ptr 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(&Entry)) { diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -230,7 +230,7 @@ template class ELFDumper : public ObjDumper { public: - ELFDumper(const object::ELFObjectFile *ObjF, ScopedPrinter &Writer); + ELFDumper(const object::ELFObjectFile &ObjF, ScopedPrinter &Writer); void printFileHeaders() override; void printSectionHeaders() override; @@ -246,7 +246,7 @@ void printProgramHeaders(bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) override; void printHashTable() override; - void printGnuHashTable(const object::ObjectFile *Obj) override; + void printGnuHashTable() override; void printLoadName() override; void printVersionInfo() override; void printGroupSections() override; @@ -265,7 +265,7 @@ void printELFLinkerOptions() override; void printStackSizes() override; - const object::ELFObjectFile *getElfObject() const { return ObjF; }; + const object::ELFObjectFile &getElfObject() const { return ObjF; }; private: std::unique_ptr> ELFDumperStyle; @@ -274,30 +274,29 @@ Expected createDRI(uint64_t Offset, uint64_t Size, uint64_t EntSize) { - const ELFFile *Obj = ObjF->getELFFile(); - if (Offset + Size < Offset || Offset + Size > Obj->getBufSize()) + if (Offset + Size < Offset || Offset + Size > Obj.getBufSize()) return createError("offset (0x" + Twine::utohexstr(Offset) + ") + size (0x" + Twine::utohexstr(Size) + ") is greater than the file size (0x" + - Twine::utohexstr(Obj->getBufSize()) + ")"); - return DynRegionInfo(Obj->base() + Offset, Size, EntSize, - ObjF->getFileName()); + Twine::utohexstr(Obj.getBufSize()) + ")"); + return DynRegionInfo(Obj.base() + Offset, Size, EntSize, + ObjF.getFileName()); } void printAttributes(); void printMipsReginfo(); void printMipsOptions(); - std::pair - findDynamic(const ELFFile *Obj); - void loadDynamicTable(const ELFFile *Obj); - void parseDynamicTable(const ELFFile *Obj); + std::pair findDynamic(); + void loadDynamicTable(); + void parseDynamicTable(); - Expected getSymbolVersion(const Elf_Sym *symb, + Expected getSymbolVersion(const Elf_Sym &Sym, bool &IsDefault) const; Error LoadVersionMap() const; - const object::ELFObjectFile *ObjF; + const object::ELFObjectFile &ObjF; + const ELFFile &Obj; DynRegionInfo DynRelRegion; DynRegionInfo DynRelaRegion; DynRegionInfo DynRelrRegion; @@ -355,12 +354,12 @@ Elf_Rel_Range dyn_rels() const; Elf_Rela_Range dyn_relas() const; Elf_Relr_Range dyn_relrs() const; - std::string getFullSymbolName(const Elf_Sym *Symbol, unsigned SymIndex, + std::string getFullSymbolName(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic) const; - Expected getSymbolSectionIndex(const Elf_Sym *Symbol, + Expected getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex) const; - Expected getSymbolSectionName(const Elf_Sym *Symbol, + Expected getSymbolSectionName(const Elf_Sym &Symbol, unsigned SectionIndex) const; std::string getStaticSymbolName(uint32_t Index) const; StringRef getDynamicString(uint64_t Value) const; @@ -385,13 +384,13 @@ const Elf_Hash *getHashTable() const { return HashTable; } const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; } - Expected> getVersionTable(const Elf_Shdr *Sec, + Expected> getVersionTable(const Elf_Shdr &Sec, ArrayRef *SymTab, StringRef *StrTab) const; Expected> - getVersionDefinitions(const Elf_Shdr *Sec) const; + getVersionDefinitions(const Elf_Shdr &Sec) const; Expected> - getVersionDependencies(const Elf_Shdr *Sec) const; + getVersionDependencies(const Elf_Shdr &Sec) const; Expected> getRelocationTarget(const Relocation &R, const Elf_Shdr *SymTab) const; @@ -412,21 +411,21 @@ template std::string ELFDumper::describe(const Elf_Shdr &Sec) const { - return ::describe(*ObjF->getELFFile(), Sec); + return ::describe(Obj, Sec); } template static Expected getLinkAsStrtab(const ELFFile &Obj, - const typename ELFT::Shdr *Sec) { + const typename ELFT::Shdr &Sec) { Expected StrTabSecOrErr = - Obj.getSection(Sec->sh_link); + Obj.getSection(Sec.sh_link); if (!StrTabSecOrErr) - return createError("invalid section linked to " + describe(Obj, *Sec) + + return createError("invalid section linked to " + describe(Obj, Sec) + ": " + toString(StrTabSecOrErr.takeError())); Expected StrTabOrErr = Obj.getStringTable(**StrTabSecOrErr); if (!StrTabOrErr) - return createError("invalid string table linked to " + describe(Obj, *Sec) + + return createError("invalid string table linked to " + describe(Obj, Sec) + ": " + toString(StrTabOrErr.takeError())); return *StrTabOrErr; } @@ -434,55 +433,52 @@ // Returns the linked symbol table and associated string table for a given section. template static Expected> -getLinkAsSymtab(const ELFFile &Obj, const typename ELFT::Shdr *Sec, +getLinkAsSymtab(const ELFFile &Obj, const typename ELFT::Shdr &Sec, unsigned ExpectedType) { Expected SymtabOrErr = - Obj.getSection(Sec->sh_link); + Obj.getSection(Sec.sh_link); if (!SymtabOrErr) - return createError("invalid section linked to " + describe(Obj, *Sec) + + return createError("invalid section linked to " + describe(Obj, Sec) + ": " + toString(SymtabOrErr.takeError())); if ((*SymtabOrErr)->sh_type != ExpectedType) return createError( - "invalid section linked to " + describe(Obj, *Sec) + ": expected " + + "invalid section linked to " + describe(Obj, Sec) + ": expected " + object::getELFSectionTypeName(Obj.getHeader().e_machine, ExpectedType) + ", but got " + object::getELFSectionTypeName(Obj.getHeader().e_machine, (*SymtabOrErr)->sh_type)); - Expected StrTabOrErr = getLinkAsStrtab(Obj, *SymtabOrErr); + Expected StrTabOrErr = getLinkAsStrtab(Obj, **SymtabOrErr); if (!StrTabOrErr) return createError( "can't get a string table for the symbol table linked to " + - describe(Obj, *Sec) + ": " + toString(StrTabOrErr.takeError())); + describe(Obj, Sec) + ": " + toString(StrTabOrErr.takeError())); Expected SymsOrErr = Obj.symbols(*SymtabOrErr); if (!SymsOrErr) - return createError("unable to read symbols from the " + - describe(Obj, *Sec) + ": " + - toString(SymsOrErr.takeError())); + return createError("unable to read symbols from the " + describe(Obj, Sec) + + ": " + toString(SymsOrErr.takeError())); return std::make_pair(*SymsOrErr, *StrTabOrErr); } template Expected> -ELFDumper::getVersionTable(const Elf_Shdr *Sec, ArrayRef *SymTab, +ELFDumper::getVersionTable(const Elf_Shdr &Sec, ArrayRef *SymTab, StringRef *StrTab) const { assert((!SymTab && !StrTab) || (SymTab && StrTab)); - const ELFFile *Obj = ObjF->getELFFile(); - - if (uintptr_t(Obj->base() + Sec->sh_offset) % sizeof(uint16_t) != 0) - return createError("the " + describe(*Sec) + " is misaligned"); + if (uintptr_t(Obj.base() + Sec.sh_offset) % sizeof(uint16_t) != 0) + return createError("the " + describe(Sec) + " is misaligned"); Expected> VersionsOrErr = - Obj->template getSectionContentsAsArray(*Sec); + Obj.template getSectionContentsAsArray(Sec); if (!VersionsOrErr) - return createError("cannot read content of " + describe(*Sec) + ": " + + return createError("cannot read content of " + describe(Sec) + ": " + toString(VersionsOrErr.takeError())); Expected, StringRef>> SymTabOrErr = - getLinkAsSymtab(*Obj, Sec, SHT_DYNSYM); + getLinkAsSymtab(Obj, Sec, SHT_DYNSYM); if (!SymTabOrErr) { reportUniqueWarning(SymTabOrErr.takeError()); return *VersionsOrErr; @@ -490,11 +486,11 @@ if (SymTabOrErr->first.size() != VersionsOrErr->size()) reportUniqueWarning( - createError(describe(*Sec) + ": the number of entries (" + + createError(describe(Sec) + ": the number of entries (" + Twine(VersionsOrErr->size()) + ") does not match the number of symbols (" + Twine(SymTabOrErr->first.size()) + - ") in the symbol table with index " + Twine(Sec->sh_link))); + ") in the symbol table with index " + Twine(Sec.sh_link))); if (SymTab) std::tie(*SymTab, *StrTab) = *SymTabOrErr; @@ -503,16 +499,14 @@ template Expected> -ELFDumper::getVersionDefinitions(const Elf_Shdr *Sec) const { - const ELFFile *Obj = ObjF->getELFFile(); - - Expected StrTabOrErr = getLinkAsStrtab(*Obj, Sec); +ELFDumper::getVersionDefinitions(const Elf_Shdr &Sec) const { + Expected StrTabOrErr = getLinkAsStrtab(Obj, Sec); if (!StrTabOrErr) return StrTabOrErr.takeError(); - Expected> ContentsOrErr = Obj->getSectionContents(*Sec); + Expected> ContentsOrErr = Obj.getSectionContents(Sec); if (!ContentsOrErr) - return createError("cannot read content of " + describe(*Sec) + ": " + + return createError("cannot read content of " + describe(Sec) + ": " + toString(ContentsOrErr.takeError())); const uint8_t *Start = ContentsOrErr->data(); @@ -521,7 +515,7 @@ auto ExtractNextAux = [&](const uint8_t *&VerdauxBuf, unsigned VerDefNdx) -> Expected { if (VerdauxBuf + sizeof(Elf_Verdaux) > End) - return createError("invalid " + describe(*Sec) + ": version definition " + + return createError("invalid " + describe(Sec) + ": version definition " + Twine(VerDefNdx) + " refers to an auxiliary entry that goes past the end " "of the section"); @@ -540,20 +534,20 @@ std::vector Ret; const uint8_t *VerdefBuf = Start; - for (unsigned I = 1; I <= /*VerDefsNum=*/Sec->sh_info; ++I) { + for (unsigned I = 1; I <= /*VerDefsNum=*/Sec.sh_info; ++I) { if (VerdefBuf + sizeof(Elf_Verdef) > End) - return createError("invalid " + describe(*Sec) + ": version definition " + + return createError("invalid " + describe(Sec) + ": version definition " + Twine(I) + " goes past the end of the section"); if (uintptr_t(VerdefBuf) % sizeof(uint32_t) != 0) return createError( - "invalid " + describe(*Sec) + + "invalid " + describe(Sec) + ": found a misaligned version definition entry at offset 0x" + Twine::utohexstr(VerdefBuf - Start)); unsigned Version = *reinterpret_cast(VerdefBuf); if (Version != 1) - return createError("unable to dump " + describe(*Sec) + ": version " + + return createError("unable to dump " + describe(Sec) + ": version " + Twine(Version) + " is not yet supported"); const Elf_Verdef *D = reinterpret_cast(VerdefBuf); @@ -568,7 +562,7 @@ const uint8_t *VerdauxBuf = VerdefBuf + D->vd_aux; for (unsigned J = 0; J < D->vd_cnt; ++J) { if (uintptr_t(VerdauxBuf) % sizeof(uint32_t) != 0) - return createError("invalid " + describe(*Sec) + + return createError("invalid " + describe(Sec) + ": found a misaligned auxiliary entry at offset 0x" + Twine::utohexstr(VerdauxBuf - Start)); @@ -590,18 +584,17 @@ template Expected> -ELFDumper::getVersionDependencies(const Elf_Shdr *Sec) const { - const ELFFile *Obj = ObjF->getELFFile(); +ELFDumper::getVersionDependencies(const Elf_Shdr &Sec) const { StringRef StrTab; - Expected StrTabOrErr = getLinkAsStrtab(*Obj, Sec); + Expected StrTabOrErr = getLinkAsStrtab(Obj, Sec); if (!StrTabOrErr) reportUniqueWarning(StrTabOrErr.takeError()); else StrTab = *StrTabOrErr; - Expected> ContentsOrErr = Obj->getSectionContents(*Sec); + Expected> ContentsOrErr = Obj.getSectionContents(Sec); if (!ContentsOrErr) - return createError("cannot read content of " + describe(*Sec) + ": " + + return createError("cannot read content of " + describe(Sec) + ": " + toString(ContentsOrErr.takeError())); const uint8_t *Start = ContentsOrErr->data(); @@ -609,20 +602,20 @@ const uint8_t *VerneedBuf = Start; std::vector Ret; - for (unsigned I = 1; I <= /*VerneedNum=*/Sec->sh_info; ++I) { + for (unsigned I = 1; I <= /*VerneedNum=*/Sec.sh_info; ++I) { if (VerneedBuf + sizeof(Elf_Verdef) > End) - return createError("invalid " + describe(*Sec) + ": version dependency " + + return createError("invalid " + describe(Sec) + ": version dependency " + Twine(I) + " goes past the end of the section"); if (uintptr_t(VerneedBuf) % sizeof(uint32_t) != 0) return createError( - "invalid " + describe(*Sec) + + "invalid " + describe(Sec) + ": found a misaligned version dependency entry at offset 0x" + Twine::utohexstr(VerneedBuf - Start)); unsigned Version = *reinterpret_cast(VerneedBuf); if (Version != 1) - return createError("unable to dump " + describe(*Sec) + ": version " + + return createError("unable to dump " + describe(Sec) + ": version " + Twine(Version) + " is not yet supported"); const Elf_Verneed *Verneed = @@ -641,13 +634,13 @@ const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux; for (unsigned J = 0; J < Verneed->vn_cnt; ++J) { if (uintptr_t(VernauxBuf) % sizeof(uint32_t) != 0) - return createError("invalid " + describe(*Sec) + + return createError("invalid " + describe(Sec) + ": found a misaligned auxiliary entry at offset 0x" + Twine::utohexstr(VernauxBuf - Start)); if (VernauxBuf + sizeof(Elf_Vernaux) > End) return createError( - "invalid " + describe(*Sec) + ": version dependency " + Twine(I) + + "invalid " + describe(Sec) + ": version dependency " + Twine(I) + " refers to an auxiliary entry that goes past the end " "of the section"); @@ -676,7 +669,6 @@ Optional StrTable; size_t Entries = 0; Elf_Sym_Range Syms(nullptr, nullptr); - const ELFFile *Obj = ObjF->getELFFile(); const Elf_Shdr *SymtabSec = IsDynamic ? DotDynsymSec : DotSymtabSec; if (IsDynamic) { @@ -685,14 +677,14 @@ Entries = Syms.size(); } else if (DotSymtabSec) { if (Expected StrTableOrErr = - Obj->getStringTableForSymtab(*DotSymtabSec)) + Obj.getStringTableForSymtab(*DotSymtabSec)) StrTable = *StrTableOrErr; else reportUniqueWarning(createError( "unable to get the string table for the SHT_SYMTAB section: " + toString(StrTableOrErr.takeError()))); - if (Expected SymsOrErr = Obj->symbols(DotSymtabSec)) + if (Expected SymsOrErr = Obj.symbols(DotSymtabSec)) Syms = *SymsOrErr; else reportUniqueWarning( @@ -710,8 +702,8 @@ }) != Syms.end(); ELFDumperStyle->printSymtabMessage(SymtabSec, Entries, NonVisibilityBitsUsed); - for (const auto &Sym : Syms) - ELFDumperStyle->printSymbol(&Sym, &Sym - Syms.begin(), StrTable, IsDynamic, + for (const Elf_Sym &Sym : Syms) + ELFDumperStyle->printSymbol(Sym, &Sym - Syms.begin(), StrTable, IsDynamic, NonVisibilityBitsUsed); } @@ -721,9 +713,9 @@ public: TYPEDEF_ELF_TYPES(ELFT) - DumpStyle(ELFDumper *Dumper) - : Obj(*Dumper->getElfObject()->getELFFile()), - ElfObj(*Dumper->getElfObject()), Dumper(Dumper) { + DumpStyle(const ELFDumper &Dumper) + : Obj(*Dumper.getElfObject().getELFFile()), ElfObj(Dumper.getElfObject()), + Dumper(Dumper) { FileName = ElfObj.getFileName(); } @@ -740,7 +732,7 @@ virtual void printDynamicRelocations() = 0; virtual void printSymtabMessage(const Elf_Shdr *Symtab, size_t Offset, bool NonVisibilityBitsUsed) {} - virtual void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex, + virtual void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic, bool NonVisibilityBitsUsed) = 0; virtual void printProgramHeaders(bool PrintProgramHeaders, @@ -766,8 +758,8 @@ virtual void printStackSizeEntry(uint64_t Size, StringRef FuncName) = 0; virtual void printMipsGOT(const MipsGOTParser &Parser) = 0; virtual void printMipsPLT(const MipsGOTParser &Parser) = 0; - virtual void printMipsABIFlags(const ELFObjectFile *Obj) = 0; - const ELFDumper *dumper() const { return Dumper; } + virtual void printMipsABIFlags() = 0; + const ELFDumper &dumper() const { return Dumper; } protected: void printDependentLibsHelper( @@ -792,7 +784,7 @@ const ELFObjectFile &ElfObj; private: - const ELFDumper *Dumper; + const ELFDumper &Dumper; }; template class GNUStyle : public DumpStyle { @@ -801,10 +793,10 @@ public: TYPEDEF_ELF_TYPES(ELFT) - GNUStyle(ScopedPrinter &W, ELFDumper *Dumper) + GNUStyle(ScopedPrinter &W, const ELFDumper &Dumper) : DumpStyle(Dumper), - OS(static_cast(W.getOStream())) { - assert (&W.getOStream() == &llvm::fouts()); + OS(static_cast(W.getOStream())) { + assert(&W.getOStream() == &llvm::fouts()); } void printFileHeaders() override; @@ -832,7 +824,7 @@ void printStackSizeEntry(uint64_t Size, StringRef FuncName) override; void printMipsGOT(const MipsGOTParser &Parser) override; void printMipsPLT(const MipsGOTParser &Parser) override; - void printMipsABIFlags(const ELFObjectFile *Obj) override; + void printMipsABIFlags() override; private: void printHashHistogram(const Elf_Hash &HashTable); @@ -851,7 +843,7 @@ template std::string printEnum(T Value, ArrayRef> EnumValues) { - for (const auto &EnumItem : EnumValues) + for (const EnumEntry &EnumItem : EnumValues) if (EnumItem.Value == Value) return std::string(EnumItem.AltName); return to_hexString(Value, false); @@ -862,7 +854,7 @@ TEnum EnumMask1 = {}, TEnum EnumMask2 = {}, TEnum EnumMask3 = {}) { std::string Str; - for (const auto &Flag : EnumValues) { + for (const EnumEntry &Flag : EnumValues) { if (Flag.Value == 0) continue; @@ -899,14 +891,14 @@ void printRelRelaReloc(const Relocation &R, const RelSymbol &RelSym); - void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex, + void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic, bool NonVisibilityBitsUsed) override; void printDynamicRelocHeader(unsigned Type, StringRef Name, const DynRegionInfo &Reg) override; void printDynamicReloc(const Relocation &R) override; - std::string getSymbolSectionNdx(const Elf_Sym *Symbol, unsigned SymIndex); + std::string getSymbolSectionNdx(const Elf_Sym &Symbol, unsigned SymIndex); void printProgramHeaders(); void printSectionMapping(); void printGNUVersionSectionProlog(const typename ELFT::Shdr *Sec, @@ -923,14 +915,14 @@ template void DumpStyle::reportUniqueWarning(Error Err) const { - this->dumper()->reportUniqueWarning(std::move(Err)); + this->dumper().reportUniqueWarning(std::move(Err)); } template class LLVMStyle : public DumpStyle { public: TYPEDEF_ELF_TYPES(ELFT) - LLVMStyle(ScopedPrinter &W, ELFDumper *Dumper) + LLVMStyle(ScopedPrinter &W, const ELFDumper &Dumper) : DumpStyle(Dumper), W(W) {} void printFileHeaders() override; @@ -955,7 +947,7 @@ void printStackSizeEntry(uint64_t Size, StringRef FuncName) override; void printMipsGOT(const MipsGOTParser &Parser) override; void printMipsPLT(const MipsGOTParser &Parser) override; - void printMipsABIFlags(const ELFObjectFile *Obj) override; + void printMipsABIFlags() override; private: void printReloc(const Relocation &R, unsigned RelIndex, @@ -966,8 +958,8 @@ void printRelRelaReloc(const Relocation &R, StringRef SymbolName); void printSymbols(); void printDynamicSymbols(); - void printSymbolSection(const Elf_Sym *Symbol, unsigned SymIndex); - void printSymbol(const Elf_Sym *Symbol, unsigned SymIndex, + void printSymbolSection(const Elf_Sym &Symbol, unsigned SymIndex); + void printSymbol(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic, bool /*NonVisibilityBitsUsed*/) override; void printProgramHeaders(); @@ -983,7 +975,7 @@ template static std::unique_ptr createELFDumper(const ELFObjectFile &Obj, ScopedPrinter &Writer) { - return std::make_unique>(&Obj, Writer); + return std::make_unique>(Obj, Writer); } std::unique_ptr createELFDumper(const object::ELFObjectFileBase &Obj, @@ -1028,7 +1020,7 @@ if (SymbolVersionDefSection) { Expected> Defs = - this->getVersionDefinitions(SymbolVersionDefSection); + this->getVersionDefinitions(*SymbolVersionDefSection); if (!Defs) return Defs.takeError(); for (const VerDef &Def : *Defs) @@ -1037,7 +1029,7 @@ if (SymbolVersionNeedSection) { Expected> Deps = - this->getVersionDependencies(SymbolVersionNeedSection); + this->getVersionDependencies(*SymbolVersionNeedSection); if (!Deps) return Deps.takeError(); for (const VerNeed &Dep : *Deps) @@ -1049,7 +1041,7 @@ } template -Expected ELFDumper::getSymbolVersion(const Elf_Sym *Sym, +Expected ELFDumper::getSymbolVersion(const Elf_Sym &Sym, bool &IsDefault) const { // This is a dynamic symbol. Look in the GNU symbol version table. if (!SymbolVersionSection) { @@ -1060,14 +1052,13 @@ assert(DynSymRegion && "DynSymRegion has not been initialised"); // Determine the position in the symbol table of this entry. - size_t EntryIndex = (reinterpret_cast(Sym) - + size_t EntryIndex = (reinterpret_cast(&Sym) - reinterpret_cast(DynSymRegion->Addr)) / sizeof(Elf_Sym); // Get the corresponding version index entry. if (Expected EntryOrErr = - ObjF->getELFFile()->template getEntry( - *SymbolVersionSection, EntryIndex)) + Obj.template getEntry(*SymbolVersionSection, EntryIndex)) return this->getSymbolVersionByIndex((*EntryOrErr)->vs_index, IsDefault); else return EntryOrErr.takeError(); @@ -1080,7 +1071,6 @@ if (R.Symbol == 0) return RelSymbol(nullptr, ""); - const ELFFile &Obj = *ObjF->getELFFile(); Expected SymOrErr = Obj.template getEntry(*SymTab, R.Symbol); if (!SymOrErr) @@ -1113,7 +1103,7 @@ const Elf_Sym *FirstSym = cantFail(Obj.template getEntry(*SymTab, 0)); std::string SymbolName = getFullSymbolName( - Sym, FirstSym - Sym, *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM); + *Sym, FirstSym - Sym, *StrTableOrErr, SymTab->sh_type == SHT_DYNSYM); return RelSymbol(Sym, SymbolName); } @@ -1130,13 +1120,12 @@ return ""; }; - const ELFFile *Obj = ObjF->getELFFile(); Expected SymOrErr = - Obj->getSymbol(DotSymtabSec, Index); + Obj.getSymbol(DotSymtabSec, Index); if (!SymOrErr) return Warn(SymOrErr.takeError()); - Expected StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec); + Expected StrTabOrErr = Obj.getStringTableForSymtab(*DotSymtabSec); if (!StrTabOrErr) return Warn(StrTabOrErr.takeError()); @@ -1174,7 +1163,7 @@ } template -std::string ELFDumper::getFullSymbolName(const Elf_Sym *Symbol, +std::string ELFDumper::getFullSymbolName(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic) const { @@ -1182,14 +1171,14 @@ return ""; std::string SymbolName; - if (Expected NameOrErr = Symbol->getName(*StrTable)) { + if (Expected NameOrErr = Symbol.getName(*StrTable)) { SymbolName = maybeDemangle(*NameOrErr); } else { reportUniqueWarning(NameOrErr.takeError()); return ""; } - if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) { + if (SymbolName.empty() && Symbol.getType() == ELF::STT_SECTION) { Expected SectionIndex = getSymbolSectionIndex(Symbol, SymIndex); if (!SectionIndex) { reportUniqueWarning(SectionIndex.takeError()); @@ -1207,7 +1196,7 @@ return SymbolName; bool IsDefault; - Expected VersionOrErr = getSymbolVersion(&*Symbol, IsDefault); + Expected VersionOrErr = getSymbolVersion(Symbol, IsDefault); if (!VersionOrErr) { reportUniqueWarning(VersionOrErr.takeError()); return SymbolName + "@"; @@ -1222,11 +1211,11 @@ template Expected -ELFDumper::getSymbolSectionIndex(const Elf_Sym *Symbol, +ELFDumper::getSymbolSectionIndex(const Elf_Sym &Symbol, unsigned SymIndex) const { - unsigned Ndx = Symbol->st_shndx; + unsigned Ndx = Symbol.st_shndx; if (Ndx == SHN_XINDEX) - return object::getExtendedSymbolTableIndex(*Symbol, SymIndex, + return object::getExtendedSymbolTableIndex(Symbol, SymIndex, ShndxTable); if (Ndx != SHN_UNDEF && Ndx < SHN_LORESERVE) return Ndx; @@ -1257,20 +1246,19 @@ template Expected -ELFDumper::getSymbolSectionName(const Elf_Sym *Symbol, +ELFDumper::getSymbolSectionName(const Elf_Sym &Symbol, unsigned SectionIndex) const { - const ELFFile *Obj = ObjF->getELFFile(); - Expected SecOrErr = Obj->getSection(SectionIndex); + Expected SecOrErr = Obj.getSection(SectionIndex); if (!SecOrErr) return SecOrErr.takeError(); - return Obj->getSectionName(**SecOrErr); + return Obj.getSectionName(**SecOrErr); } template static const typename ELFO::Elf_Shdr * -findNotEmptySectionByAddress(const ELFO *Obj, StringRef FileName, +findNotEmptySectionByAddress(const ELFO &Obj, StringRef FileName, uint64_t Addr) { - for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj->sections())) + for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj.sections())) if (Shdr.sh_addr == Addr && Shdr.sh_size > 0) return &Shdr; return nullptr; @@ -1850,10 +1838,10 @@ template std::pair -ELFDumper::findDynamic(const ELFFile *Obj) { +ELFDumper::findDynamic() { // Try to locate the PT_DYNAMIC header. const Elf_Phdr *DynamicPhdr = nullptr; - if (Expected> PhdrsOrErr = Obj->program_headers()) { + if (Expected> PhdrsOrErr = Obj.program_headers()) { for (const Elf_Phdr &Phdr : *PhdrsOrErr) { if (Phdr.p_type != ELF::PT_DYNAMIC) continue; @@ -1868,7 +1856,7 @@ // Try to locate the .dynamic section in the sections header table. const Elf_Shdr *DynamicSec = nullptr; - for (const Elf_Shdr &Sec : cantFail(Obj->sections())) { + for (const Elf_Shdr &Sec : cantFail(Obj.sections())) { if (Sec.sh_type != ELF::SHT_DYNAMIC) continue; DynamicSec = &Sec; @@ -1876,7 +1864,7 @@ } if (DynamicPhdr && ((DynamicPhdr->p_offset + DynamicPhdr->p_filesz > - ObjF->getMemoryBufferRef().getBufferSize()) || + ObjF.getMemoryBufferRef().getBufferSize()) || (DynamicPhdr->p_offset + DynamicPhdr->p_filesz < DynamicPhdr->p_offset))) { reportUniqueWarning(createError( @@ -1884,7 +1872,7 @@ Twine::utohexstr(DynamicPhdr->p_offset) + ") + file size (0x" + Twine::utohexstr(DynamicPhdr->p_filesz) + ") exceeds the size of the file (0x" + - Twine::utohexstr(ObjF->getMemoryBufferRef().getBufferSize()) + ")")); + Twine::utohexstr(ObjF.getMemoryBufferRef().getBufferSize()) + ")")); // Don't use the broken dynamic header. DynamicPhdr = nullptr; } @@ -1893,30 +1881,28 @@ if (DynamicSec->sh_addr + DynamicSec->sh_size > DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz || DynamicSec->sh_addr < DynamicPhdr->p_vaddr) - reportWarning(createError(describe(*DynamicSec) + - " is not contained within the " - "PT_DYNAMIC segment"), - ObjF->getFileName()); + reportUniqueWarning(createError(describe(*DynamicSec) + + " is not contained within the " + "PT_DYNAMIC segment")); if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr) - reportWarning(createError(describe(*DynamicSec) + - " is not at the start of " - "PT_DYNAMIC segment"), - ObjF->getFileName()); + reportUniqueWarning(createError(describe(*DynamicSec) + + " is not at the start of " + "PT_DYNAMIC segment")); } return std::make_pair(DynamicPhdr, DynamicSec); } template -void ELFDumper::loadDynamicTable(const ELFFile *Obj) { +void ELFDumper::loadDynamicTable() { const Elf_Phdr *DynamicPhdr; const Elf_Shdr *DynamicSec; - std::tie(DynamicPhdr, DynamicSec) = findDynamic(Obj); + std::tie(DynamicPhdr, DynamicSec) = findDynamic(); if (!DynamicPhdr && !DynamicSec) return; - DynRegionInfo FromPhdr(ObjF->getFileName()); + DynRegionInfo FromPhdr(ObjF.getFileName()); bool IsPhdrTableValid = false; if (DynamicPhdr) { // Use cantFail(), because p_offset/p_filesz fields of a PT_DYNAMIC are @@ -1932,7 +1918,7 @@ // Ignore sh_entsize and use the expected value for entry size explicitly. // This allows us to dump dynamic sections with a broken sh_entsize // field. - DynRegionInfo FromSec(ObjF->getFileName()); + DynRegionInfo FromSec(ObjF.getFileName()); bool IsSecTableValid = false; if (DynamicSec) { Expected RegOrErr = @@ -1954,10 +1940,9 @@ if (!DynamicPhdr || !DynamicSec) { if ((DynamicPhdr && IsPhdrTableValid) || (DynamicSec && IsSecTableValid)) { DynamicTable = DynamicPhdr ? FromPhdr : FromSec; - parseDynamicTable(Obj); + parseDynamicTable(); } else { - reportWarning(createError("no valid dynamic table was found"), - ObjF->getFileName()); + reportUniqueWarning(createError("no valid dynamic table was found")); } return; } @@ -1967,14 +1952,12 @@ // verify that. if (FromPhdr.Addr != FromSec.Addr) - reportWarning(createError("SHT_DYNAMIC section header and PT_DYNAMIC " - "program header disagree about " - "the location of the dynamic table"), - ObjF->getFileName()); + reportUniqueWarning(createError("SHT_DYNAMIC section header and PT_DYNAMIC " + "program header disagree about " + "the location of the dynamic table")); if (!IsPhdrTableValid && !IsSecTableValid) { - reportWarning(createError("no valid dynamic table was found"), - ObjF->getFileName()); + reportUniqueWarning(createError("no valid dynamic table was found")); return; } @@ -1982,43 +1965,39 @@ // in a section header. if (IsPhdrTableValid) { if (!IsSecTableValid) - reportWarning( - createError( - "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used"), - ObjF->getFileName()); + reportUniqueWarning(createError( + "SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used")); DynamicTable = FromPhdr; } else { - reportWarning( - createError( - "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used"), - ObjF->getFileName()); + reportUniqueWarning(createError( + "PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used")); DynamicTable = FromSec; } - parseDynamicTable(Obj); + parseDynamicTable(); } template -ELFDumper::ELFDumper(const object::ELFObjectFile *ObjF, +ELFDumper::ELFDumper(const object::ELFObjectFile &O, ScopedPrinter &Writer) - : ObjDumper(Writer), ObjF(ObjF), DynRelRegion(ObjF->getFileName()), - DynRelaRegion(ObjF->getFileName()), DynRelrRegion(ObjF->getFileName()), - DynPLTRelRegion(ObjF->getFileName()), DynamicTable(ObjF->getFileName()) { + : ObjDumper(Writer), ObjF(O), Obj(*O.getELFFile()), + DynRelRegion(O.getFileName()), DynRelaRegion(O.getFileName()), + DynRelrRegion(O.getFileName()), DynPLTRelRegion(O.getFileName()), + DynamicTable(O.getFileName()) { // Dumper reports all non-critical errors as warnings. // It does not print the same warning more than once. WarningHandler = [this](const Twine &Msg) { if (Warnings.insert(Msg.str()).second) - reportWarning(createError(Msg), this->ObjF->getFileName()); + reportWarning(createError(Msg), ObjF.getFileName()); return Error::success(); }; if (opts::Output == opts::GNU) - ELFDumperStyle.reset(new GNUStyle(Writer, this)); + ELFDumperStyle.reset(new GNUStyle(Writer, *this)); else - ELFDumperStyle.reset(new LLVMStyle(Writer, this)); + ELFDumperStyle.reset(new LLVMStyle(Writer, *this)); - const ELFFile *Obj = ObjF->getELFFile(); - typename ELFT::ShdrRange Sections = cantFail(Obj->sections()); + typename ELFT::ShdrRange Sections = cantFail(Obj.sections()); for (const Elf_Shdr &Sec : Sections) { switch (Sec.sh_type) { case ELF::SHT_SYMTAB: @@ -2036,10 +2015,10 @@ DynSymRegion = *RegOrErr; DynSymRegion->Context = describe(Sec); - if (Expected E = Obj->getStringTableForSymtab(Sec)) + if (Expected E = Obj.getStringTableForSymtab(Sec)) DynamicStringTable = *E; else - reportWarning(E.takeError(), ObjF->getFileName()); + reportWarning(E.takeError(), ObjF.getFileName()); } else { reportUniqueWarning(createError( "unable to read dynamic symbols from " + describe(Sec) + ": " + @@ -2048,7 +2027,7 @@ } break; case ELF::SHT_SYMTAB_SHNDX: - ShndxTable = unwrapOrError(ObjF->getFileName(), Obj->getSHNDXTable(Sec)); + ShndxTable = unwrapOrError(ObjF.getFileName(), Obj.getSHNDXTable(Sec)); break; case ELF::SHT_GNU_versym: if (!SymbolVersionSection) @@ -2073,19 +2052,19 @@ } } - loadDynamicTable(Obj); + loadDynamicTable(); } template -void ELFDumper::parseDynamicTable(const ELFFile *Obj) { +void ELFDumper::parseDynamicTable() { auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * { - auto MappedAddrOrError = ObjF->getELFFile()->toMappedAddr(VAddr); + auto MappedAddrOrError = Obj.toMappedAddr(VAddr); if (!MappedAddrOrError) { Error Err = - createError("Unable to parse DT_" + Obj->getDynamicTagAsString(Tag) + + createError("Unable to parse DT_" + Obj.getDynamicTagAsString(Tag) + ": " + llvm::toString(MappedAddrOrError.takeError())); - reportWarning(std::move(Err), ObjF->getFileName()); + reportWarning(std::move(Err), ObjF.getFileName()); return nullptr; } return MappedAddrOrError.get(); @@ -2116,7 +2095,7 @@ // If we can't map the DT_SYMTAB value to an address (e.g. when there are // no program headers), we ignore its value. if (const uint8_t *VA = toMappedAddr(Dyn.getTag(), Dyn.getPtr())) { - DynSymFromTable.emplace(ObjF->getFileName()); + DynSymFromTable.emplace(ObjF.getFileName()); DynSymFromTable->Addr = VA; DynSymFromTable->EntSize = sizeof(Elf_Sym); DynSymFromTable->EntSizePrintName = ""; @@ -2130,7 +2109,7 @@ Twine::utohexstr(Val) + " is not the size of a symbol (0x" + Twine::utohexstr(sizeof(Elf_Sym)) + ")"), - ObjF->getFileName()); + ObjF.getFileName()); break; } case ELF::DT_RELA: @@ -2184,7 +2163,7 @@ else reportError(createError(Twine("unknown DT_PLTREL value of ") + Twine((uint64_t)Dyn.getVal())), - ObjF->getFileName()); + ObjF.getFileName()); DynPLTRelRegion.EntSizePrintName = ""; break; case ELF::DT_JMPREL: @@ -2198,9 +2177,8 @@ } if (StringTableBegin) { - const uint64_t FileSize = ObjF->getELFFile()->getBufSize(); - const uint64_t Offset = - (const uint8_t *)StringTableBegin - ObjF->getELFFile()->base(); + const uint64_t FileSize = Obj.getBufSize(); + const uint64_t Offset = (const uint8_t *)StringTableBegin - Obj.base(); if (StringTableSize > FileSize - Offset) reportUniqueWarning(createError( "the dynamic string table at 0x" + Twine::utohexstr(Offset) + @@ -2256,11 +2234,10 @@ // Derive the dynamic symbol table size from the DT_HASH hash table, if // present. if (HashTable && DynSymRegion) { - const uint64_t FileSize = ObjF->getELFFile()->getBufSize(); + const uint64_t FileSize = Obj.getBufSize(); const uint64_t DerivedSize = (uint64_t)HashTable->nchain * DynSymRegion->EntSize; - const uint64_t Offset = - (const uint8_t *)DynSymRegion->Addr - ObjF->getELFFile()->base(); + const uint64_t Offset = (const uint8_t *)DynSymRegion->Addr - Obj.base(); if (DerivedSize > FileSize - Offset) reportUniqueWarning(createError( "the size (0x" + Twine::utohexstr(DerivedSize) + @@ -2418,29 +2395,20 @@ template void printFlags(T Value, ArrayRef> Flags, raw_ostream &OS) { - using FlagEntry = EnumEntry; - using FlagVector = SmallVector; - FlagVector SetFlags; - - for (const auto &Flag : Flags) { - if (Flag.Value == 0) - continue; - - if ((Value & Flag.Value) == Flag.Value) + SmallVector, 10> SetFlags; + for (const EnumEntry &Flag : Flags) + if (Flag.Value != 0 && (Value & Flag.Value) == Flag.Value) SetFlags.push_back(Flag); - } - for (const auto &Flag : SetFlags) { + for (const EnumEntry &Flag : SetFlags) OS << Flag.Name << " "; - } } template const typename ELFT::Shdr * ELFDumper::findSectionByName(StringRef Name) const { - const ELFFile *Obj = ObjF->getELFFile(); - for (const Elf_Shdr &Shdr : cantFail(Obj->sections())) { - if (Expected NameOrErr = Obj->getSectionName(Shdr)) { + for (const Elf_Shdr &Shdr : cantFail(Obj.sections())) { + if (Expected NameOrErr = Obj.getSectionName(Shdr)) { if (*NameOrErr == Name) return &Shdr; } else { @@ -2473,7 +2441,7 @@ }; // Handle custom printing of architecture specific tags - switch (ObjF->getELFFile()->getHeader().e_machine) { + switch (Obj.getHeader().e_machine) { case EM_AARCH64: switch (Type) { case DT_AARCH64_BTI_PLT: @@ -2635,9 +2603,9 @@ return ""; }; - const uint64_t FileSize = ObjF->getELFFile()->getBufSize(); + const uint64_t FileSize = Obj.getBufSize(); const uint64_t Offset = - (const uint8_t *)DynamicStringTable.data() - ObjF->getELFFile()->base(); + (const uint8_t *)DynamicStringTable.data() - Obj.base(); if (DynamicStringTable.size() > FileSize - Offset) return WarnAndReturn(" with size 0x" + Twine::utohexstr(DynamicStringTable.size()) + @@ -2669,10 +2637,8 @@ namespace { template <> void ELFDumper::printUnwindInfo() { - const ELFFile *Obj = ObjF->getELFFile(); - const unsigned Machine = Obj->getHeader().e_machine; - if (Machine == EM_ARM) { - ARM::EHABI::PrinterContext Ctx(W, Obj, ObjF->getFileName(), + if (Obj.getHeader().e_machine == EM_ARM) { + ARM::EHABI::PrinterContext Ctx(W, Obj, ObjF.getFileName(), DotSymtabSec); Ctx.PrintUnwindInformation(); } @@ -2754,7 +2720,7 @@ return; bool IsHeaderValid; - Error Err = checkHashTable(*ObjF->getELFFile(), HashTable, &IsHeaderValid); + Error Err = checkHashTable(Obj, HashTable, &IsHeaderValid); if (IsHeaderValid) { W.printNumber("Num Buckets", HashTable->nbucket); W.printNumber("Num Chains", HashTable->nchain); @@ -2804,14 +2770,13 @@ } template -void ELFDumper::printGnuHashTable(const object::ObjectFile *Obj) { +void ELFDumper::printGnuHashTable() { DictScope D(W, "GnuHashTable"); if (!GnuHashTable) return; bool IsHeaderValid; - Error Err = checkGNUHashTable(*ObjF->getELFFile(), GnuHashTable, - &IsHeaderValid); + Error Err = checkGNUHashTable(Obj, GnuHashTable, &IsHeaderValid); if (IsHeaderValid) { W.printNumber("Num Buckets", GnuHashTable->nbuckets); W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx); @@ -2848,24 +2813,23 @@ } template void ELFDumper::printArchSpecificInfo() { - const ELFFile *Obj = ObjF->getELFFile(); - switch (Obj->getHeader().e_machine) { + switch (Obj.getHeader().e_machine) { case EM_ARM: case EM_RISCV: printAttributes(); break; case EM_MIPS: { - ELFDumperStyle->printMipsABIFlags(ObjF); + ELFDumperStyle->printMipsABIFlags(); printMipsOptions(); printMipsReginfo(); MipsGOTParser Parser(*this); if (Error E = Parser.findGOT(dynamic_table(), dynamic_symbols())) - reportError(std::move(E), ObjF->getFileName()); + reportError(std::move(E), ObjF.getFileName()); else if (!Parser.isGotEmpty()) ELFDumperStyle->printMipsGOT(Parser); if (Error E = Parser.findPLT(dynamic_table())) - reportError(std::move(E), ObjF->getFileName()); + reportError(std::move(E), ObjF.getFileName()); else if (!Parser.isPltEmpty()) ELFDumperStyle->printMipsPLT(Parser); break; @@ -2875,31 +2839,28 @@ } } -namespace { - template void ELFDumper::printAttributes() { - const ELFFile *Obj = ObjF->getELFFile(); - if (!Obj->isLE()) { + if (!Obj.isLE()) { W.startLine() << "Attributes not implemented.\n"; return; } - const unsigned Machine = Obj->getHeader().e_machine; + const unsigned Machine = Obj.getHeader().e_machine; assert((Machine == EM_ARM || Machine == EM_RISCV) && "Attributes not implemented."); DictScope BA(W, "BuildAttributes"); - for (const Elf_Shdr &Sec : cantFail(Obj->sections())) { + for (const Elf_Shdr &Sec : cantFail(Obj.sections())) { if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES && Sec.sh_type != ELF::SHT_RISCV_ATTRIBUTES) continue; ArrayRef Contents = - unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Sec)); + unwrapOrError(ObjF.getFileName(), Obj.getSectionContents(Sec)); if (Contents[0] != ELFAttrs::Format_Version) { reportWarning(createError(Twine("unrecognised FormatVersion: 0x") + Twine::utohexstr(Contents[0])), - ObjF->getFileName()); + ObjF.getFileName()); continue; } W.printHex("FormatVersion", Contents[0]); @@ -2909,14 +2870,16 @@ // TODO: Delete the redundant FormatVersion check above. if (Machine == EM_ARM) { if (Error E = ARMAttributeParser(&W).parse(Contents, support::little)) - reportWarning(std::move(E), ObjF->getFileName()); + reportWarning(std::move(E), ObjF.getFileName()); } else if (Machine == EM_RISCV) { if (Error E = RISCVAttributeParser(&W).parse(Contents, support::little)) - reportWarning(std::move(E), ObjF->getFileName()); + reportWarning(std::move(E), ObjF.getFileName()); } } } +namespace { + template class MipsGOTParser { public: TYPEDEF_ELF_TYPES(ELFT) @@ -2924,7 +2887,7 @@ using Entries = ArrayRef; const bool IsStatic; - const ELFO * const Obj; + const ELFO &Obj; const ELFDumper &Dumper; MipsGOTParser(const ELFDumper &D); @@ -2978,10 +2941,10 @@ template MipsGOTParser::MipsGOTParser(const ELFDumper &D) - : IsStatic(D.dynamic_table().empty()), Obj(D.getElfObject()->getELFFile()), + : IsStatic(D.dynamic_table().empty()), Obj(*D.getElfObject().getELFFile()), Dumper(D), GotSec(nullptr), LocalNum(0), GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr), - FileName(D.getElfObject()->getFileName()) {} + FileName(D.getElfObject().getFileName()) {} template Error MipsGOTParser::findGOT(Elf_Dyn_Range DynTable, @@ -2997,7 +2960,7 @@ return Error::success(); ArrayRef Content = - unwrapOrError(FileName, Obj->getSectionContents(*GotSec)); + unwrapOrError(FileName, Obj.getSectionContents(*GotSec)); GotEntries = Entries(reinterpret_cast(Content.data()), Content.size() / sizeof(Entry)); LocalNum = GotEntries.size(); @@ -3047,7 +3010,7 @@ GlobalNum = DynSymTotal - *DtGotSym; ArrayRef Content = - unwrapOrError(FileName, Obj->getSectionContents(*GotSec)); + unwrapOrError(FileName, Obj.getSectionContents(*GotSec)); GotEntries = Entries(reinterpret_cast(Content.data()), Content.size() / sizeof(Entry)); GotDynSyms = DynSyms.drop_front(*DtGotSym); @@ -3091,7 +3054,7 @@ Twine::utohexstr(*DtJmpRel)); if (Expected> PltContentOrErr = - Obj->getSectionContents(*PltSec)) + Obj.getSectionContents(*PltSec)) PltEntries = Entries(reinterpret_cast(PltContentOrErr->data()), PltContentOrErr->size() / sizeof(Entry)); @@ -3100,19 +3063,19 @@ toString(PltContentOrErr.takeError())); if (Expected PltSymTableOrErr = - Obj->getSection(PltRelSec->sh_link)) + Obj.getSection(PltRelSec->sh_link)) PltSymTable = *PltSymTableOrErr; else return createError("unable to get a symbol table linked to the " + - describe(*Obj, *PltRelSec) + ": " + + describe(Obj, *PltRelSec) + ": " + toString(PltSymTableOrErr.takeError())); if (Expected StrTabOrErr = - Obj->getStringTableForSymtab(*PltSymTable)) + Obj.getStringTableForSymtab(*PltSymTable)) PltStrTable = *StrTabOrErr; else return createError("unable to get a string table for the " + - describe(*Obj, *PltSymTable) + ": " + + describe(Obj, *PltSymTable) + ": " + toString(StrTabOrErr.takeError())); return Error::success(); @@ -3215,13 +3178,13 @@ MipsGOTParser::getPltSym(const Entry *E) const { int64_t Offset = std::distance(getPltEntries().data(), E); if (PltRelSec->sh_type == ELF::SHT_REL) { - Elf_Rel_Range Rels = unwrapOrError(FileName, Obj->rels(*PltRelSec)); + Elf_Rel_Range Rels = unwrapOrError(FileName, Obj.rels(*PltRelSec)); return unwrapOrError(FileName, - Obj->getRelocationSymbol(Rels[Offset], PltSymTable)); + Obj.getRelocationSymbol(Rels[Offset], PltSymTable)); } else { - Elf_Rela_Range Rels = unwrapOrError(FileName, Obj->relas(*PltRelSec)); + Elf_Rela_Range Rels = unwrapOrError(FileName, Obj.relas(*PltRelSec)); return unwrapOrError(FileName, - Obj->getRelocationSymbol(Rels[Offset], PltSymTable)); + Obj.getRelocationSymbol(Rels[Offset], PltSymTable)); } } @@ -3316,9 +3279,8 @@ return; } - const ELFFile *Obj = ObjF->getELFFile(); Expected> ContentsOrErr = - Obj->getSectionContents(*RegInfoSec); + Obj.getSectionContents(*RegInfoSec); if (!ContentsOrErr) { this->reportUniqueWarning(createError( "unable to read the content of the .reginfo section (" + @@ -3376,7 +3338,6 @@ } template void ELFDumper::printMipsOptions() { - const ELFFile *Obj = ObjF->getELFFile(); const Elf_Shdr *MipsOpts = findSectionByName(".MIPS.options"); if (!MipsOpts) { W.startLine() << "There is no .MIPS.options section in the file.\n"; @@ -3386,7 +3347,7 @@ DictScope GS(W, "MIPS Options"); ArrayRef Data = - unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(*MipsOpts)); + unwrapOrError(ObjF.getFileName(), Obj.getSectionContents(*MipsOpts)); const uint8_t *const SecBegin = Data.begin(); while (!Data.empty()) { bool IsSupported; @@ -3414,7 +3375,6 @@ } template void ELFDumper::printStackMap() const { - const ELFFile *Obj = ObjF->getELFFile(); const Elf_Shdr *StackMapSection = findSectionByName(".llvm_stackmaps"); if (!StackMapSection) return; @@ -3426,7 +3386,7 @@ }; Expected> ContentOrErr = - Obj->getSectionContents(*StackMapSection); + Obj.getSectionContents(*StackMapSection); if (!ContentOrErr) { Warn(ContentOrErr.takeError()); return; @@ -3648,7 +3608,7 @@ void GNUStyle::printReloc(const Relocation &R, unsigned RelIndex, const Elf_Shdr &Sec, const Elf_Shdr *SymTab) { Expected> Target = - this->dumper()->getRelocationTarget(R, SymTab); + this->dumper().getRelocationTarget(R, SymTab); if (!Target) this->reportUniqueWarning(createError( "unable to print relocation " + Twine(RelIndex) + " in " + @@ -3853,13 +3813,13 @@ {"Address", 41}, {"Off", 58 - Bias}, {"Size", 65 - Bias}, {"ES", 72 - Bias}, {"Flg", 75 - Bias}, {"Lk", 79 - Bias}, {"Inf", 82 - Bias}, {"Al", 86 - Bias}}; - for (auto &F : Fields) + for (const Field &F : Fields) printField(F); OS << "\n"; StringRef SecStrTable; if (Expected SecStrTableOrErr = this->Obj.getSectionStringTable( - Sections, this->dumper()->WarningHandler)) + Sections, this->dumper().WarningHandler)) SecStrTable = *SecStrTableOrErr; else this->reportUniqueWarning(SecStrTableOrErr.takeError()); @@ -3925,9 +3885,9 @@ } template -std::string GNUStyle::getSymbolSectionNdx(const Elf_Sym *Symbol, +std::string GNUStyle::getSymbolSectionNdx(const Elf_Sym &Symbol, unsigned SymIndex) { - unsigned SectionIndex = Symbol->st_shndx; + unsigned SectionIndex = Symbol.st_shndx; switch (SectionIndex) { case ELF::SHN_UNDEF: return "UND"; @@ -3937,9 +3897,9 @@ return "COM"; case ELF::SHN_XINDEX: { Expected IndexOrErr = object::getExtendedSymbolTableIndex( - *Symbol, SymIndex, this->dumper()->getShndxTable()); + Symbol, SymIndex, this->dumper().getShndxTable()); if (!IndexOrErr) { - assert(Symbol->st_shndx == SHN_XINDEX && + assert(Symbol.st_shndx == SHN_XINDEX && "getExtendedSymbolTableIndex should only fail due to an invalid " "SHT_SYMTAB_SHNDX table/reference"); this->reportUniqueWarning(IndexOrErr.takeError()); @@ -3968,18 +3928,18 @@ } template -void GNUStyle::printSymbol(const Elf_Sym *Symbol, unsigned SymIndex, +void GNUStyle::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic, bool NonVisibilityBitsUsed) { unsigned Bias = ELFT::Is64Bits ? 8 : 0; Field Fields[8] = {0, 8, 17 + Bias, 23 + Bias, 31 + Bias, 38 + Bias, 48 + Bias, 51 + Bias}; Fields[0].Str = to_string(format_decimal(SymIndex, 6)) + ":"; - Fields[1].Str = to_string( - format_hex_no_prefix(Symbol->st_value, ELFT::Is64Bits ? 16 : 8)); - Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5)); + Fields[1].Str = + to_string(format_hex_no_prefix(Symbol.st_value, ELFT::Is64Bits ? 16 : 8)); + Fields[2].Str = to_string(format_decimal(Symbol.st_size, 5)); - unsigned char SymbolType = Symbol->getType(); + unsigned char SymbolType = Symbol.getType(); if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU && SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS) Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes)); @@ -3987,19 +3947,19 @@ Fields[3].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes)); Fields[4].Str = - printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings)); + printEnum(Symbol.getBinding(), makeArrayRef(ElfSymbolBindings)); Fields[5].Str = - printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities)); - if (Symbol->st_other & ~0x3) + printEnum(Symbol.getVisibility(), makeArrayRef(ElfSymbolVisibilities)); + if (Symbol.st_other & ~0x3) Fields[5].Str += - " [st_other, 2)) + ">]"; + " []"; Fields[6].Column += NonVisibilityBitsUsed ? 13 : 0; Fields[6].Str = getSymbolSectionNdx(Symbol, SymIndex); Fields[7].Str = - this->dumper()->getFullSymbolName(Symbol, SymIndex, StrTable, IsDynamic); - for (auto &Entry : Fields) + this->dumper().getFullSymbolName(Symbol, SymIndex, StrTable, IsDynamic); + for (const Field &Entry : Fields) printField(Entry); OS << "\n"; } @@ -4028,11 +3988,11 @@ printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings)); Fields[6].Str = printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities)); - Fields[7].Str = getSymbolSectionNdx(Symbol, SymIndex); + Fields[7].Str = getSymbolSectionNdx(*Symbol, SymIndex); Fields[8].Str = - this->dumper()->getFullSymbolName(Symbol, SymIndex, StrTable, true); + this->dumper().getFullSymbolName(*Symbol, SymIndex, StrTable, true); - for (auto &Entry : Fields) + for (const Field &Entry : Fields) printField(Entry); OS << "\n"; } @@ -4042,14 +4002,14 @@ if (!PrintSymbols && !PrintDynamicSymbols) return; // GNU readelf prints both the .dynsym and .symtab with --symbols. - this->dumper()->printSymbolsHelper(true); + this->dumper().printSymbolsHelper(true); if (PrintSymbols) - this->dumper()->printSymbolsHelper(false); + this->dumper().printSymbolsHelper(false); } template void GNUStyle::printHashTableSymbols(const Elf_Hash &SysVHash) { - StringRef StringTable = this->dumper()->getDynamicStringTable(); + StringRef StringTable = this->dumper().getDynamicStringTable(); if (StringTable.empty()) return; @@ -4059,10 +4019,10 @@ OS << " Num Buc: Value Size Type Bind Vis Ndx Name"; OS << "\n"; - Elf_Sym_Range DynSyms = this->dumper()->dynamic_symbols(); + Elf_Sym_Range DynSyms = this->dumper().dynamic_symbols(); const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0]; if (!FirstSym) { - Optional DynSymRegion = this->dumper()->getDynSymRegion(); + Optional DynSymRegion = this->dumper().getDynSymRegion(); this->reportUniqueWarning( createError(Twine("unable to print symbols for the .hash table: the " "dynamic symbol table ") + @@ -4096,14 +4056,14 @@ template void GNUStyle::printGnuHashTableSymbols(const Elf_GnuHash &GnuHash) { - StringRef StringTable = this->dumper()->getDynamicStringTable(); + StringRef StringTable = this->dumper().getDynamicStringTable(); if (StringTable.empty()) return; - Elf_Sym_Range DynSyms = this->dumper()->dynamic_symbols(); + Elf_Sym_Range DynSyms = this->dumper().dynamic_symbols(); const Elf_Sym *FirstSym = DynSyms.empty() ? nullptr : &DynSyms[0]; if (!FirstSym) { - Optional DynSymRegion = this->dumper()->getDynSymRegion(); + Optional DynSymRegion = this->dumper().getDynSymRegion(); this->reportUniqueWarning(createError( Twine("unable to print symbols for the .gnu.hash table: the " "dynamic symbol table ") + @@ -4129,7 +4089,7 @@ } template void GNUStyle::printHashSymbols() { - if (const Elf_Hash *SysVHash = this->dumper()->getHashTable()) { + if (const Elf_Hash *SysVHash = this->dumper().getHashTable()) { OS << "\n Symbol table of .hash for image:\n"; if (Error E = checkHashTable(this->Obj, SysVHash)) this->reportUniqueWarning(std::move(E)); @@ -4138,7 +4098,7 @@ } // Try printing the .gnu.hash table. - if (const Elf_GnuHash *GnuHash = this->dumper()->getGnuHashTable()) { + if (const Elf_GnuHash *GnuHash = this->dumper().getGnuHashTable()) { OS << "\n Symbol table of .gnu.hash for image:\n"; if (ELFT::Is64Bits) OS << " Num Buc: Value Size Type Bind Vis Ndx Name"; @@ -4282,8 +4242,8 @@ Fields[5].Str = to_string(format_hex(Phdr.p_memsz, SizeWidth)); Fields[6].Str = printPhdrFlags(Phdr.p_flags); Fields[7].Str = to_string(format_hex(Phdr.p_align, 1)); - for (auto Field : Fields) - printField(Field); + for (const Field &F : Fields) + printField(F); if (Phdr.p_type == ELF::PT_INTERP) { OS << "\n"; auto ReportBadInterp = [&](const Twine &Msg) { @@ -4369,7 +4329,7 @@ template RelSymbol getSymbolForReloc(const ELFFile &Obj, StringRef FileName, - const ELFDumper *Dumper, + const ELFDumper &Dumper, const Relocation &Reloc) { auto WarnAndReturn = [&](const typename ELFT::Sym *Sym, const Twine &Reason) -> RelSymbol { @@ -4380,7 +4340,7 @@ return {Sym, ""}; }; - ArrayRef Symbols = Dumper->dynamic_symbols(); + ArrayRef Symbols = Dumper.dynamic_symbols(); const typename ELFT::Sym *FirstSym = Symbols.begin(); if (!FirstSym) return WarnAndReturn(nullptr, "no dynamic symbol table found"); @@ -4395,7 +4355,7 @@ Twine(Symbols.size()) + ")"); const typename ELFT::Sym *Sym = FirstSym + Reloc.Symbol; - Expected ErrOrName = Sym->getName(Dumper->getDynamicStringTable()); + Expected ErrOrName = Sym->getName(Dumper.getDynamicStringTable()); if (!ErrOrName) return WarnAndReturn(Sym, toString(ErrOrName.takeError())); @@ -4419,15 +4379,13 @@ } template void GNUStyle::printDynamic() { - Elf_Dyn_Range Table = this->dumper()->dynamic_table(); + Elf_Dyn_Range Table = this->dumper().dynamic_table(); if (Table.empty()) return; - const DynRegionInfo &DynamicTableRegion = - this->dumper()->getDynamicTableRegion(); - OS << "Dynamic section at offset " - << format_hex(reinterpret_cast(DynamicTableRegion.Addr) - + << format_hex(reinterpret_cast( + this->dumper().getDynamicTableRegion().Addr) - this->Obj.base(), 1) << " contains " << Table.size() << " entries:\n"; @@ -4445,7 +4403,7 @@ uintX_t Tag = Entry.getTag(); std::string Type = std::string("(") + this->Obj.getDynamicTagAsString(Tag).c_str() + ")"; - std::string Value = this->dumper()->getDynamicEntry(Tag, Entry.getVal()); + std::string Value = this->dumper().getDynamicEntry(Tag, Entry.getVal()); OS << " " << format_hex(Tag, ELFT::Is64Bits ? 18 : 10) << format(ValueFmt.c_str(), Type.c_str()) << Value << "\n"; } @@ -4457,29 +4415,29 @@ template void DumpStyle::printDynamicRelocationsHelper() { const bool IsMips64EL = this->Obj.isMips64EL(); - const DynRegionInfo &DynRelaRegion = this->dumper()->getDynRelaRegion(); + const DynRegionInfo &DynRelaRegion = this->dumper().getDynRelaRegion(); if (DynRelaRegion.Size > 0) { printDynamicRelocHeader(ELF::SHT_RELA, "RELA", DynRelaRegion); - for (const Elf_Rela &Rela : this->dumper()->dyn_relas()) + for (const Elf_Rela &Rela : this->dumper().dyn_relas()) printDynamicReloc(Relocation(Rela, IsMips64EL)); } - const DynRegionInfo &DynRelRegion = this->dumper()->getDynRelRegion(); + const DynRegionInfo &DynRelRegion = this->dumper().getDynRelRegion(); if (DynRelRegion.Size > 0) { printDynamicRelocHeader(ELF::SHT_REL, "REL", DynRelRegion); - for (const Elf_Rel &Rel : this->dumper()->dyn_rels()) + for (const Elf_Rel &Rel : this->dumper().dyn_rels()) printDynamicReloc(Relocation(Rel, IsMips64EL)); } - const DynRegionInfo &DynRelrRegion = this->dumper()->getDynRelrRegion(); + const DynRegionInfo &DynRelrRegion = this->dumper().getDynRelrRegion(); if (DynRelrRegion.Size > 0) { printDynamicRelocHeader(ELF::SHT_REL, "RELR", DynRelrRegion); - Elf_Relr_Range Relrs = this->dumper()->dyn_relrs(); + Elf_Relr_Range Relrs = this->dumper().dyn_relrs(); for (const Elf_Rel &Rel : Obj.decode_relrs(Relrs)) printDynamicReloc(Relocation(Rel, IsMips64EL)); } - const DynRegionInfo &DynPLTRelRegion = this->dumper()->getDynPLTRelRegion(); + const DynRegionInfo &DynPLTRelRegion = this->dumper().getDynPLTRelRegion(); if (DynPLTRelRegion.Size) { if (DynPLTRelRegion.EntSize == sizeof(Elf_Rela)) { printDynamicRelocHeader(ELF::SHT_RELA, "PLT", DynPLTRelRegion); @@ -4525,7 +4483,7 @@ printGNUVersionSectionProlog(Sec, "Version symbols", Sec->sh_size / sizeof(Elf_Versym)); Expected> VerTableOrErr = - this->dumper()->getVersionTable(Sec, /*SymTab=*/nullptr, + this->dumper().getVersionTable(*Sec, /*SymTab=*/nullptr, /*StrTab=*/nullptr); if (!VerTableOrErr) { this->reportUniqueWarning(VerTableOrErr.takeError()); @@ -4543,7 +4501,7 @@ bool IsDefault; Expected NameOrErr = - this->dumper()->getSymbolVersionByIndex(Ndx, IsDefault); + this->dumper().getSymbolVersionByIndex(Ndx, IsDefault); if (!NameOrErr) { if (!NameOrErr) this->reportUniqueWarning( @@ -4599,7 +4557,7 @@ printGNUVersionSectionProlog(Sec, "Version definition", Sec->sh_info); - Expected> V = this->dumper()->getVersionDefinitions(Sec); + Expected> V = this->dumper().getVersionDefinitions(*Sec); if (!V) { this->reportUniqueWarning(V.takeError()); return; @@ -4628,7 +4586,7 @@ printGNUVersionSectionProlog(Sec, "Version needs", VerneedNum); Expected> V = - this->dumper()->getVersionDependencies(Sec); + this->dumper().getVersionDependencies(*Sec); if (!V) { this->reportUniqueWarning(V.takeError()); return; @@ -4704,7 +4662,7 @@ template void GNUStyle::printGnuHashHistogram(const Elf_GnuHash &GnuHashTable) { Expected> ChainsOrErr = getGnuHashTableChains( - this->dumper()->getDynSymRegion(), &GnuHashTable); + this->dumper().getDynSymRegion(), &GnuHashTable); if (!ChainsOrErr) { this->reportUniqueWarning( createError("unable to print the GNU hash table histogram: " + @@ -4762,7 +4720,7 @@ // buckets, and the cumulative coverage of symbols for each set of buckets. template void GNUStyle::printHashHistograms() { // Print histogram for the .hash section. - if (const Elf_Hash *HashTable = this->dumper()->getHashTable()) { + if (const Elf_Hash *HashTable = this->dumper().getHashTable()) { if (Error E = checkHashTable(this->Obj, HashTable)) this->reportUniqueWarning(std::move(E)); else @@ -4770,7 +4728,7 @@ } // Print histogram for the .gnu.hash section. - if (const Elf_GnuHash *GnuHashTable = this->dumper()->getGnuHashTable()) { + if (const Elf_GnuHash *GnuHashTable = this->dumper().getGnuHashTable()) { if (Error E = checkGNUHashTable(this->Obj, GnuHashTable)) this->reportUniqueWarning(std::move(E)); else @@ -4973,7 +4931,7 @@ static std::string getGNUBuildId(ArrayRef Desc) { std::string str; raw_string_ostream OS(str); - for (const auto &B : Desc) + for (uint8_t B : Desc) OS << format_hex_no_prefix(B, 2); return OS.str(); } @@ -5005,7 +4963,7 @@ break; case ELF::NT_GNU_PROPERTY_TYPE_0: OS << " Properties:"; - for (const auto &Property : getGNUPropertyList(Desc)) + for (const std::string &Property : getGNUPropertyList(Desc)) OS << " " << Property << "\n"; break; } @@ -5044,7 +5002,7 @@ default: return {"", ""}; case ELF::NT_AMDGPU_METADATA: { - auto MsgPackString = + StringRef MsgPackString = StringRef(reinterpret_cast(Desc.data()), Desc.size()); msgpack::Document MsgPackDoc; if (!MsgPackDoc.readFromBlob(MsgPackString, /*Multi=*/false)) @@ -5337,7 +5295,7 @@ PrintHeader(expectedToOptional(this->Obj.getSectionName(S)), S.sh_offset, S.sh_size); Error Err = Error::success(); - for (auto Note : this->Obj.notes(S, Err)) + for (const Elf_Note &Note : this->Obj.notes(S, Err)) ProcessNote(Note); if (Err) reportError(std::move(Err), this->FileName); @@ -5356,7 +5314,7 @@ continue; PrintHeader(/*SecName=*/None, P.p_offset, P.p_filesz); Error Err = Error::success(); - for (auto Note : this->Obj.notes(P, Err)) + for (const Elf_Note &Note : this->Obj.notes(P, Err)) ProcessNote(Note); if (Err) reportError(std::move(Err), this->FileName); @@ -5480,7 +5438,7 @@ StringRef DumpStyle::getPrintableSectionName(const Elf_Shdr &Sec) const { StringRef Name = ""; if (Expected SecNameOrErr = - Obj.getSectionName(Sec, this->dumper()->WarningHandler)) + Obj.getSectionName(Sec, this->dumper().WarningHandler)) Name = *SecNameOrErr; else this->reportUniqueWarning(createError("unable to get the name of " + @@ -5841,10 +5799,10 @@ else OS << " Address Access Initial Sym.Val. Type Ndx Name\n"; for (auto &E : Parser.getGlobalEntries()) { - const Elf_Sym *Sym = Parser.getGotSym(&E); - const Elf_Sym *FirstSym = &this->dumper()->dynamic_symbols()[0]; - std::string SymName = this->dumper()->getFullSymbolName( - Sym, FirstSym - Sym, this->dumper()->getDynamicStringTable(), false); + const Elf_Sym &Sym = *Parser.getGotSym(&E); + const Elf_Sym &FirstSym = this->dumper().dynamic_symbols()[0]; + std::string SymName = this->dumper().getFullSymbolName( + Sym, &FirstSym - &Sym, this->dumper().getDynamicStringTable(), false); OS.PadToColumn(2); OS << to_string(format_hex_no_prefix(Parser.getGotAddress(&E), 8 + Bias)); @@ -5853,12 +5811,12 @@ OS.PadToColumn(22 + Bias); OS << to_string(format_hex_no_prefix(E, 8 + Bias)); OS.PadToColumn(31 + 2 * Bias); - OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias)); + OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias)); OS.PadToColumn(40 + 3 * Bias); - OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes)); + OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes)); OS.PadToColumn(48 + 3 * Bias); OS << getSymbolSectionNdx( - Sym, Sym - this->dumper()->dynamic_symbols().begin()); + Sym, &Sym - this->dumper().dynamic_symbols().begin()); OS.PadToColumn(52 + 3 * Bias); OS << SymName << "\n"; } @@ -5894,24 +5852,24 @@ OS << " Entries:\n"; OS << " Address Initial Sym.Val. Type Ndx Name\n"; for (auto &E : Parser.getPltEntries()) { - const Elf_Sym *Sym = Parser.getPltSym(&E); - const Elf_Sym *FirstSym = - cantFail(this->Obj.template getEntry( + const Elf_Sym &Sym = *Parser.getPltSym(&E); + const Elf_Sym &FirstSym = + *cantFail(this->Obj.template getEntry( *Parser.getPltSymTable(), 0)); - std::string SymName = this->dumper()->getFullSymbolName( - Sym, FirstSym - Sym, this->dumper()->getDynamicStringTable(), false); + std::string SymName = this->dumper().getFullSymbolName( + Sym, &FirstSym - &Sym, this->dumper().getDynamicStringTable(), false); OS.PadToColumn(2); OS << to_string(format_hex_no_prefix(Parser.getPltAddress(&E), 8 + Bias)); OS.PadToColumn(11 + Bias); OS << to_string(format_hex_no_prefix(E, 8 + Bias)); OS.PadToColumn(20 + 2 * Bias); - OS << to_string(format_hex_no_prefix(Sym->st_value, 8 + Bias)); + OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias)); OS.PadToColumn(29 + 3 * Bias); - OS << printEnum(Sym->getType(), makeArrayRef(ElfSymbolTypes)); + OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes)); OS.PadToColumn(37 + 3 * Bias); OS << getSymbolSectionNdx( - Sym, Sym - this->dumper()->dynamic_symbols().begin()); + Sym, &Sym - this->dumper().dynamic_symbols().begin()); OS.PadToColumn(41 + 3 * Bias); OS << SymName << "\n"; } @@ -5920,15 +5878,14 @@ template Expected *> -getMipsAbiFlagsSection(const ELFObjectFile *ObjF, - const ELFDumper &Dumper) { +getMipsAbiFlagsSection(const ELFDumper &Dumper) { const typename ELFT::Shdr *Sec = Dumper.findSectionByName(".MIPS.abiflags"); if (Sec == nullptr) return nullptr; - const ELFFile *Obj = ObjF->getELFFile(); constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: "; - Expected> DataOrErr = Obj->getSectionContents(*Sec); + Expected> DataOrErr = + Dumper.getElfObject().getELFFile()->getSectionContents(*Sec); if (!DataOrErr) return createError(ErrPrefix + toString(DataOrErr.takeError())); @@ -5938,11 +5895,10 @@ return reinterpret_cast *>(DataOrErr->data()); } -template -void GNUStyle::printMipsABIFlags(const ELFObjectFile *ObjF) { +template void GNUStyle::printMipsABIFlags() { const Elf_Mips_ABIFlags *Flags = nullptr; if (Expected *> SecOrErr = - getMipsAbiFlagsSection(ObjF, *this->dumper())) + getMipsAbiFlagsSection(this->dumper())) Flags = *SecOrErr; else this->reportUniqueWarning(SecOrErr.takeError()); @@ -6088,7 +6044,7 @@ void LLVMStyle::printReloc(const Relocation &R, unsigned RelIndex, const Elf_Shdr &Sec, const Elf_Shdr *SymTab) { Expected> Target = - this->dumper()->getRelocationTarget(R, SymTab); + this->dumper().getRelocationTarget(R, SymTab); if (!Target) { this->reportUniqueWarning(createError( "unable to print relocation " + Twine(RelIndex) + " in " + @@ -6150,19 +6106,18 @@ if (opts::SectionSymbols) { ListScope D(W, "Symbols"); - if (const Elf_Shdr *Symtab = this->dumper()->getDotSymtabSec()) { + if (const Elf_Shdr *Symtab = this->dumper().getDotSymtabSec()) { StringRef StrTable = unwrapOrError( this->FileName, this->Obj.getStringTableForSymtab(*Symtab)); typename ELFT::SymRange Symbols = unwrapOrError(this->FileName, this->Obj.symbols(Symtab)); for (const Elf_Sym &Sym : Symbols) { - const Elf_Shdr *SymSec = - unwrapOrError(this->FileName, - this->Obj.getSection( - Sym, Symtab, this->dumper()->getShndxTable())); + const Elf_Shdr *SymSec = unwrapOrError( + this->FileName, this->Obj.getSection( + Sym, Symtab, this->dumper().getShndxTable())); if (SymSec == &Sec) - printSymbol(&Sym, &Sym - &Symbols[0], StrTable, false, false); + printSymbol(Sym, &Sym - &Symbols[0], StrTable, false, false); } } } @@ -6178,33 +6133,33 @@ } template -void LLVMStyle::printSymbolSection(const Elf_Sym *Symbol, +void LLVMStyle::printSymbolSection(const Elf_Sym &Symbol, unsigned SymIndex) { auto GetSectionSpecialType = [&]() -> Optional { - if (Symbol->isUndefined()) + if (Symbol.isUndefined()) return StringRef("Undefined"); - if (Symbol->isProcessorSpecific()) + if (Symbol.isProcessorSpecific()) return StringRef("Processor Specific"); - if (Symbol->isOSSpecific()) + if (Symbol.isOSSpecific()) return StringRef("Operating System Specific"); - if (Symbol->isAbsolute()) + if (Symbol.isAbsolute()) return StringRef("Absolute"); - if (Symbol->isCommon()) + if (Symbol.isCommon()) return StringRef("Common"); - if (Symbol->isReserved() && Symbol->st_shndx != SHN_XINDEX) + if (Symbol.isReserved() && Symbol.st_shndx != SHN_XINDEX) return StringRef("Reserved"); return None; }; if (Optional Type = GetSectionSpecialType()) { - W.printHex("Section", *Type, Symbol->st_shndx); + W.printHex("Section", *Type, Symbol.st_shndx); return; } Expected SectionIndex = - this->dumper()->getSymbolSectionIndex(Symbol, SymIndex); + this->dumper().getSymbolSectionIndex(Symbol, SymIndex); if (!SectionIndex) { - assert(Symbol->st_shndx == SHN_XINDEX && + assert(Symbol.st_shndx == SHN_XINDEX && "getSymbolSectionIndex should only fail due to an invalid " "SHT_SYMTAB_SHNDX table/reference"); this->reportUniqueWarning(SectionIndex.takeError()); @@ -6213,11 +6168,11 @@ } Expected SectionName = - this->dumper()->getSymbolSectionName(Symbol, *SectionIndex); + this->dumper().getSymbolSectionName(Symbol, *SectionIndex); if (!SectionName) { // Don't report an invalid section name if the section headers are missing. // In such situations, all sections will be "invalid". - if (!this->dumper()->getElfObject()->sections().empty()) + if (!this->dumper().getElfObject().sections().empty()) this->reportUniqueWarning(SectionName.takeError()); else consumeError(SectionName.takeError()); @@ -6228,24 +6183,24 @@ } template -void LLVMStyle::printSymbol(const Elf_Sym *Symbol, unsigned SymIndex, +void LLVMStyle::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex, Optional StrTable, bool IsDynamic, bool /*NonVisibilityBitsUsed*/) { std::string FullSymbolName = - this->dumper()->getFullSymbolName(Symbol, SymIndex, StrTable, IsDynamic); - unsigned char SymbolType = Symbol->getType(); + this->dumper().getFullSymbolName(Symbol, SymIndex, StrTable, IsDynamic); + unsigned char SymbolType = Symbol.getType(); DictScope D(W, "Symbol"); - W.printNumber("Name", FullSymbolName, Symbol->st_name); - W.printHex("Value", Symbol->st_value); - W.printNumber("Size", Symbol->st_size); - W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings)); + W.printNumber("Name", FullSymbolName, Symbol.st_name); + W.printHex("Value", Symbol.st_value); + W.printNumber("Size", Symbol.st_size); + W.printEnum("Binding", Symbol.getBinding(), makeArrayRef(ElfSymbolBindings)); if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU && SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS) W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes)); else W.printEnum("Type", SymbolType, makeArrayRef(ElfSymbolTypes)); - if (Symbol->st_other == 0) + if (Symbol.st_other == 0) // Usually st_other flag is zero. Do not pollute the output // by flags enumeration in that case. W.printNumber("Other", 0); @@ -6256,7 +6211,7 @@ // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16 // flag overlapped with other ST_MIPS_xxx flags. So consider both // cases separately. - if ((Symbol->st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16) + if ((Symbol.st_other & STO_MIPS_MIPS16) == STO_MIPS_MIPS16) SymOtherFlags.insert(SymOtherFlags.end(), std::begin(ElfMips16SymOtherFlags), std::end(ElfMips16SymOtherFlags)); @@ -6265,7 +6220,7 @@ std::begin(ElfMipsSymOtherFlags), std::end(ElfMipsSymOtherFlags)); } - W.printFlags("Other", Symbol->st_other, makeArrayRef(SymOtherFlags), 0x3u); + W.printFlags("Other", Symbol.st_other, makeArrayRef(SymOtherFlags), 0x3u); } printSymbolSection(Symbol, SymIndex); } @@ -6281,16 +6236,16 @@ template void LLVMStyle::printSymbols() { ListScope Group(W, "Symbols"); - this->dumper()->printSymbolsHelper(false); + this->dumper().printSymbolsHelper(false); } template void LLVMStyle::printDynamicSymbols() { ListScope Group(W, "DynamicSymbols"); - this->dumper()->printSymbolsHelper(true); + this->dumper().printSymbolsHelper(true); } template void LLVMStyle::printDynamic() { - Elf_Dyn_Range Table = this->dumper()->dynamic_table(); + Elf_Dyn_Range Table = this->dumper().dynamic_table(); if (Table.empty()) return; @@ -6306,7 +6261,7 @@ std::string ValueFmt = "%-" + std::to_string(MaxTagSize) + "s "; for (auto Entry : Table) { uintX_t Tag = Entry.getTag(); - std::string Value = this->dumper()->getDynamicEntry(Tag, Entry.getVal()); + std::string Value = this->dumper().getDynamicEntry(Tag, Entry.getVal()); W.startLine() << " " << format_hex(Tag, ELFT::Is64Bits ? 18 : 10, true) << " " << format(ValueFmt.c_str(), @@ -6375,7 +6330,7 @@ StringRef StrTable; ArrayRef Syms; Expected> VerTableOrErr = - this->dumper()->getVersionTable(Sec, &Syms, &StrTable); + this->dumper().getVersionTable(*Sec, &Syms, &StrTable); if (!VerTableOrErr) { this->reportUniqueWarning(VerTableOrErr.takeError()); return; @@ -6387,9 +6342,8 @@ for (size_t I = 0, E = Syms.size(); I < E; ++I) { DictScope S(W, "Symbol"); W.printNumber("Version", (*VerTableOrErr)[I].vs_index & VERSYM_VERSION); - W.printString("Name", - this->dumper()->getFullSymbolName(&Syms[I], I, StrTable, - /*IsDynamic=*/true)); + W.printString("Name", this->dumper().getFullSymbolName(Syms[I], I, StrTable, + /*IsDynamic=*/true)); } } @@ -6404,7 +6358,7 @@ if (!Sec) return; - Expected> V = this->dumper()->getVersionDefinitions(Sec); + Expected> V = this->dumper().getVersionDefinitions(*Sec); if (!V) { this->reportUniqueWarning(V.takeError()); return; @@ -6429,8 +6383,7 @@ if (!Sec) return; - Expected> V = - this->dumper()->getVersionDependencies(Sec); + Expected> V = this->dumper().getVersionDependencies(*Sec); if (!V) { this->reportUniqueWarning(V.takeError()); return; @@ -6459,12 +6412,12 @@ template void LLVMStyle::printCGProfile() { ListScope L(W, "CGProfile"); - if (!this->dumper()->getDotCGProfileSec()) + if (!this->dumper().getDotCGProfileSec()) return; Expected> CGProfileOrErr = this->Obj.template getSectionContentsAsArray( - *this->dumper()->getDotCGProfileSec()); + *this->dumper().getDotCGProfileSec()); if (!CGProfileOrErr) { this->reportUniqueWarning( createError("unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: " + @@ -6474,9 +6427,9 @@ for (const Elf_CGProfile &CGPE : *CGProfileOrErr) { DictScope D(W, "CGProfileEntry"); - W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from), + W.printNumber("From", this->dumper().getStaticSymbolName(CGPE.cgp_from), CGPE.cgp_from); - W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to), + W.printNumber("To", this->dumper().getStaticSymbolName(CGPE.cgp_to), CGPE.cgp_to); W.printNumber("Weight", CGPE.cgp_weight); } @@ -6499,7 +6452,7 @@ template void LLVMStyle::printAddrsig() { ListScope L(W, "Addrsig"); - const Elf_Shdr *Sec = this->dumper()->getDotAddrsigSec(); + const Elf_Shdr *Sec = this->dumper().getDotAddrsigSec(); if (!Sec) return; @@ -6519,7 +6472,7 @@ } for (uint64_t Sym : *SymsOrErr) - W.printNumber("Sym", this->dumper()->getStaticSymbolName(Sym), Sym); + W.printNumber("Sym", this->dumper().getStaticSymbolName(Sym), Sym); } template @@ -6547,7 +6500,7 @@ break; case ELF::NT_GNU_PROPERTY_TYPE_0: ListScope D(W, "Property"); - for (const auto &Property : getGNUPropertyList(Desc)) + for (const std::string &Property : getGNUPropertyList(Desc)) W.printString(Property); break; } @@ -6769,15 +6722,15 @@ PrintEntry(&E); - const Elf_Sym *Sym = Parser.getGotSym(&E); - W.printHex("Value", Sym->st_value); - W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes)); - printSymbolSection(Sym, Sym - this->dumper()->dynamic_symbols().begin()); + const Elf_Sym &Sym = *Parser.getGotSym(&E); + W.printHex("Value", Sym.st_value); + W.printEnum("Type", Sym.getType(), makeArrayRef(ElfSymbolTypes)); + printSymbolSection(Sym, &Sym - this->dumper().dynamic_symbols().begin()); - std::string SymName = this->dumper()->getFullSymbolName( - Sym, Sym - &Parser.getGotDynSyms()[0], - this->dumper()->getDynamicStringTable(), true); - W.printNumber("Name", SymName, Sym->st_name); + std::string SymName = this->dumper().getFullSymbolName( + Sym, &Sym - &Parser.getGotDynSyms()[0], + this->dumper().getDynamicStringTable(), true); + W.printNumber("Name", SymName, Sym.st_name); } } @@ -6814,26 +6767,25 @@ DictScope D(W, "Entry"); PrintEntry(&E); - const Elf_Sym *Sym = Parser.getPltSym(&E); - W.printHex("Value", Sym->st_value); - W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes)); - printSymbolSection(Sym, Sym - this->dumper()->dynamic_symbols().begin()); + const Elf_Sym &Sym = *Parser.getPltSym(&E); + W.printHex("Value", Sym.st_value); + W.printEnum("Type", Sym.getType(), makeArrayRef(ElfSymbolTypes)); + printSymbolSection(Sym, &Sym - this->dumper().dynamic_symbols().begin()); const Elf_Sym *FirstSym = cantFail(this->Obj.template getEntry( *Parser.getPltSymTable(), 0)); - std::string SymName = this->dumper()->getFullSymbolName( - Sym, Sym - FirstSym, Parser.getPltStrTable(), true); - W.printNumber("Name", SymName, Sym->st_name); + std::string SymName = this->dumper().getFullSymbolName( + Sym, &Sym - FirstSym, Parser.getPltStrTable(), true); + W.printNumber("Name", SymName, Sym.st_name); } } } -template -void LLVMStyle::printMipsABIFlags(const ELFObjectFile *ObjF) { +template void LLVMStyle::printMipsABIFlags() { const Elf_Mips_ABIFlags *Flags; if (Expected *> SecOrErr = - getMipsAbiFlagsSection(ObjF, *this->dumper())) { + getMipsAbiFlagsSection(this->dumper())) { Flags = *SecOrErr; if (!Flags) { W.startLine() << "There is no .MIPS.abiflags section in the file.\n"; diff --git a/llvm/tools/llvm-readobj/ObjDumper.h b/llvm/tools/llvm-readobj/ObjDumper.h --- a/llvm/tools/llvm-readobj/ObjDumper.h +++ b/llvm/tools/llvm-readobj/ObjDumper.h @@ -61,7 +61,7 @@ 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 @@ virtual void printStackMap() const = 0; - void printSectionsAsString(const object::ObjectFile *Obj, + void printSectionsAsString(const object::ObjectFile &Obj, ArrayRef Sections); - void printSectionsAsHex(const object::ObjectFile *Obj, + void printSectionsAsHex(const object::ObjectFile &Obj, ArrayRef Sections); protected: diff --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp --- a/llvm/tools/llvm-readobj/ObjDumper.cpp +++ b/llvm/tools/llvm-readobj/ObjDumper.cpp @@ -37,7 +37,7 @@ } static std::vector -getSectionRefsByNameOrIndex(const object::ObjectFile *Obj, +getSectionRefsByNameOrIndex(const object::ObjectFile &Obj, ArrayRef Sections) { std::vector Ret; std::map SecNames; @@ -50,9 +50,9 @@ 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 @@ if (!S.second) reportWarning( createError(formatv("could not find section '{0}'", S.first).str()), - Obj->getFileName()); + Obj.getFileName()); for (std::pair 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 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 @@ 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::printSectionsAsHex(const object::ObjectFile *Obj, +void ObjDumper::printSectionsAsHex(const object::ObjectFile &Obj, ArrayRef 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 @@ 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(); diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -495,13 +495,13 @@ 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()) {