a
w@w:~/linux_kernel/export/a$ cat helloa.c
#include <linux/init.h>
#include <linux/module.h>
static int num = 100;
static void show(void)
{
printk("aaaa: num =%d \n",num);
}
static int hello_init(void)
{
printk("hello_init \n");
return 0;
}
static void hello_exit(void)
{
printk("hello_exit \n");
return;
}
EXPORT_SYMBOL(num);
EXPORT_SYMBOL(show);
module_init(hello_init);
module_exit(hello_exit);
Makefile
w@w:~/linux_kernel/export/a$ cat Makefile
ifneq ($(KERNELRELEASE),)
$(info "2nd")
obj-m:=helloa.o 这里文件名要与.c文件对应
else
KDIR :=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
all:
$(info "1st")
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.symvers *.cmd *.mod.c *.order
endif
编译
w@w:~/linux_kernel/export/a$ sudo make
"1st"
make -C /lib/modules/5.4.0-104-generic/build M=/home/w/linux_kernel/export/a modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-104-generic'
"2nd"
CC [M] /home/w/linux_kernel/export/a/helloa.o
"2nd"
Building modules, stage 2.
MODPOST 1 modules
WARNING: modpost: missing MODULE_LICENSE() in /home/w/linux_kernel/export/a/helloa.o
see include/linux/module.h for more information
WARNING: "show" [/home/w/linux_kernel/export/a/helloa] is a static EXPORT_SYMBOL
WARNING: "num" [/home/w/linux_kernel/export/a/helloa] is a static EXPORT_SYMBOL
CC [M] /home/w/linux_kernel/export/a/helloa.mod.o
LD [M] /home/w/linux_kernel/export/a/helloa.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-104-generic'
w@w:~/linux_kernel/export/a$ ls
helloa.c helloa.ko helloa.mod helloa.mod.c helloa.mod.o helloa.o Makefile modules.order Module.symvers
w@w:~/linux_kernel/export/a$ cat Module.symvers
0x88729d93 show /home/w/linux_kernel/export/a/helloa EXPORT_SYMBOL
0x42832c3b num /home/w/linux_kernel/export/a/helloa EXPORT_SYMBOL
将a模块中生成的Module.symvers copy到b目录下
w@w:~/linux_kernel/export/a$ sudo cp Module.symvers ../b/
清除缓存日志
w@w:~/linux_kernel/export/a$ dmesg
[ 549.186132] hello_init
[ 584.376325] hello_exit
w@w:~/linux_kernel/export/a$ sudo dmesg -c
w@w:~/linux_kernel/export/a$ dmesg
w@w:~/linux_kernel/export/a$ sudo insmod helloa.ko
w@w:~/linux_kernel/export/a$ dmesg
[13425.220292] helloa: module license 'unspecified' taints kernel.
[13425.220294] Disabling lock debugging due to kernel taint
[13425.220586] hello_init
b
w@w:~/linux_kernel/export/b$ cat hellob.c
#include <linux/init.h>
#include <linux/module.h>
extern int num;
extern void show(void);
static int hello_init(void)
{
printk("hello_init %d\n",num);
show();
return 0;
}
static void hello_exit(void)
{
printk("hello_exit \n");
return;
}
module_init(hello_init);
module_exit(hello_exit);
w@w:~/linux_kernel/export/b$ cat Makefile
ifneq ($(KERNELRELEASE),)
$(info "2nd")
obj-m:=hellob.o
else
KDIR :=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
all:
$(info "1st")
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.symvers *.cmd *.mod.c *.order
endif
w@w:~/linux_kernel/export/b$ ls
hellob.c Makefile Module.symvers
w@w:~/linux_kernel/export/b$ sudo make
"1st"
make -C /lib/modules/5.4.0-104-generic/build M=/home/w/linux_kernel/export/b modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-104-generic'
"2nd"
CC [M] /home/w/linux_kernel/export/b/hellob.o
"2nd"
Building modules, stage 2.
MODPOST 1 modules
WARNING: modpost: missing MODULE_LICENSE() in /home/w/linux_kernel/export/b/hellob.o
see include/linux/module.h for more information
CC [M] /home/w/linux_kernel/export/b/hellob.mod.o
LD [M] /home/w/linux_kernel/export/b/hellob.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-104-generic'
w@w:~/linux_kernel/export/b$ sudo insmod hellob.ko
w@w:~/linux_kernel/export/b$ dmesg
[13425.220292] helloa: module license 'unspecified' taints kernel.
[13425.220294] Disabling lock debugging due to kernel taint
[13425.220586] hello_init
[13630.617249] hello_init 100
[13630.617250] aaaa: num =100
必须把a里面export出来的Module.symvers copy到b目录,否则b无法调用a中导出的变量
w@w:~/linux_kernel/export/b$ sudo rmmod helloa.ko
rmmod: ERROR: Module helloa is in use by: hellob
w@w:~/linux_kernel/export/b$ sudo rmmod hellob.ko
w@w:~/linux_kernel/export/b$ dmesg
[13425.220292] helloa: module license 'unspecified' taints kernel.
[13425.220294] Disabling lock debugging due to kernel taint
[13425.220586] hello_init
[13630.617249] hello_init 100
[13630.617250] aaaa: num =100
[14299.890225] hello_exit
w@w:~/linux_kernel/export/b$ sudo rmmod helloa.ko
w@w:~/linux_kernel/export/b$ dmesg
[13425.220292] helloa: module license 'unspecified' taints kernel.
[13425.220294] Disabling lock debugging due to kernel taint
[13425.220586] hello_init
[13630.617249] hello_init 100
[13630.617250] aaaa: num =100
[14299.890225] hello_exit
[14312.594776] hello_exit
网友评论