Should `PROVIDE` provide symbols that are only referenced by garbage-collected symbols?
PROVIDE linker script directive provide symbols that are referenced by the link but are not defined anywhere. But what should be the PROVIDE behavior for symbols that are only referenced by symbols which are garbage-collected? Please consider this concrete example:
#!/usr/bin/env bash
cat >1.c <<\EOF
extern int foo;
int bar() { return foo; }
int baz() { return 1; }
int main() { return baz(); }
EOF
cat >script.t <<\EOF
PROVIDE(foo = 3);
EOF
clang-14 -o 1.o 1.c -c -ffunction-sections
ld.lld -o 1.out 1.o -T script.t -e main --gc-sections --print-gc-sections
# Output:
# removing unused section 1.o:(.text)
# removing unused section 1.o:(.text.bar)
llvm-readelf -s 1.out
# Output:
# Symbol table '.symtab' contains 5 entries:
# Num: Value Size Type Bind Vis Ndx Name
# 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
# 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS 1.c
# 2: 0000000000000003 0 NOTYPE GLOBAL DEFAULT ABS foo
# 3: 0000000000201180 11 FUNC GLOBAL DEFAULT 2 baz
# 4: 0000000000201190 26 FUNC GLOBAL DEFAULT 2 main
In the above example, foo is inserted into the link by PROVIDE(foo = 3) because it is referenced by bar. However, bar is itself not used anywhere and is garbage-collected during the link. So, from one perspective foo is not used anywhere. Is it right to provide a symbol from PROVIDE if the symbol is not used anywhere?
I am looking forward to more thoughts and opinions on this.
@llvm/issue-subscribers-lld-elf
Author: Parth Arora (partaror)
#!/usr/bin/env bash
cat >1.c <<\EOF
extern int foo;
int bar() { return foo; }
int baz() { return 1; }
int main() { return baz(); }
EOF
cat >script.t <<\EOF
PROVIDE(foo = 3);
EOF
clang-14 -o 1.o 1.c -c -ffunction-sections
ld.lld -o 1.out 1.o -T script.t -e main --gc-sections --print-gc-sections
# Output:
# removing unused section 1.o:(.text)
# removing unused section 1.o:(.text.bar)
llvm-readelf -s 1.out
# Output:
# Symbol table '.symtab' contains 5 entries:
# Num: Value Size Type Bind Vis Ndx Name
# 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
# 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS 1.c
# 2: 0000000000000003 0 NOTYPE GLOBAL DEFAULT ABS foo
# 3: 0000000000201180 11 FUNC GLOBAL DEFAULT 2 baz
# 4: 0000000000201190 26 FUNC GLOBAL DEFAULT 2 main
In the above example, foo is inserted into the link by PROVIDE(foo = 3) because it is referenced by bar. However, bar is itself not used anywhere and is garbage-collected during the link. So, from one perspective foo is not used anywhere. Is it right to provide a symbol from PROVIDE if the symbol is not used anywhere?
I am looking forward to more thoughts and opinions on this.
@MaskRay you have closed this as completed - what was the resolution? Was there a change to how LLD works or a clarification to docs to go with this closure?
OK, this is different from #74771. Our behavior matches GNU ld: foo is defined in both output.
ld.lld -o 1.out 1.o -T script.t -e main --gc-sections --print-gc-sections
ld.bfd -o 1.out 1.o -T script.t -e main --gc-sections --print-gc-sections
We do symbol resolution first and determine that foo should be defined. Absolute symbols are not discarded by --gc-sections.
Even if the definition is modified from PROVIDE(foo = 3); to PROVIDE(foo = baz);, the symbol foo is still retained in both GNU ld and lld output.
This is because --gc-sections doesn't check symbol assignments in linker scripts.
While it is technically feasible to discard PROVIDE(foo = baz); by tracking every reference to foo, implementing this is complex. Doing so would also deviate from GNU ld's behavior.
Thanks for looking back at this and explaining your resolution :)