From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=3.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 373AFC5DF60 for ; Tue, 5 Nov 2019 16:53:10 +0000 (UTC) Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AD13F21928 for ; Tue, 5 Nov 2019 16:53:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qS+6kdos" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD13F21928 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kernelnewbies-bounces@kernelnewbies.org Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.92.3) (envelope-from ) id 1iS249-0002Bm-Gf; Tue, 05 Nov 2019 11:52:25 -0500 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by shelob.surriel.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) (envelope-from ) id 1iS245-0002BB-K5 for kernelnewbies@kernelnewbies.org; Tue, 05 Nov 2019 11:52:22 -0500 Received: by mail-ed1-x544.google.com with SMTP id s10so1750999edi.5 for ; Tue, 05 Nov 2019 08:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VqBNsbaMEEG5SiuuOXIap62O7IslXHWzu+DQ9UmnnBY=; b=qS+6kdos6WibS1+GEyCV5/lcU09Y3Q/GAKotX8S7Zyr1b/CTxG9dthouABmmMrXppj XgsGfopqwOkiO092m4LOxNoRTUIFtHKCLqE0vDxquJbgolLioAUAtGGUb4HKTjEzqcxp V1ROaKt8DygdGPC/GuNMJ24DbnJVUZeBYk4uB55uLgX0zCiHV931GkcqLiRn22ap9Bza SKmXJh6lr1CxZ5WVCZ67cTJBEJE9NzcT1tloMC2N3tbC+78/Wl01Ge5GxIg3YgVYeHGH Z9f0//9dSiZ9D/5y7cmAMgwbg11BXdUzyFupHe7M7rGO0EelTcA76UISZZzyY/r7PJE/ KFjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VqBNsbaMEEG5SiuuOXIap62O7IslXHWzu+DQ9UmnnBY=; b=gP6klWRjUFcocEeO3VIOYNsQ1uwVYPoJ9GJYBVnzXT0sR5oEpsnuksn6ztt5yd/+zT 2Hk6a79rWJCLCXQzqxB4q4v2l09PZVe7+FL034NYlxCWtfdiyzCPt0JstIZKcBLGlpFE 5ExvoG5r9YyUv0sYInMZue7UAJepf+KZn5hnnfzNnTQjgyb4p6GV7Z57dXdMWe8mYOEm zMzjoc6bUS2q+mf0k7opjMvwTd5R9Fbbf6sy5Osp2JgIIS/FWaH+lQBDCBjw1e9dtfEd Pn7mj4TA2Os9e7ykMs4zN+CAZQvfW+8VoVZN+2PACPIDq9pMTphpgdCJdul3Nd5EbpCF huaA== X-Gm-Message-State: APjAAAXyjPHXHSspA7UuWyPRWYwChZx1BmaORbzAjniKeSZzYmQzIbRx c3yD9JS0vDyxzV3xo/z+vqBd4DoiSAW3LRNoYlo= X-Google-Smtp-Source: APXvYqyCSYm66I4RkfMJL3+20W0x7nqx6+jQ2zYpPnMYOYtcPZf1L8Z7F//T7N6NopkxQBPcYRc1fCJHVEEFpA83loI= X-Received: by 2002:a50:ec89:: with SMTP id e9mr3892042edr.104.1572972738167; Tue, 05 Nov 2019 08:52:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Aruna Hewapathirane Date: Tue, 5 Nov 2019 11:52:07 -0500 Message-ID: Subject: Re: Kernel Module with multiple source files not initializing To: =?UTF-8?B?SXJmYW4gVWxsYWggKOyauOudvCDsnbTrpbTtjJAp?= Cc: kernelnewbies X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============8504598920232317782==" Errors-To: kernelnewbies-bounces@kernelnewbies.org --===============8504598920232317782== Content-Type: multipart/alternative; boundary="0000000000004cc8a505969c4191" --0000000000004cc8a505969c4191 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable >> On Fri, Oct 25, 2019 at 5:16 AM Irfan Ullah (=EC=9A=B8=EB=9D=BC =EC=9D= =B4=EB=A5=B4=ED=8C=90) 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=E2=80=99t see any of the printk() I wrote, in fact, the m= odule can be inserted and removed, but it does nothing. Code is in the attached file zipped file. >> I tried many things , e.g., link 1, link2 , and link3 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=3D/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 :=3D aruna.o aruna-objs :=3D netlink_kernel_module.o netlink_kernel_space.o and now make shows: make -C /lib/modules/3.16.0-4-amd64/build M=3D/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 :=3D aruna.o aruna-objs :=3D netlink_kernel_module.o netlink_kernel_space.o SHELL +=3D -x all: make -C /lib/modules/$(shell uname -r)/build M=3D$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=3D$(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 --0000000000004cc8a505969c4191 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
>> On Fri, Oct= 25, 2019 at 5:16 AM Irfan Ullah (=EC=9A=B8=EB=9D=BC =EC=9D=B4=EB=A5=B4=ED= =8C=90) <irfan@dke.khu.ac.kr&= gt; wrote:
>> Dear All,
>> I have dev= eloped a kernel module consists of one source file that sends and receives= =C2=A0 message to the user space.=C2=A0 I have spitted the source code in t= wo files, and now I=C2=A0 am trying to develop kernel module from these sou= rce files. After compiling >> and linking without any problems, I ins= mod the module but I didn=E2=80=99t see any of the printk() I wrote, in fac= t, 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= .,=C2=A0=C2=A0lin= k1,=C2=A0link2, and=C2=A0link3=C2=A0etc.=C2=A0 but nothings worked = out.=C2=A0
>> I also used=C2=A0nm=C2=A0to inspect th= e module, but, as expected, there was no signs of "__init and __exit&q= uot; functions can in the output.

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

aruna@debian:~/Down= loads/kmod6$ make
make -C /lib/modules/3.16.0-4-amd64/build M=3D/home/ar= una/Downloads/kmod6 modules
make[1]: Entering directory '/usr/src/li= nux-headers-3.16.0-4-amd64'
make[1]: Entering directory `/usr/src/li= nux-headers-3.16.0-4-amd64'
=C2=A0 CC [M] =C2=A0/home/aruna/Download= s/kmod6/netlink_kernel_space.o
=C2=A0 LD [M] =C2=A0/home/aruna/Download= s/kmod6/netlink_kernel_module.o
=C2=A0 Building modules, stage 2.
=C2= =A0 MODPOST 1 modules
=C2=A0 CC =C2=A0 =C2=A0 =C2=A0/home/aruna/Download= s/kmod6/netlink_kernel_module.mod.o
=C2=A0 LD [M] =C2=A0/home/aruna/Down= loads/kmod6/netlink_kernel_module.ko
make[1]: Leaving directory '/us= r/src/linux-headers-3.16.0-4-amd64'

This is wh= y nm does not show hello_init or hello_exit.

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

<= /div>
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.k= o ( make up any name different to the C source file)

obj-= m :=3D aruna.o
aruna-objs :=3D netlink_kernel_module.o netlink_kernel_sp= ace.o

and now make shows:

m= ake -C /lib/modules/3.16.0-4-amd64/build M=3D/home/aruna/kmod6/Kernel_User_= comm modules
make[1]: Entering directory '/usr/src/linux-headers-3.1= 6.0-4-amd64'
make[1]: Entering directory `/usr/src/linux-headers-3.1= 6.0-4-amd64'
=C2=A0 CC [M] =C2=A0/home/aruna/kmod6/Kernel_User_comm/= netlink_kernel_module.o=C2=A0 //=C2=A0=C2=A0 <-- THIS TIME IT COMPILES !=
=C2=A0 CC [M] =C2=A0/home/aruna/kmod6/Kernel_User_comm/netlink_kernel_s= pace.o
=C2=A0 LD [M] =C2=A0/home/aruna/kmod6/Kernel_User_comm/aruna.o=C2=A0 Building modules, stage 2.
=C2=A0 MODPOST 1 modules
=C2=A0 CC= =C2=A0 =C2=A0 =C2=A0/home/aruna/kmod6/Kernel_User_comm/aruna.mod.o
=C2= =A0 LD [M] =C2=A0/home/aruna/kmod6/Kernel_User_comm/aruna.ko
make[1]: Le= aving directory '/usr/src/linux-headers-3.16.0-4-amd64'

and now nm aruna.ko shows:
aruna@debian:~/kmod6/Kern= el_User_comm$ nm aruna.ko
0000000000000020 T cleanup_module
000000000= 0000080 T create_socket
0000000000000040 T data_update
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0U __fentry__
00000000000= 00020 t hello_exit=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 // WE HAVE hello_exit
0000000000000000 t hel= lo_init =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 // WE HAVE h= ello_init
0000000000000000 T init_module
0000000000000070 T kernel_sp= ace_receiver
0000000000000050 T kernel_space_sender
0000000000000053 = r __module_depends
0000000000000004 D pid
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0U printk
0000000000000000 D res
00= 00000000000000 D __this_module
0000000000000000 r __UNIQUE_ID_author20000000000000013 r __UNIQUE_ID_description1
0000000000000047 r __UNIQUE= _ID_license0
000000000000005c r __UNIQUE_ID_vermagic0
000000000000000= 0 r ____versions

To get make to do this smoothly you= will have to fix the multiple definition and
other errors I enco= untered along the way. And something's in your header
fil= e really should belong in a C file :) heed Valdis's advice.
As a learning experience try this Makefile:
--------------= ---------------------------------------------------------------------------= -------------
obj-m :=3D aruna.o
aruna-objs :=3D netlink_kernel_modul= e.o netlink_kernel_space.o

SHELL +=3D -x

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

One simple way to enh= ance the output of GNU Make is to redefine SHELL.=20
SHELL is a GNU Make built-in variable that contains the name of the=20 shell to use when
GNU Make executes commands.
=
Since most shells=20 have a -x option that causes them to print out each command they
=
are=20 about to execute modifying SHELL in a Makefile by appendin -x causes=20 every command
to be printed (usually preceded by +) as the M= akefile is=20 run.

Try:
make clean the make and go thr= ough 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

= --0000000000004cc8a505969c4191-- --===============8504598920232317782== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies --===============8504598920232317782==--