Linux – Why does glibc need kernel headers

glibclinux-kernelsystem-calls

Why do programs like glibc need the Linux Kernel headers? The Linux API is provided via syscalls, and other libc implementations such as musl do not require these headers.

What makes glibc special?

Best Answer

Syscalls are not different from normal function calls, except for the calling mechanism. Just as with normal C function calls, you need to know that parameters and of which type the called function expects, and what the return value is. As an example, look at the stat system call which takes as a parameter a pointer to a struct stat. The kernel and user space code must agree upon how the data is structured, and the definitive source for the type definition of this is the kernel.

Now, because the kernel API comes with a stability guarantee, these data types rarely ever change. Therefore the Musl developers have found it easier to simply maintain manually edited copies of the definitions rather than rely on the kernel headers. The Musl source code contains, for example, its own list of system call numbers in the file arch/x86_64/bits/syscall.h.in.

Related Question