1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[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
This commit is contained in:
Georgii Rymar 2020-12-03 12:57:00 +03:00
parent 2f154fa157
commit 437027774a
12 changed files with 57 additions and 60 deletions

View File

@ -441,7 +441,7 @@ public:
unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
const ELFFile<ELFT> *getELFFile() const { return &EF; }
const ELFFile<ELFT> &getELFFile() const { return EF; }
bool isDyldType() const { return isDyldELFObject; }
static bool classof(const Binary *v) {

View File

@ -287,10 +287,8 @@ bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
}
template <typename ELFT>
Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> *Obj) {
if (!Obj)
return {};
auto PhdrsOrErr = Obj->program_headers();
Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> &Obj) {
auto PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr) {
consumeError(PhdrsOrErr.takeError());
return {};
@ -299,7 +297,7 @@ Optional<ArrayRef<uint8_t>> getBuildID(const ELFFile<ELFT> *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));

View File

@ -626,7 +626,7 @@ private:
auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj);
std::string fileName(ELFObj->get()->getFileName());
return ELFLinkGraphBuilder_x86_64(std::move(fileName),
*ELFObjFile.getELFFile())
ELFObjFile.getELFFile())
.buildGraph();
}

View File

@ -585,15 +585,15 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
using Elf_Sym_Range = typename ELFT::SymRange;
using Elf_Sym = typename ELFT::Sym;
std::unique_ptr<ELFStub> DestStub = std::make_unique<ELFStub>();
const ELFFile<ELFT> *ElfFile = ElfObj.getELFFile();
const ELFFile<ELFT> &ElfFile = ElfObj.getELFFile();
// Fetch .dynamic table.
Expected<Elf_Dyn_Range> DynTable = ElfFile->dynamicEntries();
Expected<Elf_Dyn_Range> DynTable = ElfFile.dynamicEntries();
if (!DynTable) {
return DynTable.takeError();
}
// Fetch program headers.
Expected<Elf_Phdr_Range> PHdrs = ElfFile->program_headers();
Expected<Elf_Phdr_Range> PHdrs = ElfFile.program_headers();
if (!PHdrs) {
return PHdrs.takeError();
}
@ -604,8 +604,7 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
return std::move(Err);
// Get pointer to in-memory location of .dynstr section.
Expected<const uint8_t *> DynStrPtr =
ElfFile->toMappedAddr(DynEnt.StrTabAddr);
Expected<const uint8_t *> DynStrPtr = ElfFile.toMappedAddr(DynEnt.StrTabAddr);
if (!DynStrPtr)
return appendToError(DynStrPtr.takeError(),
"when locating .dynstr section contents");
@ -614,7 +613,7 @@ buildStub(const ELFObjectFile<ELFT> &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<ELFT> &ElfObj) {
}
// Populate Symbols from .dynsym table and dynamic string table.
Expected<uint64_t> SymCount = getNumSyms(DynEnt, *ElfFile);
Expected<uint64_t> SymCount = getNumSyms(DynEnt, ElfFile);
if (!SymCount)
return SymCount.takeError();
if (*SymCount > 0) {
// Get pointer to in-memory location of .dynsym section.
Expected<const uint8_t *> DynSymPtr =
ElfFile->toMappedAddr(DynEnt.DynSymAddr);
ElfFile.toMappedAddr(DynEnt.DynSymAddr);
if (!DynSymPtr)
return appendToError(DynSymPtr.takeError(),
"when locating .dynsym section contents");

View File

@ -95,16 +95,16 @@ loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
if (ObjFile.getBinary()->isELF()) {
uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
return ELFObj->getELFFile()->getRelativeRelocationType();
return ELFObj->getELFFile().getRelativeRelocationType();
else if (const auto *ELFObj =
dyn_cast<object::ELF32BEObjectFile>(ObjFile))
return ELFObj->getELFFile()->getRelativeRelocationType();
return ELFObj->getELFFile().getRelativeRelocationType();
else if (const auto *ELFObj =
dyn_cast<object::ELF64LEObjectFile>(ObjFile))
return ELFObj->getELFFile()->getRelativeRelocationType();
return ELFObj->getELFFile().getRelativeRelocationType();
else if (const auto *ELFObj =
dyn_cast<object::ELF64BEObjectFile>(ObjFile))
return ELFObj->getELFFile()->getRelativeRelocationType();
return ELFObj->getELFFile().getRelativeRelocationType();
else
return static_cast<uint32_t>(0);
}(ObjFile.getBinary());

View File

@ -207,8 +207,8 @@ static bool filterArch(MachOObjectFile &Obj) {
/// \returns A valid image base address if we are able to extract one.
template <class ELFT>
static llvm::Optional<uint64_t>
getImageBaseAddress(const object::ELFFile<ELFT> *ELFFile) {
auto PhdrRangeOrErr = ELFFile->program_headers();
getImageBaseAddress(const object::ELFFile<ELFT> &ELFFile) {
auto PhdrRangeOrErr = ELFFile.program_headers();
if (!PhdrRangeOrErr) {
consumeError(PhdrRangeOrErr.takeError());
return llvm::None;

View File

@ -192,13 +192,13 @@ findBuildID(const CopyConfig &Config, const object::ELFFile<ELFT> &In) {
static Expected<ArrayRef<uint8_t>>
findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) {
if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
return findBuildID(Config, *O->getELFFile());
return findBuildID(Config, O->getELFFile());
else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
return findBuildID(Config, *O->getELFFile());
return findBuildID(Config, O->getELFFile());
else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
return findBuildID(Config, *O->getELFFile());
return findBuildID(Config, O->getELFFile());
else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
return findBuildID(Config, *O->getELFFile());
return findBuildID(Config, O->getELFFile());
llvm_unreachable("Bad file format");
}

View File

@ -957,7 +957,7 @@ private:
public:
ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
Optional<StringRef> ExtractPartition)
: ElfFile(*ElfObj.getELFFile()), Obj(Obj),
: ElfFile(ElfObj.getELFFile()), Obj(Obj),
ExtractPartition(ExtractPartition) {}
Error build(bool EnsureSymtab);

View File

@ -25,14 +25,14 @@ using namespace llvm::object;
using namespace llvm::objdump;
template <class ELFT>
static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
auto DynamicEntriesOrError = Elf->dynamicEntries();
static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &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<const char *>(*MappedAddrOrError));
@ -40,13 +40,13 @@ static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *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 <class ELFT>
static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
const RelocationRef &RelRef,
SmallVectorImpl<char> &Result) {
const ELFFile<ELFT> &EF = *Obj->getELFFile();
const ELFFile<ELFT> &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 <class ELFT>
static uint64_t getSectionLMA(const ELFFile<ELFT> *Obj,
static uint64_t getSectionLMA(const ELFFile<ELFT> &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 <class ELFT>
static void printDynamicSection(const ELFFile<ELFT> *Elf, StringRef Filename) {
static void printDynamicSection(const ELFFile<ELFT> &Elf, StringRef Filename) {
ArrayRef<typename ELFT::Dyn> 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<ELFT> *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<ELFT> *Elf, StringRef Filename) {
}
template <class ELFT>
static void printProgramHeaders(const ELFFile<ELFT> *Obj, StringRef FileName) {
static void printProgramHeaders(const ELFFile<ELFT> &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 <class ELFT>
static void printSymbolVersionInfo(const ELFFile<ELFT> *Elf,
static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf,
StringRef FileName) {
ArrayRef<typename ELFT::Shdr> 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<uint8_t> 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<ELFT>(Contents, StrTab);

View File

@ -49,7 +49,7 @@ template <class ELFT>
static const typename ELFT::Shdr *
findSectionByAddress(const object::ELFObjectFile<ELFT> &ObjF, uint64_t Addr) {
Expected<typename ELFT::ShdrRange> SectionsOrErr =
ObjF.getELFFile()->sections();
ObjF.getELFFile().sections();
if (!SectionsOrErr)
reportError(SectionsOrErr.takeError(), ObjF.getFileName());
@ -61,7 +61,7 @@ findSectionByAddress(const object::ELFObjectFile<ELFT> &ObjF, uint64_t Addr) {
template <typename ELFT>
void PrinterContext<ELFT>::printUnwindInformation() const {
const object::ELFFile<ELFT> &Obj = *ObjF.getELFFile();
const object::ELFFile<ELFT> &Obj = ObjF.getELFFile();
Expected<typename ELFT::PhdrRange> PhdrsOrErr = Obj.program_headers();
if (!PhdrsOrErr)
@ -100,7 +100,7 @@ void PrinterContext<ELFT>::printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const {
W.startLine() << format("Offset: 0x%" PRIx64 "\n", (uint64_t)EHFramePHdr->p_offset);
W.startLine() << format("Size: 0x%" PRIx64 "\n", (uint64_t)EHFramePHdr->p_memsz);
const object::ELFFile<ELFT> &Obj = *ObjF.getELFFile();
const object::ELFFile<ELFT> &Obj = ObjF.getELFFile();
if (const Elf_Shdr *EHFrameHdr =
findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) {
Expected<StringRef> NameOrErr = Obj.getSectionName(*EHFrameHdr);
@ -180,7 +180,7 @@ void PrinterContext<ELFT>::printEHFrame(const Elf_Shdr *EHFrameShdr) const {
W.indent();
Expected<ArrayRef<uint8_t>> DataOrErr =
ObjF.getELFFile()->getSectionContents(*EHFrameShdr);
ObjF.getELFFile().getSectionContents(*EHFrameShdr);
if (!DataOrErr)
reportError(DataOrErr.takeError(), ObjF.getFileName());

View File

@ -751,7 +751,7 @@ public:
TYPEDEF_ELF_TYPES(ELFT)
DumpStyle(const ELFDumper<ELFT> &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<ELFT>::loadDynamicTable() {
template <typename ELFT>
ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> &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 <class ELFT>
static Error checkHashTable(const ELFDumper<ELFT> &Dumper,
const typename ELFT::Hash *H,
bool *IsHeaderValid = nullptr) {
const ELFFile<ELFT> &Obj = *Dumper.getElfObject().getELFFile();
const ELFFile<ELFT> &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<unsigned> &E) {
@ -2986,7 +2986,7 @@ private:
template <class ELFT>
MipsGOTParser<ELFT>::MipsGOTParser(const ELFDumper<ELFT> &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<ELFT> getSymbolForReloc(const ELFDumper<ELFT> &Dumper,
"index is greater than or equal to the number of dynamic symbols (" +
Twine(Symbols.size()) + ")");
const ELFFile<ELFT> &Obj = *Dumper.getElfObject().getELFFile();
const ELFFile<ELFT> &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<void(const typename ELFT::Note &)> ProcessNoteFn,
llvm::function_ref<void()> FinishNotesFn) {
const ELFFile<ELFT> &Obj = *Dumper.getElfObject().getELFFile();
const ELFFile<ELFT> &Obj = Dumper.getElfObject().getELFFile();
ArrayRef<typename ELFT::Shdr> Sections = cantFail(Obj.sections());
if (Obj.getHeader().e_type != ELF::ET_CORE && !Sections.empty()) {
@ -6205,7 +6205,7 @@ getMipsAbiFlagsSection(const ELFDumper<ELFT> &Dumper) {
constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: ";
Expected<ArrayRef<uint8_t>> DataOrErr =
Dumper.getElfObject().getELFFile()->getSectionContents(*Sec);
Dumper.getElfObject().getELFFile().getSectionContents(*Sec);
if (!DataOrErr)
return createError(ErrPrefix + toString(DataOrErr.takeError()));

View File

@ -1566,16 +1566,16 @@ static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj,
Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx));
return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx));
if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx));
return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx));
if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx));
return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx));
if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
return elf2yaml(Out, *ELFObj->getELFFile(), std::move(DWARFCtx));
return elf2yaml(Out, ELFObj->getELFFile(), std::move(DWARFCtx));
llvm_unreachable("unknown ELF file format");
}