我在KALI中使用内核5.10.11,我试图学习内核模块编程,但我无法构建模块。我已经尝试了互联网上给出的所有解决方案,但它们对我不起作用,或者我做错了。
下面是我的c文件:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int helloWorld_init(void)
{
printk(KERN_DEBUG "Hello World!\n");
return 0;
}
static void helloWorld_exit(void)
{
printk(KERN_DEBUG "Removing Module\n");
}
module_init(helloWorld_init);
module_exit(helloWorld_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mukul Mehar");
MODULE_DESCRIPTION("first kernel module");
以及Makefile:
obj-m += helloWorld.o
KDIR = /usr/src/linux-headers-5.10.11/
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
我收到的输出是:
make -C /usr/src/linux-headers-5.10.11/ M=/home/mukul/Documents/Eudyptula/challenge-1 modules
make[1]: Entering directory '/usr/src/linux-headers-5.10.11'
make[2]: *** No rule to make target '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.o', needed by '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.mod'. Stop.
make[1]: *** [Makefile:1805: /home/mukul/Documents/Eudyptula/challenge-1] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.10.11'
make: *** [Makefile:7: all] Error 2
发布于 2021-01-31 18:30:47
下面的makefile可以为你工作吗?
obj-m += helloworld.o
KDIR = /lib/modules/$(shell uname -r)/build/
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
发布于 2021-01-31 21:25:28
您尝试过Kbuild方法吗?
在helloworld.c所在的目录中创建一个名为Kbuild的文件,内容如下:
obj-m += helloworld.o
从同一目录中启动构建:
$ make -C /lib/modules/`uname -r`/build M=`pwd`
make: Entering directory '/usr/src/linux-headers-5.4.0-65-generic'
CC [M] .../helloworld.o
Building modules, stage 2.
MODPOST 1 modules
CC [M] .../helloworld.mod.o
LD [M] .../helloworld.ko
make: Leaving directory '/usr/src/linux-headers-5.4.0-65-generic'
$ ls -l helloworld.ko
-rw-rw-r-- 1 xxxx xxxx 4144 janv. 31 14:50 helloworld.ko
然后,使用insmod
/rmmod
将模块加载到内核或从内核卸载模块:
$ sudo insmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
$ sudo rmmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
[16497.208337] Removing Module
https://stackoverflow.com/questions/65972180
复制相似问题