Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: go:linkname does not work with dot in importpath #67361

Closed
jiping-s opened this issue May 14, 2024 · 4 comments
Closed

cmd/compile: go:linkname does not work with dot in importpath #67361

jiping-s opened this issue May 14, 2024 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@jiping-s
Copy link

jiping-s commented May 14, 2024

Go version

go1.22.1 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/myname/Library/Caches/go-build'
GOENV='/Users/myname/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/myname/.go/pkg/mod'
GONOPROXY='*'
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/myname/.go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/myname/.go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/n3/rjz31n_50516xs92p6gswgsr0000gp/T/go-build174368253=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Use go linkname with gopkg.in/yaml.v3 or gopkg.in/yaml.v2

playground: https://go.dev/play/p/1yYtFVvGnfn

package main

import (
	"fmt"
	_ "unsafe"

	_ "gopkg.in/yaml.v2"
)

//go:linkname newEncoder gopkg.in/yaml.v2.newEncoder
func newEncoder() any

func main() {
	newEncoder()
}

What did you see happen?

relocation target gopkg.in/yaml.v2.newEncoder not defined

What did you expect to see?

The symbol is imported and can be called.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 14, 2024
jiping-s added a commit to relex/slog-agent that referenced this issue May 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Does not work. See golang/go#67361
@mauri870
Copy link
Member

The go:linkname directive is not part of the language and its use in user code is highly discouraged. It can break at any time without warning. From what I see in your example you can just import the package as-is, there is no need to use linkname, plus I'm not sure if it even works with external packages, I have mainly seen its use with internal Go libraries.

@seankhliao
Copy link
Member

see #63846 (comment)

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@jiping-s
Copy link
Author

jiping-s commented May 15, 2024

From what I see in your example you can just import the package as-is, there is no need to use linkname, plus I'm not sure if it even works with external packages, I have mainly seen its use with internal Go libraries.

  • linkname is needed to access the internal decoder, not the public one (we're using yaml.Node.Decode with no public way to config.)
  • It does work with external packages and we use it on production system for other packages. Here is one working example:
//go:linkname DBBeforeQuery github.com/uptrace/bun.(*DB).beforeQuery
func DBBeforeQuery(
	instance *bun.DB,
	ctx context.Context,
	iquery bun.Query,
	queryTemplate string,
	queryArgs []interface{},
	query string,
	model bun.Model,
) (context.Context, *bun.QueryEvent)

@jiping-s
Copy link
Author

Hi! I found the actual symbol name by go tool compile. "." in the last part of package name is encoded:

// gopkg.in/yaml.v2 => gopkg.in/yaml%2ev2

//go:linkname newEncoder2 gopkg.in/yaml%2ev2.newEncoder
func newEncoder2() any

jiping-s added a commit to relex/slog-agent that referenced this issue May 17, 2024
…ut secret (#58)

* accessing internal yaml v3 methods, see golang/go#67361
* added tests for client worker's connection opening

since the YAML change, all practically optional fields in config can be turned optional by removing the value checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

4 participants