Skip to content

Commit e42ccc7

Browse files
committed
cmd/cgo: use -Wl,--no-gc-sections if available
This change adds `--no-gc-sections` to the external linker if the flag is understood. zig cc passes `--gc-sections` to the underlying linker, which then causes undefined symbol errors when compiling with cgo but without C code. 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 too: ``` 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
1 parent d8ac8dd commit e42ccc7

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,13 @@ func (b *Builder) compilerCmd(compiler []string, incdir, workdir string) []strin
25282528
a = append(a, "-Qunused-arguments")
25292529
}
25302530

2531+
// zig cc passes --gc-sections to the underlying linker, which then causes
2532+
// undefined symbol errors when compiling with cgo but without C code.
2533+
// https://github.com/golang/go/issues/52690
2534+
if b.gccSupportsLinkerFlag(compiler, "-Wl,--no-gc-sections") {
2535+
a = append(a, "-Wl,--no-gc-sections")
2536+
}
2537+
25312538
// disable word wrapping in error messages
25322539
a = append(a, "-fmessage-length=0")
25332540

0 commit comments

Comments
 (0)