From ddf7fe28fc876df29dec5db2c44003cf32254024 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Wed, 21 Aug 2019 11:07:31 +0000 Subject: [PATCH] [llvm-objdump] - Cleanup the error reporting. The error reporting function are not consistent. Before this change: * They had inconsistent naming (e.g. 'error' vs 'report_error'). * Some of them reported the object name, others - dont. * Some of them accepted the case when there was no error. (i.e. error code or Error had a success value). This patch tries to cleanup it a bit. It also renames report_error -> reportError, report_warning -> reportWarning and removes a full stop from messages. Differential revision: https://reviews.llvm.org/D66418 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369515 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Object/macho-invalid.test | 2 +- .../X86/disassemble-functions-mangling.test | 4 +- .../X86/source-interleave-invalid-source.test | 2 +- .../X86/source-interleave-missing-source.test | 2 +- .../X86/source-interleave-no-debug-info.test | 2 +- .../tools/llvm-objdump/X86/start-stop-address.test | 2 +- .../llvm-objdump/X86/warn-missing-disasm-func.test | 6 +- .../warn-on-out-of-range-start-stop-address.test | 8 +- tools/llvm-objdump/COFFDump.cpp | 61 +++++-- tools/llvm-objdump/ELFDump.cpp | 2 +- tools/llvm-objdump/MachODump.cpp | 89 +++++----- tools/llvm-objdump/llvm-objdump.cpp | 181 ++++++++++----------- tools/llvm-objdump/llvm-objdump.h | 15 +- 13 files changed, 196 insertions(+), 180 deletions(-) diff --git a/test/Object/macho-invalid.test b/test/Object/macho-invalid.test index e5bd1cceaac..da71ced2cb9 100644 --- a/test/Object/macho-invalid.test +++ b/test/Object/macho-invalid.test @@ -102,7 +102,7 @@ RUN: not llvm-objdump --macho --universal-headers %p/Inputs/macho-invalid-fat 2> INVALID-FAT: truncated or malformed fat file (fat_arch_64 structs would extend past the end of the file) RUN: not llvm-objdump --macho --private-headers -arch all %p/Inputs/macho-invalid-fat.obj.elf-x86_64 2>&1 | FileCheck -check-prefix INVALID-FAT-ELF %s -INVALID-FAT-ELF: Mach-O universal file: {{.*}}/macho-invalid-fat.obj.elf-x86_64 for architecture x86_64 is not a Mach-O file or an archive file +INVALID-FAT-ELF: error: '{{.*}}/macho-invalid-fat.obj.elf-x86_64': Mach-O universal file for architecture x86_64 is not a Mach-O file or an archive file RUN: not llvm-objdump --macho --private-headers %p/Inputs/macho-invalid-segment-fileoff 2>&1 | FileCheck -check-prefix INVALID-SEGMENT-FILEOFF %s INVALID-SEGMENT-FILEOFF: macho-invalid-segment-fileoff': truncated or malformed object (load command 0 fileoff field in LC_SEGMENT extends past the end of the file) diff --git a/test/tools/llvm-objdump/X86/disassemble-functions-mangling.test b/test/tools/llvm-objdump/X86/disassemble-functions-mangling.test index b4cdd0e6d01..23329c982df 100644 --- a/test/tools/llvm-objdump/X86/disassemble-functions-mangling.test +++ b/test/tools/llvm-objdump/X86/disassemble-functions-mangling.test @@ -20,10 +20,10 @@ # RUN: | FileCheck %s --check-prefix=DEMANGLED-MULTI # MANGLED: _Z3foov: -# MANGLED-MISS: warning: failed to disassemble missing function foo +# MANGLED-MISS: warning: '{{.*}}': failed to disassemble missing function foo # DEMANGLED: foo(): -# DEMANGLED-MISS: warning: failed to disassemble missing function _Z3foov +# DEMANGLED-MISS: warning: '{{.*}}': failed to disassemble missing function _Z3foov # NOMANGLE: i: # NOMANGLE: f: diff --git a/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test b/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test index 8cf86c39959..6337ba45802 100644 --- a/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test +++ b/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test @@ -13,6 +13,6 @@ # CHECK: main: # CHECK-NEXT: ; int main() { -# WARN: warning: debug info line number 9999 exceeds the number of lines in {{.*}}source-interleave-x86_64.c +# WARN: warning: '{{.*}}': debug info line number 9999 exceeds the number of lines in {{.*}}source-interleave-x86_64.c # GOOD: ; int *b = &a; # CHECK: ; return *b + foo(); diff --git a/test/tools/llvm-objdump/X86/source-interleave-missing-source.test b/test/tools/llvm-objdump/X86/source-interleave-missing-source.test index b519117f8c4..2aa096f4234 100644 --- a/test/tools/llvm-objdump/X86/source-interleave-missing-source.test +++ b/test/tools/llvm-objdump/X86/source-interleave-missing-source.test @@ -11,7 +11,7 @@ # RUN: llvm-objdump --source %t2.o | FileCheck %s --check-prefixes=CHECK,SOURCE # RUN: FileCheck %s --input-file %t.e --check-prefixes=WARN -# WARN: warning: failed to find source {{.*}}source-interleave-x86_64.c +# WARN: warning: '{{.*}}': failed to find source {{.*}}source-interleave-x86_64.c # CHECK: 0000000000000010 main: # SOURCE-NEXT: ; int main() { # CHECK-NEXT: 10: 55 pushq %rbp diff --git a/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test b/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test index e4de95daee1..65f8e17f8ae 100644 --- a/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test +++ b/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test @@ -9,7 +9,7 @@ # RUN: llvm-objdump --source %t2.o 2> %t2.e | FileCheck %s --check-prefixes=CHECK --implicit-check-not='main()' # RUN: FileCheck %s --input-file %t2.e --check-prefixes=WARN -# WARN: warning: failed to parse debug information for {{.*}}2.o +# WARN: warning: '{{.*}}2.o': failed to parse debug information # CHECK: 0000000000000010 main: # SOURCE-NEXT: ; int main() { # CHECK-NEXT: 10: 55 pushq %rbp diff --git a/test/tools/llvm-objdump/X86/start-stop-address.test b/test/tools/llvm-objdump/X86/start-stop-address.test index 3ce57fd04e5..e7b6e84beb7 100644 --- a/test/tools/llvm-objdump/X86/start-stop-address.test +++ b/test/tools/llvm-objdump/X86/start-stop-address.test @@ -69,4 +69,4 @@ // RUN: not llvm-objdump %t.out --start-address=0x40 --stop-address=0x3f 2>&1 | FileCheck %s --check-prefix ERRMSG // RUN: not llvm-objdump %t.out --start-address=0x40 --stop-address=0x40 2>&1 | FileCheck %s --check-prefix ERRMSG -// ERRMSG: start address should be less than stop address. +// ERRMSG: start address should be less than stop address diff --git a/test/tools/llvm-objdump/X86/warn-missing-disasm-func.test b/test/tools/llvm-objdump/X86/warn-missing-disasm-func.test index ec7d0302a71..2a96c00b3e4 100644 --- a/test/tools/llvm-objdump/X86/warn-missing-disasm-func.test +++ b/test/tools/llvm-objdump/X86/warn-missing-disasm-func.test @@ -13,9 +13,9 @@ FileHeader: Type: ET_REL Machine: EM_X86_64 -# CHECK: warning: failed to disassemble missing function foo +# CHECK: warning: {{.*}}: failed to disassemble missing function foo # MULTI: file format ELF64-x86-64 -# MULTI: warning: failed to disassemble missing function foo +# MULTI: warning: {{.*}}: failed to disassemble missing function foo # MULTI: file format ELF64-x86-64 -# MULTI: warning: failed to disassemble missing function foo +# MULTI: warning: {{.*}}: failed to disassemble missing function foo diff --git a/test/tools/llvm-objdump/warn-on-out-of-range-start-stop-address.test b/test/tools/llvm-objdump/warn-on-out-of-range-start-stop-address.test index 931b360d739..29912aee095 100644 --- a/test/tools/llvm-objdump/warn-on-out-of-range-start-stop-address.test +++ b/test/tools/llvm-objdump/warn-on-out-of-range-start-stop-address.test @@ -120,12 +120,12 @@ ## No warning if neither --start-address nor --stop-address are specified. # RUN: llvm-objdump --file-headers %t 2>&1 | FileCheck %s --implicit-check-not=warning: -# WARN: warning: no section overlaps the range {{.*}} specified by --start-address/--stop-address -# WARN-STOP-ONLY: warning: no section has address less than 0x1000 specified by --stop-address -# WARN-START-ONLY: warning: no section has address greater than or equal to 0x1500 specified by --start-address +# WARN: warning: {{.*}}: no section overlaps the range {{.*}} specified by --start-address/--stop-address +# WARN-STOP-ONLY: warning: {{.*}}: no section has address less than 0x1000 specified by --stop-address +# WARN-START-ONLY: warning: {{.*}}: no section has address greater than or equal to 0x1500 specified by --start-address # MULTI-INPUT: file format -# MULTI-INPUT: warning: no section overlaps the range [0x2001,0x2005) specified by --start-address/--stop-address +# MULTI-INPUT: warning: {{.*}}: no section overlaps the range [0x2001,0x2005) specified by --start-address/--stop-address # MULTI-INPUT: file format # MULTI-INPUT-NOT: warning: diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp index 584467bcf5b..60b0f5a3cbd 100644 --- a/tools/llvm-objdump/COFFDump.cpp +++ b/tools/llvm-objdump/COFFDump.cpp @@ -234,13 +234,14 @@ printSEHTable(const COFFObjectFile *Obj, uint32_t TableVA, int Count) { if (Count == 0) return; - uint32_t ImageBase = Obj->getPE32Header()->ImageBase; uintptr_t IntPtr = 0; - error(Obj->getVaPtr(TableVA, IntPtr)); + if (std::error_code EC = Obj->getVaPtr(TableVA, IntPtr)) + reportError(errorCodeToError(EC), Obj->getFileName()); + const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr; outs() << "SEH Table:"; for (int I = 0; I < Count; ++I) - outs() << format(" 0x%x", P[I] + ImageBase); + outs() << format(" 0x%x", P[I] + Obj->getPE32Header()->ImageBase); outs() << "\n\n"; } @@ -274,11 +275,16 @@ static void printTLSDirectory(const COFFObjectFile *Obj) { return; const data_directory *DataDir; - error(Obj->getDataDirectory(COFF::TLS_TABLE, DataDir)); - uintptr_t IntPtr = 0; + if (std::error_code EC = Obj->getDataDirectory(COFF::TLS_TABLE, DataDir)) + reportError(errorCodeToError(EC), Obj->getFileName()); + if (DataDir->RelativeVirtualAddress == 0) return; - error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)); + + uintptr_t IntPtr = 0; + if (std::error_code EC = + Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)) + reportError(errorCodeToError(EC), Obj->getFileName()); if (PE32Header) { auto *TLSDir = reinterpret_cast(IntPtr); @@ -301,11 +307,18 @@ static void printLoadConfiguration(const COFFObjectFile *Obj) { return; const data_directory *DataDir; - error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)); + + if (std::error_code EC = + Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)) + reportError(errorCodeToError(EC), Obj->getFileName()); + uintptr_t IntPtr = 0; if (DataDir->RelativeVirtualAddress == 0) return; - error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)); + + if (std::error_code EC = + Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)) + reportError(errorCodeToError(EC), Obj->getFileName()); auto *LoadConf = reinterpret_cast(IntPtr); outs() << "Load configuration:" @@ -447,7 +460,9 @@ static bool getPDataSection(const COFFObjectFile *Obj, llvm::sort(Rels, isRelocAddressLess); ArrayRef Contents; - error(Obj->getSectionContents(Pdata, Contents)); + if (Error E = Obj->getSectionContents(Pdata, Contents)) + reportError(std::move(E), Obj->getFileName()); + if (Contents.empty()) continue; @@ -563,10 +578,12 @@ static void printRuntimeFunctionRels(const COFFObjectFile *Obj, ArrayRef XContents; uint64_t UnwindInfoOffset = 0; - error(getSectionContents( - Obj, Rels, SectionOffset + - /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, - XContents, UnwindInfoOffset)); + if (Error E = getSectionContents( + Obj, Rels, + SectionOffset + + /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, + XContents, UnwindInfoOffset)) + reportError(std::move(E), Obj->getFileName()); if (XContents.empty()) return; @@ -642,9 +659,12 @@ void printCOFFSymbolTable(const object::COFFImportFile *i) { void printCOFFSymbolTable(const COFFObjectFile *coff) { for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) { Expected Symbol = coff->getSymbol(SI); + if (!Symbol) + reportError(Symbol.takeError(), coff->getFileName()); + StringRef Name; - error(Symbol.takeError()); - error(coff->getSymbolName(*Symbol, Name)); + if (std::error_code EC = coff->getSymbolName(*Symbol, Name)) + reportError(errorCodeToError(EC), coff->getFileName()); outs() << "[" << format("%2d", SI) << "]" << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")" @@ -674,7 +694,9 @@ void printCOFFSymbolTable(const COFFObjectFile *coff) { for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) { if (Symbol->isSectionDefinition()) { const coff_aux_section_definition *asd; - error(coff->getAuxSymbol(SI + 1, asd)); + if (std::error_code EC = + coff->getAuxSymbol(SI + 1, asd)) + reportError(errorCodeToError(EC), coff->getFileName()); int32_t AuxNumber = asd->getNumber(Symbol->isBigObj()); @@ -689,7 +711,8 @@ void printCOFFSymbolTable(const COFFObjectFile *coff) { , unsigned(asd->Selection)); } else if (Symbol->isFileRecord()) { const char *FileName; - error(coff->getAuxSymbol(SI + 1, FileName)); + if (std::error_code EC = coff->getAuxSymbol(SI + 1, FileName)) + reportError(errorCodeToError(EC), coff->getFileName()); StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() * coff->getSymbolTableEntrySize()); @@ -699,7 +722,9 @@ void printCOFFSymbolTable(const COFFObjectFile *coff) { break; } else if (Symbol->isWeakExternal()) { const coff_aux_weak_external *awe; - error(coff->getAuxSymbol(SI + 1, awe)); + if (std::error_code EC = + coff->getAuxSymbol(SI + 1, awe)) + reportError(errorCodeToError(EC), coff->getFileName()); outs() << "AUX " << format("indx %d srch %d\n", static_cast(awe->TagIndex), diff --git a/tools/llvm-objdump/ELFDump.cpp b/tools/llvm-objdump/ELFDump.cpp index 9c4d67d0f1b..93d070eee16 100644 --- a/tools/llvm-objdump/ELFDump.cpp +++ b/tools/llvm-objdump/ELFDump.cpp @@ -178,7 +178,7 @@ void printDynamicSection(const ELFFile *Elf, StringRef Filename) { outs() << (Data + Dyn.d_un.d_val) << "\n"; continue; } - warn(toString(StrTabOrErr.takeError())); + reportWarning(toString(StrTabOrErr.takeError()), Filename); consumeError(StrTabOrErr.takeError()); } outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val); diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index e63bbfcd562..e6845400a92 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -236,11 +236,11 @@ struct SymbolSorter { bool operator()(const SymbolRef &A, const SymbolRef &B) { Expected ATypeOrErr = A.getType(); if (!ATypeOrErr) - report_error(ATypeOrErr.takeError(), A.getObject()->getFileName()); + reportError(ATypeOrErr.takeError(), A.getObject()->getFileName()); SymbolRef::Type AType = *ATypeOrErr; Expected BTypeOrErr = B.getType(); if (!BTypeOrErr) - report_error(BTypeOrErr.takeError(), B.getObject()->getFileName()); + reportError(BTypeOrErr.takeError(), B.getObject()->getFileName()); SymbolRef::Type BType = *BTypeOrErr; uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue(); uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue(); @@ -533,8 +533,8 @@ Error getMachORelocationValueString(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_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after " - "X86_64_RELOC_SUBTRACTOR."); + reportError(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. @@ -582,8 +582,8 @@ Error getMachORelocationValueString(const MachOObjectFile *Obj, unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::GENERIC_RELOC_PAIR) - report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " - "GENERIC_RELOC_SECTDIFF."); + reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " + "GENERIC_RELOC_SECTDIFF."); printRelocationTargetName(Obj, RE, Fmt); Fmt << "-"; @@ -603,8 +603,8 @@ Error getMachORelocationValueString(const MachOObjectFile *Obj, // GENERIC_RELOC_PAIR. unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::GENERIC_RELOC_PAIR) - report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " - "GENERIC_RELOC_LOCAL_SECTDIFF."); + reportError(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " + "GENERIC_RELOC_LOCAL_SECTDIFF."); printRelocationTargetName(Obj, RE, Fmt); Fmt << "-"; @@ -643,8 +643,8 @@ Error getMachORelocationValueString(const MachOObjectFile *Obj, // ARM_RELOC_PAIR. unsigned RType = Obj->getAnyRelocationType(RENext); if (RType != MachO::ARM_RELOC_PAIR) - report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after " - "ARM_RELOC_HALF"); + reportError(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 @@ -1945,7 +1945,7 @@ static void ProcessMachO(StringRef Name, MachOObjectFile *MachOOF, // the error message. if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo) if (Error Err = MachOOF->checkSymbolTable()) - report_error(std::move(Err), ArchiveName, FileName, ArchitectureName); + reportError(std::move(Err), ArchiveName, FileName, ArchitectureName); if (DisassembleAll) { for (const SectionRef &Section : MachOOF->sections()) { @@ -2295,7 +2295,7 @@ static void printArchiveHeaders(StringRef Filename, Archive *A, bool verbose, ArchitectureName); if (Err) - report_error(std::move(Err), StringRef(), Filename, ArchitectureName); + reportError(std::move(Err), StringRef(), Filename, ArchitectureName); } static bool ValidateArchFlags() { @@ -2327,7 +2327,7 @@ void parseInputMachO(StringRef Filename) { Expected> BinaryOrErr = createBinary(Filename); if (!BinaryOrErr) { if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError())) - report_error(std::move(E), Filename); + reportError(std::move(E), Filename); else outs() << Filename << ": is not an object file\n"; return; @@ -2346,7 +2346,7 @@ void parseInputMachO(StringRef Filename) { Expected> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - report_error(std::move(E), Filename, getFileNameForError(C, I)); + reportError(std::move(E), Filename, getFileNameForError(C, I)); continue; } if (MachOObjectFile *O = dyn_cast(&*ChildOrErr.get())) { @@ -2356,7 +2356,7 @@ void parseInputMachO(StringRef Filename) { } } if (Err) - report_error(std::move(Err), Filename); + reportError(std::move(Err), Filename); return; } if (MachOUniversalBinary *UB = dyn_cast(&Bin)) { @@ -2408,7 +2408,7 @@ void parseInputMachO(MachOUniversalBinary *UB) { ProcessMachO(Filename, MachOOF, "", ArchitectureName); } else if (Error E = isNotObjectErrorInvalidFileType( ObjOrErr.takeError())) { - report_error(std::move(E), Filename, StringRef(), ArchitectureName); + reportError(std::move(E), Filename, StringRef(), ArchitectureName); continue; } else if (Expected> AOrErr = I->getAsArchive()) { @@ -2428,8 +2428,8 @@ void parseInputMachO(MachOUniversalBinary *UB) { if (!ChildOrErr) { if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - report_error(std::move(E), Filename, getFileNameForError(C, I), - ArchitectureName); + reportError(std::move(E), Filename, getFileNameForError(C, I), + ArchitectureName); continue; } if (MachOObjectFile *O = @@ -2437,12 +2437,13 @@ void parseInputMachO(MachOUniversalBinary *UB) { ProcessMachO(Filename, O, O->getFileName(), ArchitectureName); } if (Err) - report_error(std::move(Err), Filename); + reportError(std::move(Err), Filename); } else { consumeError(AOrErr.takeError()); - error("Mach-O universal file: " + Filename + " for " + - "architecture " + StringRef(I->getArchFlagName()) + - " is not a Mach-O file or an archive file"); + reportError(Filename, + "Mach-O universal file for architecture " + + StringRef(I->getArchFlagName()) + + " is not a Mach-O file or an archive file"); } } } @@ -2472,7 +2473,7 @@ void parseInputMachO(MachOUniversalBinary *UB) { ProcessMachO(Filename, MachOOF); } else if (Error E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { - report_error(std::move(E), Filename); + reportError(std::move(E), Filename); } else if (Expected> AOrErr = I->getAsArchive()) { std::unique_ptr &A = *AOrErr; @@ -2488,7 +2489,7 @@ void parseInputMachO(MachOUniversalBinary *UB) { if (!ChildOrErr) { if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - report_error(std::move(E), Filename, getFileNameForError(C, I)); + reportError(std::move(E), Filename, getFileNameForError(C, I)); continue; } if (MachOObjectFile *O = @@ -2496,12 +2497,12 @@ void parseInputMachO(MachOUniversalBinary *UB) { ProcessMachO(Filename, O, O->getFileName()); } if (Err) - report_error(std::move(Err), Filename); + reportError(std::move(Err), Filename); } else { consumeError(AOrErr.takeError()); - error("Mach-O universal file: " + Filename + " for architecture " + - StringRef(I->getArchFlagName()) + - " is not a Mach-O file or an archive file"); + reportError(Filename, "Mach-O universal file for architecture " + + StringRef(I->getArchFlagName()) + + " is not a Mach-O file or an archive file"); } return; } @@ -2523,7 +2524,7 @@ void parseInputMachO(MachOUniversalBinary *UB) { ProcessMachO(Filename, MachOOF, "", ArchitectureName); } else if (Error E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { - report_error(std::move(E), StringRef(), Filename, ArchitectureName); + reportError(std::move(E), StringRef(), Filename, ArchitectureName); } else if (Expected> AOrErr = I->getAsArchive()) { std::unique_ptr &A = *AOrErr; outs() << "Archive : " << Filename; @@ -2540,8 +2541,8 @@ void parseInputMachO(MachOUniversalBinary *UB) { Expected> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - report_error(std::move(E), Filename, getFileNameForError(C, I), - ArchitectureName); + reportError(std::move(E), Filename, getFileNameForError(C, I), + ArchitectureName); continue; } if (MachOObjectFile *O = @@ -2552,12 +2553,12 @@ void parseInputMachO(MachOUniversalBinary *UB) { } } if (Err) - report_error(std::move(Err), Filename); + reportError(std::move(Err), Filename); } else { consumeError(AOrErr.takeError()); - error("Mach-O universal file: " + Filename + " for architecture " + - StringRef(I->getArchFlagName()) + - " is not a Mach-O file or an archive file"); + reportError(Filename, "Mach-O universal file for architecture " + + StringRef(I->getArchFlagName()) + + " is not a Mach-O file or an archive file"); } } } @@ -7344,7 +7345,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, ErrorOr> BufOrErr = MemoryBuffer::getFileOrSTDIN(DSYMFile); if (std::error_code EC = BufOrErr.getError()) { - report_error(errorCodeToError(EC), DSYMFile); + reportError(errorCodeToError(EC), DSYMFile); return; } @@ -7354,7 +7355,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, Expected> BinaryOrErr = createBinary(DSYMBuf.get()->getMemBufferRef()); if (!BinaryOrErr) { - report_error(BinaryOrErr.takeError(), DSYMFile); + reportError(BinaryOrErr.takeError(), DSYMFile); return; } @@ -7390,7 +7391,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, Expected> MachDSYM = UB->getObjectForArch(ArchFlag); if (!MachDSYM) { - report_error(MachDSYM.takeError(), DSYMFile); + reportError(MachDSYM.takeError(), DSYMFile); return; } @@ -10294,7 +10295,7 @@ void printMachOExportsTrie(const object::MachOObjectFile *Obj) { outs() << "\n"; } if (Err) - report_error(std::move(Err), Obj->getFileName()); + reportError(std::move(Err), Obj->getFileName()); } //===----------------------------------------------------------------------===// @@ -10315,7 +10316,7 @@ void printMachORebaseTable(object::MachOObjectFile *Obj) { Address, Entry.typeName().str().c_str()); } if (Err) - report_error(std::move(Err), Obj->getFileName()); + reportError(std::move(Err), Obj->getFileName()); } static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) { @@ -10367,7 +10368,7 @@ void printMachOBindTable(object::MachOObjectFile *Obj) { << Entry.symbolName() << Attr << "\n"; } if (Err) - report_error(std::move(Err), Obj->getFileName()); + reportError(std::move(Err), Obj->getFileName()); } //===----------------------------------------------------------------------===// @@ -10392,7 +10393,7 @@ void printMachOLazyBindTable(object::MachOObjectFile *Obj) { << Entry.symbolName() << "\n"; } if (Err) - report_error(std::move(Err), Obj->getFileName()); + reportError(std::move(Err), Obj->getFileName()); } //===----------------------------------------------------------------------===// @@ -10424,7 +10425,7 @@ void printMachOWeakBindTable(object::MachOObjectFile *Obj) { << "\n"; } if (Err) - report_error(std::move(Err), Obj->getFileName()); + reportError(std::move(Err), Obj->getFileName()); } // get_dyld_bind_info_symbolname() is used for disassembly and passed an @@ -10443,7 +10444,7 @@ static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue, (*info->bindtable)[Address] = name; } if (Err) - report_error(std::move(Err), info->O->getFileName()); + reportError(std::move(Err), info->O->getFileName()); } auto name = info->bindtable->lookup(ReferenceValue); return !name.empty() ? name.data() : nullptr; diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index c32cd985cc4..0b699673972 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -375,43 +375,21 @@ std::string getFileNameForError(const object::Archive::Child &C, return ""; } -void error(std::error_code EC) { - if (!EC) - return; - WithColor::error(errs(), ToolName) - << "reading file: " << EC.message() << ".\n"; - errs().flush(); - exit(1); -} - -void error(Error E) { - if (!E) - return; - WithColor::error(errs(), ToolName) << toString(std::move(E)); - exit(1); -} - -LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { - WithColor::error(errs(), ToolName) << Message << ".\n"; - errs().flush(); - exit(1); -} - -void warn(Twine Message) { +void reportWarning(Twine Message, StringRef File) { // Output order between errs() and outs() matters especially for archive // files where the output is per member object. outs().flush(); - WithColor::warning(errs(), ToolName) << Message << "\n"; + WithColor::warning(errs(), ToolName) + << "'" << File << "': " << Message << "\n"; errs().flush(); } -LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message) { - WithColor::error(errs(), ToolName) - << "'" << File << "': " << Message << ".\n"; +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message) { + WithColor::error(errs(), ToolName) << "'" << File << "': " << Message << "\n"; exit(1); } -LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File) { +LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef File) { assert(E); std::string Buf; raw_string_ostream OS(Buf); @@ -421,9 +399,9 @@ LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File) { exit(1); } -LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef ArchiveName, - StringRef FileName, - StringRef ArchitectureName) { +LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef ArchiveName, + StringRef FileName, + StringRef ArchitectureName) { assert(E); WithColor::error(errs(), ToolName); if (ArchiveName != "") @@ -440,6 +418,15 @@ LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef ArchiveName, exit(1); } +static void reportCmdLineWarning(Twine Message) { + WithColor::warning(errs(), ToolName) << Message << "\n"; +} + +LLVM_ATTRIBUTE_NORETURN static void reportCmdLineError(Twine Message) { + WithColor::error(errs(), ToolName) << Message << "\n"; + exit(1); +} + static void warnOnNoMatchForSections() { SetVector MissingSections; for (StringRef S : FilterSections) { @@ -452,37 +439,29 @@ static void warnOnNoMatchForSections() { // Warn only if no section in FilterSections is matched. for (StringRef S : MissingSections) - warn("section '" + S + "' mentioned in a -j/--section option, but not " - "found in any input file"); + reportCmdLineWarning("section '" + S + + "' mentioned in a -j/--section option, but not " + "found in any input file"); } -static const Target *getTarget(const ObjectFile *Obj = nullptr) { +static const Target *getTarget(const ObjectFile *Obj) { // Figure out the target triple. Triple TheTriple("unknown-unknown-unknown"); if (TripleName.empty()) { - if (Obj) - TheTriple = Obj->makeTriple(); + TheTriple = Obj->makeTriple(); } else { TheTriple.setTriple(Triple::normalize(TripleName)); - - // Use the triple, but also try to combine with ARM build attributes. - if (Obj) { - auto Arch = Obj->getArch(); - if (Arch == Triple::arm || Arch == Triple::armeb) - Obj->setARMSubArch(TheTriple); - } + auto Arch = Obj->getArch(); + if (Arch == Triple::arm || Arch == Triple::armeb) + Obj->setARMSubArch(TheTriple); } // Get the target specific parser. std::string Error; const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, Error); - if (!TheTarget) { - if (Obj) - report_error(Obj->getFileName(), "can't find target: " + Error); - else - error("can't find target: " + Error); - } + if (!TheTarget) + reportError(Obj->getFileName(), "can't find target: " + Error); // Update the triple name and return the found target. TripleName = TheTriple.getTriple(); @@ -582,8 +561,8 @@ bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) { auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName); if (!BufferOrError) { if (MissingSources.insert(LineInfo.FileName).second) - warn("failed to find source " + LineInfo.FileName); - + reportWarning("failed to find source " + LineInfo.FileName, + Obj->getFileName()); return false; } Buffer = std::move(*BufferOrError); @@ -625,7 +604,7 @@ void SourcePrinter::printSourceLine(raw_ostream &OS, "failed to parse debug information for " + ObjectFilename.str(); if (!ErrorMessage.empty()) Warning += ": " + ErrorMessage; - warn(Warning); + reportWarning(Warning, ObjectFilename); WarnedNoDebugInfo = true; } return; @@ -644,9 +623,11 @@ void SourcePrinter::printSourceLine(raw_ostream &OS, auto LineBuffer = LineCache.find(LineInfo.FileName); if (LineBuffer != LineCache.end()) { if (LineInfo.Line > LineBuffer->second.size()) { - warn(formatv( - "debug info line number {0} exceeds the number of lines in {1}", - LineInfo.Line, LineInfo.FileName)); + reportWarning( + formatv( + "debug info line number {0} exceeds the number of lines in {1}", + LineInfo.Line, LineInfo.FileName), + ObjectFilename); return; } // Vector begins at 0, line numbers are non-zero @@ -670,13 +651,14 @@ static bool hasMappingSymbols(const ObjectFile *Obj) { return isArmElf(Obj) || isAArch64Elf(Obj); } -static void printRelocation(const RelocationRef &Rel, uint64_t Address, - bool Is64Bits) { +static void printRelocation(StringRef FileName, const RelocationRef &Rel, + uint64_t Address, bool Is64Bits) { StringRef Fmt = Is64Bits ? "\t\t%016" PRIx64 ": " : "\t\t\t%08" PRIx64 ": "; SmallString<16> Name; SmallString<32> Val; Rel.getTypeName(Name); - error(getRelocationValueString(Rel, Val)); + if (Error E = getRelocationValueString(Rel, Val)) + reportError(std::move(E), FileName); outs() << format(Fmt.data(), Address) << Name << "\t" << Val << "\n"; } @@ -760,7 +742,7 @@ public: auto PrintReloc = [&]() -> void { while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address.Address)) { if (RelCur->getOffset() == Address.Address) { - printRelocation(*RelCur, Address.Address, false); + printRelocation(ObjectFilename, *RelCur, Address.Address, false); return; } ++RelCur; @@ -1162,11 +1144,14 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, if (const auto *COFFObj = dyn_cast(Obj)) { for (const auto &ExportEntry : COFFObj->export_directories()) { StringRef Name; - error(ExportEntry.getSymbolName(Name)); + if (std::error_code EC = ExportEntry.getSymbolName(Name)) + reportError(errorCodeToError(EC), Obj->getFileName()); if (Name.empty()) continue; + uint32_t RVA; - error(ExportEntry.getExportRVA(RVA)); + if (std::error_code EC = ExportEntry.getExportRVA(RVA)) + reportError(errorCodeToError(EC), Obj->getFileName()); uint64_t VA = COFFObj->getImageBase() + RVA; auto Sec = partition_point( @@ -1494,7 +1479,8 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, Offset += AdjustVMA; } - printRelocation(*RelCur, SectionAddr + Offset, Is64Bits); + printRelocation(Obj->getFileName(), *RelCur, SectionAddr + Offset, + Is64Bits); ++RelCur; } } @@ -1506,7 +1492,8 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, StringSet<> MissingDisasmFuncsSet = set_difference(DisasmFuncsSet, FoundDisasmFuncsSet); for (StringRef MissingDisasmFunc : MissingDisasmFuncsSet.keys()) - warn("failed to disassemble missing function " + MissingDisasmFunc); + reportWarning("failed to disassemble missing function " + MissingDisasmFunc, + FileName); } static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { @@ -1521,24 +1508,24 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr MRI( TheTarget->createMCRegInfo(TripleName)); if (!MRI) - report_error(Obj->getFileName(), - "no register info for target " + TripleName); + reportError(Obj->getFileName(), + "no register info for target " + TripleName); // Set up disassembler. std::unique_ptr AsmInfo( TheTarget->createMCAsmInfo(*MRI, TripleName)); if (!AsmInfo) - report_error(Obj->getFileName(), - "no assembly info for target " + TripleName); + reportError(Obj->getFileName(), + "no assembly info for target " + TripleName); std::unique_ptr STI( TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); if (!STI) - report_error(Obj->getFileName(), - "no subtarget info for target " + TripleName); + reportError(Obj->getFileName(), + "no subtarget info for target " + TripleName); std::unique_ptr MII(TheTarget->createMCInstrInfo()); if (!MII) - report_error(Obj->getFileName(), - "no instruction info for target " + TripleName); + reportError(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 @@ -1547,8 +1534,7 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr DisAsm( TheTarget->createMCDisassembler(*STI, Ctx)); if (!DisAsm) - report_error(Obj->getFileName(), - "no disassembler for target " + TripleName); + reportError(Obj->getFileName(), "no disassembler for target " + TripleName); // If we have an ARM object file, we need a second disassembler, because // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode. @@ -1573,8 +1559,8 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { std::unique_ptr IP(TheTarget->createMCInstPrinter( Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); if (!IP) - report_error(Obj->getFileName(), - "no instruction printer for target " + TripleName); + reportError(Obj->getFileName(), + "no instruction printer for target " + TripleName); IP->setPrintImmHex(PrintImmHex); PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); @@ -1582,7 +1568,8 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { for (StringRef Opt : DisassemblerOptions) if (!IP->applyTargetSpecificCLOption(Opt)) - error("Unrecognized disassembler option: " + Opt); + reportError(Obj->getFileName(), + "Unrecognized disassembler option: " + Opt); disassembleObject(TheTarget, Obj, Ctx, DisAsm.get(), SecondaryDisAsm.get(), MIA.get(), IP.get(), STI.get(), SecondarySTI.get(), PIP, @@ -1620,7 +1607,9 @@ void printRelocations(const ObjectFile *Obj) { if (Address < StartAddress || Address > StopAddress || getHidden(Reloc)) continue; Reloc.getTypeName(RelocName); - error(getRelocationValueString(Reloc, ValueStr)); + if (Error E = getRelocationValueString(Reloc, ValueStr)) + reportError(std::move(E), Obj->getFileName()); + outs() << format(Fmt.data(), Address) << " " << RelocName << " " << ValueStr << "\n"; } @@ -1636,7 +1625,7 @@ void printDynamicRelocations(const ObjectFile *Obj) { const auto *Elf = dyn_cast(Obj); if (!Elf || Elf->getEType() != ELF::ET_DYN) { - error("not a dynamic object"); + reportError(Obj->getFileName(), "not a dynamic object"); return; } @@ -1652,7 +1641,8 @@ void printDynamicRelocations(const ObjectFile *Obj) { SmallString<32> RelocName; SmallString<32> ValueStr; Reloc.getTypeName(RelocName); - error(getRelocationValueString(Reloc, ValueStr)); + if (Error E = getRelocationValueString(Reloc, ValueStr)) + reportError(std::move(E), Obj->getFileName()); outs() << format(Fmt.data(), Address) << " " << RelocName << " " << ValueStr << "\n"; } @@ -1980,12 +1970,12 @@ static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) { printMachOLoadCommands(O); return; } - report_error(O->getFileName(), "Invalid/Unsupported object file format"); + reportError(O->getFileName(), "Invalid/Unsupported object file format"); } static void printFileHeaders(const ObjectFile *O) { if (!O->isELF() && !O->isCOFF()) - report_error(O->getFileName(), "Invalid/Unsupported object file format"); + reportError(O->getFileName(), "Invalid/Unsupported object file format"); Triple::ArchType AT = O->getArch(); outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n"; @@ -2067,15 +2057,18 @@ static void checkForInvalidStartStopAddress(ObjectFile *Obj, } if (StartAddress.getNumOccurrences() == 0) - warn("no section has address less than 0x" + - Twine::utohexstr(Stop) + " specified by --stop-address"); + reportWarning("no section has address less than 0x" + + Twine::utohexstr(Stop) + " specified by --stop-address", + Obj->getFileName()); else if (StopAddress.getNumOccurrences() == 0) - warn("no section has address greater than or equal to 0x" + - Twine::utohexstr(Start) + " specified by --start-address"); + reportWarning("no section has address greater than or equal to 0x" + + Twine::utohexstr(Start) + " specified by --start-address", + Obj->getFileName()); else - warn("no section overlaps the range [0x" + - Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) + - ") specified by --start-address/--stop-address"); + reportWarning("no section overlaps the range [0x" + + Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) + + ") specified by --start-address/--stop-address", + Obj->getFileName()); } static void dumpObject(ObjectFile *O, const Archive *A = nullptr, @@ -2163,7 +2156,7 @@ static void dumpArchive(const Archive *A) { Expected> ChildOrErr = C.getAsBinary(); if (!ChildOrErr) { if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) - report_error(std::move(E), A->getFileName(), getFileNameForError(C, I)); + reportError(std::move(E), A->getFileName(), getFileNameForError(C, I)); continue; } if (ObjectFile *O = dyn_cast(&*ChildOrErr.get())) @@ -2171,11 +2164,11 @@ static void dumpArchive(const Archive *A) { else if (COFFImportFile *I = dyn_cast(&*ChildOrErr.get())) dumpObject(I, A, &C); else - report_error(errorCodeToError(object_error::invalid_file_type), - A->getFileName()); + reportError(errorCodeToError(object_error::invalid_file_type), + A->getFileName()); } if (Err) - report_error(std::move(Err), A->getFileName()); + reportError(std::move(Err), A->getFileName()); } /// Open file and figure out how to dump it. @@ -2199,7 +2192,7 @@ static void dumpInput(StringRef file) { else if (MachOUniversalBinary *UB = dyn_cast(&Binary)) parseInputMachO(UB); else - report_error(errorCodeToError(object_error::invalid_file_type), file); + reportError(errorCodeToError(object_error::invalid_file_type), file); } } // namespace llvm @@ -2220,7 +2213,7 @@ int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); if (StartAddress >= StopAddress) - error("start address should be less than stop address"); + reportCmdLineError("start address should be less than stop address"); ToolName = argv[0]; diff --git a/tools/llvm-objdump/llvm-objdump.h b/tools/llvm-objdump/llvm-objdump.h index 4d512d296e5..771e94a23f4 100644 --- a/tools/llvm-objdump/llvm-objdump.h +++ b/tools/llvm-objdump/llvm-objdump.h @@ -96,8 +96,6 @@ Error getMachORelocationValueString(const object::MachOObjectFile *Obj, uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec); -void error(std::error_code ec); -void error(Error E); bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B); void parseInputMachO(StringRef Filename); void parseInputMachO(object::MachOUniversalBinary *UB); @@ -129,19 +127,18 @@ void printSectionHeaders(const object::ObjectFile *O); void printSectionContents(const object::ObjectFile *O); void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); -void warn(Twine Message); -LLVM_ATTRIBUTE_NORETURN void error(Twine Message); -LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message); -LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File); +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message); +LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef File); LLVM_ATTRIBUTE_NORETURN void -report_error(Error E, StringRef FileName, StringRef ArchiveName, - StringRef ArchitectureName = StringRef()); +reportError(Error E, StringRef FileName, StringRef ArchiveName, + StringRef ArchitectureName = StringRef()); +void reportWarning(Twine Message, StringRef File); template T unwrapOrError(Expected EO, Ts &&... Args) { if (EO) return std::move(*EO); - report_error(EO.takeError(), std::forward(Args)...); + reportError(EO.takeError(), std::forward(Args)...); } std::string getFileNameForError(const object::Archive::Child &C, -- 2.11.4.GIT