[PATCH] D55218: [ELF] - Allow discarding .dynsym from the linker script.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 3 07:11:38 PST 2018


grimar created this revision.
grimar added reviewers: ruiu, psmith.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.

This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.
The patch allows discarding the `.dynsym` section using linker script.


https://reviews.llvm.org/D55218

Files:
  ELF/LinkerScript.cpp
  ELF/SyntheticSections.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
@@ -12,10 +12,9 @@
 # RUN:   FileCheck -check-prefix=DYNAMIC %s
 # DYNAMIC: discarding .dynamic section is not allowed
 
+## We allow discarding .dynsym, check we don't crash.
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.dynsym) } }" > %t.script
-# RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
-# RUN:   FileCheck -check-prefix=DYNSYM %s
-# DYNSYM: discarding .dynsym section is not allowed
+# RUN: ld.lld -pie -o %t --script %t.script %t.o
 
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.dynstr) } }" > %t.script
 # RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | \
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1506,7 +1506,10 @@
   // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
   // case.
   InputSection *SymTab = Config->Relocatable ? In.SymTab : In.DynSymTab;
-  getParent()->Link = SymTab ? SymTab->getParent()->SectionIndex : 0;
+  if (SymTab && SymTab->getParent())
+    getParent()->Link = SymTab->getParent()->SectionIndex;
+  else
+    getParent()->Link = 0;
 
   if (In.RelaIplt == this || In.RelaPlt == this)
     getParent()->Info = In.GotPlt->getParent()->SectionIndex;
@@ -2123,7 +2126,8 @@
 }
 
 void GnuHashTableSection::finalizeContents() {
-  getParent()->Link = In.DynSymTab->getParent()->SectionIndex;
+  if (OutputSection *Sec = In.DynSymTab->getParent())
+    getParent()->Link = Sec->SectionIndex;
 
   // Computes bloom filter size in word size. We want to allocate 12
   // bits for each symbol. It must be a power of two.
@@ -2253,7 +2257,8 @@
 }
 
 void HashTableSection::finalizeContents() {
-  getParent()->Link = In.DynSymTab->getParent()->SectionIndex;
+  if (OutputSection *Sec = In.DynSymTab->getParent())
+    getParent()->Link = Sec->SectionIndex;
 
   unsigned NumEntries = 2;                       // nbucket and nchain.
   NumEntries += In.DynSymTab->getNumSymbols();   // The chain entries.
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.Dynamic || 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: D55218.176398.patch
Type: text/x-patch
Size: 2916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181203/1493dc3f/attachment.bin>


More information about the llvm-commits mailing list