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/go: -compiler=gccgo: -buildmode=c-archive can't produce an archive with statically linked libgo #61549

Open
andreybokhanko opened this issue Jul 24, 2023 · 1 comment
Labels
GoCommand cmd/go help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@andreybokhanko
Copy link
Contributor

andreybokhanko commented Jul 24, 2023

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

$ go version
go version go1.18 gollvm LLVM 15.0.0git 20230721 (experimental) linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/abokhanko/.cache/go-build"
GOENV="/home/abokhanko/.config/go/env"
GOEXE=""
GOEXPERIMENT="fieldtrack,regabiwrappers"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/abokhanko/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/abokhanko/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/abokhanko/gollvm/bin"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/abokhanko/gollvm/tools"
GOVCS=""
GOVERSION="go1.18 gollvm LLVM 15.0.0git 20230721 (experimental)"
GCCGO="/home/abokhanko/gollvm/bin/llvm-goc"
GOAMD64="v1"
AR="ar"
CC="/usr/bin/cc"
CXX="/usr/bin/c++"
CGO_ENABLED="1"
GOMOD=""
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2805243357=/tmp/go-build -gno-record-gcc-switches -funwind-tables"

What did you do?

$ cat >example10.go <<.
package main

import "C"
import "fmt"

//export Bar
func Bar() C.int {
fmt.Println("Hello!")
return C.int(1)
}

func main() {}
.
$ cat >example10.c <<.
#include "example10.h"

int main(int argc, char **argv) {
return Bar();
}
.
$ go build -gccgoflags=-static-libgo -buildmode=c-archive example10.go
$ clang example10.c example10.a -lpthread

What did you expect to see?

The program compiles and then executes without errors. This is what happens when using the standard "gc" compiler.

What did you see instead?

$ clang example10.c example10.a -lpthread
/usr/bin/ld: example10.a(a.out.a.o): in function `_cgo_c9ae4c457df7_Cfunc_CString':
./cgo-c-prolog-gccgo:27: undefined reference to `runtime_throw'
...
/usr/bin/ld: example10.a(a.out.a.o):(.data.DW.ref.__gccgo_personality_v0[DW.ref.__gccgo_personality_v0]+0x0): undefined reference to `__gccgo_personality_v0'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The created archive size is much smaller than archive created by "gc" (100634 vs 4876006 bytes on my machine). This clearly indicates that "-gccgoflags=-static-libgo" option got ignored.

@andreybokhanko andreybokhanko changed the title affected/package: gollvm: "-buildmode=c-archive" can't produce an archive with statically linked libgo Jul 24, 2023
@ianlancetaylor ianlancetaylor changed the title gollvm: "-buildmode=c-archive" can't produce an archive with statically linked libgo cmd/go: -compiler=gccgo: -buildmode=c-archive can't produce an archive with statically linked libgo Jul 25, 2023
@gopherbot gopherbot added this to the Gccgo milestone Jul 25, 2023
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels Jul 25, 2023
@ianlancetaylor ianlancetaylor modified the milestones: Gccgo, Backlog Jul 25, 2023
@ianlancetaylor
Copy link
Contributor

There should perhaps be some way to do this, but it won't be via -gccgoflags. That is a flag to pass to gccgo/GoLLVM, but the key step here is not being executed by gccgo/GoLLVM at all.

It may be difficult to implement this. There is a comment in the cmd/go code that says:

		// The user remains responsible for linking against
		// -lgo -lpthread -lm in the final link. We can't use
		// -r to pick them up because we can't combine
		// split-stack and non-split-stack code in a single -r
		// link, and libgo picks up non-split-stack code from
		// libffi.

But it may be possible for cmd/go to use the ar program to add all of libgo.a to the generated archive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted 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

3 participants