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: crash on custom scenario #56685

Closed
aviramha opened this issue Nov 10, 2022 · 4 comments
Closed

cmd/link: crash on custom scenario #56685

aviramha opened this issue Nov 10, 2022 · 4 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.
Milestone

Comments

@aviramha
Copy link

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

go version go1.19.2 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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/azureuser/.cache/go-build"
GOENV="/home/azureuser/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/azureuser/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/azureuser/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/azureuser/aviram/going/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/azureuser/aviram/going/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.3"
GCCGO="/usr/bin/gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/azureuser/aviram/going/go-detour/go.mod"
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3760260815=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I tried to create a c-archive that links to Go symbols used by it in runtime, instead of including the whole runtime/go stdlib.
This happens even on the simple program:

package main
func main() {
}

Then compile steps:
go tool compile -shared -dynlink main.go
^^ This creates a file that seems correct.
go tool link -buildmode=c-archive -extld clang -extldflags='-undefined dynamic_lookup' -w -installsuffix /tmp -o main.a main.o

What did you expect to see?

Output fail main.a includes only wanted functions, which I can further link later on to a binary.

What did you see instead?

warning: unable to find runtime.a
warning: unable to find runtime/cgo.a
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
cmd/link/internal/ld.(*Link).findfunctab(0xc0000fc000?, 0x67ec63?, {0xc0000183b0?, 0x14?, 0xc0000e0095?})
 /usr/local/go/src/cmd/link/internal/ld/pcln.go:827 +0x28f
cmd/link/internal/ld.Main(_, {0x20, 0x20, 0x1, 0x7, 0x10, 0x0, {0x0, 0x0}, {0x6886d9, ...}, ...})
 /usr/local/go/src/cmd/link/internal/ld/main.go:330 +0x133e
main.main()
 /usr/local/go/src/cmd/link/main.go:72 +0x1145
@aviramha aviramha changed the title affected/package: go/tool/link affected/package: go/tool/link crash on custom scenario Nov 10, 2022
@mengzhuo mengzhuo changed the title affected/package: go/tool/link crash on custom scenario cmd/link: crash on custom scenario Nov 10, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 10, 2022
@ianlancetaylor
Copy link
Contributor

Well, it shouldn't crash, but you will still get errors. Using go tool compile and go tool link directly is unlikely to ever work. You need to use things like go build -ldflags.

In general what you are trying to do is not supported and I would be surprised if it works at all. But sometimes I am surprised.

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 10, 2022
@mknyszek mknyszek added this to the Backlog milestone Nov 10, 2022
@mknyszek
Copy link
Contributor

CC @golang/compiler

Looks like there isn't really anything actionable except to see if this crash is worth fixing.

@cherrymui
Copy link
Member

go tool compile -shared -dynlink main.go

If you want to create a c-archive, I think you don't want to pass the -dynlink flag.

Output fail main.a includes only wanted functions, which I can further link later on to a binary.

I'm not sure I understand this. Are you expecting the linker to fail?

@gopherbot
Copy link

Change https://go.dev/cl/449638 mentions this issue: cmd/link: exit if runtime.a is missing

@cherrymui cherrymui modified the milestones: Backlog, Unplanned Nov 10, 2022
@golang golang locked and limited conversation to collaborators Nov 11, 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