九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4285|回复: 1
打印 上一主题 下一主题

x4412&ibox项目实战18-驱动中导出符号

[复制链接]
跳转到指定楼层
楼主
发表于 2014-9-26 18:59:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在驱动调试时,经常需要驱动互调,即在A驱动中调用B驱动中的相关函数。这时,驱动中的导出符号功能就可以大显身手了。/proc/kallsyms文件对应着内核符号表,它记录了符号以及符号所在的内存地址。
模块可以使用如下宏导出符号到内核符号表:
  1. EXPORT _ SYMBOL(符号名);
  2. EXPORT _ SYMBOL _ GPL(符号名);
复制代码
       其他模块需要使用导出的符号时,只需声明一下即可。假设我们需要实现一个驱动,实现如下功能:
一:通过加载模块传递两个整型变量,模块加载成功后打印出传递的两个变量的和;
二:将模块中的变量相加的函数声明出去,以供其他模块使用;
       我们继续在上一个实验的基础上修改,最终的hello-x4412.c源码如下:
  1. #include <linux/module.h>
  2. #include <linux/init.h>

  3. static int a = 0;
  4. static int b = 0;

  5. module_param(a,int,0644);
  6. MODULE_PARM_DESC(str,"integer variable a");

  7. module_param(b,int,0644);
  8. MODULE_PARM_DESC(str,"integer variable b");

  9. int x4412_add(int a,int b)
  10. {
  11.          return a+b;
  12. }

  13. EXPORT_SYMBOL(x4412_add);

  14. static int __devinit hello_x4412_init(void)
  15. {
  16.          printk("the sum is:%d\r\n",a+b);
  17.          return 0;
  18. }

  19. static void hello_x4412_exit(void)
  20. {
  21.          printk("Goodbye,x4412!\r\n");
  22. }

  23. module_init(hello_x4412_init);
  24. module_exit(hello_x4412_exit);

  25. MODULE_LICENSE("GPL");
  26. MODULE_AUTHOR("www.9tripod.com");
  27. MODULE_ALIAS("a character driver sample");
  28. MODULE_DESCRIPTION("hello x4412 driver");
复制代码
       编译内核后,加载驱动验证驱动的正确性:
  1. [root@x4412 mnt]# insmod hello-x4412.ko
  2. [ 1296.312641] the sum is:0
  3. [root@x4412 mnt]# rmmod hello-x4412.ko
  4. [ 1300.008432] Goodbye,x4412!
  5. [root@x4412 mnt]# insmod hello-x4412.ko a=10 b=20
  6. [ 1307.283039] the sum is:30
  7. [root@x4412 mnt]#
复制代码
       可见,在加载模块时不传递变量时,其求和结果为0,和我们预定义的一致。当传递变量时,结果也和我们期望的一致。再查看/proc/kallsyms下是否生成了对应的符号列表:
  1. [root@x4412 mnt]# more /proc/kallsyms |grep x4412
  2. c0023fe8 t init_rc_map_x4412
  3. ……
  4. bf00c000 t $a   [hello_x4412]
  5. bf00c014 t hello_x4412_exit     [hello_x4412]
  6. bf00c030 t hello_x4412_init     [hello_x4412]
  7. bf00c030 t $a   [hello_x4412]
  8. bf00c058 t $d   [hello_x4412]
  9. bf00c014 t cleanup_module       [hello_x4412]
  10. bf00c030 t init_module  [hello_x4412]
  11. bf00c000 T x4412_add    [hello_x4412]
  12. [root@x4412 mnt]#
复制代码
       在最后一行即为我们导出的符号x4412_add
附:可直接在x4412开发板或ibox卡片电脑上工作的模块文件:
hello-x4412.ko (3.38 KB, 下载次数: 3)
回复

使用道具 举报

沙发
发表于 2015-9-16 15:33:16 | 只看该作者
在驱动调试时,经常需要驱动互调,即在A驱动中调用B驱动中的相关函数。这时,驱动中的导出符号功能就可以大显身手了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-5-5 17:49 , Processed in 0.019353 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表