Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -178,6 +178,13 @@ if (Config->Optimize == 0) return false; + // In case of relocatable object generation we do not update any relocation + // addends, but as a result of the merging section's size might change + // and so we get a relocation points to out of the section border. + // To prevent this do not merge sections. + if (Config->Relocatable) + return false; + // A mergeable section with size 0 is useless because they don't have // any data to merge. A mergeable string section with size 0 can be // argued as invalid because it doesn't end with a null character. Index: test/ELF/merge.s =================================================================== --- test/ELF/merge.s +++ test/ELF/merge.s @@ -5,6 +5,9 @@ // RUN: llvm-readobj -s -section-data -t %t | FileCheck %s // RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s +// RUN: ld.lld %t.o %t2.o -r -o %t-rel +// RUN: llvm-readobj -s -section-data %t-rel | FileCheck --check-prefix=REL %s + .section .mysec,"aM",@progbits,4 .align 4 .global foo @@ -109,3 +112,21 @@ // From the other file: movl .mysec, %eax // addr(0x42) = 65828 // DISASM-NEXT: movl 65828, %eax + +// REL: Name: .mysec +// REL-NEXT: Type: SHT_PROGBITS +// REL-NEXT: Flags [ +// REL-NEXT: SHF_ALLOC +// REL-NEXT: SHF_MERGE +// REL-NEXT: ] +// REL-NEXT: Address: 0x0 +// REL-NEXT: Offset: 0x40 +// REL-NEXT: Size: 20 +// REL-NEXT: Link: 0 +// REL-NEXT: Info: 0 +// REL-NEXT: AddressAlignment: 4 +// REL-NEXT: EntrySize: 0 +// REL-NEXT: SectionData ( +// REL-NEXT: 0000: 10000000 42000000 42000000 42000000 +// REL-NEXT: 0010: 42000000 +// REL-NEXT: )