llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Should `PROVIDE` provide symbols that are only referenced by garbage-collected symbols?

Open partaror opened this issue 2 years ago • 4 comments

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.

partaror avatar Aug 27 '24 13:08 partaror

@llvm/issue-subscribers-lld-elf

Author: Parth Arora (partaror)

`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 &gt;1.c &lt;&lt;\EOF
extern int foo;

int bar() { return foo; }
int baz() { return 1; }

int main() { return baz(); }
EOF

cat &gt;script.t &lt;&lt;\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.

llvmbot avatar Aug 27 '24 14:08 llvmbot

@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?

lenary avatar Feb 02 '26 21:02 lenary

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.

MaskRay avatar Feb 03 '26 05:02 MaskRay

Thanks for looking back at this and explaining your resolution :)

lenary avatar Feb 03 '26 05:02 lenary