]> granicus.if.org Git - llvm/commitdiff
[llvm-readobj] Reapply: Improve error message for --string-dump
authorStephen Tozer <stephen.tozer@sony.com>
Mon, 15 Apr 2019 11:17:48 +0000 (11:17 +0000)
committerStephen Tozer <stephen.tozer@sony.com>
Mon, 15 Apr 2019 11:17:48 +0000 (11:17 +0000)
This is a resubmission of a previous patch that caused test failures,
with the fixes for the relevant tests included.

Fixes bug 40630: https://bugs.llvm.org/show_bug.cgi?id=40630

This patch changes the error message when the section specified by
--string-dump cannot be found by including the name of the section in
the error message and changing the prefix text to not imply that the
file itself was invalid. As part of this change some uses of
std::error_code have been replaced with the llvm Error class to better
encapsulate the error info (rather than passing File strings around),
and the WithColor class replaces string literal error prefixes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358395 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
test/Object/corrupt.test
test/tools/llvm-readobj/basic.test
test/tools/llvm-readobj/codeview-merging-cycle.test
test/tools/llvm-readobj/elf-dynamic-malformed.test
test/tools/llvm-readobj/elf-malformed-pt-dynamic.test
test/tools/llvm-readobj/elf-packed-relocs-error1.s
test/tools/llvm-readobj/elf-packed-relocs-error2.s
test/tools/llvm-readobj/elf-packed-relocs-error3.s
test/tools/llvm-readobj/elf-packed-relocs-error4.s
test/tools/llvm-readobj/elf-packed-relocs-error5.s
test/tools/llvm-readobj/mips-got.test
test/tools/llvm-readobj/string-dump.test
test/tools/llvm-readobj/thin-archive-paths.test
test/tools/llvm-readobj/thin-archive.test
test/tools/llvm-readobj/wasm-invalid.test
tools/llvm-readobj/ObjDumper.cpp
tools/llvm-readobj/llvm-readobj.cpp

index 9cbe9ef5e0896e1da6c9b96fee12b935ea0cdc45..82a581f486f9b09a15e4e351a3fcc1a440c75f0e 100644 (file)
@@ -23,7 +23,7 @@ SYMNAME: invalid string offset
 RUN: not llvm-readobj %p/Inputs/corrupt-version.elf-x86_64 -dt \
 RUN:     2>&1 | FileCheck --check-prefix=VER %s
 
-VER: Error reading file: Invalid version entry.
+VER: error: Invalid version entry
 
 
 // The file is missing the dynamic string table but has references to it.
@@ -62,11 +62,11 @@ RUN: not llvm-readobj -dyn-relocations \
 RUN:   %p/Inputs/corrupt-invalid-dynamic-table-offset.elf.x86-64 2>&1 | \
 RUN:   FileCheck --check-prefix=DYN-TABLE-OFFSET %s
 
-DYN-TABLE-OFFSET: Invalid data was encountered while parsing the file.
+DYN-TABLE-OFFSET: error: Invalid data was encountered while parsing the file
 
 
 RUN: not llvm-readobj -dyn-relocations \
 RUN:   %p/Inputs/corrupt-invalid-dynamic-table-too-large.elf.x86-64 2>&1 | \
 RUN:   FileCheck --check-prefix=DYN-TABLE-TOO-LARGE %s
 
-DYN-TABLE-TOO-LARGE: Invalid data was encountered while parsing the file.
+DYN-TABLE-TOO-LARGE: error: Invalid data was encountered while parsing the file
index ffe2374edf3ad2c18d276b6e2b720b45bb623318..a08cc10aca7d10e136bda02ef582dd091f0f8914 100644 (file)
@@ -1,11 +1,11 @@
 # Test case where input file does not exit.
 RUN: not llvm-readobj %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s
-ENOENT: {{.*}}.blah: {{[Nn]}}o such file or directory
+ENOENT: '{{.*}}.blah': {{[Nn]}}o such file or directory
 
 # Test case where input file is too small to be a recognised object file.
 RUN: touch %t.empty
 RUN: not llvm-readobj %t.empty 2>&1 | FileCheck --check-prefix=EMPTY %s
-EMPTY: {{.*}}.empty: The file was not recognized as a valid object file
+EMPTY: '{{.*}}.empty': The file was not recognized as a valid object file
 
 # Test that unrecognised files in archives are ignored.
 RUN: rm -f %t.a
index f597ad6f52f0740b851cb7141d0941bb2ddaea70..4d612e8314d4d925b35387b769f551564ca4f7d6 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: not llvm-readobj -codeview-merged-types %S/Inputs/codeview-cycle.obj 2>&1 | FileCheck %s
 
