[lld] r319526 - [ELF] - Produce relocation section name consistent with output section name when --emit-reloc used with linker script.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 01:04:52 PST 2017


Author: grimar
Date: Fri Dec  1 01:04:52 2017
New Revision: 319526

URL: http://llvm.org/viewvc/llvm-project?rev=319526&view=rev
Log:
[ELF] - Produce relocation section name consistent with output section name when --emit-reloc used with linker script.

This is for "Bug 35474 - --emit-relocs produces wrongly-named reloc sections".

LLD currently for scripts like:

.text.boot : { *(.text.boot) }
emits relocation section with name .rela.text because does not take
redefined name of output section into account and builds section name
using rules for non-scripted case. Patch fixes this oddness.

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

Added:
    lld/trunk/test/ELF/linkerscript/emit-reloc-section-names.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/Writer.cpp
    lld/trunk/ELF/Writer.h
    lld/trunk/test/ELF/linkerscript/emit-relocs-multiple.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=319526&r1=319525&r2=319526&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Dec  1 01:04:52 2017
@@ -533,7 +533,7 @@ void LinkerScript::addOrphanSections() {
     if (!S->Live || S->Parent)
       continue;
 
-    StringRef Name = getOutputSectionName(S->Name);
+    StringRef Name = getOutputSectionName(S);
 
     if (Config->OrphanHandling == OrphanHandlingPolicy::Error)
       error(toString(S) + " is being placed in '" + Name + "'");

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=319526&r1=319525&r2=319526&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Fri Dec  1 01:04:52 2017
@@ -2517,7 +2517,7 @@ void elf::mergeSections() {
     if (!MS->Live)
       continue;
 
-    StringRef OutsecName = getOutputSectionName(MS->Name);
+    StringRef OutsecName = getOutputSectionName(MS);
     uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize);
 
     auto I = llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) {

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=319526&r1=319525&r2=319526&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Fri Dec  1 01:04:52 2017
@@ -87,7 +87,9 @@ private:
 };
 } // anonymous namespace
 
-StringRef elf::getOutputSectionName(StringRef Name) {
+StringRef elf::getOutputSectionName(InputSectionBase *S) {
+  StringRef Name = S->Name;
+
   // ".zdebug_" is a prefix for ZLIB-compressed sections.
   // Because we decompressed input sections, we want to remove 'z'.
   if (Name.startswith(".zdebug_"))
@@ -96,13 +98,17 @@ StringRef elf::getOutputSectionName(Stri
   if (Config->Relocatable)
     return Name;
 
-  // This is for --emit-relocs. If .text.foo is emitted as .text, we want to
-  // emit .rela.text.foo as .rel.text for consistency (this is not technically
-  // required, but not doing it is odd). This code guarantees that.
-  if (Name.startswith(".rel."))
-    return Saver.save(".rel" + getOutputSectionName(Name.substr(4)));
-  if (Name.startswith(".rela."))
-    return Saver.save(".rela" + getOutputSectionName(Name.substr(5)));
+  // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
+  // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
+  // technically required, but not doing it is odd). This code guarantees that.
+  if ((S->Type == SHT_REL || S->Type == SHT_RELA) &&
+      !isa<SyntheticSection>(S)) {
+    OutputSection *Out =
+        cast<InputSection>(S)->getRelocatedSection()->getOutputSection();
+    if (S->Type == SHT_RELA)
+      return Saver.save(".rela" + Out->Name);
+    return Saver.save(".rel" + Out->Name);
+  }
 
   for (StringRef V :
        {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",

Modified: lld/trunk/ELF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.h?rev=319526&r1=319525&r2=319526&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.h (original)
+++ lld/trunk/ELF/Writer.h Fri Dec  1 01:04:52 2017
@@ -46,7 +46,7 @@ struct PhdrEntry {
   bool HasLMA = false;
 };
 
-llvm::StringRef getOutputSectionName(llvm::StringRef Name);
+llvm::StringRef getOutputSectionName(InputSectionBase *S);
 
 template <class ELFT> uint32_t calcMipsEFlags();
 

Added: lld/trunk/test/ELF/linkerscript/emit-reloc-section-names.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/emit-reloc-section-names.s?rev=319526&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/emit-reloc-section-names.s (added)
+++ lld/trunk/test/ELF/linkerscript/emit-reloc-section-names.s Fri Dec  1 01:04:52 2017
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .text.zed : { *(.text.foo) } \
+# RUN:                  .text.qux : { *(.text.bar) } }" > %t.script
+# RUN: ld.lld -T %t.script --emit-relocs %t.o -o %t
+# RUN: llvm-objdump -section-headers %t | FileCheck %s
+
+## Check we name relocation sections in according to
+## their target sections names.
+
+# CHECK: .text.zed
+# CHECK: .text.qux
+# CHECK: .rela.text.zed
+# CHECK: .rela.text.qux
+
+.section .text.foo,"ax"
+foo:
+ mov $bar, %rax
+
+.section .text.bar,"ax"
+bar:
+ mov $foo, %rax

Modified: lld/trunk/test/ELF/linkerscript/emit-relocs-multiple.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/emit-relocs-multiple.s?rev=319526&r1=319525&r2=319526&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/emit-relocs-multiple.s (original)
+++ lld/trunk/test/ELF/linkerscript/emit-relocs-multiple.s Fri Dec  1 01:04:52 2017
@@ -5,7 +5,7 @@
 # RUN: llvm-readobj -r %t1 | FileCheck %s
 
 # CHECK:      Relocations [
-# CHECK-NEXT:   Section {{.*}} .rela.foo {
+# CHECK-NEXT:   Section {{.*}} .rela.zed {
 # CHECK-NEXT:     0x1 R_X86_64_32 .zed 0x0
 # CHECK-NEXT:     0x6 R_X86_64_32 .zed 0x5
 # CHECK-NEXT:   }




More information about the llvm-commits mailing list