From bf9c8636f2cd1c5e6338402b67de06f9ce74cdd9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 12 Jan 2022 11:55:22 -0800 Subject: [PATCH] [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. --- lld/ELF/LinkerScript.cpp | 4 +--- lld/ELF/SyntheticSections.cpp | 3 ++- lld/test/ELF/linkerscript/discard-section-err.s | 2 ++ 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::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