package main
import (
_ "unsafe"
)
//go:linkname addmoduledata runtime.addmoduledata
func addmoduledata()
func main() {
addmoduledata()
}
This program can compile ok. However, comment said do NOT call from Go.
https://github.com/golang/go/blob/ebc763f76d1151d8aa3ac1b894b08527999a4938/src/runtime/stubs.go#L427-L428
And using linkname on this function will cause a linker crash when building with buildmode=plugin.
package main
import (
_ "unsafe"
)
//go:linkname addmoduledata runtime.addmoduledata
func addmoduledata()
func FOO() {
addmoduledata()
}
go build -buildmode=plugin a.go
# command-line-arguments
runtime.gcWriteBarrier1: phase error: addr=0x63750 but val=0x5dc00 sym=runtime.gcWriteBarrier1 type=STEXT sect=.text sect.addr=0x0 prev=runtime.addmoduledata
Comment From: gopherbot
Change https://go.dev/cl/699655 mentions this issue: cmd/link: disallow linkname of runtime.addmoduledata
Comment From: gabyhelp
Related Code Changes
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)