From 49ab2517f5d7e29b3e01bb73076077c9ce035b20 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Thu, 22 Sep 2016 21:08:51 +0000 Subject: [PATCH] [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 llvm-svn: 282197 --- lld/ELF/MarkLive.cpp | 7 +++++++ lld/test/ELF/comdat.s | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/lld/ELF/MarkLive.cpp b/lld/ELF/MarkLive.cpp index 4965377..6628973 100644 --- a/lld/ELF/MarkLive.cpp +++ b/lld/ELF/MarkLive.cpp @@ -81,6 +81,13 @@ static ResolvedReloc resolveReloc(InputSectionBase &Sec, 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) { diff --git a/lld/test/ELF/comdat.s b/lld/test/ELF/comdat.s index d422ee8..5b190b1 100644 --- a/lld/test/ELF/comdat.s +++ b/lld/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 -- 2.7.4