Index: lld/trunk/ELF/InputFiles.cpp =================================================================== --- lld/trunk/ELF/InputFiles.cpp +++ lld/trunk/ELF/InputFiles.cpp @@ -288,12 +288,6 @@ if (Config->Optimize == 0) return false; - // Do not merge sections if generating a relocatable object. It makes - // the code simpler because we do not need to update relocation addends - // to reflect changes introduced by merging. - 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: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -465,9 +465,9 @@ return Out->RelocationSection; } - // When control reaches here, mergeable sections have already been - // merged except the -r case. If that's the case, we do not combine them - // and let final link to handle this optimization. + // When control reaches here, mergeable sections have already been merged into + // synthetic sections. For relocatable case we want to create one output + // section per syntetic section so that they have a valid sh_entsize. if (Config->Relocatable && (IS->Flags & SHF_MERGE)) return createSection(IS, OutsecName); Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -496,7 +496,7 @@ // Unlike other synthetic sections, mergeable output sections contain data // copied from input sections, and there may be a relocation pointing to its - // contents if -emit-reloc is given. + // contents if -r or -emit-reloc are given. if (isa(IS) && !(IS->Flags & SHF_MERGE)) continue; Index: lld/trunk/test/ELF/merge-reloc.s =================================================================== --- lld/trunk/test/ELF/merge-reloc.s +++ lld/trunk/test/ELF/merge-reloc.s @@ -3,10 +3,9 @@ # RUN: ld.lld %t.o -r -o %t-rel # RUN: llvm-readobj -s -section-data %t-rel | FileCheck %s -# When linker generates a relocatable object it should keep "merge" -# sections as-is: do not merge content, do not join regular and -# "merge" sections, do not joint "merge" sections with different -# entry size. +# When linker generates a relocatable object it does string merging in the same +# way as for regular link. It should keep SHF_MERGE flag and set proper sh_entsize +# value so that final link can perform the final merging optimization. # CHECK: Section { # CHECK: Index: @@ -18,13 +17,13 @@ # CHECK-NEXT: ] # CHECK-NEXT: Address: # CHECK-NEXT: Offset: -# CHECK-NEXT: Size: 12 +# CHECK-NEXT: Size: 4 # CHECK-NEXT: Link: 0 # CHECK-NEXT: Info: 0 # CHECK-NEXT: AddressAlignment: 4 # CHECK-NEXT: EntrySize: 4 # CHECK-NEXT: SectionData ( -# CHECK-NEXT: 0000: 42000000 42000000 42000000 +# CHECK-NEXT: 0000: 42000000 # CHECK-NEXT: ) # CHECK-NEXT: } # CHECK: Section { @@ -37,13 +36,13 @@ # CHECK-NEXT: ] # CHECK-NEXT: Address: # CHECK-NEXT: Offset: -# CHECK-NEXT: Size: 16 +# CHECK-NEXT: Size: 8 # CHECK-NEXT: Link: 0 # CHECK-NEXT: Info: 0 # CHECK-NEXT: AddressAlignment: 8 # CHECK-NEXT: EntrySize: 8 # CHECK-NEXT: SectionData ( -# CHECK-NEXT: 0000: 42000000 42000000 42000000 42000000 +# CHECK-NEXT: 0000: 42000000 42000000 # CHECK-NEXT: ) # CHECK-NEXT: } # CHECK: Section {