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: panic with cgo export and //go:linkname when main.main not declared #46255

Open
zhuah opened this issue May 19, 2021 · 6 comments
Open
Assignees
Milestone

Comments

@zhuah
Copy link

@zhuah zhuah commented May 19, 2021

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

$ go version
go version go1.16.3 darwin/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="/Users/zhuah/Library/Caches/go-build"
GOENV="/Users/zhuah/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/zhuah/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zhuah/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io,direct"
GOROOT="/usr/local/Cellar/gotip/stable"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/gotip/stable/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8n/mb3y_4bj1xv4q9sdmgjqdx_c0000gn/T/go-build4125710039=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

the sample code is there:

package main

import (
	"C"
	_ "unsafe"
)

//go:linkname mainMain main.main
func mainMain()

//export runGoMain
func runGoMain() {
	mainMain()
}

and i forgot to declare a main function, then build it with go build -buildmode=c-archive

What did you expect to see?

What did you see instead?

go linker panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x2 pc=0x121778b]

goroutine 177 [running]:
cmd/link/internal/amd64.machoreloc1(0x14814e0, 0xc00014cd90, 0xc0004fe000, 0x8776, 0x985c, 0x0, 0xc000080001, 0x166d0, 0x0)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/amd64/asm.go:460 +0x4ab
cmd/link/internal/ld.machorelocsect(0xc00014e000, 0xc00014cd90, 0xc00014c4d0, 0xc001078538, 0x92e, 0x11d5)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/macho.go:1196 +0x3f4
cmd/link/internal/ld.relocSectFn.func1.1(0x12f18e0, 0xc00014e000, 0xc00014cd90, 0xc00014c4d0, 0xc001074000, 0x11d5, 0x11d5, 0xc001067210, 0xc00013a1c0)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/asmb.go:202 +0x66
created by cmd/link/internal/ld.relocSectFn.func1
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/asmb.go:201 +0x1bd
@thanm thanm self-assigned this May 19, 2021
@thanm thanm added this to the Go1.17 milestone May 19, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented May 19, 2021

Change https://golang.org/cl/321130 mentions this issue: cmd/link: fix buglet in machoreloc for undefined main symbol

@zhuah
Copy link
Author

@zhuah zhuah commented May 19, 2021

it's also reproducible in arm64:

➜  ~ CGO_ENABLED=1 GOARCH=arm64 go build a.go
# command-line-arguments
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x2 pc=0x122146b]

goroutine 149 [running]:
cmd/link/internal/arm64.machoreloc1(0x1481620, 0xc00010ed20, 0xc00012a000, 0x80df, 0x90bf, 0x0, 0xc000080001, 0x168d0, 0x0)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/arm64/asm.go:500 +0x7eb
cmd/link/internal/ld.machorelocsect(0xc000110000, 0xc00010ed20, 0xc00010e460, 0xc001162548, 0x961, 0x120a)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/macho.go:1196 +0x3f4
cmd/link/internal/ld.relocSectFn.func1.1(0x12f18e0, 0xc000110000, 0xc00010ed20, 0xc00010e460, 0xc00115e000, 0x120a, 0x120a, 0xc001204f70, 0xc00006c0e0)
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/asmb.go:202 +0x66
created by cmd/link/internal/ld.relocSectFn.func1
        /usr/local/Cellar/gotip/stable/src/cmd/link/internal/ld/asmb.go:201 +0x1bd

@thanm
Copy link
Contributor

@thanm thanm commented May 19, 2021

it's also reproducible in arm64

Thanks. I will update the patch.

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented May 19, 2021

This kind of use of linkname is not supported.

Is there a way of reproduce without using linkname? Thanks.

@cherrymui cherrymui removed this from the Go1.17 milestone May 19, 2021
@cherrymui cherrymui added this to the Unplanned milestone May 19, 2021
@zhuah
Copy link
Author

@zhuah zhuah commented May 20, 2021

yes, i know this is not a valid go program(as i said before, i forgot to write a main function), maybe go could give a better error message instead of panic itself.

unfortunately i couldn't reproduce it without linkname.

@zhuah
Copy link
Author

@zhuah zhuah commented May 20, 2021

by the way, i'm trying to compile a go program as a android/ios library, and call the runGoMain function from java/objective-c, just like golang/mobile repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants