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
This commit is contained in:
Kevin Enderby 2016-11-16 22:17:38 +00:00
parent d2c33c4ecb
commit fbd189d3df
3 changed files with 57 additions and 40 deletions

View File

@ -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)

View File

@ -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<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,7 +872,7 @@ 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 "
report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
"X86_64_RELOC_SUBTRACTOR.");
// The X86_64_RELOC_UNSIGNED contains the minuend symbol;
@ -920,7 +921,7 @@ 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 "
report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
"GENERIC_RELOC_SECTDIFF.");
printRelocationTargetName(Obj, RE, fmt);
@ -941,7 +942,7 @@ 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 "
report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
"GENERIC_RELOC_LOCAL_SECTDIFF.");
printRelocationTargetName(Obj, RE, fmt);
@ -981,7 +982,7 @@ 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 "
report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
"ARM_RELOC_HALF");
// NOTE: The half of the target virtual address is stashed in the
@ -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,7 +1120,7 @@ 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 " +
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) {

View File

@ -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,