summaryrefslogtreecommitdiffstats
path: root/llvm/tools
diff options
context:
space:
mode:
authorDavide Italiano <davide@freebsd.org>2016-11-16 05:10:28 +0000
committerDavide Italiano <davide@freebsd.org>2016-11-16 05:10:28 +0000
commit6cf09265f9087b5d285d3eec6ee31b49bcce0c9f (patch)
treeb5d5ba3a43360d5cf9de131aea2155f8a84bb0e5 /llvm/tools
parent86dd66e96cc57d70639bfbae71f23ad4953ea1a5 (diff)
downloadbcm5719-llvm-6cf09265f9087b5d285d3eec6ee31b49bcce0c9f.tar.gz
bcm5719-llvm-6cf09265f9087b5d285d3eec6ee31b49bcce0c9f.zip
[ELF] Convert ELF.h to Expected<T>.
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. llvm-svn: 287081
Diffstat (limited to 'llvm/tools')
-rw-r--r--llvm/tools/llvm-objdump/ELFDump.cpp5
-rw-r--r--llvm/tools/llvm-objdump/llvm-objdump.cpp30
-rw-r--r--llvm/tools/llvm-readobj/ARMEHABIPrinter.h25
-rw-r--r--llvm/tools/obj2yaml/elf2yaml.cpp142
4 files changed, 102 insertions, 100 deletions
diff --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp
index e9e0bcf30e8..2d2a5b581d1 100644
--- a/llvm/tools/llvm-objdump/ELFDump.cpp
+++ b/llvm/tools/llvm-objdump/ELFDump.cpp
@@ -25,8 +25,9 @@ template <class ELFT> void printProgramHeaders(const ELFFile<ELFT> *o) {
typedef ELFFile<ELFT> 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/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 920ad4a7dd6..be44c7450d6 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -608,22 +608,22 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
const ELFFile<ELFT> &EF = *Obj->getELFFile();
- ErrorOr<const Elf_Shdr *> 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<const Elf_Shdr *> 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<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
- if (std::error_code EC = StrTabSec.getError())
- return EC;
- ErrorOr<StringRef> 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<ELFT> *Obj,
if (!SymSI)
return errorToErrorCode(SymSI.takeError());
const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
- ErrorOr<StringRef> 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<StringRef> SymName = symb->getName(StrTab);
diff --git a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h
index 78060e3abcd..903a246ccfd 100644
--- a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h
+++ b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h
@@ -349,8 +349,9 @@ template <typename ET>
ErrorOr<StringRef>
PrinterContext<ET>::FunctionAtAddress(unsigned Section,
uint64_t Address) const {
- ErrorOr<StringRef> 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<ET>::FindExceptionTable(unsigned IndexSectionIndex,
if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
continue;
- ErrorOr<const Elf_Shdr *> 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<ET>::FindExceptionTable(unsigned IndexSectionIndex,
const Elf_Sym *Symbol =
unwrapOrError(ELF->getRelocationSymbol(&RelA, SymTab));
- ErrorOr<const Elf_Shdr *> 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 <typename ET>
void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr *IT,
const Elf_Shdr *EHT,
uint64_t TableEntryOffset) const {
- ErrorOr<ArrayRef<uint8_t> > Contents = ELF->getSectionContents(EHT);
+ Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(EHT);
if (!Contents)
return;
@@ -480,7 +481,7 @@ void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry,
template <typename ET>
void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
const Elf_Shdr *IT) const {
- ErrorOr<ArrayRef<uint8_t> > Contents = ELF->getSectionContents(IT);
+ Expected<ArrayRef<uint8_t>> Contents = ELF->getSectionContents(IT);
if (!Contents)
return;
@@ -533,7 +534,7 @@ void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
const Elf_Shdr *EHT =
FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
- if (ErrorOr<StringRef> 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<ET>::PrintUnwindInformation() const {
DictScope UIT(SW, "UnwindIndexTable");
SW.printNumber("SectionIndex", SectionIndex);
- if (ErrorOr<StringRef> SectionName = ELF->getSectionName(&Sec))
+ if (auto SectionName = ELF->getSectionName(&Sec))
SW.printString("SectionName", *SectionName);
SW.printHex("SectionOffset", Sec.sh_offset);
diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index ffcb2d0ece4..697ab79d3b5 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -75,8 +75,8 @@ ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::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<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
Symtab = &Sec;
break;
case ELF::SHT_SYMTAB_SHNDX: {
- ErrorOr<ArrayRef<Elf_Word>> 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<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
}
// Dump symbols
- ErrorOr<StringRef> 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<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
return errorToErrorCode(SymbolNameOrErr.takeError());
S.Name = SymbolNameOrErr.get();
- ErrorOr<const Elf_Shdr *> 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<StringRef> 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<ELFT>::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<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
- if (std::error_code EC = StrTabSec.getError())
- return EC;
- ErrorOr<StringRef> 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<StringRef> NameOrErr = Sym->getName(StrTab);
@@ -244,18 +244,18 @@ std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
S.Address = Shdr->sh_addr;
S.AddressAlign = Shdr->sh_addralign;
- ErrorOr<StringRef> 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<const Elf_Shdr *> 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<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
if (std::error_code EC = dumpCommonSection(Shdr, S))
return EC;
- ErrorOr<const Elf_Shdr *> 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<StringRef> 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<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC;
- ErrorOr<const Elf_Shdr *> 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<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC;
- ErrorOr<const Elf_Shdr *> 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<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
- ErrorOr<ArrayRef<uint8_t>> 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<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
// Get sh_info which is the signature.
- ErrorOr<const Elf_Shdr *> 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<const Elf_Sym *> 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<StringRef> 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<StringRef> symbolName = symbol->getName(StrTab);
if (!symbolName)
return errorToErrorCode(symbolName.takeError());
@@ -399,12 +399,12 @@ ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
s.sectionNameOrType = "GRP_COMDAT";
} else {
- ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
- if (std::error_code EC = sHdr.getError())
- return EC;
- ErrorOr<StringRef> 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<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
- ErrorOr<ArrayRef<uint8_t>> 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<const object::Elf_Mips_ABIFlags<ELFT> *>(
ContentOrErr.get().data());
OpenPOWER on IntegriCloud