[ELF] Support discarding .relr.dyn
authorFangrui Song <i@maskray.me>
Wed, 12 Jan 2022 18:38:59 +0000 (10:38 -0800)
committerFangrui Song <i@maskray.me>
Wed, 12 Jan 2022 18:38:59 +0000 (10:38 -0800)
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())`.

lld/ELF/LinkerScript.cpp
lld/test/ELF/linkerscript/discard-section-err.s

index 22c3a5c..8870db7 100644 (file)
@@ -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;
index 155d34f..4452707 100644 (file)
@@ -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