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

cmd/cgo: use --no-gc-sections if available #53028

Closed
wants to merge 1 commit into from

Commits on May 25, 2022

  1. cmd/cgo: use --no-gc-sections if available

    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
    
    This is a continuation of CL 405414: the original one broke AIX and iOS
    builds. To fix that, added `unknown option` to the list of strings
    under lookup.
    
    Fixes golang#52690
    motiejus committed May 25, 2022
    Configuration menu
    Copy the full SHA
    86f227a View commit details
    Browse the repository at this point in the history