OSDN Git Service

[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 9cbe9ef..82a581f 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
 
 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.
 
 
 // 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
 
 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
 
 
 
 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 ffe2374..a08cc10 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
 # 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
 
 # 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
 
 # Test that unrecognised files in archives are ignored.
 RUN: rm -f %t.a
index f597ad6..4d612e8 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: not llvm-readobj -codeview-merged-types %S/Inputs/codeview-cycle.obj 2>&1 | FileCheck %s
 
 ; 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
 
 ; To reproduce codeview-cycle.obj:
 ; $ cat codeview-cycle.asm
index 79af769..4e04423 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
 
 # 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:
 
 --- !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
 
 # 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:
 
 --- !ELF
 FileHeader:
index f061e1c..e762a27 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
 
 # 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:
 
 --- !ELF
 FileHeader:
index 87dc689..e418353 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
 
 // 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"
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS9"
index cbba5b7..28c325f 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
 
 // 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"
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 7b2aa7d..e373a53 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
 
 // 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"
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index b8992e6..94ef6f4 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
 
 // 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"
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 98a151e..b0c549f 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
 
 // 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"
 
 .section .rela.dyn, "a", @0x60000001
 .ascii "APS2"
index 7d8d266..ca7edc2 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
 
 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
 
 GOT-EXE:      Primary GOT {
 GOT-EXE-NEXT:   Canonical gp value: 0x418880
@@ -386,7 +386,7 @@ GOT-STATIC-NEXT:     }
 GOT-STATIC-NEXT:   ]
 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
 
 GNU-GOT-EXE:      Primary GOT:
 GNU-GOT-EXE-NEXT:  Canonical gp value: 00418880
index a0ada84..321d247 100644 (file)
 # CHECK-NEXT: [ 3] null{{$}}
 # CHECK-NOT:  {{.}}
 
 # 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:
 
 --- !ELF
 FileHeader:
index 958f845..f1952c7 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
 # 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
 
 # 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
 # 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 3f103a7..23c435c 100644 (file)
@@ -67,4 +67,4 @@
 # MISSING:     File: {{.*}}1.o
 # MISSING:     SectionHeaderCount: 10
 # MISSING-NOT: File: {{.*}}3.o
 # 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 d500d58..e0e2e5d 100644 (file)
@@ -4,4 +4,4 @@
 FileHeader:
   Version:         0x0000000c
 
 FileHeader:
   Version:         0x0000000c
 
-# CHECK: Error reading file: <stdin>: Bad version number
+# CHECK: error: '<stdin>': Bad version number
index 2009dde..15facef 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm-readobj.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Error.h"
 #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"
 
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -54,8 +55,9 @@ getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
 
     SecIndex++;
   }
 
     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,
 }
 
 void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
index 30c51c4..7ccc67b 100644 (file)
@@ -39,6 +39,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "llvm/Support/TargetRegistry.h"
 #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;
 
 using namespace llvm;
 using namespace llvm::object;
@@ -367,8 +368,8 @@ namespace opts {
 namespace llvm {
 
 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
 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);
 }
 
   exit(1);
 }
 
@@ -391,22 +392,14 @@ bool relocAddressLess(RelocationRef a, RelocationRef b) {
 
 } // namespace llvm
 
 
 } // namespace llvm
 
-static void reportError(StringRef Input, std::error_code EC) {
+static void reportError(StringRef Input, Error Err) {
   if (Input == "-")
     Input = "<stdin>";
   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) {
 }
 
 static bool isMipsArch(unsigned Arch) {