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.
/usr/bin/ld: $WORK/b001/_x002.o: relocation truncated to fit: R_X86_64_PC32 against symbol "bigTable"
collect2: error: ld returned 1 exit statusCommon 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
- Pass a large code model to the C compiler via cgo so relocations have room.
// #cgo CFLAGS: -mcmodel=large
import "C"Reduce static data size
- Move large blobs out of the binary (load at runtime) so the data section shrinks below the range.
//go:embed data.bin
var data []byteHow 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.