llvm.org GIT mirror llvm / 4432c83
[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 Stephen Tozer 6 months ago
17 changed file(s) with 38 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
2222 RUN: not llvm-readobj %p/Inputs/corrupt-version.elf-x86_64 -dt \
2323 RUN: 2>&1 | FileCheck --check-prefix=VER %s
2424
25 VER: Error reading file: Invalid version entry.
25 VER: error: Invalid version entry
2626
2727
2828 // The file is missing the dynamic string table but has references to it.
6161 RUN: %p/Inputs/corrupt-invalid-dynamic-table-offset.elf.x86-64 2>&1 | \
6262 RUN: FileCheck --check-prefix=DYN-TABLE-OFFSET %s
6363
64 DYN-TABLE-OFFSET: Invalid data was encountered while parsing the file.
64 DYN-TABLE-OFFSET: error: Invalid data was encountered while parsing the file
6565
6666
6767 RUN: not llvm-readobj -dyn-relocations \
6868 RUN: %p/Inputs/corrupt-invalid-dynamic-table-too-large.elf.x86-64 2>&1 | \
6969 RUN: FileCheck --check-prefix=DYN-TABLE-TOO-LARGE %s
7070
71 DYN-TABLE-TOO-LARGE: Invalid data was encountered while parsing the file.
71 DYN-TABLE-TOO-LARGE: error: Invalid data was encountered while parsing the file
0 # Test case where input file does not exit.
11 RUN: not llvm-readobj %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s
2 ENOENT: {{.*}}.blah: {{[Nn]}}o such file or directory
2 ENOENT: '{{.*}}.blah': {{[Nn]}}o such file or directory
33
44 # Test case where input file is too small to be a recognised object file.
55 RUN: touch %t.empty
66 RUN: not llvm-readobj %t.empty 2>&1 | FileCheck --check-prefix=EMPTY %s
7 EMPTY: {{.*}}.empty: The file was not recognized as a valid object file
7 EMPTY: '{{.*}}.empty': The file was not recognized as a valid object file
88
99 # Test that unrecognised files in archives are ignored.
1010 RUN: rm -f %t.a
0 ; RUN: not llvm-readobj -codeview-merged-types %S/Inputs/codeview-cycle.obj 2>&1 | FileCheck %s
11
2 ; CHECK: Error{{.*}} Input type graph contains cycles
2 ; CHECK: error{{.*}} Input type graph contains cycles
33
44 ; To reproduce codeview-cycle.obj:
55 ; $ cat codeview-cycle.asm
22 # RUN: not llvm-readobj --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
33 # RUN: not llvm-readelf --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE
44
5 # ERR-SIZE: Error reading file: Invalid entity size.
5 # ERR-SIZE: error: Invalid entity size
66
77 --- !ELF
88 FileHeader:
6868 # RUN: not llvm-readobj --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING
6969 # RUN: not llvm-readelf --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING
7070
71 # BAD-STRING: Error reading file: Invalid dynamic string table reference.
71 # BAD-STRING: Invalid dynamic string table reference
7272
7373 --- !ELF
7474 FileHeader:
2020 # RUN: %python -c "with open(r'%t.truncated2', 'r+') as f: f.truncate(0xFFF)"
2121 # RUN: not llvm-readobj %t.truncated2 --dynamic-table 2>&1 | FileCheck %s
2222
23 # CHECK: Error reading file: Invalid data was encountered while parsing the file.
23 # CHECK: error: Invalid data was encountered while parsing the file
2424
2525 --- !ELF
2626 FileHeader:
0 // REQUIRES: x86-registered-target
11 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
22
3 // CHECK: Error reading file: invalid packed relocation header
3 // CHECK: error: invalid packed relocation header
44
55 .section .rela.dyn, "a", @0x60000001
66 .ascii "APS9"
0 // REQUIRES: x86-registered-target
11 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
22
3 // CHECK: Error reading file: malformed sleb128, extends past end
3 // CHECK: error: malformed sleb128, extends past end
44
55 .section .rela.dyn, "a", @0x60000001
66 .ascii "APS2"
0 // REQUIRES: x86-registered-target
11 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
22
3 // CHECK: Error reading file: malformed sleb128, extends past end
3 // CHECK: error: malformed sleb128, extends past end
44
55 .section .rela.dyn, "a", @0x60000001
66 .ascii "APS2"
0 // REQUIRES: x86-registered-target
11 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
22
3 // CHECK: Error reading file: malformed sleb128, extends past end
3 // CHECK: error: malformed sleb128, extends past end
44
55 .section .rela.dyn, "a", @0x60000001
66 .ascii "APS2"
0 // REQUIRES: x86-registered-target
11 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s
22
3 // CHECK: Error reading file: relocation group unexpectedly large
3 // CHECK: error: relocation group unexpectedly large
44
55 .section .rela.dyn, "a", @0x60000001
66 .ascii "APS2"
2929 RUN: --elf-output-style=GNU | \
3030 RUN: FileCheck %s -check-prefix GNU-GOT-STATIC
3131
32 GOT-OBJ: Error reading file: Cannot find .got section.
32 GOT-OBJ: error: Cannot find .got section
3333
3434 GOT-EXE: Primary GOT {
3535 GOT-EXE-NEXT: Canonical gp value: 0x418880
385385 GOT-STATIC-NEXT: ]
386386 GOT-STATIC-NEXT: }
387387
388 GNU-GOT-OBJ: Error reading file: Cannot find .got section.
388 GNU-GOT-OBJ: error: Cannot find .got section
389389
390390 GNU-GOT-EXE: Primary GOT:
391391 GNU-GOT-EXE-NEXT: Canonical gp value: 00418880
4949 # CHECK-NEXT: [ 3] null{{$}}
5050 # CHECK-NOT: {{.}}
5151
52 # RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR
53 # RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR
52 # RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1
53 # RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2
5454
55 # RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR
56 # RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR
55 # RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1
56 # RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2
5757
58 # ERR: Error reading file: invalid section reference.
58 # ERR1: error: could not find section 'does_not_exist'
59 # ERR2: error: could not find section '42'
5960
6061 --- !ELF
6162 FileHeader:
1515 # RUN: rm a/b/1.o
1616 # RUN: not llvm-readobj --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1
1717 # RUN: not llvm-readelf --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1
18 # ERR1: Error reading file: a/relative.a: 'b/1.o': {{[Nn]}}o such file or directory
18 # ERR1: error: 'a/relative.a': 'b/1.o': {{[Nn]}}o such file or directory
1919
2020 # RUN: mkdir -p %t/c
2121 # RUN: cp %p/Inputs/trivial.obj.elf-x86-64 a/b/1.o
2929 # RUN: rm a/b/1.o
3030 # RUN: not llvm-readobj --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t
3131 # RUN: not llvm-readelf --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t
32 # ERR2: Error reading file: [[DIR]]/c/absolute.a: '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory
32 # ERR2: error: '[[DIR]]/c/absolute.a': '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory
6666 # MISSING: File: {{.*}}1.o
6767 # MISSING: SectionHeaderCount: 10
6868 # MISSING-NOT: File: {{.*}}3.o
69 # ERR: Error reading file: {{.*}}.a: '{{.*}}2.o': {{[Nn]}}o such file or directory
69 # ERR: error: '{{.*}}.a': '{{.*}}2.o': {{[Nn]}}o such file or directory
33 FileHeader:
44 Version: 0x0000000c
55
6 # CHECK: Error reading file: : Bad version number
6 # CHECK: error: '': Bad version number
1515 #include "llvm-readobj.h"
1616 #include "llvm/Object/ObjectFile.h"
1717 #include "llvm/Support/Error.h"
18 #include "llvm/Support/FormatVariadic.h"
1819 #include "llvm/Support/ScopedPrinter.h"
1920 #include "llvm/Support/raw_ostream.h"
2021
5354
5455 SecIndex++;
5556 }
56 return make_error("invalid section reference",
57 object::object_error::parse_failed);
57 return make_error(
58 formatv("could not find section '{0}'", SecName),
59 object::object_error::parse_failed);
5860 }
5961
6062 void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
3838 #include "llvm/Support/Path.h"
3939 #include "llvm/Support/ScopedPrinter.h"
4040 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/WithColor.h"
4142
4243 using namespace llvm;
4344 using namespace llvm::object;
366367 namespace llvm {
367368
368369 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
369 errs() << "\nError reading file: " << Msg << ".\n";
370 errs().flush();
370 errs() << "\n";
371 WithColor::error(errs()) << Msg << "\n";
371372 exit(1);
372373 }
373374
390391
391392 } // namespace llvm
392393
393 static void reportError(StringRef Input, std::error_code EC) {
394 if (Input == "-")
395 Input = "";
396
397 reportError(Twine(Input) + ": " + EC.message());
398 }
399
400394 static void reportError(StringRef Input, Error Err) {
401395 if (Input == "-")
402396 Input = "";
403 std::string ErrMsg;
404 {
405 raw_string_ostream ErrStream(ErrMsg);
406 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
407 }
408 reportError(ErrMsg);
397 error(createFileError(Input, std::move(Err)));
398 }
399
400 static void reportError(StringRef Input, std::error_code EC) {
401 reportError(Input, errorCodeToError(EC));
409402 }
410403
411404 static bool isMipsArch(unsigned Arch) {