Repository: go
Issue: runtime: darwin using -buildmode=c-shared so got runtime: unexpected return pc for runtime.sigpanic called from 0x41e745a
Labels: OS-Darwin,NeedsInvestigation,compiler/runtime

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.14.4 darwin/amd64
</pre>

### Does this issue reproduce with the latest release?
yes


### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/wangzhizhao/Library/Caches/go-build"
GOENV="/Users/wangzhizhao/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY="*.byted.org,*.everphoto.cn"
GONOSUMDB="*.byted.org,*.everphoto.cn"
GOOS="darwin"
GOPATH="/Users/wangzhizhao/go"
GOPRIVATE="*.byted.org,*.everphoto.cn"
GOPROXY="https://go-mod-proxy.byted.org,https://goproxy.cn,https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/wangzhizhao/go/src/goscripts/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/c5/gc9h916j1578f82zvg1hgxf40000gp/T/go-build852002820=/tmp/go-build -gno-record-gcc-switches -fno-common"
</pre></details>


<details><summary><code>uname -a</code> Output</summary><br><pre>
$uname -a
Darwin wangzhizhaodeMacBook-Pro.local 18.7.0 Darwin Kernel Version 18.7.0: Mon Apr 27 20:09:39 PDT 2020; root:xnu-4903.278.35~1/RELEASE_X86_64 x86_64
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->
1. source code is the following two files:
verify.go
```
package main

// #include <stdlib.h>
import "C"

import (
	"encoding/hex"
	"strings"

	"verify/sec"
)

//export Verify
func Verify(serviceNameC *C.char, keyC *C.char) *C.char {
	serviceName := C.GoString(serviceNameC)

	key := []byte(C.GoString(keyC))
	sec.InitWithKey(key)

	res := "res"
	return C.CString(strings.Join([]string{serviceName, res, hex.EncodeToString(key)}, "-"))
}

func main() {

}
```
sec/key.go
```
package sec

import (
	"encoding/hex"
	"fmt"
)

var CryptoKey []byte

func InitWithKey(key []byte) {
	CryptoKey = key
	fmt.Println(hex.EncodeToString(key))
}
```

2. go build genrate c-shared .so
`go build -buildmode=c-shared -a -o libverify.so verify.go`

3. using libverify.so
```
package main

// #cgo LDFLAGS: -lverify
// #include </usr/local/lib/verify.h>
import "C"
import (
	"fmt"
	"unsafe"
)

func freeCString(cString *C.char) {
	C.free(unsafe.Pointer(cString))
}

func Init(serviceName string) {
	cServiceName := C.CString(serviceName)
	defer freeCString(cServiceName)
	key := C.CString("test")
	defer freeCString(key)

	cRes := C.Verify(cServiceName, key)
	defer freeCString(cRes)

	fmt.Println(C.GoString(cRes))
}

func main() {
	Init("my-service")
}
```



### What did you expect to see?
no panic, and print out a string.


### What did you see instead?
```
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x41e745a]

goroutine 1 [running, locked to thread]:
runtime.throw(0x40d653d, 0x2a)
	/usr/local/go/src/runtime/panic.go:1116 +0x72 fp=0xc0000439b0 sp=0xc000043980 pc=0x4030dd2
runtime: unexpected return pc for runtime.sigpanic called from 0x41e745a
stack: frame={sp:0xc0000439b0, fp:0xc0000439e0} stack=[0xc000043000,0xc000044000)
000000c0000438b0:  0000000000000000  0000000000000000
000000c0000438c0:  0000000000000000  0000000000000000
000000c0000438d0:  0000000000000000  0000000000000000
000000c0000438e0:  0000000000000000  0000000000000000
000000c0000438f0:  0000000000000000  0000000000000000
000000c000043900:  0000000000000000  0000000000000000
000000c000043910:  0000000000000000  0000000000000000
000000c000043920:  0000000000000000  0000000000000000
000000c000043930:  0000000000000000  0000000000000000
000000c000043940:  0000000004030f97 <runtime.fatalthrow+87>  000000c000043950
000000c000043950:  000000000405a860 <runtime.fatalthrow.func1+0>  000000c000000180
000000c000043960:  0000000004030dd2 <runtime.throw+114>  000000c000043980
000000c000043970:  000000c0000439a0  0000000004030dd2 <runtime.throw+114>
000000c000043980:  000000c000043988  000000000405a7e0 <runtime.throw.func1+0>
000000c000043990:  00000000040d653d  000000000000002a
000000c0000439a0:  000000c0000439d0  0000000004044f2a <runtime.sigpanic+1130>
000000c0000439b0: <00000000040d653d  000000000000002a
000000c0000439c0:  0000000000000000  0000000000000000
000000c0000439d0:  000000c000043aa8 !00000000041e745a
000000c0000439e0: >0000000000000000  0000000000000000
000000c0000439f0:  0000000000000000  0000000000000000
000000c000043a00:  0000000000000000  0000000000000000
000000c000043a10:  0000000000000000  0000000000000000
000000c000043a20:  000802aaaa00aaaa  0000000000000034
000000c000043a30:  0000000000000020  0000000000000000
000000c000043a40:  0000000000000000  0000000000000000
000000c000043a50:  0000000000000000  0000000000203000
000000c000043a60:  0000000000203000  0000000000203000
000000c000043a70:  0000000000000000  0000000000000000
000000c000043a80:  0000000000000008  0000000000000000
000000c000043a90:  0000000000000000  000000c000043b38
000000c000043aa0:  000000000400d726 <runtime.mallocgc+790>  000000c000043b48
000000c000043ab0:  00000000041df53e  000000c000012200
000000c000043ac0:  0000000000000010  0000000000000010
000000c000043ad0:  0000000004281340  0000000000000680
runtime.sigpanic()
	/usr/local/go/src/runtime/signal_unix.go:679 +0x46a fp=0xc0000439e0 sp=0xc0000439b0 pc=0x4044f2a

goroutine 2 [force gc (idle)]:
runtime.gopark(0x40d7b98, 0x417f670, 0x1411, 0x1)
	/usr/local/go/src/runtime/proc.go:304 +0xe0 fp=0xc000030fb0 sp=0xc000030f90 pc=0x40337f0
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:310
runtime.forcegchelper()
	/usr/local/go/src/runtime/proc.go:253 +0xb7 fp=0xc000030fe0 sp=0xc000030fb0 pc=0x40336a7
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc000030fe8 sp=0xc000030fe0 pc=0x405da91
created by runtime.init.6
	/usr/local/go/src/runtime/proc.go:242 +0x35

goroutine 18 [GC sweep wait]:
runtime.gopark(0x40d7b98, 0x417f7a0, 0x140c, 0x1)
	/usr/local/go/src/runtime/proc.go:304 +0xe0 fp=0xc00002c7a8 sp=0xc00002c788 pc=0x40337f0
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:310
runtime.bgsweep(0xc00008e000)
	/usr/local/go/src/runtime/mgcsweep.go:70 +0x9c fp=0xc00002c7d8 sp=0xc00002c7a8 pc=0x4021a4c
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc00002c7e0 sp=0xc00002c7d8 pc=0x405da91
created by runtime.gcenable
	/usr/local/go/src/runtime/mgc.go:214 +0x5c

goroutine 19 [GC scavenge wait]:
runtime.gopark(0x40d7b98, 0x417f760, 0x140d, 0x1)
	/usr/local/go/src/runtime/proc.go:304 +0xe0 fp=0xc00002cf78 sp=0xc00002cf58 pc=0x40337f0
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:310
runtime.bgscavenge(0xc00008e000)
	/usr/local/go/src/runtime/mgcscavenge.go:237 +0xd0 fp=0xc00002cfd8 sp=0xc00002cf78 pc=0x401ffa0
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc00002cfe0 sp=0xc00002cfd8 pc=0x405da91
created by runtime.gcenable
	/usr/local/go/src/runtime/mgc.go:215 +0x7e

goroutine 3 [finalizer wait]:
runtime.gopark(0x40d7b98, 0x41a8ca8, 0x1410, 0x1)
	/usr/local/go/src/runtime/proc.go:304 +0xe0 fp=0xc000030758 sp=0xc000030738 pc=0x40337f0
runtime.goparkunlock(...)
	/usr/local/go/src/runtime/proc.go:310
runtime.runfinq()
	/usr/local/go/src/runtime/mfinal.go:175 +0xa3 fp=0xc0000307e0 sp=0xc000030758 pc=0x4017773
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc0000307e8 sp=0xc0000307e0 pc=0x405da91
created by runtime.createfing
	/usr/local/go/src/runtime/mfinal.go:156 +0x61
exit status 2
```