mold

From Gentoo Wiki
Jump to:navigation Jump to:search

mold is a linker that aims to provide drop-in compatibility with existing Unix linkers. It is many times faster than the BFD linker from GNU's binutils and slightly faster than LLVM's LLD linker in some use cases. Its speed is achieved through the usage of optimized data structures and parallelization.[1] mold is still in the early stages of development and as features are added to be on par with the other linkers[2], this could affect the speedy nature of mold.

Warning
LTO (Link-time optimization) support has been added to mold-1.1.1. Older versions of the linker lacks LTO[3] support and possibly other features traditional linkers provide for their respective compilers.

Installation

Emerge

The mold linker is in the Gentoo package repository and can be installed using the following command:

root #emerge --ask sys-devel/mold

Usage

GCC 12+ and clang support adding -fuse-ld=mold to LDFLAGS in the make.conf file so that mold is used to link all packages:

FILE /etc/portage/make.conf
LDFLAGS="-fuse-ld=mold -Wl,-O1 -Wl,--as-needed"

A patch that allows GCC 11 to invoke mold as a linker is also available. The patch can be placed in /etc/portage/patches:

root #mkdir -p /etc/portage/patches/sys-devel/gcc-11.2.1_p20211127
root #curl -Lo /etc/portage/patches/sys-devel/gcc-11.2.1_p20211127/fuse-ld-mold.patch \
    https://gist.github.com/00-matt/dda791a36318bafb68576b8576b1d283/raw/fuse-ld-mold.patch

Troubleshooting

Package fails to build using mold

Some packages do not build with mold (see bug #830404 for a list). An environment can be created to selectively disable it for some packages:

FILE /etc/portage/env/no-mold
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
FILE /etc/portage/package.env
# mold does not support linker scripts; it cannot be used to link the kernel
sys-kernel/vanilla-kernel no-mold

See also

  • Gold — a linker intended as a replacement for the ld.bfd linker.

References

  1. Why is mold so fast?, GitHub. Retrieved on January 8, 2022
  2. Why isn't ld.lld faster? LLD developer, maskray's blog December 12, 2021
  3. Support LTO linker plugin #181, GitHub. Retrieved on January 31, 2022