[PATCH] D30033: [ELF] - Do not crash when discarding sections that are referenced by others.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 02:10:54 PST 2017


grimar created this revision.

SHF_LINK_ORDER sections adds special ordering requirements.
Such sections references other sections. Previously we would crash
if section that other were referenced to was discarded by script.

Patch fixes that by discarding all dependent sections in that case.
It supports chained dependencies, testcase is provided.


https://reviews.llvm.org/D30033

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/discard-section-metadata.s


Index: test/ELF/linkerscript/discard-section-metadata.s
===================================================================
--- test/ELF/linkerscript/discard-section-metadata.s
+++ test/ELF/linkerscript/discard-section-metadata.s
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo "SECTIONS { /DISCARD/ : { *(.foo) } }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
+
+# CHECK-NOT: .foo
+# CHECK-NOT: .bar
+# CHECK-NOT: .zed
+# CHECK-NOT: .moo
+
+## Sections dependency tree for testcase is:
+## (.foo)
+##   | |
+##   | --(.bar)
+##   |
+##   --(.zed)
+##       |
+##       --(.moo)
+##
+
+.section .foo,"a"
+.quad 0
+
+.section .bar,"am", at progbits,.foo
+.quad 0
+
+.section .zed,"am", at progbits,.foo
+.quad 0
+
+.section .moo,"am", at progbits,.zed
+.quad 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -272,6 +272,11 @@
   for (InputSectionBase<ELFT> *S : V) {
     S->Live = false;
     reportDiscarded(S);
+
+    InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S);
+    if (!IS || IS->DependentSections.empty())
+      continue;
+    discard(IS->DependentSections);
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30033.88702.patch
Type: text/x-patch
Size: 1318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170216/62a6f1e6/attachment.bin>


More information about the llvm-commits mailing list