Re: kernel module spanning multiple files
Whereas for a simple single file hello world kernel module, i see that the symbols in the object (hello_world.ko) for the init and exit routines. <snip> [root@localhost #] nm hello_world.ko 0000000000000000 r ____versions 000000000000000c r __mod_license17 0000000000000020 r __mod_srcversion30 0000000000000060 r __mod_vermagic5 0000000000000000 r __mod_version18 0000000000000043 r __module_depends 0000000000000000 D __this_module 0000000000000000 T cleanup_module 0000000000000000 t hello_exit < --- -------- 0000000000000000 t hello_init < -- --------- 0000000000000000 T init_module U printk <snip> Regards, amit On Wed, Jun 15, 2011 at 3:29 PM, amit mehta <gmate.amit@gmail.com> wrote:
Hi,
After looking at some Makefiles under Linux Driver sources which uses multiple files to create the target, i tried to write a simple hello world kernel module spanning multiple files for learning purpose. But I'm observing a strange behavior. After inserting the module, I don't see the prints getting logged into the syslog(/var/log/message) or in the dmesg. Maybe its just a C programming mistake, or is due to an incorrect Makefile but as of now I'm clueless.
Here are the code snippet from the four files(hello_multi.c, helper.c, myhdr.h, Makefile)
[root@localhost #] cat hello_multi.c #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include "myhdr.h" static int __init hello_init(void) { printk(KERN_WARNING "Hello world\n"); foo(); //defined in anther file return 0; }
static void __exit hello_exit(void) { printk(KERN_WARNING "goodbye world\n"); }
module_init(hello_init); module_exit(hello_exit);
MODULE_LICENSE("GPL v2");
[root@localhost #] cat helper.c #include <linux/kernel.h> #include "myhdr.h" void foo(void) { printk(KERN_WARNING "inside foo\n"); }
[root@localhost #] cat myhdr.h #ifndef __MY_HDR_H #define __MY_HDR_H void foo(void); #endif
[root@localhost #] cat Makefile obj-m += hello_multi.o hello_multi-objs := helper.o KERNELDIR=/lib/modules/$(shell uname -r)/build PWD=$(shell pwd)
modules: make -C $(KERNELDIR) M=$(PWD) modules
modules_install: make -C $(KERNELDIR) M=$(PWD) modules_install
clean: rm -rf *.o *.ko *~ core .depend .tmp_versions .*.cmd *.mod.c .PHONY: modules modules_install clean
Invoking 'make modules' completes with success and i am able to insert this module.
[root@localhost #] make modules make -C /lib/modules/2.6.31-23-generic/build M=/home/superman/programming/ldd/hello_multi modules make[1]: Entering directory `/usr/src/linux-headers-2.6.31-23-generic' CC [M] /home/superman/programming/ldd/hello_multi/helper.o LD [M] /home/superman/programming/ldd/hello_multi/hello_multi.o Building modules, stage 2. MODPOST 1 modules CC /home/superman/programming/ldd/hello_multi/hello_multi.mod.o LD [M] /home/superman/programming/ldd/hello_multi/hello_multi.ko make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-23-generic' [root@localhost #] dmesg -c [root@localhost #] insmod hello_multi.ko [root@localhost #] dmesg|tail <-- -- No messages here
[root@localhost #] cat /proc/modules |grep -i hello hello_multi 727 0 - Live 0xffffffffa020b000 (PN
[root@localhost #] nm hello_multi.ko 0000000000000000 r ____versions 0000000000000000 r __mod_srcversion26 0000000000000040 r __mod_vermagic5 0000000000000023 r __module_depends 0000000000000000 D __this_module 0000000000000000 T foo U printk
Regards, amit
participants (1)
-
amit mehta