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

一、简介
  •   如果一个模块需要向其他模块导出符号(方法或全局变量),需要使用:
         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)/build

all:
         make -C $(KDIR) M=$(PWD) modules

clean:
        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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值