Thursday 28 October 2010

Kernel code and compiling

LKM (loadable kernel module) -> alter without reboot, load/unload when needed, smaller core kernel. Example : device driver

as opposed to SK (static kernel) -> need reboot when altered


xxx.c --> xxx.ko (ko= kernel object)
.so = shared object

A. /proc/kallsyms contains all function of kernel (ex. printk) and the services using it.
===> printk is print at kernel (seen at dmesg or /var/log/messages)
===> printf is print at user mode

B. /boot/System.map.xxxxx contains all the system calls (which is a "call" to kernel space, from user space)

Symbols that are visible for Load kernel module is in:
C. /lib/modules/'uname -r'/build/Module.symvers


ABC relation:
C containing the symbol (function/call) and the memory address (address important for debugging). B containing all the symbol(along with the ID) with the services use it (ex. service : ip, ipv6,nfs). A containing the symbol/function/call(along with the ID) available, and with subs of that function.





To compile:

Make the xxx.c file (C source code)
Cretae the Makefile (enough this 1 line ==> obj-m := xxx.o )

compile it by:
make -C /usr/src/kernels/2.6.18-92.el5-i686/ modules M=$PWD

Result many files, one of them is xxx.ko

modinfo xxx.ko (To see the information which is written in the xxx.c)

insmod xxx.ko (opposed : rmmod xxx.ko or rmmod xxx)
==> must use the path/file location, and execute/install module dependency beforehand
==> modules and its alias is in /etc/modules.conf (either can be used)
==> dependency is in /lib/modules/version/modules.dep

modprobe xxx (opposed : modprobe -r xxx)
==> modprobe use insmod too, but it is aware the def location of module /lib/modules/version, and it is aware of the dep. So use this better.

lsmod (to see what is installed) ==> /proc/modules

depmod -a (to refresh the module database list) IMPORTANT!!

To make it (copy auto) to /lib/modules/.........
make -C /usr/src/kernels/2.6.18-92.el5-i686/ modules_install M=$PWD

No comments:

Post a Comment