1. Introduction

In GNU/Linux systems, three common linkers are widely used for C/C++ development, supporting nearly all features and options. These linkers, in chronological order of their creation, are GNU ld (GNU Linker, or BFD Linker), GNU gold (GNU’s Other LD), and LLVM lld (LLVM Linker).

In this tutorial, we’ll explore the gold linker. We’ll begin with its history, followed by a comparison with ld, a guide on how to use it, and finally, we’ll do a simple performance test.

All commands mentioned in this guide have been tested on Debian 12 (Bookworm) with GNU GCC 12.2.0 for gcc and g++, and GNU binutils 2.40 for ld, and gold linkers.

2. Linker

In a C/C++ compilation process, we use a linker to build executables or shared libraries. The process usually follows these steps:

Compilation processes

During the compilation process:

  • preprocessor replaces #include directives and macro define statements with their contents
  • compiler (gcc, g++) compiles the expanded source code to assembly code (.s)
  • assembler transforms the assembly code to object files (.o)
  • linker (ld, gold, lld) resolves all references in the object files, links all functions and data from other libraries, and converts them into executables or shared libraries
  • Notably, we can distribute the compiling process but not linking

In addition, linkers don’t build static libraries. Static libraries are collections of object files bundled into a single archive file (.a), which can be built using the gcc or ar command.

3. GNU Gold Linker

GNU gold is an ELF linker developed from scratch in C++ by Ian Lance Taylor. It was released in March 2008 and included in GNU binutils.

GNU gold is a complete re-implementation of ld. However, unlike ld, it only supports the ELF format. This means it doesn’t support older formats like a.out (assembler output) and COFF object file.

The removal of the BFD abstraction layer to support older formats makes gold much faster than ld. Taylor mentioned in his research publication that gold* ranges from two to five times faster than *ld. As a result, gold is worth considering as a replacement for ld when we’re building big applications.

4. Using GNU Gold Linker

We can use gold by specifying it with the compiler option -fuse-ld=gold:

LD = ld.gold

If we’re using a makefile, we need to set the LD or LDFLAGS variables:

LDFLAGS += -fuse-ld=gold

Those variables will tell GCC to use the gold linker.

Let’s try the gold linker by building the Bitcoin Core project version 25.0. We’ll build the project twice, first with ld and then with gold, and compare the build times.

4.1. Building Bitcoin Core With GNU Linker

To start, let’s build the project with ld:

$ git clone https://github.com/bitcoin/bitcoin.git
$ cd bitcoin
$ git checkout v25.0
$ ./autogen.sh
$ ./configure
$ time make
...
real 57m47.547s
user 51m48.954s
sys 3m47.348s

The total build time was 57 minutes and 47 seconds.

In the command chain above, we cloned the repo, checked out the v25.0 branch, executed the Autotools scripts (autogen.sh, configure), and built the project by running make. We measured the build time using the time command.

4.2. Building Bitcoin Core With GNU Gold

Next, let’s build the project using the gold linker.

The Bitcoin project uses Autotools, which provides an autogen.sh script to generate the configure script for modifying the project’s configuration. Therefore, we tell the compiler to link using gold by passing the –with-gold option to the configure script:

$ git clone https://github.com/bitcoin/bitcoin.git
$ cd bitcoin
$ git checkout v25.0
$ cd bitcoin
$ ./autogen.sh
$ ./configure --with-gold
$ time make
real 47m46.288s
user 43m12.872s
sys 3m11.672s

Building Bitcoin Core with gold took 47 minutes and 46 seconds, which was 10 minutes or 17% faster than with ld.

5. Conclusion

In this article, we explored the GNU gold linker. We began by discussing linkers in general, then delved into the specifics of the gold linker.

Afterward, we looked into its history, the reasons for its creation, and compared it with ld.

Finally, we built a project with both ld and gold and compared their build times.