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/link: fails to statically link with musl #56386

Closed
motiejus opened this issue Oct 24, 2022 · 3 comments
Closed

cmd/link: fails to statically link with musl #56386

motiejus opened this issue Oct 24, 2022 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@motiejus
Copy link
Contributor

What version of Go are you using (go version)?

$ /code/go/bin/go version
go version devel go1.20-7e72d384d6 Mon Oct 24 03:45:54 2022 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ CC="musl-gcc -static" /code/go/bin/go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/motiejus/.cache/go-build"
GOENV="/home/motiejus/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/motiejus/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/motiejus/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/code/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/code/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.20-7e72d384d6 Mon Oct 24 03:45:54 2022 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="musl-gcc -static"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/motiejus/x/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS=". -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build790367339=/tmp/go-build -gno-record-gcc-switches"

What did you do?

main.go

package main

import (
        "fmt"
        _ "os/user"
)

func main() {
        fmt.Println("this should work with cgo too")
}

Build with musl-gcc:

$ CC="musl-gcc -static" /code/go/bin/go build -x .
WORK=/tmp/go-build1223769270
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile example.org/x=/home/motiejus/.cache/go-build/09/09a091d010c2c57fec7896b790b5fc703959a7b1a818b6e71daa2b1ceae52b06-d
packagefile fmt=/code/go/pkg/linux_amd64/fmt.a
packagefile os/user=/home/motiejus/.cache/go-build/43/43be38a31537a79192834a9ae86110883ac61fe8e3521ce0d1ca686a8c5d3051-d
packagefile runtime=/code/go/pkg/linux_amd64/runtime.a
packagefile errors=/code/go/pkg/linux_amd64/errors.a
packagefile internal/fmtsort=/code/go/pkg/linux_amd64/internal/fmtsort.a
packagefile io=/code/go/pkg/linux_amd64/io.a
packagefile math=/code/go/pkg/linux_amd64/math.a
packagefile os=/code/go/pkg/linux_amd64/os.a
packagefile reflect=/code/go/pkg/linux_amd64/reflect.a
packagefile sort=/code/go/pkg/linux_amd64/sort.a
packagefile strconv=/code/go/pkg/linux_amd64/strconv.a
packagefile sync=/code/go/pkg/linux_amd64/sync.a
packagefile unicode/utf8=/code/go/pkg/linux_amd64/unicode/utf8.a
packagefile strings=/code/go/pkg/linux_amd64/strings.a
packagefile syscall=/code/go/pkg/linux_amd64/syscall.a
packagefile runtime/cgo=/home/motiejus/.cache/go-build/41/411699e891b30b89b7c18665d54941f0463412f39d4dabb985bf58bd4c8df4a3-d
packagefile internal/abi=/code/go/pkg/linux_amd64/internal/abi.a
packagefile internal/bytealg=/code/go/pkg/linux_amd64/internal/bytealg.a
packagefile internal/coverage/rtcov=/code/go/pkg/linux_amd64/internal/coverage/rtcov.a
packagefile internal/cpu=/code/go/pkg/linux_amd64/internal/cpu.a
packagefile internal/goarch=/code/go/pkg/linux_amd64/internal/goarch.a
packagefile internal/goexperiment=/code/go/pkg/linux_amd64/internal/goexperiment.a
packagefile internal/goos=/code/go/pkg/linux_amd64/internal/goos.a
packagefile runtime/internal/atomic=/code/go/pkg/linux_amd64/runtime/internal/atomic.a
packagefile runtime/internal/math=/code/go/pkg/linux_amd64/runtime/internal/math.a
packagefile runtime/internal/sys=/code/go/pkg/linux_amd64/runtime/internal/sys.a
packagefile runtime/internal/syscall=/code/go/pkg/linux_amd64/runtime/internal/syscall.a
packagefile internal/reflectlite=/code/go/pkg/linux_amd64/internal/reflectlite.a
packagefile math/bits=/code/go/pkg/linux_amd64/math/bits.a
packagefile internal/itoa=/code/go/pkg/linux_amd64/internal/itoa.a
packagefile internal/poll=/code/go/pkg/linux_amd64/internal/poll.a
packagefile internal/syscall/execenv=/code/go/pkg/linux_amd64/internal/syscall/execenv.a
packagefile internal/syscall/unix=/code/go/pkg/linux_amd64/internal/syscall/unix.a
packagefile internal/testlog=/code/go/pkg/linux_amd64/internal/testlog.a
packagefile io/fs=/code/go/pkg/linux_amd64/io/fs.a
packagefile sync/atomic=/code/go/pkg/linux_amd64/sync/atomic.a
packagefile time=/code/go/pkg/linux_amd64/time.a
packagefile internal/unsafeheader=/code/go/pkg/linux_amd64/internal/unsafeheader.a
packagefile unicode=/code/go/pkg/linux_amd64/unicode.a
packagefile internal/race=/code/go/pkg/linux_amd64/internal/race.a
packagefile internal/oserror=/code/go/pkg/linux_amd64/internal/oserror.a
packagefile path=/code/go/pkg/linux_amd64/path.a
modinfo "0w\xaf\f\x92t\b\x02A\xe1\xc1\a\xe6\xd6\x18\xe6path\texample.org/x\nmod\texample.org/x\t(devel)\t\nbuild\t-buildmode=exe\nbuild\t-compiler=gc\nbuild\tCGO_ENABLED=1\nbuild\tCGO_CFLAGS=\nbuild\tCGO_CPPFLAGS=\nbuild\tCGO_CXXFLAGS=\nbuild\tCGO_LDFLAGS=\nbuild\tGOARCH=amd64\nbuild\tGOOS=linux\nbuild\tGOAMD64=v1\n\xf92C1\x86\x18 r\x00\x82B\x10A\x16\xd8\xf2"
EOF
mkdir -p $WORK/b001/exe/
cd .
/code/go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=unzWWhRe5xdecsY67OX2/yWJsVVSrfI_s_-_c53nJ/mVyyAhIdsBST9cqTAFQm/unzWWhRe5xdecsY67OX2 "-extld=musl-gcc -static" /home/motiejus/.cache/go-build/09/09a091d010c2c57fec7896b790b5fc703959a7b1a818b6e71daa2b1ceae52b06-d
# example.org/x
runtime.gcdata: missing Go type information for global symbol .dynsym: size 72
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_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 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 strerror not defined
runtime/cgo(.text): relocation target stderr not defined
runtime/cgo(.text): relocation target fprintf not defined
runtime/cgo(.text): relocation target abort not defined
runtime/cgo(.text): relocation target free not defined
runtime/cgo(.text): relocation target malloc not defined
runtime/cgo(.text): relocation target pthread_attr_init not defined
runtime/cgo(.text): relocation target pthread_attr_getstacksize not defined
/code/go/pkg/tool/linux_amd64/link: too many errors
Build with `zig-cc`: same error.
$ CC="zig cc -target x86_64-linux-musl"  /code/go/bin/go build -x .
WORK=/tmp/go-build1066116826
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile example.org/x=/home/motiejus/.cache/go-build/79/791eac43f9edf62463a660eb6c62404a2f779a1401cf3256825462c7e1747747-d
packagefile fmt=/code/go/pkg/linux_amd64/fmt.a
packagefile os/user=/home/motiejus/.cache/go-build/1f/1fe980c0217338b361b695538f7a67211a21fa926063c4a55e6c77c61208fc15-d
packagefile runtime=/code/go/pkg/linux_amd64/runtime.a
packagefile errors=/code/go/pkg/linux_amd64/errors.a
packagefile internal/fmtsort=/code/go/pkg/linux_amd64/internal/fmtsort.a
packagefile io=/code/go/pkg/linux_amd64/io.a
packagefile math=/code/go/pkg/linux_amd64/math.a
packagefile os=/code/go/pkg/linux_amd64/os.a
packagefile reflect=/code/go/pkg/linux_amd64/reflect.a
packagefile sort=/code/go/pkg/linux_amd64/sort.a
packagefile strconv=/code/go/pkg/linux_amd64/strconv.a
packagefile sync=/code/go/pkg/linux_amd64/sync.a
packagefile unicode/utf8=/code/go/pkg/linux_amd64/unicode/utf8.a
packagefile strings=/code/go/pkg/linux_amd64/strings.a
packagefile syscall=/code/go/pkg/linux_amd64/syscall.a
packagefile runtime/cgo=/home/motiejus/.cache/go-build/88/88af8bba10f8f3ade3a14acf713878938add8d5947bea76f55487f0e33d44be9-d
packagefile internal/abi=/code/go/pkg/linux_amd64/internal/abi.a
packagefile internal/bytealg=/code/go/pkg/linux_amd64/internal/bytealg.a
packagefile internal/coverage/rtcov=/code/go/pkg/linux_amd64/internal/coverage/rtcov.a
packagefile internal/cpu=/code/go/pkg/linux_amd64/internal/cpu.a
packagefile internal/goarch=/code/go/pkg/linux_amd64/internal/goarch.a
packagefile internal/goexperiment=/code/go/pkg/linux_amd64/internal/goexperiment.a
packagefile internal/goos=/code/go/pkg/linux_amd64/internal/goos.a
packagefile runtime/internal/atomic=/code/go/pkg/linux_amd64/runtime/internal/atomic.a
packagefile runtime/internal/math=/code/go/pkg/linux_amd64/runtime/internal/math.a
packagefile runtime/internal/sys=/code/go/pkg/linux_amd64/runtime/internal/sys.a
packagefile runtime/internal/syscall=/code/go/pkg/linux_amd64/runtime/internal/syscall.a
packagefile internal/reflectlite=/code/go/pkg/linux_amd64/internal/reflectlite.a
packagefile math/bits=/code/go/pkg/linux_amd64/math/bits.a
packagefile internal/itoa=/code/go/pkg/linux_amd64/internal/itoa.a
packagefile internal/poll=/code/go/pkg/linux_amd64/internal/poll.a
packagefile internal/syscall/execenv=/code/go/pkg/linux_amd64/internal/syscall/execenv.a
packagefile internal/syscall/unix=/code/go/pkg/linux_amd64/internal/syscall/unix.a
packagefile internal/testlog=/code/go/pkg/linux_amd64/internal/testlog.a
packagefile io/fs=/code/go/pkg/linux_amd64/io/fs.a
packagefile sync/atomic=/code/go/pkg/linux_amd64/sync/atomic.a
packagefile time=/code/go/pkg/linux_amd64/time.a
packagefile internal/unsafeheader=/code/go/pkg/linux_amd64/internal/unsafeheader.a
packagefile unicode=/code/go/pkg/linux_amd64/unicode.a
packagefile internal/race=/code/go/pkg/linux_amd64/internal/race.a
packagefile internal/oserror=/code/go/pkg/linux_amd64/internal/oserror.a
packagefile path=/code/go/pkg/linux_amd64/path.a
modinfo "0w\xaf\f\x92t\b\x02A\xe1\xc1\a\xe6\xd6\x18\xe6path\texample.org/x\nmod\texample.org/x\t(devel)\t\nbuild\t-buildmode=exe\nbuild\t-compiler=gc\nbuild\tCGO_ENABLED=1\nbuild\tCGO_CFLAGS=\nbuild\tCGO_CPPFLAGS=\nbuild\tCGO_CXXFLAGS=\nbuild\tCGO_LDFLAGS=\nbuild\tGOARCH=amd64\nbuild\tGOOS=linux\nbuild\tGOAMD64=v1\n\xf92C1\x86\x18 r\x00\x82B\x10A\x16\xd8\xf2"
EOF
mkdir -p $WORK/b001/exe/
cd .
/code/go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=BHiKWR6kP0Jzq1hd_E8e/KGeKpNSsfTwlpos4AQJl/mVyyAhIdsBST9cqTAFQm/BHiKWR6kP0Jzq1hd_E8e "-extld=zig cc -target x86_64-linux-musl" /home/motiejus/.cache/go-build/79/791eac43f9edf62463a660eb6c62404a2f779a1401cf3256825462c7e1747747-d
# example.org/x
runtime.gcdata: missing Go type information for global symbol .dynsym: size 72
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
runtime/cgo(.text): relocation target __errno_location not defined
runtime/cgo(.text): relocation target strerror not defined
runtime/cgo(.text): relocation target pthread_attr_init not defined
/code/go/pkg/tool/linux_amd64/link: too many errors

