2011年10月23日 星期日

EXPORT_SYMBOL用法

【轉貼】夜深人靜

EXPORT_SYMBOL定義的函數對全部kernel代碼公開,不用修改kernel code就可以在您的kernel模組中直接使用。您還可以手動修改kernel code来導出另外的函數,用於重新编譯並加戴新kernel後的測試。


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.

ref:夜深人靜

BTW:
  • EXPORT_SYMBOL和EXPORT_SYMBOL_GPL共存,不知二者有何區别?
        ==>如果你module的協議不是GPL, 那麼EXPORT_SYMBOL_GPL導出的那些symbol,你就不能用 (= =")

  • 所有EXPORT_SYMBOL的變數函式都在  /proc/kallsyms  (2.6.32)




EX: Here

/*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,v 0.0 2007/01/26 02:02:51 leno Exp $
## Keywords:
## X-URL:
ifneq ($(KERNELRELEASE),)
obj-m := -DEXPORT_SYMTAB #加载该参数的地方
obj-m := mod2.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

沒有留言:

張貼留言