Index: test/ELF/relocatable-convert-rela-rel.s =================================================================== --- /dev/null +++ test/ELF/relocatable-convert-rela-rel.s @@ -0,0 +1,105 @@ +# REQUIRES: x86 +# Check that we correctly write addends with -r if the output use Elf_Rel but +# the input uses Elf_Rela + +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t-x86_64-rela.o +# RUN: llvm-objdump --section=.data -s %t-x86_64-rela.o | FileCheck -check-prefix DATA-RELA %s +# DATA-RELA: Contents of section .data: +# DATA-RELA-NEXT: 0000 00000000 00000000 78563412 00efcdab + +# RUN: llvm-mc -filetype=obj -triple=i386-unknown-linux %s -o %t-i386-rel.o +# RUN: llvm-objdump --section=.data -s %t-i386-rel.o | FileCheck -check-prefix DATA-REL %s +# Rel should have the relocation value 10 in .data: +# DATA-REL: Contents of section .data: +# DATA-REL-NEXT: 0000 00000000 44550000 78563412 00efcdab +# ^---- (addend 0x5544) + +# Create two object files that use the non-default relocation format +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -elf-relocation-format=rel -o %t-x86_64-rel.o +# RUN: llvm-objdump --section=.data -s %t-x86_64-rel.o | FileCheck -check-prefix DATA-REL %s +# RUN: llvm-mc -filetype=obj -triple=i386-unknown-linux %s -elf-relocation-format=rela -o %t-i386-rela.o +# RUN: llvm-objdump --section=.data -s %t-i386-rela.o | FileCheck -check-prefix DATA-RELA %s + +# RUN: ld.lld -r -o %t.o %t-i386-rel.o +# RUN: llvm-readobj -h -s -section-data -relocations %t.o | FileCheck -check-prefix RELOCATABLE-REL %s + +# Note: llvm-readobj prints 0x0 as the relocation addend since it doesn't parse +# REL relocations. In order to verify the addend we need to check the contents of .data +# RELOCATABLE-REL: ElfHeader { +# RELOCATABLE-REL: Class: 32-bit +# RELOCATABLE-REL: DataEncoding: LittleEndian +# RELOCATABLE-REL: Section { +# RELOCATABLE-REL: Name: .data +# RELOCATABLE-REL: SectionData ( +# RELOCATABLE-REL-NEXT: 0000: 00000000 44550000 78563412 00EFCDAB |....DU..xV4.....| +# ^--- Addend 0x5544 for the relocation below +# RELOCATABLE-REL-NEXT: ) +# RELOCATABLE-REL: Relocations [ +# RELOCATABLE-REL-NEXT: Section ({{.+}}) .rel.data { +# RELOCATABLE-REL-NEXT: 0x4 R_386_32 foo 0x0 +# RELOCATABLE-REL-NEXT: } +# RELOCATABLE-REL-NEXT: ] + + +# RUN: ld.lld -r -o %t.o %t-x86_64-rela.o +# RUN: llvm-readobj -h -s -section-data -relocations %t.o | FileCheck -check-prefix RELOCATABLE-RELA %s +# RELOCATABLE-RELA: ElfHeader { +# RELOCATABLE-RELA: Class: 64-bit +# RELOCATABLE-RELA: DataEncoding: LittleEndian +# RELOCATABLE-RELA: Section { +# RELOCATABLE-RELA: Name: .data +# RELOCATABLE-RELA: SectionData ( +# RELOCATABLE-RELA-NEXT: 0000: 00000000 00000000 78563412 00EFCDAB |........xV4.....| +# ^--- No addend written here since it is part of Elf_Rela +# RELOCATABLE-RELA: Relocations [ +# RELOCATABLE-RELA-NEXT: Section ({{.+}}) .rela.data { +# RELOCATABLE-RELA-NEXT: 0x4 R_X86_64_32 foo 0x5544 +# RELOCATABLE-RELA-NEXT: } +# RELOCATABLE-RELA-NEXT: ] + +# Previously we would lose the addend if the output uses REL but a .foo.rela was being +# copied into the relocatable output. This happenend because addends were only added +# to the output section when Config->IsRela was true instead of checking for RelTy::IsRela. +# Check that the relacation addends are copied into the output as expected: +# RUN: ld.lld -r -o %t.o %t-i386-rela.o +# RUN: llvm-readobj -h -s -section-data -relocations %t.o | FileCheck -check-prefix RELA-TO-REL %s +# RELA-TO-REL: ElfHeader { +# RELA-TO-REL: Class: 32-bit +# RELA-TO-REL: DataEncoding: LittleEndian +# RELA-TO-REL: Section { +# RELA-TO-REL: Name: .data +# RELA-TO-REL: SectionData ( +# RELA-TO-REL-NEXT: 0000: 00000000 00000000 78563412 00EFCDAB |........xV4.....| +# ^--- No addend written here since it is part of Elf_Rela +# RELA-TO-REL: Relocations [ +# RELA-TO-REL-NEXT: Section ({{.+}}) .rela.data { +# RELA-TO-REL-NEXT: 0x4 R_386_32 foo 0x5544 +# RELA-TO-REL-NEXT: } +# RELA-TO-REL-NEXT: ] + + +# And also check that RELA output works fine with REL input +# RUN: ld.lld -r -o %t.o %t-x86_64-rel.o +# RUN: llvm-readobj -h -s -section-data -relocations %t.o | FileCheck -check-prefix REL-TO-RELA %s +# REL-TO-RELA: ElfHeader { +# REL-TO-RELA: Class: 64-bit +# REL-TO-RELA: DataEncoding: LittleEndian +# REL-TO-RELA: Section { +# REL-TO-RELA: Name: .data +# REL-TO-RELA: SectionData ( +# REL-TO-RELA-NEXT: 0000: 00000000 44550000 78563412 00EFCDAB |....DU..xV4.....| +# ^--- Addend from the input file for the relocation below +# REL-TO-RELA-NEXT: ) +# REL-TO-RELA: Relocations [ +# REL-TO-RELA-NEXT: Section ({{.+}}) .rel.data { +# REL-TO-RELA-NEXT: 0x4 R_X86_64_32 foo 0x0 +# REL-TO-RELA-NEXT: } +# REL-TO-RELA-NEXT: ] + + +.extern foo + +.data +.long 0 +.long foo + 0x5544 +.quad 0xabcdef0012345678