I'm using lld as my linker currently for Rust, but recently encountered the mold project, which is faster than lld. I'd like to try it for Rust compilation, but I'm not sure how to pass it in as my linker. In my .cargo/config
file I've got:
[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C", "link-arg=-fuse-ld=lld",
]
But I can't just change that lld
to mold
, or provide the path to the mold
executable. Is there a way to get gcc to accept a path to a linker?
In the second phase, a linker takes all object files to combine them into a single executable or a shared library file. The second phase takes a long time if your build output is large. mold can make it faster, saving your time and keeping you from being distracted while waiting for a long build to finish.
Feel free to file a bug if you find mold is not faster than other linkers. mold currently supports x86-64, i386, ARM32, ARM64 and 64-bit RISC-V. Why does the speed of linking matter? If you are using a compiled language such as C, C++ or Rust, a build consists of two phases.
One reason is because it simply uses faster algorithms and efficient data structures than other linkers do. The other reason is that the new linker is highly parallelized. Here is a side-by-side comparison of per-core CPU usage of lld (left) and mold (right). They are linking the same program, Chromium executable.
By default, mold is installed to /usr/local/bin. If you don't use a recent enough Linux distribution, or if for any reason make in the above commands doesn't work for you, you can use Docker to build it in a Docker environment. To do so, just run ./dist.sh in this directory instead of running make -j$ (nproc).
Mold can now be used with Clang by simply adding this to ~/.cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-C", "link-arg=--ld-path=/usr/bin/mold"]
Note: Mold may be installed at /usr/local/bin/mold
if installed from source so the flags should be rustflags = ["-C", "link-arg=--ld-path=/usr/local/bin/mold"]
. Run $ which mold
to double check where it's installed
You can check it's worked by running readelf -p .comment target/<type>/<binary_name>
$ readelf -p .comment target/debug/my_binary
String dump of section '.comment':
[ 0] GCC: (GNU) 11.1.0
[ 13] mold 1.0.0 (compatible with GNU ld and GNU gold)
gcc does not accept an arbitrary path as an argument for -fuse-ld
, unfortunately. Instead of tweaking .cargo/config
file, try running cargo build
as path/to/mold -run cargo build
. By doing this, cargo runs under an influence of mold, and all invocations of /usr/bin/ld
, /usr/bin/ld.gold
and /usr/bin/ld.lld
are intercepted and replaced by mold
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With