[lld] db08df0 - [ELF] Support discarding .relr.dyn

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 12 10:39:04 PST 2022


Author: Fangrui Song
Date: 2022-01-12T10:38:59-08:00
New Revision: db08df0570b6dfaf00d7b1b8555c1d2d4effb224

URL: https://github.com/llvm/llvm-project/commit/db08df0570b6dfaf00d7b1b8555c1d2d4effb224
DIFF: https://github.com/llvm/llvm-project/commit/db08df0570b6dfaf00d7b1b8555c1d2d4effb224.diff

LOG: [ELF] Support discarding .relr.dyn

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())`.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 22c3a5c0c91ea..8870db771fa7f 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -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 
diff erently 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 
diff erently
+  // 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;

diff  --git a/lld/test/ELF/linkerscript/discard-section-err.s b/lld/test/ELF/linkerscript/discard-section-err.s
index 155d34f884ea3..44527074396f2 100644
--- a/lld/test/ELF/linkerscript/discard-section-err.s
+++ b/lld/test/ELF/linkerscript/discard-section-err.s
@@ -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


        


More information about the llvm-commits mailing list