Index: test/tools/llvm-objcopy/ELF/common-suffix-string-tables.test =================================================================== --- test/tools/llvm-objcopy/ELF/common-suffix-string-tables.test +++ test/tools/llvm-objcopy/ELF/common-suffix-string-tables.test @@ -0,0 +1,74 @@ +## This test shows that when symbol/section names are optimized in the string +## table to share the same entry, because one is a substring of the other, +## llvm-objcopy operations function correctly without corrupting the names of +## unchanged sections/symbols. + +# RUN: yaml2obj %s -o %t.o +## Sanity check that the strings have been pooled. +# RUN: llvm-readobj %t.o --string-dump .strtab --string-dump .shstrtab \ +# RUN: | FileCheck %s --check-prefix=VALIDATE --implicit-check-not=bar --implicit-check-not=.blag + +# VALIDATE: String dump of section '.strtab': +# VALIDATE: foobar +# VALIDATE: String dump of section '.shstrtab': +# VALIDATE: .blah.blag + +## Case 1: Basic copy. +# RUN: llvm-objcopy %t.o %t.copy +# RUN: llvm-readobj --section-headers --symbols %t.copy | FileCheck %s --check-prefix=COPY + +# COPY: Sections [ +# COPY: Name: .blah.blag ( +# COPY: Name: .blag ( +# COPY: Symbols [ +# COPY: Name: bar ( +# COPY: Name: foobar ( + +## Case 2: Rename section. +# RUN: llvm-objcopy %t.o %t.rename-section --rename-section .blag=.blab +# RUN: llvm-readobj --section-headers --symbols %t.rename-section \ +# RUN: | FileCheck %s --check-prefix=RENAME-SECTION + +# RENAME-SECTION: Sections [ +# RENAME-SECTION: Name: .blah.blag ( +# RENAME-SECTION: Name: .blab ( + +## Case 3: Rename symbol. +# RUN: llvm-objcopy %t.o %t.rename-symbol --redefine-sym bar=rab +# RUN: llvm-readobj --section-headers --symbols %t.rename-symbol \ +# RUN: | FileCheck %s --check-prefix=RENAME-SYMBOL + +# RENAME-SYMBOL: Symbols [ +# RENAME-SYMBOL: Name: rab ( +# RENAME-SYMBOL: Name: foobar ( + +## Case 4: Remove section. +# RUN: llvm-objcopy %t.o %t.remove-section -R .blag +# RUN: llvm-readobj --section-headers --symbols %t.remove-section \ +# RUN: | FileCheck %s --check-prefix=REMOVE-SECTION --implicit-check-not .blag + +# REMOVE-SECTION: Sections [ +# REMOVE-SECTION: Name: .blah.blag ( + +## Case 5: Remove symbol. +# RUN: llvm-objcopy %t.o %t.remove-symbol -N bar +# RUN: llvm-readobj --section-headers --symbols %t.remove-symbol \ +# RUN: | FileCheck %s --check-prefix=REMOVE-SYMBOL --implicit-check-not bar + +# REMOVE-SYMBOL: Symbols [ +# REMOVE-SYMBOL: Name: foobar ( + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .blah.blag + Type: SHT_PROGBITS + - Name: .blag + Type: SHT_PROGBITS +Symbols: + - Name: bar + - Name: foobar Index: test/tools/llvm-objcopy/ELF/invalid-e_phoff.test =================================================================== --- test/tools/llvm-objcopy/ELF/invalid-e_phoff.test +++ 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+') 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+') 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 Index: test/tools/llvm-objcopy/ELF/invalid-e_shoff.test =================================================================== --- test/tools/llvm-objcopy/ELF/invalid-e_shoff.test +++ 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+') 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+') 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 Index: test/tools/llvm-objcopy/ELF/linked-section.test =================================================================== --- test/tools/llvm-objcopy/ELF/linked-section.test +++ 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{{$}} Index: test/tools/llvm-objcopy/ELF/overlapping-sections.test =================================================================== --- test/tools/llvm-objcopy/ELF/overlapping-sections.test +++ test/tools/llvm-objcopy/ELF/overlapping-sections.test @@ -0,0 +1,56 @@ +## This test shows that llvm-objcopy does not baulk at overlapping sections. +## 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 ELF +## header. +# RUN: llvm-readobj --file-headers %t.o | FileCheck %s --check-prefix=SHDRS-OFFSET +# SHDRS-OFFSET: SectionHeaderOffset: 0x40{{$}} + +## 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+') as input: import struct; bytes = struct.pack('