[ELF/GC] Don't crash while processing Discarded sections.
authorDavide Italiano <davide@freebsd.org>
Thu, 22 Sep 2016 21:08:51 +0000 (21:08 +0000)
committerDavide Italiano <davide@freebsd.org>
Thu, 22 Sep 2016 21:08:51 +0000 (21:08 +0000)
The ELF spec doesn't allow relocations to point directly to
a deduplicated COMDAT section but this unfortunately happens in
practice. Bail out early instead of crashing.

Differential Revision:  https://reviews.llvm.org/D24750

llvm-svn: 282197

lld/ELF/MarkLive.cpp
lld/test/ELF/comdat.s

index 4965377..6628973 100644 (file)
@@ -81,6 +81,13 @@ static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
 template <class ELFT>
 static void forEachSuccessor(InputSection<ELFT> &Sec,
                              std::function<void(ResolvedReloc<ELFT>)> Fn) {
+  // Skip over discarded sections. This in theory shouldn't happen, because
+  // the ELF spec doesn't allow a relocation to point to a deduplicated
+  // COMDAT section directly. Unfortunately this happens in practice (e.g.
+  // .eh_frame) so we need to add a check.
+  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) {
index d422ee8..5b190b1 100644 (file)
@@ -5,6 +5,15 @@
 // 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",@progbits,foo,comdat,unique,0
 foo:
         nop