TExcelTAD

Faster Links: lld, mold & Split DWARF

After the compiles, every build ends with links, and an incremental build is often mostly link: change one file, compile it in a second, then wait for the linker to process hundreds of megabytes of objects. Two things decide link time: which linker you use, and how much debug information it has to move around.

Choosing a linker

Linker From Notes
GNU ld (BFD) binutils the default on most Linux distributions; the slowest on big programs
gold binutils faster than BFD, but deprecated upstream and no longer developed
lld LLVM much faster than BFD, multi-threaded, drop-in for ELF, COFF, Mach-O and Wasm
mold Rui Ueyama (also the original lld author) usually the fastest on Linux; highly parallel
link.exe / lld-link MSVC / LLVM Windows; lld-link is a drop-in for link.exe
ld64 / ld-prime Apple Xcode 15's new linker is much faster than the old ld64

Switching is a flag, not a migration. GCC and Clang both accept -fuse-ld=lld (and -fuse-ld=mold with GCC 12.1+ / recent Clang); CMake 3.29+ has a portable variable for it, CMAKE_LINKER_TYPE (LLD, MOLD, GOLD, BFD, …):

bash · try it yourself
mkdir -p linkers/src && cd linkers
printf 'int helper();\nint main() { return helper() - 1; }\n' > src/main.cpp
printf 'int helper() { return 1; }\n' > src/helper.cpp
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.29)
project(Linkers CXX)
add_executable(app src/main.cpp src/helper.cpp)
CM
for type in BFD LLD; do
  cmake -S . -B build-$type -G Ninja -DCMAKE_LINKER_TYPE=$type > /dev/null
  cmake --build build-$type > /dev/null && ./build-$type/app
  echo "$type: $(cmake --build build-$type -v -t clean > /dev/null; cmake --build build-$type -v | grep -o -- '-fuse-ld=[a-z]*' | head -1) -> $(readelf -p .comment build-$type/app | grep -o 'LLD [0-9.]*' || echo 'GNU ld')"
done
BFD: -fuse-ld=bfd -> GNU ld
LLD: -fuse-ld=lld -> LLD 18.1.3

lld records itself in the .comment section, which is a handy check that the switch really happened. mold isn't installed in this image; it works the same way (-DCMAKE_LINKER_TYPE=MOLD). Measure link time on your real program before and after; for large C++ binaries, moving from BFD to lld or mold commonly cuts link time by several times.

Debug info is most of the bytes

A debug build's objects are mostly DWARF debug information, and the linker has to read, relocate and copy all of it. Compare one small TU with <map> and <string> compiled three ways:

bash · try it yourself
mkdir -p dwarf && cd dwarf
printf '#include <map>\n#include <string>\nint f() { std::map<std::string, int> m{{"a", 1}}; return int(m.size()); }\n' > f.cpp
g++ -c f.cpp -o plain.o
g++ -g -c f.cpp -o debug.o
g++ -g -gsplit-dwarf -c f.cpp -o split.o
for f in plain.o debug.o split.o split.dwo; do printf '%-10s %7d bytes\n' $f $(stat -c %s $f); done
echo "--- debug sections still in split.o: $(readelf -SW split.o | grep -c '\.debug_')"
echo "--- debug sections in split.dwo:     $(readelf -SW split.dwo | grep -c '\.debug_.*\.dwo')"
plain.o      65080 bytes
debug.o     260504 bytes
split.o     120272 bytes
split.dwo   155432 bytes
--- debug sections still in split.o: 16
--- debug sections in split.dwo:     6
  • -g makes the object four times bigger: three quarters of debug.o is debug info.
  • -gsplit-dwarf moves most of it into a separate .dwo file next to the object. The linker never sees .dwo files, so it has far less to process; the debugger finds them through references left in the binary. Linking big debug builds gets markedly faster, and incremental relinks benefit the most.

Related switches:

Option Effect
-gsplit-dwarf debug info in .dwo files the linker skips; dwp packs them for shipping
-g1 / -gline-tables-only (Clang) only line tables: enough for stack traces, much smaller
-gz (-gz=zstd with newer toolchains) compress debug sections: less I/O, a bit more CPU
-Wl,--gdb-index (lld, gold, mold) build the debugger's index at link time: faster gdb startup
-fdebug-types-section deduplicate type info across TUs (DWARF 4; mostly superseded by DWARF 5 features)

Other link-time savers

  • Shared libraries in development builds. Linking an executable against ten .so files is cheaper than relinking all their objects statically every time; BUILD_SHARED_LIBS=ON for local builds, static for releases.
  • Fewer, smaller binaries. Twenty test executables each linking the whole engine means twenty big links. One test runner per module, or tests in shared libraries, cuts that.
  • --gc-sections and -ffunction-sections make binaries smaller but give the linker more sections to process; they're for releases, not for fast iteration.
  • Avoid LTO in inner-loop builds (see LTO).

Test yourself

Which CMake variable (3.29+) selects lld or mold portably?

  1. CMAKE_LINKER=lld
  2. CMAKE_LINKER_TYPE
  3. CMAKE_EXE_LINKER_FLAGS only
  4. CMAKE_LD_MODE

What does -gsplit-dwarf change for the linker?

  1. Nothing
  2. Most debug info goes to .dwo files the linker never reads, so it processes far less data
  3. It removes all debug info
  4. It makes linking parallel

In the session, how much of the -g object was debug information?

  1. About a tenth
  2. About half
  3. About three quarters
  4. None