转自:http://blog.chinaunix.net/uid-23381466-id-3837650.html

内核版本:2.6.38-11-generic

内核自己都大量利用内核符号表导出函数,那么应该导出呢,ldd3上面说只需要EXPORT_SYMBOL一类的宏导出即可,结果试了很久都不行,最后查看文档,算是明白一点了。

对于导出符号表,内核文档给出了三种解决方案,见尾部,现在忽略。

现在有两个模块,a模块导出函数myprint,b模块使用该函数,想象一下如果a模块 EXPORT_SYMBOL(myprint) ,实际上b模块知道吗,很明显b模块对这件事情不是很清楚(这么说不是很准确),要调用a模块的myprint函数,需要知道myprint函数在内存中的位置,首先在内核符号表中是没有说明的,所以...

当我们编译完a模块后,看看有些什么文件,是不是有一个Module.symvers文件,打开看看什么状况?
0x705034f7    myprint    /home/darren/Desktop/darren/print/myprint    EXPORT_SYMBOL
好了,这一行对b模块来说已经足够了,指定了内存位置,符号名称,模块路径。最简单的方法就是把这个文件复制到b模块所在目录,然后编译就没有讨厌的错误了,可以正常insmod模块。这种方法是内核文档中提到的方法之一。

但是每次调用该函数都要复制一次,更糟糕的是a模块每编译一次,都要重新复制一次,为什么内核自己导出的函数我们可以直接用呢?现在就就解决:

编译内核的时候同样会生成一个Module.symvers文件,内核导出的所有符号都在里面,我们在编译模块的时候实际上会调用内核的顶层makefile,也就是说内核的Module.symvers对我们的模块是可见的,和我们自己的Module.symvers文件一样,OK,把a模块的Module.symvers文件合并到内核的Module.symvers文件中,这时候myprint函数就成了真正的导出函数了,其他的模块只需要生命一下就可以用了。

代码如下
a模块代码

  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. MODULE_LICENSE("GPL");
  5. int myprint(void)
  6. {
  7. printk("c");
  8. return 0;
  9. }
  10. static int darren_init(void)
  11. {
  12. return 0;
  13. }
  14. static void darren_exit(void)
  15. {
  16. }
  17. module_init(darren_init);
  18. module_exit(darren_exit);
  19. EXPORT_SYMBOL(myprint);

b模块代码

  1. #include <linux/seq_file.h>
  2. #include <linux/cdev.h>
  3. #include <asm/system.h>
  4. MODULE_LICENSE("GPL");
  5. extern int print(void);
  6. static int darren_init(void)
  7. {
  8. int i=0;
  9. printk("b module init\n");
  10. for(;i<10;i++)print();
  11. return 0;
  12. }
  13. static void darren_exit(void)
  14. {
  15. }
  16. module_init(darren_init);
  17. module_exit(darren_exit);

a模块的Makefile如下:

  1. NAME:=a
  2. SYM:=/usr/src/linux-headers-2.6.38-8-generic/Module.symvers
  3. DIR:=/lib/modules/$(shell uname -r)/build/
  4. PWD:=$(shell pwd)
  5. obj-m = $(NAME).o
  6. build:
  7. make -C $(DIR) M=$(PWD)
  8. sudo chmod 777 $(SYM)
  9. sudo sed -i '/myprint/d' $(SYM)
  10. sudo cat Module.symvers>>$(SYM)
  11. sudo chmod 644 $(SYM)

b模块的makefile:

  1. NAME:=b
  2. DIR:=/lib/modules/$(shell uname -r)/build/
  3. PWD:=$(shell pwd)
  4. obj-m = $(NAME).o
  5. build:
  6. make -C $(DIR) M=$(PWD)

注意:路径/usr/src/linux-headers-2.6.38-8-generic/Module.symvers 有可能不对如果不行就改成/usr/src/linux-headers-`uname -r`-generic/Module.symvers

内核文档:

  1. Sometimes, an external module uses exported symbols from
  2. another external module. kbuild needs to have full knowledge of
  3. all symbols to avoid spitting out warnings about undefined
  4. symbols. Three solutions exist for this situation.
  5. NOTE: The method with a top-level kbuild file is recommended
  6. but may be impractical in certain situations.
  7. Use a top-level kbuild file
  8. If you have two modules, foo.ko and bar.ko, where
  9. foo.ko needs symbols from bar.ko, you can use a
  10. common top-level kbuild file so both modules are
  11. compiled in the same build. Consider the following
  12. directory layout:
  13. ./foo/ <= contains foo.ko
  14. ./bar/ <= contains bar.ko
  15. The top-level kbuild file would then look like:
  16. #./Kbuild (or ./Makefile):
  17. obj-y := foo/ bar/
  18. And executing
  19. $ make -C $KDIR M=$PWD
  20. will then do the expected and compile both modules with
  21. full knowledge of symbols from either module.
  22. Use an extra Module.symvers file
  23. When an external module is built, a Module.symvers file
  24. is generated containing all exported symbols which are
  25. not defined in the kernel. To get access to symbols
  26. from bar.ko, copy the Module.symvers file from the
  27. compilation of bar.ko to the directory where foo.ko is
  28. built. During the module build, kbuild will read the
  29. Module.symvers file in the directory of the external
  30. module, and when the build is finished, a new
  31. Module.symvers file is created containing the sum of
  32. all symbols defined and not part of the kernel.
  33. Use "make" variable KBUILD_EXTRA_SYMBOLS
  34. If it is impractical to copy Module.symvers from
  35. another module, you can assign a space separated list
  36. of files to KBUILD_EXTRA_SYMBOLS in your build file.
  37. These files will be loaded by modpost during the
  38. initialization of its symbol tables.

driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL) 【转】的更多相关文章

  1. linux模块导出符号 EXPORT_SYMBOL_GPL&EXPORT_SYMBOL(转)

    转自:http://blog.csdn.net/angle_birds/article/details/7396748 一个模块mod1中定义一个函数func1:在另外一个模块mod2中定义一个函数f ...

  2. AFX_MANAGE_STATE(AfxGetStaticModuleState())DLL导出函数包含MFC资源

    AFX_MANAGE_STATE(AfxGetStaticModuleState()) 先看一个例子: .创建一个动态链接到MFC DLL的规则DLL,其内部包含一个对话框资源.指定该对话框ID如下: ...

  3. DLL 导出函数

    DLL的链接方式分为两种:隐式链接和显式链接 DLL导出的函数 和 导出类在调用时,有些区别,这里暂时不讲,直说简单的导出函数: 隐式链接: #include "stdafx.h" ...

  4. dll导出函数的两种方式的比较

    最初的网页链接已经挂了, 在此贴一个中间的转载链接 https://blog.csdn.net/zhazhiqiang/article/details/51577523 一 概要 vs中导出 dll的 ...

  5. DLL导出函数和类的定义区别 __declspec(dllexport)

    DLL导出函数和类的定义区别 __declspec(dllexport) 是有区别的, 请看 : //定义头文件的使用方,是导出还是导入 #if defined(_DLL_API) #ifndef D ...

  6. 分享一个批量导出当前实例下的所有linkedserver脚本

    分享一个批量导出当前实例下的所有linkedserver脚本 很多时候,我们都需要导出实例下面的登录用户,job,linkedserver等等 导出job比较复杂,下午写了一个脚本把所有的linked ...

  7. JavaScript学习笔记-函数实例

    函数实例 var p = { a:15, b:'5', f1:function(){ var self = this; console.log(self.a+self.b); f2(); functi ...

  8. Mysql导出函数、存储过程

    下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...

  9. python迭代器与iter()函数实例教程

    python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...

随机推荐

  1. 06 自学Aruba之win7系统802.1x认证网卡设置指导

    点击返回:自学Aruba之路 06 自学Aruba之win7系统802.1x认证网卡设置指导 步骤1.在桌面任务栏找到“打开网络和共享中心”,点击进入: 步骤2.点击“管理无线网络”,进入无线网络配置 ...

  2. 自学Zabbix7.1 IT services

    自学Zabbix7.1 IT services 1. 概念IT Services 服务器或者某项服务.业务的可用率,不懂技术的上级领导会过问最近服务器可用率如何.所有api的状况怎么样?通常一些技术人 ...

  3. CRM 2013 批量更新two options的缺省值

    1: string entNames = "new_print_plan,new_radio_plan,new_bill_board,new_tv_plan,new_btl_posm,new ...

  4. 洛谷 P3853 路标设置 解题报告

    P3853 路标设置 题目背景 B市和T市之间有一条长长的高速公路,这条公路的某些地方设有路标,但是大家都感觉路标设得太少了,相邻两个路标之间往往隔着相当长的一段距离.为了便于研究这个问题,我们把公路 ...

  5. 「THUPC2018」赛艇 / Citing

    https://loj.ac/problem/6388 矩形匹配,小地图经过位置为1,和大地图匹配不能同时存在一个1的位置,就可以是一个当前位置 1.bitset压位,....O(n^2m^2/64) ...

  6. Gradle安装 Gradle效率提升 eclipse安装gradle插件 【我】

    Gradle安装 从官网下载 gradle4.6版本,也可以从svn地址下载 https://downloads.gradle.org/distributions/gradle-4.6-bin.zip ...

  7. 第一次使用Android Studio时你应该知道的一切配置(一)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  8. 在VS2010中使用Git【图文】转

    在之前的一片博客<Windows 下使用Git管理Github项目>中简单介绍了在Windows环境中使用Git管理Github项目,但是是使用命令行来进行操作的,本文将简单介绍下在VS2 ...

  9. mui dtpicker 时间的设置 以及MUI的弹窗

    1)引入mui.min.css,然后引入mui.picker.min.css            注意这个mui.picker.min.css  与 mui.picker.css  不一样 2)引入 ...

  10. marshaller unmarshaller解析xml和读取xml

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...