3

The kernel module can not call libc since libc run under user space.
There are some other kernel specified APIs just like printk() to make modules work fine.
As I understand that libc is a collection of several standard c function obj(s).
It is supposed to exist a collection (or library) to include several kernel standard function objects.
So I can link my kernel module with these kernel standard libraries to do work, right ?

briefly speaking , my question is as followings ...

in user space :
aaa.o link bbb.o to call myfunc()
aaa.o link libc.so to call printf()

in kernel space :
aaa.ko link bbb.ko call myfunc() ? this is Question1
aaa.ko link xxx to call printk() ? what is xxx called , Question2

thanks !

summerdog
  • 71
  • 1
  • 6

2 Answers2

8

Kernel modules can only call kernel functions (which are in the fixed part of the kernel). They don't and can't use any external libraries.

So there is no kernel standard library (it is the kernel itself which contains printk).

Conceptually, kernel code is in the freestanding dialect of C; it does not use any C standard library functions (for obscure reasons, Linux kernel code is not compiled with -ffreestanding dialect option to gcc )

Community
  • 1
  • 1
Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509
  • I am confused about the module build process. In compile time, GCC checked the header files which be included by my module (e.g. init.h module.h) since I specified the kernel header directory. In link time, GCC check the definition of functions which be called by my module (e.g. printk() ) , how can it PASS without any error if no any libraries offer the definition of function to my module ??? I known module call kernel function in 'run time'. thanks ! – summerdog Apr 03 '14 at 02:17
2

You get a .ko file which is a kernel object file. GCC does not link it. The kernel you insert it to does, at runtime.

xiay
  • 701
  • 7
  • 16