Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the mold linker with cargo?

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?

like image 369
Marcus Buffett Avatar asked May 12 '21 22:05

Marcus Buffett


People also ask

What is a linker in mould?

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.

Is mold faster than other linkers?

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.

Why use LLD instead of other linkers?

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.

How do I run mold in Docker?

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).


Video Answer


2 Answers

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)
like image 194
timlyo Avatar answered Nov 15 '22 11:11

timlyo


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.

like image 40
Rui Ueyama Avatar answered Nov 15 '22 11:11

Rui Ueyama