x4412开发板&ibox卡片电脑项目实战18-驱动中导出符号

在驱动调试时,经常需要驱动互调,即在A驱动中调用B驱动中的相关函数。这时,驱动中的导出符号功能就可以大显身手了。/proc/kallsyms文件对应着内核符号表,它记录了符号以及符号所在的内存地址。

模块可以使用如下宏导出符号到内核符号表:

EXPORT _ SYMBOL(符号名);
EXPORT _ SYMBOL _ GPL(符号名);

       其他模块需要使用导出的符号时,只需声明一下即可。假设我们需要实现一个驱动,实现如下功能:

一:通过加载模块传递两个整型变量,模块加载成功后打印出传递的两个变量的和;

二:将模块中的变量相加的函数声明出去,以供其他模块使用;

       我们继续在上一个实验的基础上修改,最终的hello-x4412.c源码如下:

#include 
#include 
 
static int a = 0;
static int b = 0;
 
module_param(a,int,0644);
MODULE_PARM_DESC(str,"integer variablea");
 
module_param(b,int,0644);
MODULE_PARM_DESC(str,"integer variableb");
 
int x4412_add(int a,int b)
{
         returna+b;
}
 
EXPORT_SYMBOL(x4412_add);
 
static int __devinit hello_x4412_init(void)
{
         printk("thesum is:%d\r\n",a+b);
         return0;
}
 
static void hello_x4412_exit(void)
{
         printk("Goodbye,x4412!\r\n");
}
 
module_init(hello_x4412_init);
module_exit(hello_x4412_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("www.9tripod.com");
MODULE_ALIAS("a character driversample");
MODULE_DESCRIPTION("hello x4412 driver");

       编译内核后,加载驱动验证驱动的正确性:

[root@x4412 mnt]# insmod hello-x4412.ko
[ 1296.312641] the sum is:0
[root@x4412 mnt]# rmmod hello-x4412.ko
[ 1300.008432] Goodbye,x4412!
[root@x4412 mnt]# insmod hello-x4412.ko a=10 b=20
[ 1307.283039] the sum is:30
[root@x4412 mnt]#

       可见,在加载模块时不传递变量时,其求和结果为0,和我们预定义的一致。当传递变量时,结果也和我们期望的一致。再查看/proc/kallsyms下是否生成了对应的符号列表:

[root@x4412 mnt]# more /proc/kallsyms |grep x4412
c0023fe8 t init_rc_map_x4412
……
bf00c000 t $a  [hello_x4412]
bf00c014 t hello_x4412_exit     [hello_x4412]
bf00c030 t hello_x4412_init     [hello_x4412]
bf00c030 t $a  [hello_x4412]
bf00c058 t $d  [hello_x4412]
bf00c014 t cleanup_module       [hello_x4412]
bf00c030 t init_module  [hello_x4412]
bf00c000 T x4412_add    [hello_x4412]
[root@x4412 mnt]#

       在最后一行即为我们导出的符号x4412_add。

你可能感兴趣的:(Linux驱动开发)