Farid Zakaria

17 min read

Stop linking by symbol: from O(symbols) to O(store paths)


I spent years thinking about process startup and how to make it faster. I defended my PhD thesis in 20251 for research on store-based systems and dynamic linking optimizations. I was continuously nagged by the idea that software systems, especially store-based ones such as Nix, are incredibly static and do not take advantage of the fact that most of the time, nothing changes.

The thesis generalizes the terms a little bit beyond store-based systems by introducing the notion of epoch-based computation or Stability, where the system’s state is divided into long periods of stability punctuated by discrete management events. In Nix you can think of each immutable store path as having its own epoch: once it is created, it never changes until it is explicitly removed or replaced.

To take advantage of that stability, I built tools that chip away specifically at the dynamic linking overhead:

  1. Shrinkwrap froze library search. Instead of letting ld.so walk the RUNPATH directories one by one, causing a stat storm, Shrinkwrap baked exact library paths into the binary.2

  2. MatR froze symbol resolution. Rather than letting ld.so search for each undefined symbol across every loaded library, MatR pre-computed which library satisfies each relocation once per epoch, making dynamic linking significantly faster.

Even this optimization was not enough to eliminate the overhead of dynamic linking entirely.

I eliminated the need for ld.so to search for symbols but the overhead of iterating over all relocations and patching GOT slots remained.

Aside Although MatR reduces symbol resolution to O(r), the number of undefined symbols can still be very large in real-world binaries, and patching the Global Offset Table (GOT) touches copy-on-write memory pages which incur kernel page faults and can be expensive in terms of performance.

We can shrink dynamic linking down from O(R+L·r·m)3 to O(R+r) with MatR, where R is the number of relative relocations, L is the number of loaded shared libraries, r is the number of undefined named symbols, and m is the symbol name length.

Can we go even faster? What if we swap r for L, the number of libraries?

How much smaller is L compared to r in real-world binaries? You probably expect it to be significantly smaller, since many symbols are resolved by the same store path.

1980-01-01T00:00:00+00:00 image/svg+xml Matplotlib v3.10.5, https://matplotlib.org/ 10 100 1,000 10,000 100,000 git sshd curl python3 firefox (libxul) nix clang qemu-system-riscv64 509 vs 6 (85x) 906 vs 7 (129x) 4,603 vs 16 (288x) 5,296 vs 3 (1,765x) 31,071 vs 56 (555x) 39,823 vs 59 (675x) 48,075 vs 10 (4,808x) 56,111 vs 124 (453x) count (log scale) ELF relocations — O(symbols) store paths — O(store paths)

MatR made the mistake of still linking by symbol, rather than by store path.

MatR had the right insight: in a stable system, the byte offset of every function never changes within a library. The only thing that moves from each process invocation (because of ASLR) is the base address of the library.

We only need 1 pointer per library (store path), not 1 pointer per symbol.

That insight is what storeld builds on: replacing ld.so with a loader that only needs to handle one pointer per store path, rather than one per symbol. Doing so has a dramatic effect on speedup keeping the cost of symbol resolution effectively flat irrespective of the number of symbols.

Curious?

Come along as we explore why linking by store path, rather than by symbol, changes the game entirely.

§Dynamic linking in an attempted nutshell

What actually happens when a process starts and needs to resolve symbols from shared libraries is a bit of a mystery to most. To appreciate the insight behind linking by store path, rather than by symbol, it’s helpful to first understand what dynamic linking actually does under the hood.

Let’s look at a minimal example: a program farm which calls a single function moo() in a shared library libcow.so.

$ llvm-readelf --dynamic --relocs farm
Dynamic section contains 15 entries:
  (RUNPATH)   Library runpath: [/nix/store/lv3h60…-cow-elf-x86_64/lib]  (NEEDED)    Shared library:  [libcow.so]  (GNU_HASH)  0x230  (SYMTAB)    0x200  (STRTAB)    0x264  (JMPREL)    0x2b8  (PLTGOT)    0x3410
