ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Home Page:https://ziglang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

motiejus opened this issue · comments

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.

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

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 <kubkon@jakubkonka.com>
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

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 <motiejus@uber.com>
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 <andrew@ziglang.org>
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

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