Kernel Module with multiple source files not initializing

Aruna Hewapathirane aruna.hewapathirane at gmail.com
Tue Nov 5 11:52:07 EST 2019


>> On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) <irfan at dke.khu.ac.kr>
wrote:
>> Dear All,
>> 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.
>> I tried many things , e.g.,  link <https://paguilar.org/?p=7>1, link2
<https://www.linuxquestions.org/questions/linux-kernel-70/kernel-module-with-multiple-source-files-not-initializing-886190/>,
and link3
<https://android.googlesource.com/kernel/msm/+/android-msm-dory-3.10-kitkat-wear/Documentation/kbuild/modules.txt>
etc.
but nothings worked out.
>> I also used *nm* to inspect the module, but, as expected, there was no
signs of "__init and __exit" functions can in the output.

When you take a careful look at make's output we see that
netlink_kernel_module.c
is not being compiled. See below:

aruna at debian:~/Downloads/kmod6$ make
make -C /lib/modules/3.16.0-4-amd64/build M=/home/aruna/Downloads/kmod6
modules
make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'
make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64'
  CC [M]  /home/aruna/Downloads/kmod6/netlink_kernel_space.o
  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/aruna/Downloads/kmod6/netlink_kernel_module.mod.o
  LD [M]  /home/aruna/Downloads/kmod6/netlink_kernel_module.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'

This is why nm does not show hello_init or hello_exit.

>> Can you please help me: what's the problem/mistake I am doing?

Change your Makefile so the module name is not the same as the C source
file. Let's say
we want the module to be named aruna.ko ( make up any name different to the
C source file)

obj-m := aruna.o
aruna-objs := netlink_kernel_module.o netlink_kernel_space.o

and now make shows:

make -C /lib/modules/3.16.0-4-amd64/build
M=/home/aruna/kmod6/Kernel_User_comm modules
make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'
make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64'
  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_module.o  //
<-- THIS TIME IT COMPILES !
  CC [M]  /home/aruna/kmod6/Kernel_User_comm/netlink_kernel_space.o
  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/aruna/kmod6/Kernel_User_comm/aruna.mod.o
  LD [M]  /home/aruna/kmod6/Kernel_User_comm/aruna.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'

and now nm aruna.ko shows:
aruna at debian:~/kmod6/Kernel_User_comm$ nm aruna.ko
0000000000000020 T cleanup_module
0000000000000080 T create_socket
0000000000000040 T data_update
                 U __fentry__
0000000000000020 t hello_exit               // WE HAVE hello_exit
0000000000000000 t hello_init                // WE HAVE hello_init
0000000000000000 T init_module
0000000000000070 T kernel_space_receiver
0000000000000050 T kernel_space_sender
0000000000000053 r __module_depends
0000000000000004 D pid
                 U printk
0000000000000000 D res
0000000000000000 D __this_module
0000000000000000 r __UNIQUE_ID_author2
0000000000000013 r __UNIQUE_ID_description1
0000000000000047 r __UNIQUE_ID_license0
000000000000005c r __UNIQUE_ID_vermagic0
0000000000000000 r ____versions

To get make to do this smoothly you will have to fix the multiple
definition and
other errors I encountered along the way. And something's in your header
file really should belong in a C file :) heed Valdis's advice.

As a learning experience try this Makefile:
------------------------------------------------------------------------------------------------------
obj-m := aruna.o
aruna-objs := netlink_kernel_module.o netlink_kernel_space.o

SHELL += -x

all:
         make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
------------------------------------------------------------------------------------------------------

One simple way to enhance the output of GNU Make is to redefine SHELL.
SHELL is a GNU Make built-in variable that contains the name of the shell
to use when
GNU Make executes commands.

Reference:
https://www.cmcrossroads.com/article/tracing-rule-execution-gnu-make

Since most shells have a -x option that causes them to print out each
command they
are about to execute modifying SHELL in a Makefile by appendin -x causes
every command
to be printed (usually preceded by +) as the Makefile is run.

Try:
make clean the make and go through the output can be an enlightening
experience.

Ah the joys of building a kernel module that has more than one source file
:-)--

Good luck - Aruna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20191105/e6d695f3/attachment.html>


More information about the Kernelnewbies mailing list