From: James Henderson Date: Fri, 10 May 2019 12:58:52 +0000 (+0000) Subject: [llvm-objcopy] Add additional testing for various cases X-Git-Tag: android-x86-9.0-r1~3604 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=e8d332f233dd6663bf033ac6ef5caf7efba9423c;p=android-x86%2Fexternal-llvm.git [llvm-objcopy] Add additional testing for various cases This patch adds a number of tests to test various cases not covered by existing tests. All of them work correctly, with no need to change llvm-objcopy itself, although some do indicate possible areas for improvement. Reviewed by: MaskRay Differential Revision: https://reviews.llvm.org/D61727 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360422 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/tools/llvm-objcopy/ELF/invalid-e_phoff.test b/test/tools/llvm-objcopy/ELF/invalid-e_phoff.test new file mode 100644 index 00000000000..5efe2fc3829 --- /dev/null +++ b/test/tools/llvm-objcopy/ELF/invalid-e_phoff.test @@ -0,0 +1,31 @@ +## If the e_phoff field is set to a value either past the end of the file, or +## such that e_phoff + e_phnum * sizeof(Elf_Phdr) is past the end of the file, +## we should emit an error. This test checks that the emitted error is sensible. + +# RUN: yaml2obj %s -o %t.o +## Remove the default section headers so that the section header table is not invalid. +# RUN: llvm-objcopy --strip-sections %t.o +# RUN: cp %t.o %t2.o + +## Sanity check that the phdr table is at offset 64: +# RUN: llvm-readobj --file-headers %t.o | FileCheck %s --check-prefix=VALIDATE +# VALIDATE: ProgramHeaderOffset: 0x40{{$}} + +## Truncate the file to end before the program header table ends. +# RUN: %python -c "with open('%/t.o', 'r+b') as input: input.truncate(65)" +# RUN: not llvm-objcopy %t.o 2>&1 | FileCheck %s + +## Set the e_phoff field to a value much larger than the object file size. +# RUN: %python -c "with open('%/t2.o', 'r+b') as input: import struct; bytes = struct.pack('&1 | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +ProgramHeaders: + - Type: PT_LOAD + +# CHECK: error: program headers longer than binary diff --git a/test/tools/llvm-objcopy/ELF/invalid-e_shoff.test b/test/tools/llvm-objcopy/ELF/invalid-e_shoff.test new file mode 100644 index 00000000000..7eecde6b0ab --- /dev/null +++ b/test/tools/llvm-objcopy/ELF/invalid-e_shoff.test @@ -0,0 +1,30 @@ +## If the e_shoff field is set to a value either past the end of the file, or +## such that e_shoff + e_shnum * sizeof(Elf_Shdr) is past the end of the file, +## we should emit an error. This test checks that the emitted error is sensible. + +# RUN: yaml2obj %s -o %t.o +# RUN: cp %t.o %t2.o + +## Sanity check that the section header table is at offset 64: +# RUN: llvm-readobj --file-headers %t.o | FileCheck %s --check-prefix=VALIDATE +# VALIDATE: SectionHeaderOffset: 0x40{{$}} + +## Truncate the file to end before the section header table ends. +# RUN: %python -c "with open('%/t.o', 'r+b') as input: input.truncate(65)" +# RUN: not llvm-objcopy %t.o 2>&1 | FileCheck %s -DINPUT=%t.o + +## Set the e_shoff field to a value much larger than the object file size. +# RUN: %python -c "with open('%/t2.o', 'r+b') as input: import struct; bytes = struct.pack('&1 | FileCheck %s -DINPUT=%t2.o + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + +# CHECK: error: '[[INPUT]]': section header table goes past the end of the file diff --git a/test/tools/llvm-objcopy/ELF/linked-section.test b/test/tools/llvm-objcopy/ELF/linked-section.test new file mode 100644 index 00000000000..eb54dedda44 --- /dev/null +++ b/test/tools/llvm-objcopy/ELF/linked-section.test @@ -0,0 +1,29 @@ +## This test checks that if a section has a sh_link field, and one or more +## sections are removed such that the target section index changes, then +## llvm-objcopy correctly updates the sh_link field. + +# RUN: yaml2obj %s -o %t.o +# RUN: llvm-objcopy --remove-section .foo %t.o %t2.o +# RUN: llvm-readobj --section-headers %t2.o | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + - Name: .bar + Type: SHT_PROGBITS + - Name: .baz + Type: SHT_PROGBITS + Link: .bar + +# CHECK: Index: 1 +# CHECK-NEXT: Name: .bar +# CHECK: Name: .baz +## Find the next "Link" line, then check the value is exactly the index of .bar. +# CHECK: Link +# CHECK-SAME: : 1{{$}} diff --git a/test/tools/llvm-objcopy/ELF/overlapping-sections-in-segments.test b/test/tools/llvm-objcopy/ELF/overlapping-sections-in-segments.test new file mode 100644 index 00000000000..94363a457d3 --- /dev/null +++ b/test/tools/llvm-objcopy/ELF/overlapping-sections-in-segments.test @@ -0,0 +1,56 @@ +## This test shows that llvm-objcopy does not baulk at overlapping sections +## where such sections are within segments. +## These don't really make sense, but the tool should still handle invalid +## inputs somehow. +# RUN: yaml2obj %s -o %t.o + +## First, check that the section header table appears immediately after the program +## header table. +# RUN: llvm-readobj --file-headers %t.o | FileCheck %s --check-prefix=SHDRS-OFFSET +# SHDRS-OFFSET: SectionHeaderOffset: 0x78{{$}} + +## Binary edit the section header sh_offset field of the second section to +## overlap the first one. +# RUN: %python -c "with open('%/t.o', 'r+b') as input: import struct; bytes = struct.pack('