Linux内核模块:模块的编译

模块源码:

#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/ modules
make: 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 file
Building modules, stage 2.
MODPOST
CC /driver_study/hello.mod.o
LD [M] /driver_study/hello.ko
make: 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.o
module-objs := file1.o file2.o



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值