#include <linux/kernel.h> #include <linux/module.h> static int __init mini2440_hello_module_init(void) { printk("Hello, Mini2440 module is installed !\n"); return 0; } static void __exit mini2440_hello_module_cleanup(void) { printk("Good-bye, Mini2440 module was removed!\n"); } module_init(mini2440_hello_module_init); module_exit(mini2440_hello_module_cleanup); MODULE_LICENSE("GPL");以上是driver/char/ mini2440_hello_module.c
上面作法不方便做测试,如果想要将任意一个目录下的.c文件比如/home/mymodule.c编译成一个模块,可以
在/home下创建一个Makefile
内容是
ifneq ($(KERNELRELEASE),) obj-m := mymodule.o else KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2 #KDIR := /lib/modules/$(uname-r)*/build all: make -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers endifline 2 名字要与mymodule.c对应,写成mymodule.o
ifneq ($(KERNELRELEASE),) obj-m:= mymodule.o mymodule-objs := file1.o file2.o file3.o else KDIR := /lib/modules/$(uname-r)*/build/ all: make -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers *~ modules.order endif
/**file1.c**/ #include <linux/kernel.h> #include <linux/module.h> extern test2(void); extern test3(void); static int __init mini2440_hello_module_init(void) { printk("Hello, Mini2440 module is installed !\n"); test2(); test3(); return 0; } static void __exit mini2440_hello_module_cleanup(void) { printk("Good-bye, Mini2440 module was removed!\n"); } module_init(mini2440_hello_module_init); module_exit(mini2440_hello_module_cleanup); MODULE_LICENSE("GPL");
/**file2.c**/ #include <linux/kernel.h> void test2(void) { printk("test2\n"); }
/**file3.c**/ #include <linux/kernel.h> void test3(void) { printk("test3 \n"); }执行make,即可生成mymodule.ko
注意:上面的Makefile中,
①写成obj-y := mymodule.o或obj- := mymodule.o,执行make均不会生成file1.o,file2.o,file3.o,mymodule,ko
②写成obj-m:= mymodule.o,make时会生成file1.o,file2.o,file3.o,mymodule.ko
③
obj-m := mymodule.o 和mymodule-objs := file1.o file2.o file3.o,名字需要对应,生成mymodule.ko,又如
obj-m := song.o 和song-objs := file1.o file2.o file3.o,生成song.ko
[root@localhost test]# arm-linux-objdump -S mymodule.ko mymodule.ko: file format elf32-littlearm Disassembly of section .exit.text: 00000000 <cleanup_module>: 0: e1a0c00d mov ip, sp 4: e92dd800 push {fp, ip, lr, pc} 8: e24cb004 sub fp, ip, #4 ; 0x4 c: e59f0004 ldr r0, [pc, #4] ; 18 <cleanup_module+0x18> 10: ebfffffe bl 0 <printk> 14: e89da800 ldm sp, {fp, sp, pc} 18: 00000000 .word 0x00000000 Disassembly of section .init.text: 00000000 <init_module>: 0: e1a0c00d mov ip, sp 4: e92dd800 push {fp, ip, lr, pc} 8: e24cb004 sub fp, ip, #4 ; 0x4 c: e59f0008 ldr r0, [pc, #8] ; 1c <init_module+0x1c> 10: ebfffffe bl 0 <printk> 14: e3a00000 mov r0, #0 ; 0x0 18: e89da800 ldm sp, {fp, sp, pc} 1c: 00000028 .word 0x00000028
[root@localhost test]# arm-linux-objdump -S mymodule.o mymodule.o: file format elf32-littlearm Disassembly of section .exit.text: 00000000 <cleanup_module>: 0: e1a0c00d mov ip, sp 4: e92dd800 push {fp, ip, lr, pc} 8: e24cb004 sub fp, ip, #4 ; 0x4 c: e59f0004 ldr r0, [pc, #4] ; 18 <cleanup_module+0x18> 10: ebfffffe bl 0 <printk> 14: e89da800 ldm sp, {fp, sp, pc} 18: 00000000 .word 0x00000000 Disassembly of section .init.text: 00000000 <init_module>: 0: e1a0c00d mov ip, sp 4: e92dd800 push {fp, ip, lr, pc} 8: e24cb004 sub fp, ip, #4 ; 0x4 c: e59f0008 ldr r0, [pc, #8] ; 1c <init_module+0x1c> 10: ebfffffe bl 0 <printk> 14: e3a00000 mov r0, #0 ; 0x0 18: e89da800 ldm sp, {fp, sp, pc} 1c: 00000028 .word 0x00000028 [root@localhost test]#refer to