Relocation section '.rela.plt' at offset 0x2b8 contains 1 entries:
  Offset            Type                 Symbol's Name + Addend
  0000000000003428  R_X86_64_JUMP_SLOT   moo + 0

Every time Linux runs farm, the kernel loads the dynamic linker ld-linux-x86-64.so.2, whose role is to resolve all dynamic symbols and patch the program’s memory accordingly before main() starts:

  1. Find the libraries. The dynamic linker first reads the RUNPATH and NEEDED entries to determine which shared libraries are required and where to look for them. Shrinkwrap eliminated this step.
  2. Look up every symbol by name. For every relocation, the dynamic linker hashes the symbol name and searches (strcmp) each loaded library in order until it finds a match. MatR eliminated this step.
  3. Apply the relocations. The dynamic linker then writes the resolved addresses into the appropriate locations in the Global Offset Table (GOT), ensuring that all function calls and global variable accesses point to the correct memory addresses.

Each tool has peeled the startup process one phase at a time, reducing the work done by the dynamic linker before main() is reached. The last layer left is the applying of relocations.

ELF + ld.so O(libs × syms) 1. stat storm RUNPATH + NEEDED 2. symbol lookup GNU_HASH + strcmp 3. patch N GOT slots CoW dirty pages main() + Shrinkwrap exact paths skipped 2. symbol lookup GNU_HASH + strcmp 3. patch N GOT slots CoW dirty pages main() + MatR O(symbols) skipped skipped (2.19x) 3. patch N GOT slots still ld.so + CoW main() storeld O(store paths) write M base addrs 1 page · PROT_READ main() Shrinkwrap and MatR skip phases 1 and 2; storeld replaces phase 3 and deletes ld.so.
Four generations of startup linking on an immutable store. Even with symbol lookup eliminated, ELF still binds O(symbols) GOT slots per launch. Linking by store path drops that to O(store paths).

This cost is real. It is one of the reasons hyperscalers like Meta and Google prefer to statically link their binaries.

The cost is so high that glibc even includes the capability to lazily resolve symbols at runtime, meaning that the actual address of a function is only looked up the first time it is called via the Procedure Linkage Table (PLT) rather than at program startup. Lazily binding symbols however has fallen out of favor due to its potential for exploits and unpredictable performance overhead.4

§storeld: a PLT that shares a single base pointer

Understanding the PLT is key to seeing how storeld achieves this reduction in dynamic linking overhead.

The traditional PLT in ELF creates a separate GOT slot for each symbol, which the dynamic linker must populate at runtime. Code jumps to a stub in the PLT, which then reads the final address of the symbol from the corresponding GOT slot.

; ELF today: one GOT slot PER SYMBOL
0000000000001310 <moo@plt>:
    jmp   qword ptr [rip + 0x2112]  ; read final address of moo from GOT[moo]

The PLT stub does not know the actual address of the symbol it is calling; it only knows which GOT slot to read. This indirection is what necessitates a separate GOT entry for each symbol.

If farm calls 500 functions in libcow.so, it will need 500 separate GOT entries, one for each symbol, all of which the dynamic linker must resolve at load time.

storeld flips this on its head: it uses a single base pointer per library and computes the final address of each symbol by adding a build-time constant offset, eliminating the need for per-symbol GOT entries.

; storeld: one slot PER STORE PATH, constant offset baked into read-only code
0000000000000130 <moo>:
    mov   r11, qword ptr [rip + 0xec9]  ; read BASE of libcow from slot[0]    add   r11, 0x150                    ; add build-time constant offset of moo    jmp   r11                           ; tail jump (args & stack untouched)

Crucially, this is compiled from the exact same farm.c and cow.c: zero C source changes are needed.

This is eerily similar to a traditional PLT stub, except that instead of reading a per-symbol GOT entry, it reads a single base pointer and adds a constant offset.

