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)
- 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.
- 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? - 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
? - 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.