[llvm-readobj]Fix error messages for bad archive members and add testing for archive...
authorJames Henderson <jh7370@my.bristol.ac.uk>
Wed, 27 Feb 2019 11:07:08 +0000 (11:07 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Wed, 27 Feb 2019 11:07:08 +0000 (11:07 +0000)
llvm-readobj's error messages were broken for bad archive members. This
patch fixes them, and also adds testing for archive and thin archive
handling within llvm-readobj.

Reviewed by: rupprecht, grimar, higuoxing

Differential Revision: https://reviews.llvm.org/D58681

llvm-svn: 354960

llvm/test/tools/llvm-readobj/archive.test [new file with mode: 0644]
llvm/test/tools/llvm-readobj/thin-archive.test [new file with mode: 0644]
llvm/tools/llvm-readobj/llvm-readobj.cpp

diff --git a/llvm/test/tools/llvm-readobj/archive.test b/llvm/test/tools/llvm-readobj/archive.test
new file mode 100644 (file)
index 0000000..08b04fa
--- /dev/null
@@ -0,0 +1,42 @@
+# Show that dumping operations apply to all members in an archive.
+
+# RUN: rm -f %t.a
+# RUN: llvm-ar rc %t.a %p/Inputs/trivial.obj.elf-x86-64 %p/Inputs/trivial.obj.elf-i386 %p/Inputs/trivial.obj.coff-arm
+# RUN: llvm-readobj --all %t.a | FileCheck %s --check-prefixes=LLVM,COFF
+# RUN: llvm-readelf --all %t.a | FileCheck %s --check-prefixes=GNU,COFF
+
+# LLVM: File: trivial.obj.elf-x86-64
+# LLVM: Format: ELF64-x86-64
+# LLVM: ElfHeader {
+# LLVM:   Machine: EM_X86_64
+# LLVM: Sections [
+# LLVM: Relocations [
+# LLVM: Symbols [
+
+# GNU: ELF Header:
+# GNU:   Machine: Advanced Micro Devices X86-64
+# GNU: Section Headers:
+# GNU: Relocation section '.rela.text'
+# GNU: Symbol table '.symtab'
+
+# LLVM: File: trivial.obj.elf-i386
+# LLVM: Format: ELF32-i386
+# LLVM: ElfHeader {
+# LLVM:   Machine: EM_386
+# LLVM: Sections [
+# LLVM: Relocations [
+# LLVM: Symbols [
+
+# GNU: ELF Header:
+# GNU:   Machine: Intel 80386
+# GNU: Section Headers:
+# GNU: Relocation section '.rel.text'
+# GNU: Symbol table '.symtab'
+
+# LLVM: File: trivial.obj.coff-arm
+# LLVM: Format: COFF-ARM
+# COFF: ImageFileHeader {
+# COFF:   Machine: IMAGE_FILE_MACHINE_ARMNT
+# COFF: Sections [
+# COFF: Relocations [
+# COFF: Symbols [
diff --git a/llvm/test/tools/llvm-readobj/thin-archive.test b/llvm/test/tools/llvm-readobj/thin-archive.test
new file mode 100644 (file)
index 0000000..3cdbaa5
--- /dev/null
@@ -0,0 +1,69 @@
+# RUN: rm -f %t.a
+# RUN: rm -rf %t
+# RUN: mkdir -p %t
+
+# Make copies of the test inputs for placing in the archive so that we can mess
+# about with them later on.
+# RUN: cp %p/Inputs/trivial.obj.elf-x86-64 %t/1.o
+# RUN: cp %p/Inputs/relocs.obj.elf-x86_64 %t/2.o
+# RUN: cp %p/Inputs/trivial.obj.coff-arm %t/3.o
+# RUN: llvm-ar rcT %t.a %t/1.o %t/2.o %t/3.o
+
+# Test that basic dumping works for all members.
+# RUN: llvm-readobj --all %t.a | FileCheck %s --check-prefixes=LLVM,COFF
+# RUN: llvm-readelf --all %t.a | FileCheck %s --check-prefixes=GNU,COFF
+
+# LLVM: File: {{.*}}1.o
+# LLVM: Format: ELF64-x86-64
+# LLVM: ElfHeader {
+# LLVM:   SectionHeaderCount: 10
+# LLVM: Sections [
+# LLVM: Relocations [
+# LLVM: Symbols [
+
+# GNU: ELF Header:
+# GNU:   Number of section headers: 10
+# GNU: Section Headers:
+# GNU: Relocation section '.rela.text'
+# GNU: Symbol table '.symtab'
+
+# LLVM: File: {{.*}}2.o
+# LLVM: Format: ELF64-x86-64
+# LLVM: ElfHeader {
+# LLVM:   SectionHeaderCount: 8
+# LLVM: Sections [
+# LLVM: Relocations [
+# LLVM: Symbols [
+
+# GNU: ELF Header:
+# GNU:   Number of section headers: 8
+# GNU: Section Headers:
+# GNU: Relocation section '.rela.text'
+# GNU: Symbol table '.symtab'
+
+# LLVM: File: {{.*}}3.o
+# LLVM: Format: COFF-ARM
+# COFF: ImageFileHeader {
+# COFF: Sections [
+# COFF: Relocations [
+# COFF: Symbols [
+
+# Overwrite one of the members with a member of a different size to show that
+# the size field in the member header is not relevant.
+# RUN: cp %t/1.o %t/2.o
+# RUN: llvm-readobj --file-headers %t.a | FileCheck %s --check-prefix=RESIZED
+
+# RESIZED: File: {{.*}}1.o
+# RESIZED: SectionHeaderCount: 10
+# RESIZED: File: {{.*}}2.o
+# RESIZED: SectionHeaderCount: 10
+
+# Remove the second member and show that the first can still be dumped, but that the last isn't.
+# RUN: rm %t/2.o
+# RUN: not llvm-readobj --file-headers %t.a 2> %t.err | FileCheck %s --check-prefix=MISSING
+# RUN: FileCheck %s --check-prefix=ERR --input-file=%t.err
+
+# 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
index 2c1674e..14f8dae 100644 (file)
@@ -582,7 +582,7 @@ static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
     Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
     if (!ChildOrErr) {
       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
-        reportError(Arc->getFileName(), ChildOrErr.takeError());
+        reportError(Arc->getFileName(), std::move(E));
       }
       continue;
     }