-; CHECK: Error{{.*}} Input type graph contains cycles
+; CHECK: error{{.*}} Input type graph contains cycles
 
 ; To reproduce codeview-cycle.obj:
 ; $ cat codeview-cycle.asm
index 79af769aecdc695cbb776f42a4a16b4593d8b536..4e04423d7b18cd90b59028e28eacb74903ce6bf7 100644 (file)
@@ -3,7 +3,7 @@
 # RUN: not llvm-readobj --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
 # RUN: not llvm-readelf --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
 
-# ERR-SIZE: Error reading file: Invalid entity size.
+# ERR-SIZE: error: Invalid entity size
 
 --- !ELF
 FileHeader:
@@ -69,7 +69,7 @@ ProgramHeaders:
 # RUN: not llvm-readobj --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING
 # RUN: not llvm-readelf --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING
 
-# BAD-STRING: Error reading file: Invalid dynamic string table reference.
+# BAD-STRING: Invalid dynamic string table reference
 
 --- !ELF
 FileHeader:
index f061e1c34038a8285f89d9d01a6fb2aa1d7c0a08..e762a27865fb731ed7a29629d32eb4be00322a28 100644 (file)
@@ -21,7 +21,7 @@
 # RUN: %python -c "with open(r'%t.truncated2', 'r+') as f: f.truncate(0xFFF)"
 # RUN: not llvm-readobj %t.truncated2 --dynamic-table 2>&1 | FileCheck %s
 
-# CHECK: Error reading file: Invalid data was encountered while parsing the file.
+# CHECK: error: Invalid data was encountered while parsing the file
 
 --- !ELF
 FileHeader:
index 87dc6890a6c6848b8af0ba57322dd74727e7781e..e418353465b4f78c2cdea306080d286c848b0572 100644 (file)
@@ -1,7 +1,7 @@
 // REQUIRES: x86-registered-target
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
 
-// CHECK: Error reading file: invalid packed relocation header
+// CHECK: error: invalid packed relocation header
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS9"
index cbba5b73b604b2e7528b569ca70993d57fee2be4..28c325fec0854af9336a7c3d394ac1406f24aea7 100644 (file)
@@ -1,7 +1,7 @@
 // REQUIRES: x86-registered-target
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
 
-// CHECK: Error reading file: malformed sleb128, extends past end
+// CHECK: error: malformed sleb128, extends past end
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 7b2aa7dac5d1027b30cf834a9cc6e0ed90b9322b..e373a53f7305a9cd80a617251da21fa33955960e 100644 (file)
@@ -1,7 +1,7 @@
 // REQUIRES: x86-registered-target
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
 
-// CHECK: Error reading file: malformed sleb128, extends past end
+// CHECK: error: malformed sleb128, extends past end
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index b8992e6b6e72580cba7eb62d263f8a679764baeb..94ef6f4ca563fa303f0deb061d2dcd477a02e3ca 100644 (file)
@@ -1,7 +1,7 @@
 // REQUIRES: x86-registered-target
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
 
-// CHECK: Error reading file: malformed sleb128, extends past end
+// CHECK: error: malformed sleb128, extends past end
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 98a151e947b2e224cd73df75f76fb483cda89041..b0c549fe06fbfe03e7a9bac3fe487105a00f2dac 100644 (file)
@@ -1,7 +1,7 @@
 // REQUIRES: x86-registered-target
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
 
-// CHECK: Error reading file: relocation group unexpectedly large
+// CHECK: error: relocation group unexpectedly large
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 7d8d2669bd27488716b7f70e96d54fc0515f9a34..ca7edc2fbf1748bb8b97b4f848db203d7e105ade 100644 (file)
@@ -30,7 +30,7 @@ RUN: llvm-readobj -mips-plt-got %p/Inputs/got-static.exe.mips \
 RUN:                  --elf-output-style=GNU | \
 RUN:   FileCheck %s -check-prefix GNU-GOT-STATIC
 
-GOT-OBJ: Error reading file: Cannot find .got section.
+GOT-OBJ: error: Cannot find .got section
 
 GOT-EXE:      Primary GOT {
 GOT-EXE-NEXT:   Canonical gp value: 0x418880
@@ -386,7 +386,7 @@ GOT-STATIC-NEXT:     }
 GOT-STATIC-NEXT:   ]
 GOT-STATIC-NEXT: }
 
