summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Rimar <grimar@accesssoftek.com>2018-12-10 09:24:49 +0000
committerGeorge Rimar <grimar@accesssoftek.com>2018-12-10 09:24:49 +0000
commitd37aa05d4433830d4f1b22b5a600c5bc04e0c1ea (patch)
treea6feb8ecd215682b57fbb35dceb95db95a3a4b57
parentb827cb61df9f60bda803337106f78ef1909a8c5c (diff)
[LLD][ELF] - Support discarding the .dynamic section.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810. Seems it turns out that supporting /DISCARD/ for the .dynamic section with the linker script is something we can do easily. The patch does this. Differential revision: https://reviews.llvm.org/D55211
-rw-r--r--lld/ELF/LinkerScript.cpp3
-rw-r--r--lld/ELF/Writer.cpp7
-rw-r--r--lld/test/ELF/linkerscript/discard-section-err.s5
3 files changed, 6 insertions, 9 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index e37b4a38e8d..fbc02541620 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -415,8 +415,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
void LinkerScript::discard(ArrayRef<InputSection *> V) {
for (InputSection *S : V) {
- if (S == In.ShStrTab || S == In.Dynamic || S == In.RelaDyn ||
- S == In.RelrDyn)
+ if (S == In.ShStrTab || 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
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 64f4f655257..5eed0184ecd 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1621,7 +1621,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// 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 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
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.
diff --git a/lld/test/ELF/linkerscript/discard-section-err.s b/lld/test/ELF/linkerscript/discard-section-err.s
index 82105b514a1..bb77dbb087d 100644
--- a/lld/test/ELF/linkerscript/discard-section-err.s
+++ b/lld/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
## We allow discarding .dynsym, check we don't crash.
# RUN: echo "SECTIONS { /DISCARD/ : { *(.dynsym) } }" > %t.script