Is there any import path limitation when using go:linkname localname importpath.name?

131 views
Skip to first unread message

Cholerae Hu

unread,
Jan 18, 2017, 9:55:00 PM1/18/17
to golang-nuts
I'm trying to play with go:linkname, and I wrote a demo for this.

directory structure:

[test]$ tree
.
├── main.go
├── pri
│   └── a.go
└── pub
    ├── issue15006.s
    └── b.go

a.go:

package pri


import (
   
"fmt"
)
func rua
() int64 {
    fmt
.Println("rua in pri")
   
return 1
}

b.go:

package pub


import (
   
"unsafe"
)


var _ = unsafe.Sizeof(0)


//go:noescape
//go:linkname rua test/pri.rua
func rua
() int64


func
Rua() int64 {
   
return rua()
}

main.go:

package main


import (
   
"test/pub"
)


func main
() {
    println
(pub.Rua())
}

issue15006.s is a workaround for issue 15006 and it's totally empty.

Than I ran `go run main.go` and got an error:

# command-line-arguments
test
/pub.Rua: test/pri.rua: not defined
test
/pub.Rua: undefined: test/pri.rua

So what's wrong with this demo?

Ian Lance Taylor

unread,
Jan 20, 2017, 12:34:43 AM1/20/17
to Cholerae Hu, golang-nuts
Nothing in your program imports test/pri. go:linkname only changes
the name used for a symbol. It doesn't cause a package to be
imported.

Ian

Cholerae Hu

unread,
Jan 20, 2017, 2:33:25 AM1/20/17
to golang-nuts, chole...@gmail.com
I edited my code to import test/pri in test/pub:

pri/a.go:

package pri


import (
   
"fmt"
)
const (
   
Dummy = 1
)
func rua
() int64 {
    fmt
.Println("rua in pri")

   
return int64(1)
}

pub/b.go:

package pub


import (
   
"unsafe"

    pri
"test/pri"

)


var _ = unsafe.Sizeof(0)
var _ = pri.Dummy


//go:noescape
//go:linkname rua pri.rua

func rua
() int64


func
Rua() int64 {
   
return rua()
}

But this still don't work.  

# command-line-arguments
test/pub.Rua: pri.rua: not defined
test/pub.Rua: undefined: pri.rua

Besides, this package doesn't import "runtime" at all, but it can use "runtime.nanotime" in go:linkname.


在 2017年1月20日星期五 UTC+8下午1:34:43,Ian Lance Taylor写道:

Cholerae Hu

unread,
Jan 20, 2017, 3:16:05 AM1/20/17
to golang-nuts, chole...@gmail.com
I removed the alias "pri", and it works, thanks!
By the way, when go:linkname with runtime, why don't I need to import "runtime" package explicitly?

在 2017年1月20日星期五 UTC+8下午1:34:43,Ian Lance Taylor写道:
On Wed, Jan 18, 2017 at 6:55 PM, Cholerae Hu <chole...@gmail.com> wrote:

Ian Lance Taylor

unread,
Jan 20, 2017, 4:14:39 PM1/20/17
to Cholerae Hu, golang-nuts
On Fri, Jan 20, 2017 at 12:16 AM, Cholerae Hu <chole...@gmail.com> wrote:
> I removed the alias "pri", and it works, thanks!
> By the way, when go:linkname with runtime, why don't I need to import
> "runtime" package explicitly?

The runtime package is always imported implicitly by every Go package
(other than runtime and runtime/internal/*).

Ian
Reply all
Reply to author
Forward
0 new messages