Farid Zakaria

20 min read

Huge binaries: one code-model to rule them all


I am such a Nix zealot, I have not written much about the ongoing work on huge binaries I am pursuing through my employment at Meta. Dear Nix fans, please excuse this digression, I am multi-faceted beyond my love for reproducible builds.

The day before my fourth child was born, I had just given a talk at the 2026 LLVM Developers’ Meeting about building massive binaries, alongside Arthur Eubanks. We’ve made significant progress in understanding and addressing the challenges posed by extremely large binaries that exceed the limits of traditional code-models.

Here is a brief overview of the problem and our proposed solution.

Aside For more posts on huge binaries or the challenges of large code-models, check out my previous writings : what a relocation overflow is, why linker thunks are appealing, how much of the toolchain breaks past 2GiB, why thread-local storage is broken under the large code-model

We have designed a new code-model specifically for handling massive binaries, which we call the progressive large code-model.1

The design was done with my colleague Grigory Pastukhov and co-designed with Arthur at Google.

Aside The design was done in the open on public mailing lists and in collaboration with the wider community via a working group.

Personally, I like to call it the “Tolkien code-model”.

One Ring meme. Frodo holds out the One Ring in his open palm. The caption reads "One code-model to rule them all, one GOT to find them, one thunk to bring them all and in the linker bind them".

Most developers never think about code-models, or know by default they use the small code-model, until they hit a confusing relocation overflow. With this new code-model, the option to choose between small, medium, or large disappears.

§Why do code-models exist?

Even though x86-64 has 64-bit pointers, most instructions that reference a function or global variable (call, lea, mov) offer a variant that embeds a signed 32-bit offset relative to the instruction pointer (%rip), keeping machine code compact and fast.

The signed 32-bit offset variants are fast and allow the instruction to reach targets within ±2GiB of its own location, which is sufficient for most programs.

What happens when a target is more than 2GiB away from the instruction that references it? 💥

opcode rel32: four bytes, signed, little-endian the furthest forward a call can encode e8 ff ff ff 7f +2,147,483,647 one byte past 2 GiB relocation overflow e8 00 00 00 80 00 +2,147,483,648 no room
Anything beyond 2 GiB requires more than a 32-bit offset.

Who actually has >2GiB of code?

Hyperscalars! These companies all tend to use a large monorepo with extensive static linking, resulting in massive binaries that can easily exceed 2GiB of code (.text).

Tip For scale, clang with assertions is ~90MB of .text from about 4 million lines of source, so 2GiB is roughly 90 million lines.

Clang .text: 90 MB / ~4M LOC 2 GiB / ~90M LOC (extrapolated · ~22× Clang)
Drawn to scale: filling 2 GiB of .text takes roughly 22 Clangs.

The standard answer for handling large binaries in the x86-64 world is changing the code model, which tells the compiler how to generate addresses and offsets for your program.

small
Everything fits within 2GiB, so 32-bit %rip-relative offsets work everywhere. This is the default.
medium
Code still fits within 2GiB, while large data objects move into separate large sections (.ldata, .lbss, .lrodata) addressed with 64-bit offsets.
large
No size assumptions at all. Every reference to another symbol is built from a 64-bit address.

Google rolled out medium and it bought them some runway by pushing big data tables out of the way. That doesn’t help when your .text itself is what’s blowing past 2GiB.

Great, problem solved? Why not just use -mcmodel=large? 🤔

§The trap of -mcmodel=large

The large code-model is not the panacea that it seems. It has some serious drawbacks and in practice we found it doesn’t even work.

The most obvious drawback is the sheer complexity of the generated code.

extern int counter;
extern void callee(void);
static int hits;

int read_global(void) { return counter; }
int read_static(void) { return hits; }
void call(void) { callee(); callee(); }
; -mcmodel=small -fpie
read_global:
  movq  counter@GOTPCREL(%rip), %rax
  movl  (%rax), %eax
  retq

read_static:
  movl  hits(%rip), %eax
  retq

call:
  pushq %rax
  callq callee@PLT
  popq  %rax
  jmp   callee@PLT
