让我们编写以下最简单的模块源文件:
#include <linux/init.h>
#include <linux/module.h>
static int __init md_init(void){
printk("Hello kernel");
return 0;
}
static void __exit md_exit(void){
printk("Goodbye kernel");
}
module_init(md_init);
module_exit(md_exit);
Run Code Online (Sandbox Code Playgroud)
如何在预处理后看到此源?我想知道如何__init和__exit宏部署和什么是module_init(md_init)和module_exit(md_exit)?这个怎么运作?
,查看中间文件。即编译器预处理后的.i文件和.s文件,更改Makefile并添加EXTRA_CFLAGS=\xe2\x80\x99-save-temps\xe2\x80\x99
\n\n生成文件:
\n\n\n\n\nmake -C /usr/lib/modules/$(shell uname -r)/build M=$(shell pwd)\n 模块 EXTRA_CFLAGS=\xe2\x80\x99 -save-temps \xe2\x80\x99
\n
之后,一旦运行“make”,您就可以在中看到 your_module_filename.i
\n\n\n\n\nls /usr/lib/modules/$(uname -r)/build/{your_modulename.i}
\n
并且带有预处理器更改的源代码几乎在文件末尾可用。
\n如果您的驱动程序位于内核中,则可以执行以下操作:
制作模块路径/srcfile.i
例如,我在drivers / staging /下创建了一个测试目录,将文件放在其中,创建了一个简单的Kconfig和Makefile,在分阶段中更新了Kconfig和Makefile,然后运行
使驱动程序/阶段/测试/test.i
如果源代码在内核树之外,但是设置了Kconfig和Makefile,则:
使-C / path / to / kernel / src M = / path / to / driver srcfile.i
结果是init和exit宏:
static int __attribute__ ((__section__(".init.text"))) __attribute__((__cold__)) __attribute__((no_instrument_function)) md_init(void)
{
printk("Hello kernel");
return 0;
}
static void __attribute__ ((__section__(".exit.text"))) __attribute__((__used__)) __attribute__((__cold__)) __attribute__((no_instrument_function)) md_exit(void)
{
printk("Goodbye kernel");
}
Run Code Online (Sandbox Code Playgroud)
如果您只想获得内核模块的预处理输出,请不要使用Makefile,因为Makefile(子make)将尝试生成能够插入内核的目标文件。这与 矛盾gcc -E,后者在预处理后停止。因此,只需使用以下命令即可gcc:
gcc -E new.c -I$TREE/include -I$TREE/arch/x86/include -I$TREE/include/uapi
Run Code Online (Sandbox Code Playgroud)
-E是为了获取预处理的输出,$TREE 是内核树的位置,如果您使用其他架构,则更改 x86。我们知道,gcc需要包含 dir 参数-I,因此将所有内核包含目录传递给-I。希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
2688 次 |
| 最近记录: |