Index: lld/trunk/ELF/MarkLive.cpp =================================================================== --- lld/trunk/ELF/MarkLive.cpp +++ lld/trunk/ELF/MarkLive.cpp @@ -81,6 +81,13 @@ template static void forEachSuccessor(InputSection &Sec, std::function)> 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::Discarded) + return; + ELFFile &Obj = Sec.getFile()->getObj(); for (const typename ELFT::Shdr *RelSec : Sec.RelocSections) { if (RelSec->sh_type == SHT_RELA) { Index: lld/trunk/test/ELF/comdat.s =================================================================== --- lld/trunk/test/ELF/comdat.s +++ lld/trunk/test/ELF/comdat.s @@ -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