关于EXPORT_SYMBOL的用法

linux2.6的“/proc/kallsyms”文件对应着内核符号表,记录了符号以及符号所在的内存地址。模块可以使用如下宏导出符号到内核符号表:
EXPORT_SYMBOL(符号名);  
EXPORT_SYMBOL_GPL(符号名) 
导出的符号可以被其他模块使用,不过使用之前一定要extern声明一下。EXPORT_SYMBOL_GPL()只适用于包含GPL许可权的模块。
      举一个代码演示:
一个文件是hello.c文件,定义2个函数,用于导出 

#ifndef _KERNEL_
     #define _KERNEL_
#endif

#ifndef MODULE
     #define MODULE
#endif
#include <linux/init.h> 
#include <linux/module.h>  
MODULE_LICENSE("Dual BSD/GPL");  
int add_integar(int a,int b)  
{  
    return a + b;  
}  
int sub_integar(int a,int b)  
{  
    return a - b;  
}  
EXPORT_SYMBOL(add_integar);  
EXPORT_SYMBOL(sub_integar);  

另一个文件是test.c 用于调用hello模块导出的函数 
#ifndef _KERNEL_
     #define _KERNEL_
#endif

#include <linux/init.h> 
#include <linux/module.h>  
MODULE_LICENSE("Dual BSD/GPL");  
extern int add_integar(int ,int); //声明要调用的函数  
extern int sub_integar(int ,int); //声明要调用的函数  
int result(void)  
{  
    int a,b;  
    a = add_integar(1,1);  
    b = sub_integar(1,1);     
    printk("%d/n",a);  
    printk("%d/n",b);  
    return 0;  

     两个文件编译都要有对应的makefile。模板如下:
ifeq ($(KERNELRELEASE),)

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?=  /lib/modules/$(shell uname -r)/build

    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    # called from kernel build system: just declare what our modules are
    obj-m := hello.o
endif
      把hello.o改成对应的文件名.o就可以了。分别make后,先加载hello模块,再加载test模块。然后cat /proc/kallsyms | grep integar,显示:
f8eae000 u add_integar  [hello2]
f8eae004 u sub_integar  [hello2]
f8eae02c r __ksymtab_sub_integar        [hello]
f8eae03c r __kstrtab_sub_integar        [hello]
f8eae034 r __ksymtab_add_integar        [hello]
f8eae048 r __kstrtab_add_integar        [hello]
f8eae000 T add_integar  [hello]
f8eae004 T sub_integar  [hello]

      可以看出符号表的名字和对应的内存地址。EXPORT_SYMBOL标签内定义的函数对全部内核代码公开,不用修改内核代码就可以在您的内核模块中直接调用。

原文地址:http://blog.csdn.net/lisan04/archive/2009/04/16/4076013.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值