# Bald
Bald is the world's fastest linker for ELF, Mach-O, PE/COFF, and WebAssembly. It achieves superior performance by:
1. Supporting incremental linking.
2. Being significantly faster than competitors even when incremental linking is disabled.
## Cold link benchmarks
| Format | Bald speedup | Benchmark |
| --- | --- | --- |
| ELF | **1.56×** vs Mold | [7 Linux application links](clanker-docs/BENCHMARKS.md#elf-linux-application-links) |
| Mach-O | **1.62×** vs Apple `ld`, **2.52×** vs `ld64.lld` | [14 macOS links](clanker-docs/BENCHMARKS.md#mach-o) |
| PE/COFF | **1.3×** vs RAD, **1.8×** vs LLD | [6 Windows applications](clanker-docs/BENCHMARKS.md#pecoff-windows) |
| WebAssembly | **4.84×** vs LLD, **1.72×** vs Wild | [49 workloads vs LLD and 36 vs Wild, at 8 threads](clanker-docs/BENCHMARKS.md#webassembly-and-many-core) |
These results were measured natively i.e. Mach-O was benchmarked on macOS and PE was benchmarked on Windows Server.
Headline results for a linker leave out a lot of variance across workloads, but Bald beats the competition on every workload benchmarked so far.
## Installation
### npm
```sh
npm install -g --allow-scripts=@bald-linker/bald @bald-linker/bald
```
Installation requires Node.js 22 or newer.
If you are only linking a particular format (eg ELF) you can install a smaller binary:
| Package | Command | Format |
| --- | --- | --- |
| `@bald-linker/bald-elf` | `bald-elf` | ELF |
| `@bald-linker/bald-macho` | `bald-macho` | Mach-O |
| `@bald-linker/bald-pe` | `bald-pe` | PE/COFF |
| `@bald-linker/bald-wasm` | `bald-wasm` | WebAssembly |
### Building from source
```sh
cargo install --locked --git https://github.com/kevinheavey/bald bald
```
The full `bald` build needs a
shared LLVM development SDK and a compatible C++17 compiler (if you don't want LTO you can install with --no-default-features).
| Platform | LLVM setup |
| --- | --- |
| Debian or Ubuntu | Install `clang` and `llvm-dev`. |
| macOS | Run `brew install llvm`. |
| Windows | Install the MSYS2 CLANG64 packages `mingw-w64-clang-x86_64-clang` and `mingw-w64-clang-x86_64-llvm`. Set `LLVM_CONFIG` to the SDK's `llvm-config.exe` when running Cargo. |
See [LTO build and runtime requirements](clanker-docs/LTO.md)
for details.
For a single-format build, replace the Cargo package name `bald` with
`bald-elf`, `bald-macho`, `bald-pe`, or `bald-wasm`.
## Using Bald from a compiler
Bald is designed to be a drop-in replacement for existing linkers. For most use cases the only post-installation requirement is a configuration step to tell your conpiler to use Bald.
### Clang and GCC
| Compiler and target | How to select Bald |
| --- | --- |
| Clang targeting ELF, Mach-O, or MinGW | Use `--ld-path=/path/to/bald`. |
| Clang targeting WebAssembly | Use `-fuse-ld=/path/to/bald-wasm` (or point it at `bald`). |
| Clang targeting MSVC | Use `-fuse-ld=lld -B`, with `lld-link` in that directory pointing to `bald-pe` or `bald`. |
| GCC targeting ELF | Use `-B`, with `ld` in that directory pointing to `bald-elf` or `bald`. |
For ELF, Bald behaves like LLD by default. To make it behave like GNU ld,
run it under the name `ld.bfd`. With GCC, put a link named `ld.bfd` that
points to Bald in the `-B` directory and pass `-fuse-ld=bfd`. With Clang,
pass `--ld-path=/path/to/ld.bfd`.
### Cargo
Make your `.cargo/config.toml` look something like this:
```toml
[target.'cfg(any(target_os = "linux", target_os = "macos"))']
linker = "clang"
rustflags = ["-C", "link-arg=--ld-path=/path/to/bald"]
# Windows only
[target.x86_64-pc-windows-msvc]
linker = "C:/tools/bald/lld-link.exe"
```
On Windows, use a copy of `bald.exe` or `bald-pe.exe` named `lld-link.exe`.
Run `cargo build` in Visual Studio Developer PowerShell to pick up the MSVC
and Windows SDK libraries.
Other languages such as Go are also supported.
## Using Bald from the command line
For direct linking, run `bald [options] ...`. Use `bald --help` for
common options or `bald --help-all` for detailed help. Pass `--stats` to print link timings.
Pass `--repro` to write a reproducer archive. Its `replay.txt` gives the
command to run from the extracted directory.
Bald detects the target by default. To make it act like a particular
linker, start the command with `--flavor` and one of these names:
| Name | Acts like |
| --- | --- |
| `ld.bfd` | GNU ld |
| `ld.lld` | LLD for ELF or MinGW |
| `ld64.lld` | LLD for Mach-O |
| `lld-link` | LLD for Windows (MSVC) |
| `wasm-ld` | LLD for WebAssembly |
With `ld.bfd` or `ld.lld`, `bald` uses ELF unless a PE emulation such as
`-m i386pep` selects MinGW. `bald-pe` already selects PE/COFF, including
when given either GNU-style driver name.
For example, `bald --flavor lld-link --help` lists the Windows options. LLD's
own form also works: `-flavor` with one dash, and the names `gnu`, `darwin`,
`link` and `wasm`. As in LLD, `gnu` means `ld.lld`, not GNU ld.
MSVC-style commands read default options from `_LINK_` and `LINK` before
the command-line arguments, including when a response file selects MSVC.
Pass `/lldignoreenv` directly on the command line to skip these defaults and
the `LIB` search path.
Links with debug information are incremental by default, except optimized
ones, such as those using `-O1` or LTO. To choose, pass `--incremental` or
`--no-incremental`. With `--cache=0`, Bald reuses nothing from earlier
links, not even the output of an unchanged one. See the
[incremental linking settings](clanker-docs/compatibility.md#incremental-layout)
for details.
An explicit command-line setting takes precedence over its environment
default when linking.
Repeated `--cache` or `--lto-adapter` options must give the same value.
| Variable | Option | Effect |
|---|---|---|
| `BALD_CACHE` | `--cache=0\|PATH` | Where the link cache goes; `0` turns it off. |
| `BALD_INCREMENTAL` | `--incremental` (`1`), `--no-incremental` (`0`) | Incremental layout on or off. |
| `BALD_THREADS` | `--threads=N` | Worker limit, max 4096. |
| `BALD_LTO_ADAPTER` | `--lto-adapter=PATH` | The LLVM LTO helper; see [advanced overrides](crates/bald-wasm/LTO.md#advanced-overrides). |
| `BALD_LARGE_PAGES` | none | `0` turns off large pages on Windows. |
## Supported formats
| Format | Targets |
| --- | --- |
| ELF | x86-64, i386, AArch64, ARM, RISC-V, LoongArch, PowerPC, s390x, m68k, SPARC V9, and SuperH SH-4 |
| Mach-O | arm64 and x86-64 |
| PE/COFF | x86-64, i386, and ARM64 |
| WebAssembly | wasm32 and wasm64 |
The [compatibility reference](clanker-docs/compatibility.md) lists the
supported variants and limitations for each target.
## Using Bald as a library
Rust applications can call `link` through `bald-lib` or the format-specific
crates `bald-elf-lib`, `bald-macho-lib`, `bald-pe-lib`, and `bald-wasm-lib`.
```rust
use bald_lib::{ElfOptions, Error, LinkInput, LinkOptions, LinkRequest, LinkTarget};
fn link_program() -> Result<(), Error> {
let request = LinkRequest::new(
"program",
LinkTarget::Elf(ElfOptions::default()),
[LinkInput::File("input.o".into())],
);
bald_lib::link(&request, &LinkOptions::default())
}
```
## Development
See the [developer scripts](scripts/README.md) for setup, builds, tests, and
benchmarks, and the [npm packaging guide](npm/README.md) for release tooling.