1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

[lib/Object] - Refine interface of ELFFile<ELFT>. NFCI.

`ELFFile<ELFT>` has many methods that take pointers,
though they assume that arguments are never null and
hence could take references instead.

This patch performs such clean-up.

Differential revision: https://reviews.llvm.org/D87385
This commit is contained in:
Georgii Rymar 2020-09-09 17:03:53 +03:00
parent afed5569ad
commit dfa9fe5b23
11 changed files with 349 additions and 350 deletions

View File

@ -58,11 +58,11 @@ enum PPCInstrMasks : uint64_t {
template <class ELFT> class ELFFile; template <class ELFT> class ELFFile;
template <class ELFT> template <class ELFT>
std::string getSecIndexForError(const ELFFile<ELFT> *Obj, std::string getSecIndexForError(const ELFFile<ELFT> &Obj,
const typename ELFT::Shdr *Sec) { const typename ELFT::Shdr &Sec) {
auto TableOrErr = Obj->sections(); auto TableOrErr = Obj.sections();
if (TableOrErr) if (TableOrErr)
return "[index " + std::to_string(Sec - &TableOrErr->front()) + "]"; return "[index " + std::to_string(&Sec - &TableOrErr->front()) + "]";
// To make this helper be more convenient for error reporting purposes we // To make this helper be more convenient for error reporting purposes we
// drop the error. But really it should never be triggered. Before this point, // drop the error. But really it should never be triggered. Before this point,
// our code should have called 'sections()' and reported a proper error on // our code should have called 'sections()' and reported a proper error on
@ -72,11 +72,11 @@ std::string getSecIndexForError(const ELFFile<ELFT> *Obj,
} }
template <class ELFT> template <class ELFT>
std::string getPhdrIndexForError(const ELFFile<ELFT> *Obj, std::string getPhdrIndexForError(const ELFFile<ELFT> &Obj,
const typename ELFT::Phdr *Phdr) { const typename ELFT::Phdr &Phdr) {
auto Headers = Obj->program_headers(); auto Headers = Obj.program_headers();
if (Headers) if (Headers)
return ("[index " + Twine(Phdr - &Headers->front()) + "]").str(); return ("[index " + Twine(&Phdr - &Headers->front()) + "]").str();
// See comment in the getSecIndexForError() above. // See comment in the getSecIndexForError() above.
llvm::consumeError(Headers.takeError()); llvm::consumeError(Headers.takeError());
return "[unknown index]"; return "[unknown index]";
@ -134,17 +134,17 @@ private:
ELFFile(StringRef Object); ELFFile(StringRef Object);
public: public:
const Elf_Ehdr *getHeader() const { const Elf_Ehdr &getHeader() const {
return reinterpret_cast<const Elf_Ehdr *>(base()); return *reinterpret_cast<const Elf_Ehdr *>(base());
} }
template <typename T> template <typename T>
Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const; Expected<const T *> getEntry(uint32_t Section, uint32_t Entry) const;
template <typename T> template <typename T>
Expected<const T *> getEntry(const Elf_Shdr *Section, uint32_t Entry) const; Expected<const T *> getEntry(const Elf_Shdr &Section, uint32_t Entry) const;
Expected<StringRef> Expected<StringRef>
getStringTable(const Elf_Shdr *Section, getStringTable(const Elf_Shdr &Section,
WarningHandler WarnHandler = &defaultWarningHandler) const; WarningHandler WarnHandler = &defaultWarningHandler) const;
Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const; Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section, Expected<StringRef> getStringTableForSymtab(const Elf_Shdr &Section,
@ -163,18 +163,18 @@ public:
std::string getDynamicTagAsString(uint64_t Type) const; std::string getDynamicTagAsString(uint64_t Type) const;
/// Get the symbol for a given relocation. /// Get the symbol for a given relocation.
Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel, Expected<const Elf_Sym *> getRelocationSymbol(const Elf_Rel &Rel,
const Elf_Shdr *SymTab) const; const Elf_Shdr *SymTab) const;
static Expected<ELFFile> create(StringRef Object); static Expected<ELFFile> create(StringRef Object);
bool isLE() const { bool isLE() const {
return getHeader()->getDataEncoding() == ELF::ELFDATA2LSB; return getHeader().getDataEncoding() == ELF::ELFDATA2LSB;
} }
bool isMipsELF64() const { bool isMipsELF64() const {
return getHeader()->e_machine == ELF::EM_MIPS && return getHeader().e_machine == ELF::EM_MIPS &&
getHeader()->getFileClass() == ELF::ELFCLASS64; getHeader().getFileClass() == ELF::ELFCLASS64;
} }
bool isMips64EL() const { return isMipsELF64() && isLE(); } bool isMips64EL() const { return isMipsELF64() && isLE(); }
@ -188,43 +188,43 @@ public:
Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const { Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
if (!Sec) if (!Sec)
return makeArrayRef<Elf_Sym>(nullptr, nullptr); return makeArrayRef<Elf_Sym>(nullptr, nullptr);
return getSectionContentsAsArray<Elf_Sym>(Sec); return getSectionContentsAsArray<Elf_Sym>(*Sec);
} }
Expected<Elf_Rela_Range> relas(const Elf_Shdr *Sec) const { Expected<Elf_Rela_Range> relas(const Elf_Shdr &Sec) const {
return getSectionContentsAsArray<Elf_Rela>(Sec); return getSectionContentsAsArray<Elf_Rela>(Sec);
} }
Expected<Elf_Rel_Range> rels(const Elf_Shdr *Sec) const { Expected<Elf_Rel_Range> rels(const Elf_Shdr &Sec) const {
return getSectionContentsAsArray<Elf_Rel>(Sec); return getSectionContentsAsArray<Elf_Rel>(Sec);
} }
Expected<Elf_Relr_Range> relrs(const Elf_Shdr *Sec) const { Expected<Elf_Relr_Range> relrs(const Elf_Shdr &Sec) const {
return getSectionContentsAsArray<Elf_Relr>(Sec); return getSectionContentsAsArray<Elf_Relr>(Sec);
} }
std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const; std::vector<Elf_Rel> decode_relrs(Elf_Relr_Range relrs) const;
Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr *Sec) const; Expected<std::vector<Elf_Rela>> android_relas(const Elf_Shdr &Sec) const;
/// Iterate over program header table. /// Iterate over program header table.
Expected<Elf_Phdr_Range> program_headers() const { Expected<Elf_Phdr_Range> program_headers() const {
if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr)) if (getHeader().e_phnum && getHeader().e_phentsize != sizeof(Elf_Phdr))
return createError("invalid e_phentsize: " + return createError("invalid e_phentsize: " +
Twine(getHeader()->e_phentsize)); Twine(getHeader().e_phentsize));
uint64_t HeadersSize = uint64_t HeadersSize =
(uint64_t)getHeader()->e_phnum * getHeader()->e_phentsize; (uint64_t)getHeader().e_phnum * getHeader().e_phentsize;
uint64_t PhOff = getHeader()->e_phoff; uint64_t PhOff = getHeader().e_phoff;
if (PhOff + HeadersSize < PhOff || PhOff + HeadersSize > getBufSize()) if (PhOff + HeadersSize < PhOff || PhOff + HeadersSize > getBufSize())
return createError("program headers are longer than binary of size " + return createError("program headers are longer than binary of size " +
Twine(getBufSize()) + ": e_phoff = 0x" + Twine(getBufSize()) + ": e_phoff = 0x" +
Twine::utohexstr(getHeader()->e_phoff) + Twine::utohexstr(getHeader().e_phoff) +
", e_phnum = " + Twine(getHeader()->e_phnum) + ", e_phnum = " + Twine(getHeader().e_phnum) +
", e_phentsize = " + Twine(getHeader()->e_phentsize)); ", e_phentsize = " + Twine(getHeader().e_phentsize));
auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + PhOff); auto *Begin = reinterpret_cast<const Elf_Phdr *>(base() + PhOff);
return makeArrayRef(Begin, Begin + getHeader()->e_phnum); return makeArrayRef(Begin, Begin + getHeader().e_phnum);
} }
/// Get an iterator over notes in a program header. /// Get an iterator over notes in a program header.
@ -257,7 +257,7 @@ public:
assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE"); assert(Shdr.sh_type == ELF::SHT_NOTE && "Shdr is not of type SHT_NOTE");
ErrorAsOutParameter ErrAsOutParam(&Err); ErrorAsOutParameter ErrAsOutParam(&Err);
if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) { if (Shdr.sh_offset + Shdr.sh_size > getBufSize()) {
Err = createError("SHT_NOTE section " + getSecIndexForError(this, &Shdr) + Err = createError("SHT_NOTE section " + getSecIndexForError(*this, Shdr) +
" has invalid offset (0x" + " has invalid offset (0x" +
Twine::utohexstr(Shdr.sh_offset) + ") or size (0x" + Twine::utohexstr(Shdr.sh_offset) + ") or size (0x" +
Twine::utohexstr(Shdr.sh_size) + ")"); Twine::utohexstr(Shdr.sh_size) + ")");
@ -298,12 +298,12 @@ public:
Expected<StringRef> getSectionStringTable( Expected<StringRef> getSectionStringTable(
Elf_Shdr_Range Sections, Elf_Shdr_Range Sections,
WarningHandler WarnHandler = &defaultWarningHandler) const; WarningHandler WarnHandler = &defaultWarningHandler) const;
Expected<uint32_t> getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, Expected<uint32_t> getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
ArrayRef<Elf_Word> ShndxTable) const; ArrayRef<Elf_Word> ShndxTable) const;
Expected<const Elf_Shdr *> getSection(const Elf_Sym *Sym, Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
const Elf_Shdr *SymTab, const Elf_Shdr *SymTab,
ArrayRef<Elf_Word> ShndxTable) const; ArrayRef<Elf_Word> ShndxTable) const;
Expected<const Elf_Shdr *> getSection(const Elf_Sym *Sym, Expected<const Elf_Shdr *> getSection(const Elf_Sym &Sym,
Elf_Sym_Range Symtab, Elf_Sym_Range Symtab,
ArrayRef<Elf_Word> ShndxTable) const; ArrayRef<Elf_Word> ShndxTable) const;
Expected<const Elf_Shdr *> getSection(uint32_t Index) const; Expected<const Elf_Shdr *> getSection(uint32_t Index) const;
@ -312,14 +312,14 @@ public:
uint32_t Index) const; uint32_t Index) const;
Expected<StringRef> Expected<StringRef>
getSectionName(const Elf_Shdr *Section, getSectionName(const Elf_Shdr &Section,
WarningHandler WarnHandler = &defaultWarningHandler) const; WarningHandler WarnHandler = &defaultWarningHandler) const;
Expected<StringRef> getSectionName(const Elf_Shdr *Section, Expected<StringRef> getSectionName(const Elf_Shdr &Section,
StringRef DotShstrtab) const; StringRef DotShstrtab) const;
template <typename T> template <typename T>
Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr *Sec) const; Expected<ArrayRef<T>> getSectionContentsAsArray(const Elf_Shdr &Sec) const;
Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr *Sec) const; Expected<ArrayRef<uint8_t>> getSectionContents(const Elf_Shdr &Sec) const;
Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr *Phdr) const; Expected<ArrayRef<uint8_t>> getSegmentContents(const Elf_Phdr &Phdr) const;
}; };
using ELF32LEFile = ELFFile<ELF32LE>; using ELF32LEFile = ELFFile<ELF32LE>;
@ -337,11 +337,11 @@ getSection(typename ELFT::ShdrRange Sections, uint32_t Index) {
template <class ELFT> template <class ELFT>
inline Expected<uint32_t> inline Expected<uint32_t>
getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym, getExtendedSymbolTableIndex(const typename ELFT::Sym &Sym,
const typename ELFT::Sym *FirstSym, const typename ELFT::Sym &FirstSym,
ArrayRef<typename ELFT::Word> ShndxTable) { ArrayRef<typename ELFT::Word> ShndxTable) {
assert(Sym->st_shndx == ELF::SHN_XINDEX); assert(Sym.st_shndx == ELF::SHN_XINDEX);
unsigned Index = Sym - FirstSym; unsigned Index = &Sym - &FirstSym;
if (Index >= ShndxTable.size()) if (Index >= ShndxTable.size())
return createError( return createError(
"extended symbol index (" + Twine(Index) + "extended symbol index (" + Twine(Index) +
@ -354,12 +354,12 @@ getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym,
template <class ELFT> template <class ELFT>
Expected<uint32_t> Expected<uint32_t>
ELFFile<ELFT>::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, ELFFile<ELFT>::getSectionIndex(const Elf_Sym &Sym, Elf_Sym_Range Syms,
ArrayRef<Elf_Word> ShndxTable) const { ArrayRef<Elf_Word> ShndxTable) const {
uint32_t Index = Sym->st_shndx; uint32_t Index = Sym.st_shndx;
if (Index == ELF::SHN_XINDEX) { if (Index == ELF::SHN_XINDEX) {
auto ErrorOrIndex = getExtendedSymbolTableIndex<ELFT>( Expected<uint32_t> ErrorOrIndex =
Sym, Syms.begin(), ShndxTable); getExtendedSymbolTableIndex<ELFT>(Sym, *Syms.begin(), ShndxTable);
if (!ErrorOrIndex) if (!ErrorOrIndex)
return ErrorOrIndex.takeError(); return ErrorOrIndex.takeError();
return *ErrorOrIndex; return *ErrorOrIndex;
@ -371,7 +371,7 @@ ELFFile<ELFT>::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms,
template <class ELFT> template <class ELFT>
Expected<const typename ELFT::Shdr *> Expected<const typename ELFT::Shdr *>
ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ELFFile<ELFT>::getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab,
ArrayRef<Elf_Word> ShndxTable) const { ArrayRef<Elf_Word> ShndxTable) const {
auto SymsOrErr = symbols(SymTab); auto SymsOrErr = symbols(SymTab);
if (!SymsOrErr) if (!SymsOrErr)
@ -381,7 +381,7 @@ ELFFile<ELFT>::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
template <class ELFT> template <class ELFT>
Expected<const typename ELFT::Shdr *> Expected<const typename ELFT::Shdr *>
ELFFile<ELFT>::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols, ELFFile<ELFT>::getSection(const Elf_Sym &Sym, Elf_Sym_Range Symbols,
ArrayRef<Elf_Word> ShndxTable) const { ArrayRef<Elf_Word> ShndxTable) const {
auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable); auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
if (!IndexOrErr) if (!IndexOrErr)
@ -402,7 +402,7 @@ ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
Elf_Sym_Range Symbols = *SymsOrErr; Elf_Sym_Range Symbols = *SymsOrErr;
if (Index >= Symbols.size()) if (Index >= Symbols.size())
return createError("unable to get symbol from section " + return createError("unable to get symbol from section " +
getSecIndexForError(this, Sec) + getSecIndexForError(*this, *Sec) +
": invalid symbol index (" + Twine(Index) + ")"); ": invalid symbol index (" + Twine(Index) + ")");
return &Symbols[Index]; return &Symbols[Index];
} }
@ -410,26 +410,26 @@ ELFFile<ELFT>::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const {
template <class ELFT> template <class ELFT>
template <typename T> template <typename T>
Expected<ArrayRef<T>> Expected<ArrayRef<T>>
ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const { ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr &Sec) const {
if (Sec->sh_entsize != sizeof(T) && sizeof(T) != 1) if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1)
return createError("section " + getSecIndexForError(this, Sec) + return createError("section " + getSecIndexForError(*this, Sec) +
" has an invalid sh_entsize: " + Twine(Sec->sh_entsize)); " has an invalid sh_entsize: " + Twine(Sec.sh_entsize));
uintX_t Offset = Sec->sh_offset; uintX_t Offset = Sec.sh_offset;
uintX_t Size = Sec->sh_size; uintX_t Size = Sec.sh_size;
if (Size % sizeof(T)) if (Size % sizeof(T))
return createError("section " + getSecIndexForError(this, Sec) + return createError("section " + getSecIndexForError(*this, Sec) +
" has an invalid sh_size (" + Twine(Size) + " has an invalid sh_size (" + Twine(Size) +
") which is not a multiple of its sh_entsize (" + ") which is not a multiple of its sh_entsize (" +
Twine(Sec->sh_entsize) + ")"); Twine(Sec.sh_entsize) + ")");
if (std::numeric_limits<uintX_t>::max() - Offset < Size) if (std::numeric_limits<uintX_t>::max() - Offset < Size)
return createError("section " + getSecIndexForError(this, Sec) + return createError("section " + getSecIndexForError(*this, Sec) +
" has a sh_offset (0x" + Twine::utohexstr(Offset) + " has a sh_offset (0x" + Twine::utohexstr(Offset) +
") + sh_size (0x" + Twine::utohexstr(Size) + ") + sh_size (0x" + Twine::utohexstr(Size) +
") that cannot be represented"); ") that cannot be represented");
if (Offset + Size > Buf.size()) if (Offset + Size > Buf.size())
return createError("section " + getSecIndexForError(this, Sec) + return createError("section " + getSecIndexForError(*this, Sec) +
" has a sh_offset (0x" + Twine::utohexstr(Offset) + " has a sh_offset (0x" + Twine::utohexstr(Offset) +
") + sh_size (0x" + Twine::utohexstr(Size) + ") + sh_size (0x" + Twine::utohexstr(Size) +
") that is greater than the file size (0x" + ") that is greater than the file size (0x" +
@ -445,17 +445,17 @@ ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
template <class ELFT> template <class ELFT>
Expected<ArrayRef<uint8_t>> Expected<ArrayRef<uint8_t>>
ELFFile<ELFT>::getSegmentContents(const Elf_Phdr *Phdr) const { ELFFile<ELFT>::getSegmentContents(const Elf_Phdr &Phdr) const {
uintX_t Offset = Phdr->p_offset; uintX_t Offset = Phdr.p_offset;
uintX_t Size = Phdr->p_filesz; uintX_t Size = Phdr.p_filesz;
if (std::numeric_limits<uintX_t>::max() - Offset < Size) if (std::numeric_limits<uintX_t>::max() - Offset < Size)
return createError("program header " + getPhdrIndexForError(this, Phdr) + return createError("program header " + getPhdrIndexForError(*this, Phdr) +
" has a p_offset (0x" + Twine::utohexstr(Offset) + " has a p_offset (0x" + Twine::utohexstr(Offset) +
") + p_filesz (0x" + Twine::utohexstr(Size) + ") + p_filesz (0x" + Twine::utohexstr(Size) +
") that cannot be represented"); ") that cannot be represented");
if (Offset + Size > Buf.size()) if (Offset + Size > Buf.size())
return createError("program header " + getPhdrIndexForError(this, Phdr) + return createError("program header " + getPhdrIndexForError(*this, Phdr) +
" has a p_offset (0x" + Twine::utohexstr(Offset) + " has a p_offset (0x" + Twine::utohexstr(Offset) +
") + p_filesz (0x" + Twine::utohexstr(Size) + ") + p_filesz (0x" + Twine::utohexstr(Size) +
") that is greater than the file size (0x" + ") that is greater than the file size (0x" +
@ -465,13 +465,13 @@ ELFFile<ELFT>::getSegmentContents(const Elf_Phdr *Phdr) const {
template <class ELFT> template <class ELFT>
Expected<ArrayRef<uint8_t>> Expected<ArrayRef<uint8_t>>
ELFFile<ELFT>::getSectionContents(const Elf_Shdr *Sec) const { ELFFile<ELFT>::getSectionContents(const Elf_Shdr &Sec) const {
return getSectionContentsAsArray<uint8_t>(Sec); return getSectionContentsAsArray<uint8_t>(Sec);
} }
template <class ELFT> template <class ELFT>
StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const { StringRef ELFFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
return getELFRelocationTypeName(getHeader()->e_machine, Type); return getELFRelocationTypeName(getHeader().e_machine, Type);
} }
template <class ELFT> template <class ELFT>
@ -507,24 +507,24 @@ void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
template <class ELFT> template <class ELFT>
uint32_t ELFFile<ELFT>::getRelativeRelocationType() const { uint32_t ELFFile<ELFT>::getRelativeRelocationType() const {
return getELFRelativeRelocationType(getHeader()->e_machine); return getELFRelativeRelocationType(getHeader().e_machine);
} }
template <class ELFT> template <class ELFT>
Expected<const typename ELFT::Sym *> Expected<const typename ELFT::Sym *>
ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel *Rel, ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel &Rel,
const Elf_Shdr *SymTab) const { const Elf_Shdr *SymTab) const {
uint32_t Index = Rel->getSymbol(isMips64EL()); uint32_t Index = Rel.getSymbol(isMips64EL());
if (Index == 0) if (Index == 0)
return nullptr; return nullptr;
return getEntry<Elf_Sym>(SymTab, Index); return getEntry<Elf_Sym>(*SymTab, Index);
} }
template <class ELFT> template <class ELFT>
Expected<StringRef> Expected<StringRef>
ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections, ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
WarningHandler WarnHandler) const { WarningHandler WarnHandler) const {
uint32_t Index = getHeader()->e_shstrndx; uint32_t Index = getHeader().e_shstrndx;
if (Index == ELF::SHN_XINDEX) { if (Index == ELF::SHN_XINDEX) {
// If the section name string table section index is greater than // If the section name string table section index is greater than
// or equal to SHN_LORESERVE, then the actual index of the section name // or equal to SHN_LORESERVE, then the actual index of the section name
@ -542,7 +542,7 @@ ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
if (Index >= Sections.size()) if (Index >= Sections.size())
return createError("section header string table index " + Twine(Index) + return createError("section header string table index " + Twine(Index) +
" does not exist"); " does not exist");
return getStringTable(&Sections[Index], WarnHandler); return getStringTable(Sections[Index], WarnHandler);
} }
template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {} template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
@ -558,13 +558,13 @@ Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
template <class ELFT> template <class ELFT>
Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const { Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
const uintX_t SectionTableOffset = getHeader()->e_shoff; const uintX_t SectionTableOffset = getHeader().e_shoff;
if (SectionTableOffset == 0) if (SectionTableOffset == 0)
return ArrayRef<Elf_Shdr>(); return ArrayRef<Elf_Shdr>();
if (getHeader()->e_shentsize != sizeof(Elf_Shdr)) if (getHeader().e_shentsize != sizeof(Elf_Shdr))
return createError("invalid e_shentsize in ELF header: " + return createError("invalid e_shentsize in ELF header: " +
Twine(getHeader()->e_shentsize)); Twine(getHeader().e_shentsize));
const uint64_t FileSize = Buf.size(); const uint64_t FileSize = Buf.size();
if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize || if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize ||
@ -581,7 +581,7 @@ Expected<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
const Elf_Shdr *First = const Elf_Shdr *First =
reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset); reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
uintX_t NumSections = getHeader()->e_shnum; uintX_t NumSections = getHeader().e_shnum;
if (NumSections == 0) if (NumSections == 0)
NumSections = First->sh_size; NumSections = First->sh_size;
@ -612,21 +612,21 @@ Expected<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
auto SecOrErr = getSection(Section); auto SecOrErr = getSection(Section);
if (!SecOrErr) if (!SecOrErr)
return SecOrErr.takeError(); return SecOrErr.takeError();
return getEntry<T>(*SecOrErr, Entry); return getEntry<T>(**SecOrErr, Entry);
} }
template <class ELFT> template <class ELFT>
template <typename T> template <typename T>
Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr *Section, Expected<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr &Section,
uint32_t Entry) const { uint32_t Entry) const {
if (sizeof(T) != Section->sh_entsize) if (sizeof(T) != Section.sh_entsize)
return createError("section " + getSecIndexForError(this, Section) + return createError("section " + getSecIndexForError(*this, Section) +
" has invalid sh_entsize: expected " + Twine(sizeof(T)) + " has invalid sh_entsize: expected " + Twine(sizeof(T)) +
", but got " + Twine(Section->sh_entsize)); ", but got " + Twine(Section.sh_entsize));
uint64_t Pos = Section->sh_offset + (uint64_t)Entry * sizeof(T); uint64_t Pos = Section.sh_offset + (uint64_t)Entry * sizeof(T);
if (Pos + sizeof(T) > Buf.size()) if (Pos + sizeof(T) > Buf.size())
return createError("unable to access section " + return createError("unable to access section " +
getSecIndexForError(this, Section) + " data at 0x" + getSecIndexForError(*this, Section) + " data at 0x" +
Twine::utohexstr(Pos) + Twine::utohexstr(Pos) +
": offset goes past the end of file"); ": offset goes past the end of file");
return reinterpret_cast<const T *>(base() + Pos); return reinterpret_cast<const T *>(base() + Pos);
@ -643,14 +643,14 @@ ELFFile<ELFT>::getSection(uint32_t Index) const {
template <class ELFT> template <class ELFT>
Expected<StringRef> Expected<StringRef>
ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section, ELFFile<ELFT>::getStringTable(const Elf_Shdr &Section,
WarningHandler WarnHandler) const { WarningHandler WarnHandler) const {
if (Section->sh_type != ELF::SHT_STRTAB) if (Section.sh_type != ELF::SHT_STRTAB)
if (Error E = WarnHandler("invalid sh_type for string table section " + if (Error E = WarnHandler("invalid sh_type for string table section " +
getSecIndexForError(this, Section) + getSecIndexForError(*this, Section) +
": expected SHT_STRTAB, but got " + ": expected SHT_STRTAB, but got " +
object::getELFSectionTypeName( object::getELFSectionTypeName(
getHeader()->e_machine, Section->sh_type))) getHeader().e_machine, Section.sh_type)))
return std::move(E); return std::move(E);
auto V = getSectionContentsAsArray<char>(Section); auto V = getSectionContentsAsArray<char>(Section);
@ -659,10 +659,10 @@ ELFFile<ELFT>::getStringTable(const Elf_Shdr *Section,
ArrayRef<char> Data = *V; ArrayRef<char> Data = *V;
if (Data.empty()) if (Data.empty())
return createError("SHT_STRTAB string table section " + return createError("SHT_STRTAB string table section " +
getSecIndexForError(this, Section) + " is empty"); getSecIndexForError(*this, Section) + " is empty");
if (Data.back() != '\0') if (Data.back() != '\0')
return createError("SHT_STRTAB string table section " + return createError("SHT_STRTAB string table section " +
getSecIndexForError(this, Section) + getSecIndexForError(*this, Section) +
" is non-null terminated"); " is non-null terminated");
return StringRef(Data.begin(), Data.size()); return StringRef(Data.begin(), Data.size());
} }
@ -681,7 +681,7 @@ Expected<ArrayRef<typename ELFT::Word>>
ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section, ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
Elf_Shdr_Range Sections) const { Elf_Shdr_Range Sections) const {
assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX);
auto VOrErr = getSectionContentsAsArray<Elf_Word>(&Section); auto VOrErr = getSectionContentsAsArray<Elf_Word>(Section);
if (!VOrErr) if (!VOrErr)
return VOrErr.takeError(); return VOrErr.takeError();
ArrayRef<Elf_Word> V = *VOrErr; ArrayRef<Elf_Word> V = *VOrErr;
@ -691,10 +691,10 @@ ELFFile<ELFT>::getSHNDXTable(const Elf_Shdr &Section,
const Elf_Shdr &SymTable = **SymTableOrErr; const Elf_Shdr &SymTable = **SymTableOrErr;
if (SymTable.sh_type != ELF::SHT_SYMTAB && if (SymTable.sh_type != ELF::SHT_SYMTAB &&
SymTable.sh_type != ELF::SHT_DYNSYM) SymTable.sh_type != ELF::SHT_DYNSYM)
return createError("SHT_SYMTAB_SHNDX section is linked with " + return createError(
object::getELFSectionTypeName(getHeader()->e_machine, "SHT_SYMTAB_SHNDX section is linked with " +
SymTable.sh_type) + object::getELFSectionTypeName(getHeader().e_machine, SymTable.sh_type) +
" section (expected SHT_SYMTAB/SHT_DYNSYM)"); " section (expected SHT_SYMTAB/SHT_DYNSYM)");
uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym); uint64_t Syms = SymTable.sh_size / sizeof(Elf_Sym);
if (V.size() != Syms) if (V.size() != Syms)
@ -722,15 +722,16 @@ ELFFile<ELFT>::getStringTableForSymtab(const Elf_Shdr &Sec,
if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM)
return createError( return createError(
"invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM"); "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link); Expected<const Elf_Shdr *> SectionOrErr =
object::getSection<ELFT>(Sections, Sec.sh_link);
if (!SectionOrErr) if (!SectionOrErr)
return SectionOrErr.takeError(); return SectionOrErr.takeError();
return getStringTable(*SectionOrErr); return getStringTable(**SectionOrErr);
} }
template <class ELFT> template <class ELFT>
Expected<StringRef> Expected<StringRef>
ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section, ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
WarningHandler WarnHandler) const { WarningHandler WarnHandler) const {
auto SectionsOrErr = sections(); auto SectionsOrErr = sections();
if (!SectionsOrErr) if (!SectionsOrErr)
@ -742,13 +743,13 @@ ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section,
} }
template <class ELFT> template <class ELFT>
Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section, Expected<StringRef> ELFFile<ELFT>::getSectionName(const Elf_Shdr &Section,
StringRef DotShstrtab) const { StringRef DotShstrtab) const {
uint32_t Offset = Section->sh_name; uint32_t Offset = Section.sh_name;
if (Offset == 0) if (Offset == 0)
return StringRef(); return StringRef();
if (Offset >= DotShstrtab.size()) if (Offset >= DotShstrtab.size())
return createError("a section " + getSecIndexForError(this, Section) + return createError("a section " + getSecIndexForError(*this, Section) +
" has an invalid sh_name (0x" + " has an invalid sh_name (0x" +
Twine::utohexstr(Offset) + Twine::utohexstr(Offset) +
") offset which goes past the end of the " ") offset which goes past the end of the "

View File

@ -377,7 +377,7 @@ protected:
for (const Elf_Shdr &Sec : *SectionsOrErr) { for (const Elf_Shdr &Sec : *SectionsOrErr) {
if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES || if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES ||
Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) { Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) {
auto ErrorOrContents = EF.getSectionContents(&Sec); auto ErrorOrContents = EF.getSectionContents(Sec);
if (!ErrorOrContents) if (!ErrorOrContents)
return ErrorOrContents.takeError(); return ErrorOrContents.takeError();
@ -432,7 +432,7 @@ public:
Triple::ArchType getArch() const override; Triple::ArchType getArch() const override;
Expected<uint64_t> getStartAddress() const override; Expected<uint64_t> getStartAddress() const override;
unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; } unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
const ELFFile<ELFT> *getELFFile() const { return &EF; } const ELFFile<ELFT> *getELFFile() const { return &EF; }
@ -468,7 +468,7 @@ Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
if (!StrTabOrErr) if (!StrTabOrErr)
return StrTabOrErr.takeError(); return StrTabOrErr.takeError();
const Elf_Shdr *StringTableSec = *StrTabOrErr; const Elf_Shdr *StringTableSec = *StrTabOrErr;
auto SymStrTabOrErr = EF.getStringTable(StringTableSec); auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
if (!SymStrTabOrErr) if (!SymStrTabOrErr)
return SymStrTabOrErr.takeError(); return SymStrTabOrErr.takeError();
Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr); Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr);
@ -507,9 +507,9 @@ uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
if (ESym->st_shndx == ELF::SHN_ABS) if (ESym->st_shndx == ELF::SHN_ABS)
return Ret; return Ret;
const Elf_Ehdr *Header = EF.getHeader(); const Elf_Ehdr &Header = EF.getHeader();
// Clear the ARM/Thumb or microMIPS indicator flag. // Clear the ARM/Thumb or microMIPS indicator flag.
if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) && if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
ESym->getType() == ELF::STT_FUNC) ESym->getType() == ELF::STT_FUNC)
Ret &= ~1; Ret &= ~1;
@ -533,14 +533,13 @@ ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
return Result; return Result;
} }
const Elf_Ehdr *Header = EF.getHeader();
auto SymTabOrErr = EF.getSection(Symb.d.a); auto SymTabOrErr = EF.getSection(Symb.d.a);
if (!SymTabOrErr) if (!SymTabOrErr)
return SymTabOrErr.takeError(); return SymTabOrErr.takeError();
const Elf_Shdr *SymTab = *SymTabOrErr;
if (Header->e_type == ELF::ET_REL) { if (EF.getHeader().e_type == ELF::ET_REL) {
auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable); Expected<const Elf_Shdr *> SectionOrErr =
EF.getSection(*ESym, *SymTabOrErr, ShndxTable);
if (!SectionOrErr) if (!SectionOrErr)
return SectionOrErr.takeError(); return SectionOrErr.takeError();
const Elf_Shdr *Section = *SectionOrErr; const Elf_Shdr *Section = *SectionOrErr;
@ -561,11 +560,11 @@ uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
template <class ELFT> template <class ELFT>
uint16_t ELFObjectFile<ELFT>::getEMachine() const { uint16_t ELFObjectFile<ELFT>::getEMachine() const {
return EF.getHeader()->e_machine; return EF.getHeader().e_machine;
} }
template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const { template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
return EF.getHeader()->e_type; return EF.getHeader().e_type;
} }
template <class ELFT> template <class ELFT>
@ -652,7 +651,7 @@ Expected<uint32_t> ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
// TODO: Test this error. // TODO: Test this error.
return SymbolsOrErr.takeError(); return SymbolsOrErr.takeError();
if (EF.getHeader()->e_machine == ELF::EM_ARM) { if (EF.getHeader().e_machine == ELF::EM_ARM) {
if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) { if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
StringRef Name = *NameOrErr; StringRef Name = *NameOrErr;
if (Name.startswith("$d") || Name.startswith("$t") || if (Name.startswith("$d") || Name.startswith("$t") ||
@ -685,7 +684,7 @@ template <class ELFT>
Expected<section_iterator> Expected<section_iterator>
ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym, ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
const Elf_Shdr *SymTab) const { const Elf_Shdr *SymTab) const {
auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable); auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
if (!ESecOrErr) if (!ESecOrErr)
return ESecOrErr.takeError(); return ESecOrErr.takeError();
@ -717,7 +716,7 @@ void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
template <class ELFT> template <class ELFT>
Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const { Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const {
return EF.getSectionName(&*getSection(Sec)); return EF.getSectionName(*getSection(Sec));
} }
template <class ELFT> template <class ELFT>
@ -847,7 +846,7 @@ ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
if (!SectionsOrErr) if (!SectionsOrErr)
return relocation_iterator(RelocationRef()); return relocation_iterator(RelocationRef());
uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin()); uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
RelData.d.b = 0; RelData.d.b = 0;
return relocation_iterator(RelocationRef(RelData, this)); return relocation_iterator(RelocationRef(RelData, this));
} }
@ -874,7 +873,7 @@ ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
template <class ELFT> template <class ELFT>
Expected<section_iterator> Expected<section_iterator>
ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
if (EF.getHeader()->e_type != ELF::ET_REL) if (EF.getHeader().e_type != ELF::ET_REL)
return section_end(); return section_end();
const Elf_Shdr *EShdr = getSection(Sec); const Elf_Shdr *EShdr = getSection(Sec);
@ -933,7 +932,7 @@ uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
template <class ELFT> template <class ELFT>
StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const { StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
return getELFRelocationTypeName(EF.getHeader()->e_machine, Type); return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
} }
template <class ELFT> template <class ELFT>
@ -1087,9 +1086,9 @@ uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
template <class ELFT> template <class ELFT>
StringRef ELFObjectFile<ELFT>::getFileFormatName() const { StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
bool IsLittleEndian = ELFT::TargetEndianness == support::little; bool IsLittleEndian = ELFT::TargetEndianness == support::little;
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
case ELF::ELFCLASS32: case ELF::ELFCLASS32:
switch (EF.getHeader()->e_machine) { switch (EF.getHeader().e_machine) {
case ELF::EM_386: case ELF::EM_386:
return "elf32-i386"; return "elf32-i386";
case ELF::EM_IAMCU: case ELF::EM_IAMCU:
@ -1123,7 +1122,7 @@ StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
return "elf32-unknown"; return "elf32-unknown";
} }
case ELF::ELFCLASS64: case ELF::ELFCLASS64:
switch (EF.getHeader()->e_machine) { switch (EF.getHeader().e_machine) {
case ELF::EM_386: case ELF::EM_386:
return "elf64-i386"; return "elf64-i386";
case ELF::EM_X86_64: case ELF::EM_X86_64:
@ -1157,7 +1156,7 @@ StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const { template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
bool IsLittleEndian = ELFT::TargetEndianness == support::little; bool IsLittleEndian = ELFT::TargetEndianness == support::little;
switch (EF.getHeader()->e_machine) { switch (EF.getHeader().e_machine) {
case ELF::EM_386: case ELF::EM_386:
case ELF::EM_IAMCU: case ELF::EM_IAMCU:
return Triple::x86; return Triple::x86;
@ -1174,7 +1173,7 @@ template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
case ELF::EM_LANAI: case ELF::EM_LANAI:
return Triple::lanai; return Triple::lanai;
case ELF::EM_MIPS: case ELF::EM_MIPS:
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
case ELF::ELFCLASS32: case ELF::ELFCLASS32:
return IsLittleEndian ? Triple::mipsel : Triple::mips; return IsLittleEndian ? Triple::mipsel : Triple::mips;
case ELF::ELFCLASS64: case ELF::ELFCLASS64:
@ -1189,7 +1188,7 @@ template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
case ELF::EM_PPC64: case ELF::EM_PPC64:
return IsLittleEndian ? Triple::ppc64le : Triple::ppc64; return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
case ELF::EM_RISCV: case ELF::EM_RISCV:
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
case ELF::ELFCLASS32: case ELF::ELFCLASS32:
return Triple::riscv32; return Triple::riscv32;
case ELF::ELFCLASS64: case ELF::ELFCLASS64:
@ -1210,7 +1209,7 @@ template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
if (!IsLittleEndian) if (!IsLittleEndian)
return Triple::UnknownArch; return Triple::UnknownArch;
unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH; unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST && if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
MACH <= ELF::EF_AMDGPU_MACH_R600_LAST) MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
return Triple::r600; return Triple::r600;
@ -1235,7 +1234,7 @@ template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
template <class ELFT> template <class ELFT>
Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const { Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
return EF.getHeader()->e_entry; return EF.getHeader().e_entry;
} }
template <class ELFT> template <class ELFT>
@ -1245,7 +1244,7 @@ ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
} }
template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const { template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
return EF.getHeader()->e_type == ELF::ET_REL; return EF.getHeader().e_type == ELF::ET_REL;
} }
} // end namespace object } // end namespace object

View File

@ -244,7 +244,7 @@ private:
object::ELFFile<object::ELF64LE>::Elf_Shdr_Range sections; object::ELFFile<object::ELF64LE>::Elf_Shdr_Range sections;
SymbolTable SymTab; SymbolTable SymTab;
bool isRelocatable() { return Obj.getHeader()->e_type == llvm::ELF::ET_REL; } bool isRelocatable() { return Obj.getHeader().e_type == llvm::ELF::ET_REL; }
support::endianness support::endianness
getEndianness(const object::ELFFile<object::ELF64LE> &Obj) { getEndianness(const object::ELFFile<object::ELF64LE> &Obj) {
@ -253,7 +253,7 @@ private:
// This could also just become part of a template // This could also just become part of a template
unsigned getPointerSize(const object::ELFFile<object::ELF64LE> &Obj) { unsigned getPointerSize(const object::ELFFile<object::ELF64LE> &Obj) {
return Obj.getHeader()->getFileClass() == ELF::ELFCLASS64 ? 8 : 4; return Obj.getHeader().getFileClass() == ELF::ELFCLASS64 ? 8 : 4;
} }
// We don't technically need this right now // We don't technically need this right now
@ -277,7 +277,7 @@ private:
auto StrTabSec = Obj.getSection(SecRef.sh_link); auto StrTabSec = Obj.getSection(SecRef.sh_link);
if (!StrTabSec) if (!StrTabSec)
return StrTabSec.takeError(); return StrTabSec.takeError();
auto StringTable = Obj.getStringTable(*StrTabSec); auto StringTable = Obj.getStringTable(**StrTabSec);
if (!StringTable) if (!StringTable)
return StringTable.takeError(); return StringTable.takeError();
@ -310,7 +310,7 @@ private:
Error createNormalizedSections() { Error createNormalizedSections() {
LLVM_DEBUG(dbgs() << "Creating normalized sections...\n"); LLVM_DEBUG(dbgs() << "Creating normalized sections...\n");
for (auto &SecRef : sections) { for (auto &SecRef : sections) {
auto Name = Obj.getSectionName(&SecRef); auto Name = Obj.getSectionName(SecRef);
if (!Name) if (!Name)
return Name.takeError(); return Name.takeError();
sys::Memory::ProtectionFlags Prot; sys::Memory::ProtectionFlags Prot;
@ -343,7 +343,7 @@ private:
if (SecRef.sh_type != ELF::SHT_NOBITS) { if (SecRef.sh_type != ELF::SHT_NOBITS) {
// .sections() already checks that the data is not beyond the end of // .sections() already checks that the data is not beyond the end of
// file // file
auto contents = Obj.getSectionContentsAsArray<char>(&SecRef); auto contents = Obj.getSectionContentsAsArray<char>(SecRef);
if (!contents) if (!contents)
return contents.takeError(); return contents.takeError();
@ -375,7 +375,7 @@ private:
return make_error<llvm::StringError>("Shouldn't have REL in x64", return make_error<llvm::StringError>("Shouldn't have REL in x64",
llvm::inconvertibleErrorCode()); llvm::inconvertibleErrorCode());
auto RelSectName = Obj.getSectionName(&SecRef); auto RelSectName = Obj.getSectionName(SecRef);
if (!RelSectName) if (!RelSectName)
return RelSectName.takeError(); return RelSectName.takeError();
// Deal with .eh_frame later // Deal with .eh_frame later
@ -386,7 +386,7 @@ private:
if (!UpdateSection) if (!UpdateSection)
return UpdateSection.takeError(); return UpdateSection.takeError();
auto UpdateSectionName = Obj.getSectionName(*UpdateSection); auto UpdateSectionName = Obj.getSectionName(**UpdateSection);
if (!UpdateSectionName) if (!UpdateSectionName)
return UpdateSectionName.takeError(); return UpdateSectionName.takeError();
@ -397,7 +397,7 @@ private:
*UpdateSectionName, *UpdateSectionName,
llvm::inconvertibleErrorCode()); llvm::inconvertibleErrorCode());
auto Relocations = Obj.relas(&SecRef); auto Relocations = Obj.relas(SecRef);
if (!Relocations) if (!Relocations)
return Relocations.takeError(); return Relocations.takeError();
@ -409,7 +409,7 @@ private:
<< "Name: " << Obj.getRelocationTypeName(Type) << "\n"; << "Name: " << Obj.getRelocationTypeName(Type) << "\n";
}); });
auto SymbolIndex = Rela.getSymbol(false); auto SymbolIndex = Rela.getSymbol(false);
auto Symbol = Obj.getRelocationSymbol(&Rela, &SymTab); auto Symbol = Obj.getRelocationSymbol(Rela, &SymTab);
if (!Symbol) if (!Symbol)
return Symbol.takeError(); return Symbol.takeError();
@ -472,10 +472,10 @@ private:
auto StrTabSec = Obj.getSection(SecRef.sh_link); auto StrTabSec = Obj.getSection(SecRef.sh_link);
if (!StrTabSec) if (!StrTabSec)
return StrTabSec.takeError(); return StrTabSec.takeError();
auto StringTable = Obj.getStringTable(*StrTabSec); auto StringTable = Obj.getStringTable(**StrTabSec);
if (!StringTable) if (!StringTable)
return StringTable.takeError(); return StringTable.takeError();
auto Name = Obj.getSectionName(&SecRef); auto Name = Obj.getSectionName(SecRef);
if (!Name) if (!Name)
return Name.takeError(); return Name.takeError();
auto Section = G->findSectionByName(*Name); auto Section = G->findSectionByName(*Name);
@ -520,7 +520,7 @@ private:
auto DefinedSection = Obj.getSection(SymRef.st_shndx); auto DefinedSection = Obj.getSection(SymRef.st_shndx);
if (!DefinedSection) if (!DefinedSection)
return DefinedSection.takeError(); return DefinedSection.takeError();
auto sectName = Obj.getSectionName(*DefinedSection); auto sectName = Obj.getSectionName(**DefinedSection);
if (!sectName) if (!sectName)
return Name.takeError(); return Name.takeError();

View File

@ -320,7 +320,7 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
DynEnt.StrSize); DynEnt.StrSize);
// Populate Arch from ELF header. // 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. // Populate SoName from .dynamic entries and dynamic string table.
if (DynEnt.SONameOffset.hasValue()) { if (DynEnt.SONameOffset.hasValue()) {

View File

@ -366,7 +366,7 @@ ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
template <class ELFT> template <class ELFT>
Expected<std::vector<typename ELFT::Rela>> Expected<std::vector<typename ELFT::Rela>>
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const { ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
// This function reads relocations in Android's packed relocation format, // This function reads relocations in Android's packed relocation format,
// which is based on SLEB128 and delta encoding. // which is based on SLEB128 and delta encoding.
Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec); Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
@ -511,7 +511,7 @@ std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
template <class ELFT> template <class ELFT>
std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const { std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
return getDynamicTagAsString(getHeader()->e_machine, Type); return getDynamicTagAsString(getHeader().e_machine, Type);
} }
template <class ELFT> template <class ELFT>
@ -541,7 +541,7 @@ Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
for (const Elf_Shdr &Sec : *SectionsOrError) { for (const Elf_Shdr &Sec : *SectionsOrError) {
if (Sec.sh_type == ELF::SHT_DYNAMIC) { if (Sec.sh_type == ELF::SHT_DYNAMIC) {
Expected<ArrayRef<Elf_Dyn>> DynOrError = Expected<ArrayRef<Elf_Dyn>> DynOrError =
getSectionContentsAsArray<Elf_Dyn>(&Sec); getSectionContentsAsArray<Elf_Dyn>(Sec);
if (!DynOrError) if (!DynOrError)
return DynOrError.takeError(); return DynOrError.takeError();
Dyn = *DynOrError; Dyn = *DynOrError;

View File

@ -1320,7 +1320,7 @@ void ELFBuilder<ELFT>::readProgramHeaders(const ELFFile<ELFT> &HeadersFile) {
ElfHdr.Index = Index++; ElfHdr.Index = Index++;
ElfHdr.OriginalOffset = ElfHdr.Offset = EhdrOffset; ElfHdr.OriginalOffset = ElfHdr.Offset = EhdrOffset;
const auto &Ehdr = *HeadersFile.getHeader(); const typename ELFT::Ehdr &Ehdr = HeadersFile.getHeader();
auto &PrHdr = Obj.ProgramHdrSegment; auto &PrHdr = Obj.ProgramHdrSegment;
PrHdr.Type = PT_PHDR; PrHdr.Type = PT_PHDR;
PrHdr.Flags = 0; PrHdr.Flags = 0;
@ -1398,7 +1398,7 @@ void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
const Elf_Shdr &ShndxSec = const Elf_Shdr &ShndxSec =
*unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index)); *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
ShndxData = unwrapOrError( ShndxData = unwrapOrError(
ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec)); ElfFile.template getSectionContentsAsArray<Elf_Word>(ShndxSec));
if (ShndxData.size() != Symbols.size()) if (ShndxData.size() != Symbols.size())
error("symbol section index table does not have the same number of " error("symbol section index table does not have the same number of "
"entries as the symbol table"); "entries as the symbol table");
@ -1476,7 +1476,7 @@ SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
case SHT_REL: case SHT_REL:
case SHT_RELA: case SHT_RELA:
if (Shdr.sh_flags & SHF_ALLOC) { if (Shdr.sh_flags & SHF_ALLOC) {
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<DynamicRelocationSection>(Data); return Obj.addSection<DynamicRelocationSection>(Data);
} }
return Obj.addSection<RelocationSection>(); return Obj.addSection<RelocationSection>();
@ -1485,7 +1485,7 @@ SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
// mean altering the memory image. There are no special link types or // mean altering the memory image. There are no special link types or
// anything so we can just use a Section. // anything so we can just use a Section.
if (Shdr.sh_flags & SHF_ALLOC) { if (Shdr.sh_flags & SHF_ALLOC) {
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<Section>(Data); return Obj.addSection<Section>(Data);
} }
return Obj.addSection<StringTableSection>(); return Obj.addSection<StringTableSection>();
@ -1493,16 +1493,16 @@ SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
case SHT_GNU_HASH: case SHT_GNU_HASH:
// Hash tables should refer to SHT_DYNSYM which we're not going to change. // Hash tables should refer to SHT_DYNSYM which we're not going to change.
// Because of this we don't need to mess with the hash tables either. // Because of this we don't need to mess with the hash tables either.
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<Section>(Data); return Obj.addSection<Section>(Data);
case SHT_GROUP: case SHT_GROUP:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<GroupSection>(Data); return Obj.addSection<GroupSection>(Data);
case SHT_DYNSYM: case SHT_DYNSYM:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<DynamicSymbolTableSection>(Data); return Obj.addSection<DynamicSymbolTableSection>(Data);
case SHT_DYNAMIC: case SHT_DYNAMIC:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
return Obj.addSection<DynamicSection>(Data); return Obj.addSection<DynamicSection>(Data);
case SHT_SYMTAB: { case SHT_SYMTAB: {
auto &SymTab = Obj.addSection<SymbolTableSection>(); auto &SymTab = Obj.addSection<SymbolTableSection>();
@ -1517,9 +1517,9 @@ SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
case SHT_NOBITS: case SHT_NOBITS:
return Obj.addSection<Section>(Data); return Obj.addSection<Section>(Data);
default: { default: {
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); Data = unwrapOrError(ElfFile.getSectionContents(Shdr));
StringRef Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); StringRef Name = unwrapOrError(ElfFile.getSectionName(Shdr));
if (Name.startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { if (Name.startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) {
uint64_t DecompressedSize, DecompressedAlign; uint64_t DecompressedSize, DecompressedAlign;
std::tie(DecompressedSize, DecompressedAlign) = std::tie(DecompressedSize, DecompressedAlign) =
@ -1541,7 +1541,7 @@ template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
continue; continue;
} }
auto &Sec = makeSection(Shdr); auto &Sec = makeSection(Shdr);
Sec.Name = std::string(unwrapOrError(ElfFile.getSectionName(&Shdr))); Sec.Name = std::string(unwrapOrError(ElfFile.getSectionName(Shdr)));
Sec.Type = Sec.OriginalType = Shdr.sh_type; Sec.Type = Sec.OriginalType = Shdr.sh_type;
Sec.Flags = Sec.OriginalFlags = Shdr.sh_flags; Sec.Flags = Sec.OriginalFlags = Shdr.sh_flags;
Sec.Addr = Shdr.sh_addr; Sec.Addr = Shdr.sh_addr;
@ -1560,7 +1560,7 @@ template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
} }
template <class ELFT> void ELFBuilder<ELFT>::readSections(bool EnsureSymtab) { template <class ELFT> void ELFBuilder<ELFT>::readSections(bool EnsureSymtab) {
uint32_t ShstrIndex = ElfFile.getHeader()->e_shstrndx; uint32_t ShstrIndex = ElfFile.getHeader().e_shstrndx;
if (ShstrIndex == SHN_XINDEX) if (ShstrIndex == SHN_XINDEX)
ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link; ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
@ -1602,10 +1602,10 @@ template <class ELFT> void ELFBuilder<ELFT>::readSections(bool EnsureSymtab) {
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
if (RelSec->Type == SHT_REL) if (RelSec->Type == SHT_REL)
initRelocations(RelSec, Obj.SymbolTable, initRelocations(RelSec, Obj.SymbolTable,
unwrapOrError(ElfFile.rels(Shdr))); unwrapOrError(ElfFile.rels(*Shdr)));
else else
initRelocations(RelSec, Obj.SymbolTable, initRelocations(RelSec, Obj.SymbolTable,
unwrapOrError(ElfFile.relas(Shdr))); unwrapOrError(ElfFile.relas(*Shdr)));
} else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) { } else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) {
initGroupSection(GroupSec); initGroupSection(GroupSec);
} }
@ -1622,7 +1622,7 @@ template <class ELFT> void ELFBuilder<ELFT>::build(bool EnsureSymtab) {
ELFFile<ELFT> HeadersFile = unwrapOrError(ELFFile<ELFT>::create(toStringRef( ELFFile<ELFT> HeadersFile = unwrapOrError(ELFFile<ELFT>::create(toStringRef(
{ElfFile.base() + EhdrOffset, ElfFile.getBufSize() - EhdrOffset}))); {ElfFile.base() + EhdrOffset, ElfFile.getBufSize() - EhdrOffset})));
auto &Ehdr = *HeadersFile.getHeader(); auto &Ehdr = HeadersFile.getHeader();
Obj.OSABI = Ehdr.e_ident[EI_OSABI]; Obj.OSABI = Ehdr.e_ident[EI_OSABI];
Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION]; Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
Obj.Type = Ehdr.e_type; Obj.Type = Ehdr.e_type;

View File

@ -92,7 +92,7 @@ static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
return SymSI.takeError(); return SymSI.takeError();
const typename ELFT::Shdr *SymSec = const typename ELFT::Shdr *SymSec =
Obj->getSection((*SymSI)->getRawDataRefImpl()); Obj->getSection((*SymSI)->getRawDataRefImpl());
auto SecName = EF.getSectionName(SymSec); auto SecName = EF.getSectionName(*SymSec);
if (!SecName) if (!SecName)
return SecName.takeError(); return SecName.takeError();
Fmt << *SecName; Fmt << *SecName;
@ -338,10 +338,10 @@ static void printSymbolVersionInfo(const ELFFile<ELFT> *Elf,
continue; continue;
ArrayRef<uint8_t> Contents = ArrayRef<uint8_t> Contents =
unwrapOrError(Elf->getSectionContents(&Shdr), FileName); unwrapOrError(Elf->getSectionContents(Shdr), FileName);
const typename ELFT::Shdr *StrTabSec = const typename ELFT::Shdr *StrTabSec =
unwrapOrError(Elf->getSection(Shdr.sh_link), FileName); unwrapOrError(Elf->getSection(Shdr.sh_link), FileName);
StringRef StrTab = unwrapOrError(Elf->getStringTable(StrTabSec), FileName); StringRef StrTab = unwrapOrError(Elf->getStringTable(*StrTabSec), FileName);
if (Shdr.sh_type == ELF::SHT_GNU_verneed) if (Shdr.sh_type == ELF::SHT_GNU_verneed)
printSymbolVersionDependency<ELFT>(Contents, StrTab); printSymbolVersionDependency<ELFT>(Contents, StrTab);

View File

@ -407,7 +407,7 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
reportError(SymTabOrErr.takeError(), FileName); reportError(SymTabOrErr.takeError(), FileName);
const Elf_Shdr *SymTab = *SymTabOrErr; const Elf_Shdr *SymTab = *SymTabOrErr;
for (const Elf_Rel &R : unwrapOrError(FileName, ELF->rels(&Sec))) { for (const Elf_Rel &R : unwrapOrError(FileName, ELF->rels(Sec))) {
if (R.r_offset != static_cast<unsigned>(IndexTableOffset)) if (R.r_offset != static_cast<unsigned>(IndexTableOffset))
continue; continue;
@ -417,9 +417,9 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
RelA.r_addend = 0; RelA.r_addend = 0;
const Elf_Sym *Symbol = 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) if (!Ret)
report_fatal_error(errorToErrorCode(Ret.takeError()).message()); report_fatal_error(errorToErrorCode(Ret.takeError()).message());
return *Ret; return *Ret;
@ -432,7 +432,7 @@ template <typename ET>
void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr *IT, void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr *IT,
const Elf_Shdr *EHT, const Elf_Shdr *EHT,
uint64_t TableEntryOffset) const { uint64_t TableEntryOffset) const {
Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(EHT); Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(*EHT);
if (!Contents) if (!Contents)
return; return;
@ -499,7 +499,7 @@ void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry,
template <typename ET> template <typename ET>
void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex, void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
const Elf_Shdr *IT) const { const Elf_Shdr *IT) const {
Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(IT); Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(*IT);
if (!Contents) if (!Contents)
return; return;
@ -553,7 +553,7 @@ void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4); FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
if (EHT) if (EHT)
if (auto Name = ELF->getSectionName(EHT)) if (auto Name = ELF->getSectionName(*EHT))
SW.printString("ExceptionHandlingTable", *Name); SW.printString("ExceptionHandlingTable", *Name);
uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr); uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr);
@ -575,7 +575,7 @@ void PrinterContext<ET>::PrintUnwindInformation() const {
DictScope UIT(SW, "UnwindIndexTable"); DictScope UIT(SW, "UnwindIndexTable");
SW.printNumber("SectionIndex", SectionIndex); SW.printNumber("SectionIndex", SectionIndex);
if (auto SectionName = ELF->getSectionName(&Sec)) if (auto SectionName = ELF->getSectionName(Sec))
SW.printString("SectionName", *SectionName); SW.printString("SectionName", *SectionName);
SW.printHex("SectionOffset", Sec.sh_offset); SW.printHex("SectionOffset", Sec.sh_offset);

View File

@ -85,7 +85,7 @@ void PrinterContext<ELFT>::printUnwindInformation() const {
reportError(SectionsOrErr.takeError(), ObjF->getFileName()); reportError(SectionsOrErr.takeError(), ObjF->getFileName());
for (const Elf_Shdr &Shdr : *SectionsOrErr) { for (const Elf_Shdr &Shdr : *SectionsOrErr) {
Expected<StringRef> NameOrErr = Obj->getSectionName(&Shdr); Expected<StringRef> NameOrErr = Obj->getSectionName(Shdr);
if (!NameOrErr) if (!NameOrErr)
reportError(NameOrErr.takeError(), ObjF->getFileName()); reportError(NameOrErr.takeError(), ObjF->getFileName());
if (*NameOrErr == ".eh_frame") if (*NameOrErr == ".eh_frame")
@ -104,13 +104,13 @@ void PrinterContext<ELFT>::printEHFrameHdr(const Elf_Phdr *EHFramePHdr) const {
const object::ELFFile<ELFT> *Obj = ObjF->getELFFile(); const object::ELFFile<ELFT> *Obj = ObjF->getELFFile();
if (const Elf_Shdr *EHFrameHdr = if (const Elf_Shdr *EHFrameHdr =
findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) { findSectionByAddress(ObjF, EHFramePHdr->p_vaddr)) {
Expected<StringRef> NameOrErr = Obj->getSectionName(EHFrameHdr); Expected<StringRef> NameOrErr = Obj->getSectionName(*EHFrameHdr);
if (!NameOrErr) if (!NameOrErr)
reportError(NameOrErr.takeError(), ObjF->getFileName()); reportError(NameOrErr.takeError(), ObjF->getFileName());
W.printString("Corresponding Section", *NameOrErr); W.printString("Corresponding Section", *NameOrErr);
} }
Expected<ArrayRef<uint8_t>> Content = Obj->getSegmentContents(EHFramePHdr); Expected<ArrayRef<uint8_t>> Content = Obj->getSegmentContents(*EHFramePHdr);
if (!Content) if (!Content)
reportError(Content.takeError(), ObjF->getFileName()); reportError(Content.takeError(), ObjF->getFileName());
@ -181,7 +181,7 @@ void PrinterContext<ELFT>::printEHFrame(const Elf_Shdr *EHFrameShdr) const {
W.indent(); W.indent();
Expected<ArrayRef<uint8_t>> DataOrErr = Expected<ArrayRef<uint8_t>> DataOrErr =
ObjF->getELFFile()->getSectionContents(EHFrameShdr); ObjF->getELFFile()->getSectionContents(*EHFrameShdr);
if (!DataOrErr) if (!DataOrErr)
reportError(DataOrErr.takeError(), ObjF->getFileName()); reportError(DataOrErr.takeError(), ObjF->getFileName());

View File

@ -404,7 +404,7 @@ template <class ELFT>
static std::string describe(const ELFFile<ELFT> &Obj, static std::string describe(const ELFFile<ELFT> &Obj,
const typename ELFT::Shdr &Sec) { const typename ELFT::Shdr &Sec) {
unsigned SecNdx = &Sec - &cantFail(Obj.sections()).front(); unsigned SecNdx = &Sec - &cantFail(Obj.sections()).front();
return (object::getELFSectionTypeName(Obj.getHeader()->e_machine, return (object::getELFSectionTypeName(Obj.getHeader().e_machine,
Sec.sh_type) + Sec.sh_type) +
" section with index " + Twine(SecNdx)) " section with index " + Twine(SecNdx))
.str(); .str();
@ -424,7 +424,7 @@ static Expected<StringRef> getLinkAsStrtab(const ELFFile<ELFT> &Obj,
return createError("invalid section linked to " + describe(Obj, *Sec) + return createError("invalid section linked to " + describe(Obj, *Sec) +
": " + toString(StrTabSecOrErr.takeError())); ": " + toString(StrTabSecOrErr.takeError()));
Expected<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSecOrErr); Expected<StringRef> StrTabOrErr = Obj.getStringTable(**StrTabSecOrErr);
if (!StrTabOrErr) 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())); ": " + toString(StrTabOrErr.takeError()));
@ -443,13 +443,12 @@ getLinkAsSymtab(const ELFFile<ELFT> &Obj, const typename ELFT::Shdr *Sec,
": " + toString(SymtabOrErr.takeError())); ": " + toString(SymtabOrErr.takeError()));
if ((*SymtabOrErr)->sh_type != ExpectedType) if ((*SymtabOrErr)->sh_type != ExpectedType)
return createError("invalid section linked to " + describe(Obj, *Sec) + return createError(
": expected " + "invalid section linked to " + describe(Obj, *Sec) + ": expected " +
object::getELFSectionTypeName(Obj.getHeader()->e_machine, object::getELFSectionTypeName(Obj.getHeader().e_machine, ExpectedType) +
ExpectedType) + ", but got " +
", but got " + object::getELFSectionTypeName(Obj.getHeader().e_machine,
object::getELFSectionTypeName(Obj.getHeader()->e_machine, (*SymtabOrErr)->sh_type));
(*SymtabOrErr)->sh_type));
Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Obj, *SymtabOrErr); Expected<StringRef> StrTabOrErr = getLinkAsStrtab(Obj, *SymtabOrErr);
if (!StrTabOrErr) if (!StrTabOrErr)
@ -477,7 +476,7 @@ ELFDumper<ELFT>::getVersionTable(const Elf_Shdr *Sec, ArrayRef<Elf_Sym> *SymTab,
return createError("the " + describe(*Sec) + " is misaligned"); return createError("the " + describe(*Sec) + " is misaligned");
Expected<ArrayRef<Elf_Versym>> VersionsOrErr = Expected<ArrayRef<Elf_Versym>> VersionsOrErr =
Obj->template getSectionContentsAsArray<Elf_Versym>(Sec); Obj->template getSectionContentsAsArray<Elf_Versym>(*Sec);
if (!VersionsOrErr) if (!VersionsOrErr)
return createError("cannot read content of " + describe(*Sec) + ": " + return createError("cannot read content of " + describe(*Sec) + ": " +
toString(VersionsOrErr.takeError())); toString(VersionsOrErr.takeError()));
@ -511,7 +510,7 @@ ELFDumper<ELFT>::getVersionDefinitions(const Elf_Shdr *Sec) const {
if (!StrTabOrErr) if (!StrTabOrErr)
return StrTabOrErr.takeError(); return StrTabOrErr.takeError();
Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(Sec); Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(*Sec);
if (!ContentsOrErr) if (!ContentsOrErr)
return createError("cannot read content of " + describe(*Sec) + ": " + return createError("cannot read content of " + describe(*Sec) + ": " +
toString(ContentsOrErr.takeError())); toString(ContentsOrErr.takeError()));
@ -600,7 +599,7 @@ ELFDumper<ELFT>::getVersionDependencies(const Elf_Shdr *Sec) const {
else else
StrTab = *StrTabOrErr; StrTab = *StrTabOrErr;
Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(Sec); Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(*Sec);
if (!ContentsOrErr) if (!ContentsOrErr)
return createError("cannot read content of " + describe(*Sec) + ": " + return createError("cannot read content of " + describe(*Sec) + ": " +
toString(ContentsOrErr.takeError())); toString(ContentsOrErr.takeError()));
@ -1069,7 +1068,7 @@ Expected<StringRef> ELFDumper<ELFT>::getSymbolVersion(const Elf_Sym *Sym,
// Get the corresponding version index entry. // Get the corresponding version index entry.
if (Expected<const Elf_Versym *> EntryOrErr = if (Expected<const Elf_Versym *> EntryOrErr =
ObjF->getELFFile()->template getEntry<Elf_Versym>( ObjF->getELFFile()->template getEntry<Elf_Versym>(
SymbolVersionSection, EntryIndex)) *SymbolVersionSection, EntryIndex))
return this->getSymbolVersionByIndex((*EntryOrErr)->vs_index, IsDefault); return this->getSymbolVersionByIndex((*EntryOrErr)->vs_index, IsDefault);
else else
return EntryOrErr.takeError(); return EntryOrErr.takeError();
@ -1084,7 +1083,7 @@ ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
const ELFFile<ELFT> &Obj = *ObjF->getELFFile(); const ELFFile<ELFT> &Obj = *ObjF->getELFFile();
Expected<const Elf_Sym *> SymOrErr = Expected<const Elf_Sym *> SymOrErr =
Obj.template getEntry<Elf_Sym>(SymTab, R.Symbol); Obj.template getEntry<Elf_Sym>(*SymTab, R.Symbol);
if (!SymOrErr) if (!SymOrErr)
return SymOrErr.takeError(); return SymOrErr.takeError();
const Elf_Sym *Sym = *SymOrErr; const Elf_Sym *Sym = *SymOrErr;
@ -1095,14 +1094,14 @@ ELFDumper<ELFT>::getRelocationTarget(const Relocation<ELFT> &R,
// This code block returns the section name. // This code block returns the section name.
if (Sym->getType() == ELF::STT_SECTION) { if (Sym->getType() == ELF::STT_SECTION) {
Expected<const Elf_Shdr *> SecOrErr = Expected<const Elf_Shdr *> SecOrErr =
Obj.getSection(Sym, SymTab, ShndxTable); Obj.getSection(*Sym, SymTab, ShndxTable);
if (!SecOrErr) if (!SecOrErr)
return SecOrErr.takeError(); return SecOrErr.takeError();
// A section symbol describes the section at index 0. // A section symbol describes the section at index 0.
if (*SecOrErr == nullptr) if (*SecOrErr == nullptr)
return RelSymbol<ELFT>(Sym, ""); return RelSymbol<ELFT>(Sym, "");
Expected<StringRef> NameOrErr = Obj.getSectionName(*SecOrErr); Expected<StringRef> NameOrErr = Obj.getSectionName(**SecOrErr);
if (!NameOrErr) if (!NameOrErr)
return NameOrErr.takeError(); return NameOrErr.takeError();
return RelSymbol<ELFT>(Sym, NameOrErr->str()); return RelSymbol<ELFT>(Sym, NameOrErr->str());
@ -1227,7 +1226,7 @@ Expected<unsigned>
ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol, ELFDumper<ELFT>::getSymbolSectionIndex(const Elf_Sym *Symbol,
const Elf_Sym *FirstSym) const { const Elf_Sym *FirstSym) const {
return Symbol->st_shndx == SHN_XINDEX return Symbol->st_shndx == SHN_XINDEX
? object::getExtendedSymbolTableIndex<ELFT>(Symbol, FirstSym, ? object::getExtendedSymbolTableIndex<ELFT>(*Symbol, *FirstSym,
ShndxTable) ShndxTable)
: Symbol->st_shndx; : Symbol->st_shndx;
} }
@ -1259,7 +1258,7 @@ ELFDumper<ELFT>::getSymbolSectionName(const Elf_Sym *Symbol,
Obj->getSection(SectionIndex); Obj->getSection(SectionIndex);
if (!SecOrErr) if (!SecOrErr)
return SecOrErr.takeError(); return SecOrErr.takeError();
return Obj->getSectionName(*SecOrErr); return Obj->getSectionName(**SecOrErr);
} }
template <class ELFO> template <class ELFO>
@ -2423,7 +2422,7 @@ const typename ELFT::Shdr *
ELFDumper<ELFT>::findSectionByName(StringRef Name) const { ELFDumper<ELFT>::findSectionByName(StringRef Name) const {
const ELFFile<ELFT> *Obj = ObjF->getELFFile(); const ELFFile<ELFT> *Obj = ObjF->getELFFile();
for (const Elf_Shdr &Shdr : cantFail(Obj->sections())) { for (const Elf_Shdr &Shdr : cantFail(Obj->sections())) {
if (Expected<StringRef> NameOrErr = Obj->getSectionName(&Shdr)) { if (Expected<StringRef> NameOrErr = Obj->getSectionName(Shdr)) {
if (*NameOrErr == Name) if (*NameOrErr == Name)
return &Shdr; return &Shdr;
} else { } else {
@ -2456,7 +2455,7 @@ std::string ELFDumper<ELFT>::getDynamicEntry(uint64_t Type,
}; };
// Handle custom printing of architecture specific tags // Handle custom printing of architecture specific tags
switch (ObjF->getELFFile()->getHeader()->e_machine) { switch (ObjF->getELFFile()->getHeader().e_machine) {
case EM_AARCH64: case EM_AARCH64:
switch (Type) { switch (Type) {
case DT_AARCH64_BTI_PLT: case DT_AARCH64_BTI_PLT:
@ -2653,7 +2652,7 @@ namespace {
template <> void ELFDumper<ELF32LE>::printUnwindInfo() { template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
const ELFFile<ELF32LE> *Obj = ObjF->getELFFile(); const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
const unsigned Machine = Obj->getHeader()->e_machine; const unsigned Machine = Obj->getHeader().e_machine;
if (Machine == EM_ARM) { if (Machine == EM_ARM) {
ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF->getFileName(), ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF->getFileName(),
DotSymtabSec); DotSymtabSec);
@ -2832,7 +2831,7 @@ template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() { template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() {
const ELFFile<ELFT> *Obj = ObjF->getELFFile(); const ELFFile<ELFT> *Obj = ObjF->getELFFile();
switch (Obj->getHeader()->e_machine) { switch (Obj->getHeader().e_machine) {
case EM_ARM: case EM_ARM:
case EM_RISCV: case EM_RISCV:
printAttributes(); printAttributes();
@ -2867,7 +2866,7 @@ template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
return; return;
} }
const unsigned Machine = Obj->getHeader()->e_machine; const unsigned Machine = Obj->getHeader().e_machine;
assert((Machine == EM_ARM || Machine == EM_RISCV) && assert((Machine == EM_ARM || Machine == EM_RISCV) &&
"Attributes not implemented."); "Attributes not implemented.");
@ -2878,7 +2877,7 @@ template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
continue; continue;
ArrayRef<uint8_t> Contents = ArrayRef<uint8_t> Contents =
unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(&Sec)); unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Sec));
if (Contents[0] != ELFAttrs::Format_Version) { if (Contents[0] != ELFAttrs::Format_Version) {
reportWarning(createError(Twine("unrecognised FormatVersion: 0x") + reportWarning(createError(Twine("unrecognised FormatVersion: 0x") +
Twine::utohexstr(Contents[0])), Twine::utohexstr(Contents[0])),
@ -2978,7 +2977,7 @@ Error MipsGOTParser<ELFT>::findGOT(Elf_Dyn_Range DynTable,
return Error::success(); return Error::success();
ArrayRef<uint8_t> Content = ArrayRef<uint8_t> Content =
unwrapOrError(FileName, Obj->getSectionContents(GotSec)); unwrapOrError(FileName, Obj->getSectionContents(*GotSec));
GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()), GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
Content.size() / sizeof(Entry)); Content.size() / sizeof(Entry));
LocalNum = GotEntries.size(); LocalNum = GotEntries.size();
@ -3028,7 +3027,7 @@ Error MipsGOTParser<ELFT>::findGOT(Elf_Dyn_Range DynTable,
GlobalNum = DynSymTotal - *DtGotSym; GlobalNum = DynSymTotal - *DtGotSym;
ArrayRef<uint8_t> Content = ArrayRef<uint8_t> Content =
unwrapOrError(FileName, Obj->getSectionContents(GotSec)); unwrapOrError(FileName, Obj->getSectionContents(*GotSec));
GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()), GotEntries = Entries(reinterpret_cast<const Entry *>(Content.data()),
Content.size() / sizeof(Entry)); Content.size() / sizeof(Entry));
GotDynSyms = DynSyms.drop_front(*DtGotSym); GotDynSyms = DynSyms.drop_front(*DtGotSym);
@ -3072,7 +3071,7 @@ Error MipsGOTParser<ELFT>::findPLT(Elf_Dyn_Range DynTable) {
Twine::utohexstr(*DtJmpRel)); Twine::utohexstr(*DtJmpRel));
if (Expected<ArrayRef<uint8_t>> PltContentOrErr = if (Expected<ArrayRef<uint8_t>> PltContentOrErr =
Obj->getSectionContents(PltSec)) Obj->getSectionContents(*PltSec))
PltEntries = PltEntries =
Entries(reinterpret_cast<const Entry *>(PltContentOrErr->data()), Entries(reinterpret_cast<const Entry *>(PltContentOrErr->data()),
PltContentOrErr->size() / sizeof(Entry)); PltContentOrErr->size() / sizeof(Entry));
@ -3196,13 +3195,13 @@ const typename MipsGOTParser<ELFT>::Elf_Sym *
MipsGOTParser<ELFT>::getPltSym(const Entry *E) const { MipsGOTParser<ELFT>::getPltSym(const Entry *E) const {
int64_t Offset = std::distance(getPltEntries().data(), E); int64_t Offset = std::distance(getPltEntries().data(), E);
if (PltRelSec->sh_type == ELF::SHT_REL) { 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, return unwrapOrError(FileName,
Obj->getRelocationSymbol(&Rels[Offset], PltSymTable)); Obj->getRelocationSymbol(Rels[Offset], PltSymTable));
} else { } else {
Elf_Rela_Range Rels = unwrapOrError(FileName, Obj->relas(PltRelSec)); Elf_Rela_Range Rels = unwrapOrError(FileName, Obj->relas(*PltRelSec));
return unwrapOrError(FileName, return unwrapOrError(FileName,
Obj->getRelocationSymbol(&Rels[Offset], PltSymTable)); Obj->getRelocationSymbol(Rels[Offset], PltSymTable));
} }
} }
@ -3299,7 +3298,7 @@ template <class ELFT> void ELFDumper<ELFT>::printMipsReginfo() {
const ELFFile<ELFT> *Obj = ObjF->getELFFile(); const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Expected<ArrayRef<uint8_t>> ContentsOrErr = Expected<ArrayRef<uint8_t>> ContentsOrErr =
Obj->getSectionContents(RegInfoSec); Obj->getSectionContents(*RegInfoSec);
if (!ContentsOrErr) { if (!ContentsOrErr) {
this->reportUniqueWarning(createError( this->reportUniqueWarning(createError(
"unable to read the content of the .reginfo section (" + "unable to read the content of the .reginfo section (" +
@ -3367,7 +3366,7 @@ template <class ELFT> void ELFDumper<ELFT>::printMipsOptions() {
DictScope GS(W, "MIPS Options"); DictScope GS(W, "MIPS Options");
ArrayRef<uint8_t> Data = ArrayRef<uint8_t> Data =
unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(MipsOpts)); unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(*MipsOpts));
const uint8_t *const SecBegin = Data.begin(); const uint8_t *const SecBegin = Data.begin();
while (!Data.empty()) { while (!Data.empty()) {
bool IsSupported; bool IsSupported;
@ -3407,7 +3406,7 @@ template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
}; };
Expected<ArrayRef<uint8_t>> ContentOrErr = Expected<ArrayRef<uint8_t>> ContentOrErr =
Obj->getSectionContents(StackMapSection); Obj->getSectionContents(*StackMapSection);
if (!ContentOrErr) { if (!ContentOrErr) {
Warn(ContentOrErr.takeError()); Warn(ContentOrErr.takeError());
return; return;
@ -3442,9 +3441,9 @@ static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
template <class ELFT> template <class ELFT>
static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj, static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj,
StringRef FileName) { StringRef FileName) {
const typename ELFT::Ehdr *ElfHeader = Obj.getHeader(); const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
if (ElfHeader->e_shnum != 0) if (ElfHeader.e_shnum != 0)
return to_string(ElfHeader->e_shnum); return to_string(ElfHeader.e_shnum);
ArrayRef<typename ELFT::Shdr> Arr = cantFail(Obj.sections()); ArrayRef<typename ELFT::Shdr> Arr = cantFail(Obj.sections());
if (Arr.empty()) if (Arr.empty())
@ -3455,71 +3454,71 @@ static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj,
template <class ELFT> template <class ELFT>
static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> &Obj, static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> &Obj,
StringRef FileName) { StringRef FileName) {
const typename ELFT::Ehdr *ElfHeader = Obj.getHeader(); const typename ELFT::Ehdr &ElfHeader = Obj.getHeader();
if (ElfHeader->e_shstrndx != SHN_XINDEX) if (ElfHeader.e_shstrndx != SHN_XINDEX)
return to_string(ElfHeader->e_shstrndx); return to_string(ElfHeader.e_shstrndx);
ArrayRef<typename ELFT::Shdr> Arr = cantFail(Obj.sections()); ArrayRef<typename ELFT::Shdr> Arr = cantFail(Obj.sections());
if (Arr.empty()) if (Arr.empty())
return "65535 (corrupt: out of range)"; return "65535 (corrupt: out of range)";
return to_string(ElfHeader->e_shstrndx) + " (" + to_string(Arr[0].sh_link) + return to_string(ElfHeader.e_shstrndx) + " (" + to_string(Arr[0].sh_link) +
")"; ")";
} }
template <class ELFT> void GNUStyle<ELFT>::printFileHeaders() { template <class ELFT> void GNUStyle<ELFT>::printFileHeaders() {
const Elf_Ehdr *e = this->Obj.getHeader(); const Elf_Ehdr &e = this->Obj.getHeader();
OS << "ELF Header:\n"; OS << "ELF Header:\n";
OS << " Magic: "; OS << " Magic: ";
std::string Str; std::string Str;
for (int i = 0; i < ELF::EI_NIDENT; i++) for (int i = 0; i < ELF::EI_NIDENT; i++)
OS << format(" %02x", static_cast<int>(e->e_ident[i])); OS << format(" %02x", static_cast<int>(e.e_ident[i]));
OS << "\n"; OS << "\n";
Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass)); Str = printEnum(e.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
printFields(OS, "Class:", Str); printFields(OS, "Class:", Str);
Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding)); Str = printEnum(e.e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
printFields(OS, "Data:", Str); printFields(OS, "Data:", Str);
OS.PadToColumn(2u); OS.PadToColumn(2u);
OS << "Version:"; OS << "Version:";
OS.PadToColumn(37u); OS.PadToColumn(37u);
OS << to_hexString(e->e_ident[ELF::EI_VERSION]); OS << to_hexString(e.e_ident[ELF::EI_VERSION]);
if (e->e_version == ELF::EV_CURRENT) if (e.e_version == ELF::EV_CURRENT)
OS << " (current)"; OS << " (current)";
OS << "\n"; OS << "\n";
Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI)); Str = printEnum(e.e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
printFields(OS, "OS/ABI:", Str); printFields(OS, "OS/ABI:", Str);
printFields(OS, printFields(OS,
"ABI Version:", std::to_string(e->e_ident[ELF::EI_ABIVERSION])); "ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType)); Str = printEnum(e.e_type, makeArrayRef(ElfObjectFileType));
printFields(OS, "Type:", Str); printFields(OS, "Type:", Str);
Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType)); Str = printEnum(e.e_machine, makeArrayRef(ElfMachineType));
printFields(OS, "Machine:", Str); printFields(OS, "Machine:", Str);
Str = "0x" + to_hexString(e->e_version); Str = "0x" + to_hexString(e.e_version);
printFields(OS, "Version:", Str); printFields(OS, "Version:", Str);
Str = "0x" + to_hexString(e->e_entry); Str = "0x" + to_hexString(e.e_entry);
printFields(OS, "Entry point address:", Str); printFields(OS, "Entry point address:", Str);
Str = to_string(e->e_phoff) + " (bytes into file)"; Str = to_string(e.e_phoff) + " (bytes into file)";
printFields(OS, "Start of program headers:", Str); printFields(OS, "Start of program headers:", Str);
Str = to_string(e->e_shoff) + " (bytes into file)"; Str = to_string(e.e_shoff) + " (bytes into file)";
printFields(OS, "Start of section headers:", Str); printFields(OS, "Start of section headers:", Str);
std::string ElfFlags; std::string ElfFlags;
if (e->e_machine == EM_MIPS) if (e.e_machine == EM_MIPS)
ElfFlags = ElfFlags =
printFlags(e->e_flags, makeArrayRef(ElfHeaderMipsFlags), printFlags(e.e_flags, makeArrayRef(ElfHeaderMipsFlags),
unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI), unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
unsigned(ELF::EF_MIPS_MACH)); unsigned(ELF::EF_MIPS_MACH));
else if (e->e_machine == EM_RISCV) else if (e.e_machine == EM_RISCV)
ElfFlags = printFlags(e->e_flags, makeArrayRef(ElfHeaderRISCVFlags)); ElfFlags = printFlags(e.e_flags, makeArrayRef(ElfHeaderRISCVFlags));
Str = "0x" + to_hexString(e->e_flags); Str = "0x" + to_hexString(e.e_flags);
if (!ElfFlags.empty()) if (!ElfFlags.empty())
Str = Str + ", " + ElfFlags; Str = Str + ", " + ElfFlags;
printFields(OS, "Flags:", Str); printFields(OS, "Flags:", Str);
Str = to_string(e->e_ehsize) + " (bytes)"; Str = to_string(e.e_ehsize) + " (bytes)";
printFields(OS, "Size of this header:", Str); printFields(OS, "Size of this header:", Str);
Str = to_string(e->e_phentsize) + " (bytes)"; Str = to_string(e.e_phentsize) + " (bytes)";
printFields(OS, "Size of program headers:", Str); printFields(OS, "Size of program headers:", Str);
Str = to_string(e->e_phnum); Str = to_string(e.e_phnum);
printFields(OS, "Number of program headers:", Str); printFields(OS, "Number of program headers:", Str);
Str = to_string(e->e_shentsize) + " (bytes)"; Str = to_string(e.e_shentsize) + " (bytes)";
printFields(OS, "Size of section headers:", Str); printFields(OS, "Size of section headers:", Str);
Str = getSectionHeadersNumString(this->Obj, this->FileName); Str = getSectionHeadersNumString(this->Obj, this->FileName);
printFields(OS, "Number of section headers:", Str); printFields(OS, "Number of section headers:", Str);
@ -3563,11 +3562,11 @@ std::vector<GroupSection> getGroups(const ELFFile<ELFT> &Obj,
StringRef StrTable = StringRef StrTable =
unwrapOrError(FileName, Obj.getStringTableForSymtab(*Symtab)); unwrapOrError(FileName, Obj.getStringTableForSymtab(*Symtab));
const Elf_Sym *Sym = unwrapOrError( const Elf_Sym *Sym = unwrapOrError(
FileName, Obj.template getEntry<Elf_Sym>(Symtab, Sec.sh_info)); FileName, Obj.template getEntry<Elf_Sym>(*Symtab, Sec.sh_info));
auto Data = unwrapOrError( auto Data = unwrapOrError(
FileName, Obj.template getSectionContentsAsArray<Elf_Word>(&Sec)); FileName, Obj.template getSectionContentsAsArray<Elf_Word>(Sec));
StringRef Name = unwrapOrError(FileName, Obj.getSectionName(&Sec)); StringRef Name = unwrapOrError(FileName, Obj.getSectionName(Sec));
StringRef Signature = StrTable.data() + Sym->st_name; StringRef Signature = StrTable.data() + Sym->st_name;
Ret.push_back({Name, Ret.push_back({Name,
maybeDemangle(Signature), maybeDemangle(Signature),
@ -3580,7 +3579,7 @@ std::vector<GroupSection> getGroups(const ELFFile<ELFT> &Obj,
std::vector<GroupMember> &GM = Ret.back().Members; std::vector<GroupMember> &GM = Ret.back().Members;
for (uint32_t Ndx : Data.slice(1)) { for (uint32_t Ndx : Data.slice(1)) {
auto Sec = unwrapOrError(FileName, Obj.getSection(Ndx)); const Elf_Shdr &Sec = *unwrapOrError(FileName, Obj.getSection(Ndx));
const StringRef Name = unwrapOrError(FileName, Obj.getSectionName(Sec)); const StringRef Name = unwrapOrError(FileName, Obj.getSectionName(Sec));
GM.push_back({Name, Ndx}); GM.push_back({Name, Ndx});
} }
@ -3727,7 +3726,7 @@ template <class ELFT> void GNUStyle<ELFT>::printRelocations() {
if (Sec.sh_type == ELF::SHT_ANDROID_REL || if (Sec.sh_type == ELF::SHT_ANDROID_REL ||
Sec.sh_type == ELF::SHT_ANDROID_RELA) { Sec.sh_type == ELF::SHT_ANDROID_RELA) {
Expected<std::vector<typename ELFT::Rela>> RelasOrErr = Expected<std::vector<typename ELFT::Rela>> RelasOrErr =
this->Obj.android_relas(&Sec); this->Obj.android_relas(Sec);
if (!RelasOrErr) if (!RelasOrErr)
return RelasOrErr.takeError(); return RelasOrErr.takeError();
return RelasOrErr->size(); return RelasOrErr->size();
@ -3735,7 +3734,7 @@ template <class ELFT> void GNUStyle<ELFT>::printRelocations() {
if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR || if (!opts::RawRelr && (Sec.sh_type == ELF::SHT_RELR ||
Sec.sh_type == ELF::SHT_ANDROID_RELR)) { Sec.sh_type == ELF::SHT_ANDROID_RELR)) {
Expected<Elf_Relr_Range> RelrsOrErr = this->Obj.relrs(&Sec); Expected<Elf_Relr_Range> RelrsOrErr = this->Obj.relrs(Sec);
if (!RelrsOrErr) if (!RelrsOrErr)
return RelrsOrErr.takeError(); return RelrsOrErr.takeError();
return this->Obj.decode_relrs(*RelrsOrErr).size(); return this->Obj.decode_relrs(*RelrsOrErr).size();
@ -3827,7 +3826,7 @@ template <class ELFT> void GNUStyle<ELFT>::printSectionHeaders() {
ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections()); ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
OS << "There are " << to_string(Sections.size()) OS << "There are " << to_string(Sections.size())
<< " section headers, starting at offset " << " section headers, starting at offset "
<< "0x" << to_hexString(this->Obj.getHeader()->e_shoff, false) << ":\n\n"; << "0x" << to_hexString(this->Obj.getHeader().e_shoff, false) << ":\n\n";
OS << "Section Headers:\n"; OS << "Section Headers:\n";
Field Fields[11] = { Field Fields[11] = {
{"[Nr]", 2}, {"Name", 7}, {"Type", 25}, {"[Nr]", 2}, {"Name", 7}, {"Type", 25},
@ -3852,15 +3851,15 @@ template <class ELFT> void GNUStyle<ELFT>::printSectionHeaders() {
Fields[1].Str = "<no-strings>"; Fields[1].Str = "<no-strings>";
else else
Fields[1].Str = std::string(unwrapOrError<StringRef>( Fields[1].Str = std::string(unwrapOrError<StringRef>(
this->FileName, this->Obj.getSectionName(&Sec, SecStrTable))); this->FileName, this->Obj.getSectionName(Sec, SecStrTable)));
Fields[2].Str = Fields[2].Str =
getSectionTypeString(this->Obj.getHeader()->e_machine, Sec.sh_type); getSectionTypeString(this->Obj.getHeader().e_machine, Sec.sh_type);
Fields[3].Str = Fields[3].Str =
to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8)); to_string(format_hex_no_prefix(Sec.sh_addr, ELFT::Is64Bits ? 16 : 8));
Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6)); Fields[4].Str = to_string(format_hex_no_prefix(Sec.sh_offset, 6));
Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6)); Fields[5].Str = to_string(format_hex_no_prefix(Sec.sh_size, 6));
Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2)); Fields[6].Str = to_string(format_hex_no_prefix(Sec.sh_entsize, 2));
Fields[7].Str = getGNUFlags(this->Obj.getHeader()->e_machine, Sec.sh_flags); Fields[7].Str = getGNUFlags(this->Obj.getHeader().e_machine, Sec.sh_flags);
Fields[8].Str = to_string(Sec.sh_link); Fields[8].Str = to_string(Sec.sh_link);
Fields[9].Str = to_string(Sec.sh_info); Fields[9].Str = to_string(Sec.sh_info);
Fields[10].Str = to_string(Sec.sh_addralign); Fields[10].Str = to_string(Sec.sh_addralign);
@ -3880,7 +3879,7 @@ template <class ELFT> void GNUStyle<ELFT>::printSectionHeaders() {
OS << "\n"; OS << "\n";
++SectionIndex; ++SectionIndex;
} }
printSectionDescription(OS, this->Obj.getHeader()->e_machine); printSectionDescription(OS, this->Obj.getHeader().e_machine);
} }
template <class ELFT> template <class ELFT>
@ -3918,7 +3917,7 @@ std::string GNUStyle<ELFT>::getSymbolSectionNdx(const Elf_Sym *Symbol,
return "COM"; return "COM";
case ELF::SHN_XINDEX: { case ELF::SHN_XINDEX: {
Expected<uint32_t> IndexOrErr = object::getExtendedSymbolTableIndex<ELFT>( Expected<uint32_t> IndexOrErr = object::getExtendedSymbolTableIndex<ELFT>(
Symbol, FirstSym, this->dumper()->getShndxTable()); *Symbol, *FirstSym, this->dumper()->getShndxTable());
if (!IndexOrErr) { if (!IndexOrErr) {
assert(Symbol->st_shndx == SHN_XINDEX && assert(Symbol->st_shndx == SHN_XINDEX &&
"getSymbolSectionIndex should only fail due to an invalid " "getSymbolSectionIndex should only fail due to an invalid "
@ -3961,7 +3960,7 @@ void GNUStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
Fields[2].Str = to_string(format_decimal(Symbol->st_size, 5)); 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 && if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS) SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes)); Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
else else
@ -4000,7 +3999,7 @@ void GNUStyle<ELFT>::printHashedSymbol(const Elf_Sym *FirstSym, uint32_t Sym,
Fields[3].Str = to_string(format_decimal(Symbol->st_size, 5)); Fields[3].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 && if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS) SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes)); Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
else else
@ -4227,14 +4226,14 @@ void GNUStyle<ELFT>::printProgramHeaders(
template <class ELFT> void GNUStyle<ELFT>::printProgramHeaders() { template <class ELFT> void GNUStyle<ELFT>::printProgramHeaders() {
unsigned Bias = ELFT::Is64Bits ? 8 : 0; unsigned Bias = ELFT::Is64Bits ? 8 : 0;
const Elf_Ehdr *Header = this->Obj.getHeader(); const Elf_Ehdr &Header = this->Obj.getHeader();
Field Fields[8] = {2, 17, 26, 37 + Bias, Field Fields[8] = {2, 17, 26, 37 + Bias,
48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias}; 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
OS << "\nElf file type is " OS << "\nElf file type is "
<< printEnum(Header->e_type, makeArrayRef(ElfObjectFileType)) << "\n" << printEnum(Header.e_type, makeArrayRef(ElfObjectFileType)) << "\n"
<< "Entry point " << format_hex(Header->e_entry, 3) << "\n" << "Entry point " << format_hex(Header.e_entry, 3) << "\n"
<< "There are " << Header->e_phnum << " program headers," << "There are " << Header.e_phnum << " program headers,"
<< " starting at offset " << Header->e_phoff << "\n\n" << " starting at offset " << Header.e_phoff << "\n\n"
<< "Program Headers:\n"; << "Program Headers:\n";
if (ELFT::Is64Bits) if (ELFT::Is64Bits)
OS << " Type Offset VirtAddr PhysAddr " OS << " Type Offset VirtAddr PhysAddr "
@ -4254,7 +4253,7 @@ template <class ELFT> void GNUStyle<ELFT>::printProgramHeaders() {
} }
for (const Elf_Phdr &Phdr : *PhdrsOrErr) { for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
Fields[0].Str = getGNUPtType(Header->e_machine, Phdr.p_type); Fields[0].Str = getGNUPtType(Header.e_machine, Phdr.p_type);
Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8)); Fields[1].Str = to_string(format_hex(Phdr.p_offset, 8));
Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width)); Fields[2].Str = to_string(format_hex(Phdr.p_vaddr, Width));
Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width)); Fields[3].Str = to_string(format_hex(Phdr.p_paddr, Width));
@ -4322,8 +4321,7 @@ template <class ELFT> void GNUStyle<ELFT>::printSectionMapping() {
if (checkTLSSections<ELFT>(Phdr, Sec) && checkOffsets<ELFT>(Phdr, Sec) && if (checkTLSSections<ELFT>(Phdr, Sec) && checkOffsets<ELFT>(Phdr, Sec) &&
checkVMA<ELFT>(Phdr, Sec) && checkPTDynamic<ELFT>(Phdr, Sec)) { checkVMA<ELFT>(Phdr, Sec) && checkPTDynamic<ELFT>(Phdr, Sec)) {
Sections += Sections +=
unwrapOrError(this->FileName, this->Obj.getSectionName(&Sec)) unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
.str() +
" "; " ";
BelongsToSegment.insert(&Sec); BelongsToSegment.insert(&Sec);
} }
@ -4337,7 +4335,7 @@ template <class ELFT> void GNUStyle<ELFT>::printSectionMapping() {
for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) { for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
if (BelongsToSegment.find(&Sec) == BelongsToSegment.end()) if (BelongsToSegment.find(&Sec) == BelongsToSegment.end())
Sections += Sections +=
unwrapOrError(this->FileName, this->Obj.getSectionName(&Sec)).str() + unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)).str() +
' '; ' ';
} }
if (!Sections.empty()) { if (!Sections.empty()) {
@ -4478,7 +4476,7 @@ template <class ELFT>
void GNUStyle<ELFT>::printGNUVersionSectionProlog( void GNUStyle<ELFT>::printGNUVersionSectionProlog(
const typename ELFT::Shdr *Sec, const Twine &Label, unsigned EntriesNum) { const typename ELFT::Shdr *Sec, const Twine &Label, unsigned EntriesNum) {
StringRef SecName = StringRef SecName =
unwrapOrError(this->FileName, this->Obj.getSectionName(Sec)); unwrapOrError(this->FileName, this->Obj.getSectionName(*Sec));
OS << Label << " section '" << SecName << "' " OS << Label << " section '" << SecName << "' "
<< "contains " << EntriesNum << " entries:\n"; << "contains " << EntriesNum << " entries:\n";
@ -4487,7 +4485,7 @@ void GNUStyle<ELFT>::printGNUVersionSectionProlog(
this->Obj.getSection(Sec->sh_link); this->Obj.getSection(Sec->sh_link);
if (SymTabOrErr) if (SymTabOrErr)
SymTabName = SymTabName =
unwrapOrError(this->FileName, this->Obj.getSectionName(*SymTabOrErr)); unwrapOrError(this->FileName, this->Obj.getSectionName(**SymTabOrErr));
else else
this->reportUniqueWarning(createError("invalid section linked to " + this->reportUniqueWarning(createError("invalid section linked to " +
describe(this->Obj, *Sec) + ": " + describe(this->Obj, *Sec) + ": " +
@ -5273,7 +5271,7 @@ template <class ELFT> void GNUStyle<ELFT>::printNotes() {
<< format_hex(Descriptor.size(), 10) << '\t'; << format_hex(Descriptor.size(), 10) << '\t';
StringRef NoteType = StringRef NoteType =
getNoteTypeName<ELFT>(Note, this->Obj.getHeader()->e_type); getNoteTypeName<ELFT>(Note, this->Obj.getHeader().e_type);
if (!NoteType.empty()) if (!NoteType.empty())
OS << NoteType << '\n'; OS << NoteType << '\n';
else else
@ -5311,11 +5309,11 @@ template <class ELFT> void GNUStyle<ELFT>::printNotes() {
}; };
ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections()); ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
if (this->Obj.getHeader()->e_type != ELF::ET_CORE && !Sections.empty()) { if (this->Obj.getHeader().e_type != ELF::ET_CORE && !Sections.empty()) {
for (const auto &S : Sections) { for (const Elf_Shdr &S : Sections) {
if (S.sh_type != SHT_NOTE) if (S.sh_type != SHT_NOTE)
continue; continue;
PrintHeader(expectedToOptional(this->Obj.getSectionName(&S)), S.sh_offset, PrintHeader(expectedToOptional(this->Obj.getSectionName(S)), S.sh_offset,
S.sh_size); S.sh_size);
Error Err = Error::success(); Error Err = Error::success();
for (auto Note : this->Obj.notes(S, Err)) for (auto Note : this->Obj.notes(S, Err))
@ -5367,7 +5365,7 @@ void DumpStyle<ELFT>::printDependentLibsHelper(
OnSectionStart(Shdr); OnSectionStart(Shdr);
Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj.getSectionContents(&Shdr); Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj.getSectionContents(Shdr);
if (!ContentsOrErr) { if (!ContentsOrErr) {
Warn(I, toString(ContentsOrErr.takeError())); Warn(I, toString(ContentsOrErr.takeError()));
continue; continue;
@ -5412,7 +5410,7 @@ void DumpStyle<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
const bool IsMips64EL = this->Obj.isMips64EL(); const bool IsMips64EL = this->Obj.isMips64EL();
switch (Sec.sh_type) { switch (Sec.sh_type) {
case ELF::SHT_REL: case ELF::SHT_REL:
if (Expected<Elf_Rel_Range> RangeOrErr = Obj.rels(&Sec)) { if (Expected<Elf_Rel_Range> RangeOrErr = Obj.rels(Sec)) {
for (const Elf_Rel &R : *RangeOrErr) for (const Elf_Rel &R : *RangeOrErr)
printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab); printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab);
} else { } else {
@ -5420,7 +5418,7 @@ void DumpStyle<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
} }
break; break;
case ELF::SHT_RELA: case ELF::SHT_RELA:
if (Expected<Elf_Rela_Range> RangeOrErr = Obj.relas(&Sec)) { if (Expected<Elf_Rela_Range> RangeOrErr = Obj.relas(Sec)) {
for (const Elf_Rela &R : *RangeOrErr) for (const Elf_Rela &R : *RangeOrErr)
printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab); printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab);
} else { } else {
@ -5429,7 +5427,7 @@ void DumpStyle<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
break; break;
case ELF::SHT_RELR: case ELF::SHT_RELR:
case ELF::SHT_ANDROID_RELR: { case ELF::SHT_ANDROID_RELR: {
Expected<Elf_Relr_Range> RangeOrErr = Obj.relrs(&Sec); Expected<Elf_Relr_Range> RangeOrErr = Obj.relrs(Sec);
if (!RangeOrErr) { if (!RangeOrErr) {
Warn(RangeOrErr.takeError()); Warn(RangeOrErr.takeError());
break; break;
@ -5447,7 +5445,7 @@ void DumpStyle<ELFT>::printRelocationsHelper(const Elf_Shdr &Sec) {
} }
case ELF::SHT_ANDROID_REL: case ELF::SHT_ANDROID_REL:
case ELF::SHT_ANDROID_RELA: case ELF::SHT_ANDROID_RELA:
if (Expected<std::vector<Elf_Rela>> RelasOrErr = Obj.android_relas(&Sec)) { if (Expected<std::vector<Elf_Rela>> RelasOrErr = Obj.android_relas(Sec)) {
for (const Elf_Rela &R : *RelasOrErr) for (const Elf_Rela &R : *RelasOrErr)
printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab); printReloc(Relocation<ELFT>(R, IsMips64EL), ++RelNdx, Sec, SymTab);
} else { } else {
@ -5461,7 +5459,7 @@ template <class ELFT>
StringRef DumpStyle<ELFT>::getPrintableSectionName(const Elf_Shdr &Sec) const { StringRef DumpStyle<ELFT>::getPrintableSectionName(const Elf_Shdr &Sec) const {
StringRef Name = "<?>"; StringRef Name = "<?>";
if (Expected<StringRef> SecNameOrErr = if (Expected<StringRef> SecNameOrErr =
Obj.getSectionName(&Sec, this->dumper()->WarningHandler)) Obj.getSectionName(Sec, this->dumper()->WarningHandler))
Name = *SecNameOrErr; Name = *SecNameOrErr;
else else
this->reportUniqueWarning(createError("unable to get the name of " + this->reportUniqueWarning(createError("unable to get the name of " +
@ -5659,7 +5657,7 @@ void DumpStyle<ELFT>::printNonRelocatableStackSizes(
PrintHeader(); PrintHeader();
const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl()); const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
ArrayRef<uint8_t> Contents = ArrayRef<uint8_t> Contents =
unwrapOrError(this->FileName, EF->getSectionContents(ElfSec)); unwrapOrError(this->FileName, EF->getSectionContents(*ElfSec));
DataExtractor Data(Contents, Obj->isLittleEndian(), sizeof(Elf_Addr)); DataExtractor Data(Contents, Obj->isLittleEndian(), sizeof(Elf_Addr));
uint64_t Offset = 0; uint64_t Offset = 0;
while (Offset < Contents.size()) { while (Offset < Contents.size()) {
@ -5724,7 +5722,7 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
const Elf_Shdr *ContentsSec = const Elf_Shdr *ContentsSec =
Obj->getSection((*RelSecOrErr)->getRawDataRefImpl()); Obj->getSection((*RelSecOrErr)->getRawDataRefImpl());
Expected<StringRef> ContentsSectionNameOrErr = Expected<StringRef> ContentsSectionNameOrErr =
EF->getSectionName(ContentsSec); EF->getSectionName(*ContentsSec);
if (!ContentsSectionNameOrErr) { if (!ContentsSectionNameOrErr) {
consumeError(ContentsSectionNameOrErr.takeError()); consumeError(ContentsSectionNameOrErr.takeError());
continue; continue;
@ -5936,7 +5934,7 @@ getMipsAbiFlagsSection(const ELFObjectFile<ELFT> *ObjF,
const ELFFile<ELFT> *Obj = ObjF->getELFFile(); const ELFFile<ELFT> *Obj = ObjF->getELFFile();
constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: "; constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: ";
Expected<ArrayRef<uint8_t>> DataOrErr = Obj->getSectionContents(Sec); Expected<ArrayRef<uint8_t>> DataOrErr = Obj->getSectionContents(*Sec);
if (!DataOrErr) if (!DataOrErr)
return createError(ErrPrefix + toString(DataOrErr.takeError())); return createError(ErrPrefix + toString(DataOrErr.takeError()));
@ -5981,21 +5979,21 @@ void GNUStyle<ELFT>::printMipsABIFlags(const ELFObjectFile<ELFT> *ObjF) {
} }
template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders() { template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders() {
const Elf_Ehdr *E = this->Obj.getHeader(); const Elf_Ehdr &E = this->Obj.getHeader();
{ {
DictScope D(W, "ElfHeader"); DictScope D(W, "ElfHeader");
{ {
DictScope D(W, "Ident"); DictScope D(W, "Ident");
W.printBinary("Magic", makeArrayRef(E->e_ident).slice(ELF::EI_MAG0, 4)); W.printBinary("Magic", makeArrayRef(E.e_ident).slice(ELF::EI_MAG0, 4));
W.printEnum("Class", E->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass)); W.printEnum("Class", E.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
W.printEnum("DataEncoding", E->e_ident[ELF::EI_DATA], W.printEnum("DataEncoding", E.e_ident[ELF::EI_DATA],
makeArrayRef(ElfDataEncoding)); makeArrayRef(ElfDataEncoding));
W.printNumber("FileVersion", E->e_ident[ELF::EI_VERSION]); W.printNumber("FileVersion", E.e_ident[ELF::EI_VERSION]);
auto OSABI = makeArrayRef(ElfOSABI); auto OSABI = makeArrayRef(ElfOSABI);
if (E->e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH && if (E.e_ident[ELF::EI_OSABI] >= ELF::ELFOSABI_FIRST_ARCH &&
E->e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) { E.e_ident[ELF::EI_OSABI] <= ELF::ELFOSABI_LAST_ARCH) {
switch (E->e_machine) { switch (E.e_machine) {
case ELF::EM_AMDGPU: case ELF::EM_AMDGPU:
OSABI = makeArrayRef(AMDGPUElfOSABI); OSABI = makeArrayRef(AMDGPUElfOSABI);
break; break;
@ -6007,32 +6005,32 @@ template <class ELFT> void LLVMStyle<ELFT>::printFileHeaders() {
break; break;
} }
} }
W.printEnum("OS/ABI", E->e_ident[ELF::EI_OSABI], OSABI); W.printEnum("OS/ABI", E.e_ident[ELF::EI_OSABI], OSABI);
W.printNumber("ABIVersion", E->e_ident[ELF::EI_ABIVERSION]); W.printNumber("ABIVersion", E.e_ident[ELF::EI_ABIVERSION]);
W.printBinary("Unused", makeArrayRef(E->e_ident).slice(ELF::EI_PAD)); W.printBinary("Unused", makeArrayRef(E.e_ident).slice(ELF::EI_PAD));
} }
W.printEnum("Type", E->e_type, makeArrayRef(ElfObjectFileType)); W.printEnum("Type", E.e_type, makeArrayRef(ElfObjectFileType));
W.printEnum("Machine", E->e_machine, makeArrayRef(ElfMachineType)); W.printEnum("Machine", E.e_machine, makeArrayRef(ElfMachineType));
W.printNumber("Version", E->e_version); W.printNumber("Version", E.e_version);
W.printHex("Entry", E->e_entry); W.printHex("Entry", E.e_entry);
W.printHex("ProgramHeaderOffset", E->e_phoff); W.printHex("ProgramHeaderOffset", E.e_phoff);
W.printHex("SectionHeaderOffset", E->e_shoff); W.printHex("SectionHeaderOffset", E.e_shoff);
if (E->e_machine == EM_MIPS) if (E.e_machine == EM_MIPS)
W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderMipsFlags), W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderMipsFlags),
unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI), unsigned(ELF::EF_MIPS_ARCH), unsigned(ELF::EF_MIPS_ABI),
unsigned(ELF::EF_MIPS_MACH)); unsigned(ELF::EF_MIPS_MACH));
else if (E->e_machine == EM_AMDGPU) else if (E.e_machine == EM_AMDGPU)
W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderAMDGPUFlags), W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderAMDGPUFlags),
unsigned(ELF::EF_AMDGPU_MACH)); unsigned(ELF::EF_AMDGPU_MACH));
else if (E->e_machine == EM_RISCV) else if (E.e_machine == EM_RISCV)
W.printFlags("Flags", E->e_flags, makeArrayRef(ElfHeaderRISCVFlags)); W.printFlags("Flags", E.e_flags, makeArrayRef(ElfHeaderRISCVFlags));
else else
W.printFlags("Flags", E->e_flags); W.printFlags("Flags", E.e_flags);
W.printNumber("HeaderSize", E->e_ehsize); W.printNumber("HeaderSize", E.e_ehsize);
W.printNumber("ProgramHeaderEntrySize", E->e_phentsize); W.printNumber("ProgramHeaderEntrySize", E.e_phentsize);
W.printNumber("ProgramHeaderCount", E->e_phnum); W.printNumber("ProgramHeaderCount", E.e_phnum);
W.printNumber("SectionHeaderEntrySize", E->e_shentsize); W.printNumber("SectionHeaderEntrySize", E.e_shentsize);
W.printString("SectionHeaderCount", W.printString("SectionHeaderCount",
getSectionHeadersNumString(this->Obj, this->FileName)); getSectionHeadersNumString(this->Obj, this->FileName));
W.printString("StringTableSectionIndex", W.printString("StringTableSectionIndex",
@ -6133,13 +6131,13 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
int SectionIndex = -1; int SectionIndex = -1;
std::vector<EnumEntry<unsigned>> FlagsList = std::vector<EnumEntry<unsigned>> FlagsList =
getSectionFlagsForTarget(this->Obj.getHeader()->e_machine); getSectionFlagsForTarget(this->Obj.getHeader().e_machine);
for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) { for (const Elf_Shdr &Sec : cantFail(this->Obj.sections())) {
DictScope SectionD(W, "Section"); DictScope SectionD(W, "Section");
W.printNumber("Index", ++SectionIndex); W.printNumber("Index", ++SectionIndex);
W.printNumber("Name", this->getPrintableSectionName(Sec), Sec.sh_name); W.printNumber("Name", this->getPrintableSectionName(Sec), Sec.sh_name);
W.printHex("Type", W.printHex("Type",
object::getELFSectionTypeName(this->Obj.getHeader()->e_machine, object::getELFSectionTypeName(this->Obj.getHeader().e_machine,
Sec.sh_type), Sec.sh_type),
Sec.sh_type); Sec.sh_type);
W.printFlags("Flags", Sec.sh_flags, makeArrayRef(FlagsList)); W.printFlags("Flags", Sec.sh_flags, makeArrayRef(FlagsList));
@ -6167,7 +6165,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
const Elf_Shdr *SymSec = const Elf_Shdr *SymSec =
unwrapOrError(this->FileName, unwrapOrError(this->FileName,
this->Obj.getSection( this->Obj.getSection(
&Sym, Symtab, this->dumper()->getShndxTable())); Sym, Symtab, this->dumper()->getShndxTable()));
if (SymSec == &Sec) if (SymSec == &Sec)
printSymbol(&Sym, printSymbol(&Sym,
unwrapOrError(this->FileName, this->Obj.symbols(Symtab)) unwrapOrError(this->FileName, this->Obj.symbols(Symtab))
@ -6179,7 +6177,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printSectionHeaders() {
if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) { if (opts::SectionData && Sec.sh_type != ELF::SHT_NOBITS) {
ArrayRef<uint8_t> Data = ArrayRef<uint8_t> Data =
unwrapOrError(this->FileName, this->Obj.getSectionContents(&Sec)); unwrapOrError(this->FileName, this->Obj.getSectionContents(Sec));
W.printBinaryBlock( W.printBinaryBlock(
"SectionData", "SectionData",
StringRef(reinterpret_cast<const char *>(Data.data()), Data.size())); StringRef(reinterpret_cast<const char *>(Data.data()), Data.size()));
@ -6229,7 +6227,7 @@ void LLVMStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
W.printHex("Value", Symbol->st_value); W.printHex("Value", Symbol->st_value);
W.printNumber("Size", Symbol->st_size); W.printNumber("Size", Symbol->st_size);
W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings)); W.printEnum("Binding", Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
if (this->Obj.getHeader()->e_machine == ELF::EM_AMDGPU && if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS) SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes)); W.printEnum("Type", SymbolType, makeArrayRef(AMDGPUSymbolTypes));
else else
@ -6241,7 +6239,7 @@ void LLVMStyle<ELFT>::printSymbol(const Elf_Sym *Symbol, const Elf_Sym *First,
else { else {
std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags), std::vector<EnumEntry<unsigned>> SymOtherFlags(std::begin(ElfSymOtherFlags),
std::end(ElfSymOtherFlags)); std::end(ElfSymOtherFlags));
if (this->Obj.getHeader()->e_machine == EM_MIPS) { if (this->Obj.getHeader().e_machine == EM_MIPS) {
// Someones in their infinite wisdom decided to make STO_MIPS_MIPS16 // Someones in their infinite wisdom decided to make STO_MIPS_MIPS16
// flag overlapped with other ST_MIPS_xxx flags. So consider both // flag overlapped with other ST_MIPS_xxx flags. So consider both
// cases separately. // cases separately.
@ -6342,7 +6340,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printProgramHeaders() {
for (const Elf_Phdr &Phdr : *PhdrsOrErr) { for (const Elf_Phdr &Phdr : *PhdrsOrErr) {
DictScope P(W, "ProgramHeader"); DictScope P(W, "ProgramHeader");
StringRef Type = StringRef Type =
segmentTypeToString(this->Obj.getHeader()->e_machine, Phdr.p_type); segmentTypeToString(this->Obj.getHeader().e_machine, Phdr.p_type);
W.printHex("Type", Type.empty() ? "Unknown" : Type, Phdr.p_type); W.printHex("Type", Type.empty() ? "Unknown" : Type, Phdr.p_type);
W.printHex("Offset", Phdr.p_offset); W.printHex("Offset", Phdr.p_offset);
@ -6452,7 +6450,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printCGProfile() {
Expected<ArrayRef<Elf_CGProfile>> CGProfileOrErr = Expected<ArrayRef<Elf_CGProfile>> CGProfileOrErr =
this->Obj.template getSectionContentsAsArray<Elf_CGProfile>( this->Obj.template getSectionContentsAsArray<Elf_CGProfile>(
this->dumper()->getDotCGProfileSec()); *this->dumper()->getDotCGProfileSec());
if (!CGProfileOrErr) { if (!CGProfileOrErr) {
this->reportUniqueWarning( this->reportUniqueWarning(
createError("unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: " + createError("unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: " +
@ -6491,7 +6489,8 @@ template <class ELFT> void LLVMStyle<ELFT>::printAddrsig() {
if (!Sec) if (!Sec)
return; return;
Expected<ArrayRef<uint8_t>> ContentsOrErr = this->Obj.getSectionContents(Sec); Expected<ArrayRef<uint8_t>> ContentsOrErr =
this->Obj.getSectionContents(*Sec);
if (!ContentsOrErr) { if (!ContentsOrErr) {
this->reportUniqueWarning(ContentsOrErr.takeError()); this->reportUniqueWarning(ContentsOrErr.takeError());
return; return;
@ -6573,7 +6572,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printNotes() {
W.printHex("Data size", Descriptor.size()); W.printHex("Data size", Descriptor.size());
StringRef NoteType = StringRef NoteType =
getNoteTypeName<ELFT>(Note, this->Obj.getHeader()->e_type); getNoteTypeName<ELFT>(Note, this->Obj.getHeader().e_type);
if (!NoteType.empty()) if (!NoteType.empty())
W.printString("Type", NoteType); W.printString("Type", NoteType);
else else
@ -6609,12 +6608,12 @@ template <class ELFT> void LLVMStyle<ELFT>::printNotes() {
}; };
ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections()); ArrayRef<Elf_Shdr> Sections = cantFail(this->Obj.sections());
if (this->Obj.getHeader()->e_type != ELF::ET_CORE && !Sections.empty()) { if (this->Obj.getHeader().e_type != ELF::ET_CORE && !Sections.empty()) {
for (const auto &S : Sections) { for (const Elf_Shdr &S : Sections) {
if (S.sh_type != SHT_NOTE) if (S.sh_type != SHT_NOTE)
continue; continue;
DictScope D(W, "NoteSection"); DictScope D(W, "NoteSection");
PrintHeader(expectedToOptional(this->Obj.getSectionName(&S)), S.sh_offset, PrintHeader(expectedToOptional(this->Obj.getSectionName(S)), S.sh_offset,
S.sh_size); S.sh_size);
Error Err = Error::success(); Error Err = Error::success();
for (auto Note : this->Obj.notes(S, Err)) for (auto Note : this->Obj.notes(S, Err))
@ -6655,7 +6654,7 @@ template <class ELFT> void LLVMStyle<ELFT>::printELFLinkerOptions() {
continue; continue;
Expected<ArrayRef<uint8_t>> ContentsOrErr = Expected<ArrayRef<uint8_t>> ContentsOrErr =
this->Obj.getSectionContents(&Shdr); this->Obj.getSectionContents(Shdr);
if (!ContentsOrErr) { if (!ContentsOrErr) {
this->reportUniqueWarning( this->reportUniqueWarning(
createError("unable to read the content of the " createError("unable to read the content of the "

View File

@ -124,7 +124,7 @@ ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr *Sec) {
if (!SectionNames[SecIndex].empty()) if (!SectionNames[SecIndex].empty())
return SectionNames[SecIndex]; return SectionNames[SecIndex];
auto NameOrErr = Obj.getSectionName(Sec); auto NameOrErr = Obj.getSectionName(*Sec);
if (!NameOrErr) if (!NameOrErr)
return NameOrErr; return NameOrErr;
StringRef Name = *NameOrErr; StringRef Name = *NameOrErr;
@ -153,7 +153,7 @@ ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable,
return SymbolNameOrErr; return SymbolNameOrErr;
StringRef Name = *SymbolNameOrErr; StringRef Name = *SymbolNameOrErr;
if (Name.empty() && Sym->getType() == ELF::STT_SECTION) { if (Name.empty() && Sym->getType() == ELF::STT_SECTION) {
auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTable);
if (!ShdrOrErr) if (!ShdrOrErr)
return ShdrOrErr.takeError(); return ShdrOrErr.takeError();
return getUniquedSectionName(*ShdrOrErr); return getUniquedSectionName(*ShdrOrErr);
@ -235,14 +235,14 @@ template <class ELFT> Expected<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
// Dump header. We do not dump EPh* and ESh* fields. When not explicitly set, // Dump header. We do not dump EPh* and ESh* fields. When not explicitly set,
// the values are set by yaml2obj automatically and there is no need to dump // the values are set by yaml2obj automatically and there is no need to dump
// them here. // them here.
Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass()); Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader().getFileClass());
Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding()); Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader().getDataEncoding());
Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI]; Y->Header.OSABI = Obj.getHeader().e_ident[ELF::EI_OSABI];
Y->Header.ABIVersion = Obj.getHeader()->e_ident[ELF::EI_ABIVERSION]; Y->Header.ABIVersion = Obj.getHeader().e_ident[ELF::EI_ABIVERSION];
Y->Header.Type = Obj.getHeader()->e_type; Y->Header.Type = Obj.getHeader().e_type;
Y->Header.Machine = ELFYAML::ELF_EM(Obj.getHeader()->e_machine); Y->Header.Machine = ELFYAML::ELF_EM(Obj.getHeader().e_machine);
Y->Header.Flags = Obj.getHeader()->e_flags; Y->Header.Flags = Obj.getHeader().e_flags;
Y->Header.Entry = Obj.getHeader()->e_entry; Y->Header.Entry = Obj.getHeader().e_entry;
// Dump sections // Dump sections
auto SectionsOrErr = Obj.sections(); auto SectionsOrErr = Obj.sections();
@ -588,7 +588,7 @@ Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
return Error::success(); return Error::success();
} }
auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTable);
if (!ShdrOrErr) if (!ShdrOrErr)
return ShdrOrErr.takeError(); return ShdrOrErr.takeError();
const Elf_Shdr *Shdr = *ShdrOrErr; const Elf_Shdr *Shdr = *ShdrOrErr;
@ -611,7 +611,7 @@ Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
R.Offset = Rel->r_offset; R.Offset = Rel->r_offset;
R.Addend = 0; R.Addend = 0;
auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab); auto SymOrErr = Obj.getRelocationSymbol(*Rel, SymTab);
if (!SymOrErr) if (!SymOrErr)
return SymOrErr.takeError(); return SymOrErr.takeError();
@ -624,7 +624,7 @@ Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
auto StrTabSec = Obj.getSection(SymTab->sh_link); auto StrTabSec = Obj.getSection(SymTab->sh_link);
if (!StrTabSec) if (!StrTabSec)
return StrTabSec.takeError(); return StrTabSec.takeError();
auto StrTabOrErr = Obj.getStringTable(*StrTabSec); auto StrTabOrErr = Obj.getStringTable(**StrTabSec);
if (!StrTabOrErr) if (!StrTabOrErr)
return StrTabOrErr.takeError(); return StrTabOrErr.takeError();
@ -725,7 +725,7 @@ ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -758,7 +758,7 @@ ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -799,7 +799,7 @@ ELFDumper<ELFT>::dumpLinkerOptionsSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -830,7 +830,7 @@ ELFDumper<ELFT>::dumpDependentLibrariesSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *DL)) if (Error E = dumpCommonSection(Shdr, *DL))
return std::move(E); return std::move(E);
Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -857,7 +857,7 @@ ELFDumper<ELFT>::dumpCallGraphProfileSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
ArrayRef<uint8_t> Content = *ContentOrErr; ArrayRef<uint8_t> Content = *ContentOrErr;
@ -913,7 +913,7 @@ ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(Shdr); auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(*Shdr);
if (!DynTagsOrErr) if (!DynTagsOrErr)
return DynTagsOrErr.takeError(); return DynTagsOrErr.takeError();
@ -936,7 +936,7 @@ ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
const Elf_Shdr *SymTab = *SymTabOrErr; const Elf_Shdr *SymTab = *SymTabOrErr;
if (Shdr->sh_type == ELF::SHT_REL) { if (Shdr->sh_type == ELF::SHT_REL) {
auto Rels = Obj.rels(Shdr); auto Rels = Obj.rels(*Shdr);
if (!Rels) if (!Rels)
return Rels.takeError(); return Rels.takeError();
for (const Elf_Rel &Rel : *Rels) { for (const Elf_Rel &Rel : *Rels) {
@ -946,7 +946,7 @@ ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
S->Relocations.push_back(R); S->Relocations.push_back(R);
} }
} else { } else {
auto Rels = Obj.relas(Shdr); auto Rels = Obj.relas(*Shdr);
if (!Rels) if (!Rels)
return Rels.takeError(); return Rels.takeError();
for (const Elf_Rela &Rel : *Rels) { for (const Elf_Rela &Rel : *Rels) {
@ -968,7 +968,7 @@ ELFDumper<ELFT>::dumpRelrSection(const Elf_Shdr *Shdr) {
if (auto E = dumpCommonSection(Shdr, *S)) if (auto E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
if (Expected<ArrayRef<Elf_Relr>> Relrs = Obj.relrs(Shdr)) { if (Expected<ArrayRef<Elf_Relr>> Relrs = Obj.relrs(*Shdr)) {
S->Entries.emplace(); S->Entries.emplace();
for (Elf_Relr Rel : *Relrs) for (Elf_Relr Rel : *Relrs)
S->Entries->emplace_back(Rel); S->Entries->emplace_back(Rel);
@ -978,7 +978,7 @@ ELFDumper<ELFT>::dumpRelrSection(const Elf_Shdr *Shdr) {
consumeError(Relrs.takeError()); consumeError(Relrs.takeError());
} }
Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr); Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
S->Content = *ContentOrErr; S->Content = *ContentOrErr;
@ -994,7 +994,7 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
unsigned SecIndex = Shdr - &Sections[0]; unsigned SecIndex = Shdr - &Sections[0];
if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) { if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) {
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
ArrayRef<uint8_t> Content = *ContentOrErr; ArrayRef<uint8_t> Content = *ContentOrErr;
@ -1016,7 +1016,7 @@ ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr); auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr);
if (!EntriesOrErr) if (!EntriesOrErr)
return EntriesOrErr.takeError(); return EntriesOrErr.takeError();
for (const Elf_Word &E : *EntriesOrErr) for (const Elf_Word &E : *EntriesOrErr)
@ -1042,7 +1042,7 @@ ELFDumper<ELFT>::dumpNoteSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -1078,7 +1078,7 @@ ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -1119,7 +1119,7 @@ ELFDumper<ELFT>::dumpGnuHashSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();
@ -1179,11 +1179,11 @@ ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) {
if (!StringTableShdrOrErr) if (!StringTableShdrOrErr)
return StringTableShdrOrErr.takeError(); return StringTableShdrOrErr.takeError();
auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr);
if (!StringTableOrErr) if (!StringTableOrErr)
return StringTableOrErr.takeError(); return StringTableOrErr.takeError();
auto Contents = Obj.getSectionContents(Shdr); auto Contents = Obj.getSectionContents(*Shdr);
if (!Contents) if (!Contents)
return Contents.takeError(); return Contents.takeError();
@ -1224,7 +1224,7 @@ ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(Shdr); auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(*Shdr);
if (!VersionsOrErr) if (!VersionsOrErr)
return VersionsOrErr.takeError(); return VersionsOrErr.takeError();
for (const Elf_Half &E : *VersionsOrErr) for (const Elf_Half &E : *VersionsOrErr)
@ -1245,7 +1245,7 @@ ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) {
S->Info = Shdr->sh_info; S->Info = Shdr->sh_info;
auto Contents = Obj.getSectionContents(Shdr); auto Contents = Obj.getSectionContents(*Shdr);
if (!Contents) if (!Contents)
return Contents.takeError(); return Contents.takeError();
@ -1253,7 +1253,7 @@ ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) {
if (!StringTableShdrOrErr) if (!StringTableShdrOrErr)
return StringTableShdrOrErr.takeError(); return StringTableShdrOrErr.takeError();
auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr);
if (!StringTableOrErr) if (!StringTableOrErr)
return StringTableOrErr.takeError(); return StringTableOrErr.takeError();
@ -1322,7 +1322,7 @@ Expected<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
return SymbolName.takeError(); return SymbolName.takeError();
S->Signature = *SymbolName; S->Signature = *SymbolName;
auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr); auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr);
if (!MembersOrErr) if (!MembersOrErr)
return MembersOrErr.takeError(); return MembersOrErr.takeError();
@ -1352,7 +1352,7 @@ ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
if (Error E = dumpCommonSection(Shdr, *S)) if (Error E = dumpCommonSection(Shdr, *S))
return std::move(E); return std::move(E);
auto ContentOrErr = Obj.getSectionContents(Shdr); auto ContentOrErr = Obj.getSectionContents(*Shdr);
if (!ContentOrErr) if (!ContentOrErr)
return ContentOrErr.takeError(); return ContentOrErr.takeError();