-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/cgo: use --no-gc-sections if available #52815
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
Conversation
This PR (HEAD: 136bf8f) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/405414 to see it. Tip: You can toggle comments from me using the |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Bryan Mills: Patch Set 1: Run-TryBot+1 Code-Review+1 (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: TryBot-Result-1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Bryan Mills: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Bryan Mills: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: TryBot-Result-1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Ian Lance Taylor: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 1: TryBot-Result+1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Bryan Mills: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Motiejus Jakštys: Patch Set 1: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Bryan Mills: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
This PR (HEAD: e42ccc7) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/405414 to see it. Tip: You can toggle comments from me using the |
Message from Motiejus Jakštys: Patch Set 2: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Ian Lance Taylor: Patch Set 2: (5 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
This PR (HEAD: 856a2e0) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/405414 to see it. Tip: You can toggle comments from me using the |
This PR (HEAD: 55004bc) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/405414 to see it. Tip: You can toggle comments from me using the |
Message from Motiejus Jakštys: Patch Set 5: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
As of 742dcba `go build` can accept `$CC` with spaces and quotes, which lets us easily use `zig cc` as the C compiler, or easily pass extra compiler parameters: ``` CC="zig cc" go build <...> CC="clang-13 -v" go build <...> CC="zig cc -Wl,--print-gc-sections" go build <...> ``` However, the same does not apply for building go itself: ``` $ CC="zig cc" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) go tool dist: cannot invoke C compiler "zig cc": exec: "zig cc": executable file not found in $PATH Go needs a system C compiler for use with cgo. To set a C compiler, set CC=the-compiler. To disable cgo, set CGO_ENABLED=0. ``` With this change Go can be built directly with `zig cc` (the linker arg will disappear with golang#52815 and/or golang#52690): ``` $ CC="zig cc -Wl,--no-gc-sections" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) Building Go toolchain1 using /usr/local/go. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1. Building Go toolchain2 using go_bootstrap and Go toolchain1. Building Go toolchain3 using go_bootstrap and Go toolchain2. Building packages and commands for linux/amd64. --- Installed Go for linux/amd64 in /home/motiejus/code/go Installed commands in /home/motiejus/code/go/bin $ ../bin/go version go version devel go1.19-811f1913a8 Thu May 19 09:44:49 2022 +0300 linux/amd64 ``` Fixes golang#52990
clang and gcc accept `-r`, which is a `--relocatable` argument of ld.lld. This diff understands the flag and passes to the linker. Impact: this makes the Go's `cmd/link` test suite pass[*]. To reproduce, do in go/src directory (currently patched, hopefully not for long): ``` echo 'exec zig cc "$@"' > zig-cc; chmod a+x zig-cc CC="./zig-cc" ./run.bash -run cmd/link ``` [*]: at the moment it requires golang/go#52815 to pass
clang and gcc accept `-r`, which is a `--relocatable` argument of ld.lld. This diff understands the flag and passes to the linker. Impact: this makes the Go's `cmd/link` test suite pass[*]. To reproduce, do in go/src directory (currently patched, hopefully not for long): ``` echo 'exec zig cc "$@"' > zig-cc; chmod a+x zig-cc CC="./zig-cc" ./run.bash -run cmd/link ``` [*]: at the moment it requires golang/go#52815 to pass
clang and gcc accept `-r`, which is a `--relocatable` argument of ld.lld. This diff understands the flag and passes to the linker. Impact: this makes the Go's `cmd/link` test suite pass[*]. To reproduce, do in go/src directory (currently patched, hopefully not for long): ``` echo 'exec zig cc "$@"' > zig-cc; chmod a+x zig-cc CC="./zig-cc" ./run.bash -run cmd/link ``` [*]: at the moment it requires golang/go#52815 to pass
Message from Ian Lance Taylor: Patch Set 5: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Ian Lance Taylor: Patch Set 5: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
zig cc passes `--gc-sections` to the underlying linker, which then causes undefined symbol errors when compiling with cgo but without C code. Add `-Wl,--no-gc-sections` to make it work with zig cc. Minimal example: **main.go** package main import _ "runtime/cgo" func main() {} Run (works after the patch, doesn't work before): CC="zig cc" go build main.go Among the existing code, `src/runtime/testdata/testprognet` fails to build: src/runtime/testdata/testprognet$ CC="zig cc" go build . net(.text): relocation target __errno_location not defined net(.text): relocation target getaddrinfo not defined net(.text): relocation target freeaddrinfo not defined net(.text): relocation target gai_strerror not defined runtime/cgo(.text): relocation target stderr not defined runtime/cgo(.text): relocation target fwrite not defined runtime/cgo(.text): relocation target vfprintf not defined runtime/cgo(.text): relocation target fputc not defined runtime/cgo(.text): relocation target abort not defined runtime/cgo(.text): relocation target pthread_create not defined runtime/cgo(.text): relocation target nanosleep not defined runtime/cgo(.text): relocation target pthread_detach not defined runtime/cgo(.text): relocation target stderr not defined runtime/cgo(.text): relocation target strerror not defined runtime/cgo(.text): relocation target fprintf not defined runtime/cgo(.text): relocation target abort not defined runtime/cgo(.text): relocation target pthread_mutex_lock not defined runtime/cgo(.text): relocation target pthread_cond_wait not defined runtime/cgo(.text): relocation target pthread_mutex_unlock not defined runtime/cgo(.text): relocation target pthread_cond_broadcast not defined runtime/cgo(.text): relocation target malloc not defined With the patch both examples build as expected. @ianlancetaylor suggested: > It would be fine with me if somebody wants to send a cgo patch that passes -Wl,--no-gc-sections, with a fallback if that option is not supported. ... and this is what we are doing. Tested with zig 0.10.0-dev.2252+a4369918b Fixes golang#52690
As of CL 334732 `go build` can accept `$CC` with spaces and quotes, which lets us easily use `zig cc` as the C compiler, or easily pass extra compiler parameters: ``` CC="zig cc" go build <...> CC="clang-13 -v" go build <...> CC="zig cc -Wl,--print-gc-sections" go build <...> ``` However, the same does not apply for building go itself: ``` $ CC="zig cc" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) go tool dist: cannot invoke C compiler "zig cc": exec: "zig cc": executable file not found in $PATH Go needs a system C compiler for use with cgo. To set a C compiler, set CC=the-compiler. To disable cgo, set CGO_ENABLED=0. ``` With this change Go can be built directly with `zig cc` (the linker arg will disappear with golang#52815 and/or golang#52690): ``` $ CC="zig cc -Wl,--no-gc-sections" ./make.bash Building Go cmd/dist using /usr/local/go. (go1.18.2 linux/amd64) Building Go toolchain1 using /usr/local/go. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1. Building Go toolchain2 using go_bootstrap and Go toolchain1. Building Go toolchain3 using go_bootstrap and Go toolchain2. Building packages and commands for linux/amd64. --- Installed Go for linux/amd64 in /home/motiejus/code/go Installed commands in /home/motiejus/code/go/bin $ ../bin/go version go version devel go1.19-811f1913a8 Thu May 19 09:44:49 2022 +0300 linux/amd64 ``` Fixes golang#52990
Message from Motiejus Jakštys: Patch Set 5: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
This PR (HEAD: 58406b3) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/405414 to see it. Tip: You can toggle comments from me using the |
Message from Ian Lance Taylor: Patch Set 6: Run-TryBot+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 6: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
Message from Gopher Robot: Patch Set 6: TryBot-Result+1 (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/405414. |
zig cc passes `--gc-sections` to the underlying linker, which then causes undefined symbol errors when compiling with cgo but without C code. Add `-Wl,--no-gc-sections` to make it work with zig cc. Minimal example: **main.go** package main import _ "runtime/cgo" func main() {} Run (works after the patch, doesn't work before): CC="zig cc" go build main.go Among the existing code, `src/runtime/testdata/testprognet` fails to build: src/runtime/testdata/testprognet$ CC="zig cc" go build . net(.text): relocation target __errno_location not defined net(.text): relocation target getaddrinfo not defined net(.text): relocation target freeaddrinfo not defined net(.text): relocation target gai_strerror not defined runtime/cgo(.text): relocation target stderr not defined runtime/cgo(.text): relocation target fwrite not defined runtime/cgo(.text): relocation target vfprintf not defined runtime/cgo(.text): relocation target fputc not defined runtime/cgo(.text): relocation target abort not defined runtime/cgo(.text): relocation target pthread_create not defined runtime/cgo(.text): relocation target nanosleep not defined runtime/cgo(.text): relocation target pthread_detach not defined runtime/cgo(.text): relocation target stderr not defined runtime/cgo(.text): relocation target strerror not defined runtime/cgo(.text): relocation target fprintf not defined runtime/cgo(.text): relocation target abort not defined runtime/cgo(.text): relocation target pthread_mutex_lock not defined runtime/cgo(.text): relocation target pthread_cond_wait not defined runtime/cgo(.text): relocation target pthread_mutex_unlock not defined runtime/cgo(.text): relocation target pthread_cond_broadcast not defined runtime/cgo(.text): relocation target malloc not defined With the patch both examples build as expected. @ianlancetaylor suggested: > It would be fine with me if somebody wants to send a cgo patch that passes -Wl,--no-gc-sections, with a fallback if that option is not supported. ... and this is what we are doing. Tested with zig 0.10.0-dev.2252+a4369918b Fixes #52690 Change-Id: Ib6d1b2bd59335e9663afefd360ddad7da358a938 GitHub-Last-Rev: 58406b3 GitHub-Pull-Request: #52815 Reviewed-on: https://go-review.googlesource.com/c/go/+/405414 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
This PR is being closed because golang.org/cl/405414 has been merged. |
Rolling back the fix, because it broke on the AIX and iOS builders. |
Sorry, I got confused and thought this was an issue rather than a pull request. |
zig cc passes
--gc-sections
to the underlying linker, which thencauses undefined symbol errors when compiling with cgo but without C
code. Add
-Wl,--no-gc-sections
to make it work with zig cc. Minimalexample:
main.go
Run (works after the patch, doesn't work before):
Among the existing code,
src/runtime/testdata/testprognet
fails tobuild:
With the patch both examples build as expected.
@ianlancetaylor suggested:
... and this is what we are doing. Tested with zig
0.10.0-dev.2252+a4369918b
Fixes #52690