[ELF] Support discarding .relr.dyn

db08df0570 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.
This commit is contained in:
Fangrui Song 2022-01-12 11:55:22 -08:00
parent f6fab68c30
commit bf9c8636f2
3 changed files with 5 additions and 4 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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