Index: llvm/trunk/test/tools/llvm-readobj/gnu-notes.test =================================================================== --- llvm/trunk/test/tools/llvm-readobj/gnu-notes.test +++ llvm/trunk/test/tools/llvm-readobj/gnu-notes.test @@ -79,10 +79,10 @@ ## that goes past the end of file. # RUN: yaml2obj --docnum=2 %s > %t2.so -# RUN: not llvm-readelf --notes %t2.so 2>&1 | FileCheck %s --check-prefix=ERR1 -# RUN: not llvm-readobj --notes %t2.so 2>&1 | FileCheck %s --check-prefix=ERR1 +# RUN: not llvm-readelf --notes %t2.so 2>&1 | FileCheck -DFILE=%t2.so %s --check-prefix=ERR1 +# RUN: not llvm-readobj --notes %t2.so 2>&1 | FileCheck -DFILE=%t2.so %s --check-prefix=ERR1 -# ERR1: error: SHT_NOTE section [index 1] has invalid offset (0xffff0000) or size (0x0) +# ERR1: error: '[[FILE]]': SHT_NOTE section [index 1] has invalid offset (0xffff0000) or size (0x0) --- !ELF FileHeader: @@ -99,10 +99,10 @@ ## that goes past the end of file. # RUN: yaml2obj --docnum=3 %s > %t3.so -# RUN: not llvm-readelf --notes %t3.so 2>&1 | FileCheck %s --check-prefix=ERR2 -# RUN: not llvm-readobj --notes %t3.so 2>&1 | FileCheck %s --check-prefix=ERR2 +# RUN: not llvm-readelf --notes %t3.so 2>&1 | FileCheck -DFILE=%t3.so %s --check-prefix=ERR2 +# RUN: not llvm-readobj --notes %t3.so 2>&1 | FileCheck -DFILE=%t3.so %s --check-prefix=ERR2 -# ERR2: error: SHT_NOTE section [index 1] has invalid offset (0x180) or size (0xffff0000) +# ERR2: error: '[[FILE]]': SHT_NOTE section [index 1] has invalid offset (0x180) or size (0xffff0000) --- !ELF FileHeader: @@ -119,10 +119,10 @@ ## goes past the end of file. # RUN: yaml2obj --docnum=4 %s > %t4.so -# RUN: not llvm-readelf --notes %t4.so 2>&1 | FileCheck %s --check-prefix=ERR3 -# RUN: not llvm-readobj --notes %t4.so 2>&1 | FileCheck %s --check-prefix=ERR3 +# RUN: not llvm-readelf --notes %t4.so 2>&1 | FileCheck -DFILE=%t4.so %s --check-prefix=ERR3 +# RUN: not llvm-readobj --notes %t4.so 2>&1 | FileCheck -DFILE=%t4.so %s --check-prefix=ERR3 -# ERR3: error: PT_NOTE header has invalid offset (0xffff0000) or size (0x0) +# ERR3: error: '[[FILE]]': PT_NOTE header has invalid offset (0xffff0000) or size (0x0) --- !ELF FileHeader: @@ -143,10 +143,10 @@ ## goes past the end of file. # RUN: yaml2obj --docnum=5 %s > %t5.so -# RUN: not llvm-readelf --notes %t5.so 2>&1 | FileCheck %s --check-prefix=ERR4 -# RUN: not llvm-readobj --notes %t5.so 2>&1 | FileCheck %s --check-prefix=ERR4 +# RUN: not llvm-readelf --notes %t5.so 2>&1 | FileCheck -DFILE=%t5.so %s --check-prefix=ERR4 +# RUN: not llvm-readobj --notes %t5.so 2>&1 | FileCheck -DFILE=%t5.so %s --check-prefix=ERR4 -# ERR4: error: PT_NOTE header has invalid offset (0x1b8) or size (0xffff0000) +# ERR4: error: '[[FILE]]': PT_NOTE header has invalid offset (0x1b8) or size (0xffff0000) --- !ELF FileHeader: Index: llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h =================================================================== --- llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h +++ llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h @@ -370,7 +370,7 @@ return readobj_error::unknown_symbol; auto StrTableOrErr = ELF->getStringTableForSymtab(*Symtab); if (!StrTableOrErr) - error(StrTableOrErr.takeError()); + reportError(StrTableOrErr.takeError(), FileName); StringRef StrTable = *StrTableOrErr; for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF->symbols(Symtab))) @@ -405,7 +405,7 @@ auto SymTabOrErr = ELF->getSection(Sec.sh_link); if (!SymTabOrErr) - error(SymTabOrErr.takeError()); + reportError(SymTabOrErr.takeError(), FileName); const Elf_Shdr *SymTab = *SymTabOrErr; for (const Elf_Rel &R : unwrapOrError(FileName, ELF->rels(&Sec))) { Index: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp =================================================================== --- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp +++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp @@ -912,26 +912,33 @@ // The section consists of a number of subsection in the following format: // |SubSectionType|SubSectionSize|Contents...| uint32_t SubType, SubSectionSize; - error(Reader.readInteger(SubType)); - error(Reader.readInteger(SubSectionSize)); + + if (Error E = Reader.readInteger(SubType)) + reportError(std::move(E), Obj->getFileName()); + if (Error E = Reader.readInteger(SubSectionSize)) + reportError(std::move(E), Obj->getFileName()); StringRef Contents; - error(Reader.readFixedString(Contents, SubSectionSize)); + if (Error E = Reader.readFixedString(Contents, SubSectionSize)) + reportError(std::move(E), Obj->getFileName()); BinaryStreamRef ST(Contents, support::little); switch (DebugSubsectionKind(SubType)) { case DebugSubsectionKind::FileChecksums: - error(CVFileChecksumTable.initialize(ST)); + if (Error E = CVFileChecksumTable.initialize(ST)) + reportError(std::move(E), Obj->getFileName()); break; case DebugSubsectionKind::StringTable: - error(CVStringTable.initialize(ST)); + if (Error E = CVStringTable.initialize(ST)) + reportError(std::move(E), Obj->getFileName()); break; default: break; } uint32_t PaddedSize = alignTo(SubSectionSize, 4); - error(Reader.skip(PaddedSize - SubSectionSize)); + if (Error E = Reader.skip(PaddedSize - SubSectionSize)) + reportError(std::move(E), Obj->getFileName()); } } @@ -949,7 +956,9 @@ W.printNumber("Section", SectionName, Obj->getSectionID(Section)); uint32_t Magic; - error(consume(Data, Magic)); + if (Error E = consume(Data, Magic)) + reportError(std::move(E), Obj->getFileName()); + W.printHex("Magic", Magic); if (Magic != COFF::DEBUG_SECTION_MAGIC) return error(object_error::parse_failed); @@ -962,8 +971,10 @@ // The section consists of a number of subsection in the following format: // |SubSectionType|SubSectionSize|Contents...| uint32_t SubType, SubSectionSize; - error(consume(Data, SubType)); - error(consume(Data, SubSectionSize)); + if (Error E = consume(Data, SubType)) + reportError(std::move(E), Obj->getFileName()); + if (Error E = consume(Data, SubSectionSize)) + reportError(std::move(E), Obj->getFileName()); ListScope S(W, "Subsection"); // Dump the subsection as normal even if the ignore bit is set. @@ -1038,7 +1049,8 @@ BinaryStreamReader SR(Contents, llvm::support::little); DebugFrameDataSubsectionRef FrameData; - error(FrameData.initialize(SR)); + if (Error E = FrameData.initialize(SR)) + reportError(std::move(E), Obj->getFileName()); StringRef LinkageName; error(resolveSymbolName(Obj->getCOFFSection(Section), SectionContents, @@ -1100,7 +1112,8 @@ BinaryStreamReader Reader(FunctionLineTables[Name], support::little); DebugLinesSubsectionRef LineInfo; - error(LineInfo.initialize(Reader)); + if (Error E = LineInfo.initialize(Reader)) + reportError(std::move(E), Obj->getFileName()); W.printHex("Flags", LineInfo.header()->Flags); W.printHex("CodeSize", LineInfo.header()->CodeSize); @@ -1154,9 +1167,9 @@ error(object_error::parse_failed); } - if (auto EC = CVSD.dump(Symbols)) { + if (Error E = CVSD.dump(Symbols)) { W.flush(); - error(std::move(EC)); + reportError(std::move(E), Obj->getFileName()); } CompilationCPUType = CVSD.getCompilationCPUType(); W.flush(); @@ -1165,7 +1178,8 @@ void COFFDumper::printCodeViewFileChecksums(StringRef Subsection) { BinaryStreamRef Stream(Subsection, llvm::support::little); DebugChecksumsSubsectionRef Checksums; - error(Checksums.initialize(Stream)); + if (Error E = Checksums.initialize(Stream)) + reportError(std::move(E), Obj->getFileName()); for (auto &FC : Checksums) { DictScope S(W, "FileChecksum"); @@ -1184,7 +1198,8 @@ void COFFDumper::printCodeViewInlineeLines(StringRef Subsection) { BinaryStreamReader SR(Subsection, llvm::support::little); DebugInlineeLinesSubsectionRef Lines; - error(Lines.initialize(SR)); + if (Error E = Lines.initialize(SR)) + reportError(std::move(E), Obj->getFileName()); for (auto &Line : Lines) { DictScope S(W, "InlineeSourceLine"); @@ -1232,7 +1247,9 @@ if (SectionName == ".debug$T") { StringRef Data = unwrapOrError(Obj->getFileName(), S.getContents()); uint32_t Magic; - error(consume(Data, Magic)); + if (Error E = consume(Data, Magic)) + reportError(std::move(E), Obj->getFileName()); + if (Magic != 4) error(object_error::parse_failed); @@ -1248,14 +1265,14 @@ if (GHash) { std::vector Hashes = GloballyHashedType::hashTypes(Types); - if (auto EC = + if (Error E = mergeTypeAndIdRecords(GlobalCVIDs, GlobalCVTypes, SourceToDest, Types, Hashes, PCHSignature)) - return error(std::move(EC)); + return reportError(std::move(E), Obj->getFileName()); } else { - if (auto EC = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types, + if (Error E = mergeTypeAndIdRecords(CVIDs, CVTypes, SourceToDest, Types, PCHSignature)) - return error(std::move(EC)); + return reportError(std::move(E), Obj->getFileName()); } } } @@ -1271,7 +1288,9 @@ W.printBinaryBlock("Data", Data); uint32_t Magic; - error(consume(Data, Magic)); + if (Error E = consume(Data, Magic)) + reportError(std::move(E), Obj->getFileName()); + W.printHex("Magic", Magic); if (Magic != COFF::DEBUG_SECTION_MAGIC) return error(object_error::parse_failed); @@ -1279,7 +1298,9 @@ Types.reset(Data, 100); TypeDumpVisitor TDV(Types, &W, opts::CodeViewSubsectionBytes); - error(codeview::visitTypeStream(Types, TDV)); + if (Error E = codeview::visitTypeStream(Types, TDV)) + reportError(std::move(E), Obj->getFileName()); + W.flush(); } @@ -1504,7 +1525,7 @@ error(EC); Expected Res = getSectionName(Obj, AuxNumber, Assoc); if (!Res) - error(Res.takeError()); + reportError(Res.takeError(), Obj->getFileName()); AssocName = *Res; W.printNumber("AssocSection", AssocName, AuxNumber); @@ -1906,7 +1927,8 @@ { ListScope S(Writer, "MergedTypeStream"); TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); - error(codeview::visitTypeStream(TpiTypes, TDV)); + if (Error Err = codeview::visitTypeStream(TpiTypes, TDV)) + reportError(std::move(Err), ""); Writer.flush(); } @@ -1917,7 +1939,8 @@ ListScope S(Writer, "MergedIDStream"); TypeDumpVisitor TDV(TpiTypes, &Writer, opts::CodeViewSubsectionBytes); TDV.setIpiTypes(IpiTypes); - error(codeview::visitTypeStream(IpiTypes, TDV)); + if (Error Err = codeview::visitTypeStream(IpiTypes, TDV)) + reportError(std::move(Err), ""); Writer.flush(); } } Index: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp +++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp @@ -4417,7 +4417,7 @@ for (const auto &Note : Obj->notes(P, Err)) ProcessNote(Note); if (Err) - error(std::move(Err)); + reportError(std::move(Err), this->FileName); } } else { for (const auto &S : @@ -4429,7 +4429,7 @@ for (const auto &Note : Obj->notes(S, Err)) ProcessNote(Note); if (Err) - error(std::move(Err)); + reportError(std::move(Err), this->FileName); } } } @@ -4483,10 +4483,10 @@ // integer. if (*Offset == PrevOffset) reportError( - FileStr, createStringError(object_error::parse_failed, "could not extract a valid stack size in section %s", - SectionName.data())); + SectionName.data()), + FileStr); printStackSizeEntry(StackSize, FuncName); } @@ -4545,11 +4545,12 @@ uint64_t Offset = Reloc.getOffset(); if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) - reportError(FileStr, createStringError( - object_error::parse_failed, - "found invalid relocation offset into section %s " - "while trying to extract a stack size entry", - StackSizeSectionName.data())); + reportError( + createStringError(object_error::parse_failed, + "found invalid relocation offset into section %s " + "while trying to extract a stack size entry", + StackSizeSectionName.data()), + FileStr); uint64_t Addend = Data.getAddress(&Offset); uint64_t SymValue = Resolver(Reloc, RelocSymValue, Addend); @@ -4595,11 +4596,11 @@ // size. Check for an extra byte before we try to process the entry. if (!Data.isValidOffsetForDataOfSize(Offset, sizeof(Elf_Addr) + 1)) { reportError( - FileStr, createStringError( object_error::parse_failed, "section %s ended while trying to extract a stack size entry", - SectionName.data())); + SectionName.data()), + FileStr); } uint64_t SymValue = Data.getAddress(&Offset); printFunctionStackSize(Obj, SymValue, @@ -4689,10 +4690,10 @@ RelocSec.getName(RelocSectionName); StringRef RelocName = EF->getRelocationTypeName(Reloc.getType()); reportError( - FileStr, createStringError(object_error::parse_failed, "unsupported relocation type in section %s: %s", - RelocSectionName.data(), RelocName.data())); + RelocSectionName.data(), RelocName.data()), + FileStr); } this->printStackSize(Obj, Reloc, FunctionSec, StackSizeSectionName, Resolver, Data); @@ -5568,7 +5569,7 @@ for (const auto &Note : Obj->notes(P, Err)) ProcessNote(Note); if (Err) - error(std::move(Err)); + reportError(std::move(Err), this->FileName); } } else { for (const auto &S : unwrapOrError(this->FileName, Obj->sections())) { @@ -5580,7 +5581,7 @@ for (const auto &Note : Obj->notes(S, Err)) ProcessNote(Note); if (Err) - error(std::move(Err)); + reportError(std::move(Err), this->FileName); } } } Index: llvm/trunk/tools/llvm-readobj/Win64EHDumper.cpp =================================================================== --- llvm/trunk/tools/llvm-readobj/Win64EHDumper.cpp +++ llvm/trunk/tools/llvm-readobj/Win64EHDumper.cpp @@ -289,7 +289,9 @@ resolveRelocation(Ctx, Section, SectionOffset + 8, XData, Offset); ArrayRef Contents; - error(Ctx.COFF.getSectionContents(XData, Contents)); + if (Error E = Ctx.COFF.getSectionContents(XData, Contents)) + reportError(std::move(E), Ctx.COFF.getFileName()); + if (Contents.empty()) return; @@ -311,7 +313,9 @@ const coff_section *PData = Ctx.COFF.getCOFFSection(Section); ArrayRef Contents; - error(Ctx.COFF.getSectionContents(PData, Contents)); + + if (Error E = Ctx.COFF.getSectionContents(PData, Contents)) + reportError(std::move(E), Ctx.COFF.getFileName()); if (Contents.empty()) continue; Index: llvm/trunk/tools/llvm-readobj/llvm-readobj.h =================================================================== --- llvm/trunk/tools/llvm-readobj/llvm-readobj.h +++ llvm/trunk/tools/llvm-readobj/llvm-readobj.h @@ -22,17 +22,16 @@ // Various helper functions. LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg); - void reportError(StringRef Input, Error Err); + void reportError(Error Err, StringRef Input); void reportWarning(Twine Msg); void reportWarning(StringRef Input, Error Err); void warn(llvm::Error Err); void error(std::error_code EC); - void error(llvm::Error EC); template T unwrapOrError(StringRef Input, Expected EO) { if (EO) return *EO; - reportError(Input, EO.takeError()); + reportError(EO.takeError(), Input); llvm_unreachable("reportError shouldn't return in this case"); } } // namespace llvm Index: llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp +++ llvm/trunk/tools/llvm-readobj/llvm-readobj.cpp @@ -382,10 +382,12 @@ exit(1); } -void reportError(StringRef Input, Error Err) { +void reportError(Error Err, StringRef Input) { + assert(Err); if (Input == "-") Input = ""; - error(createFileError(Input, std::move(Err))); + handleAllErrors(createFileError(Input, std::move(Err)), + [&](const ErrorInfoBase &EI) { reportError(EI.message()); }); } void reportWarning(Twine Msg) { @@ -406,13 +408,6 @@ }); } -void error(Error EC) { - if (!EC) - return; - handleAllErrors(std::move(EC), - [&](const ErrorInfoBase &EI) { reportError(EI.message()); }); -} - void error(std::error_code EC) { if (!EC) return; @@ -421,8 +416,9 @@ } // namespace llvm -static void reportError(StringRef Input, std::error_code EC) { - reportError(Input, errorCodeToError(EC)); +static void reportError(std::error_code EC, StringRef Input) { + assert(EC != readobj_error::success); + reportError(errorCodeToError(EC), Input); } static bool isMipsArch(unsigned Arch) { @@ -482,7 +478,7 @@ std::unique_ptr Dumper; if (std::error_code EC = createDumper(Obj, Writer, Dumper)) - reportError(FileStr, EC); + reportError(EC, FileStr); if (opts::Output == opts::LLVM || opts::InputFilenames.size() > 1 || A) { Writer.startLine() << "\n"; @@ -604,9 +600,8 @@ for (auto &Child : Arc->children(Err)) { Expected> ChildOrErr = Child.getAsBinary(); if (!ChildOrErr) { - if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) { - reportError(Arc->getFileName(), std::move(E)); - } + if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) + reportError(std::move(E), Arc->getFileName()); continue; } if (ObjectFile *Obj = dyn_cast(&*ChildOrErr.get())) @@ -614,10 +609,10 @@ else if (COFFImportFile *Imp = dyn_cast(&*ChildOrErr.get())) dumpCOFFImportFile(Imp, Writer); else - reportError(Arc->getFileName(), readobj_error::unrecognized_file_format); + reportError(readobj_error::unrecognized_file_format, Arc->getFileName()); } if (Err) - reportError(Arc->getFileName(), std::move(Err)); + reportError(std::move(Err), Arc->getFileName()); } /// Dumps each object file in \a MachO Universal Binary; @@ -627,9 +622,8 @@ Expected> ObjOrErr = Obj.getAsObjectFile(); if (ObjOrErr) dumpObject(&*ObjOrErr.get(), Writer); - else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { - reportError(UBinary->getFileName(), ObjOrErr.takeError()); - } + else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) + reportError(ObjOrErr.takeError(), UBinary->getFileName()); else if (Expected> AOrErr = Obj.getAsArchive()) dumpArchive(&*AOrErr.get(), Writer); } @@ -640,7 +634,7 @@ ScopedPrinter &Printer) { WindowsRes::Dumper Dumper(WinRes, Printer); if (auto Err = Dumper.printData()) - reportError(WinRes->getFileName(), std::move(Err)); + reportError(std::move(Err), WinRes->getFileName()); } @@ -649,7 +643,7 @@ // Attempt to open the binary. Expected> BinaryOrErr = createBinary(File); if (!BinaryOrErr) - reportError(File, BinaryOrErr.takeError()); + reportError(BinaryOrErr.takeError(), File); Binary &Binary = *BinaryOrErr.get().getBinary(); if (Archive *Arc = dyn_cast(&Binary)) @@ -664,7 +658,7 @@ else if (WindowsResource *WinRes = dyn_cast(&Binary)) dumpWindowsResourceFile(WinRes, Writer); else - reportError(File, readobj_error::unrecognized_file_format); + reportError(readobj_error::unrecognized_file_format, File); CVTypes.Binaries.push_back(std::move(*BinaryOrErr)); }