[lld] r282197 - [ELF/GC] Don't crash while processing Discarded sections.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 14:08:51 PDT 2016


Author: davide
Date: Thu Sep 22 16:08:51 2016
New Revision: 282197

URL: http://llvm.org/viewvc/llvm-project?rev=282197&view=rev
Log:
[ELF/GC] Don't crash while processing Discarded sections.

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

Modified:
    lld/trunk/ELF/MarkLive.cpp
    lld/trunk/test/ELF/comdat.s

Modified: lld/trunk/ELF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MarkLive.cpp?rev=282197&r1=282196&r2=282197&view=diff
==============================================================================
--- lld/trunk/ELF/MarkLive.cpp (original)
+++ lld/trunk/ELF/MarkLive.cpp Thu Sep 22 16:08:51 2016
@@ -81,6 +81,13 @@ static ResolvedReloc<ELFT> resolveReloc(
 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) {

Modified: lld/trunk/test/ELF/comdat.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/comdat.s?rev=282197&r1=282196&r2=282197&view=diff
==============================================================================
--- lld/trunk/test/ELF/comdat.s (original)
+++ lld/trunk/test/ELF/comdat.s Thu Sep 22 16:08:51 2016
@@ -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", at progbits,foo,comdat,unique,0
 foo:
         nop




More information about the llvm-commits mailing list