-GNU-GOT-OBJ: Error reading file: Cannot find .got section.
+GNU-GOT-OBJ: error: Cannot find .got section
 
 GNU-GOT-EXE:      Primary GOT:
 GNU-GOT-EXE-NEXT:  Canonical gp value: 00418880
index a0ada84650e53ed144eaf73c870f21845cd34bb8..321d2470177b1b9b33c85cbe78db31b9f56adc2a 100644 (file)
 # CHECK-NEXT: [ 3] null{{$}}
 # CHECK-NOT:  {{.}}
 
-# RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR
-# RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1
+# RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2
 
-# RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR
-# RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1
+# RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2
 
-# ERR: Error reading file: invalid section reference.
+# ERR1: error: could not find section 'does_not_exist'
+# ERR2: error: could not find section '42'
 
 --- !ELF
 FileHeader:
index 958f84509de0a16999b63d3e0fab8ac02226b81c..f1952c739ccff177059a5f8f1d928f2dc165584f 100644 (file)
@@ -16,7 +16,7 @@
 # RUN: rm a/b/1.o
 # RUN: not llvm-readobj --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1
 # RUN: not llvm-readelf --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1
-# ERR1: Error reading file: a/relative.a: 'b/1.o': {{[Nn]}}o such file or directory
+# ERR1: error: 'a/relative.a': 'b/1.o': {{[Nn]}}o such file or directory
 
 # RUN: mkdir -p %t/c
 # RUN: cp %p/Inputs/trivial.obj.elf-x86-64 a/b/1.o
@@ -30,4 +30,4 @@
 # RUN: rm a/b/1.o
 # RUN: not llvm-readobj --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t
 # RUN: not llvm-readelf --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t
-# ERR2: Error reading file: [[DIR]]/c/absolute.a: '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory
+# ERR2: error: '[[DIR]]/c/absolute.a': '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory
index 3f103a7a01fbe008af6ffb2802cdddecec6464af..23c435c28bb5ecd7baf5ebee7de9f297dcefb766 100644 (file)
@@ -67,4 +67,4 @@
 # MISSING:     File: {{.*}}1.o
 # MISSING:     SectionHeaderCount: 10
 # MISSING-NOT: File: {{.*}}3.o
-# ERR: Error reading file: {{.*}}.a: '{{.*}}2.o': {{[Nn]}}o such file or directory
+# ERR: error: '{{.*}}.a': '{{.*}}2.o': {{[Nn]}}o such file or directory
index d500d582ca03c6439835b218a88995552c943347..e0e2e5d904444e21a91575b6ac6d9000d4562039 100644 (file)
@@ -4,4 +4,4 @@
 FileHeader:
   Version:         0x0000000c
 
-# CHECK: Error reading file: <stdin>: Bad version number
+# CHECK: error: '<stdin>': Bad version number
index 2009ddeb57aad4190d2808cd58f40d317024c126..15facefaddf59158c3b66b6650a576e3f195d22b 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm-readobj.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -54,8 +55,9 @@ getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
 
     SecIndex++;
   }
-  return make_error<StringError>("invalid section reference",
-                                 object::object_error::parse_failed);
+  return make_error<StringError>(
+      formatv("could not find section '{0}'", SecName),
+      object::object_error::parse_failed);
 }
 
 void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
index 30c51c46c9f106a46b65a92b8f96a636b19d4216..7ccc67bfb31b84224554368f2f3164802ad82901 100644 (file)
@@ -39,6 +39,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/WithColor.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -367,8 +368,8 @@ namespace opts {
 namespace llvm {
 
 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
-  errs() << "\nError reading file: " << Msg << ".\n";
-  errs().flush();
+  errs() << "\n";
+  WithColor::error(errs()) << Msg << "\n";
   exit(1);
 }
 
@@ -391,22 +392,14 @@ bool relocAddressLess(RelocationRef a, RelocationRef b) {
 
 } // namespace llvm
 
-static void reportError(StringRef Input, std::error_code EC) {
+static void reportError(StringRef Input, Error Err) {
   if (Input == "-")
     Input = "<stdin>";
-
-  reportError(Twine(Input) + ": " + EC.message());
+  error(createFileError(Input, std::move(Err)));
 }
 
-static void reportError(StringRef Input, Error Err) {
-  if (Input == "-")
-    Input = "<stdin>";
-  std::string ErrMsg;
-  {
-    raw_string_ostream ErrStream(ErrMsg);
-    logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
-  }
-  reportError(ErrMsg);
+static void reportError(StringRef Input, std::error_code EC) {
+  reportError(Input, errorCodeToError(EC));
 }
 
 static bool isMipsArch(unsigned Arch) {