Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[lld][ELF] Merge equivalent symbols found during ICF #139493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for to prevent local-global merging
  • Loading branch information
pranavk committed May 20, 2025
commit 6c41c380415015809f8abb9befc9609b8579cc12
7 changes: 4 additions & 3 deletions 7 lld/ELF/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,11 @@ template <class ELFT> void ICF<ELFT>::run() {
getRelocTargetSyms<ELFT>(sections[i]);
assert(syms.size() == replacedSyms.size() &&
"Should have same number of syms!");
for (size_t i = 0; i < syms.size(); i++) {
if (!canMergeSymbols(syms[i].second, replacedSyms[i].second))
for (size_t j = 0; j < syms.size(); j++) {
if (!syms[j].first->isGlobal() || !replacedSyms[j].first->isGlobal() ||
!canMergeSymbols(syms[j].second, replacedSyms[j].second))
continue;
symbolEquivalence.unionSets(syms[i].first, replacedSyms[i].first);
symbolEquivalence.unionSets(syms[j].first, replacedSyms[j].first);
}

// At this point we know sections merged are fully identical and hence
Expand Down
79 changes: 58 additions & 21 deletions 79 lld/test/ELF/icf-addend.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,50 @@
# RUN: rm -rf %t && split-file %s %t && cd %t

#--- trivial-relocation.s
# For trivial relocations, merging two equivalent sections is allowed but we must not
# merge their symbols if addends are different.
# Tests following for trivial relocations:
# 1. Merging two equivalent sections is allowed but we must not merge their symbols if addends are different.
# 2. Local symbols should not be merged together even though their sections can be merged together.

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux trivial-relocation.s -o trivial.o
# RUN: ld.lld trivial.o -o /dev/null --icf=all --print-icf-sections | FileCheck %s
# RUN: ld.lld trivial.o -o /dev/null --icf=all --print-icf-sections | FileCheck %s --check-prefix=TRIVIAL

# CHECK: selected section {{.*}}:(.text.f1)
# CHECK: removing identical section {{.*}}:(.text.f2)
# CHECK-NOT: redirecting 'y' in symtab to x
# CHECK-NOT: redirecting 'y' to 'x'
# TRIVIAL: selected section {{.*}}:(.rodata.sec1)
# TRIVIAL-NEXT: removing identical section {{.*}}:(.rodata.sec2)
# TRIVIAL-NEXT: selected section {{.*}}:(.text.f1)
# TRIVIAL-NEXT: removing identical section {{.*}}:(.text.f2)
# TRIVIAL-NEXT: removing identical section {{.*}}:(.text.f1_local)
# TRIVIAL-NEXT: removing identical section {{.*}}:(.text.f2_local)

.globl x, y
.addrsig

.section .rodata,"a",@progbits
.globl x_glob, y_glob

.section .rodata.sec1,"a",@progbits
x_glob:
.long 11
y_glob:
.long 12

.section .rodata.sec2,"a",@progbits
x:
.long 11
y:
.long 12

.section .text.f1,"ax",@progbits
f1:
movq x+4(%rip), %rax
movq x_glob+4(%rip), %rax

.section .text.f2,"ax",@progbits
f2:
movq y_glob(%rip), %rax

.section .text.f1_local,"ax",@progbits
f1_local:
movq x+4(%rip), %rax

.section .text.f2_local,"ax",@progbits
f2_local:
movq y(%rip), %rax

.section .text._start,"ax",@progbits
Expand All @@ -36,34 +55,52 @@ call f1
call f2

#--- non-trivial-relocation.s
# For non-trivial relocations, we must not merge sections if addends are different.
# Not merging sections would automatically disable symbol merging.
# Tests following for non-trivial relocations:
# 1. We must not merge sections if addends are different.
# 2. We must not merge sections pointing to local and global symbols.

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux trivial-relocation.s -o trivial.o
# RUN: ld.lld trivial.o -o /dev/null --icf=all --print-icf-sections | FileCheck %s
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux non-trivial-relocation.s -o non-trivial.o
# RUN: ld.lld non-trivial.o -o /dev/null --icf=all --print-icf-sections | FileCheck %s --check-prefix=NONTRIVIAL

# NONTRIVIAL: selected section {{.*}}:(.rodata.sec1)
# NONTRIVIAL-NEXT: removing identical section {{.*}}:(.rodata.sec2)
# NONTRIVIAL-NEXT: selected section {{.*}}:(.text.f1_local)
# NONTRIVIAL-NEXT: removing identical section {{.*}}:(.text.f2_local)

# CHECK-NOT: selected section {{.*}}:(.text.f1)
# CHECK-NOT: removing identical section {{.*}}:(.text.f2)
.addrsig

.globl x, y
.globl x_glob, y_glob

.section .rodata,"a",@progbits
.section .rodata.sec1,"a",@progbits
x_glob:
.long 11
y_glob:
.long 12

.section .rodata.sec2,"a",@progbits
x:
.long 11
y:
.long 12

.section .text.f1,"ax",@progbits
f1:
movq x+4@GOTPCREL(%rip), %rax
movq x_glob+4@GOTPCREL(%rip), %rax

.section .text.f2,"ax",@progbits
f2:
movq y@GOTPCREL(%rip), %rax
movq y_glob@GOTPCREL(%rip), %rax

.section .text.f1_local,"ax",@progbits
f1_local:
movq x+4(%rip), %rax

.section .text.f2_local,"ax",@progbits
f2_local:
movq y(%rip), %rax

.section .text._start,"ax",@progbits
.globl _start
_start:
call f1
call f2

Morty Proxy This is a proxified and sanitized view of the page, visit original site.