ELF: jmp [GOT[i]] 500 GOT writes at startup fn_0@plt jmp [GOT[0]] fn_1@plt jmp [GOT[1]] ⋮ fn_499@plt jmp [GOT[499]] GOT[0] base + 0x2740 GOT[1] base + 0x2750 500 CoW slots GOT[499] base + 0x4670 fn_0 fn_1 fn_499 ld.so computes base + offset 500 times at startup storeld: jmp ([slot[0]] + const) 1 slot write at startup fn_0 stub [slot[0]] + 0x2740 fn_1 stub [slot[0]] + 0x2750 ⋮ fn_499 stub [slot[0]] + 0x4670 slot[0] base addr 8B · PROT_READ fn_0 fn_1 fn_499 offset baked into read-only code; all 500 stubs share one 8B base pointer
The core insight: ELF's PLT reads a pre-added final address from a separate GOT slot per symbol. storeld's stub reads one shared library base pointer from slot[0] and adds the constant offset in a single instruction.

A natural question is who generates those stubs, and where do they live?

You might assume you have to hack clang or fork ld.lld to emit them, especially given my fondness for hacking on LLVM but you don’t.

storeld generates the stubs at build time as a tiny assembly file and hands it to a stock linker (i.e. ld.lld) to produce a standard Linux ELF64 binary, replacing ld.so with storeld-run. The metadata is stored in a read-only .storeld ELF section.

Integrating it into a Nixpkgs stdenv.mkDerivation is straightforward: just add storeldHook to nativeBuildInputs and the rest is handled automatically.5

farm = pkgs.stdenv.mkDerivation {
  name = "farm-1.0";
  src = ./demo/farm;
  nativeBuildInputs = [ storeldHook ];  buildInputs = [ cow ];
  buildPhase = "$CC -o farm farm.c -lcow";
  installPhase = "install -D -m755 farm $out/bin/farm";
};

How fast is process startup for storeld compared to those traditionally dynamically linked?

Well it obviously depends on the number of symbols. I gave some concrete numbers for the number of relocations you might expect for some sample binaries earlier but let’s see how the performance scales as the number of symbols grows from 1,000 to 200,000.

1980-01-01T00:00:00+00:00 image/svg+xml Matplotlib v3.10.5, https://matplotlib.org/ 0 50 100 150 200 0 10 20 30 40 0 50 100 150 200 0 500 1000 1500 cross-library symbols (thousands, across 40 libraries) Startup latency (ms) Private Anon RAM (KB) ELF + ld.so (BIND_NOW) storeld (PT_INTERP = storeld-run)

Through this scaling experiment, we can observe the dramatic difference in performance between storeld and traditional ELF linking as the number of symbols increases. Both the startup latency and private anonymous RAM usage remain nearly constant for storeld, while they grow significantly for ld.so.

The top-end performance difference is 43.38 ms vs 0.88 ms. To appreciate the speed improvement, here is a fun visual comparison in real time (slowed 100x):

ELF BIND_NOW 43.38 ms storeld 0.88 ms 200,000 symbols across 40 libraries (to scale, 100x slow motion): storeld finishes 49.1x sooner.

§Stability matters

Nix ushered in a new way of building software but we are still reliant on traditional solutions and tooling. We have yet to fully rethink how to leverage the Stability guarantees provided by Nix; dynamic linking is only one piece of the puzzle.

Although many systems offer epoch-based computation, they do not make it so easy to patch the world to modify our software to make better use of these epochs. Changing the dynamic linker and rebuilding every package is table stakes for Nix.

Performance is not the only reason to pursue these advancements. Removing ld.so from the equation also improves security (no IFUNC, no writable GOT) and makes the dependency graph explicit, reducing the attack surface and making it easier to reason about software behavior.

There is likely a cornucopia of opportunities to be had from leveraging the additional metadata stored in the .storeld ELF section, such as improved dependency tracking, more efficient rebuilds, and enhanced security auditing.

The majority of software today presumes complete mutability of everything despite the growing stability guarantees provided by modern package managers and build systems. We need to rethink this assumption and design our software to take full advantage of the immutability and stability offered by these systems.

Static linking throws away sharing. Dynamic linking throws away immutability. On an immutable store, you shouldn’t have to pick.