[lld] r318516 - [ELF] - Allow applying SHF_MERGE optimization for relocatable output.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 17 03:27:58 PST 2017


Author: grimar
Date: Fri Nov 17 03:27:57 2017
New Revision: 318516

URL: http://llvm.org/viewvc/llvm-project?rev=318516&view=rev
Log:
[ELF] - Allow applying SHF_MERGE optimization for relocatable output.

This fixes PR35223.

Here I enabled SHF_MERGE section content merging for -r like
we do for regular linking.

Differential revision: https://reviews.llvm.org/D40026

Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/merge-reloc.s

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=318516&r1=318515&r2=318516&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Fri Nov 17 03:27:57 2017
@@ -288,12 +288,6 @@ template <class ELFT> bool ObjFile<ELFT>
   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.

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=318516&r1=318515&r2=318516&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Nov 17 03:27:57 2017
@@ -465,9 +465,9 @@ static OutputSection *addInputSec(String
     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);
 

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=318516&r1=318515&r2=318516&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Nov 17 03:27:57 2017
@@ -496,7 +496,7 @@ template <class ELFT> void Writer<ELFT>:
 
     // 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<SyntheticSection>(IS) && !(IS->Flags & SHF_MERGE))
       continue;
 

Modified: lld/trunk/test/ELF/merge-reloc.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/merge-reloc.s?rev=318516&r1=318515&r2=318516&view=diff
==============================================================================
--- lld/trunk/test/ELF/merge-reloc.s (original)
+++ lld/trunk/test/ELF/merge-reloc.s Fri Nov 17 03:27:57 2017
@@ -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 {




More information about the llvm-commits mailing list