; -mcmodel=large -fpie
read_global:
  leaq    -0x7(%rip), %rax  movabsq $_GLOBAL_OFFSET_TABLE_, %rcx  addq    %rax, %rcx  movabsq $counter@GOT, %rax
  movq    (%rcx,%rax), %rax
  movl    (%rax), %eax
  retq

read_static:
  leaq    -0x7(%rip), %rax  movabsq $_GLOBAL_OFFSET_TABLE_, %rcx  addq    %rax, %rcx  movabsq $hits@GOTOFF, %rax
  movl    (%rcx,%rax), %eax
  retq

call:
  pushq   %rbx
  leaq    -0x7(%rip), %rax  movabsq $_GLOBAL_OFFSET_TABLE_, %rcx  addq    %rax, %rcx  movabsq $callee@GOT, %rax
  movq    (%rcx,%rax), %rbx
  callq   *%rbx
  movq    %rbx, %rax
  popq    %rbx
  jmpq    *%rax

What was only a few instructions in the small code-model becomes a complex sequence of instructions in the large code-model. Although we found CPUs to handle it reasonably well when executing real workloads, we found microbenchmarks to suffer significantly (>20%) in performance.

1980-01-01T00:00:00+00:00 image/svg+xml Matplotlib v3.10.5, https://matplotlib.org/ call a function read an extern global read a static global 0 10 20 30 40 5 9 6 36 36 33 5 9 9 bytes of instructions small large (today) large (proposed)

Looking past the performance regressions, the large code-model has fundamental limitations that make it impractical for real-world use, which to be honest was a big surprise to us.

The two issues that were immediately apparent to us: thread-local storage has no 64-bit form (deeper writeup) and mixing code-models in real binaries is fraught with pitfalls (any real binary will inevitably link prebuilt small objects).

.ltext .lrodata .rodata .eh_frame .text .got .data .bss .ldata .lbss call into large code R_X86_64_PLT32 lea of a large global R_X86_64_PC32 thread-local load through the GOT R_X86_64_GOTTPOFF
The layout lld gives a mixed binary today. Once .ltext grows past 2 GiB, every arrow crossing between the shaded middle and the outside overflows its 32-bit field.

§Progressive large code-model

We set out to solve these problems either by improving the existing large code-model or by designing a new one from scratch.

Turns out that the existing large code-model is so unused that we decided to throw it away and start fresh. We put forward a new design to the psABI community, a very diffuse group that somehow dictates the rules for the ABI, that fixes all of the shortcomings we identified with the current model.

Monty Python and the Holy Grail meme. King Arthur stands over two peasants digging in the mud. The caption reads "Who approves a psABI change? We're an anarcho-syndicalist commune".

The core idea is to stop making the compiler assume the worst up front. We want our cake and eat it too: generate efficient small-code style instructions for the common case without sacrificing the ability to handle large binaries when necessary.

The design comes down to three mechanisms: multiple GOTs, linker thunks, and section demotion.

§Multiple GOTs

One of the advantages of the small code-model is that it can use compact, %rip-relative addressing for most symbol references via the Global Offset Table (GOT). The linker often relaxes these references when it can determine that the target is within reach, eliminating the need for a GOT slot entirely.

; far: a load from the GOT
movq counter@GOTPCREL(%rip), %rax
movl (%rax), %eax
; near: relaxed by the linker
leaq counter(%rip), %rax
movl (%rax), %eax

This works well as long as all the code and data are within ±2GiB.

The challenge arises when binaries grow beyond this limit, which is where multiple GOTs come into play.

We can have the linker create additional GOTs as needed partitioned by the code sections. As it lays out .ltext, roughly every 1.5GiB it splits the section and drops in a fresh GOT (.got.ltext.0, .got.ltext.1, and so on).

Each chunk of code uses the GOT sitting right beside it, so a GOT is always within 2GiB.

≤ 1.5 GiB ≤ 1.5 GiB .ltext .ltext.1 .ltext.2 .rodata .text .got .data .bss .ldata .lbss .got.ltext.0 .got.ltext.1 own PT_GNU_RELRO own PT_GNU_RELRO
The proposed layout. Each chunk of .ltext loads addresses from the GOT beside it; a symbol referenced from two chunks simply gets a slot in both.

