/proc/kallsyms records the names and addresses of all the exported symbols in the kernel
We need to compile 2 kernel modules, and then one of the kernel modules to call functions in another kernel module
The HELLO.C code is as follows
#include <linux/module.h>
#include <linux/init.h>
module_license ("GPL");
Module_author ("David Xie");
Module_description ("Hello World Module");
Module_alias ("A simplest module");
extern int Add_integar (int a, int b);
extern int Sub_integer (int a, int b);
static int __init Hello_init ()
{
int res = Add_integar (1, 2);
PRINTK ("add[%d]\n", res);
return 0;
}
static void __exit Hello_exit ()
{
int res = Sub_integer (2, 1);
PRINTK ("sub[%d]\n", res);
Module_init (hello_init);
Module_exit (Hello_exit);
From this we can see that we have called 2 functions Add_integar and Sub_integer in our hello.c, and that these 2 functions are implemented in another module.
CALCULATE.C's code is as follows
#include <linux/module.h>
#include <linux/init.h>
module_license ("GPL");
int Add_integar (int a, int b)
{return
a+b;
}
int Sub_integer (int a, int b)
{return
a-b;
}
static int __init Sym_init ()
{return
0;
}
static void __exit Sym_exit ()
{
}
module_init (sym_init);
Module_exit (sym_exit);
Export_symbol (Add_integar);//export function for HELLO.C call
Export_symbol (Sub_integer);//export function for HELLO.C call
CALCULATE.C corresponds to the makefile as follows
Ifneq ($ (kernelrelease),)
obj-m: = calculate.o
else
kerneldir? =/home/grb/grb/arm/linux-2.6.38/
PWD: = $ (Shell PWD) all
:
$ (make)-C $ (Kerneldir) m=$ (PWD) modules clean
:
rm-f *.ko *.o *.MOD.O *.MOD.C *.symvers *~ *.order
endif
We'll get a Hello.ko file and a Calculate.ko file when we're done with each.
Then we can copy our module to our Development board and run the test.
2 Modules we have to install Calculate.ko first, otherwise Hello.ko will not be able to find the implementation of the function and error, test results are as follows
use of kernel symbol export
Export_symbol (symbol name)
EXPORT_SYMBOL_GPL (symbol name)
Where EXPORT_SYMBOL_GPL can only be used for modules that contain GPL licenses
Frequently Asked Questions: versions do not match
The version of the kernel module is determined by the version of the kernel code on which it relies, and when the kernel module is loaded, the INSMOD program compares the version of the kernel module to the currently running kernel version, and if it is inconsistent, the following error occurs:
Insmod Hello.ko
Disagrees about version of symbol Struct_module
Insmod:error inserting ' Hello.ko ':-1 Invalid module format Workaround:
1, use modprobe--force-modversion forcibly insert (not recommended)
2. Ensure that the kernel module is compiled with a kernel code version equal to the currently running kernel, which allows you to view the currently running kernel version by uname-r
There is an opportunistic way to modify the version number in the/home/grb/grb/arm/linux-2.6.38/makefile file, as follows
The first few lines are his version number, this method is very easy to go wrong, so this method is not recommended.
Kernel Printing Priority
8 record levels are defined in <linux/kernel.h>. Descending order of precedence is the following:
Kern_emerg "<0>" is used for emergency messages, often before the crash.
Kern_alert "<1>" need immediate action message
Kern_crit "<2>" serious situation
Kern_err "<3>" error condition
If you do not specify a level for the message, PRINTK () uses the default Default_message_loglevel, which is an integer defined in KERNEL/PRINTK.C