EXPORT_SYMBOL的用法

来源:互联网 发布:郑州北京百知尚行科技 编辑:程序博客网 时间:2024/05/23 01:56

      linux2.6的“/proc/kallsyms”文件对应着内核符号表,记录了符号以及符号所在的内存地址。用cat的结果如下:

模块可以使用如下宏导出符号到内核符号表:
EXPORT_SYMBOL(符号名);  
EXPORT_SYMBOL_GPL(符号名)  只适用于包含GPL许可权的模块。
导出的符号可以被其他模块使用,不过使用之前一定要extern声明一下。这就是作为模块之间互相通信的一种方法。
      举一个代码演示:
一个文件是hello.c文件,定义2个函数,用于导出 

#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模块导出的函数:

#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模块(验证失败,无法加载test.ko)。然后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