Linux内核模块:模块的编译

来源:互联网 发布:淘宝网店装修工具 编辑:程序博客网 时间:2024/05/22 06:41

模块源码:

#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");static int hello_init(void){  printk(KERN_ALERT " Hello World enter\n");  return 0;}static void hello_exit(void){  printk(KERN_ALERT " Hello World exit\n ");}module_init(hello_init);module_exit(hello_exit);MODULE_AUTHOR("Song Baohua");MODULE_DESCRIPTION("A simple Hello World Module");MODULE_ALIAS("a simplest module");

Makefile:

obj-m := hello.o

使用如下命令编译helloworld模块:

make -C /usr/src/linux-2.6.15.5/ M=/driver_study/ modules

如果当前处于模块所在的目录,则以下命令与上述命令同等:

make –C /usr/src/linux-2.6.15.5 M=$(pwd) modules

其中 -C后指定的是Linux内核源码目录,而M=后指定的是hello.c和Makefile所在的目录,编译结果如下所示:

[root@localhost driver_study]# make -C /usr/src/linux-2.6.15.5/M=/driver_study/ modulesmake: Entering directory '/usr/src/linux-2.6.15.5'CC [M] /driver_study/hello.o/driver_study/hello.c:18:35: warning: no newline at end of fileBuilding modules, stage 2.MODPOSTCC /driver_study/hello.mod.oLD [M] /driver_study/hello.komake: Leaving directory '/usr/src/linux-2.6.15.5'

从中可以看出,编译过程经历了这样的步骤:先进入Linux内核所在的目录,并编译出hello.o文件,运行MODPOST会生成临时hello.mod.c文件,而后根据此文件编译出hello.mod.o,之后连接hello.o和hello.mod.o文件得到模块目标文件hello.ko,最后离开Linux内核所在的目录。

中间生成的hello.mod.c文件的源代码如下所示:

#include <linux/module.h>#include <linux/vermagic.h>#include <linux/compiler.h> MODULE_INFO(vermagic, VERMAGIC_STRING);struct module _ _this_module_ _attribute_ _((section(".gnu.linkonce.this_module"))) = { .name = KBUILD_MODNAME, .init = init_module, #ifdef CONFIG_MODULE_UNLOAD  .exit = cleanup_module, #endif};static const char _ _module_depends[]_ _attribute_used_ __ _attribute_ _((section(".modinfo"))) ="depends=";

hello.mod.o产生了ELF的两个节,即modinfo和.gun.linkonce.this_module。

如果一个模块包括多个.c文件,则应该如下写Makefile:

obj-m := modulename.omodule-objs := file1.o file2.o



参考资料:《linux设备驱动开发详解》 宋宝华

0 0
原创粉丝点击