summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2022-01-12 11:55:22 -0800
committerFangrui Song <i@maskray.me>2022-01-12 11:55:22 -0800
commitbf9c8636f2cd1c5e6338402b67de06f9ce74cdd9 (patch)
tree3b437bf83bfb318bf4122beff8c23ec64ae77983
parentf6fab68c30622b7c97b0d21b5ea02809210444eb (diff)
[ELF] Support discarding .relr.dyn
db08df0570b6dfaf00d7b1b8555c1d2d4effb224 does not work because part.relrDyn is a unique_ptr and `reset` destroys the object which may still be referenced. This commit uses the D114180 approach. Also improve the test to check that there is no R_X86_64_RELATIVE.
-rw-r--r--lld/ELF/LinkerScript.cpp4
-rw-r--r--lld/ELF/SyntheticSections.cpp3
-rw-r--r--lld/test/ELF/linkerscript/discard-section-err.s2
3 files changed, 5 insertions, 4 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 8870db771fa7..00a5492031bf 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -564,15 +564,13 @@ void LinkerScript::discard(InputSectionBase &s) {
if (&s == in.shStrTab.get())
error("discarding " + s.name + " section is not allowed");
- // You can discard .hash, .gnu.hash, and .relr.dyn sections by linker scripts.
+ // You can discard .hash and .gnu.hash sections by linker scripts.
// Since they are synthesized sections, we need to handle them differently
// than other regular sections.
if (&s == mainPart->gnuHashTab)
mainPart->gnuHashTab = nullptr;
else if (&s == mainPart->hashTab)
mainPart->hashTab = nullptr;
- else if (&s == mainPart->relrDyn.get())
- mainPart->relrDyn.reset();
s.markDead();
s.parent = nullptr;
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index d0f3c87ac8c7..680281f3315e 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1412,7 +1412,8 @@ DynamicSection<ELFT>::computeContents() {
addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels);
}
}
- if (part.relrDyn && !part.relrDyn->relocs.empty()) {
+ if (part.relrDyn && part.relrDyn->getParent() &&
+ !part.relrDyn->relocs.empty()) {
addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
*part.relrDyn);
addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
diff --git a/lld/test/ELF/linkerscript/discard-section-err.s b/lld/test/ELF/linkerscript/discard-section-err.s
index c186ba00af41..592c33fdd5cc 100644
--- a/lld/test/ELF/linkerscript/discard-section-err.s
+++ b/lld/test/ELF/linkerscript/discard-section-err.s
@@ -27,6 +27,8 @@
# RUN: llvm-readobj -S %t | FileCheck /dev/null --implicit-check-not='Name: .rela.dyn'
# RUN: echo "SECTIONS { /DISCARD/ : { *(.relr.dyn) } }" > %t.script
+# RUN: ld.lld -pie --pack-dyn-relocs=relr -T %t.script %t.o -o %t
+# RUN: llvm-readobj -S -r %t | FileCheck /dev/null --implicit-check-not='Name: .relr.dyn' --implicit-check-not=R_X86_64_RELATIVE
.data
.align 8