Go:linkname Comment

Hi, I was trying to figure out how Go perform a side effect to the environment such as opening file, writing file, open socket, etc. I started to write os.Open in my code and went into the implementation. I ended up looking at a code like this:

//go:linkname runtime_entersyscall runtime.entersyscall
func runtime_entersyscall()

func RawSyscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
  1. Is this a valid Golang syntax? From what I know, a function should have implementation, and we can’t just write the function header like that.
  2. Does the //go:linkname have anything to do with this code? Maybe link it to some .so or .dll file? If it does, where does the .so and .dll file loaded from?
  3. If my guess in number (2) is correct, can I make my own function with //go:linkname and link it with my own shared library written in C/C++/Rust/anything and compiled to .so or .dll?
  4. What is it with the second function definition (the RawSyscall6)? It doesn’t even have //go:linkname comment. What is the explanation behind this?

I’m asking because I’m trying to understand how Go compiler work and I’m curious how does Go perform side effect because the “language” alone shouldn’t be capable doing that (as far as I know). Unlike C, where we can write inline assembly which implies we can perform any syscall with interrupt command.

Hi! From what I know //go:linkname is used for referring to an other external function definition not exported and not for importing/referring to external system or dynamic library.
What I mean is specified in this page.
Of course you need to import unsafe for this kind of hack since it’s a way to bypass Go standard package’s rules :slight_smile:.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.