zig icon indicating copy to clipboard operation
zig copied to clipboard

linker error using zig cc with Go tests using race detection (again, __popcountdi2 not defined)

Open misha-ridge opened this issue 2 years ago • 6 comments

Zig Version

0.11.0-dev.1987+a2c6ecd6d

Steps to Reproduce and Observed Behavior

I'm not sure it's not a Go bug again, so please help me investigate.

CC='zig cc' go test -race . fails for me in master build with the failure different from #11398:

# pkg.test
runtime/race/internal/amd64v1(.text): relocation target __popcountdi2 not defined

This is with Go 1.20.1 that includes fix for https://github.com/golang/go/issues/52690

This symbol is present in Go repository in a single file: src/runtime/race/internal/amd64v1/race_linux.syso. Indeed, bumping version of amd64 ISA using GOAMD64=v3 makes this problem disappear.

The repository with reproducer (using Docker to make sure no host libraries can be accidentally linked): https://github.com/misha-ridge/zig-race-test-repo, just clone and docker build . to reproduce.

Expected Behavior

CC='zig cc' go test -race . works.

misha-ridge avatar Mar 17 '23 17:03 misha-ridge

Zig's compiler_rt does provide this symbol, and zig will unconditionally provide compiler_rt when it is used as a linker. I don't know why this symbol would be missing. Perhaps a helpful next step would be to examine the linker line.

andrewrk avatar Apr 10 '23 18:04 andrewrk

It looks like Go does not invoke Zig as linker. Instead it picks up libgcc location and then uses built-in linker for race tests.

Here's the end of the build log (verbosity is enabled in the repo linked above, so can easily be reproduced):

/go/pkg/tool/linux_amd64/link -o $WORK/b001/pkg.test -importcfg $WORK/b001/importcfg.link -installsuffix race -s -w -buildmode=exe -buildid=KcXKitRSSGP6Kl20ppNk/xhtVM94Muq5qURMGvwkx/67v8jA5tmPfILXH-CuN6/KcXKitRSSGP6Kl20ppNk -race -v -extld=/zigcc $WORK/b001/_pkg_.a
# pkg.test
HEADER = -H5 -T0x401000 -R0x1000
[/zigcc] [-m64 --print-libgcc-file-name]
skipping libgcc file: open libgcc.a: no such file or directory
runtime/race/internal/amd64v1(.text): relocation target __popcountdi2 not defined

And indeed zig cc -m64 --print-libgcc-file-name as installed in Dockerfile (download & untar & move to /zig) returns libgcc.a without a path.

misha-ridge avatar Apr 16 '23 10:04 misha-ridge

I got the following error while compiling a Go fyne application with latest Go 1.20 and zig 0.11.0-dev.632+d69e97ae1:

2023-04-24T21:40:59.4077489Z /usr/local/go/pkg/tool/linux_amd64/link: running zig failed: exit status 1
2023-04-24T21:40:59.4077841Z ld.lld: error: undefined symbol: res_search
2023-04-24T21:40:59.4078197Z >>> referenced by cgo-gcc-prolog:60
2023-04-24T21:40:59.4078669Z >>>               /tmp/go-link-2806048561/000005.o:(_cgo_cbcce81e6342_C2func_res_search)
2023-04-24T21:40:59.4079052Z >>> referenced by cgo-gcc-prolog:86
2023-04-24T21:40:59.4079509Z >>>               /tmp/go-link-2806048561/000005.o:(_cgo_cbcce81e6342_Cfunc_res_search)
2023-04-24T21:40:59.4079803Z >>> did you mean: re_search
2023-04-24T21:40:59.4080149Z >>> defined in: /home/docker/.cache/zig/o/551832a6c3cb8d23056bae5f629b485b/libc.so.6

Do you think it could be the same problem? It only affect build for Linux all other OS are fine.

Bluebugs avatar Apr 26 '23 15:04 Bluebugs

@Bluebugs Looks different, in your case the error is emitted by lld, and in mine it is coming from Go's internal linker.

misha-ridge avatar Apr 27 '23 08:04 misha-ridge

Thanks, I will open a new bug report for this then.

Bluebugs avatar Apr 27 '23 16:04 Bluebugs

I got the following error while compiling a Go fyne application with latest Go 1.20 and zig 0.11.0-dev.632+d69e97ae1:

2023-04-24T21:40:59.4077489Z /usr/local/go/pkg/tool/linux_amd64/link: running zig failed: exit status 1
2023-04-24T21:40:59.4077841Z ld.lld: error: undefined symbol: res_search
2023-04-24T21:40:59.4078197Z >>> referenced by cgo-gcc-prolog:60
2023-04-24T21:40:59.4078669Z >>>               /tmp/go-link-2806048561/000005.o:(_cgo_cbcce81e6342_C2func_res_search)
2023-04-24T21:40:59.4079052Z >>> referenced by cgo-gcc-prolog:86
2023-04-24T21:40:59.4079509Z >>>               /tmp/go-link-2806048561/000005.o:(_cgo_cbcce81e6342_Cfunc_res_search)
2023-04-24T21:40:59.4079803Z >>> did you mean: re_search
2023-04-24T21:40:59.4080149Z >>> defined in: /home/docker/.cache/zig/o/551832a6c3cb8d23056bae5f629b485b/libc.so.6

Do you think it could be the same problem? It only affect build for Linux all other OS are fine.

This has been fixed as of https://github.com/ziglang/zig/commit/7e6aeead85c0831b054f095f35677abb49e9b984

Thanks, I will open a new bug report for this then.

There is one unsolved issue in Go when it's using the internal linker, reported in https://github.com/golang/go/issues/59735. May be the same issue.

motiejus avatar Jun 20 '23 07:06 motiejus

This has been fixed as of https://github.com/ziglang/zig/commit/7e6aeead85c0831b054f095f35677abb49e9b984

I upgraded zig to 0.11.0, and still get this error. Anything else I'm missing?

/opt/homebrew/Cellar/go/1.21.0/libexec/pkg/tool/darwin_arm64/link: running zig failed: exit status 1
ld.lld: error: undefined symbol: res_search
>>> referenced by cgo-gcc-prolog:60
>>>               /var/folders/r9/w8fkjcrx65ld6nqr0w34c9kr0000gp/T/go-link-4067459605/000005.o:(_cgo_9c8efe9babca_C2func_res_search)
>>> referenced by cgo-gcc-prolog:86
>>>               /var/folders/r9/w8fkjcrx65ld6nqr0w34c9kr0000gp/T/go-link-4067459605/000005.o:(_cgo_9c8efe9babca_Cfunc_res_search)
>>> did you mean: re_search
>>> defined in: /Users/mazar/.cache/zig/o/82df229a84fc157a4b2cd4530f047e37/libc.so.6

Here is my environment:

go version go1.21.0 darwin/arm64
zig version 0.11.0

mazar avatar Nov 15 '23 17:11 mazar

I'm getting the same error as above with

go version 1.21.1
zig version 0.10.1

momentmaker avatar Jan 18 '24 18:01 momentmaker