I know that the system calls are not in the C standard Library. Is there any library (some sort of a system library) where the system calls are?
If there is such a library how is this library linked to the executable program?
I know that the system calls are not in the C standard Library. Is there any library (some sort of a system library) where the system calls are?
If there is such a library how is this library linked to the executable program?
A system call (listed in syscalls(2)) is mostly interpreted by the linux kernel. The C library just contains the glue code to interface to the kernel. From the application (user-land) point of view, a system call is atomic and is nearly a single machine code instruction (
SYSCALL
orSYSENTER
on x86-64). The ABI used to invoke system calls is not the C ABI. See also Linux Assembler HowTo and x86-64 ABI. Use strace(1) to understand what system calls are done by some command or process.Read about operating systems, system calls, linux kernel.
A
SYSENTER
(orSYSCALL
) machine instruction switches the CPU mode from user-mode to kernel-mode. The kernel then run many (millions) of machine instructions in privileged [kernel] mode, and finally would execute aSYSEXIT
(orSYSRETURN
) machine instruction to return back to user-mode. The kernel could even schedule another task. So from the application's point of view, a system call is a kind of elementary virtual machine instruction; the application code just "sees" a very complex machine instruction doing an entire system call, and that illusion is provided by the kernel.See also this answer to a related question.
(You could avoid the libc by directly doing syscalls in assembler; Bones is an example of such program; but almost all programs on Linux are using some libc).
Read also vdso(7), ld-linux(8), elf(5).
The library is the standard C library (libc). This is normally glibc but some embedded linuxes will use musl and Android uses bionic.
Linux as a platform is fairly unique in that the kernel/user interface is rather stable so different c libraries can be used.
libc library will be linked in automatically as a shared library by your toolchain unless you tell it not to.
A system call can work in a few different ways, depending on the target architecture, but in any case, it is not a library call. It is a way for a running user-space program to call some functionality in the kernel.
In very old systems, this typically meant to jump directly to some address where this kernel function starts. Later, kernels introduced "jump tables" adding a layer of indirection, so the addresses don't have to change when the kernel was changed. This simple approach doesn't work any more for a long time (and linux never used it) because nowadays, user-space programs run in some "protected" mode that restricts what they can do and lets them run in virtual address space, thus protecting the system from crashing, just because one user-space program is erroneous.
So new methods are needed that put the CPU in a mode allowing any code (privileged mode) and still making sure control is passed to the kernel only, so no other code can accidentally run in privileged mode. On
x86
, this was typically done using anint
instruction that triggers a soft interrupt and the kernel handled that interrupt. Onamd64
, there is a specialsyscall
instruction for entering the kernel.In any case, C doesn't know about syscalls. You can't directly issue a syscall in C (but you can with inline assembly, if you know your target architecture). The C library on Linux includes a lot of functions that are just tiny wrappers around actual syscalls, so you can use them from C directly.
Although not in the scope of the question: Contrary to Linux, Windows hides syscalls to an extent that they aren't even documented and subject to change any time. On Windows, you're supposed to only use the system libraries (like
user32.dll
) for your application software.