diff options
author | Kevin Enderby <enderby@apple.com> | 2016-11-16 22:17:38 +0000 |
---|---|---|
committer | Kevin Enderby <enderby@apple.com> | 2016-11-16 22:17:38 +0000 |
commit | 7fa40c9f2b176fde3f99a96f6eb952f590f9d694 (patch) | |
tree | b1d5119d313da96bd74f11c7741247741ad89a6c /llvm/tools/llvm-objdump/llvm-objdump.cpp | |
parent | a789f400022f4941e00142d2299e2ca0df695c63 (diff) | |
download | bcm5719-llvm-7fa40c9f2b176fde3f99a96f6eb952f590f9d694.tar.gz bcm5719-llvm-7fa40c9f2b176fde3f99a96f6eb952f590f9d694.zip |
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.
llvm-svn: 287163
Diffstat (limited to 'llvm/tools/llvm-objdump/llvm-objdump.cpp')
-rw-r--r-- | llvm/tools/llvm-objdump/llvm-objdump.cpp | 93 |
1 files changed, 53 insertions, 40 deletions
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index be44c7450d6..cbb74a3abd2 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -293,6 +293,12 @@ LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) { } 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); errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; @@ -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<uint64_t> 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<StringRef> 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<StringRef> 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<const MCRegisterInfo> 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<const MCAsmInfo> 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<const MCSubtargetInfo> 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<const MCInstrInfo> 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<MCDisassembler> 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<const MCInstrAnalysis> MIA( TheTarget->createMCInstrAnalysis(MII.get())); @@ -1114,8 +1120,8 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr<MCInstPrinter> 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<SectionRef, SectionSymbolsTy> AllSymbols; for (const SymbolRef &Symbol : Obj->symbols()) { Expected<uint64_t> AddressOrErr = Symbol.getAddress(); - error(errorToErrorCode(AddressOrErr.takeError())); + if (!AddressOrErr) + report_error(Obj->getFileName(), AddressOrErr.takeError()); uint64_t Address = *AddressOrErr; Expected<StringRef> Name = Symbol.getName(); - error(errorToErrorCode(Name.takeError())); + if (!Name) + report_error(Obj->getFileName(), Name.takeError()); if (Name->empty()) continue; Expected<section_iterator> 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<uint64_t> 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<SymbolRef::Type> 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<section_iterator> 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) { |