“ 本文将简单介绍驱动模块之间如何互动,调用其他模块的接口资源 ......”
内核符号表
insmod会使用公共内核符号表来解析模块中未定义的符号。
当模块被装入内核后,它所导出的任何符号也会变成内核符号表的一部分。
公共内核符号表中包含了所有的全局内核项(即函数和变量)的地址,这是实现模块化驱动程序所必须的。
模块层叠技术的应用
在hello.ko模块中引用hello_print_comm.ko模块所导出的符号。
hello_print_comm.ko的源代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
static int hello_print_comm_init(void)
{
printk(KERN_INFO "This process is \"%s\" (pid %i) \n", current->comm, current->pid);
return 0;
}
static void hello_print_comm_exit(void)
{
printk(KERN_INFO "This process is \"%s\" (pid %i) \n", current->comm, current->pid);
return;
}
//导出符号到内核符号表
EXPORT_SYMBOL_GPL(hello_print_comm_init);
EXPORT_SYMBOL_GPL(hello_print_comm_exit);
module_init(hello_print_comm_init);
module_exit(hello_print_comm_exit);
MODULE_LICENSE("GPL");
hello.ko的源代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
extern int hello_print_comm_init(void);
extern void hello_print_comm_exit(void);
static int __init hello_init(void)
{
printk(KERN_INFO "Hello!\n");
printk(KERN_INFO "Nice to meet you!\n");
hello_print_comm_init();
return 0;
}
static void __exit hello_exit(void)
{
hello_print_comm_exit();
printk(KERN_INFO "Goodbye!\n");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
加载模块
[root@xxx]: /#:cd ./lib/modules/4.1.15/
[root@xxx]: /lib/modules/4.1.15#:ls
hello.ko hello_print_comm.ko
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:depmod
[root@xxx]: /lib/modules/4.1.15#:ls
hello.ko modules.alias modules.symbols
hello_print_comm.ko modules.dep
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:insmod hello.ko
[ 63.687893] hello: Unknown symbol hello_print_comm_exit (err 0)
[ 63.693856] hello: Unknown symbol hello_print_comm_init (err 0)
[ 63.724682] hello: Unknown symbol hello_print_comm_exit (err 0)
[ 63.730634] hello: Unknown symbol hello_print_comm_init (err 0)
insmod: can't insert 'hello.ko': unknown symbol in module, or unknown parameter
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:modprobe -a hello.ko
[ 89.259356] This process is "modprobe" (pid 223)
[ 89.265090] Hello!
[ 89.267905] Nice to meet you!
[ 89.270891] This process is "modprobe" (pid 223)
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:modprobe -r hello.ko
[ 128.857764] This process is "modprobe" (pid 225)
[ 128.862497] Goodbye!
[ 128.857999] This process is "modprobe" (pid 225)
结束语
希望本文可以帮助到大家!
如需获取文中的测试程序源码,可通过后台回复获取!水平有限,如有错漏的地方请不吝指出,谢谢!