[PATCH] D43942: [lld] Fix handling of output section selection for unmerged mergeable inputs and relocatable output

Owen Reynolds via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 07:11:48 PST 2018


gbreynoo created this revision.
gbreynoo added reviewers: grimar, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.

Usually mergeable sections, those with the flag SHF_MERGE, can be combined into synthetic sections; however use of the optimisation flag "-O0" does not allow this merging. When "-O0" is called with relocatable object output "-r", these non synthetic sections are used to create output sections. This allows the incorrect output of multiple sections with same name.

The change below ensures only synthetic sections are used to create output sections in this way.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D43942

Files:
  ELF/LinkerScript.cpp
  test/ELF/merge-relocatable-O0.s


Index: test/ELF/merge-relocatable-O0.s
===================================================================
--- test/ELF/merge-relocatable-O0.s
+++ test/ELF/merge-relocatable-O0.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o
+# RUN: ld.lld -r -O0 %t.o %t2.o -o %t
+# RUN: llvm-objdump -section-headers %t | FileCheck %s --check-prefix=RODATA
+# RUN: llvm-objdump -section-headers %t | FileCheck %s --check-prefix=DEBUG_STR
+
+# RODATA:         .rodata.1
+# RODATA-NOT:     .rodata.1
+# DEBUG_STR:         .debug_str
+# DEBUG_STR-NOT:     .debug_str
+
+.section        .rodata.1,"aM", at progbits,4
+        .align  4
+        .long   0x42
+.section         .debug_str,"MS", at progbits,1
+        .asciz  "string"
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -498,7 +498,8 @@
   // 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))
+  if (Config->Relocatable && (IS->Flags & SHF_MERGE) &&
+    isa<SyntheticSection> (IS))
     return createSection(IS, OutsecName);
 
   //  The ELF spec just says


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43942.136504.patch
Type: text/x-patch
Size: 1470 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180301/b24f82b2/attachment.bin>


More information about the llvm-commits mailing list