[PATCH] D55211: [LLD][ELF] - Support discarding the .dynamic section.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 3 05:48:22 PST 2018


grimar updated this revision to Diff 176380.
grimar added a comment.

- Restored, updated the removed test case.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55211/new/

https://reviews.llvm.org/D55211

Files:
  ELF/LinkerScript.cpp
  ELF/Writer.cpp
  test/ELF/linkerscript/discard-section-err.s


Index: test/ELF/linkerscript/discard-section-err.s
===================================================================
--- test/ELF/linkerscript/discard-section-err.s
+++ test/ELF/linkerscript/discard-section-err.s
@@ -7,10 +7,9 @@
 # RUN:   FileCheck -check-prefix=SHSTRTAB %s
 # SHSTRTAB: discarding .shstrtab section is not allowed
 
+## We allow discarding .dynamic, check we don't crash.
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.dynamic) } }" > %t.script
-# RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
-# RUN:   FileCheck -check-prefix=DYNAMIC %s
-# DYNAMIC: discarding .dynamic section is not allowed
+# RUN: ld.lld -pie -o %t --script %t.script %t.o
 
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.dynsym) } }" > %t.script
 # RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1621,7 +1621,7 @@
   // It should be okay as no one seems to care about the type.
   // Even the author of gold doesn't remember why gold behaves that way.
   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
-  if (In.DynSymTab)
+  if (In.Dynamic->Parent)
     Symtab->addDefined("_DYNAMIC", STV_HIDDEN, STT_NOTYPE, 0 /*Value*/,
                        /*Size=*/0, STB_WEAK, In.Dynamic,
                        /*File=*/nullptr);
@@ -1935,9 +1935,8 @@
     Ret.push_back(TlsHdr);
 
   // Add an entry for .dynamic.
-  if (In.DynSymTab)
-    AddHdr(PT_DYNAMIC, In.Dynamic->getParent()->getPhdrFlags())
-        ->add(In.Dynamic->getParent());
+  if (OutputSection *Sec = In.Dynamic->getParent())
+    AddHdr(PT_DYNAMIC, Sec->getPhdrFlags())->add(Sec);
 
   // PT_GNU_RELRO includes all sections that should be marked as
   // read-only by dynamic linker after proccessing relocations.
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -415,9 +415,8 @@
 
 void LinkerScript::discard(ArrayRef<InputSection *> V) {
   for (InputSection *S : V) {
-    if (S == In.ShStrTab || S == In.Dynamic || S == In.DynSymTab ||
-        S == In.DynStrTab || S == In.RelaPlt || S == In.RelaDyn ||
-        S == In.RelrDyn)
+    if (S == In.ShStrTab || S == In.DynSymTab || S == In.DynStrTab ||
+        S == In.RelaPlt || S == In.RelaDyn || S == In.RelrDyn)
       error("discarding " + S->Name + " section is not allowed");
 
     // You can discard .hash and .gnu.hash sections by linker scripts. Since


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55211.176380.patch
Type: text/x-patch
Size: 2535 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181203/c934502f/attachment.bin>


More information about the llvm-commits mailing list