14

I just got started with learning kernel development and had a small doubt. Why can't we use c functions in kernel development after linking it with the c library? Why is it that the kernel is never linked with a c library but has its own implementation of some standard c functions like printk() instead of printf(). IF the kernel is written in c and compiled with the help of a c compiler then why can't we use the standard function from the c library?

1 Answer 1

23

Because the GNU C Library which you are familiar with is implemented for user mode, not kernel mode. The kernel cannot access a userspace API (which might invoke a syscall to the Linux kernel).

From the KernelNewbies FAQ

Can I use library functions in the kernel ?

System libraries (such as glibc, libreadline, libproplist, whatever) that are typically available to userspace programmers are unavailable to kernel programmers. When a process is being loaded the loader will automatically load any dependent libraries into the address space of the process. None of this mechanism is available to kernel programmers: forget about ISO C libraries, the only things available is what is already implemented (and exported) in the kernel and what you can implement yourself.

Note that it is possible to "convert" libraries to work in the kernel; however, they won't fit well, the process is tedious and error-prone, and there might be significant problems with stack handling (the kernel is limited to a small amount of stack space, while userspace programs don't have this limitation) causing random memory corruption.

Many of the commonly requested functions have already been implemented in the kernel, sometimes in "lightweight" versions that aren't as featureful as their userland counterparts. Be sure to grep the headers for any functions you might be able to use before writing your own version from scratch. Some of the most commonly used ones are in include/linux/string.h.

Whenever you feel you need a library function, you should consider your design, and ask yourself if you could move some or all the code into user-space instead.

If you need to use functions from standard library, you have to re-implement that functionality because of a simple reason - there is no standard C library.

C library is basically implemented on the top of the Linux kernel (or other operating system's kernel).

For instance, C library's mkdir(3) function is basically nothing more than a wrapper for Linux kernel's system call mkdir(2).

http://linux.die.net/man/3/mkdir http://linux.die.net/man/2/mkdir

8
  • 1
    But lets say (for the sake of understanding) that I wrote some c code and compiled it after linking it with a standard c library. This in turn will produce some binary which can be executed by the machine. What happens if I place this binary into the kernel ? Why willl it not execute since the instruction set for the processor does not depend on whether the program is running in user space or kernel space. Of course this code may invoke system calls. But what if we load that part of the kernel before executing the piece of code that I wrote? Commented Nov 21, 2012 at 2:11
  • It is not about machine code only, but about binary format as well. You are trying to link Linux binary with non-Linux binary. Commented Nov 21, 2012 at 2:14
  • Can you please elaborate on that. I didnt really understand Commented Nov 21, 2012 at 2:17
  • 2
    " I wrote some c code and compiled it after linking it" -- Code is compiled, then linked with a static library, OR linked with a dynamic library when loaded for execution. "produce some binary which can be executed by the machine" -- This binary is rarely a raw image file. More often it is an executable file type (e.g. COFF or EABI) that the OS's loader knows how to process (i.e. link with libraries) and then let execute. You seem to underestimate the complexity of "executing a program" in a modern OS.
    – sawdust
    Commented Nov 21, 2012 at 6:14
  • 3
    "linking with the standard c libraries like stdio.h etc" -- Header files are not linked, they are source code that can be included during compile step. That's at least the 2nd misuse of "linking", and makes me wonder if you have the correct comprehension of these terms you are using. "Why not just use the c library directly" Oleksandr already answered that; reread the first line of the answer. BTW the kernel has no use for the functions declared in stdio.h. The kernel implements those I/O functions, not use them.
    – sawdust
    Commented Nov 21, 2012 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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