From 437027774aa2d53bc111cc2a434f03c6f041dae0 Mon Sep 17 00:00:00 2001 From: Georgii Rymar Date: Thu, 3 Dec 2020 12:57:00 +0300 Subject: [PATCH] [lib/Object, tools] - Make ELFObjectFile::getELFFile return reference. We always have an object, so we don't have to return a pointer. Differential revision: https://reviews.llvm.org/D92560 --- include/llvm/Object/ELFObjectFile.h | 2 +- lib/DebugInfo/Symbolize/Symbolize.cpp | 8 ++--- lib/ExecutionEngine/JITLink/ELF_x86_64.cpp | 2 +- lib/InterfaceStub/ELFObjHandler.cpp | 15 ++++----- lib/XRay/InstrumentationMap.cpp | 8 ++--- tools/llvm-gsymutil/llvm-gsymutil.cpp | 4 +-- tools/llvm-objcopy/ELF/ELFObjcopy.cpp | 8 ++--- tools/llvm-objcopy/ELF/Object.h | 2 +- tools/llvm-objdump/ELFDump.cpp | 38 +++++++++++----------- tools/llvm-readobj/DwarfCFIEHPrinter.h | 8 ++--- tools/llvm-readobj/ELFDumper.cpp | 14 ++++---- tools/obj2yaml/elf2yaml.cpp | 8 ++--- 12 files changed, 57 insertions(+), 60 deletions(-) diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 3a4937235d5..a75aba86f9d 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -441,7 +441,7 @@ public: unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; } - const ELFFile *getELFFile() const { return &EF; } + const ELFFile &getELFFile() const { return EF; } bool isDyldType() const { return isDyldELFObject; } static bool classof(const Binary *v) { diff --git a/lib/DebugInfo/Symbolize/Symbolize.cpp b/lib/DebugInfo/Symbolize/Symbolize.cpp index 3eb84a141b8..5a98ce0753b 100644 --- a/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -287,10 +287,8 @@ bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, } template -Optional> getBuildID(const ELFFile *Obj) { - if (!Obj) - return {}; - auto PhdrsOrErr = Obj->program_headers(); +Optional> getBuildID(const ELFFile &Obj) { + auto PhdrsOrErr = Obj.program_headers(); if (!PhdrsOrErr) { consumeError(PhdrsOrErr.takeError()); return {}; @@ -299,7 +297,7 @@ Optional> getBuildID(const ELFFile *Obj) { if (P.p_type != ELF::PT_NOTE) continue; Error Err = Error::success(); - for (auto N : Obj->notes(P, Err)) + for (auto N : Obj.notes(P, Err)) if (N.getType() == ELF::NT_GNU_BUILD_ID && N.getName() == ELF::ELF_NOTE_GNU) return N.getDesc(); consumeError(std::move(Err)); diff --git a/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp index 49933fc14d5..a0f944db794 100644 --- a/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp +++ b/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp @@ -626,7 +626,7 @@ private: auto &ELFObjFile = cast>(**ELFObj); std::string fileName(ELFObj->get()->getFileName()); return ELFLinkGraphBuilder_x86_64(std::move(fileName), - *ELFObjFile.getELFFile()) + ELFObjFile.getELFFile()) .buildGraph(); } diff --git a/lib/InterfaceStub/ELFObjHandler.cpp b/lib/InterfaceStub/ELFObjHandler.cpp index ba583b79571..e50ebd7b8ba 100644 --- a/lib/InterfaceStub/ELFObjHandler.cpp +++ b/lib/InterfaceStub/ELFObjHandler.cpp @@ -585,15 +585,15 @@ buildStub(const ELFObjectFile &ElfObj) { using Elf_Sym_Range = typename ELFT::SymRange; using Elf_Sym = typename ELFT::Sym; std::unique_ptr DestStub = std::make_unique(); - const ELFFile *ElfFile = ElfObj.getELFFile(); + const ELFFile &ElfFile = ElfObj.getELFFile(); // Fetch .dynamic table. - Expected DynTable = ElfFile->dynamicEntries(); + Expected DynTable = ElfFile.dynamicEntries(); if (!DynTable) { return DynTable.takeError(); } // Fetch program headers. - Expected PHdrs = ElfFile->program_headers(); + Expected PHdrs = ElfFile.program_headers(); if (!PHdrs) { return PHdrs.takeError(); } @@ -604,8 +604,7 @@ buildStub(const ELFObjectFile &ElfObj) { return std::move(Err); // Get pointer to in-memory location of .dynstr section. - Expected DynStrPtr = - ElfFile->toMappedAddr(DynEnt.StrTabAddr); + Expected DynStrPtr = ElfFile.toMappedAddr(DynEnt.StrTabAddr); if (!DynStrPtr) return appendToError(DynStrPtr.takeError(), "when locating .dynstr section contents"); @@ -614,7 +613,7 @@ buildStub(const ELFObjectFile &ElfObj) { DynEnt.StrSize); // Populate Arch from ELF header. - DestStub->Arch = ElfFile->getHeader().e_machine; + DestStub->Arch = ElfFile.getHeader().e_machine; // Populate SoName from .dynamic entries and dynamic string table. if (DynEnt.SONameOffset.hasValue()) { @@ -637,13 +636,13 @@ buildStub(const ELFObjectFile &ElfObj) { } // Populate Symbols from .dynsym table and dynamic string table. - Expected SymCount = getNumSyms(DynEnt, *ElfFile); + Expected SymCount = getNumSyms(DynEnt, ElfFile); if (!SymCount) return SymCount.takeError(); if (*SymCount > 0) { // Get pointer to in-memory location of .dynsym section. Expected DynSymPtr = - ElfFile->toMappedAddr(DynEnt.DynSymAddr); + ElfFile.toMappedAddr(DynEnt.DynSymAddr); if (!DynSymPtr) return appendToError(DynSymPtr.takeError(), "when locating .dynsym section contents"); diff --git a/lib/XRay/InstrumentationMap.cpp b/lib/XRay/InstrumentationMap.cpp index 9202e2870bd..e6534e5a7be 100644 --- a/lib/XRay/InstrumentationMap.cpp +++ b/lib/XRay/InstrumentationMap.cpp @@ -95,16 +95,16 @@ loadObj(StringRef Filename, object::OwningBinary &ObjFile, if (ObjFile.getBinary()->isELF()) { uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) { if (const auto *ELFObj = dyn_cast(ObjFile)) - return ELFObj->getELFFile()->getRelativeRelocationType(); + return ELFObj->getELFFile().getRelativeRelocationType(); else if (const auto *ELFObj = dyn_cast(ObjFile)) - return ELFObj->getELFFile()->getRelativeRelocationType(); + return ELFObj->getELFFile().getRelativeRelocationType(); else if (const auto *ELFObj = dyn_cast(ObjFile)) - return ELFObj->getELFFile()->getRelativeRelocationType(); + return ELFObj->getELFFile().getRelativeRelocationType(); else if (const auto *ELFObj = dyn_cast(ObjFile)) - return ELFObj->getELFFile()->getRelativeRelocationType(); + return ELFObj->getELFFile().getRelativeRelocationType(); else return static_cast(0); }(ObjFile.getBinary()); diff --git a/tools/llvm-gsymutil/llvm-gsymutil.cpp b/tools/llvm-gsymutil/llvm-gsymutil.cpp index 2d75473a3fd..b1dd5482938 100644 --- a/tools/llvm-gsymutil/llvm-gsymutil.cpp +++ b/tools/llvm-gsymutil/llvm-gsymutil.cpp @@ -207,8 +207,8 @@ static bool filterArch(MachOObjectFile &Obj) { /// \returns A valid image base address if we are able to extract one. template static llvm::Optional -getImageBaseAddress(const object::ELFFile *ELFFile) { - auto PhdrRangeOrErr = ELFFile->program_headers(); +getImageBaseAddress(const object::ELFFile &ELFFile) { + auto PhdrRangeOrErr = ELFFile.program_headers(); if (!PhdrRangeOrErr) { consumeError(PhdrRangeOrErr.takeError()); return llvm::None; diff --git a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index 16984ad266e..c53a34bc46a 100644 --- a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -192,13 +192,13 @@ findBuildID(const CopyConfig &Config, const object::ELFFile &In) { static Expected> findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) { if (auto *O = dyn_cast>(&In)) - return findBuildID(Config, *O->getELFFile()); + return findBuildID(Config, O->getELFFile()); else if (auto *O = dyn_cast>(&In)) - return findBuildID(Config, *O->getELFFile()); + return findBuildID(Config, O->getELFFile()); else if (auto *O = dyn_cast>(&In)) - return findBuildID(Config, *O->getELFFile()); + return findBuildID(Config, O->getELFFile()); else if (auto *O = dyn_cast>(&In)) - return findBuildID(Config, *O->getELFFile()); + return findBuildID(Config, O->getELFFile()); llvm_unreachable("Bad file format"); } diff --git a/tools/llvm-objcopy/ELF/Object.h b/tools/llvm-objcopy/ELF/Object.h index 5bd15890e5f..0205c2d4f39 100644 --- a/tools/llvm-objcopy/ELF/Object.h +++ b/tools/llvm-objcopy/ELF/Object.h @@ -957,7 +957,7 @@ private: public: ELFBuilder(const ELFObjectFile &ElfObj, Object &Obj, Optional ExtractPartition) - : ElfFile(*ElfObj.getELFFile()), Obj(Obj), + : ElfFile(ElfObj.getELFFile()), Obj(Obj), ExtractPartition(ExtractPartition) {} Error build(bool EnsureSymtab); diff --git a/tools/llvm-objdump/ELFDump.cpp b/tools/llvm-objdump/ELFDump.cpp index c7a84385ffd..031edb37f5c 100644 --- a/tools/llvm-objdump/ELFDump.cpp +++ b/tools/llvm-objdump/ELFDump.cpp @@ -25,14 +25,14 @@ using namespace llvm::object; using namespace llvm::objdump; template -static Expected getDynamicStrTab(const ELFFile *Elf) { - auto DynamicEntriesOrError = Elf->dynamicEntries(); +static Expected getDynamicStrTab(const ELFFile &Elf) { + auto DynamicEntriesOrError = Elf.dynamicEntries(); if (!DynamicEntriesOrError) return DynamicEntriesOrError.takeError(); for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) { if (Dyn.d_tag == ELF::DT_STRTAB) { - auto MappedAddrOrError = Elf->toMappedAddr(Dyn.getPtr()); + auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr()); if (!MappedAddrOrError) consumeError(MappedAddrOrError.takeError()); return StringRef(reinterpret_cast(*MappedAddrOrError)); @@ -40,13 +40,13 @@ static Expected getDynamicStrTab(const ELFFile *Elf) { } // If the dynamic segment is not present, we fall back on the sections. - auto SectionsOrError = Elf->sections(); + auto SectionsOrError = Elf.sections(); if (!SectionsOrError) return SectionsOrError.takeError(); for (const typename ELFT::Shdr &Sec : *SectionsOrError) { if (Sec.sh_type == ELF::SHT_DYNSYM) - return Elf->getStringTableForSymtab(Sec); + return Elf.getStringTableForSymtab(Sec); } return createError("dynamic string table not found"); @@ -56,7 +56,7 @@ template static Error getRelocationValueString(const ELFObjectFile *Obj, const RelocationRef &RelRef, SmallVectorImpl &Result) { - const ELFFile &EF = *Obj->getELFFile(); + const ELFFile &EF = Obj->getELFFile(); DataRefImpl Rel = RelRef.getRawDataRefImpl(); auto SecOrErr = EF.getSection(Rel.d.a); if (!SecOrErr) @@ -133,9 +133,9 @@ Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj, } template -static uint64_t getSectionLMA(const ELFFile *Obj, +static uint64_t getSectionLMA(const ELFFile &Obj, const object::ELFSectionRef &Sec) { - auto PhdrRangeOrErr = Obj->program_headers(); + auto PhdrRangeOrErr = Obj.program_headers(); if (!PhdrRangeOrErr) report_fatal_error(toString(PhdrRangeOrErr.takeError())); @@ -162,14 +162,14 @@ uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) { } template -static void printDynamicSection(const ELFFile *Elf, StringRef Filename) { +static void printDynamicSection(const ELFFile &Elf, StringRef Filename) { ArrayRef DynamicEntries = - unwrapOrError(Elf->dynamicEntries(), Filename); + unwrapOrError(Elf.dynamicEntries(), Filename); // Find the maximum tag name length to format the value column properly. size_t MaxLen = 0; for (const typename ELFT::Dyn &Dyn : DynamicEntries) - MaxLen = std::max(MaxLen, Elf->getDynamicTagAsString(Dyn.d_tag).size()); + MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size()); std::string TagFmt = " %-" + std::to_string(MaxLen) + "s "; outs() << "Dynamic Section:\n"; @@ -177,7 +177,7 @@ static void printDynamicSection(const ELFFile *Elf, StringRef Filename) { if (Dyn.d_tag == ELF::DT_NULL) continue; - std::string Str = Elf->getDynamicTagAsString(Dyn.d_tag); + std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag); outs() << format(TagFmt.c_str(), Str.c_str()); const char *Fmt = @@ -199,9 +199,9 @@ static void printDynamicSection(const ELFFile *Elf, StringRef Filename) { } template -static void printProgramHeaders(const ELFFile *Obj, StringRef FileName) { +static void printProgramHeaders(const ELFFile &Obj, StringRef FileName) { outs() << "Program Header:\n"; - auto ProgramHeaderOrError = Obj->program_headers(); + auto ProgramHeaderOrError = Obj.program_headers(); if (!ProgramHeaderOrError) { reportWarning("unable to read program headers: " + toString(ProgramHeaderOrError.takeError()), @@ -328,20 +328,20 @@ static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr, } template -static void printSymbolVersionInfo(const ELFFile *Elf, +static void printSymbolVersionInfo(const ELFFile &Elf, StringRef FileName) { ArrayRef Sections = - unwrapOrError(Elf->sections(), FileName); + unwrapOrError(Elf.sections(), FileName); for (const typename ELFT::Shdr &Shdr : Sections) { if (Shdr.sh_type != ELF::SHT_GNU_verneed && Shdr.sh_type != ELF::SHT_GNU_verdef) continue; ArrayRef Contents = - unwrapOrError(Elf->getSectionContents(Shdr), FileName); + unwrapOrError(Elf.getSectionContents(Shdr), FileName); const typename ELFT::Shdr *StrTabSec = - unwrapOrError(Elf->getSection(Shdr.sh_link), FileName); - StringRef StrTab = unwrapOrError(Elf->getStringTable(*StrTabSec), FileName); + unwrapOrError(Elf.getSection(Shdr.sh_link), FileName); + StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName); if (Shdr.sh_type == ELF::SHT_GNU_verneed) printSymbolVersionDependency(Contents, StrTab); diff --git a/tools/llvm-readobj/DwarfCFIEHPrinter.h b/tools/llvm-readobj/DwarfCFIEHPrinter.h index d57f82185c6..2dfe21684a6 100644 --- a/tools/llvm-readobj/DwarfCFIEHPrinter.h +++ b/tools/llvm-readobj/DwarfCFIEHPrinter.h @@ -49,7 +49,7 @@ template static const typename ELFT::Shdr * findSectionByAddress(const object::ELFObjectFile &ObjF, uint64_t Addr) { Expected SectionsOrErr = - ObjF.getELFFile()->sections(); + ObjF.getELFFile().sections(); if (!SectionsOrErr) reportError(SectionsOrErr.takeError(), ObjF.getFileName()); @@ -61,7 +61,7 @@ findSectionByAddress(const object::ELFObjectFile &ObjF, uint64_t Addr) { template void PrinterContext::printUnwindInformation() const { - const object::ELFFile &Obj = *ObjF.getELFFile(); + const object::ELFFile &Obj = ObjF.getELFFile(); Expected PhdrsOrErr = Obj.program_headers(); if (!PhdrsOrErr) @@ -100,7 +100,7 @@ void PrinterContext::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 &Obj = *ObjF.getELFFile(); + const object::ELFFile &Obj = ObjF.getELFFile(); if (const Elf_Shdr *EHFrameHdr = findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) { Expected NameOrErr = Obj.getSectionName(*EHFrameHdr); @@ -180,7 +180,7 @@ void PrinterContext::printEHFrame(const Elf_Shdr *EHFrameShdr) const { W.indent(); Expected> DataOrErr = - ObjF.getELFFile()->getSectionContents(*EHFrameShdr); + ObjF.getELFFile().getSectionContents(*EHFrameShdr); if (!DataOrErr) reportError(DataOrErr.takeError(), ObjF.getFileName()); diff --git a/tools/llvm-readobj/ELFDumper.cpp b/tools/llvm-readobj/ELFDumper.cpp index e682581684e..75caf4b150b 100644 --- a/tools/llvm-readobj/ELFDumper.cpp +++ b/tools/llvm-readobj/ELFDumper.cpp @@ -751,7 +751,7 @@ public: TYPEDEF_ELF_TYPES(ELFT) DumpStyle(const ELFDumper &Dumper) - : Obj(*Dumper.getElfObject().getELFFile()), ElfObj(Dumper.getElfObject()), + : Obj(Dumper.getElfObject().getELFFile()), ElfObj(Dumper.getElfObject()), Dumper(Dumper) { FileName = ElfObj.getFileName(); } @@ -2012,7 +2012,7 @@ void ELFDumper::loadDynamicTable() { template ELFDumper::ELFDumper(const object::ELFObjectFile &O, ScopedPrinter &Writer) - : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(*O.getELFFile()), + : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(O.getELFFile()), DynRelRegion(O, *this), DynRelaRegion(O, *this), DynRelrRegion(O, *this), DynPLTRelRegion(O, *this), DynamicTable(O, *this) { if (opts::Output == opts::GNU) @@ -2695,7 +2695,7 @@ template static Error checkHashTable(const ELFDumper &Dumper, const typename ELFT::Hash *H, bool *IsHeaderValid = nullptr) { - const ELFFile &Obj = *Dumper.getElfObject().getELFFile(); + const ELFFile &Obj = Dumper.getElfObject().getELFFile(); const uint64_t SecOffset = (const uint8_t *)H - Obj.base(); if (Dumper.getHashTableEntSize() == 8) { auto It = llvm::find_if(ElfMachineType, [&](const EnumEntry &E) { @@ -2986,7 +2986,7 @@ private: 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()) {} @@ -4578,7 +4578,7 @@ RelSymbol getSymbolForReloc(const ELFDumper &Dumper, "index is greater than or equal to the number of dynamic symbols (" + Twine(Symbols.size()) + ")"); - const ELFFile &Obj = *Dumper.getElfObject().getELFFile(); + const ELFFile &Obj = Dumper.getElfObject().getELFFile(); const uint64_t FileSize = Obj.getBufSize(); const uint64_t SymOffset = ((const uint8_t *)FirstSym - Obj.base()) + (uint64_t)Reloc.Symbol * sizeof(Elf_Sym); @@ -5523,7 +5523,7 @@ static void printNotesHelper( StartNotesFn, llvm::function_ref ProcessNoteFn, llvm::function_ref FinishNotesFn) { - const ELFFile &Obj = *Dumper.getElfObject().getELFFile(); + const ELFFile &Obj = Dumper.getElfObject().getELFFile(); ArrayRef Sections = cantFail(Obj.sections()); if (Obj.getHeader().e_type != ELF::ET_CORE && !Sections.empty()) { @@ -6205,7 +6205,7 @@ getMipsAbiFlagsSection(const ELFDumper &Dumper) { constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: "; Expected> DataOrErr = - Dumper.getElfObject().getELFFile()->getSectionContents(*Sec); + Dumper.getElfObject().getELFFile().getSectionContents(*Sec); if (!DataOrErr) return createError(ErrPrefix + toString(DataOrErr.takeError())); diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index 0a85179a7b9..405e8b17d2f 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -1566,16 +1566,16 @@ static Error elf2yaml(raw_ostream &Out, const object::ELFFile &Obj, Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { std::unique_ptr DWARFCtx = DWARFContext::create(Obj); if (const auto *ELFObj = dyn_cast(&Obj)) - return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); + return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx)); if (const auto *ELFObj = dyn_cast(&Obj)) - return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); + return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx)); if (const auto *ELFObj = dyn_cast(&Obj)) - return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); + return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx)); if (const auto *ELFObj = dyn_cast(&Obj)) - return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx)); + return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx)); llvm_unreachable("unknown ELF file format"); }