2020-10-21 08:55:50 +03:00
|
|
|
# mold: A Modern Linker
|
2020-10-21 06:52:11 +03:00
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
mold is a faster drop-in replacement for existing Unix linkers.
|
|
|
|
It is several times faster than LLVM lld linker, the second-fastest
|
|
|
|
open-source linker which I originally created a few years ago.
|
|
|
|
mold is created for increasing developer productivity by reducing
|
|
|
|
build time especially in rapid debug-edit-rebuild cycles.
|
2020-10-21 06:52:11 +03:00
|
|
|
|
2021-08-06 02:55:25 +03:00
|
|
|
Here is a performance comparison of GNU gold, LLVM lld, and mold for
|
2021-12-12 15:19:12 +03:00
|
|
|
linking final debuginfo-enabled executables of major large programs
|
|
|
|
on a simulated 8-core 16-threads machine.
|
2021-03-21 07:51:34 +03:00
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
![Link speed comparison](docs/comparison.png)
|
2021-03-21 07:51:34 +03:00
|
|
|
|
2021-12-13 09:28:26 +03:00
|
|
|
| Program (linker output size) | GNU gold | LLVM lld | mold
|
|
|
|
|-------------------------------|----------|----------|--------
|
|
|
|
| Chrome 96 (1.89 GiB) | 53.86s | 11.74s | 2.21s
|
|
|
|
| Clang 13 (3.18 GiB) | 64.12s | 5.82s | 2.90s
|
|
|
|
| Firefox 89 libxul (1.64 GiB) | 32.95s | 6.80s | 1.42s
|
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
mold is so fast that it is only 2x _slower_ than `cp` on the same
|
|
|
|
machine.
|
2021-03-21 07:51:34 +03:00
|
|
|
|
2021-12-15 04:44:10 +03:00
|
|
|
Feel free to [file a bug](https://github.com/rui314/mold/issues) if
|
|
|
|
you find mold is not faster than other linkers.
|
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
## 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. In the first phase, a compiler compiles a
|
|
|
|
source file into an object file (`.o` files). In the second phase,
|
|
|
|
a linker takes all object files to combine them into a single executable
|
|
|
|
or a shared library file.
|
2021-04-18 15:41:36 +03:00
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
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. The difference is
|
|
|
|
most noticeable when you are in rapid debug-edit-rebuild cycles.
|
2021-04-19 16:25:13 +03:00
|
|
|
|
2021-12-22 13:59:39 +03:00
|
|
|
## Install
|
|
|
|
|
|
|
|
Binary packages for the following distros are currently available.
|
|
|
|
|
|
|
|
[![Packaging status](https://repology.org/badge/vertical-allrepos/mold.svg)](https://repology.org/project/mold/versions)
|
|
|
|
|
2021-03-16 19:35:32 +03:00
|
|
|
## How to build
|
|
|
|
|
2021-12-22 13:59:39 +03:00
|
|
|
mold is written in C++20, so if you build mold yourself, you need a
|
|
|
|
very recent version of GCC or Clang. I'm using Ubuntu 20.04 as a
|
|
|
|
development platform. In that environment, you can build mold by the
|
|
|
|
following commands.
|
2021-03-16 19:35:32 +03:00
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
### Install dependencies
|
|
|
|
|
|
|
|
#### Ubuntu 20.04 and later / Debian 11 and later
|
|
|
|
|
|
|
|
```shell
|
|
|
|
sudo apt-get update
|
|
|
|
sudo apt-get install -y build-essential git clang cmake libstdc++-10-dev libssl-dev libxxhash-dev zlib1g-dev
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Fedora 34 and later
|
|
|
|
|
|
|
|
```shell
|
2021-12-16 15:02:00 +03:00
|
|
|
sudo dnf install -y git clang cmake openssl-devel xxhash-devel zlib-devel libstdc++-devel
|
2021-03-16 19:35:32 +03:00
|
|
|
```
|
2021-10-27 20:22:38 +03:00
|
|
|
|
|
|
|
### Compile mold
|
|
|
|
|
|
|
|
```shell
|
|
|
|
git clone https://github.com/rui314/mold.git
|
|
|
|
cd mold
|
2021-12-15 15:25:26 +03:00
|
|
|
git checkout v1.0.0
|
2021-10-27 20:22:38 +03:00
|
|
|
make -j$(nproc)
|
|
|
|
sudo make install
|
2021-03-16 19:35:32 +03:00
|
|
|
```
|
|
|
|
|
2021-12-13 09:28:26 +03:00
|
|
|
By default, `mold` is installed to `/usr/local/bin`.
|
2021-03-24 12:38:08 +03:00
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
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
|
2021-05-18 07:20:57 +03:00
|
|
|
a Docker environment. To do so, just run `./build-static.sh` in this
|
2021-12-13 09:28:26 +03:00
|
|
|
directory instead of running `make -j$(nproc)`. The shell script creates a
|
|
|
|
Ubuntu 20.04 Docker image, installs necessary tools and libraries to it,
|
|
|
|
and builds mold as a statically-linked executable.
|
2021-04-19 17:08:29 +03:00
|
|
|
|
2021-07-21 08:22:15 +03:00
|
|
|
`make test` depends on a few more packages. To install, run the following commands:
|
2021-05-23 10:02:47 +03:00
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
```shell
|
|
|
|
sudo dpkg --add-architecture i386
|
|
|
|
sudo apt update
|
|
|
|
sudo apt-get install bsdmainutils dwarfdump libc6-dev:i386 lib32gcc-10-dev libstdc++-10-dev-arm64-cross gcc-10-aarch64-linux-gnu g++-10-aarch64-linux-gnu
|
2021-05-23 10:02:47 +03:00
|
|
|
```
|
|
|
|
|
2021-03-26 11:06:35 +03:00
|
|
|
## How to use
|
|
|
|
|
2021-12-27 06:14:30 +03:00
|
|
|
<details><summary>A classic way to use mold</summary>
|
|
|
|
|
2021-03-26 11:06:35 +03:00
|
|
|
On Unix, the linker command (which is usually `/usr/bin/ld`) is
|
2021-12-27 06:14:30 +03:00
|
|
|
invoked indirectly by the compiler driver (which is usually `cc`,
|
|
|
|
`gcc` or `clang`), which is typically in turn indirectly invoked by
|
|
|
|
`make` or some other build system command.
|
|
|
|
|
|
|
|
If you can specify an additional command line option to your compiler
|
|
|
|
driver by modifying build system's config files, add one of the
|
|
|
|
following flags to use `mold` instead of `/usr/bin/ld`:
|
2021-06-30 20:45:04 +03:00
|
|
|
|
2021-12-29 15:21:06 +03:00
|
|
|
- Clang: pass `-fuse-ld=<absolute-path-to-mold-executable>`;
|
2021-12-27 07:37:25 +03:00
|
|
|
|
2021-12-29 15:21:06 +03:00
|
|
|
- GCC 12.1.0 or later: pass `-fuse-ld=<absolute-path-to-mold-executable>`;
|
2021-12-27 07:37:25 +03:00
|
|
|
|
2021-12-29 15:21:06 +03:00
|
|
|
- GCC before 12.1.0: `-fuse-ld` does not accept `mold` as a valid
|
|
|
|
argument, so you need to use `-B` option instead. `-B` is an option
|
|
|
|
to tell GCC where to look for external commands such as `ld`.
|
2021-12-27 07:37:25 +03:00
|
|
|
|
|
|
|
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 `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.
|
2021-12-27 06:14:30 +03:00
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
<details><summary>If you are using Rust</summary>
|
|
|
|
|
|
|
|
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 `mold` exectuable.
|
|
|
|
|
|
|
|
If you want to use mold for all projects, put the above snippet to
|
|
|
|
`~/.cargo/config.toml`.
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
<details><summary>mold -run</summary>
|
2021-03-26 11:06:35 +03:00
|
|
|
|
2021-09-23 08:31:08 +03:00
|
|
|
It is sometimes very hard to pass an appropriate command line option
|
|
|
|
to `cc` to specify an alternative linker. To deal with the situation,
|
|
|
|
mold has a feature to intercept all invocations of `ld`, `ld.lld` or
|
|
|
|
`ld.gold` and redirect it to itself. To use the feature, run `make`
|
|
|
|
(or another build command) as a subcommand of mold as follows:
|
2021-03-24 12:38:08 +03:00
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
```shell
|
2021-12-13 09:28:26 +03:00
|
|
|
mold -run make <make-options-if-any>
|
2021-03-24 12:38:08 +03:00
|
|
|
```
|
|
|
|
|
2021-07-17 09:09:16 +03:00
|
|
|
Here's an example showing how to link Rust code when using the
|
2021-09-23 08:31:08 +03:00
|
|
|
cargo package manager:
|
2021-07-16 16:00:12 +03:00
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
```shell
|
2021-12-13 09:28:26 +03:00
|
|
|
mold -run cargo build
|
2021-07-16 16:00:12 +03:00
|
|
|
```
|
|
|
|
|
2021-03-26 11:06:35 +03:00
|
|
|
Internally, mold invokes a given command with `LD_PRELOAD` environment
|
|
|
|
variable set to its companion shared object file. The shared object
|
2021-09-23 08:31:08 +03:00
|
|
|
file intercepts all function calls to `exec(3)`-family functions to
|
|
|
|
replace `argv[0]` with `mold` if it is `ld`, `ld.gold` or `ld.lld`.
|
2021-03-26 11:06:35 +03:00
|
|
|
|
2021-12-27 06:14:30 +03:00
|
|
|
</details>
|
|
|
|
|
2021-03-24 12:38:08 +03:00
|
|
|
mold leaves its identification string in `.comment` section in an output
|
|
|
|
file. You can print it out to verify that you are actually using mold.
|
|
|
|
|
2021-10-27 20:22:38 +03:00
|
|
|
```shell
|
|
|
|
readelf -p .comment <executable-file>
|
2021-03-24 12:38:08 +03:00
|
|
|
|
|
|
|
String dump of section '.comment':
|
|
|
|
[ 0] GCC: (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
|
|
|
|
[ 2b] mold 9a1679b47d9b22012ec7dfbda97c8983956716f7
|
|
|
|
```
|
|
|
|
|
|
|
|
If `mold` is in `.comment`, the file is created by mold.
|
|
|
|
|
2021-12-12 15:19:12 +03:00
|
|
|
## Why is mold so fast?
|
|
|
|
|
2021-12-13 09:28:26 +03:00
|
|
|
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.
|
2021-12-12 15:19:12 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
![CPU usage comparison in htop animation](docs/htop.gif)
|
|
|
|
|
|
|
|
As you can see, mold uses all available cores throughout its execution
|
|
|
|
and finishes quickly. On the other hand, lld failed to use available
|
|
|
|
cores most of the time. In this demo, the maximum parallelism is
|
|
|
|
artificially capped to 16 so that the bars fit in the GIF.
|
|
|
|
|
2021-12-13 09:28:26 +03:00
|
|
|
For details, please read [design notes](docs/design.md).
|
2021-12-12 15:19:12 +03:00
|
|
|
|
|
|
|
# Logo
|
|
|
|
|
|
|
|
-![mold image](docs/mold.jpg)
|