All of lore.kernel.org
 help / color / mirror / Atom feed
From: Aruna Hewapathirane <aruna.hewapathirane@gmail.com>
To: "Irfan Ullah (울라 이르판)" <irfan@dke.khu.ac.kr>
Cc: kernelnewbies <kernelnewbies@kernelnewbies.org>
Subject: Re: Kernel Module with multiple source files not initializing
Date: Tue, 5 Nov 2019 11:52:07 -0500	[thread overview]
Message-ID: <CAFSeFg_iRUdjUY1cjqVtmJw=TBYKvZcPAC3auSwP9=L9DiX35w@mail.gmail.com> (raw)
In-Reply-To: <CA+mB8Oz1Hu82nk2vbinhpOQW8uasp7_b8CFNf2jNovqP==OH6A@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 5189 bytes --]

>> On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (울라 이르판) <irfan@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@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@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

[-- Attachment #1.2: Type: text/html, Size: 6284 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  parent reply	other threads:[~2019-11-05 16:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-25  9:14 Kernel Module with multiple source files not initializing Irfan Ullah (울라 이르판)
2019-10-25 16:15 ` Abu Rasheda
2019-10-27  7:11   ` Irfan Ullah (울라 이르판)
2019-10-27  8:43 ` Bernd Petrovitsch
2019-10-28  9:43   ` Irfan Ullah (울라 이르판)
2019-10-28 10:04     ` Bernd Petrovitsch
2019-10-29  8:30       ` Irfan Ullah (울라 이르판)
2019-11-05 16:52 ` Aruna Hewapathirane [this message]
2019-11-07  1:50   ` Irfan Ullah (울라 이르판)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFSeFg_iRUdjUY1cjqVtmJw=TBYKvZcPAC3auSwP9=L9DiX35w@mail.gmail.com' \
    --to=aruna.hewapathirane@gmail.com \
    --cc=irfan@dke.khu.ac.kr \
    --cc=kernelnewbies@kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.