bazelbuild / bazelbuild/rules_rust

[Feature request] Easy way to switch to rust-lld for linking on Linux

Open
#3,602 4 comments 1 reaction 0 assignees View on GitHub
needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

With the exception of wasm, rules_rust currently always uses the linker specified in the relevant cc_toolchain: https://github.com/bazelbuild/rules_rust/blob/6d532fd5e170ca5cfe2ab0acb83d67527bafc4e5/rust/private/rustc.bzl#L1053-L1063. However, it would be good to let users configure what linker to use for Rust:

* On Linux, rules_cc defaults to LLD, then gold, then the default ld (ld.bfd). If LLD is not installed globally, then it'll use gold, but gold is deprecated and [not appropriate](https://github.com/rust-lang/rust/issues/141748) for Rust.
* On several platforms, Rust comes bundled with [`rust-lld`](https://blog.rust-lang.org/2024/05/17/enabling-rust-lld-on-linux/). In fact, Rust 1.90.0 will make it so that it uses `rust-lld` [by default](https://blog.rust-lang.org/2025/09/01/rust-lld-on-1.90.0-stable/) on x86-64 Linux.
* LLD is [much faster](https://www.phoronix.com/news/LLD-Linker-Why-So-Fast) than both ld.bfd and gold.

It would be nice to be able to use rust-lld for linking, either through a `gcc` driver, or directly. (I.e., `-Clinker-flavor=`[`ld.lld`](https://doc.rust-lang.org/rustc/codegen-options/index.html#linker-flavor)/[`gnu-lld`](https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/codegen-options.html#linker-flavor) and [`gnu-lld-cc`](https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/codegen-options.html#linker-flavor).)

----

As breadcrumbs, I was able to get the equivalent of `linker-flavor=gnu-lld-cc` working locally by applying:

* https://github.com/bazelbuild/rules_rust/pull/3600
* A patch to expand $(RUST_SYSROOT) in extra_exec_rustc_flags

```diff
diff -ur rules_rust/rust/private/rustc.bzl rules_rust-patched/rust/private/rustc.bzl
--- rules_rust/rust/private/rustc.bzl 2025-09-09 01:16:47.480776299 +0000
+++ rules_rust-patched/rust/private/rustc.bzl 2025-09-09 01:20:48.211779196 +0000
@@ -1099,10 +1099,12 @@
if crate_info.type in toolchain.extra_rustc_flags_for_crate_types.keys():
rustc_flags.add_all(toolchain.extra_rustc_flags_for_crate_types[crate_info.type], map_each = map_flag)

- if is_exec_configuration(ctx):
- rustc_flags.add_all(toolchain.extra_exec_rustc_flags, map_each = map_flag)
- else:
- rustc_flags.add_all(toolchain.extra_rustc_flags, map_each = map_flag)
+ for flag in (toolchain.extra_exec_rustc_flags if is_exec_configuration(ctx) else toolchain.extra_rustc_flags):
+ if map_flag:
+ flag = map_flag(flag)
+ if flag != None:
+ flag = flag.replace("$(RUST_SYSROOT)", toolchain.sysroot)
+ rustc_flags.add(flag)

# extra_rustc_flags apply to the target configuration, not the exec configuration.
if hasattr(ctx.attr, "_extra_rustc_flags") and not is_exec_configuration(ctx):
```

https://github.com/bazelbuild/rules_rust/pull/3640

* Finally, `extra_rustc_flags` to tell the GCC driver to use lld
```starlark
rust_register_toolchains(
versions = ["1.89.0"],
extra_rustc_flags = {
"x86_64-unknown-linux-gnu": [
"--codegen=link-arg=-B$(RUST_SYSROOT)/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld",
"--codegen=link-arg=-fuse-ld=lld",
],
},
)
```
With https://github.com/bazelbuild/rules_rust/pull/3601 applied, we would be able to also do the same with `extra_exec_rustc_flags`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.