[转载]内核编程的EXPORT_SYMBOL

简介:

转载自maypeng.cublog.cn

EXPORT_SYMBOL标签内定义的函数或者符号对全部内核代码公开,不用修改内核代码就可以在您的内核模块中直接调用,即使用EXPORT_SYMBOL可以将一个函数以符号的方式导出给其他模块使用  您还可以手工修改内核源代码来导出另外的函数,用于重新编译并加载新内核后的测试。


Linux symbol export method:

[1] If we want export the symbol in a module, just use the EXPORT_SYMBOL(xxxx ) in the C or H file.
    And compile the module by adding the compile flag -DEXPORT_SYMTAB .
    Then we can use the xxxx in the other module.


[2] If we want export some symbol in Kernel that is not in a module such as xxxx in the /arch/ppc/fec.c .
    Firstly, define the 
xxxx 
in the fec.c ;
    Secondly, make a new file which contain the "extern" define the 
xxxx (for example, extern int xxxx 
);
    Lastly, in the ppc_ksyms.c we includes the new file, and add the EXPORT_SYMBOL (
 xxxx 
).
    Then we can use the 
xxxx .



使用时注意事项:
在使用EXPORT_SYMBOL 的.c文件中 需要 #include <linux/module.h> 文件。
// 先写函数
func_a ()
{

}
//再使用EXPORT_SYMBOL
EXPORT_SYMBOL(func_a);
注:export出的函数必须为非static函数,且要在相应的.h文件中进行声明。
      在内核中新加入文件时,需要对相应目录下的Makefile进行更改,并且新加入的文件使用的config设置与调用函数文件的config设置同。///
 
 
下面简单代码: 

/*mod1.c*/ 
#include <linux/init.h> 
#include <linux/module.h> 

MODULE_LICENSE("Dual BSD/GPL"); 

static int He1(void) 

printk(KERN_INFO "He1../n"); 
return 0; 


EXPORT_SYMBOL(He1); 

static int __init hello_init(void) 

        printk(KERN_ALERT "Hello, world/n"); 
        return 0; 


static void __exit hello_exit(void) 

        printk(KERN_ALERT "Goodbye, cruel world/n"); 


module_init(hello_init); 
module_exit(hello_exit); 

================================== 
/*mod2.c*/ 

#include <linux/init.h> 
#include <linux/module.h> 

MODULE_LICENSE("Dual BSD/GPL"); 

static int He2(void) 

extern int He1(void); 
He1(); 
printk(KERN_INFO "He2../n"); 
return 0; 



static int __init hello_init(void) 

He2(); 
        printk(KERN_ALERT "Hello, world/n"); 
        return 0; 


static void __exit hello_exit(void) 

        printk(KERN_ALERT "Goodbye, cruel world/n"); 


module_init(hello_init); 
module_exit(hello_exit); 


### Makefile --- 

## Author: hefuhua@163.com 
## Version: Id:Makefile,v0.02007/01/2602:02:51lenoExp 
## Keywords: 
## X-URL: 


ifneq ((KERNELRELEASE),)      obj-m := -DEXPORT_SYMTAB #加载该参数的地方      obj-m := mod2.o      else          KERNELDIR ?= /lib/modules/(shell uname -r)/build 
PWD := (shellpwd)default:(MAKE) -C (KERNELDIR)M=(PWD) modules 

endif




本文转自五岳博客园博客,原文链接:www.cnblogs.com/wuyuegb2312/articles/2511324.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
20
分享
相关文章
IsoAlgo Export Symbol Template
IsoAlgo Export Symbol Template eryar@163.com Key Words: IsoAlgo, Symbol Template When you want to check the symbol templates that you defined in IsoAlgoSymbolTemplate.
1020 0
symbol lookup error: /home/quantum6/build_libs/lib/libreadline.so.7: undefined symbol: UP
symbol lookup error: /home/quantum6/build_libs/lib/libreadline.so.7: undefined symbol: UP
162 0
关于"symbol lookup error xxxxx , undefined symbol"问题的解决方式
​ 今天在测试基于netlib实现的数据包处理模块时,突然提示symbollookup error gxio_mpipe_init , undefined symbol问题。通过查阅资料对于该问题大部分的问题原因都是动态库的版本过旧,导致应用程序找不到对应的符号而引起的。
1160 0
理解和使用ES6中的Symbol
ES6中引入了一种新的基础数据类型:Symbol,不过很多开发者可能都不怎么了解它,或者觉得在实际的开发工作中并没有什么场景应用到它,那么今天我们来讲讲这个数据类型,并看看我们怎么来利用它来改进一下我们的代码。
1358 0
ES6(Symbol)
Symbol 1.Syobol的概念 这种数据类型提供一个独一无二的值 2.Symbol的作用 见代码   永远不可能相等,保证值的独一无二。
966 0
ES6 中的 Symbol —— 多的是你不知道的事
ES6 中的 Symbol —— 多的是你不知道的事
116 0
ES6:什么是Symbol?
ES6:什么是Symbol?
135 1
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等