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 | 7bd8d994978d6404a79b28e04a802d070c45ba16 (patch) | |
tree | b827e6424c503f1ec157ad5dd700b9a0fee9bc06 /llvm/lib | |
parent | 34c549ea02081e48946750adbc928ae57ff67bd9 (diff) | |
download | bcm5719-llvm-7bd8d994978d6404a79b28e04a802d070c45ba16.tar.gz bcm5719-llvm-7bd8d994978d6404a79b28e04a802d070c45ba16.zip |
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.
llvm-svn: 268298
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp | 7 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp | 23 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h | 10 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h | 10 | ||||
-rw-r--r-- | llvm/lib/Object/COFFObjectFile.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/Object/MachOObjectFile.cpp | 15 | ||||
-rw-r--r-- | llvm/lib/Object/Object.cpp | 11 | ||||
-rw-r--r-- | llvm/lib/Object/ObjectFile.cpp | 7 |
11 files changed, 81 insertions, 30 deletions
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 6aa4675630f..d3b5e736c48 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/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/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp index 2fb5a1090ab..a66722cb2c5 100644 --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/llvm/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/llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp b/llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp index 1fff153bf24..962e8d06397 100644 --- a/llvm/lib/ExecutionEngine/IntelJITEvents/IntelJITEventListener.cpp +++ b/llvm/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/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 5ac2db96245..a14a6bbd1e5 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/llvm/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/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index d36794dc704..a080b1374e4 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/llvm/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/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h index 3456b337197..4ce83798950 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h +++ b/llvm/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/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h index 7dd112e5f83..f3a32eefa3e 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ b/llvm/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/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp index a00d0c43e50..4b6ab23f5ff 100644 --- a/llvm/lib/Object/COFFObjectFile.cpp +++ b/llvm/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/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index a182c4f5b9b..2240dc8b04a 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/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/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp index 58d8fa08235..6777b322da3 100644 --- a/llvm/lib/Object/Object.cpp +++ b/llvm/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/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp index 27a93688d5d..92f9c1f4f0a 100644 --- a/llvm/lib/Object/ObjectFile.cpp +++ b/llvm/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; } |