How to export internal runtime functions using go:linkname to bypass linkname checks in Go 1.23?

152 views
Skip to first unread message

zhengyao xie

unread,
Dec 26, 2024, 1:35:08 AM12/26/24
to golang-nuts
I would like to ask everyone for your help. In external code, I used `//go:linkname` directive as follows:

```go
//go:linkname runtime_setEventErr runtime.(*pollDesc).setEventErr
func runtime_setEventErr(pd runtimePollDesc, b bool, seq uintptr)
```
The `runtime.(*pollDesc).setEventErr` function has been linked using linkname. It works well before go1.23, but since [go1.23 lock down future uses of linkname](https://github.com/golang/go/issues/67401), it can't work. I want to export this function within a private go runtime to remove the linkname check. How should I modify it? I have tried several ways but none of them seem to work:

```go
//go:linkname setEventErr
func (pd *pollDesc) setEventErr(b bool, seq uintptr)

//go:linkname (*pollDesc).setEventErr
func (pd *pollDesc) setEventErr(b bool, seq uintptr)

//go:linkname runtime.(*pollDesc).setEventErr
func (pd *pollDesc) setEventErr(b bool, seq uintptr)
```

```bash
cd go/src && ./make.bash
```

The error log is:

```
/usr/local/go/src/runtime/netpoll.go:178:3: //go:linkname must refer to declared function or variable
```

Kurtis Rader

unread,
Dec 26, 2024, 2:09:07 AM12/26/24
to zhengyao xie, golang-nuts
This seems like an XY Problem. Why are you depending on a private implementation detail of the Go runtime? What happens to your code if that function is removed or its behavior changes? There is probably a better solution than creating that dependency. If nothing else by keeping it private rather than exporting the symbol.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/a7ba5ba3-31b0-465b-9706-12c528d0a098n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Ian Lance Taylor

unread,
Dec 26, 2024, 3:22:30 PM12/26/24
to zhengyao xie, golang-nuts
Build with "go build -ldflags=-checklinkname=0".

Ian
Reply all
Reply to author
Forward
0 new messages