内核版本: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.

0

driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)相关推荐

  1. linux内核微妙时,Linux内核模块时间同步函数汇总

    在linux内核模块中能用到的函数比用户空间多,但是它的函数往往被用得很少.当然在内核中叶可以调用用户空间的函数只要直接调用系统调用的封装函数就行,如下: 如调用long gettimeofday(s ...

  2. linux-2.6内核模块引用计数的实现(try_module_get和module_put)

    linux-2.6内核模块引用计数的实现 作者:刘洪涛,华清远见嵌入式学院金牌讲师. 一.模块使用计数的背景知识 模块是一种可以在内核运行过程中动态加载.卸载的内核功能组件.2.6内核中模块的命名方式 ...

  3. C语言KDF密钥源代码,区块链中的数学-SM2算法与KDF密钥导出函数

    本节讲了SM2算法的KDF函数,从一般用途到SM2特定实现 ## 写在前面 上一节说了[sm2概述和加密过程](https://learnblockchain.cn/article/1516)实现, ...

  4. dumpbin发现没有入口函数_详解VS2019 dumpbin查看DLL的导出函数

    之前有人问过ViewDLL这些软件在哪下载.实际上使用VS就可以查看DLL的导出接口. 1.先打开VS内部的Power Shell 2.打开以后应该如下图所示,首先输入dumpbin查看下用法: 3. ...

  5. php脚本函数,PHP执行系统命令函数实例讲解

    命令注入 命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者 bash 命令)的一种注入攻击手段. 常见的执行系统命令 ...

  6. 一张图说明 函数, 实例(对象), 原型之间的关系

    前言 很多初学者都对函数,实例(对象), 原型之间的关系理不清楚. 网上五花八门的文章很多,要么不知所云,要么是晦涩难懂. 本文意在用最简洁的语言跟示例让初学者理清楚这三者之间的关系,无需理会其内部原 ...

  7. linux nm工具 查看静动态库导出函数

    查看动态导出函数 方法一 nm -D  **.so 但这样能看到所有的导出,乱七八糟的很多,筛选用: nm **.so | grep XX 方法二 objdump -tT **.so 查看静态导出函数 ...

  8. 内核分析PE获取DLL导出函数地址

    环境:VS2012+WIN8 64 类型:C++编写的WDM驱动程序 测试:VM WIN7 用途:主要用于驱动程序中得到WIN32 API地址,也可得到自定义的DLL中的函数导出地址,记录内核文件相关 ...

  9. countif和sum套用_if函数嵌套countif、sumif函数实例操作详解 你需要学习了

    excel中if函数嵌套countif函数实例操作详解,需要了解if函数.countif函数.sumif函数字符串引用方式,isblank函数 操作方法 01 用扫描枪连接excel后将自动将货物名称 ...

最新文章

  1. 2021新款 iPad,包邮送一个!10月25日截止
  2. 组装计算机的游戏,如何组装一台游戏电脑
  3. pycharm 修改新建文件时的头部模板
  4. python查看opencv版本命令行_查看python下OpenCV版本的方法
  5. 公司想申请网易企业电子邮箱,怎么样?
  6. c malloc 头文件_C/C++笔试题:主要考察C/C++语言基础概念算法及编程,附参考答案...
  7. 回发或回调参数无效。在配置中使用 enableEventValidation=true或在页面中启用了事件验证...(转)...
  8. 面向对象与原型(二)
  9. python AES加密解密
  10. 联邦快递“误运”华为包裹遭调查 联邦快递:全力配合
  11. python的django框架是干嘛的_Django框架在Python开发很重要为什么?
  12. 微信小程序在组件中关闭小程序
  13. MyBatis 简介
  14. 飞信2009_那个时代独有的目光局限——浅谈“飞信”的兴衰
  15. Windows下的JavaFX桌面应用程序打包ABC
  16. 记一次小白调参baseline——NLP中文预训练模型泛化能力比赛
  17. Power BI可视化技巧:正负值配色
  18. 2021年目标,我打算这样去实现
  19. Java实现发送邮件(定时自动发送邮件)
  20. Pycharm 简介

热门文章

  1. 假笨说-我是如何走上JVM这条贼船的
  2. 初探 React Router 4.0
  3. 分解和合并:Java 也擅长轻松的并行编程!
  4. Java final、static final 讲解
  5. java 贝塞尔_java贝塞尔曲线翻页效果
  6. java代码中添加事务_C#和JAVA中编写事务代码
  7. python粘贴代码到word_Python复制Word内容并使用格式设字体与大小实例代码
  8. 机房环境监控系统基础知识
  9. 配电基础知识汇总,99%的人都收藏了!
  10. radio按扭设置只读_disabled属性样式问题