Linker error when building Zig from source

Hi, I’m attempting to build the zig compiler from source, and consistently run into this error:

fatal linker error: unhandled relocation type R_X86_64_PC64

This happens when I build from source using cmake and the system C, Clang, LLVM, etc. I’ve verified that all the dependencies are installed with compatible versions, and reproduced this on multiple commits on master. The failure seems to happen when the stage 2 compiler is building the stage 3 compiler.

[100%] Building stage3
install
└─ install zig
   └─ compile exe zig Debug native 2 errors
error: fatal linker error: unhandled relocation type R_X86_64_PC64 at offset 0x1c
    note: in /usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../lib/crt1.o:.sframe
error: fatal linker error: unhandled relocation type R_X86_64_PC64 at offset 0x2c
    note: in /usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../lib/crt1.o:.sframe
error: 2 compilation errors
failed command: /home/abs/workspace/zig/build/zig2 build-exe -fallow-so-scripts --stack 48234496 /home/abs/workspace/zig/build/zigcpp/libzigcpp.a /usr/lib/libclang-cpp.so.21.1 /usr/lib/liblldMinGW.so /usr/lib/liblldELF.so /usr/lib/liblldCOFF.so /usr/lib/liblldWasm.so /usr/lib/liblldMachO.so /usr/lib/liblldCommon.so -lLLVM-21 /usr/lib/libstdc++.so -lunwind -fno-sanitize-thread -ODebug -I /usr/include -I /usr/include -L /usr/lib --dep aro --dep build_options -Mroot=/home/abs/workspace/zig/src/main.zig -Maro=/home/abs/workspace/zig/lib/compiler/aro/aro.zig -Mbuild_options=.zig-cache/c/c59001929ba6452ad1c9a5223a5cf30b/options.zig -lc --cache-dir .zig-cache --global-cache-dir /home/abs/.cache/zig --name zig --zig-lib-dir lib/ --listen=-

It also happens when I try to build zig-bootstrap, in the final step in the build when we are rebuilding the compiler for the target architecture:

install
└─ install generated to doc/langref.html
   └─ run exe docgen (langref.html)
      └─ WriteFile Assembly Syntax Explained.out
         └─ run exe doctest (verbose_cimport_flag.out) failure
error: fatal linker error: unhandled relocation type R_X86_64_PC64 at offset 0x1c
    note: in /usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../lib/crt1.o:.sframe
error: fatal linker error: unhandled relocation type R_X86_64_PC64 at offset 0x2c
    note: in /usr/lib/gcc/x86_64-pc-linux-gnu/15.2.1/../../../../lib/crt1.o:.sframe

I’ve tried all the following all of the following commands for zig-bootstrap:

  1. ./build native-linux-gnu native
  2. ./build native-linux-gnu znver4
  3. ./build x86_64-linux-gnu znver4
  4. ./build x86_64-linux-musl znver4

I’ve sanity checked that a basic hello world zig program compiles fine when using a prebuilt. I’m running this on an arch based, 64-bit system with an AMD 7700X cpu. Any help figuring this out would be appreciated!

1 Like

Dump of the crt1.o file in case it’s relevant:

❯ objdump -r /usr/lib/crt1.o

/usr/lib/crt1.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000001b R_X86_64_REX_GOTPCRELX  main-0x0000000000000004
0000000000000021 R_X86_64_GOTPCRELX  __libc_start_main-0x0000000000000004


RELOCATION RECORDS FOR [.eh_frame]:
OFFSET           TYPE              VALUE
0000000000000020 R_X86_64_PC32     .text
0000000000000050 R_X86_64_PC32     .text+0x0000000000000030


RELOCATION RECORDS FOR [.sframe]:
OFFSET           TYPE              VALUE
000000000000001c R_X86_64_PC64     .text
000000000000002c R_X86_64_PC64     .text+0x0000000000000030
2 Likes

Thanks! Was going crazy trying to figure this out, glad to hear it’s a known bug. Are there any workarounds / feature flags to disable or is this just a wait for the fix situation?

Using the LLVM backend (-fllvm / .use_llvm = true) should get you unblocked in the meantime.

1 Like