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

lld provide - provides more symbols than needed

Open shankarke opened this issue 2 years ago • 5 comments

cat > 1.c << \!
int foo = 10;
!

cat > script.t << \!
SECTIONS {
  PROVIDE(f4 = 0x1000);
  PROVIDE(f3 = f4);
  PROVIDE(f2 = f3);
  PROVIDE(f2 = f3);
  PROVIDE(f1 = f2);
  PROVIDE(foo = f1);
}
!

clang -target riscv32 -c 1.c
ld.lld  1.o -T script.t
$ ld.lld --version
LLD 18.0.0 (compatible with GNU linkers)

$llvm-readelf -s a.out
Symbol table '.symtab' contains 10 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS 1.c
     2: 00000000     0 NOTYPE  LOCAL  DEFAULT    2 $d.0
     3: 00000000     0 NOTYPE  LOCAL  DEFAULT    3 $d.1
     4: 00000000     0 NOTYPE  LOCAL  DEFAULT  ABS $d.2
     5: 00000000     4 OBJECT  GLOBAL DEFAULT    2 foo
     6: 00001000     0 NOTYPE  GLOBAL DEFAULT  ABS f4
     7: 00001000     0 NOTYPE  GLOBAL DEFAULT  ABS f3
     8: 00001000     0 NOTYPE  GLOBAL DEFAULT  ABS f2
     9: 00001000     0 NOTYPE  GLOBAL DEFAULT  ABS f1

Is this is a known issue why GNU linker script PROVIDE directive works this way with lld ?

shankarke avatar Dec 07 '23 21:12 shankarke

If a symbol is only referenced by the right hand side of a PROVIDE assignment, it is not considered referenced and PROVIDE will not define it. For simplicity, ld.lld does not implement this special rule.

as /dev/null -o a.o
cat > a.t <<e
SECTIONS {
  PROVIDE(f3 = 0x1000);
  PROVIDE(f2 = f3);
  PROVIDE(f1 = f2);
  PROVIDE(foo = f1);
}
e
ld.bfd a.o -T a.t -o a.bfd
ld.lld a.o -T a.t -o a.lld
% readelf -s a.bfd

Symbol table '.symtab' contains 1 entry:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
% readelf -s a.lld

Symbol table '.symtab' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000001000     0 NOTYPE  GLOBAL DEFAULT  ABS f3
     2: 0000000000001000     0 NOTYPE  GLOBAL DEFAULT  ABS f2
     3: 0000000000001000     0 NOTYPE  GLOBAL DEFAULT  ABS f1

MaskRay avatar Dec 09 '23 19:12 MaskRay

Thanks @MaskRay, I don't see that this behavior will break anything, any plans on fixing it ?

shankarke avatar Dec 11 '23 22:12 shankarke

I think implementing this behavior is non-trivial. so I'd ask why it's important to implement this behavior.

The RHS of a symbol assignment does serve the purpose of Undefined and we implement it with referencedSymbols. referencedSymbols is needed to extract archive members and inform LTO about the uses. One nice aspect using referencedSymbols is that we reduce position dependence.

For BFD like PROVIDE behavior, it seems that we cannot unconditionally add Undefined for referencedSymbols. We need to add back the entries when a PROVIDE gets used. I believe the changes would be very intrusive.


GNU ld performs many iterations, but some ports (e.g. x86, but not aarch64/riscv) do not allow too many iterations.

% cat chain.t
PROVIDE(f7 = 0x1000);
PROVIDE(f6 = f7);
PROVIDE(f5 = f6);
PROVIDE(f4 = f5);
PROVIDE(f3 = f4);
PROVIDE(f2 = f3);
PROVIDE(f1 = f2);
PROVIDE(newsym = f1);
% ld.bfd a.o -T chain.t
ld.bfd:chain.t:2: undefined symbol `f7' referenced in expression

FWIW I have improved PROVIDE tests in 215c5656449c8e817a3759d989f27ba39d711cbd

MaskRay avatar Dec 13 '23 07:12 MaskRay

I see zephyr project needs this kind of behavior :- https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/linker/thread-local-storage.ld

shankarke avatar Dec 13 '23 12:12 shankarke

@llvm/issue-subscribers-lld-elf

Author: None (shankarke)

``` cat > 1.c << \! int foo = 10; !

cat > script.t << ! SECTIONS { PROVIDE(f4 = 0x1000); PROVIDE(f3 = f4); PROVIDE(f2 = f3); PROVIDE(f2 = f3); PROVIDE(f1 = f2); PROVIDE(foo = f1); } !

clang -target riscv32 -c 1.c ld.lld 1.o -T script.t

$ ld.lld --version LLD 18.0.0 (compatible with GNU linkers)

$llvm-readelf -s a.out Symbol table '.symtab' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS 1.c 2: 00000000 0 NOTYPE LOCAL DEFAULT 2 $d.0 3: 00000000 0 NOTYPE LOCAL DEFAULT 3 $d.1 4: 00000000 0 NOTYPE LOCAL DEFAULT ABS $d.2 5: 00000000 4 OBJECT GLOBAL DEFAULT 2 foo 6: 00001000 0 NOTYPE GLOBAL DEFAULT ABS f4 7: 00001000 0 NOTYPE GLOBAL DEFAULT ABS f3 8: 00001000 0 NOTYPE GLOBAL DEFAULT ABS f2 9: 00001000 0 NOTYPE GLOBAL DEFAULT ABS f1


Is this is a known issue why GNU linker script PROVIDE directive works this way with lld ?

</details>

llvmbot avatar Dec 13 '23 23:12 llvmbot