<div dir="ltr"><div dir="ltr" class="gmail_attr">>> On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) <<a href="mailto:irfan@dke.khu.ac.kr">irfan@dke.khu.ac.kr</a>> wrote:</div><div dir="ltr">>> Dear All,<div>>> I have developed a kernel module consists of one source file that sends and receives  message to the user space.  I have spitted the source code in two files, and now I  am trying to develop kernel module from these source files. After compiling >> and linking without any problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the module can be inserted and removed, but it does nothing. Code is in the attached file zipped file.</div><div>>> I tried many things , e.g.,  <a href="https://paguilar.org/?p=7" target="_blank">link</a>1, <a href="https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-multiple-source-files-not-initializing-886190/" target="_blank">link2</a>, and <a href="https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-wear/Documentation/kbuild/modules.txt" target="_blank">link3</a> etc.  but nothings worked out. </div><div>>> I also used <i>nm</i> to inspect the module, but, as expected, there was no signs of "__init and __exit" functions can in the output. <br></div><div><br></div><div>When you take a careful look at make's output we see that netlink_kernel_module.c <br>is not being compiled. See below:<br></div><div><br>aruna@debian:~/Downloads/kmod6$ make<br>make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/Downloads/kmod6 modules<br>make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'<br>make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64'<br>  CC [M]  /home/aruna/Downloads/kmod6/netlink_kernel_space.o <br>  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.o<br>  Building modules, stage 2.<br>  MODPOST 1 modules<br>  CC      /home/aruna/Downloads/kmod6/netlink_kernel_module.mod.o<br>  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.ko<br>make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'</div><div><br></div><div>This is why nm does not show hello_init or hello_exit.</div><br><div>>> Can you please help me: what's the problem/mistake I am doing?</div><div><br></div><div>Change your Makefile so the module name is not the same as the C source file. Let's say</div><div>we want the module to be named aruna.ko ( make up any name different to the C source file)<br></div><div><br>obj-m := aruna.o<br>aruna-objs := netlink_kernel_module.o netlink_kernel_space.o <br></div><div><br></div><div>and now make shows:<br><br></div><div>make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/kmod6/Kernel_User_comm modules<br>make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'<br>make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64'<br>  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_module.o  //   <-- THIS TIME IT COMPILES !<br>  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_space.o<br>  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.o<br>  Building modules, stage 2.<br>  MODPOST 1 modules<br>  CC      /home/aruna/kmod6/Kernel_User_comm/aruna.mod.o<br>  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.ko<br>make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'<br></div><div><br></div><div>and now nm aruna.ko shows:</div>aruna@debian:~/kmod6/Kernel_User_comm$ nm aruna.ko<br>0000000000000020 T cleanup_module<br>0000000000000080 T create_socket<br>0000000000000040 T data_update<br>                 U __fentry__<br>0000000000000020 t hello_exit               // WE HAVE hello_exit<br>0000000000000000 t hello_init                // WE HAVE hello_init<br>0000000000000000 T init_module<br>0000000000000070 T kernel_space_receiver<br>0000000000000050 T kernel_space_sender<br>0000000000000053 r __module_depends<br>0000000000000004 D pid<br>                 U printk<br>0000000000000000 D res<br>0000000000000000 D __this_module<br>0000000000000000 r __UNIQUE_ID_author2<br>0000000000000013 r __UNIQUE_ID_description1<br>0000000000000047 r __UNIQUE_ID_license0<br>000000000000005c r __UNIQUE_ID_vermagic0<br>0000000000000000 r ____versions<br><div><br></div><div>To get make to do this smoothly you will have to fix the multiple definition and</div><div>other errors I encountered along the way. And something's in your header<br></div><div>file really should belong in a C file :) heed Valdis's advice.<br></div><br><div>As a learning experience try this Makefile:</div><div>------------------------------------------------------------------------------------------------------<br>obj-m := aruna.o<br>aruna-objs := netlink_kernel_module.o netlink_kernel_space.o <br><br>SHELL += -x<br><br>all:<br>                make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules<br>clean:<br>        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean<br></div><div>------------------------------------------------------------------------------------------------------</div><div><br></div><div>One simple way to enhance the output of GNU Make is to redefine SHELL. 
<br>SHELL is a GNU Make built-in variable that contains the name of the 
shell to use when <br></div><div>GNU Make executes commands.<br></div><div><br></div><div>Reference: <a href="https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make">https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make</a><br><br></div><div>Since most shells 
have a -x option that causes them to print out each command they <br></div><div>are 
about to execute modifying SHELL in a Makefile by appendin -x causes 
every command <br></div><div>to be printed (usually preceded by +) as the Makefile is 
run.</div><div><br></div><div>Try:</div><div>make clean the make and go through the output can be an enlightening experience.<br><br></div><div>Ah the joys of building a kernel module that has more than one source file :-)-- <br></div></div><div dir="ltr"><br></div><div>Good luck - Aruna</div><br></div>