3.5 KiB
obj | repo | rev |
---|---|---|
application | https://github.com/rui314/mold | 2024-03-22 |
mold
mold is a faster drop-in replacement for existing Unix linkers. It is several times quicker than the LLVM lld linker, the second-fastest open-source linker, which I initially developed a few years ago. mold aims to enhance developer productivity by minimizing build time, particularly in rapid debug-edit-rebuild cycles.
Usage
A classic way to use mold
On Unix, the linker command (usually /usr/bin/ld
) is indirectly invoked by the compiler driver (typically cc
, gcc
, or clang
), which is in turn indirectly invoked by make
or other build system commands.
If you can specify an additional command line option for your compiler driver by modifying the build system's config files, add one of the following flags to use mold instead of /usr/bin/ld
:
- For Clang: pass
-fuse-ld=mold
- For GCC 12.1.0 or later: pass
-fuse-ld=mold
- For GCC before 12.1.0: the
-fuse-ld
option does not acceptmold
as a valid argument, so you need to use the-B
option instead. The-B
option tells GCC where to look for external commands likeld
.
If you have installed mold with make install
, there should be a directory named /usr/libexec/mold
(or /usr/local/libexec/mold
, depending on your $PREFIX
), and the ld
command should be there. The ld
is actually a symlink to mold
. So, all you need is to pass -B/usr/libexec/mold
(or -B/usr/local/libexec/mold
) to GCC.
If you haven't installed ld.mold
to any $PATH
, you can still pass -fuse-ld=/absolute/path/to/mold
to clang to use mold. However, GCC does not accept an absolute path as an argument for -fuse-ld
.
If you are using Rust
Create .cargo/config.toml
in your project directory with the following:
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=/path/to/mold"]
where /path/to/mold
is an absolute path to the mold executable. In the example above, we use clang
as a linker driver since it always accepts the -fuse-ld
option. If your GCC is recent enough to recognize the option, you may be able to remove the linker = "clang"
line.
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=/path/to/mold"]
If you want to use mold for all projects, add the above snippet to ~/.cargo/config.toml
.
GitHub Actions
You can use our setup-mold GitHub Action to speed up GitHub-hosted continuous builds. Although GitHub Actions run on a 4 core machine, mold is still significantly faster than the default GNU linker, especially when linking large programs.
Verify that you are using mold
mold leaves its identification string in the .comment
section of an output file. You can print it out to verify that you are actually using mold.
$ readelf -p .comment <executable-file>
String dump of section '.comment':
[ 0] GCC: (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
[ 2b] mold 9a1679b47d9b22012ec7dfbda97c8983956716f7
If mold
is present in the .comment
section, the file was created by mold.
Documentation
Since mold is a drop-in replacement, you should be able to use it without reading its manual. However, if you need it, mold's man page is available online. You can read the same manual by running man mold
.