[lld] r328419 - [ELF] - Do not ignore discarding of .rela.plt/.rela.dyn, allow doing custom layout for them.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 24 06:10:19 PDT 2018


Author: grimar
Date: Sat Mar 24 06:10:19 2018
New Revision: 328419

URL: http://llvm.org/viewvc/llvm-project?rev=328419&view=rev
Log:
[ELF] - Do not ignore discarding of .rela.plt/.rela.dyn, allow doing custom layout for them.

Currently when we build input sections list in linker script
we ignore all rel[a] sections. That was done to support
scripts like .rela.dyn : { *(.rela.data) } for emit relocs.

Though as a result following scripts were also silently ignored:

/DISCARD/ : { *(.rela.plt)
/DISCARD/ : { *(.rela.dyn)

and we produced output with this sections. That is not ideal.
The solution this patch suggests is simple: do not ignore synthetic
rel[a] sections. That way we can enable common discarding logic
for them and report a proper error.

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

Added:
    lld/trunk/test/ELF/linkerscript/synthetic-relsec-layout.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/discard-section-err.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=328419&r1=328418&r2=328419&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sat Mar 24 06:10:19 2018
@@ -380,7 +380,10 @@ LinkerScript::computeInputSections(const
       // For -emit-relocs we have to ignore entries like
       //   .rela.dyn : { *(.rela.data) }
       // which are common because they are in the default bfd script.
-      if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
+      // We do not ignore SHT_REL[A] linker-synthesized sections here because
+      // want to support scripts that do custom layout for them.
+      if (!isa<SyntheticSection>(Sec) &&
+          (Sec->Type == SHT_REL || Sec->Type == SHT_RELA))
         continue;
 
       std::string Filename = getFilename(Sec->File);
@@ -405,7 +408,7 @@ LinkerScript::computeInputSections(const
 void LinkerScript::discard(ArrayRef<InputSection *> V) {
   for (InputSection *S : V) {
     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
-        S == InX::DynStrTab)
+        S == InX::DynStrTab || S == InX::RelaPlt || S == InX::RelaDyn)
       error("discarding " + S->Name + " section is not allowed");
 
     // You can discard .hash and .gnu.hash sections by linker scripts. Since

Modified: lld/trunk/test/ELF/linkerscript/discard-section-err.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/discard-section-err.s?rev=328419&r1=328418&r2=328419&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/discard-section-err.s (original)
+++ lld/trunk/test/ELF/linkerscript/discard-section-err.s Sat Mar 24 06:10:19 2018
@@ -22,4 +22,14 @@
 # RUN:   FileCheck -check-prefix=DYNSTR %s
 # DYNSTR: discarding .dynstr section is not allowed
 
+# RUN: echo "SECTIONS { /DISCARD/ : { *(.rela.plt) } }" > %t.script
+# RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
+# RUN:   FileCheck -check-prefix=RELAPLT %s
+# RELAPLT: discarding .rela.plt section is not allowed
+
+# RUN: echo "SECTIONS { /DISCARD/ : { *(.rela.dyn) } }" > %t.script
+# RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
+# RUN:   FileCheck -check-prefix=RELADYN %s
+# RELADYN: discarding .rela.dyn section is not allowed
+
 .comm foo,4,4

Added: lld/trunk/test/ELF/linkerscript/synthetic-relsec-layout.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/synthetic-relsec-layout.s?rev=328419&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/synthetic-relsec-layout.s (added)
+++ lld/trunk/test/ELF/linkerscript/synthetic-relsec-layout.s Sat Mar 24 06:10:19 2018
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .foo : { *(.rela.dyn) } }" > %t.script
+# RUN: ld.lld -T %t.script %t.o -o %t.so -shared
+# RUN: llvm-readobj -r %t.so | FileCheck %s
+
+# Check we are able to do custom layout for synthetic sections.
+# (here we check we can place synthetic .rela.dyn into .foo).
+
+# CHECK: Relocations [
+# CHECK:   Section ({{.*}}) .foo {
+# CHECK:     R_X86_64_64 .foo 0x0
+# CHECK:   }
+
+.data
+.quad .foo




More information about the llvm-commits mailing list