Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
misha-ridge opened this issue Mar 17, 2023 · 8 comments
Labels
bug Observed behavior contradicts documented or intended behavior downstream An issue with a third party project that uses Zig.
Milestone

Comments

@misha-ridge
Copy link

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 golang/go#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 misha-ridge added the bug Observed behavior contradicts documented or intended behavior label Mar 17, 2023
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Apr 10, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Apr 10, 2023
@andrewrk
Copy link
Member

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.

@misha-ridge
Copy link
Author

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.

@Bluebugs
Copy link

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.

@misha-ridge
Copy link
Author

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

@Bluebugs
Copy link

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

@motiejus
Copy link
Contributor

motiejus commented Jun 20, 2023

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 7e6aeea

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 golang/go#59735. May be the same issue.

@andrewrk andrewrk added the downstream An issue with a third party project that uses Zig. label Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.13.0 Jul 22, 2023
@andrewrk andrewrk removed the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Jul 22, 2023
@mazar
Copy link

mazar commented Nov 15, 2023

This has been fixed as of 7e6aeea

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

@momentmaker
Copy link

I'm getting the same error as above with

go version 1.21.1
zig version 0.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior downstream An issue with a third party project that uses Zig.
Projects
None yet
Development

No branches or pull requests

6 participants