By vikram_cvk at 2004-07-03 04:21
Kernel Compilation & Avoiding 'Unresolved Symbol' HOWTO
Many of the kernel newbies are not aware of certain important concepts which are essential for proper compilation and insertion of the modules into the kernel This HOWTO is all about kernel compilation , how to avoid the annoying ''unresolved symbols''errors while Insmoding the modules .
Why a Kernel should be compiled?
By default kernel module version has to be disabled when developing new modules, but this is enabled by default in some older versions of popular distribution.
Apart from this there are many other reasons why a kernel has to be recompiled . I'm not listing all those reasons due to lack of space.
Following are the things to be taken into consideration before compilation.
Creating copy of complete kernel sources at different location other than /usr/src .
Taking a backup of important user data is a good precautionary measure .
Prerequisite for kernel recompilation
Logging in as root.
Kernel sources directory has to be installed on the system ,this could be done by installing the kernel source rpm's from the installation CD's or downloading latest kernels from kernel.org.
Important things to enable/disable in the 'menuconfig' dialog box.
Under Loadable module support
* Enabling 'Enable loadable module support'
* Disabling 'Set version information on all module symbols'
Under Processor type and features
* Selecting the correct processor type
* Enabling Symmetric multi-processing support (regardless of whether your system is single processor or multi processor one)
Under USB Support
* Enabling USB support (If you intend to develop USB drivers)
Under Kernel Hacking
* Enable Kernel Debugging.
Steps in Kernel Compilation
Untar or Install kernel source rpm's.
Go through the 'readme' under kernel source directory.
bash#make menuconfig - Make the above changes and additionally any other changes you want.
bash#make depends - This for knowing about the file dependencies.
bash#make bzImage - This is for creating bootable kernel.
bash#make modules_install - 6 & 7 commands for compiling the kernel modules if any.
After the above step bootable kernel image is found under /boot/arch/i386/boot/ directory with the file name 'bzImage'
In order to make the BIOS find the above bootable image file any of the below 2 things can be done
10. Making the entry in bootloaders (lilo or grub)
If 'Lilo' is the bootloader then do the following
bash# man lilo
bash# man lilo.conf
In /etc/lilo.conf file add the following lines
After editing /etc/lilo.conf
If 'Grub' is the bootloader
In /etc/grub.conf add the following
title Red Hat Linux (2.4.18-188.8.131.52mar2003)
kernel /boot/bzImage ro root=/dev/hda1
Following is optional
Dumping the boot image to a floppy with the following command
bash#cat bzImage > /dev/fd0
Instead of using kernel sources which comes with the distribution CD's.
It is always better to download the latest kernel from kernel.org. The reason being the kernel sources in the distribution CD's are altered and in many cases it has been found that some header files were missing or they were of the older versions
Avoiding Unresolved Symbols /Version mismatch Errors
Most of the kernel newbie's are intimidated by the Unresolved symbols / Version mismatch errors which they face when trying to compile and 'insmod' there newly compiled kernel modules.
The reasons for this error.............
Though there are many reasons , the most common reason is
Kernel and modules are compiled with different modversion information.
Now you might get a doubt what is this modversion ! Every kernel module created consists of information about the kernel version on which it is compiled , for instance if module X is compiled on kernel 2.4.18 and if you are trying to run this module X on a kernel whose version is 2.4.22 then there will be a mismatch of module versions .
How to avoid this Problem
Add the following at the starting to the kernel module source file you are creating.
#if defined __KERNEL__
#if defined( CONFIG_MODVERSIONS ) && ! defined( MODVERSIONS )
/* modversions.h should be before should be before module.h */
#if defined( MODVERSIONS )
/* Now your module include files & source code follows */
Apart from the above you should compile your module with the following compiler flags.
For instance if your module name is xyz.c then -
bash#gcc -I/lib/modules/<current kernel version name >/build/include -
D__KERNEL__ -DMODULE -DLINUX -O2 -Wall -Wstrict-prototypes
-fno-strict-aliasing -fno-strict-aliasing -c -o xyz.o xyz.c
<current kernel source directory >
Substitute the above with the kernel version name you are currently using.You can know that with the following command.
Bash# uname -r
The above hack will help you get rid of the ugly unresolved symbols error only if you have not compiled your brand new kernel , If you have already compiled your kernel then the above will not work because during kernel compilation there is a possibility of some of the header files in the ../include directory getting deleted ,so in such situations you have to reinstall your kernel sources once again and try the above .
In spite of all the above still you will get some warnings like 'this is a tainted module blahh blahh' just ignore them. Hope this HOWTO will help you in getting started comfortably in your Kernel programming endeavor !!!!! Very soon I'll come out with a HOWTO on device driver programming , in this I'll try to simplify the task of writing full-fledged device driver (Real device - not a simple 'hello world' program )
This HOWTO is authored by Vikram Kumar C, I'm a Linux freak since ages , currently working as a Linux programmer in the city of Hyderabad, India You can reach me at firstname.lastname@example.org.