3

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?

  • If a libc provides syscall APIs, it needs to let user know about the argument types and function names, maybe musl provide its own version – 炸鱼薯条德里克 Jul 8 at 9:33
2

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.

  • Why doesn't glibc do that then? Relying on the kernel headers is just plain bad. They have to be specially "sanitized" and IIRC the kernel headers have special support/exceptions for glibc because they and programs using it use the headers wrong. On linux the syscall ABI is stable, so theres no need to require the headers, the definitions won't change, a change that breaks userspace is considered by linux to be a BUG – user361212 Jul 8 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.