This solves our threshold problem for GOT reachability and the Thread Local Storage (TLS) R_X86_64_GOTTPOFF lack of reach simultaneously.2

Aside Dynamic linkers set each GOT as read-only after startup, respecting the PT_GNU_RELRO segments. We have had to update dynamic linkers, such as glibc, to handle multiple PT_GNU_RELRO segments correctly.

§Zero-register linker thunks

Other architecutres have already faced the issue of limited branch reach and have implemented trampoline (a thunk) mechanisms to bridge distant calls. Aarch64, for example, has a 128MiB branch reach limit and relies on thunks for calls beyond that range.

We’ve adopted a similar approach for x86-64, introducing zero-register linker thunks to handle calls that exceed the 2GiB reach limit. The beauty of this approach is it pairs well with the multi-GOT layout, ensuring that a nearby GOT slot is always available for the thunk to use.

__X86_64Thunk_far_function:
  jmpq  *far_function@GOTPCREL(%rip)    # ff 25 <rel32>, 6 bytes
target within 2 GiB: nothing changes call near_function e8 rel32 · 5 bytes near_function: target beyond 2 GiB: the linker inserts a thunk call far_function e8 rel32 · still 5 bytes 1 __X86_64Thunk_far_function jmp *slot(%rip) · 6 bytes reads .got.ltext.0 &far_function · 8 bytes 2 more than 2 GiB far_function:
A call within 2 GiB is untouched and only calls that actually cross 2 GiB pay an indirect jump.

Crucially, this touches zero registers. It turns out that on x86-64, even clobbering “scratch registers” like %r10 or %r11 isn’t safe everywhere (%r10 carries the static chain for nested functions, and split functions under -fbasic-block-sections expect every register to survive a jump). Going through the GOT avoids that problem completely.

Why not just compile with -fno-plt so every call goes through the GOT? Indirect calls are 1 byte longer than direct calls, leaving a wasted padding byte at every relaxed call site.

§Mixing code-models

In real binaries, prebuilt small objects still end up holding direct 32-bit references (R_X86_64_PC32) to symbols defined inside large sections. This happens constantly, whether from third-party libraries and hand-written assembly, ThinLTO marking declarations dso_local, or COMDAT inline functions where the linker picks the large copy over the small one.

Grigory had noticed that we could safely mix small and large code models by demoting large sections that are referenced from small code. This way, the small code can continue using direct 32-bit references, and the large code can still reach the demoted sections through its GOT.3

Effectively, whenever the linker sees a 32-bit relocation touching a large section, it demotes that section: it clears SHF_X86_64_LARGE and moves it into the small region (.ltext → .text, .ldata → .data), right next to the small code that needs it.

before .ltext small sections .ldata shared() caller table 32-bit relocations, out of range after demotion .ltext small sections .ldata caller shared() table SHF_X86_64_LARGE cleared .ltext → .text, .ldata → .data
Demotion moves the referenced section into the small cluster and leaves the small object alone.

In practice, we found that the amount of demoted code and data is tiny, so it doesn’t threaten the 2GiB small budget. This was incredibly helpful since it allowed us to adopt the new code model much quicker without having to recompile all of our existing third-party code.

§To infinity and beyond

This work is happening in the open. You can check out the current prototype branch.

There is a long-tail, as with any large ambitious project, in ironing out all the remaining edge cases: linker scripts and all the userland tooling that interacts with the new code model. Everything expects there to only be one GOT or to help disassemble through the thunking layers correctly.

It’s been very rewarding to see different stakeholders engage in the problem via the LLVM Massive Binaries working group. What started as a discussion betwen two companies has grown into a broader community effort with members from Meta, Google, ByteDance, Arm, CHERI, and OxCaml, among others.

I have thought about maybe seeing what the largest real-world binaries we can build with this new code model are and submitting them to Guinness World Records.

Although we are removing the theoretical limit for linking large binaries, it remains to be seen how large in practice we can push real-world binaries before hitting other bottlenecks.

Engineering is always about removing one bottleneck and having it bubble up to the next one.