Skip to content

go:linkname must refer to declared function or variable #51706

@littlejiancc

Description

@littlejiancc

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

$ go version
go version go1.18 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/wxj/Library/Caches/go-build"
GOENV="/Users/wxj/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/wxj/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/wxj/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io"
GOROOT="/Users/wxj/sdk/go1.18"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/wxj/sdk/go1.18/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/wxj/go/src/demo/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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/85/j24r836d5ygfhjgsn4j5zr0m0000gn/T/go-build178809849=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

$ tree
.
├── 1.s
├── go.mod
├── go.sum
├── inner
│   └── inner.go
└── main.go
$ cat inner.go
package inner

type name struct {
	name string
}

func hello() {
	n := name{
		name: "jack",
	}
	println("hello", n.name)
}

$ cat main.go
package main

import (
	`fmt`

	_ "demo/inner"
	_ "unsafe"
)

//go:linkname hello demo/inner.hello
func hello()

//go:linkname runtimeG runtime.g
type runtimeG struct {
}

func main() {
	hello()
	r := runtimeG{}
	fmt.Println(r)
}

go run main.go

What did you expect to see?

hello jack
{}

What did you see instead?

# command-line-arguments
./main.go:13:3: //go:linkname must refer to declared function or variable

I think runtimeG is declared in main.go, And it works well in version 1.17.8 and 1.16.4

Maybe I'm using it incorrectly, hope it clears my mind, thanks very much!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions