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

[darwin x86_64 cgo] regression from 0.9.0-dev.1414+cde3dd365 #10297

Closed
motiejus opened this issue Dec 8, 2021 · 4 comments · Fixed by #10300
Closed

[darwin x86_64 cgo] regression from 0.9.0-dev.1414+cde3dd365 #10297

motiejus opened this issue Dec 8, 2021 · 4 comments · Fixed by #10300
Labels
arch-x86_64 64-bit x86 bug Observed behavior contradicts documented or intended behavior linking os-macos regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@motiejus
Copy link
Contributor

motiejus commented Dec 8, 2021

Zig Version

0.9.0-dev.1920+de81c504b

Steps to Reproduce

0.9.0-dev.1920+de81c504b cannot be used as a CC compiler for cgo (go 1.17), whereas 0.9.0-dev.1414+cde3dd365 can:

hello.go

package main

// #include <stdio.h>
// void helloworld() { printf("hello, world\n"); }
import "C"

func main() {
	C.helloworld()
}

zcc1414

#!/bin/bash
zig=$HOME/code/zig-linux-x86_64-0.9.0-dev.1414+cde3dd365/zig
exec "$zig" cc -target x86_64-macos-gnu "$@"

zcc1920

#!/bin/bash
zig=$HOME/code/zig-linux-x86_64-0.9.0-dev.1920+de81c504b/zig
exec "$zig" cc -target x86_64-macos-gnu "$@"

1414 builds the file successfully:

motiejus ~/x/go $ GOOS=darwin GOARCH=amd64 CC=$PWD/zcc1414 CGO_ENABLED=1 go build -a -buildmode=pie -ldflags "-s -w" hello.go
# command-line-arguments
warning: unsupported linker arg: -S
warning: unsupported linker arg: --compress-debug-sections
warning: unsupported linker arg: zlib-gnu
motiejus ~/x/go $ file hello
hello: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE|HAS_TLV_DESCRIPTORS>
motiejus ~/x/go $ 

