Last week, Go 1.23 entered the freeze period, meaning no new features will be added, and any already added features are unlikely to be removed. This is a great opportunity to preview the upcoming changes.
This article is first published in the medium MPP plan. If you are a medium user, please follow me in medium. Thank you very much.
Today, we will discuss the changes to the //go:linkname directive in Go 1.23.
Related issue: GitHub Issue #67401
TL;DR: The //go:linkname directive is not officially recommended and does not guarantee any forward or backward compatibility. It is wise to avoid using it whenever possible.
With that in mind, let’s dive into the new changes and see how they relate to us.
What Does the linkname Directive Do?
In simple terms, the linkname directive is used to pass information to the compiler and linker. Depending on its usage, it can be divided into three categories:
1. Pull
The “pull” usage is as follows:
|
|
This directive format is //go:linkname <local function or package-level variable> <fully defined function or variable in this or another package>. It tells the compiler and linker that my_func should directly use fmt.Println, making my_func an alias for fmt.Println.
This usage allows ignoring whether functions or variables are exported, pulling even package-private elements into use. However, this method is risky and can lead to panics if there’s a type mismatch.
2. Push
The “push” usage looks like this:
|
|
Here, you only need to pass the function or variable name as the first parameter to the directive, specifying the package name where it should be used. This usage signifies that the function or variable will be named xxx.yyy.
3. Handshake
The “handshake” usage combines both methods:
|
|
The pull side remains unchanged, but the push side doesn’t need to specify the package name. This usage implies a handshake between the two ends, clearly marking which function or variable should be linked.
Risks of linkname
The primary risk is the ability to use package-private functions or variables without the package’s knowledge. For example:
|
|
Normally, uintPow shouldn’t be accessible outside its package. But linkname bypasses this restriction, which can lead to severe type-related memory errors or runtime panics.
Positive Aspects of linkname
Despite its risks, linkname exists for valid reasons, such as during the startup of Go programs. For example, in Go’s runtime:
|
|
Here, linkname allows the runtime to call the user-defined main function.
Changes to linkname in Go 1.23
Given the risks, the Go core team has decided to limit linkname usage:
- New standard library packages will prohibit
linkname. - A new ldflag,
-checklinkname=1, has been added to enforce restrictions. It defaults to 0 but will be set to 1 in the final release of 1.23. - Pull-only
linknamewill be prohibited for the standard library, allowing only the handshake mode.
For instance, the following code will no longer compile in 1.23:
|
|
Future of linkname
The long-term goal is to only allow handshake mode. As developers, we should:
- Use
-checklinkname=1to audit and remove unnecessarylinknameusage. - Propose to make private APIs public if necessary.
- As a last resort, disable the restriction with
-ldflags=-checklinkname=0.
Conclusion
In summary, avoid using //go:linkname to prevent unforeseen issues.