[PATCH] D24750: [GC] Don't crash while processing Discarded sections

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 19 17:13:59 PDT 2016


davide created this revision.
davide added a reviewer: ruiu.
davide added a subscriber: llvm-commits.

When we mark all the reachable sections, we might end up with a InputSection<ELFT>::Discarded section in the queue. This section has no successor, we can't get the object associated, so pretty much every operation we want to run on it it's pointless and cause a crash. Detect this case and bail out early.

https://reviews.llvm.org/D24750

Files:
  ELF/MarkLive.cpp
  test/ELF/comdat.s

Index: test/ELF/comdat.s
===================================================================
--- test/ELF/comdat.s
+++ test/ELF/comdat.s
@@ -5,6 +5,16 @@
 // RUN: llvm-readobj -s -t %t | FileCheck --check-prefix=READ %s
 // REQUIRES: x86
 
+// Check that we don't crash with --gc-section and that we print a list of
+// reclaimed sections on stderr.
+// RUN: ld.lld --gc-sections --print-gc-sections -shared %t.o %t.o %t2.o -o %t \
+// RUN:   2>&1 | FileCheck --check-prefix=GC %s
+// GC: removing unused section from '.text' in file
+// GC: removing unused section from '.text3' in file
+// GC: removing unused section from '.text' in file
+// GC: removing unused section from '.text' in file
+
+
         .section	.text2,"axG", at progbits,foo,comdat,unique,0
 foo:
         nop
Index: ELF/MarkLive.cpp
===================================================================
--- ELF/MarkLive.cpp
+++ ELF/MarkLive.cpp
@@ -81,6 +81,10 @@
 template <class ELFT>
 static void forEachSuccessor(InputSection<ELFT> &Sec,
                              std::function<void(ResolvedReloc<ELFT>)> Fn) {
+  // If this is a discarded section, bail out.
+  if (&Sec == &InputSection<ELFT>::Discarded)
+    return;
+
   ELFFile<ELFT> &Obj = Sec.getFile()->getObj();
   for (const typename ELFT::Shdr *RelSec : Sec.RelocSections) {
     if (RelSec->sh_type == SHT_RELA) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24750.71894.patch
Type: text/x-patch
Size: 1350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160920/a0d0b3b3/attachment.bin>


More information about the llvm-commits mailing list