Skip to content

Commit c20717e

Browse files
committed
[lld][ELF] Avoid folding sections with unmergeable symbols
1 parent faa11ed commit c20717e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lld/ELF/ICF.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, Relocs<RelTy> ra,
287287
// Relocations referring to InputSections are constant-equal if their
288288
// section offsets are equal.
289289
if (isa<InputSection>(da->section)) {
290-
if (da->value + addA == db->value + addB)
290+
// Our symbol folding logic later merges symbols in two folded sections
291+
// We should not merge sections in the first place if their symbols
292+
// cannot be merged together.
293+
bool canMergeSymbols = addA == addB;
294+
if (da->value + addA == db->value + addB && canMergeSymbols)
291295
continue;
292296
return false;
293297
}

lld/test/ELF/icf-addend.s

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
3+
# RUN: ld.lld %t.o -o /dev/null --icf=all --print-icf-sections | FileCheck %s
4+
5+
# Check that ICF doesn't fold different symbols when functions referencing them
6+
# can be folded because they are pointing to the same address.
7+
8+
# CHECK: selected section {{.*}}:(.text.f1)
9+
# CHECK: removing identical section {{.*}}:(.text.f2)
10+
# CHECK-NOT: redirecting 'y' in symtab to 'x'
11+
# CHECK-NOT: redirecting 'y' to 'x'
12+
13+
.globl x, y
14+
15+
.section .rodata,"a",@progbits
16+
x:
17+
.long 11
18+
y:
19+
.long 12
20+
21+
.section .text.f1,"ax",@progbits
22+
f1:
23+
movq x+4(%rip), %rax
24+
25+
.section .text.f2,"ax",@progbits
26+
f2:
27+
movq y(%rip), %rax
28+
29+
.section .text._start,"ax",@progbits
30+
.globl _start
31+
_start:
32+
call f1
33+
call f2

0 commit comments

Comments
 (0)