[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
Mon Mar 5 09:49:13 PST 2018


gbreynoo updated this revision to Diff 137029.
gbreynoo added a comment.

In response to Rafael on the mailing list:

You are correct that the output looks to be valid, however as DWARF consumers are expecting one ".debug_str" section they can function incorrectly. For example llvm-dwarfdump appears to use any ".debug_str" offsets with the first ".debug_str" section it finds.

A test case with llvm-dwarfdump, the name attributes in one CU will be incorrect:

main.cpp:

  int other_value();
  
  int value (){
      return other_value();
  }
  int main (){
      return value ();
  }

other.cpp:

  int other_value(){
      return 0;
  }

The DWARF Specification appears to implicitly say there is only one ".debug_str" section, meaning the current lld output is not valid.

Also I have updated the diff so this change in behaviour only effects ".debug_str".


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:         .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,9 @@
   // 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) &&
+      !((IS->Flags & SHF_STRINGS) &&
+        (IS->Name.startswith(".debug") || IS->Name.startswith(".zdebug"))))
     return createSection(IS, OutsecName);
 
   //  The ELF spec just says


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43942.137029.patch
Type: text/x-patch
Size: 1553 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180305/959c2ab6/attachment.bin>


More information about the llvm-commits mailing list