Skip to content

Commit e4041b7

Browse files
committed
[lld][ICF] Don't merge symbols with different addends
1 parent faa11ed commit e4041b7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 --allow-empty %s
4+
5+
# Check that ICF doesn't fold sections containing functions that references
6+
# unmergeable symbols. We should only merge symbols of two relocations when
7+
# their addends are same.
8+
9+
# CHECK-NOT: selected section {{.*}}:(.text.f1)
10+
# CHECK-NOT: removing identical section {{.*}}:(.text.f2)
11+
12+
.globl x, y
13+
14+
.section .rodata,"a",@progbits
15+
x:
16+
.long 11
17+
y:
18+
.long 12
19+
20+
.section .text.f1,"ax",@progbits
21+
f1:
22+
movq x+4(%rip), %rax
23+
24+
.section .text.f2,"ax",@progbits
25+
f2:
26+
movq y(%rip), %rax
27+
28+
.section .text._start,"ax",@progbits
29+
.globl _start
30+
_start:
31+
call f1
32+
call f2

0 commit comments

Comments
 (0)