-
Notifications
You must be signed in to change notification settings - Fork 12.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELF] Fix unnecessary inclusion of unreferenced provide symbols
Previously, linker was unnecessarily including a PROVIDE symbol which was referenced by another unused PROVIDE symbol. For example, if a linker script contained the below code and 'not_used_sym' provide symbol is not included, then linker was still unnecessarily including 'foo' PROVIDE symbol because it was referenced by 'not_used_sym'. This commit fixes this behavior. PROVIDE(not_used_sym = foo) PROVIDE(foo = 0x1000) This commit fixes this behavior by using dfs-like algorithm to find all the symbols referenced in provide expressions of included provide symbols. This commit also fixes the issue of unused section not being garbage-collected if a symbol of the section is referenced by an unused PROVIDE symbol. Closes #74771 Closes #84730 Co-authored-by: Fangrui Song <[email protected]>
- Loading branch information
Showing
8 changed files
with
188 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# REQUIRES: x86 | ||
|
||
# This test verifies that garbage-collection is correctly garbage collecting | ||
# unused sections when the symbol of the unused section is only referred by | ||
# an unused PROVIDE symbol. | ||
|
||
# RUN: rm -rf %t && split-file %s %t && cd %t | ||
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o | ||
# RUN: ld.lld -o a_nogc a.o -T script.t | ||
# RUN: llvm-nm a_nogc | FileCheck -check-prefix=NOGC %s | ||
# RUN: ld.lld -o a_gc a.o --gc-sections --print-gc-sections -T script.t | FileCheck --check-prefix=GC_LINK %s | ||
# RUN: llvm-nm a_gc | FileCheck -check-prefix=GC %s | ||
|
||
NOGC-NOT: another_unused | ||
NOGC: another_used | ||
NOGC: bar | ||
NOGC: baz | ||
NOGC: baz_ref | ||
NOGC: foo | ||
NOGC-NOT: unused | ||
NOGC: used | ||
|
||
GC_LINK: removing unused section a.o:(.text.bar) | ||
|
||
GC-NOT: another_unused | ||
GC: another_used | ||
GC-NOT: bar | ||
GC: baz | ||
GC: baz_ref | ||
GC: foo | ||
GC-NOT: unused | ||
GC: used | ||
|
||
#--- a.s | ||
.global _start | ||
_start: | ||
call foo | ||
call used | ||
|
||
.section .text.foo,"ax",@progbits | ||
foo: | ||
nop | ||
|
||
.section .text.bar,"ax",@progbits | ||
.global bar | ||
bar: | ||
nop | ||
|
||
.section .text.baz,"ax",@progbits | ||
.global baz | ||
baz: | ||
nop | ||
|
||
|
||
#--- script.t | ||
PROVIDE(unused = bar + used); | ||
PROVIDE(used = another_used); | ||
PROVIDE(baz_ref = baz); | ||
PROVIDE(another_used = baz_ref); | ||
PROVIDE(another_unused = unused + bar + 0x1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters