Skip to content

Commit

Permalink
[ELF] Support discarding .relr.dyn
Browse files Browse the repository at this point in the history
to prepare for D116838, otherwise for linkerscript/discard-section-err.s,
there will be a null pointer dereference in `part.relrDyn->getParent()->size`
in `finalizeSynthetic(part.relrDyn.get())`.
  • Loading branch information
MaskRay committed Jan 12, 2022
1 parent 6e77ad1 commit db08df0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -561,16 +561,18 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
}

void LinkerScript::discard(InputSectionBase &s) {
if (&s == in.shStrTab.get() || &s == mainPart->relrDyn.get())
if (&s == in.shStrTab.get())
error("discarding " + s.name + " section is not allowed");

// 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.
// You can discard .hash, .gnu.hash, and .relr.dyn 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;
if (&s == mainPart->hashTab)
else if (&s == mainPart->hashTab)
mainPart->hashTab = nullptr;
else if (&s == mainPart->relrDyn.get())
mainPart->relrDyn.reset();

s.markDead();
s.parent = nullptr;
Expand Down
5 changes: 2 additions & 3 deletions lld/test/ELF/linkerscript/discard-section-err.s
Expand Up @@ -27,9 +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: not ld.lld -pie --pack-dyn-relocs=relr -o /dev/null --script %t.script %t.o 2>&1 | \
# RUN: FileCheck -check-prefix=RELRDYN %s
# RELRDYN: discarding .relr.dyn section is not allowed
# RUN: ld.lld -pie --pack-dyn-relocs=relr -T %t.script %t.o -o %t
# RUN: llvm-readobj -S %t | FileCheck /dev/null --implicit-check-not='Name: .relr.dyn'

.data
.align 8
Expand Down

0 comments on commit db08df0

Please sign in to comment.