0

I am using kernel 5.10.11 in KALI and I am trying to learn kernel module programming but I am unable to build the module. I have tried all the solutions given on the internet but they are not working for me, or I am doing it the wrong way.

Here is my c file

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int helloWorld_init(void)
{
    printk(KERN_DEBUG "Hello World!\n");
    return 0;
}

static void helloWorld_exit(void)
{
    printk(KERN_DEBUG "Removing Module\n");
}

module_init(helloWorld_init);
module_exit(helloWorld_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mukul Mehar");
MODULE_DESCRIPTION("first kernel module");

And the Makefile:

obj-m += helloWorld.o
KDIR = /usr/src/linux-headers-5.10.11/ 

all:
    make -C $(KDIR) M=$(shell pwd) modules 

clean:
    make -C $(KDIR) M=$(shell pwd) clean

The output which I am receiving is:

make -C /usr/src/linux-headers-5.10.11/  M=/home/mukul/Documents/Eudyptula/challenge-1 modules 
make[1]: Entering directory '/usr/src/linux-headers-5.10.11'
make[2]: *** No rule to make target '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.o', needed by '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.mod'.  Stop.
make[1]: *** [Makefile:1805: /home/mukul/Documents/Eudyptula/challenge-1] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.10.11'
make: *** [Makefile:7: all] Error 2
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
m_here
  • 41
  • 7

2 Answers2

0

Can this following makefile work for you?

obj-m += helloworld.o
KDIR = /lib/modules/$(shell uname -r)/build/

all:
    make -C $(KDIR) M=$(shell pwd) modules
    
clean:
    make -C $(KDIR) M=$(shell pwd) clean

tyChen
  • 1,404
  • 8
  • 27
0

Have you tried the Kbuild method ?

Make a file named Kbuild in the same directory as helloworld.c with the following content:

obj-m += helloworld.o

Launch the Build from the same directory:

$ make -C /lib/modules/`uname -r`/build M=`pwd`
make: Entering directory '/usr/src/linux-headers-5.4.0-65-generic'
  CC [M]  .../helloworld.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC [M]  .../helloworld.mod.o
  LD [M]  .../helloworld.ko
make: Leaving directory '/usr/src/linux-headers-5.4.0-65-generic'
$ ls -l helloworld.ko
-rw-rw-r-- 1 xxxx xxxx 4144 janv.  31 14:50 helloworld.ko

Then, use insmod/rmmod to load/unload the module into/from the kernel:

$ sudo insmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
$ sudo rmmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
[16497.208337] Removing Module
Rachid K.
  • 4,490
  • 3
  • 11
  • 30