Skip to content
Latchkey

Go "relocation truncated to fit" - Fix in CI

On some architectures the linker uses a code model that assumes symbols sit within a 2 GB window. A very large cgo binary or huge static data can push symbols out of range, truncating the relocation.

What this error means

A cgo link fails with relocation truncated to fit: R_X86_64_PC32 against symbol. A large binary with big static data exceeded the default code-model addressing range.

go
/usr/bin/ld: $WORK/b001/_x002.o: relocation truncated to fit: R_X86_64_PC32 against symbol "bigTable"
collect2: error: ld returned 1 exit status

Common causes

Binary exceeds the small code model

Large static data or a very large cgo binary pushed symbols beyond the 32-bit PC-relative range the default model assumes.

Huge generated C arrays

A generated C blob produced a data section large enough to overflow the addressing window.

How to fix it

Use a larger code model

  1. Pass a large code model to the C compiler via cgo so relocations have room.
Go
// #cgo CFLAGS: -mcmodel=large
import "C"

Reduce static data size

  1. Move large blobs out of the binary (load at runtime) so the data section shrinks below the range.
Go
//go:embed data.bin
var data []byte

How to prevent it

  • Keep large blobs out of the binary; load them at runtime.
  • Use -mcmodel=large for genuinely large native binaries.
  • Watch binary and data-section size growth in CI.

Frequently asked questions

What causes ""relocation truncated to fit""?
Large static data or a very large cgo binary pushed symbols beyond the 32-bit PC-relative range the default model assumes.
How do I fix "relocation truncated to fit"?
Use a larger code model

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card