Commit ee8a4fe
committed
.cargo: Define default linker for different targets
GCC and clang on Ubuntu tend to report different system library paths,
e.g.
```
$ gcc -print-search-dirs
[...]
libraries: =/usr/lib/gcc/x86_64-linux-gnu/11/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/11/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/lib/x86_64-linux-gnu/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/lib/../lib/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/11/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib/:
/lib/x86_64-linux-gnu/11/:/lib/x86_64-linux-gnu/:
/lib/../lib/:/usr/lib/x86_64-linux-gnu/11/:/usr/lib/x86_64-linux-gnu/:
/usr/lib/../lib/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/lib/:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../:/lib/:/usr/lib/
```
```
$ clang -print-search-dirs
[...]
libraries: =/usr/lib/llvm-15/lib/clang/15.0.7:
/usr/lib/gcc/x86_64-linux-gnu/12:
/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib64:
/lib/x86_64-linux-gnu:/lib/../lib64:/usr/lib/x86_64-linux-gnu:
/usr/lib/../lib64:/lib:/usr/lib
```
In consequence, GCC and clang select different versions of libstdc++
when linking statically - GCC selects libstdc++ 11, clang selects
libstdc++ 12.
We build LLVM from source (`cargo xtask build-llvm`) using clang, which
causes our libLLVM artifacts to be linked to libstdc++ 12. Building
a static bpf-linker binary with GCC leads to linking errors, because
GCC attempts to statically link the older version of libstdc++.
To avoid such mismatches, and given that bpf-linker heavily relies on
LLVM ecosystem, use clang as a linker on all Linux targets.
Rust since 1.90.0 uses rust-lld as a default linker for
`x86_64-unknown-linux-gnu` target [0][1] and it does so by feeding
rust-lld into a C compiler, that is still used as a driver as a part of
`gnu-lld-cc` linker flavor (formerly implemented as `-Z gcc-ld=lld`
option)[2][3][4].
To benefit from faster linking times of lld (compared to bfd) on other
Linux targets, use `-fuse-ld` to force the usage of lld, that has to be
available in the system. Unfortunately, enabling rust-lld with
`-C linker-features=+lld`, `-C linker-flavor=gnu-lld-cc` or
`-C link-self-contained=+linker` is not stabilized. Using
`-C link-self-contained` (currently stabilized only with all options
enabled) does not work, as rustup toolchains do not provide all
necessary libraries for Linux targets. Neither does using rust-lld
directly due to lack of awarness about system search paths.
Use rust-lld directly on macOS without worrying about system search
paths.
[0] https://blog.rust-lang.org/2025/09/01/rust-lld-on-1.90.0-stable/
[1] rust-lang/rust#140525
[2] rust-lang/rust#71519
[3] rust-lang/rust#85961
[4] rust-lang/rust#1129101 parent 83bc2ee commit ee8a4fe
1 file changed
+32
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
0 commit comments