Whereas 1920 (yesterday's nightly) doesn't:

motiejus ~/x/go $ GOOS=darwin GOARCH=amd64 CC=$PWD/zcc1920 CGO_ENABLED=1 go build -a -buildmode=pie -ldflags "-s -w" hello.go
# runtime/cgo
/tmp/go-build1027406569/b003/_cgo_import.go:2:3: usage: //go:cgo_import_dynamic local [remote ["library"]]
/tmp/go-build1027406569/b003/_cgo_import.go:4:3: usage: //go:cgo_import_dynamic local [remote ["library"]]
/tmp/go-build1027406569/b003/_cgo_import.go:6:3: usage: //go:cgo_import_dynamic local [remote ["library"]]
motiejus ~/x/go $ echo $?
2

Using go 1.17 linux/amd64.

Expected Behavior

It successfully produces a working binary with 0.9.0-dev.1920+de81c504b.

Actual Behavior

It fails to produce cgo binary.

@motiejus motiejus added the bug Observed behavior contradicts documented or intended behavior label Dec 8, 2021
@motiejus
Copy link
Contributor Author

motiejus commented Dec 8, 2021

0.9.0-dev.1414+cde3dd365 can still be downloaded from https://dl.jakstys.lt/zig/

@motiejus
Copy link
Contributor Author

motiejus commented Dec 8, 2021

Bisecting

~/test

#!/bin/bash
set -xe

pushd $HOME/dev/zig/build
cmake ..
make -j$(nproc)
popd
GOOS=darwin GOARCH=amd64 CC=$HOME/zcc CGO_ENABLED=1 go build -a -buildmode=pie -ldflags "-s -w" $HOME/hello.go

~/zcc

user@motiejus:~/dev/zig$ cat ~/zcc 
#!/bin/bash
zig=$HOME/dev/zig/build/zig
exec "$zig" cc -target x86_64-macos-gnu "$@"

Bisection

$ git bisect bad de81c504b
$ git bisect good cde3dd365
$ git bisect run ~/test
<...>
58a552aaf99f518d3f347c074d20a25c5223404b is the first bad commit
commit 58a552aaf99f518d3f347c074d20a25c5223404b
Author: Jakub Konka <[email protected]>
Date:   Sun Nov 28 20:48:54 2021 +0100

    macho: save all undef symbols even if null

 src/link/MachO.zig | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
bisect run success

@motiejus
Copy link
Contributor Author

motiejus commented Dec 8, 2021

After reverting 58a552a it works again:

user@motiejus:~/dev/zig$ git checkout master
Previous HEAD position was 7e27ab094 macho: fix parsing addend for non-extern SIGNED_X reloc
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
user@motiejus:~/dev/zig$ git revert 58a552aaf99f518d3f347c074d20a25c5223404b
Auto-merging src/link/MachO.zig
[master 35538c4d1] Revert "macho: save all undef symbols even if null"
 1 file changed, 10 insertions(+), 2 deletions(-)
user@motiejus:~/dev/zig$ ~/test
+ pushd /home/user/dev/zig/build
~/dev/zig/build ~/dev/zig
+ cmake ..
Configuring zig version 0.9.0-dev.1922+35538c4d1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/dev/zig/build
++ nproc
+ make -j96
[  1%] Built target opt_c_util
[ 78%] Built target embedded_softfloat
[ 83%] Built target zigcpp
Scanning dependencies of target zigstage1
[ 85%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/codegen.cpp.o
[ 85%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/astgen.cpp.o
[ 85%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/errmsg.cpp.o
[ 87%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/error.cpp.o
[ 87%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/ir.cpp.o
[ 87%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/dump_analysis.cpp.o
[ 87%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/ir_print.cpp.o
[ 88%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/bigfloat.cpp.o
[ 89%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/bigint.cpp.o
[ 90%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/os.cpp.o
[ 92%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/stage1.cpp.o
[ 92%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/analyze.cpp.o
[ 92%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/tokenizer.cpp.o
[ 93%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/target.cpp.o
[ 93%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/parser.cpp.o
[ 94%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/range_set.cpp.o
[ 95%] Building CXX object CMakeFiles/zigstage1.dir/src/stage1/util.cpp.o
[ 96%] Linking CXX static library zigcpp/libzigstage1.a
[ 97%] Built target zigstage1
Scanning dependencies of target zig0
[ 98%] Building CXX object CMakeFiles/zig0.dir/src/stage1/zig0.cpp.o
[ 98%] Linking CXX executable zig0
[ 98%] Built target zig0
[ 99%] Building self-hosted component /home/user/dev/zig/build/zig1.o
[100%] Linking CXX executable zig
[100%] Built target zig
+ popd
~/dev/zig
+ GOOS=darwin
+ GOARCH=amd64
+ CC=/home/user/zcc
+ CGO_ENABLED=1
+ go build -a -buildmode=pie -ldflags '-s -w' /home/user/hello.go
# command-line-arguments
warning: unsupported linker arg: -S
warning: unsupported linker arg: --compress-debug-sections
warning: unsupported linker arg: zlib-gnu
user@motiejus:~/dev/zig$ file hello
hello: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE|HAS_TLV_DESCRIPTORS>
user@motiejus:~/dev/zig$ git log HEAD~2..
commit 35538c4d147874902e4b21604ea1695e2c852aa9 (HEAD -> master)
Author: Motiejus Jakštys <[email protected]>
Date:   Wed Dec 8 09:35:07 2021 +0000

    Revert "macho: save all undef symbols even if null"

    This reverts commit 58a552aaf99f518d3f347c074d20a25c5223404b.

commit 83a668195526df745362b1174bfd643a0cdfb128 (origin/master, origin/HEAD)
Author: Andrew Kelley <[email protected]>
Date:   Tue Dec 7 17:03:29 2021 -0700

    link: fix build for 32-bit targets

    This wasn't caught by the CI checks because this function is is only
    called for the `use_stage1` codepath.

cc @kubkon

@andrewrk andrewrk added this to the 0.9.0 milestone Dec 8, 2021
@andrewrk andrewrk added the regression It worked in a previous version of Zig, but stopped working. label Dec 8, 2021
@motiejus motiejus changed the title [darwin cgo] regression from 0.9.0-dev.1414+cde3dd365 [darwin x86_64 cgo] regression from 0.9.0-dev.1414+cde3dd365 Dec 8, 2021
@andrewrk andrewrk added the arch-x86_64 64-bit x86 label Dec 8, 2021
@kubkon kubkon added the linking label Dec 8, 2021
@kubkon
Copy link
Member

kubkon commented Dec 8, 2021

Thanks for bisecting @motiejus - this has been super helpful in coming up with a patch! The fix is provided in #10300 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-x86_64 64-bit x86 bug Observed behavior contradicts documented or intended behavior linking os-macos regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants