Skip to content

cmd/link: panic with cgo export and //go:linkname when main.main not declared  #46255

Open
@zhuah

Description

@zhuah

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions