From 63830bcae99c09c3d983368568adc80c9c211455 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Wed, 16 Nov 2016 05:10:28 +0000 Subject: [PATCH] [ELF] Convert ELF.h to Expected. This has two advantages: 1) We slowly move away from ErrorOr to the new handling interface, in the hope of having an uniform error handling in LLVM, eventually. 2) We're starting to have *meaningful* error messages for invalid object ELF files, rather than a generic "parse error". At some point we should include also the offset to improve the quality of the diagnostic. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287081 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 247 ++++++++++++++++++----------------- include/llvm/Object/ELFObjectFile.h | 116 ++++++++++------ test/Object/corrupt.test | 8 +- test/Object/invalid.test | 18 +-- tools/llvm-objdump/ELFDump.cpp | 5 +- tools/llvm-objdump/llvm-objdump.cpp | 30 ++--- tools/llvm-readobj/ARMEHABIPrinter.h | 25 ++-- tools/obj2yaml/elf2yaml.cpp | 142 ++++++++++---------- 8 files changed, 318 insertions(+), 273 deletions(-) diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index a7fc19f0aa7..aaa79ae70f0 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -33,6 +33,10 @@ getElfArchType(StringRef Object) { (uint8_t)Object[ELF::EI_DATA]); } +static inline Error createError(StringRef Err) { + return make_error(Err, object_error::parse_failed); +} + template class ELFFile { public: @@ -75,18 +79,18 @@ public: } template - ErrorOr getEntry(uint32_t Section, uint32_t Entry) const; + Expected getEntry(uint32_t Section, uint32_t Entry) const; template - ErrorOr getEntry(const Elf_Shdr *Section, uint32_t Entry) const; + Expected getEntry(const Elf_Shdr *Section, uint32_t Entry) const; - ErrorOr getStringTable(const Elf_Shdr *Section) const; - ErrorOr getStringTableForSymtab(const Elf_Shdr &Section) const; - ErrorOr getStringTableForSymtab(const Elf_Shdr &Section, - Elf_Shdr_Range Sections) const; + Expected getStringTable(const Elf_Shdr *Section) const; + Expected getStringTableForSymtab(const Elf_Shdr &Section) const; + Expected getStringTableForSymtab(const Elf_Shdr &Section, + Elf_Shdr_Range Sections) const; - ErrorOr> getSHNDXTable(const Elf_Shdr &Section) const; - ErrorOr> getSHNDXTable(const Elf_Shdr &Section, - Elf_Shdr_Range Sections) const; + Expected> getSHNDXTable(const Elf_Shdr &Section) const; + Expected> getSHNDXTable(const Elf_Shdr &Section, + Elf_Shdr_Range Sections) const; void VerifyStrTab(const Elf_Shdr *sh) const; @@ -95,8 +99,8 @@ public: SmallVectorImpl &Result) const; /// \brief Get the symbol for a given relocation. - ErrorOr getRelocationSymbol(const Elf_Rel *Rel, - const Elf_Shdr *SymTab) const; + Expected getRelocationSymbol(const Elf_Rel *Rel, + const Elf_Shdr *SymTab) const; ELFFile(StringRef Object); @@ -110,50 +114,51 @@ public: getHeader()->getDataEncoding() == ELF::ELFDATA2LSB; } - ErrorOr sections() const; + Expected sections() const; - ErrorOr symbols(const Elf_Shdr *Sec) const { + Expected symbols(const Elf_Shdr *Sec) const { if (!Sec) return makeArrayRef(nullptr, nullptr); return getSectionContentsAsArray(Sec); } - ErrorOr relas(const Elf_Shdr *Sec) const { + Expected relas(const Elf_Shdr *Sec) const { return getSectionContentsAsArray(Sec); } - ErrorOr rels(const Elf_Shdr *Sec) const { + Expected rels(const Elf_Shdr *Sec) const { return getSectionContentsAsArray(Sec); } /// \brief Iterate over program header table. - ErrorOr program_headers() const { + Expected program_headers() const { if (getHeader()->e_phnum && getHeader()->e_phentsize != sizeof(Elf_Phdr)) - return object_error::parse_failed; + return createError("invalid e_phentsize"); auto *Begin = reinterpret_cast(base() + getHeader()->e_phoff); return makeArrayRef(Begin, Begin + getHeader()->e_phnum); } - ErrorOr getSectionStringTable(Elf_Shdr_Range Sections) const; - ErrorOr getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, - ArrayRef ShndxTable) const; - ErrorOr getSection(const Elf_Sym *Sym, - const Elf_Shdr *SymTab, - ArrayRef ShndxTable) const; - ErrorOr getSection(const Elf_Sym *Sym, Elf_Sym_Range Symtab, - ArrayRef ShndxTable) const; - ErrorOr getSection(uint32_t Index) const; - - ErrorOr getSymbol(const Elf_Shdr *Sec, - uint32_t Index) const; - - ErrorOr getSectionName(const Elf_Shdr *Section) const; - ErrorOr getSectionName(const Elf_Shdr *Section, - StringRef DotShstrtab) const; + Expected getSectionStringTable(Elf_Shdr_Range Sections) const; + Expected getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, + ArrayRef ShndxTable) const; + Expected getSection(const Elf_Sym *Sym, + const Elf_Shdr *SymTab, + ArrayRef ShndxTable) const; + Expected getSection(const Elf_Sym *Sym, + Elf_Sym_Range Symtab, + ArrayRef ShndxTable) const; + Expected getSection(uint32_t Index) const; + + Expected getSymbol(const Elf_Shdr *Sec, + uint32_t Index) const; + + Expected getSectionName(const Elf_Shdr *Section) const; + Expected getSectionName(const Elf_Shdr *Section, + StringRef DotShstrtab) const; template - ErrorOr> getSectionContentsAsArray(const Elf_Shdr *Sec) const; - ErrorOr > getSectionContents(const Elf_Shdr *Sec) const; + Expected> getSectionContentsAsArray(const Elf_Shdr *Sec) const; + Expected> getSectionContents(const Elf_Shdr *Sec) const; }; typedef ELFFile> ELF32LEFile; @@ -162,36 +167,37 @@ typedef ELFFile> ELF32BEFile; typedef ELFFile> ELF64BEFile; template -inline ErrorOr +inline Expected getSection(typename ELFT::ShdrRange Sections, uint32_t Index) { if (Index >= Sections.size()) - return object_error::invalid_section_index; + return createError("invalid section index"); return &Sections[Index]; } template -inline ErrorOr +inline Expected getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym, const typename ELFT::Sym *FirstSym, ArrayRef ShndxTable) { assert(Sym->st_shndx == ELF::SHN_XINDEX); unsigned Index = Sym - FirstSym; if (Index >= ShndxTable.size()) - return object_error::parse_failed; + return createError("index past the end of the symbol table"); + // The size of the table was checked in getSHNDXTable. return ShndxTable[Index]; } template -ErrorOr +Expected ELFFile::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, ArrayRef ShndxTable) const { uint32_t Index = Sym->st_shndx; if (Index == ELF::SHN_XINDEX) { auto ErrorOrIndex = object::getExtendedSymbolTableIndex( Sym, Syms.begin(), ShndxTable); - if (std::error_code EC = ErrorOrIndex.getError()) - return EC; + if (!ErrorOrIndex) + return ErrorOrIndex.takeError(); return *ErrorOrIndex; } if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE) @@ -200,70 +206,70 @@ ELFFile::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, } template -ErrorOr +Expected ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef ShndxTable) const { auto SymsOrErr = symbols(SymTab); - if (std::error_code EC = SymsOrErr.getError()) - return EC; + if (!SymsOrErr) + return SymsOrErr.takeError(); return getSection(Sym, *SymsOrErr, ShndxTable); } template -ErrorOr +Expected ELFFile::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols, ArrayRef ShndxTable) const { - ErrorOr IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable); - if (std::error_code EC = IndexOrErr.getError()) - return EC; + auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable); + if (!IndexOrErr) + return IndexOrErr.takeError(); uint32_t Index = *IndexOrErr; if (Index == 0) return nullptr; auto SectionsOrErr = sections(); - if (std::error_code EC = SectionsOrErr.getError()) - return EC; + if (!SectionsOrErr) + return SectionsOrErr.takeError(); return object::getSection(*SectionsOrErr, Index); } template -inline ErrorOr +inline Expected getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) { if (Index >= Symbols.size()) - return object_error::invalid_symbol_index; + return createError("invalid symbol index"); return &Symbols[Index]; } template -ErrorOr +Expected ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { auto SymtabOrErr = symbols(Sec); - if (std::error_code EC = SymtabOrErr.getError()) - return EC; + if (!SymtabOrErr) + return SymtabOrErr.takeError(); return object::getSymbol(*SymtabOrErr, Index); } template template -ErrorOr> +Expected> ELFFile::getSectionContentsAsArray(const Elf_Shdr *Sec) const { if (Sec->sh_entsize != sizeof(T) && sizeof(T) != 1) - return object_error::parse_failed; + return createError("invalid sh_entsize"); uintX_t Offset = Sec->sh_offset; uintX_t Size = Sec->sh_size; if (Size % sizeof(T)) - return object_error::parse_failed; + return createError("size is not a multiple of sh_entsize"); if ((std::numeric_limits::max() - Offset < Size) || Offset + Size > Buf.size()) - return object_error::parse_failed; + return createError("invalid section offset"); const T *Start = reinterpret_cast(base() + Offset); return makeArrayRef(Start, Size / sizeof(T)); } template -ErrorOr> +Expected> ELFFile::getSectionContents(const Elf_Shdr *Sec) const { return getSectionContentsAsArray(Sec); } @@ -305,7 +311,7 @@ void ELFFile::getRelocationTypeName(uint32_t Type, } template -ErrorOr +Expected ELFFile::getRelocationSymbol(const Elf_Rel *Rel, const Elf_Shdr *SymTab) const { uint32_t Index = Rel->getSymbol(isMips64EL()); @@ -315,7 +321,7 @@ ELFFile::getRelocationSymbol(const Elf_Rel *Rel, } template -ErrorOr +Expected ELFFile::getSectionStringTable(Elf_Shdr_Range Sections) const { uint32_t Index = getHeader()->e_shstrndx; if (Index == ELF::SHN_XINDEX) @@ -324,7 +330,7 @@ ELFFile::getSectionStringTable(Elf_Shdr_Range Sections) const { if (!Index) // no section string table. return ""; if (Index >= Sections.size()) - return object_error::parse_failed; + return createError("invalid section index"); return getStringTable(&Sections[Index]); } @@ -339,24 +345,23 @@ static bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl *Phdr) { } template -ErrorOr ELFFile::sections() const { +Expected ELFFile::sections() const { const uintX_t SectionTableOffset = getHeader()->e_shoff; if (SectionTableOffset == 0) return ArrayRef(); - // Invalid section header entry size (e_shentsize) in ELF header if (getHeader()->e_shentsize != sizeof(Elf_Shdr)) - return object_error::parse_failed; + return createError( + "invalid section header entry size (e_shentsize) in ELF header"); const uint64_t FileSize = Buf.size(); - // Section header table goes past end of file! if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) - return object_error::parse_failed; + return createError("section header table goes past the end of the file"); // Invalid address alignment of section headers if (SectionTableOffset & (alignof(Elf_Shdr) - 1)) - return object_error::parse_failed; + return createError("invalid alignment of section headers"); const Elf_Shdr *First = reinterpret_cast(base() + SectionTableOffset); @@ -365,140 +370,138 @@ ErrorOr ELFFile::sections() const { if (NumSections == 0) NumSections = First->sh_size; - // Section table goes past end of file! if (NumSections > UINT64_MAX / sizeof(Elf_Shdr)) - return object_error::parse_failed; + return createError("section table goes past the end of file"); const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr); // Section table goes past end of file! if (SectionTableOffset + SectionTableSize > FileSize) - return object_error::parse_failed; + return createError("section table goes past the end of file"); return makeArrayRef(First, NumSections); } template template -ErrorOr ELFFile::getEntry(uint32_t Section, - uint32_t Entry) const { - ErrorOr Sec = getSection(Section); - if (std::error_code EC = Sec.getError()) - return EC; - return getEntry(*Sec, Entry); +Expected ELFFile::getEntry(uint32_t Section, + uint32_t Entry) const { + auto SecOrErr = getSection(Section); + if (!SecOrErr) + return SecOrErr.takeError(); + return getEntry(*SecOrErr, Entry); } template template -ErrorOr ELFFile::getEntry(const Elf_Shdr *Section, - uint32_t Entry) const { +Expected ELFFile::getEntry(const Elf_Shdr *Section, + uint32_t Entry) const { if (sizeof(T) != Section->sh_entsize) - return object_error::parse_failed; + return createError("invalid sh_entsize"); size_t Pos = Section->sh_offset + Entry * sizeof(T); if (Pos + sizeof(T) > Buf.size()) - return object_error::parse_failed; + return createError("invalid section offset"); return reinterpret_cast(base() + Pos); } template -ErrorOr +Expected ELFFile::getSection(uint32_t Index) const { auto TableOrErr = sections(); - if (std::error_code EC = TableOrErr.getError()) - return EC; + if (!TableOrErr) + return TableOrErr.takeError(); return object::getSection(*TableOrErr, Index); } template -ErrorOr +Expected ELFFile::getStringTable(const Elf_Shdr *Section) const { if (Section->sh_type != ELF::SHT_STRTAB) - return object_error::parse_failed; + return createError("invalid sh_type for string table, expected SHT_STRTAB"); auto V = getSectionContentsAsArray(Section); - if (std::error_code EC = V.getError()) - return EC; + if (!V) + return V.takeError(); ArrayRef Data = *V; if (Data.empty()) - return object_error::parse_failed; + return createError("empty string table"); if (Data.back() != '\0') - return object_error::string_table_non_null_end; + return createError("string table non-null terminated"); return StringRef(Data.begin(), Data.size()); } template -ErrorOr> +Expected> ELFFile::getSHNDXTable(const Elf_Shdr &Section) const { auto SectionsOrErr = sections(); - if (std::error_code EC = SectionsOrErr.getError()) - return EC; + if (!SectionsOrErr) + return SectionsOrErr.takeError(); return getSHNDXTable(Section, *SectionsOrErr); } template -ErrorOr> +Expected> ELFFile::getSHNDXTable(const Elf_Shdr &Section, Elf_Shdr_Range Sections) const { assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); auto VOrErr = getSectionContentsAsArray(&Section); - if (std::error_code EC = VOrErr.getError()) - return EC; + if (!VOrErr) + return VOrErr.takeError(); ArrayRef V = *VOrErr; - ErrorOr SymTableOrErr = - object::getSection(Sections, Section.sh_link); - if (std::error_code EC = SymTableOrErr.getError()) - return EC; + auto SymTableOrErr = object::getSection(Sections, Section.sh_link); + if (!SymTableOrErr) + return SymTableOrErr.takeError(); const Elf_Shdr &SymTable = **SymTableOrErr; if (SymTable.sh_type != ELF::SHT_SYMTAB && SymTable.sh_type != ELF::SHT_DYNSYM) - return object_error::parse_failed; + return createError("invalid sh_type"); if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym))) - return object_error::parse_failed; + return createError("invalid section contents size"); return V; } template -ErrorOr +Expected ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const { auto SectionsOrErr = sections(); - if (std::error_code EC = SectionsOrErr.getError()) - return EC; + if (!SectionsOrErr) + return SectionsOrErr.takeError(); return getStringTableForSymtab(Sec, *SectionsOrErr); } template -ErrorOr +Expected ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec, Elf_Shdr_Range Sections) const { if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) - return object_error::parse_failed; - ErrorOr SectionOrErr = - object::getSection(Sections, Sec.sh_link); - if (std::error_code EC = SectionOrErr.getError()) - return EC; + return createError( + "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM"); + auto SectionOrErr = object::getSection(Sections, Sec.sh_link); + if (!SectionOrErr) + return SectionOrErr.takeError(); return getStringTable(*SectionOrErr); } template -ErrorOr +Expected ELFFile::getSectionName(const Elf_Shdr *Section) const { auto SectionsOrErr = sections(); - if (std::error_code EC = SectionsOrErr.getError()) - return EC; - ErrorOr Table = getSectionStringTable(*SectionsOrErr); - if (std::error_code EC = Table.getError()) - return EC; + if (!SectionsOrErr) + return SectionsOrErr.takeError(); + auto Table = getSectionStringTable(*SectionsOrErr); + if (!Table) + return Table.takeError(); return getSectionName(Section, *Table); } template -ErrorOr ELFFile::getSectionName(const Elf_Shdr *Section, - StringRef DotShstrtab) const { +Expected ELFFile::getSectionName(const Elf_Shdr *Section, + StringRef DotShstrtab) const { uint32_t Offset = Section->sh_name; if (Offset == 0) return StringRef(); if (Offset >= DotShstrtab.size()) - return object_error::parse_failed; + return createError("invalid string offset"); return StringRef(DotShstrtab.data() + Offset); } diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 2a23210e092..c84881899d8 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -255,7 +255,10 @@ protected: /// \brief Get the relocation section that contains \a Rel. const Elf_Shdr *getRelSection(DataRefImpl Rel) const { - return *EF.getSection(Rel.d.a); + auto RelSecOrErr = EF.getSection(Rel.d.a); + if (!RelSecOrErr) + report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message()); + return *RelSecOrErr; } DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const { @@ -268,7 +271,13 @@ protected: assert(SymTable->sh_type == ELF::SHT_SYMTAB || SymTable->sh_type == ELF::SHT_DYNSYM); - uintptr_t SHT = reinterpret_cast((*EF.sections()).begin()); + auto SectionsOrErr = EF.sections(); + if (!SectionsOrErr) { + DRI.d.a = 0; + DRI.d.b = 0; + return DRI; + } + uintptr_t SHT = reinterpret_cast((*SectionsOrErr).begin()); unsigned SymTableIndex = (reinterpret_cast(SymTable) - SHT) / sizeof(Elf_Shdr); @@ -318,8 +327,8 @@ public: const Elf_Sym *getSymbol(DataRefImpl Sym) const { auto Ret = EF.template getEntry(Sym.d.a, Sym.d.b); - if (std::error_code EC = Ret.getError()) - report_fatal_error(EC.message()); + if (!Ret) + report_fatal_error(errorToErrorCode(Ret.takeError()).message()); return *Ret; } @@ -373,10 +382,18 @@ void ELFObjectFile::moveSymbolNext(DataRefImpl &Sym) const { template Expected ELFObjectFile::getSymbolName(DataRefImpl Sym) const { const Elf_Sym *ESym = getSymbol(Sym); - const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a); - const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link); - StringRef SymTable = *EF.getStringTable(StringTableSec); - return ESym->getName(SymTable); + auto SymTabOrErr = EF.getSection(Sym.d.a); + if (!SymTabOrErr) + return SymTabOrErr.takeError(); + const Elf_Shdr *SymTableSec = *SymTabOrErr; + auto StrTabOrErr = EF.getSection(SymTableSec->sh_link); + if (!StrTabOrErr) + return StrTabOrErr.takeError(); + const Elf_Shdr *StringTableSec = *StrTabOrErr; + auto SymStrTabOrErr = EF.getStringTable(StringTableSec); + if (!SymStrTabOrErr) + return SymStrTabOrErr.takeError(); + return ESym->getName(*SymStrTabOrErr); } template @@ -423,13 +440,15 @@ ELFObjectFile::getSymbolAddress(DataRefImpl Symb) const { } const Elf_Ehdr *Header = EF.getHeader(); - const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a); + auto SymTabOrErr = EF.getSection(Symb.d.a); + if (!SymTabOrErr) + return SymTabOrErr.takeError(); + const Elf_Shdr *SymTab = *SymTabOrErr; if (Header->e_type == ELF::ET_REL) { - ErrorOr SectionOrErr = - EF.getSection(ESym, SymTab, ShndxTable); - if (std::error_code EC = SectionOrErr.getError()) - return errorCodeToError(EC); + auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable); + if (!SectionOrErr) + return SectionOrErr.takeError(); const Elf_Shdr *Section = *SectionOrErr; if (Section) Result += Section->sh_addr; @@ -509,9 +528,14 @@ uint32_t ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { if (ESym->st_shndx == ELF::SHN_ABS) Result |= SymbolRef::SF_Absolute; - if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION || - ESym == (*EF.symbols(DotSymtabSec)).begin() || - ESym == (*EF.symbols(DotDynSymSec)).begin()) + if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION) + Result |= SymbolRef::SF_FormatSpecific; + + auto DotSymtabSecSyms = EF.symbols(DotSymtabSec); + if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin()) + Result |= SymbolRef::SF_FormatSpecific; + auto DotDynSymSecSyms = EF.symbols(DotDynSymSec); + if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin()) Result |= SymbolRef::SF_FormatSpecific; if (EF.getHeader()->e_machine == ELF::EM_ARM) { @@ -547,9 +571,9 @@ template Expected ELFObjectFile::getSymbolSection(const Elf_Sym *ESym, const Elf_Shdr *SymTab) const { - ErrorOr ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable); - if (std::error_code EC = ESecOrErr.getError()) - return errorCodeToError(EC); + auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable); + if (!ESecOrErr) + return ESecOrErr.takeError(); const Elf_Shdr *ESec = *ESecOrErr; if (!ESec) @@ -564,7 +588,10 @@ template Expected ELFObjectFile::getSymbolSection(DataRefImpl Symb) const { const Elf_Sym *Sym = getSymbol(Symb); - const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a); + auto SymTabOrErr = EF.getSection(Symb.d.a); + if (!SymTabOrErr) + return SymTabOrErr.takeError(); + const Elf_Shdr *SymTab = *SymTabOrErr; return getSymbolSection(Sym, SymTab); } @@ -577,9 +604,9 @@ void ELFObjectFile::moveSectionNext(DataRefImpl &Sec) const { template std::error_code ELFObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const { - ErrorOr Name = EF.getSectionName(&*getSection(Sec)); + auto Name = EF.getSectionName(&*getSection(Sec)); if (!Name) - return Name.getError(); + return errorToErrorCode(Name.takeError()); Result = *Name; return std::error_code(); } @@ -641,7 +668,10 @@ template relocation_iterator ELFObjectFile::section_rel_begin(DataRefImpl Sec) const { DataRefImpl RelData; - uintptr_t SHT = reinterpret_cast((*EF.sections()).begin()); + auto SectionsOrErr = EF.sections(); + if (!SectionsOrErr) + return relocation_iterator(RelocationRef()); + uintptr_t SHT = reinterpret_cast((*SectionsOrErr).begin()); RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; RelData.d.b = 0; return relocation_iterator(RelocationRef(RelData, this)); @@ -658,9 +688,9 @@ ELFObjectFile::section_rel_end(DataRefImpl Sec) const { const Elf_Shdr *RelSec = getRelSection(RelData); // Error check sh_link here so that getRelocationSymbol can just use it. - ErrorOr SymSecOrErr = EF.getSection(RelSec->sh_link); - if (std::error_code EC = SymSecOrErr.getError()) - report_fatal_error(EC.message()); + auto SymSecOrErr = EF.getSection(RelSec->sh_link); + if (!SymSecOrErr) + report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message()); RelData.d.b += S->sh_size / S->sh_entsize; return relocation_iterator(RelocationRef(RelData, this)); @@ -677,9 +707,9 @@ ELFObjectFile::getRelocatedSection(DataRefImpl Sec) const { if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) return section_end(); - ErrorOr R = EF.getSection(EShdr->sh_info); - if (std::error_code EC = R.getError()) - report_fatal_error(EC.message()); + auto R = EF.getSection(EShdr->sh_info); + if (!R) + report_fatal_error(errorToErrorCode(R.takeError()).message()); return section_iterator(SectionRef(toDRI(*R), this)); } @@ -753,8 +783,8 @@ const typename ELFObjectFile::Elf_Rel * ELFObjectFile::getRel(DataRefImpl Rel) const { assert(getRelSection(Rel)->sh_type == ELF::SHT_REL); auto Ret = EF.template getEntry(Rel.d.a, Rel.d.b); - if (std::error_code EC = Ret.getError()) - report_fatal_error(EC.message()); + if (!Ret) + report_fatal_error(errorToErrorCode(Ret.takeError()).message()); return *Ret; } @@ -763,8 +793,8 @@ const typename ELFObjectFile::Elf_Rela * ELFObjectFile::getRela(DataRefImpl Rela) const { assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA); auto Ret = EF.template getEntry(Rela.d.a, Rela.d.b); - if (std::error_code EC = Ret.getError()) - report_fatal_error(EC.message()); + if (!Ret) + report_fatal_error(errorToErrorCode(Ret.takeError()).message()); return *Ret; } @@ -775,8 +805,10 @@ ELFObjectFile::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC) Object), EF(Data.getBuffer()) { auto SectionsOrErr = EF.sections(); - if ((EC = SectionsOrErr.getError())) + if (!SectionsOrErr) { + EC = errorToErrorCode(SectionsOrErr.takeError()); return; + } for (const Elf_Shdr &Sec : *SectionsOrErr) { switch (Sec.sh_type) { case ELF::SHT_DYNSYM: { @@ -798,9 +830,11 @@ ELFObjectFile::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC) break; } case ELF::SHT_SYMTAB_SHNDX: { - ErrorOr> TableOrErr = EF.getSHNDXTable(Sec); - if ((EC = TableOrErr.getError())) + auto TableOrErr = EF.getSHNDXTable(Sec); + if (!TableOrErr) { + EC = errorToErrorCode(TableOrErr.takeError()); return; + } ShndxTable = *TableOrErr; break; } @@ -838,12 +872,18 @@ elf_symbol_iterator ELFObjectFile::dynamic_symbol_end() const { template section_iterator ELFObjectFile::section_begin() const { - return section_iterator(SectionRef(toDRI((*EF.sections()).begin()), this)); + auto SectionsOrErr = EF.sections(); + if (!SectionsOrErr) + return section_iterator(SectionRef()); + return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this)); } template section_iterator ELFObjectFile::section_end() const { - return section_iterator(SectionRef(toDRI((*EF.sections()).end()), this)); + auto SectionsOrErr = EF.sections(); + if (!SectionsOrErr) + return section_iterator(SectionRef()); + return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this)); } template diff --git a/test/Object/corrupt.test b/test/Object/corrupt.test index d19d2924f14..9cbe9ef5e08 100644 --- a/test/Object/corrupt.test +++ b/test/Object/corrupt.test @@ -2,21 +2,21 @@ RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -sections \ RUN: 2>&1 | FileCheck --check-prefix=SECNAME %s -SECNAME: Error reading file: Invalid data was encountered while parsing the file. +SECNAME: invalid string offset // Section data offset past end of file. RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -sections -section-data \ RUN: 2>&1 | FileCheck --check-prefix=SECDATA %s -SECDATA: Error reading file: Invalid data was encountered while parsing the file. +SECDATA: invalid section offset // Symbol name offset overflows string table. RUN: not llvm-readobj %p/Inputs/corrupt.elf-x86-64 -symbols \ RUN: 2>&1 | FileCheck --check-prefix=SYMNAME %s -SYMNAME: Error reading file: Invalid data was encountered while parsing the file. +SYMNAME: invalid string offset // Version index in .gnu.version overflows the version map. @@ -36,7 +36,7 @@ RUN: not llvm-readobj -program-headers \ RUN: %p/Inputs/corrupt-invalid-phentsize.elf.x86-64 2>&1 | \ RUN: FileCheck --check-prefix=PHENTSIZE %s -PHENTSIZE: Invalid data was encountered while parsing the file. +PHENTSIZE: invalid e_phentsize RUN: not llvm-readobj -dynamic-table \ RUN: %p/Inputs/corrupt-invalid-virtual-addr.elf.x86-64 2>&1 | \ diff --git a/test/Object/invalid.test b/test/Object/invalid.test index ae6b3feb528..fc1a77b2c0c 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -5,7 +5,7 @@ RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-zero-size.elf 2>&1 | FileCheck CHECK: Invalid data was encountered while parsing the file RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-non-null.elf 2>&1 | FileCheck --check-prefix=NON-NULL %s -NON-NULL: String table must end with a null terminator +NON-NULL: Invalid data was encountered while parsing the file Test the sh_entsize are invalid RUN: llvm-readobj -s %p/Inputs/invalid-sh_entsize.elf | FileCheck --check-prefix=SECTION %s @@ -36,37 +36,37 @@ SECTION-NEXT: AddressAlignment: SECTION-NEXT: EntrySize: 32 RUN: not llvm-readobj -t %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s -INVALID-SYM-SIZE: Invalid data was encountered while parsing the file +INVALID-SYM-SIZE: invalid sh_entsize RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s INVALID-DYNSYM-SIZE: Invalid entity size RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s -INVALID-SECTION-INDEX: Invalid section index +INVALID-SECTION-INDEX: invalid section index RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s INVALID-SECTION-SIZE: Invalid data was encountered while parsing the file RUN: not llvm-readobj -t %p/Inputs/invalid-symbol-table-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SYMTAB-SIZE %s -INVALID-SYMTAB-SIZE: Invalid data was encountered while parsing the file +INVALID-SYMTAB-SIZE: size is not a multiple of sh_entsize RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file. RUN: not llvm-readobj -t %p/Inputs/invalid-e_shnum.elf 2>&1 | FileCheck --check-prefix=INVALID-SH-NUM %s -INVALID-SH-NUM: Invalid data was encountered while parsing the file. +INVALID-SH-NUM: invalid e_phentsize RUN: not llvm-readobj -t %p/Inputs/invalid-ext-symtab-index.elf-x86-64 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-EXT-SYMTAB-INDEX %s -INVALID-EXT-SYMTAB-INDEX: Invalid data was encountered while parsing the file. +INVALID-EXT-SYMTAB-INDEX: index past the end of the symbol table RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-i386 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s -INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file +INVALID-RELOC-SH-OFFSET: invalid section offset RUN: not llvm-readobj -t %p/Inputs/invalid-sections-address-alignment.x86-64 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-SEC-ADDRESS-ALIGNMENT %s @@ -74,10 +74,10 @@ INVALID-SEC-ADDRESS-ALIGNMENT: Invalid data was encountered while parsing the fi RUN: not llvm-readobj -t %p/Inputs/invalid-section-size2.elf 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s -INVALID-SECTION-SIZE2: Invalid data was encountered while parsing the file. +INVALID-SECTION-SIZE2: invalid section offset RUN: not llvm-readobj -t %p/Inputs/invalid-sections-num.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-NUM %s INVALID-SECTION-NUM: Invalid data was encountered while parsing the file. RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s -INVALID-REL-SYM: Invalid data was encountered while parsing the file. +INVALID-REL-SYM: invalid section offset diff --git a/tools/llvm-objdump/ELFDump.cpp b/tools/llvm-objdump/ELFDump.cpp index e9e0bcf30e8..2d2a5b581d1 100644 --- a/tools/llvm-objdump/ELFDump.cpp +++ b/tools/llvm-objdump/ELFDump.cpp @@ -25,8 +25,9 @@ template void printProgramHeaders(const ELFFile *o) { typedef ELFFile ELFO; outs() << "Program Header:\n"; auto ProgramHeaderOrError = o->program_headers(); - if (std::error_code EC = ProgramHeaderOrError.getError()) - report_fatal_error(EC.message()); + if (!ProgramHeaderOrError) + report_fatal_error( + errorToErrorCode(ProgramHeaderOrError.takeError()).message()); for (const typename ELFO::Elf_Phdr &Phdr : *ProgramHeaderOrError) { switch (Phdr.p_type) { case ELF::PT_LOAD: diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 920ad4a7dd6..be44c7450d6 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -608,22 +608,22 @@ static std::error_code getRelocationValueString(const ELFObjectFile *Obj, const ELFFile &EF = *Obj->getELFFile(); - ErrorOr SecOrErr = EF.getSection(Rel.d.a); - if (std::error_code EC = SecOrErr.getError()) - return EC; + auto SecOrErr = EF.getSection(Rel.d.a); + if (!SecOrErr) + return errorToErrorCode(SecOrErr.takeError()); const Elf_Shdr *Sec = *SecOrErr; - ErrorOr SymTabOrErr = EF.getSection(Sec->sh_link); - if (std::error_code EC = SymTabOrErr.getError()) - return EC; + auto SymTabOrErr = EF.getSection(Sec->sh_link); + if (!SymTabOrErr) + return errorToErrorCode(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; assert(SymTab->sh_type == ELF::SHT_SYMTAB || SymTab->sh_type == ELF::SHT_DYNSYM); - ErrorOr StrTabSec = EF.getSection(SymTab->sh_link); - if (std::error_code EC = StrTabSec.getError()) - return EC; - ErrorOr StrTabOrErr = EF.getStringTable(*StrTabSec); - if (std::error_code EC = StrTabOrErr.getError()) - return EC; + auto StrTabSec = EF.getSection(SymTab->sh_link); + if (!StrTabSec) + return errorToErrorCode(StrTabSec.takeError()); + auto StrTabOrErr = EF.getStringTable(*StrTabSec); + if (!StrTabOrErr) + return errorToErrorCode(StrTabOrErr.takeError()); StringRef StrTab = *StrTabOrErr; uint8_t type = RelRef.getType(); StringRef res; @@ -649,9 +649,9 @@ static std::error_code getRelocationValueString(const ELFObjectFile *Obj, if (!SymSI) return errorToErrorCode(SymSI.takeError()); const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl()); - ErrorOr SecName = EF.getSectionName(SymSec); - if (std::error_code EC = SecName.getError()) - return EC; + auto SecName = EF.getSectionName(SymSec); + if (!SecName) + return errorToErrorCode(SecName.takeError()); Target = *SecName; } else { Expected SymName = symb->getName(StrTab); diff --git a/tools/llvm-readobj/ARMEHABIPrinter.h b/tools/llvm-readobj/ARMEHABIPrinter.h index 78060e3abcd..903a246ccfd 100644 --- a/tools/llvm-readobj/ARMEHABIPrinter.h +++ b/tools/llvm-readobj/ARMEHABIPrinter.h @@ -349,8 +349,9 @@ template ErrorOr PrinterContext::FunctionAtAddress(unsigned Section, uint64_t Address) const { - ErrorOr StrTableOrErr = ELF->getStringTableForSymtab(*Symtab); - error(StrTableOrErr.getError()); + auto StrTableOrErr = ELF->getStringTableForSymtab(*Symtab); + if (!StrTableOrErr) + error(StrTableOrErr.takeError()); StringRef StrTable = *StrTableOrErr; for (const Elf_Sym &Sym : unwrapOrError(ELF->symbols(Symtab))) @@ -383,8 +384,9 @@ PrinterContext::FindExceptionTable(unsigned IndexSectionIndex, if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex) continue; - ErrorOr SymTabOrErr = ELF->getSection(Sec.sh_link); - error(SymTabOrErr.getError()); + auto SymTabOrErr = ELF->getSection(Sec.sh_link); + if (!SymTabOrErr) + error(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; for (const Elf_Rel &R : unwrapOrError(ELF->rels(&Sec))) { @@ -399,10 +401,9 @@ PrinterContext::FindExceptionTable(unsigned IndexSectionIndex, const Elf_Sym *Symbol = unwrapOrError(ELF->getRelocationSymbol(&RelA, SymTab)); - ErrorOr Ret = - ELF->getSection(Symbol, SymTab, ShndxTable); - if (std::error_code EC = Ret.getError()) - report_fatal_error(EC.message()); + auto Ret = ELF->getSection(Symbol, SymTab, ShndxTable); + if (!Ret) + report_fatal_error(errorToErrorCode(Ret.takeError()).message()); return *Ret; } } @@ -413,7 +414,7 @@ template void PrinterContext::PrintExceptionTable(const Elf_Shdr *IT, const Elf_Shdr *EHT, uint64_t TableEntryOffset) const { - ErrorOr > Contents = ELF->getSectionContents(EHT); + Expected> Contents = ELF->getSectionContents(EHT); if (!Contents) return; @@ -480,7 +481,7 @@ void PrinterContext::PrintOpcodes(const uint8_t *Entry, template void PrinterContext::PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *IT) const { - ErrorOr > Contents = ELF->getSectionContents(IT); + Expected> Contents = ELF->getSectionContents(IT); if (!Contents) return; @@ -533,7 +534,7 @@ void PrinterContext::PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *EHT = FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4); - if (ErrorOr Name = ELF->getSectionName(EHT)) + if (auto Name = ELF->getSectionName(EHT)) SW.printString("ExceptionHandlingTable", *Name); uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr); @@ -554,7 +555,7 @@ void PrinterContext::PrintUnwindInformation() const { DictScope UIT(SW, "UnwindIndexTable"); SW.printNumber("SectionIndex", SectionIndex); - if (ErrorOr SectionName = ELF->getSectionName(&Sec)) + if (auto SectionName = ELF->getSectionName(&Sec)) SW.printString("SectionName", *SectionName); SW.printHex("SectionOffset", Sec.sh_offset); diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index ffcb2d0ece4..697ab79d3b5 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -75,8 +75,8 @@ ErrorOr ELFDumper::dump() { // Dump sections auto SectionsOrErr = Obj.sections(); - if (std::error_code EC = SectionsOrErr.getError()) - return EC; + if (!SectionsOrErr) + return errorToErrorCode(SectionsOrErr.takeError()); for (const Elf_Shdr &Sec : *SectionsOrErr) { switch (Sec.sh_type) { case ELF::SHT_NULL: @@ -88,9 +88,9 @@ ErrorOr ELFDumper::dump() { Symtab = &Sec; break; case ELF::SHT_SYMTAB_SHNDX: { - ErrorOr> TableOrErr = Obj.getSHNDXTable(Sec); - if (std::error_code EC = TableOrErr.getError()) - return EC; + auto TableOrErr = Obj.getSHNDXTable(Sec); + if (!TableOrErr) + return errorToErrorCode(TableOrErr.takeError()); ShndxTable = *TableOrErr; break; } @@ -139,15 +139,15 @@ ErrorOr ELFDumper::dump() { } // Dump symbols - ErrorOr StrTableOrErr = Obj.getStringTableForSymtab(*Symtab); - if (std::error_code EC = StrTableOrErr.getError()) - return EC; + auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab); + if (!StrTableOrErr) + return errorToErrorCode(StrTableOrErr.takeError()); StringRef StrTable = *StrTableOrErr; bool IsFirstSym = true; auto SymtabOrErr = Obj.symbols(Symtab); - if (std::error_code EC = SymtabOrErr.getError()) - return EC; + if (!SymtabOrErr) + return errorToErrorCode(SymtabOrErr.takeError()); for (const Elf_Sym &Sym : *SymtabOrErr) { if (IsFirstSym) { IsFirstSym = false; @@ -192,16 +192,16 @@ ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, return errorToErrorCode(SymbolNameOrErr.takeError()); S.Name = SymbolNameOrErr.get(); - ErrorOr ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); - if (std::error_code EC = ShdrOrErr.getError()) - return EC; + auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); + if (!ShdrOrErr) + return errorToErrorCode(ShdrOrErr.takeError()); const Elf_Shdr *Shdr = *ShdrOrErr; if (!Shdr) return obj2yaml_error::success; - ErrorOr NameOrErr = Obj.getSectionName(Shdr); - if (std::error_code EC = NameOrErr.getError()) - return EC; + auto NameOrErr = Obj.getSectionName(Shdr); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); S.Section = NameOrErr.get(); return obj2yaml_error::success; @@ -217,15 +217,15 @@ std::error_code ELFDumper::dumpRelocation(const RelT *Rel, R.Addend = 0; auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab); - if (std::error_code EC = SymOrErr.getError()) - return EC; + if (!SymOrErr) + return errorToErrorCode(SymOrErr.takeError()); const Elf_Sym *Sym = *SymOrErr; - ErrorOr StrTabSec = Obj.getSection(SymTab->sh_link); - if (std::error_code EC = StrTabSec.getError()) - return EC; - ErrorOr StrTabOrErr = Obj.getStringTable(*StrTabSec); - if (std::error_code EC = StrTabOrErr.getError()) - return EC; + auto StrTabSec = Obj.getSection(SymTab->sh_link); + if (!StrTabSec) + return errorToErrorCode(StrTabSec.takeError()); + auto StrTabOrErr = Obj.getStringTable(*StrTabSec); + if (!StrTabOrErr) + return errorToErrorCode(StrTabOrErr.takeError()); StringRef StrTab = *StrTabOrErr; Expected NameOrErr = Sym->getName(StrTab); @@ -244,18 +244,18 @@ std::error_code ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr, S.Address = Shdr->sh_addr; S.AddressAlign = Shdr->sh_addralign; - ErrorOr NameOrErr = Obj.getSectionName(Shdr); - if (std::error_code EC = NameOrErr.getError()) - return EC; + auto NameOrErr = Obj.getSectionName(Shdr); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); S.Name = NameOrErr.get(); if (Shdr->sh_link != ELF::SHN_UNDEF) { - ErrorOr LinkSection = Obj.getSection(Shdr->sh_link); - if (std::error_code EC = LinkSection.getError()) - return EC; + auto LinkSection = Obj.getSection(Shdr->sh_link); + if (LinkSection.takeError()) + return errorToErrorCode(LinkSection.takeError()); NameOrErr = Obj.getSectionName(*LinkSection); - if (std::error_code EC = NameOrErr.getError()) - return EC; + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); S.Link = NameOrErr.get(); } @@ -269,13 +269,13 @@ ELFDumper::dumpCommonRelocationSection(const Elf_Shdr *Shdr, if (std::error_code EC = dumpCommonSection(Shdr, S)) return EC; - ErrorOr InfoSection = Obj.getSection(Shdr->sh_info); - if (std::error_code EC = InfoSection.getError()) - return EC; + auto InfoSection = Obj.getSection(Shdr->sh_info); + if (!InfoSection) + return errorToErrorCode(InfoSection.takeError()); - ErrorOr NameOrErr = Obj.getSectionName(*InfoSection); - if (std::error_code EC = NameOrErr.getError()) - return EC; + auto NameOrErr = Obj.getSectionName(*InfoSection); + if (!NameOrErr) + return errorToErrorCode(NameOrErr.takeError()); S.Info = NameOrErr.get(); return obj2yaml_error::success; @@ -290,14 +290,14 @@ ELFDumper::dumpRelSection(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) return EC; - ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link); - if (std::error_code EC = SymTabOrErr.getError()) - return EC; + auto SymTabOrErr = Obj.getSection(Shdr->sh_link); + if (!SymTabOrErr) + return errorToErrorCode(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; auto Rels = Obj.rels(Shdr); - if (std::error_code EC = Rels.getError()) - return EC; + if (!Rels) + return errorToErrorCode(Rels.takeError()); for (const Elf_Rel &Rel : *Rels) { ELFYAML::Relocation R; if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) @@ -317,14 +317,14 @@ ELFDumper::dumpRelaSection(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) return EC; - ErrorOr SymTabOrErr = Obj.getSection(Shdr->sh_link); - if (std::error_code EC = SymTabOrErr.getError()) - return EC; + auto SymTabOrErr = Obj.getSection(Shdr->sh_link); + if (!SymTabOrErr) + return errorToErrorCode(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; auto Rels = Obj.relas(Shdr); - if (std::error_code EC = Rels.getError()) - return EC; + if (!Rels) + return errorToErrorCode(Rels.takeError()); for (const Elf_Rela &Rel : *Rels) { ELFYAML::Relocation R; if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) @@ -344,9 +344,9 @@ ELFDumper::dumpContentSection(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonSection(Shdr, *S)) return EC; - ErrorOr> ContentOrErr = Obj.getSectionContents(Shdr); - if (std::error_code EC = ContentOrErr.getError()) - return EC; + auto ContentOrErr = Obj.getSectionContents(Shdr); + if (!ContentOrErr) + return errorToErrorCode(ContentOrErr.takeError()); S->Content = yaml::BinaryRef(ContentOrErr.get()); S->Size = S->Content.binary_size(); @@ -372,21 +372,21 @@ ErrorOr ELFDumper::dumpGroup(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonSection(Shdr, *S)) return EC; // Get sh_info which is the signature. - ErrorOr SymtabOrErr = Obj.getSection(Shdr->sh_link); - if (std::error_code EC = SymtabOrErr.getError()) - return EC; + auto SymtabOrErr = Obj.getSection(Shdr->sh_link); + if (!SymtabOrErr) + return errorToErrorCode(SymtabOrErr.takeError()); const Elf_Shdr *Symtab = *SymtabOrErr; - ErrorOr SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info); - if (std::error_code EC = SymOrErr.getError()) - return EC; + auto SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info); + if (!SymOrErr) + return errorToErrorCode(SymOrErr.takeError()); const Elf_Sym *symbol = *SymOrErr; - ErrorOr StrTabOrErr = Obj.getStringTableForSymtab(*Symtab); - if (std::error_code EC = StrTabOrErr.getError()) - return EC; + auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab); + if (!StrTabOrErr) + return errorToErrorCode(StrTabOrErr.takeError()); StringRef StrTab = *StrTabOrErr; auto sectionContents = Obj.getSectionContents(Shdr); - if (std::error_code ec = sectionContents.getError()) - return ec; + if (!sectionContents) + return errorToErrorCode(sectionContents.takeError()); Expected symbolName = symbol->getName(StrTab); if (!symbolName) return errorToErrorCode(symbolName.takeError()); @@ -399,12 +399,12 @@ ErrorOr ELFDumper::dumpGroup(const Elf_Shdr *Shdr) { if (groupMembers[i] == llvm::ELF::GRP_COMDAT) { s.sectionNameOrType = "GRP_COMDAT"; } else { - ErrorOr sHdr = Obj.getSection(groupMembers[i]); - if (std::error_code EC = sHdr.getError()) - return EC; - ErrorOr sectionName = Obj.getSectionName(*sHdr); - if (std::error_code ec = sectionName.getError()) - return ec; + auto sHdr = Obj.getSection(groupMembers[i]); + if (!sHdr) + return errorToErrorCode(sHdr.takeError()); + auto sectionName = Obj.getSectionName(*sHdr); + if (!sectionName) + return errorToErrorCode(sectionName.takeError()); s.sectionNameOrType = *sectionName; } S->Members.push_back(s); @@ -421,9 +421,9 @@ ELFDumper::dumpMipsABIFlags(const Elf_Shdr *Shdr) { if (std::error_code EC = dumpCommonSection(Shdr, *S)) return EC; - ErrorOr> ContentOrErr = Obj.getSectionContents(Shdr); - if (std::error_code EC = ContentOrErr.getError()) - return EC; + auto ContentOrErr = Obj.getSectionContents(Shdr); + if (!ContentOrErr) + return errorToErrorCode(ContentOrErr.takeError()); auto *Flags = reinterpret_cast *>( ContentOrErr.get().data()); -- 2.11.0