Skip to content

Commit 49ab251

Browse files
committed
[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
1 parent 7176862 commit 49ab251

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lld/ELF/MarkLive.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
8181
template <class ELFT>
8282
static void forEachSuccessor(InputSection<ELFT> &Sec,
8383
std::function<void(ResolvedReloc<ELFT>)> Fn) {
84+
// Skip over discarded sections. This in theory shouldn't happen, because
85+
// the ELF spec doesn't allow a relocation to point to a deduplicated
86+
// COMDAT section directly. Unfortunately this happens in practice (e.g.
87+
// .eh_frame) so we need to add a check.
88+
if (&Sec == &InputSection<ELFT>::Discarded)
89+
return;
90+
8491
ELFFile<ELFT> &Obj = Sec.getFile()->getObj();
8592
for (const typename ELFT::Shdr *RelSec : Sec.RelocSections) {
8693
if (RelSec->sh_type == SHT_RELA) {

lld/test/ELF/comdat.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
// RUN: llvm-readobj -s -t %t | FileCheck --check-prefix=READ %s
66
// REQUIRES: x86
77

8+
// Check that we don't crash with --gc-section and that we print a list of
9+
// reclaimed sections on stderr.
10+
// RUN: ld.lld --gc-sections --print-gc-sections -shared %t.o %t.o %t2.o -o %t \
11+
// RUN: 2>&1 | FileCheck --check-prefix=GC %s
12+
// GC: removing unused section from '.text' in file
13+
// GC: removing unused section from '.text3' in file
14+
// GC: removing unused section from '.text' in file
15+
// GC: removing unused section from '.text' in file
16+
817
.section .text2,"axG",@progbits,foo,comdat,unique,0
918
foo:
1019
nop

0 commit comments

Comments
 (0)