How could the body for a func be "provided by runtime"?

446 views
Skip to first unread message

Sina Siadat

unread,
Feb 9, 2017, 7:15:00 AM2/9/17
to golang-nuts
Hi! In $GOROOT/src/sync/mutex.go:19 there is a function declaration like this:

    func throw(string) // provided by runtime

The function body is missing. I was wondering what is meant by "provided by runtime" and how it is done.
I am guessing this is the same as the throw func defined in src/runtime/panic.go:589. But can't find how the compiler connects the two. I thought maybe a body is assigned to this function later like this:

    throw = func(s string) { }

But this fails to compile with "cannot assign to throw" probably because throw is not a variable.

Thanks!

Ilya Kostarev

unread,
Feb 9, 2017, 7:31:53 AM2/9/17
to golan...@googlegroups.com

It's documented. From lang spec
https://golang.org/ref/spec#Function_declarations
"A function declaration may omit the body. Such a declaration
provides the signature for a function implemented outside Go,
such as an assembly routine."

Christian Joergensen

unread,
Feb 9, 2017, 7:40:21 AM2/9/17
to golang-nuts
On Thursday, February 9, 2017 at 1:15:00 PM UTC+1, Sina Siadat wrote:
But can't find how the compiler connects the two.

It's using the //go:linkname compiler directive:


 Cheers,

Christian

Sina Siadat

unread,
Feb 9, 2017, 8:19:45 AM2/9/17
to golang-nuts
Thank you!

To make sure I understand what is going on I tried to reproduce the same thing. But I can't get the compiler to find the definition of my function even though I used the compiler directive here


I want to link the hello.hello and greet.hello functions.

What am I doing wrong?

Ian Lance Taylor

unread,
Feb 9, 2017, 11:23:43 AM2/9/17
to Sina Siadat, golang-nuts
On Thu, Feb 9, 2017 at 5:19 AM, Sina Siadat <sia...@gmail.com> wrote:
> Thank you!
>
> To make sure I understand what is going on I tried to reproduce the same
> thing. But I can't get the compiler to find the definition of my function
> even though I used the compiler directive here
>
> https://github.com/siadat/golinkname-test/blob/master/hello/hello.go#L6
>
> I want to link the hello.hello and greet.hello functions.
>
> What am I doing wrong?

You need to explicitly import your "hello" package. go:linkname can
link up names that are included in the link, but you need to make sure
that they are included in the link.

Ian

Sina Siadat

unread,
Feb 9, 2017, 7:16:52 PM2/9/17
to golang-nuts, sia...@gmail.com
Thanks, Ian! I followed your advice and got it working. :)

I did two things: imported the hello package, and compiled/linked everything using go-tool-compile/link instead of go-build command. The go-build command fails, because it passes the -complete flag to the compiler. I pushed the fix to the github repo if anyone is interested.

One thing I still don't understand is the cases when go:linkname is needed. Why not just import that package, if the function is exported. Is it only used for accessing unexported functions?

Ian Lance Taylor

unread,
Feb 9, 2017, 7:20:15 PM2/9/17
to Sina Siadat, golang-nuts
On Thu, Feb 9, 2017 at 4:16 PM, Sina Siadat <sia...@gmail.com> wrote:
>
> One thing I still don't understand is the cases when go:linkname is needed.
> Why not just import that package, if the function is exported. Is it only
> used for accessing unexported functions?

Yes.

It's primarily a hack that lets certain functions live in the runtime
package, and access runtime internals, but still pretend that they are
unexported functions of some different package. Look for the uses of
go:linkname in the runtime package itself.

Ian

Sina Siadat

unread,
Feb 9, 2017, 8:18:33 PM2/9/17
to golang-nuts, sia...@gmail.com
Thanks for the clarification.
Reply all
Reply to author
Forward
0 new messages