From fbd189d3dfd5b2d029e9da250437aaeafe941d86 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Wed, 16 Nov 2016 22:17:38 +0000 Subject: [PATCH] General clean up of error handling in llvm-objdump to remove its use of report_fatal_error(). No real functional change with this commit. The problem with report_fatal_error() is it does not include the tool name and the file name the for which the error message was generated. Uses of report_fatal_error() were change to report_error() or error() to get a better error and to make the code smaller and cleaner. Also changed things like error(errorToErrorCode(SOrErr.takeError())) to use report_error() with a file name and the llvm::Error (as well as the ArchitectureName if available) so the error message is printed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287163 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../llvm-objdump/X86/malformed-machos.test | 3 + tools/llvm-objdump/llvm-objdump.cpp | 93 +++++++++++-------- tools/llvm-objdump/llvm-objdump.h | 1 + 3 files changed, 57 insertions(+), 40 deletions(-) diff --git a/test/tools/llvm-objdump/X86/malformed-machos.test b/test/tools/llvm-objdump/X86/malformed-machos.test index 9db87de889e..83ebfc4364e 100644 --- a/test/tools/llvm-objdump/X86/malformed-machos.test +++ b/test/tools/llvm-objdump/X86/malformed-machos.test @@ -55,6 +55,9 @@ INVALID-SYMBOL-INDR-ARCHIVE-UNIVERSAL: macho-invalid-symbol-indr-archive-univers RUN: not llvm-objdump -macho -disassemble %p/Inputs/macho-invalid-symbol-strx 2>&1 | FileCheck -check-prefix INVALID-SYMBOL-STRX %s INVALID-SYMBOL-STRX: macho-invalid-symbol-strx': truncated or malformed object (bad string table index: 22 past the end of string table, for symbol at index 1) +RUN: not llvm-objdump -disassemble %p/Inputs/macho-invalid-symbol-strx 2>&1 | FileCheck -check-prefix INVALID-SYMBOL-STRX-NO-MACHO-FLAG %s +INVALID-SYMBOL-STRX-NO-MACHO-FLAG: macho-invalid-symbol-strx': truncated or malformed object (bad string index: 22 for symbol at index 1) + RUN: not llvm-objdump -macho -disassemble -arch all %p/Inputs/macho-invalid-symbol-strx-universal 2>&1 | FileCheck -check-prefix INVALID-SYMBOL-STRX-UNIVERSAL %s INVALID-SYMBOL-STRX-UNIVERSAL: macho-invalid-symbol-strx-universal' (for architecture i386): truncated or malformed object (bad string table index: 22 past the end of string table, for symbol at index 1) diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index be44c7450d6..cbb74a3abd2 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -292,6 +292,12 @@ LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) { exit(1); } +LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, + Twine Message) { + errs() << ToolName << ": '" << File << "': " << Message << ".\n"; + exit(1); +} + LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, std::error_code EC) { assert(EC); @@ -370,8 +376,12 @@ static const Target *getTarget(const ObjectFile *Obj = nullptr) { std::string Error; const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, Error); - if (!TheTarget) - report_fatal_error("can't find target: " + Error); + if (!TheTarget) { + if (Obj) + report_error(Obj->getFileName(), "can't find target: " + Error); + else + error("can't find target: " + Error); + } // Update the triple name and return the found target. TripleName = TheTriple.getTriple(); @@ -772,23 +782,13 @@ static void printRelocationTargetName(const MachOObjectFile *O, for (const SymbolRef &Symbol : O->symbols()) { std::error_code ec; Expected Addr = Symbol.getAddress(); - if (!Addr) { - std::string Buf; - raw_string_ostream OS(Buf); - logAllUnhandledErrors(Addr.takeError(), OS, ""); - OS.flush(); - report_fatal_error(Buf); - } + if (!Addr) + report_error(O->getFileName(), Addr.takeError()); if (*Addr != Val) continue; Expected Name = Symbol.getName(); - if (!Name) { - std::string Buf; - raw_string_ostream OS(Buf); - logAllUnhandledErrors(Name.takeError(), OS, ""); - OS.flush(); - report_fatal_error(Buf); - } + if (!Name) + report_error(O->getFileName(), Name.takeError()); fmt << *Name; return; } @@ -803,7 +803,7 @@ static void printRelocationTargetName(const MachOObjectFile *O, if (Addr != Val) continue; if ((ec = Section.getName(Name))) - report_fatal_error(ec.message()); + report_error(O->getFileName(), ec); fmt << Name; return; } @@ -820,7 +820,8 @@ static void printRelocationTargetName(const MachOObjectFile *O, symbol_iterator SI = O->symbol_begin(); advance(SI, Val); Expected SOrErr = SI->getName(); - error(errorToErrorCode(SOrErr.takeError())); + if (!SOrErr) + report_error(O->getFileName(), SOrErr.takeError()); S = *SOrErr; } else { section_iterator SI = O->section_begin(); @@ -871,8 +872,8 @@ static std::error_code getRelocationValueString(const MachOObjectFile *Obj, // NOTE: Scattered relocations don't exist on x86_64. unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::X86_64_RELOC_UNSIGNED) - report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " - "X86_64_RELOC_SUBTRACTOR."); + report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after " + "X86_64_RELOC_SUBTRACTOR."); // The X86_64_RELOC_UNSIGNED contains the minuend symbol; // X86_64_RELOC_SUBTRACTOR contains the subtrahend. @@ -920,8 +921,8 @@ static std::error_code getRelocationValueString(const MachOObjectFile *Obj, unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::GENERIC_RELOC_PAIR) - report_fatal_error("Expected GENERIC_RELOC_PAIR after " - "GENERIC_RELOC_SECTDIFF."); + report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " + "GENERIC_RELOC_SECTDIFF."); printRelocationTargetName(Obj, RE, fmt); fmt << "-"; @@ -941,8 +942,8 @@ static std::error_code getRelocationValueString(const MachOObjectFile *Obj, // GENERIC_RELOC_PAIR. unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::GENERIC_RELOC_PAIR) - report_fatal_error("Expected GENERIC_RELOC_PAIR after " - "GENERIC_RELOC_LOCAL_SECTDIFF."); + report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " + "GENERIC_RELOC_LOCAL_SECTDIFF."); printRelocationTargetName(Obj, RE, fmt); fmt << "-"; @@ -981,8 +982,8 @@ static std::error_code getRelocationValueString(const MachOObjectFile *Obj, // ARM_RELOC_PAIR. unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::ARM_RELOC_PAIR) - report_fatal_error("Expected ARM_RELOC_PAIR after " - "ARM_RELOC_HALF"); + report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after " + "ARM_RELOC_HALF"); // NOTE: The half of the target virtual address is stashed in the // address field of the secondary relocation, but we can't reverse @@ -1083,20 +1084,24 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr MRI( TheTarget->createMCRegInfo(TripleName)); if (!MRI) - report_fatal_error("error: no register info for target " + TripleName); + report_error(Obj->getFileName(), "no register info for target " + + TripleName); // Set up disassembler. std::unique_ptr AsmInfo( TheTarget->createMCAsmInfo(*MRI, TripleName)); if (!AsmInfo) - report_fatal_error("error: no assembly info for target " + TripleName); + report_error(Obj->getFileName(), "no assembly info for target " + + TripleName); std::unique_ptr STI( TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); if (!STI) - report_fatal_error("error: no subtarget info for target " + TripleName); + report_error(Obj->getFileName(), "no subtarget info for target " + + TripleName); std::unique_ptr MII(TheTarget->createMCInstrInfo()); if (!MII) - report_fatal_error("error: no instruction info for target " + TripleName); + report_error(Obj->getFileName(), "no instruction info for target " + + TripleName); MCObjectFileInfo MOFI; MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI); // FIXME: for now initialize MCObjectFileInfo with default values @@ -1105,7 +1110,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr DisAsm( TheTarget->createMCDisassembler(*STI, Ctx)); if (!DisAsm) - report_fatal_error("error: no disassembler for target " + TripleName); + report_error(Obj->getFileName(), "no disassembler for target " + + TripleName); std::unique_ptr MIA( TheTarget->createMCInstrAnalysis(MII.get())); @@ -1114,8 +1120,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr IP(TheTarget->createMCInstPrinter( Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); if (!IP) - report_fatal_error("error: no instruction printer for target " + - TripleName); + report_error(Obj->getFileName(), "no instruction printer for target " + + TripleName); IP->setPrintImmHex(PrintImmHex); PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); @@ -1140,16 +1146,19 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::map AllSymbols; for (const SymbolRef &Symbol : Obj->symbols()) { Expected AddressOrErr = Symbol.getAddress(); - error(errorToErrorCode(AddressOrErr.takeError())); + if (!AddressOrErr) + report_error(Obj->getFileName(), AddressOrErr.takeError()); uint64_t Address = *AddressOrErr; Expected Name = Symbol.getName(); - error(errorToErrorCode(Name.takeError())); + if (!Name) + report_error(Obj->getFileName(), Name.takeError()); if (Name->empty()) continue; Expected SectionOrErr = Symbol.getSection(); - error(errorToErrorCode(SectionOrErr.takeError())); + if (!SectionOrErr) + report_error(Obj->getFileName(), SectionOrErr.takeError()); section_iterator SecI = *SectionOrErr; if (SecI == Obj->section_end()) continue; @@ -1664,17 +1673,21 @@ void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName, for (const SymbolRef &Symbol : o->symbols()) { Expected AddressOrError = Symbol.getAddress(); if (!AddressOrError) - report_error(ArchiveName, o->getFileName(), AddressOrError.takeError()); + report_error(ArchiveName, o->getFileName(), AddressOrError.takeError(), + ArchitectureName); uint64_t Address = *AddressOrError; if ((Address < StartAddress) || (Address > StopAddress)) continue; Expected TypeOrError = Symbol.getType(); if (!TypeOrError) - report_error(ArchiveName, o->getFileName(), TypeOrError.takeError()); + report_error(ArchiveName, o->getFileName(), TypeOrError.takeError(), + ArchitectureName); SymbolRef::Type Type = *TypeOrError; uint32_t Flags = Symbol.getFlags(); Expected SectionOrErr = Symbol.getSection(); - error(errorToErrorCode(SectionOrErr.takeError())); + if (!SectionOrErr) + report_error(ArchiveName, o->getFileName(), SectionOrErr.takeError(), + ArchitectureName); section_iterator Section = *SectionOrErr; StringRef Name; if (Type == SymbolRef::ST_Debug && Section != o->section_end()) { @@ -1903,7 +1916,7 @@ static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) { printMachOLoadCommands(o); return; } - report_fatal_error("Invalid/Unsupported object file format"); + report_error(o->getFileName(), "Invalid/Unsupported object file format"); } static void DumpObject(const ObjectFile *o, const Archive *a = nullptr) { diff --git a/tools/llvm-objdump/llvm-objdump.h b/tools/llvm-objdump/llvm-objdump.h index 85ad0bbe26c..c87ef2deb22 100644 --- a/tools/llvm-objdump/llvm-objdump.h +++ b/tools/llvm-objdump/llvm-objdump.h @@ -91,6 +91,7 @@ void PrintSectionContents(const object::ObjectFile *o); void PrintSymbolTable(const object::ObjectFile *o, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); LLVM_ATTRIBUTE_NORETURN void error(Twine Message); +LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,