Skip to content

cmd/link: __attribute__((weak_import)) is not honored by the internal linker on Darwin #76023

@qmuntal

Description

@qmuntal

Go version

go version go1.25.3 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/qmuntaldiaz/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/qmuntaldiaz/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/6_/bk27w0c510ld88800_hfj6j00000gn/T/go-build1729745112=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/qmuntaldiaz/code/gotest/go.mod'
GOMODCACHE='/Users/qmuntaldiaz/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/qmuntaldiaz/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/qmuntaldiaz/sdk/go1.25.3'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/qmuntaldiaz/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/Users/qmuntaldiaz/sdk/go1.25.3/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Run the following code with go run -ldflags=-linkmode=internal

package main

/*
#cgo LDFLAGS: -Wl,-undefined,dynamic_lookup

extern void __gotest_cgo_null_api(void) __attribute__((weak_import));

int foo(void) {
    if (__gotest_cgo_null_api) return 1;
    return 0;
}
*/
import "C"

func main() {
	println("foo returned:", C.foo())
}

What did you see happen?

The dynamic loader fails to load the Go binary:

go run -ldflags=-linkmode=internal .
dyld[43497]: symbol not found in flat namespace '___gotest_cgo_null_api'
signal: abort trap

What did you expect to see?

The dynamic loader can load the Go binary, and the binary prints 0.

go run -ldflags=-linkmode=internal .
foo returned: 0

Note that this works when using an external linker, and that GCC and LLVM only support __attribute__((weak_import)) on Darwin.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions