diff options
author | Kevin Enderby <enderby@apple.com> | 2016-05-02 20:28:12 +0000 |
---|---|---|
committer | Kevin Enderby <enderby@apple.com> | 2016-05-02 20:28:12 +0000 |
commit | a486dcaf360f13deb0a5c485a2b4a4e4652392e8 (patch) | |
tree | 023b46c9a2c7fcc414f1dceff6a92ef46a210c69 | |
parent | 6b3315d64b0b05c136590496ac27470a53afe127 (diff) |
Thread Expected<...> up from libObject’s getType() for symbols to allow llvm-objdump to produce a good error message.
Produce another specific error message for a malformed Mach-O file when a symbol’s
section index is more than the number of sections. The existing test case in test/Object/macho-invalid.test
for macho-invalid-section-index-getSectionRawName now reports the error with the message indicating
that a symbol at a specific index has a bad section index and that bad section index value.
Again converting interfaces to Expected<> from ErrorOr<> does involve
touching a number of places. Where the existing code reported the error with a
string message or an error code it was converted to do the same.
Also there some were bugs in the existing code that did not deal with the
old ErrorOr<> return values. So now with Expected<> since they must be
checked and the error handled, I added a TODO and a comment:
"// TODO: Actually report errors helpfully" and a call something like
consumeError(NameOrErr.takeError()) so the buggy code will not crash
since needed to deal with the Error.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268298 91177308-0d34-0410-b5e6-96231b3b80d8
26 files changed, 229 insertions, 106 deletions
diff --git a/include/llvm/Object/COFF.h b/include/llvm/Object/COFF.h index 1ba8ac1c225..7553f4bdaf7 100644 --- a/include/llvm/Object/COFF.h +++ b/include/llvm/Object/COFF.h @@ -684,8 +684,8 @@ protected: uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; uint32_t getSymbolFlags(DataRefImpl Symb) const override; - ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; - ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override; + Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; + Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; void moveSectionNext(DataRefImpl &Sec) const override; std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override; diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 38283f2825d..a0c06970847 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -200,10 +200,10 @@ protected: uint32_t getSymbolFlags(DataRefImpl Symb) const override; uint8_t getSymbolOther(DataRefImpl Symb) const override; uint8_t getSymbolELFType(DataRefImpl Symb) const override; - ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; - ErrorOr<section_iterator> getSymbolSection(const Elf_Sym *Symb, - const Elf_Shdr *SymTab) const; - ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override; + Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; + Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb, + const Elf_Shdr *SymTab) const; + Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; void moveSectionNext(DataRefImpl &Sec) const override; std::error_code getSectionName(DataRefImpl Sec, @@ -440,7 +440,7 @@ uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const { } template <class ELFT> -ErrorOr<SymbolRef::Type> +Expected<SymbolRef::Type> ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const { const Elf_Sym *ESym = getSymbol(Symb); @@ -512,12 +512,12 @@ uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const { } template <class ELFT> -ErrorOr<section_iterator> +Expected<section_iterator> ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym, const Elf_Shdr *SymTab) const { ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable); if (std::error_code EC = ESecOrErr.getError()) - return EC; + return errorCodeToError(EC); const Elf_Shdr *ESec = *ESecOrErr; if (!ESec) @@ -529,7 +529,7 @@ ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym, } template <class ELFT> -ErrorOr<section_iterator> +Expected<section_iterator> ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const { const Elf_Sym *Sym = getSymbol(Symb); const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a); diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index 852846890a1..611acf3124d 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -208,9 +208,9 @@ public: ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override; uint32_t getSymbolAlignment(DataRefImpl Symb) const override; uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; - ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; + Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; uint32_t getSymbolFlags(DataRefImpl Symb) const override; - ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override; + Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; unsigned getSymbolSectionID(SymbolRef Symb) const; unsigned getSectionID(SectionRef Sec) const; diff --git a/include/llvm/Object/ObjectFile.h b/include/llvm/Object/ObjectFile.h index 07656675e92..0a902a4590a 100644 --- a/include/llvm/Object/ObjectFile.h +++ b/include/llvm/Object/ObjectFile.h @@ -142,11 +142,11 @@ public: /// @brief Get the alignment of this symbol as the actual value (not log 2). uint32_t getAlignment() const; uint64_t getCommonSize() const; - ErrorOr<SymbolRef::Type> getType() const; + Expected<SymbolRef::Type> getType() const; /// @brief Get section this symbol is defined in reference to. Result is /// end_sections() if it is undefined or is an absolute symbol. - ErrorOr<section_iterator> getSection() const; + Expected<section_iterator> getSection() const; const ObjectFile *getObject() const; }; @@ -200,8 +200,8 @@ protected: virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0; virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const; virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0; - virtual ErrorOr<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0; - virtual ErrorOr<section_iterator> + virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0; + virtual Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const = 0; // Same as above for SectionRef. @@ -324,11 +324,11 @@ inline uint64_t SymbolRef::getCommonSize() const { return getObject()->getCommonSymbolSize(getRawDataRefImpl()); } -inline ErrorOr<section_iterator> SymbolRef::getSection() const { +inline Expected<section_iterator> SymbolRef::getSection() const { return getObject()->getSymbolSection(getRawDataRefImpl()); } -inline ErrorOr<SymbolRef::Type> SymbolRef::getType() const { +inline Expected<SymbolRef::Type> SymbolRef::getType() const { return getObject()->getSymbolType(getRawDataRefImpl()); } diff --git a/lib/DebugInfo/DWARF/DWARFContext.cpp b/lib/DebugInfo/DWARF/DWARFContext.cpp index 6aa4675630f..d3b5e736c48 100644 --- a/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -763,7 +763,17 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, } SymAddr = *SymAddrOrErr; // Also remember what section this symbol is in for later - RSec = *Sym->getSection(); + auto SectOrErr = Sym->getSection(); + if (!SectOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SectOrErr.takeError(), OS, ""); + OS.flush(); + errs() << "error: failed to get symbol section: " + << Buf << '\n'; + continue; + } + RSec = *SectOrErr; } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) { // MachO also has relocations that point to sections and // scattered relocations. diff --git a/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp index 2fb5a1090ab..a66722cb2c5 100644 --- a/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -119,9 +119,9 @@ std::error_code SymbolizableObjectFile::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize, DataExtractor *OpdExtractor, uint64_t OpdAddress) { - ErrorOr<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType(); - if (auto EC = SymbolTypeOrErr.getError()) - return EC; + Expected<SymbolRef::Type> SymbolTypeOrErr = Symbol.getType(); + if (!SymbolTypeOrErr) + return errorToErrorCode(SymbolTypeOrErr.takeError()); SymbolRef::Type SymbolType = *SymbolTypeOrErr; if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data) return std::error_code(); diff --git a/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp b/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp index 1fff153bf24..962e8d06397 100644 --- a/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp +++ b/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp @@ -113,9 +113,12 @@ void IntelJITEventListener::NotifyObjectEmitted( std::vector<LineNumberInfo> LineInfo; std::string SourceFileName; - ErrorOr<SymbolRef::Type> SymTypeOrErr = Sym.getType(); - if (!SymTypeOrErr) + Expected<SymbolRef::Type> SymTypeOrErr = Sym.getType(); + if (!SymTypeOrErr) { + // TODO: Actually report errors helpfully. + consumeError(SymTypeOrErr.takeError()); continue; + } SymbolRef::Type SymType = *SymTypeOrErr; if (SymType != SymbolRef::ST_Function) continue; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 5ac2db96245..a14a6bbd1e5 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -211,7 +211,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { if (auto SymTypeOrErr = I->getType()) SymType = *SymTypeOrErr; else - return errorCodeToError(SymTypeOrErr.getError()); + return SymTypeOrErr.takeError(); // Get symbol name. StringRef Name; @@ -252,7 +252,7 @@ RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { if (auto SIOrErr = I->getSection()) SI = *SIOrErr; else - return errorCodeToError(SIOrErr.getError()); + return SIOrErr.takeError(); if (SI == Obj.section_end()) continue; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index d36794dc704..a080b1374e4 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -877,7 +877,7 @@ Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj, if (auto TSIOrErr = TargetSymbol->getSection()) TSI = *TSIOrErr; else - return errorCodeToError(TSIOrErr.getError()); + return TSIOrErr.takeError(); assert(TSI != Obj.section_end() && "TSI should refer to a valid section"); bool IsCode = TSI->isText(); @@ -1210,9 +1210,14 @@ RuntimeDyldELF::processRelocationRef( RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end(); if (Symbol != Obj.symbol_end()) { gsi = GlobalSymbolTable.find(TargetName.data()); - ErrorOr<SymbolRef::Type> SymTypeOrErr = Symbol->getType(); - if (std::error_code EC = SymTypeOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType(); + if (!SymTypeOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymType = *SymTypeOrErr; } if (gsi != GlobalSymbolTable.end()) { @@ -1226,7 +1231,15 @@ RuntimeDyldELF::processRelocationRef( // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously // and can be changed by another developers. Maybe best way is add // a new symbol type ST_Section to SymbolRef and use it. - section_iterator si = *Symbol->getSection(); + auto SectionOrErr = Symbol->getSection(); + if (!SectionOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SectionOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } + section_iterator si = *SectionOrErr; if (si == Obj.section_end()) llvm_unreachable("Symbol section not found, bad object file format!"); DEBUG(dbgs() << "\t\tThis is section symbol\n"); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h index 3456b337197..4ce83798950 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h @@ -55,7 +55,15 @@ public: } StringRef TargetName = *TargetNameOrErr; - auto Section = *Symbol->getSection(); + auto SectionOrErr = Symbol->getSection(); + if (!SectionOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SectionOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } + auto Section = *SectionOrErr; uint64_t RelType = RelI->getType(); uint64_t Offset = RelI->getOffset(); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h index 7dd112e5f83..f3a32eefa3e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h @@ -117,7 +117,15 @@ public: symbol_iterator Symbol = RelI->getSymbol(); if (Symbol == Obj.symbol_end()) report_fatal_error("Unknown symbol in relocation"); - section_iterator SecI = *Symbol->getSection(); + auto SectionOrError = Symbol->getSection(); + if (!SectionOrError) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SectionOrError.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } + section_iterator SecI = *SectionOrError; // If there is no section, this must be an external reference. const bool IsExtern = SecI == Obj.section_end(); diff --git a/lib/Object/COFFObjectFile.cpp b/lib/Object/COFFObjectFile.cpp index a00d0c43e50..4b6ab23f5ff 100644 --- a/lib/Object/COFFObjectFile.cpp +++ b/lib/Object/COFFObjectFile.cpp @@ -178,7 +178,7 @@ ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const { return Result; } -ErrorOr<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const { +Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const { COFFSymbolRef Symb = getCOFFSymbol(Ref); int32_t SectionNumber = Symb.getSectionNumber(); @@ -234,14 +234,14 @@ uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const { return Symb.getValue(); } -ErrorOr<section_iterator> +Expected<section_iterator> COFFObjectFile::getSymbolSection(DataRefImpl Ref) const { COFFSymbolRef Symb = getCOFFSymbol(Ref); if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) return section_end(); const coff_section *Sec = nullptr; if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) - return EC; + return errorCodeToError(EC); DataRefImpl Ret; Ret.p = reinterpret_cast<uintptr_t>(Sec); return section_iterator(SectionRef(Ret, this)); diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index a182c4f5b9b..2240dc8b04a 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -508,7 +508,7 @@ uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const { return getNValue(DRI); } -ErrorOr<SymbolRef::Type> +Expected<SymbolRef::Type> MachOObjectFile::getSymbolType(DataRefImpl Symb) const { MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); uint8_t n_type = Entry.n_type; @@ -521,9 +521,9 @@ MachOObjectFile::getSymbolType(DataRefImpl Symb) const { case MachO::N_UNDF : return SymbolRef::ST_Unknown; case MachO::N_SECT : - ErrorOr<section_iterator> SecOrError = getSymbolSection(Symb); + Expected<section_iterator> SecOrError = getSymbolSection(Symb); if (!SecOrError) - return SecOrError.getError(); + return SecOrError.takeError(); section_iterator Sec = *SecOrError; if (Sec->isData() || Sec->isBSS()) return SymbolRef::ST_Data; @@ -571,7 +571,7 @@ uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const { return Result; } -ErrorOr<section_iterator> +Expected<section_iterator> MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb); uint8_t index = Entry.n_sect; @@ -581,9 +581,10 @@ MachOObjectFile::getSymbolSection(DataRefImpl Symb) const { DataRefImpl DRI; DRI.d.a = index - 1; if (DRI.d.a >= Sections.size()){ - // Diagnostic("bad section index (" + index + ") for symbol at index " + - // SymbolIndex); - return object_error::parse_failed; + return malformedError(*this, Twine("truncated or malformed object (bad " + "section index: ") + Twine((int)index) + Twine(" for " + "symbol at index ") + Twine(getSymbolIndex(Symb)) + + Twine(")")); } return section_iterator(SectionRef(DRI, this)); } diff --git a/lib/Object/Object.cpp b/lib/Object/Object.cpp index 58d8fa08235..6777b322da3 100644 --- a/lib/Object/Object.cpp +++ b/lib/Object/Object.cpp @@ -101,9 +101,14 @@ void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) { void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, LLVMSymbolIteratorRef Sym) { - ErrorOr<section_iterator> SecOrErr = (*unwrap(Sym))->getSection(); - if (std::error_code ec = SecOrErr.getError()) - report_fatal_error(ec.message()); + Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection(); + if (!SecOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SecOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } *unwrap(Sect) = *SecOrErr; } diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index 27a93688d5d..92f9c1f4f0a 100644 --- a/lib/Object/ObjectFile.cpp +++ b/lib/Object/ObjectFile.cpp @@ -29,9 +29,12 @@ ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) : SymbolicFile(Type, Source) {} bool SectionRef::containsSymbol(SymbolRef S) const { - ErrorOr<section_iterator> SymSec = S.getSection(); - if (!SymSec) + Expected<section_iterator> SymSec = S.getSection(); + if (!SymSec) { + // TODO: Actually report errors helpfully. + consumeError(SymSec.takeError()); return false; + } return *this == **SymSec; } diff --git a/test/Object/macho-invalid.test b/test/Object/macho-invalid.test index 5d83ce762fc..038f5a840f9 100644 --- a/test/Object/macho-invalid.test +++ b/test/Object/macho-invalid.test @@ -55,7 +55,7 @@ RUN: | FileCheck -check-prefix INVALID-SECTION-IDX-SYMBOL-SEC-pax %s INVALID-SECTION-IDX-SYMBOL-SEC-pax: 0000000100000000 0f 42 0010 00000065 __mh_execute_header RUN: not llvm-objdump -t %p/Inputs/macho-invalid-section-index-getSectionRawName 2>&1 \ RUN: | FileCheck -check-prefix INVALID-SECTION-IDX-SYMBOL-SEC-objdump %s -INVALID-SECTION-IDX-SYMBOL-SEC-objdump: Invalid data was encountered while parsing the file. +INVALID-SECTION-IDX-SYMBOL-SEC-objdump: truncated or malformed object (bad section index: 66 for symbol at index 8) RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-header 2>&1 | FileCheck -check-prefix INVALID-HEADER %s INVALID-HEADER: The file was not recognized as a valid object file diff --git a/tools/dsymutil/MachODebugMapParser.cpp b/tools/dsymutil/MachODebugMapParser.cpp index 0a8c4783930..476ac80fba2 100644 --- a/tools/dsymutil/MachODebugMapParser.cpp +++ b/tools/dsymutil/MachODebugMapParser.cpp @@ -436,9 +436,12 @@ void MachODebugMapParser::loadMainBinarySymbols( section_iterator Section = MainBinary.section_end(); MainBinarySymbolAddresses.clear(); for (const auto &Sym : MainBinary.symbols()) { - ErrorOr<SymbolRef::Type> TypeOrErr = Sym.getType(); - if (!TypeOrErr) + Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); + if (!TypeOrErr) { + // TODO: Actually report errors helpfully. + consumeError(TypeOrErr.takeError()); continue; + } SymbolRef::Type Type = *TypeOrErr; // Skip undefined and STAB entries. if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown)) @@ -449,9 +452,12 @@ void MachODebugMapParser::loadMainBinarySymbols( // addresses should be fetched for the debug map. if (!(Sym.getFlags() & SymbolRef::SF_Global)) continue; - ErrorOr<section_iterator> SectionOrErr = Sym.getSection(); - if (!SectionOrErr) + Expected<section_iterator> SectionOrErr = Sym.getSection(); + if (!SectionOrErr) { + // TODO: Actually report errors helpfully. + consumeError(SectionOrErr.takeError()); continue; + } Section = *SectionOrErr; if (Section == MainBinary.section_end() || Section->isText()) continue; diff --git a/tools/llvm-cxxdump/llvm-cxxdump.cpp b/tools/llvm-cxxdump/llvm-cxxdump.cpp index dc1cc23c232..206d65b6c20 100644 --- a/tools/llvm-cxxdump/llvm-cxxdump.cpp +++ b/tools/llvm-cxxdump/llvm-cxxdump.cpp @@ -178,8 +178,8 @@ static void dumpCXXData(const ObjectFile *Obj) { Expected<StringRef> SymNameOrErr = Sym.getName(); error(errorToErrorCode(SymNameOrErr.takeError())); StringRef SymName = *SymNameOrErr; - ErrorOr<object::section_iterator> SecIOrErr = Sym.getSection(); - error(SecIOrErr.getError()); + Expected<object::section_iterator> SecIOrErr = Sym.getSection(); + error(errorToErrorCode(SecIOrErr.takeError())); object::section_iterator SecI = *SecIOrErr; // Skip external symbols. if (SecI == Obj->section_end()) diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 05ebc44ff83..4c0d7535ec2 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -373,9 +373,10 @@ static void darwinPrintSymbol(SymbolicFile &Obj, SymbolListT::iterator I, outs() << "(?,?) "; break; } - ErrorOr<section_iterator> SecOrErr = + Expected<section_iterator> SecOrErr = MachO->getSymbolSection(I->Sym.getRawDataRefImpl()); - if (SecOrErr.getError()) { + if (!SecOrErr) { + consumeError(SecOrErr.takeError()); outs() << "(?,?) "; break; } @@ -697,9 +698,11 @@ static char getSymbolNMTypeChar(ELFObjectFileBase &Obj, // OK, this is ELF elf_symbol_iterator SymI(I); - ErrorOr<elf_section_iterator> SecIOrErr = SymI->getSection(); - if (error(SecIOrErr.getError())) + Expected<elf_section_iterator> SecIOrErr = SymI->getSection(); + if (!SecIOrErr) { + consumeError(SecIOrErr.takeError()); return '?'; + } elf_section_iterator SecI = *SecIOrErr; if (SecI != Obj.section_end()) { @@ -759,9 +762,11 @@ static char getSymbolNMTypeChar(COFFObjectFile &Obj, symbol_iterator I) { uint32_t Characteristics = 0; if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) { - ErrorOr<section_iterator> SecIOrErr = SymI->getSection(); - if (error(SecIOrErr.getError())) + Expected<section_iterator> SecIOrErr = SymI->getSection(); + if (!SecIOrErr) { + consumeError(SecIOrErr.takeError()); return '?'; + } section_iterator SecI = *SecIOrErr; const coff_section *Section = Obj.getCOFFSection(*SecI); Characteristics = Section->Characteristics; @@ -802,9 +807,11 @@ static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) { case MachO::N_INDR: return 'i'; case MachO::N_SECT: { - ErrorOr<section_iterator> SecOrErr = Obj.getSymbolSection(Symb); - if (SecOrErr.getError()) + Expected<section_iterator> SecOrErr = Obj.getSymbolSection(Symb); + if (!SecOrErr) { + consumeError(SecOrErr.takeError()); return 's'; + } section_iterator Sec = *SecOrErr; DataRefImpl Ref = Sec->getRawDataRefImpl(); StringRef SectionName; diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp index c20edfbdc68..6b98b72b5c1 100644 --- a/tools/llvm-objdump/COFFDump.cpp +++ b/tools/llvm-objdump/COFFDump.cpp @@ -165,9 +165,9 @@ resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym, if (std::error_code EC = ResolvedAddrOrErr.getError()) return EC; ResolvedAddr = *ResolvedAddrOrErr; - ErrorOr<section_iterator> Iter = Sym.getSection(); - if (std::error_code EC = Iter.getError()) - return EC; + Expected<section_iterator> Iter = Sym.getSection(); + if (!Iter) + return errorToErrorCode(Iter.takeError()); ResolvedSection = Obj->getCOFFSection(**Iter); return std::error_code(); } diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 72acabd59a8..232f17f4cd0 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -178,13 +178,23 @@ static const Target *GetTarget(const MachOObjectFile *MachOObj, struct SymbolSorter { bool operator()(const SymbolRef &A, const SymbolRef &B) { - ErrorOr<SymbolRef::Type> ATypeOrErr = A.getType(); - if (std::error_code EC = ATypeOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> ATypeOrErr = A.getType(); + if (!ATypeOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(ATypeOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type AType = *ATypeOrErr; - ErrorOr<SymbolRef::Type> BTypeOrErr = B.getType(); - if (std::error_code EC = BTypeOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> BTypeOrErr = B.getType(); + if (!BTypeOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(BTypeOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type BType = *BTypeOrErr; uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue(); uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue(); @@ -597,9 +607,14 @@ static void CreateSymbolAddressMap(MachOObjectFile *O, SymbolAddressMap *AddrMap) { // Create a map of symbol addresses to symbol names. for (const SymbolRef &Symbol : O->symbols()) { - ErrorOr<SymbolRef::Type> STOrErr = Symbol.getType(); - if (std::error_code EC = STOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> STOrErr = Symbol.getType(); + if (!STOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(STOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type ST = *STOrErr; if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || ST == SymbolRef::ST_Other) { @@ -6163,9 +6178,14 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, SymbolAddressMap AddrMap; bool DisSymNameFound = false; for (const SymbolRef &Symbol : MachOOF->symbols()) { - ErrorOr<SymbolRef::Type> STOrErr = Symbol.getType(); - if (std::error_code EC = STOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> STOrErr = Symbol.getType(); + if (!STOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(STOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type ST = *STOrErr; if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data || ST == SymbolRef::ST_Other) { @@ -6229,9 +6249,14 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, } StringRef SymName = *SymNameOrErr; - ErrorOr<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType(); - if (std::error_code EC = STOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType(); + if (!STOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(STOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type ST = *STOrErr; if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data) continue; @@ -6256,9 +6281,14 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, uint64_t NextSym = 0; uint64_t NextSymIdx = SymIdx + 1; while (Symbols.size() > NextSymIdx) { - ErrorOr<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType(); - if (std::error_code EC = STOrErr.getError()) - report_fatal_error(EC.message()); + Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType(); + if (!STOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(STOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } SymbolRef::Type NextSymType = *STOrErr; if (NextSymType == SymbolRef::ST_Function) { containsNextSym = @@ -6534,7 +6564,15 @@ static void findUnwindRelocNameAddend(const MachOObjectFile *Obj, // Go back one so that SymbolAddress <= Addr. --Sym; - section_iterator SymSection = *Sym->second.getSection(); + auto SectOrErr = Sym->second.getSection(); + if (!SectOrErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(SectOrErr.takeError(), OS, ""); + OS.flush(); + report_fatal_error(Buf); + } + section_iterator SymSection = *SectOrErr; if (RelocSection == *SymSection) { // There's a valid symbol in the same section before this reference. Expected<StringRef> NameOrErr = Sym->second.getName(); @@ -6882,7 +6920,13 @@ void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) { for (const SymbolRef &SymRef : Obj->symbols()) { // Discard any undefined or absolute symbols. They're not going to take part // in the convenience lookup for unwind info and just take up resources. - section_iterator Section = *SymRef.getSection(); + auto SectOrErr = SymRef.getSection(); + if (!SectOrErr) { + // TODO: Actually report errors helpfully. + consumeError(SectOrErr.takeError()); + continue; + } + section_iterator Section = *SectOrErr; if (Section == Obj->section_end()) continue; diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 2fcd59fe2b2..1eb9f61c6ac 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -490,9 +490,9 @@ static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj, const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl()); StringRef Target; if (symb->getType() == ELF::STT_SECTION) { - ErrorOr<section_iterator> SymSI = SI->getSection(); - if (std::error_code EC = SymSI.getError()) - return EC; + Expected<section_iterator> SymSI = SI->getSection(); + 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()) @@ -967,8 +967,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { if (Name->empty()) continue; - ErrorOr<section_iterator> SectionOrErr = Symbol.getSection(); - error(SectionOrErr.getError()); + Expected<section_iterator> SectionOrErr = Symbol.getSection(); + error(errorToErrorCode(SectionOrErr.takeError())); section_iterator SecI = *SectionOrErr; if (SecI == Obj->section_end()) continue; @@ -1357,12 +1357,13 @@ void llvm::PrintSymbolTable(const ObjectFile *o) { ErrorOr<uint64_t> AddressOrError = Symbol.getAddress(); error(AddressOrError.getError()); uint64_t Address = *AddressOrError; - ErrorOr<SymbolRef::Type> TypeOrError = Symbol.getType(); - error(TypeOrError.getError()); + Expected<SymbolRef::Type> TypeOrError = Symbol.getType(); + if (!TypeOrError) + report_error(o->getFileName(), TypeOrError.takeError()); SymbolRef::Type Type = *TypeOrError; uint32_t Flags = Symbol.getFlags(); - ErrorOr<section_iterator> SectionOrErr = Symbol.getSection(); - error(SectionOrErr.getError()); + Expected<section_iterator> SectionOrErr = Symbol.getSection(); + error(errorToErrorCode(SectionOrErr.takeError())); section_iterator Section = *SectionOrErr; StringRef Name; if (Type == SymbolRef::ST_Debug && Section != o->section_end()) { diff --git a/tools/llvm-readobj/ARMWinEHPrinter.cpp b/tools/llvm-readobj/ARMWinEHPrinter.cpp index 8d00c90b3c1..9e14e69015d 100644 --- a/tools/llvm-readobj/ARMWinEHPrinter.cpp +++ b/tools/llvm-readobj/ARMWinEHPrinter.cpp @@ -198,9 +198,9 @@ Decoder::getSectionContaining(const COFFObjectFile &COFF, uint64_t VA) { ErrorOr<object::SymbolRef> Decoder::getSymbol(const COFFObjectFile &COFF, uint64_t VA, bool FunctionOnly) { for (const auto &Symbol : COFF.symbols()) { - ErrorOr<SymbolRef::Type> Type = Symbol.getType(); - if (std::error_code EC = Type.getError()) - return EC; + Expected<SymbolRef::Type> Type = Symbol.getType(); + if (!Type) + return errorToErrorCode(Type.takeError()); if (FunctionOnly && *Type != SymbolRef::ST_Function) continue; @@ -648,9 +648,12 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF, SW.printString("ExceptionRecord", formatSymbol(*Name, Address)); - ErrorOr<section_iterator> SIOrErr = XDataRecord->getSection(); - if (!SIOrErr) + Expected<section_iterator> SIOrErr = XDataRecord->getSection(); + if (!SIOrErr) { + // TODO: Actually report errors helpfully. + consumeError(SIOrErr.takeError()); return false; + } section_iterator SI = *SIOrErr; return dumpXDataRecord(COFF, *SI, FunctionAddress, Address); diff --git a/tools/llvm-readobj/MachODumper.cpp b/tools/llvm-readobj/MachODumper.cpp index 2faf36842ed..cc57211b280 100644 --- a/tools/llvm-readobj/MachODumper.cpp +++ b/tools/llvm-readobj/MachODumper.cpp @@ -617,8 +617,8 @@ void MachODumper::printSymbol(const SymbolRef &Symbol) { getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol); StringRef SectionName = ""; - ErrorOr<section_iterator> SecIOrErr = Symbol.getSection(); - error(SecIOrErr.getError()); + Expected<section_iterator> SecIOrErr = Symbol.getSection(); + error(errorToErrorCode(SecIOrErr.takeError())); section_iterator SecI = *SecIOrErr; if (SecI != Obj->section_end()) error(SecI->getName(SectionName)); diff --git a/tools/llvm-readobj/Win64EHDumper.cpp b/tools/llvm-readobj/Win64EHDumper.cpp index e6e8367f293..f797d4baa40 100644 --- a/tools/llvm-readobj/Win64EHDumper.cpp +++ b/tools/llvm-readobj/Win64EHDumper.cpp @@ -153,7 +153,9 @@ static std::error_code resolveRelocation(const Dumper::Context &Ctx, return EC; ResolvedAddress = *ResolvedAddressOrErr; - ErrorOr<section_iterator> SI = Symbol.getSection(); + Expected<section_iterator> SI = Symbol.getSection(); + if (!SI) + return errorToErrorCode(SI.takeError()); ResolvedSection = Ctx.COFF.getCOFFSection(**SI); return std::error_code(); } diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index d65fde1d696..7d302f1faed 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -335,9 +335,12 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { // Use symbol info to iterate functions in the object. for (const auto &P : SymAddr) { object::SymbolRef Sym = P.first; - ErrorOr<SymbolRef::Type> TypeOrErr = Sym.getType(); - if (!TypeOrErr) + Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); + if (!TypeOrErr) { + // TODO: Actually report errors helpfully. + consumeError(TypeOrErr.takeError()); continue; + } SymbolRef::Type Type = *TypeOrErr; if (Type == object::SymbolRef::ST_Function) { Expected<StringRef> Name = Sym.getName(); @@ -356,7 +359,13 @@ static int printLineInfoForInput(bool LoadObjects, bool UseDebugObj) { // symbol in memory (rather than that in the unrelocated object file) // and use that to query the DWARFContext. if (!UseDebugObj && LoadObjects) { - object::section_iterator Sec = *Sym.getSection(); + auto SecOrErr = Sym.getSection(); + if (!SecOrErr) { + // TODO: Actually report errors helpfully. + consumeError(SecOrErr.takeError()); + continue; + } + object::section_iterator Sec = *SecOrErr; StringRef SecName; Sec->getName(SecName); uint64_t SectionLoadAddress = |