[llvm-readobj] Reapply: Improve error message for --string-dump

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
This commit is contained in:
Stephen Tozer 2019-04-15 11:17:48 +00:00
parent a5912ea9c6
commit 4432c83404
17 changed files with 38 additions and 42 deletions

View 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

View 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

View 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

View 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:

View 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:

View 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"

View 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"

View 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"

View 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"

View 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"

View 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

View File

@ -50,13 +50,14 @@
# 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:

View 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

View 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

View File

@ -4,4 +4,4 @@
FileHeader:
Version: 0x0000000c
# CHECK: Error reading file: <stdin>: Bad version number
# CHECK: error: '<stdin>': Bad version number

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

View 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) {
if (Input == "-")
Input = "<stdin>";
reportError(Twine(Input) + ": " + EC.message());
}
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);
error(createFileError(Input, std::move(Err)));
}
static void reportError(StringRef Input, std::error_code EC) {
reportError(Input, errorCodeToError(EC));
}
static bool isMipsArch(unsigned Arch) {