* All rights reserved.
*
* Filename: mod_a.c
* Description: This file
*
* Version: 1.0.0(07/12/2013~)
* Author: fulinux
* ChangeLog: 1, Release initial version on "07/12/2013 10:06:50 AM"
*
********************************************************************************/
#include
#include
#include
static int func1(void)
{
printk("In Func: %s...\n",__func__);
return 0;
}
EXPORT_SYMBOL(func1);
static int __init hello_init(void)
{
printk("Module 1, Init!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("Module 1, Exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
其中EXPORT_SYMBOL(func1)导出func1函数符号,保存函数地址和名称.
这个模块的第一个Makefile文件:
[lingyun@localhost mod1]$ ls
Makefile mod_a.c
[lingyun@localhost mod1]$ vim Makefile
[cpp]
obj-m:=mod1.o
mod1-y:=mod_a.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
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 *.ko.* *.mod.c .tmp_versions *odule* $(TARGET)
obj-m:=mod1.o
mod1-y:=mod_a.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
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 *.ko.* *.mod.c .tmp_versions *odule* $(TARGET)
其中内嵌对象 - obj-y,可加载模块 - obj-m,KERNELDIR指向指向内核代码目录。
编译编译并加载:
[lingyun@localhost mod1]$ ls
Makefile mod_a.c
[lingyun@localhost mod1]$ make
make -C /lib/modules/2.6.32-279.el6.x86_64/build M=/usr/local/src/lingyun/fulinux/export_symbol/mod1 modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-279.el6.x86_64'
CC [M] /usr/local/src/lingyun/fulinux/export_symbol/mod1/mod_a.o
LD [M] /usr/local/src/lingyun/fulinux/export_symbol/mod1/mod1.o
Building modules, stage 2.
MODPOST 1 modules
CC /usr/local/src/lingyun/fulinux/export_symbol/mod1/mod1.mod.o
LD [M] /usr/local/src/lingyun/fulinux/export_symbol/mod1/mod1.ko.unsigned
NO SIGN [M] /usr/local/src/lingyun/fulinux/export_symbol/mod1/mod1.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.32-279.el6.x86_64'
[lingyun@localhost mod1]$ sudo insmod mod1.ko
[lingyun@localhost mod1]$ cat /proc/kallsyms | grep func1
0000000000000000 r __ksymtab_func1 [mod1]
0000000000000000 r __kstrtab_func1 [mod1]
0000000000000000 r __kcrctab_func1 [mod1]
0000000000000000 T func1 [mod1]
[lingyun@localhost mod1]$
[lingyun@localhost mod1]$ dmesg | grep Module
- User ID: CentOS (Kernel Module GPG key)
Module 1, Init!
第二个模块的文件如下:
[lingyun@localhost mod1]$ cd ../mod2/
[lingyun@localhost mod2]$ vim mod_b.c
[cpp]
/*********************************************************************************
* Copyright: (C) 2013 fulinux
* All rights reserved.
*
* Filename: mod_b.c
* Description: This file
*
* Version: 1.0.0(07/12/2013~)
* Author: fulinux
* ChangeLog: 1, Release initial version on "07/12/2013 10:29:55 AM"
*
****************