您的位置:首页 > 运维架构 > Linux

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

2014-04-06 20:03 351 查看
一、简介

  如果一个模块需要向其他模块导出符号(方法或全局变量),需要使用:

         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)/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

注意:函数被调用时不能卸载,应先卸载调用函数的模块。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux内核 makefile