Linux内核模块-导出符号表(五)

来源:互联网 发布:网络借贷p2p系统 编辑:程序博客网 时间:2024/05/01 14:45
一、简介
  •   如果一个模块需要向其他模块导出符号(方法或全局变量),需要使用:
         EXPORT_SYMBOL(name);
         EXPORT_SYMBOL_GPL(name);
 注意:符号必须在模块文件的全局部分导出,不能再函数部分导出。
            更多信息可参考 <linux/module.h>文件             
  •  显示所有导出的符号
          [root@localhost test]# cat /proc/kallsyms

  • 应用步骤
  1. 在模块函数定义之后使用EXPORT_SYMBOL(function_name);
  2. 在调用该函数的模块中使用extern对之声明;
  3. 首先加载定义该函数的模块,在家在调用该函数的模块。
二、应用实例
  1. 代码
  • 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]# rmmod hello-4.ko
[root@localhost test]# rmmod hello-3.ko
[root@localhost test]# dmesg | tail -10
。。。。。
Hello world 3
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
注意:装载顺序,必须先是声明函数的模块,然后装载调用函数的模块。
           这样才能获取导出符号(对应内存地址),找到函数地址。
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
注意:函数被调用时不能卸载,应先卸载调用函数的模块。

0 0