From: George Rimar Date: Mon, 15 Jul 2019 11:00:42 +0000 (+0000) Subject: Revert r366052 "[obj2yaml] - Rework tool's error reporting logic for ELF target." X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cabe7eba745e98f0bf0b502a7fa7ce8772ed7994;p=llvm Revert r366052 "[obj2yaml] - Rework tool's error reporting logic for ELF target." Seems it broke LLD: http://lab.llvm.org:8011/builders/sanitizer-windows/builds/48434 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366053 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 7bc6dc4620c..8fe9f2919c5 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -355,20 +355,23 @@ ELFFile::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols, } template -Expected -ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { - auto SymsOrErr = symbols(Sec); - if (!SymsOrErr) - return SymsOrErr.takeError(); - - Elf_Sym_Range Symbols = *SymsOrErr; +inline Expected +getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) { if (Index >= Symbols.size()) - return createError("unable to get symbol from section " + - getSecIndexForError(this, Sec) + - ": invalid symbol index (" + Twine(Index) + ")"); + // TODO: this error is untested. + return createError("invalid symbol index"); return &Symbols[Index]; } +template +Expected +ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { + auto SymtabOrErr = symbols(Sec); + if (!SymtabOrErr) + return SymtabOrErr.takeError(); + return object::getSymbol(*SymtabOrErr, Index); +} + template template Expected> diff --git a/test/tools/obj2yaml/section-group.test b/test/tools/obj2yaml/section-group.test index cd520cb1b36..78af00cd138 100644 --- a/test/tools/obj2yaml/section-group.test +++ b/test/tools/obj2yaml/section-group.test @@ -1,6 +1,6 @@ ## Checks that the tool is able to read section groups from ELF. -# RUN: yaml2obj --docnum=1 %s > %t1.o +# RUN: yaml2obj %s > %t1.o # RUN: llvm-readobj --elf-section-groups %t1.o | FileCheck %s -check-prefix=OBJ # RUN: obj2yaml %t1.o | FileCheck %s --check-prefix YAML @@ -46,25 +46,3 @@ Symbols: - Name: signature Type: STT_OBJECT Section: .rodata - -## Check obj2yaml report an error when sh_info field of -## group section contains invalid (too large) signature symbol index. - -# RUN: yaml2obj --docnum=2 %s > %t2.o -# RUN: not obj2yaml %t2.o 2>&1 | FileCheck %s --check-prefix ERR - ---- !ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_REL - Machine: EM_X86_64 -Sections: - - Name: .group - Type: SHT_GROUP - Link: .symtab - Info: 0xFF - Members: - - SectionOrType: GRP_COMDAT - -# ERR: Error reading file: {{.*}}2.o: unable to get symbol from section [index 2]: invalid symbol index (255) diff --git a/test/tools/obj2yaml/special-symbol-indices.yaml b/test/tools/obj2yaml/special-symbol-indices.yaml index fcc2a705f9c..25550c944f3 100644 --- a/test/tools/obj2yaml/special-symbol-indices.yaml +++ b/test/tools/obj2yaml/special-symbol-indices.yaml @@ -51,4 +51,4 @@ Symbols: ## shn_xindex.o contains a symbol with st_shndx == SHN_XINDEX. ## We do not support it at this moment. # RUN: not obj2yaml %S/Inputs/shn_xindex.o 2>&1 | FileCheck %s --check-prefix=ERR -# ERR: Error reading file: {{.*}}shn_xindex.o: SHN_XINDEX symbols are not supported +# ERR: Error reading file: {{.*}}shn_xindex.o: Feature not yet implemented. diff --git a/tools/obj2yaml/elf2yaml.cpp b/tools/obj2yaml/elf2yaml.cpp index bd27c103403..7404bae2a08 100644 --- a/tools/obj2yaml/elf2yaml.cpp +++ b/tools/obj2yaml/elf2yaml.cpp @@ -44,31 +44,31 @@ class ELFDumper { const object::ELFFile &Obj; ArrayRef ShndxTable; - Error dumpSymbols(const Elf_Shdr *Symtab, - std::vector &Symbols); - Error dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, - StringRef StrTable, ELFYAML::Symbol &S); - Error dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S); - Error dumpCommonRelocationSection(const Elf_Shdr *Shdr, - ELFYAML::RelocationSection &S); + std::error_code dumpSymbols(const Elf_Shdr *Symtab, + std::vector &Symbols); + std::error_code dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, + StringRef StrTable, ELFYAML::Symbol &S); + std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S); + std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr, + ELFYAML::RelocationSection &S); template - Error dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, - ELFYAML::Relocation &R); - - Expected dumpDynamicSection(const Elf_Shdr *Shdr); - Expected dumpRelocSection(const Elf_Shdr *Shdr); - Expected + std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, + ELFYAML::Relocation &R); + + ErrorOr dumpDynamicSection(const Elf_Shdr *Shdr); + ErrorOr dumpRelocSection(const Elf_Shdr *Shdr); + ErrorOr dumpContentSection(const Elf_Shdr *Shdr); - Expected dumpNoBitsSection(const Elf_Shdr *Shdr); - Expected dumpVerdefSection(const Elf_Shdr *Shdr); - Expected dumpSymverSection(const Elf_Shdr *Shdr); - Expected dumpVerneedSection(const Elf_Shdr *Shdr); - Expected dumpGroup(const Elf_Shdr *Shdr); - Expected dumpMipsABIFlags(const Elf_Shdr *Shdr); + ErrorOr dumpNoBitsSection(const Elf_Shdr *Shdr); + ErrorOr dumpVerdefSection(const Elf_Shdr *Shdr); + ErrorOr dumpSymverSection(const Elf_Shdr *Shdr); + ErrorOr dumpVerneedSection(const Elf_Shdr *Shdr); + ErrorOr dumpGroup(const Elf_Shdr *Shdr); + ErrorOr dumpMipsABIFlags(const Elf_Shdr *Shdr); public: ELFDumper(const object::ELFFile &O); - Expected dump(); + ErrorOr dump(); }; } @@ -134,7 +134,7 @@ ELFDumper::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable, return Name; } -template Expected ELFDumper::dump() { +template ErrorOr ELFDumper::dump() { auto Y = make_unique(); // Dump header. We do not dump SHEntSize, SHOffset, SHNum and SHStrNdx field. @@ -152,7 +152,7 @@ template Expected ELFDumper::dump() { // Dump sections auto SectionsOrErr = Obj.sections(); if (!SectionsOrErr) - return SectionsOrErr.takeError(); + return errorToErrorCode(SectionsOrErr.takeError()); Sections = *SectionsOrErr; SectionNames.resize(Sections.size()); @@ -160,20 +160,20 @@ template Expected ELFDumper::dump() { // to access the deduplicated symbol names that we also create here. for (const Elf_Shdr &Sec : Sections) { if (Sec.sh_type == ELF::SHT_SYMTAB) - if (Error E = dumpSymbols(&Sec, Y->Symbols)) - return std::move(E); + if (auto EC = dumpSymbols(&Sec, Y->Symbols)) + return EC; if (Sec.sh_type == ELF::SHT_DYNSYM) - if (Error E = dumpSymbols(&Sec, Y->DynamicSymbols)) - return std::move(E); + if (auto EC = dumpSymbols(&Sec, Y->DynamicSymbols)) + return EC; } for (const Elf_Shdr &Sec : Sections) { switch (Sec.sh_type) { case ELF::SHT_DYNAMIC: { - Expected SecOrErr = dumpDynamicSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpDynamicSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } case ELF::SHT_NULL: @@ -185,66 +185,65 @@ template Expected ELFDumper::dump() { case ELF::SHT_SYMTAB_SHNDX: { auto TableOrErr = Obj.getSHNDXTable(Sec); if (!TableOrErr) - return TableOrErr.takeError(); + return errorToErrorCode(TableOrErr.takeError()); ShndxTable = *TableOrErr; break; } case ELF::SHT_REL: case ELF::SHT_RELA: { - Expected SecOrErr = dumpRelocSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpRelocSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } case ELF::SHT_GROUP: { - Expected GroupOrErr = dumpGroup(&Sec); - if (!GroupOrErr) - return GroupOrErr.takeError(); - Y->Sections.emplace_back(*GroupOrErr); + ErrorOr G = dumpGroup(&Sec); + if (std::error_code EC = G.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(G.get())); break; } case ELF::SHT_MIPS_ABIFLAGS: { - Expected SecOrErr = dumpMipsABIFlags(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr G = dumpMipsABIFlags(&Sec); + if (std::error_code EC = G.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(G.get())); break; } case ELF::SHT_NOBITS: { - Expected SecOrErr = dumpNoBitsSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpNoBitsSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } case ELF::SHT_GNU_verdef: { - Expected SecOrErr = dumpVerdefSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpVerdefSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } case ELF::SHT_GNU_versym: { - Expected SecOrErr = dumpSymverSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpSymverSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } case ELF::SHT_GNU_verneed: { - Expected SecOrErr = dumpVerneedSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpVerneedSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); break; } default: { - Expected SecOrErr = - dumpContentSection(&Sec); - if (!SecOrErr) - return SecOrErr.takeError(); - Y->Sections.emplace_back(*SecOrErr); + ErrorOr S = dumpContentSection(&Sec); + if (std::error_code EC = S.getError()) + return EC; + Y->Sections.push_back(std::unique_ptr(S.get())); } } } @@ -253,19 +252,20 @@ template Expected ELFDumper::dump() { } template -Error ELFDumper::dumpSymbols(const Elf_Shdr *Symtab, +std::error_code +ELFDumper::dumpSymbols(const Elf_Shdr *Symtab, std::vector &Symbols) { if (!Symtab) - return Error::success(); + return std::error_code(); auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab); if (!StrTableOrErr) - return StrTableOrErr.takeError(); + return errorToErrorCode(StrTableOrErr.takeError()); StringRef StrTable = *StrTableOrErr; auto SymtabOrErr = Obj.symbols(Symtab); if (!SymtabOrErr) - return SymtabOrErr.takeError(); + return errorToErrorCode(SymtabOrErr.takeError()); if (Symtab->sh_type == ELF::SHT_SYMTAB) { SymTable = *SymtabOrErr; @@ -279,12 +279,13 @@ Error ELFDumper::dumpSymbols(const Elf_Shdr *Symtab, Symbols.push_back(S); } - return Error::success(); + return std::error_code(); } template -Error ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, - StringRef StrTable, ELFYAML::Symbol &S) { +std::error_code +ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, + StringRef StrTable, ELFYAML::Symbol &S) { S.Type = Sym->getType(); S.Value = Sym->st_value; S.Size = Sym->st_size; @@ -294,56 +295,56 @@ Error ELFDumper::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab, Expected SymbolNameOrErr = getUniquedSymbolName(Sym, StrTable, SymTab); if (!SymbolNameOrErr) - return SymbolNameOrErr.takeError(); + return errorToErrorCode(SymbolNameOrErr.takeError()); S.Name = SymbolNameOrErr.get(); if (Sym->st_shndx >= ELF::SHN_LORESERVE) { if (Sym->st_shndx == ELF::SHN_XINDEX) - return createStringError(obj2yaml_error::not_implemented, - "SHN_XINDEX symbols are not supported"); + return obj2yaml_error::not_implemented; S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx; - return Error::success(); + return obj2yaml_error::success; } auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable); if (!ShdrOrErr) - return ShdrOrErr.takeError(); + return errorToErrorCode(ShdrOrErr.takeError()); const Elf_Shdr *Shdr = *ShdrOrErr; if (!Shdr) - return Error::success(); + return obj2yaml_error::success; auto NameOrErr = getUniquedSectionName(Shdr); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); S.Section = NameOrErr.get(); - return Error::success(); + return obj2yaml_error::success; } template template -Error ELFDumper::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, - ELFYAML::Relocation &R) { +std::error_code ELFDumper::dumpRelocation(const RelT *Rel, + const Elf_Shdr *SymTab, + ELFYAML::Relocation &R) { R.Type = Rel->getType(Obj.isMips64EL()); R.Offset = Rel->r_offset; R.Addend = 0; auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab); if (!SymOrErr) - return SymOrErr.takeError(); + return errorToErrorCode(SymOrErr.takeError()); const Elf_Sym *Sym = *SymOrErr; auto StrTabSec = Obj.getSection(SymTab->sh_link); if (!StrTabSec) - return StrTabSec.takeError(); + return errorToErrorCode(StrTabSec.takeError()); auto StrTabOrErr = Obj.getStringTable(*StrTabSec); if (!StrTabOrErr) - return StrTabOrErr.takeError(); + return errorToErrorCode(StrTabOrErr.takeError()); StringRef StrTab = *StrTabOrErr; if (Sym) { Expected NameOrErr = getUniquedSymbolName(Sym, StrTab, SymTab); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); R.Symbol = NameOrErr.get(); } else { // We have some edge cases of relocations without a symbol associated, @@ -353,12 +354,12 @@ Error ELFDumper::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab, R.Symbol = ""; } - return Error::success(); + return obj2yaml_error::success; } template -Error ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr, - ELFYAML::Section &S) { +std::error_code ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr, + ELFYAML::Section &S) { // Dump fields. We do not dump the ShOffset field. When not explicitly // set, the value is set by yaml2obj automatically. S.Type = Shdr->sh_type; @@ -371,50 +372,51 @@ Error ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr, auto NameOrErr = getUniquedSectionName(Shdr); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); S.Name = NameOrErr.get(); if (Shdr->sh_link != ELF::SHN_UNDEF) { auto LinkSection = Obj.getSection(Shdr->sh_link); if (LinkSection.takeError()) - return LinkSection.takeError(); + return errorToErrorCode(LinkSection.takeError()); NameOrErr = getUniquedSectionName(*LinkSection); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); S.Link = NameOrErr.get(); } - return Error::success(); + return obj2yaml_error::success; } template -Error ELFDumper::dumpCommonRelocationSection( - const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) { - if (Error E = dumpCommonSection(Shdr, S)) - return E; +std::error_code +ELFDumper::dumpCommonRelocationSection(const Elf_Shdr *Shdr, + ELFYAML::RelocationSection &S) { + if (std::error_code EC = dumpCommonSection(Shdr, S)) + return EC; auto InfoSection = Obj.getSection(Shdr->sh_info); if (!InfoSection) - return InfoSection.takeError(); + return errorToErrorCode(InfoSection.takeError()); auto NameOrErr = getUniquedSectionName(*InfoSection); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); S.RelocatableSec = NameOrErr.get(); - return Error::success(); + return obj2yaml_error::success; } template -Expected +ErrorOr ELFDumper::dumpDynamicSection(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; auto DynTagsOrErr = Obj.template getSectionContentsAsArray(Shdr); if (!DynTagsOrErr) - return DynTagsOrErr.takeError(); + return errorToErrorCode(DynTagsOrErr.takeError()); for (const Elf_Dyn &Dyn : *DynTagsOrErr) S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()}); @@ -423,35 +425,35 @@ ELFDumper::dumpDynamicSection(const Elf_Shdr *Shdr) { } template -Expected +ErrorOr ELFDumper::dumpRelocSection(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (auto E = dumpCommonRelocationSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S)) + return EC; auto SymTabOrErr = Obj.getSection(Shdr->sh_link); if (!SymTabOrErr) - return SymTabOrErr.takeError(); + return errorToErrorCode(SymTabOrErr.takeError()); const Elf_Shdr *SymTab = *SymTabOrErr; if (Shdr->sh_type == ELF::SHT_REL) { auto Rels = Obj.rels(Shdr); if (!Rels) - return Rels.takeError(); + return errorToErrorCode(Rels.takeError()); for (const Elf_Rel &Rel : *Rels) { ELFYAML::Relocation R; - if (Error E = dumpRelocation(&Rel, SymTab, R)) - return std::move(E); + if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) + return EC; S->Relocations.push_back(R); } } else { auto Rels = Obj.relas(Shdr); if (!Rels) - return Rels.takeError(); + return errorToErrorCode(Rels.takeError()); for (const Elf_Rela &Rel : *Rels) { ELFYAML::Relocation R; - if (Error E = dumpRelocation(&Rel, SymTab, R)) - return std::move(E); + if (std::error_code EC = dumpRelocation(&Rel, SymTab, R)) + return EC; R.Addend = Rel.r_addend; S->Relocations.push_back(R); } @@ -461,15 +463,16 @@ ELFDumper::dumpRelocSection(const Elf_Shdr *Shdr) { } template -Expected +ErrorOr ELFDumper::dumpContentSection(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; auto ContentOrErr = Obj.getSectionContents(Shdr); if (!ContentOrErr) - return ContentOrErr.takeError(); + return errorToErrorCode(ContentOrErr.takeError()); ArrayRef Content = *ContentOrErr; if (!Content.empty()) S->Content = yaml::BinaryRef(Content); @@ -479,39 +482,40 @@ ELFDumper::dumpContentSection(const Elf_Shdr *Shdr) { } template -Expected +ErrorOr ELFDumper::dumpNoBitsSection(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; S->Size = Shdr->sh_size; return S.release(); } template -Expected +ErrorOr ELFDumper::dumpVerdefSection(const Elf_Shdr *Shdr) { typedef typename ELFT::Verdef Elf_Verdef; typedef typename ELFT::Verdaux Elf_Verdaux; auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; S->Info = Shdr->sh_info; auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); if (!StringTableShdrOrErr) - return StringTableShdrOrErr.takeError(); + return errorToErrorCode(StringTableShdrOrErr.takeError()); auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); if (!StringTableOrErr) - return StringTableOrErr.takeError(); + return errorToErrorCode(StringTableOrErr.takeError()); auto Contents = Obj.getSectionContents(Shdr); if (!Contents) - return Contents.takeError(); + return errorToErrorCode(Contents.takeError()); llvm::ArrayRef Data = *Contents; const uint8_t *Buf = Data.data(); @@ -540,17 +544,17 @@ ELFDumper::dumpVerdefSection(const Elf_Shdr *Shdr) { } template -Expected +ErrorOr ELFDumper::dumpSymverSection(const Elf_Shdr *Shdr) { typedef typename ELFT::Half Elf_Half; auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; auto VersionsOrErr = Obj.template getSectionContentsAsArray(Shdr); if (!VersionsOrErr) - return VersionsOrErr.takeError(); + return errorToErrorCode(VersionsOrErr.takeError()); for (const Elf_Half &E : *VersionsOrErr) S->Entries.push_back(E); @@ -558,28 +562,28 @@ ELFDumper::dumpSymverSection(const Elf_Shdr *Shdr) { } template -Expected +ErrorOr ELFDumper::dumpVerneedSection(const Elf_Shdr *Shdr) { typedef typename ELFT::Verneed Elf_Verneed; typedef typename ELFT::Vernaux Elf_Vernaux; auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; S->Info = Shdr->sh_info; auto Contents = Obj.getSectionContents(Shdr); if (!Contents) - return Contents.takeError(); + return errorToErrorCode(Contents.takeError()); auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link); if (!StringTableShdrOrErr) - return StringTableShdrOrErr.takeError(); + return errorToErrorCode(StringTableShdrOrErr.takeError()); auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr); if (!StringTableOrErr) - return StringTableOrErr.takeError(); + return errorToErrorCode(StringTableOrErr.takeError()); llvm::ArrayRef Data = *Contents; const uint8_t *Buf = Data.data(); @@ -615,32 +619,32 @@ ELFDumper::dumpVerneedSection(const Elf_Shdr *Shdr) { } template -Expected ELFDumper::dumpGroup(const Elf_Shdr *Shdr) { +ErrorOr ELFDumper::dumpGroup(const Elf_Shdr *Shdr) { auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; auto SymtabOrErr = Obj.getSection(Shdr->sh_link); if (!SymtabOrErr) - return SymtabOrErr.takeError(); + return errorToErrorCode(SymtabOrErr.takeError()); // Get symbol with index sh_info which name is the signature of the group. const Elf_Shdr *Symtab = *SymtabOrErr; auto SymOrErr = Obj.getSymbol(Symtab, Shdr->sh_info); if (!SymOrErr) - return SymOrErr.takeError(); + return errorToErrorCode(SymOrErr.takeError()); auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab); if (!StrTabOrErr) - return StrTabOrErr.takeError(); + return errorToErrorCode(StrTabOrErr.takeError()); Expected SymbolName = getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab); if (!SymbolName) - return SymbolName.takeError(); + return errorToErrorCode(SymbolName.takeError()); S->Signature = *SymbolName; auto MembersOrErr = Obj.template getSectionContentsAsArray(Shdr); if (!MembersOrErr) - return MembersOrErr.takeError(); + return errorToErrorCode(MembersOrErr.takeError()); for (Elf_Word Member : *MembersOrErr) { if (Member == llvm::ELF::GRP_COMDAT) { @@ -650,27 +654,27 @@ Expected ELFDumper::dumpGroup(const Elf_Shdr *Shdr) { auto SHdrOrErr = Obj.getSection(Member); if (!SHdrOrErr) - return SHdrOrErr.takeError(); + return errorToErrorCode(SHdrOrErr.takeError()); auto NameOrErr = getUniquedSectionName(*SHdrOrErr); if (!NameOrErr) - return NameOrErr.takeError(); + return errorToErrorCode(NameOrErr.takeError()); S->Members.push_back({*NameOrErr}); } return S.release(); } template -Expected +ErrorOr ELFDumper::dumpMipsABIFlags(const Elf_Shdr *Shdr) { assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS && "Section type is not SHT_MIPS_ABIFLAGS"); auto S = make_unique(); - if (Error E = dumpCommonSection(Shdr, *S)) - return std::move(E); + if (std::error_code EC = dumpCommonSection(Shdr, *S)) + return EC; auto ContentOrErr = Obj.getSectionContents(Shdr); if (!ContentOrErr) - return ContentOrErr.takeError(); + return errorToErrorCode(ContentOrErr.takeError()); auto *Flags = reinterpret_cast *>( ContentOrErr.get().data()); @@ -689,20 +693,21 @@ ELFDumper::dumpMipsABIFlags(const Elf_Shdr *Shdr) { } template -static Error elf2yaml(raw_ostream &Out, const object::ELFFile &Obj) { +static std::error_code elf2yaml(raw_ostream &Out, + const object::ELFFile &Obj) { ELFDumper Dumper(Obj); - Expected YAMLOrErr = Dumper.dump(); - if (!YAMLOrErr) - return YAMLOrErr.takeError(); + ErrorOr YAMLOrErr = Dumper.dump(); + if (std::error_code EC = YAMLOrErr.getError()) + return EC; std::unique_ptr YAML(YAMLOrErr.get()); yaml::Output Yout(Out); Yout << *YAML; - return Error::success(); + return std::error_code(); } -Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { +std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { if (const auto *ELFObj = dyn_cast(&Obj)) return elf2yaml(Out, *ELFObj->getELFFile()); @@ -715,5 +720,5 @@ Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) { if (const auto *ELFObj = dyn_cast(&Obj)) return elf2yaml(Out, *ELFObj->getELFFile()); - llvm_unreachable("unknown ELF file format"); + return obj2yaml_error::unsupported_obj_file_format; } diff --git a/tools/obj2yaml/obj2yaml.cpp b/tools/obj2yaml/obj2yaml.cpp index f03b1ef4bad..8622e38319b 100644 --- a/tools/obj2yaml/obj2yaml.cpp +++ b/tools/obj2yaml/obj2yaml.cpp @@ -17,20 +17,19 @@ using namespace llvm; using namespace llvm::object; -static Error dumpObject(const ObjectFile &Obj) { +static std::error_code dumpObject(const ObjectFile &Obj) { if (Obj.isCOFF()) - return errorCodeToError(coff2yaml(outs(), cast(Obj))); + return coff2yaml(outs(), cast(Obj)); if (Obj.isXCOFF()) - return errorCodeToError(xcoff2yaml(outs(), cast(Obj))); + return xcoff2yaml(outs(), cast(Obj)); if (Obj.isELF()) return elf2yaml(outs(), Obj); - if (Obj.isWasm()) - return errorCodeToError(wasm2yaml(outs(), cast(Obj))); + return wasm2yaml(outs(), cast(Obj)); - return errorCodeToError(obj2yaml_error::unsupported_obj_file_format); + return obj2yaml_error::unsupported_obj_file_format; } static Error dumpInput(StringRef File) { @@ -45,7 +44,7 @@ static Error dumpInput(StringRef File) { return errorCodeToError(macho2yaml(outs(), Binary)); // TODO: If this is an archive, then burst it and dump each entry if (ObjectFile *Obj = dyn_cast(&Binary)) - return dumpObject(*Obj); + return errorCodeToError(dumpObject(*Obj)); if (MinidumpFile *Minidump = dyn_cast(&Binary)) return minidump2yaml(outs(), *Minidump); diff --git a/tools/obj2yaml/obj2yaml.h b/tools/obj2yaml/obj2yaml.h index 4f4a5330429..b40e2c5c5a6 100644 --- a/tools/obj2yaml/obj2yaml.h +++ b/tools/obj2yaml/obj2yaml.h @@ -21,7 +21,7 @@ std::error_code coff2yaml(llvm::raw_ostream &Out, const llvm::object::COFFObjectFile &Obj); -llvm::Error elf2yaml(llvm::raw_ostream &Out, +std::error_code elf2yaml(llvm::raw_ostream &Out, const llvm::object::ObjectFile &Obj); std::error_code macho2yaml(llvm::raw_ostream &Out, const llvm::object::Binary &Obj);