Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Go make system calls?

Tags:

go

As far as I know, in CPython, open() and read() - the API to read a file is written in C code. The C code probably calls some C library which knows how to make system call.

What about a language such as Go? Isn't Go itself now written in Go? Does Go call C libraries behind the scenes?

like image 932
CppLearner Avatar asked Apr 17 '19 20:04

CppLearner


1 Answers

The short answer is "it depends".

Go compiles for multiple combinations of H/W and OS, and they all have different approaches to how syscalls are to be made when working with them.

For instance, Solaris does not provide a stable supported set of syscalls, so they go through the systems libc — just as required by the vendor.

Windows does support a rather stable set of syscalls but it is defined as a C API provided by a set of standard DLLs. The functions exposed by those DLLs are mostly shims which use a single "make a syscall by number" function, but these numbers are not documented and are different between the kernel flavours and releases (perhaps, intentionally).

Linux does provide a stable and documented set of numbered syscalls and hence there Go just calls the kernel directly.

Now keep in mind that for Go to "call the kernel directly" means following the so-called ABI of the H/W and OS combo. For instance, on modern Linux on amd64 making a syscall requires filling a set of CPU registers with certain values, doing some other arrangements and then issuing the SYSENTER CPU instruction.

On Windows, you have to use its native calling convention (which is stdcall, not cdecl).

like image 183
kostix Avatar answered Nov 08 '22 22:11

kostix