Linux内核模块-导出符号表(五)
来源:互联网 发布:网络借贷p2p系统 编辑:程序博客网 时间:2024/05/01 14:45
一、简介
- 如果一个模块需要向其他模块导出符号(方法或全局变量),需要使用:
EXPORT_SYMBOL(name);
EXPORT_SYMBOL_GPL(name);
注意:符号必须在模块文件的全局部分导出,不能再函数部分导出。
更多信息可参考 <linux/module.h>文件
- 显示所有导出的符号
[root@localhost test]# cat /proc/kallsyms
- 应用步骤
- 在模块函数定义之后使用EXPORT_SYMBOL(function_name);
- 在调用该函数的模块中使用extern对之声明;
- 首先加载定义该函数的模块,在家在调用该函数的模块。
二、应用实例
- 代码
- hello-3.c
/** hello-3.c - Illustration the difference* between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL* Include hello-3.c hello-4.c*/# include <linux/kernel.h> /* Wd're doing kernel work */# include <linux/module.h> /* specifically, a module */ # include <linux/init.h> /* Need for the macros */void function1(void){ printk(KERN_INFO "Hello world 3\n");}EXPORT_SYMBOL(function1);void function2(void){ printk(KERN_INFO "Hello world 3 again\n");}EXPORT_SYMBOL_GPL(function2);static int __init hello_3_init(void){ return 0;} static void __exit hello_3_exit(void){ printk(KERN_INFO "Bye\n");}module_init(hello_3_init);module_exit(hello_3_exit);MODULE_LICENSE("GPL");
- hello-4.c
/** hello-4.c - Illustration the difference* between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL* Include hello-3.c hello-4.c*/# include <linux/kernel.h> /* Wd're doing kernel work */# include <linux/module.h> /* specifically, a module */ # include <linux/init.h> /* Need for the macros */extern int function1(void);extern int function2(void);static int __init hello_4_init(void){ function1(); // function2(); return 0;} static void __exit hello_4_exit(void){ printk(KERN_INFO "Goodbye\n");}module_init(hello_4_init);module_exit(hello_4_exit);//MODULE_LICENSE("GPL");
- Makefile文件
obj-m += hello-3.o obj-m += hello-4.o PWD := $(shell pwd) KVER := $(shell uname -r) KDIR := /lib/modules/$(KVER)/buildall: make -C $(KDIR) M=$(PWD) modulesclean: make -C $(KDIR) M=$(PWD) clean
注意:all和clean下面,命令行前的空隙是按“Tab键”。
2.操作步骤
[root@localhost test]# make
[root@localhost test]# insmod hello-3.ko
[root@localhost test]# insmod hello-4.ko
[root@localhost test]# insmod hello-4.ko
[root@localhost test]# rmmod hello-4.ko
[root@localhost test]# rmmod hello-3.ko
[root@localhost test]# dmesg | tail -10
[root@localhost test]# rmmod hello-3.ko
[root@localhost test]# dmesg | tail -10
。。。。。
Hello world 3
Hello world 3 again
Goodbye
Bye
Hello world 3 again
Goodbye
Bye
3.可能异常
a.装载顺序,先insmod hello-4.ko,然后insmod hello-3.ko,会有以下出错信息:
[root@localhost test]# insmod hello-4.ko
insmod: error inserting 'hello-4.ko': -1 Unknown symbol in module
insmod: error inserting 'hello-4.ko': -1 Unknown symbol in module
注意:装载顺序,必须先是声明函数的模块,然后装载调用函数的模块。
这样才能获取导出符号(对应内存地址),找到函数地址。
b.在hello-4.c中注释掉MODULE_LISENCE("GPL"),装载hello-4.ko时,会有以下出错信息:
insmod: error inserting 'hello-4.ko': -1 Unknown symbol in module
注意:这就是 EXPORT_SYMBOL与 EXPORT_SYMBOL_GPL用法的区别。
c.卸载时,先rmmod hello-3.ko,然后rmmod hello-4.ko,会有如下出错信息:
[root@localhost test]# rmmod hello-3.ko
ERROR: Module hello_3 is in use by hello_4
ERROR: Module hello_3 is in use by hello_4
注意:函数被调用时不能卸载,应先卸载调用函数的模块。
0 0
- Linux内核模块-导出符号表(五)
- linux 内核模块编程之内核符号导出(五)
- linux内核模块(2)导出符号
- Linux 驱动开发之内核模块开发(四)—— 符号表的导出
- Linux 驱动开发之内核模块开发(四)—— 符号表的导出
- 内核模块导出符号使用
- linux内核导出符号
- linux内核导出符号
- linux内核导出符号
- Linux内核模块符号导出后无法调用问题
- Linux内核模块符号导出后无法调用问题
- Linux内核模块:初始化、加载/卸载、模块参数、导出符号、错误处理、模块装载竞争
- 内核模块编程---符号导出(EXPORT_SYMBOL()) (3)
- linux kernel2.6编译启用新内核以及模块符号的导出和调用
- 内核符号表导出EXPORT_SYMBOL
- linux模块导出符号 EXPORT_SYMBOL_GPL EXPORT_SYMBOL
- linux模块导出符号 EXPORT_SYMBOL_GPL EXPORT_SYMBOL
- linux模块导出符号 EXPORT_SYMBOL_GPL EXPORT_SYMBOL
- Linux内核模块-printk函数(四)
- 2014-3-31~2014-4-6工作总结
- ubuntu下sublime text 3安装
- leetcode----Reverse Integer
- 项目异常处理架构
- Linux内核模块-导出符号表(五)
- iOS开发 - 设置UIButton的Image为Aspect Fit
- 新一代OFBIZ(Gorun8)更新了。。。
- 【Android】Android优化-oom
- C语言基础-------全局变量、局部变量、静态全局变量、静态局部变量的区别
- 4.输入一个整数,如果该整数大于1000,则输出“big”,如果该整数介于100到1000之间(包含两个边界),则输出“middle”,如果上面两种情况都不满足,输出”small“
- hwui——Layer的使用
- 梅森旋转算法
- Win8.1使用