What did you expect to see?

The program successfully builds.

@motiejus motiejus changed the title cgo/cgo: failed to link with musl cgo/cgo: fails to link with musl Oct 24, 2022
@motiejus motiejus changed the title cgo/cgo: fails to link with musl cgo/cgo: fails to statically link with musl Oct 24, 2022
@seankhliao seankhliao changed the title cgo/cgo: fails to statically link with musl cmd/link: fails to link with musl Oct 24, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 24, 2022
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 24, 2022
@cherrymui
Copy link
Member

Does it fail without -static? Does it fail if you pass -ldflags=-linkmode=external? Does it fail with previous versions of Go? Thanks.

@motiejus motiejus changed the title cmd/link: fails to link with musl cmd/link: fails to statically link with musl Oct 25, 2022
@motiejus
Copy link
Contributor Author

Does it fail without -static?

It does not, but it dynamically links, which is undesirable. I've updated the title.

Does it fail if you pass -ldflags=-linkmode=external?

It works with -ldflags=-linkmode=external! I forgot about that flag completely, thanks!

Is it still an "issue" if can be linked with an external linker?

Does it fail with previous versions of Go?

Did not test, but I guess it's not quite relevant now.

@mknyszek
Copy link
Contributor

In triage it looks like there isn't much to do here. Closing optimistically, @motiejus please let us know if you think anything else should be done here. Thanks!

@golang golang locked and limited conversation to collaborators Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants