I written simple hello world dynamic module for ubuntu, while inserting it I'm getting an error
insmod: ERROR: could not insert module pcd.ko: Invalid module format
when I check dmesg, I get this
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 00000000f1750993, val ffffffffc39162ea
I tried to check if version of kernel is same or not but they are same
xxxxxxx#xxxxxxx:~/workspace/ldd/custom_drivers/002pseudo_char_driver$ uname -r
5.13.0-35-generic
xxxxxx#xxxxxxxx:~/workspace/ldd/custom_drivers/002pseudo_char_driver$ modinfo pcd.ko
filename: /home/xxxxxxx/workspace/ldd/custom_drivers/002pseudo_char_driver/pcd.ko
description: A pseudo character driver
author: xxxxxxx
license: GPL
srcversion: 30261074162DBCAE996D8E0
depends:
retpoline: Y
name: pcd
vermagic: 5.13.0-35-generic SMP mod_unload modversions
so what is the problem and how to solve it?
this is Makefile I used
obj-m := pcd.o
ARCH=arm
CROSS_COMPILE=arm-linux-gnueabihf-
KERN_DIR=/home/xxxxxxx/workspace/ldd/source/linux_kernel4.19.94-ti-r42/
HOST_KERN_DIR = /lib/modules/$(shell uname -r)/build/
all:
sudo make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(shell pwd) modules
clean:
sudo make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(shell pwd) clean
help:
sudo make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(shell pwd) help
host:
sudo make -C $(HOST_KERN_DIR) M=$(shell pwd) modules
to compile I entered
make host
result of uname -a
subhash#subhashHP:~/workspace/ldd/custom_drivers/002pseudo_char_driver$ uname -a
Linux subhashHP 5.13.0-35-generic #40~20.04.1-Ubuntu SMP Mon Mar 7 09:18:32 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Related
I am currently trying to develop a simple linux kernel module. It should just log something, its 1:1 copied from the internet.
I have the following files:
lkm_example.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robert W. Oliver II");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");
static int __init lkm_example_init(void) {
printk(KERN_INFO "Hello, World!\n");
return 0;
}
static void __exit lkm_example_exit(void) {
printk(KERN_INFO "Goodbye, World!\n");
}
module_init(lkm_example_init);
module_exit(lkm_example_exit);
Makefile:
obj-m += lkm_example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
I also did the following:
sudo apt-get install build-essential linux-headers-`uname -r`
For compilation i used:
stbau#kernel-dev-vm:~/src/lkm_example$ sudo make
make -C /lib/modules/5.13.0-39-generic/build M=/home/stbau/src/lkm_example modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-39-generic'
CC [M] /home/stbau/src/lkm_example/lkm_example.o
MODPOST /home/stbau/src/lkm_example/Module.symvers
CC [M] /home/stbau/src/lkm_example/lkm_example.mod.o
LD [M] /home/stbau/src/lkm_example/lkm_example.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-39-generic'
Executing with insmod:
stbau#kernel-dev-vm:~/src/lkm_example$ sudo insmod lkm_example.ko
insmod: ERROR: could not insert module lkm_example.ko: Invalid module format
The dmesg log gives the following error:
[ 49.272618] lkm_example: module verification failed: signature and/or required key missing - tainting kernel
[ 49.272630] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000054f3f1c5, val ffffffffc0a0a000
I am using the following kernel:
stbau#kernel-dev-vm:~/src/lkm_example$ uname -a
Linux kernel-dev-vm 5.13.0-39-generic #44-Ubuntu SMP Thu Mar 24 15:35:05 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
As you can see in the dmesg log, i only get an error and not the messages i expected. I have no idea what i did wrong/what is missing.
I think the problem is that the module is not signed. I tried signing it using the sign-file but i was not able to generate a private/public key file.
Re-Installing the kernel headers worked for me.
I used the following commands:
sudo apt update && sudo apt upgrade
sudo apt remove --purge linux-headers-*
sudo apt autoremove && sudo apt autoclean
sudo apt install linux-headers-generic
I am currently trying to write basic kernel module on linux. I can compile the source code on terminal without any error
~/Desktop/kernelDriver$ make
make -C /lib/modules/5.8.0-7642-generic/build M=/home/cryonayes/Desktop/kernelDriver modules
make[1]: Entering directory '/usr/src/linux-headers-5.8.0-7642-generic'
CC [M] /home/cryonayes/Desktop/kernelDriver/basicModule.o
MODPOST /home/cryonayes/Desktop/kernelDriver/Module.symvers
CC [M] /home/cryonayes/Desktop/kernelDriver/basicModule.mod.o
LD [M] /home/cryonayes/Desktop/kernelDriver/basicModule.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.8.0-7642-generic'
But when I try to compile with same files on Emacs I get this error
-*- mode: compilation; default-directory: "~/Desktop/kernelDriver/" -*-
Compilation started at Wed Mar 3 20:42:07
make
make -C /lib/modules/5.8.0-7642-generic/build M=/home/cryonayes/Desktop/kernelDriver modules
make[1]: *** /lib/modules/5.8.0-7642-generic/build: No such file or directory. Stop.
make: *** [Makefile:7: all] Error 2
Compilation exited abnormally with code 2 at Wed Mar 3 20:42:07
It says No such file or directory. but it actually exists.
Here is the content of my Makefile
obj-m += basicModule.o
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
What causes this error ?
As far as I understand, the /lib/modules/5.8.0-7642-generic/build is a symlink to /usr/src/linux-headers-5.8.0-7642-generic.
You can use /usr/src/linux-headers-$(shell uname -r) instead and try again.
Add a test rule to show information about -C's state as follows;
test:
stat $(KERNEL_DIR)
This should show what and where it's pointing to.
If you're in GUI session, something might have sandboxed your emacs while terminal is running without it. You may have some kind of "fake-root" to isolate it from vulnerabilities.
Eg: a common device module's Makefile
obj-m:=jc.o
default:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules clean
I consider if I can set CFLAGS to the file. When I change default section to
$(MAKE) -O2 -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
But it didn't work.
Any help? Thanks a lot.
-O2 would be an option to make (or $(MAKE), as you're using it) in what you tried. Obviously, the compiler (probably gcc) needs this flag, not make.
Kbuild understands a make variable named CFLAGS_modulename.o to add specific C flags when compiling this unit. In your case, your module object will be jc.o, so you can specify:
CFLAGS_jc.o := -O2
and it should work. Add V=1 to your $(MAKE) lines to get a verbose output and you should see -O2 when jc.c is being compiled.
You can find more about compiling modules in the official documentation.
You can also use
ccflags-y := -O2
This will be applied to all of the source files compiled for your module with the Makefile. This is indirectly documented in the link provided by eepp in Section 4.2
I am learning Linux kernel module programming. I am using Beaglebone black for that. I have made simple 'Hello World' application and makefile. I have checked my makefile it's proper. But when I command 'make', it gives me following error:
root#beaglebone:/home/sonu# make
make: Warning: File `Makefile' has modification time 2.2e+02 s in the future
make -C /lib/modules/3.8.13-bone70/build M=/home/sonu modules
make: *** /lib/modules/3.8.13-bone70/build: No such file or directory. Stop.
make: *** [all] Error 2
Though I referred some websites. But, all they are asking me to install packages. As I am a newbie. I don't even know how to configure and start internet connection on Beaglebone using ethernet. Please help me I am stuck. Thanks in advance.
Code is:
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
static int __init hello(void)
{
printk(KERN_INFO "Hello World!");
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Bye");
}
module_init(hello);
module_exit(hello_cleanup);
Makefile is:
obj-m+=Hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
$(CC) Hello.c -o test
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm test
It appears that you're trying to build your module for a kernel version that you haven't installed the headers for.
Instead of calling uname directly in your rules, it's helpful to put that into a variable you can override:
obj-m+=Hello.o
KSRC := /lib/modules/$(shell uname -r)/build
all:
make -C $(KSRC) M=$(PWD) modules
$(CC) Hello.c -o test
clean:
make -C $(KSRC) M=$(PWD) clean
rm test
Now, you can override with the actual location of your kernel headers:
make KSRC=/usr/src/linux-headers-4.9.2 all
You can simplify the Makefile further with a catch-all rule:
obj-m+=Hello.o
KSRC := /lib/modules/$(shell uname -r)/build
all: modules
$(CC) Hello.c -o test
%:
make -C $(KSRC) M=$(PWD) $#
clean::
make -C $(KSRC) M=$(PWD) clean
$(RM) test
$ make
gcc -Wall -D__KERNEL__ -DLINUX -DMODULE -O -I /lib/modules/`uname -r`/build/include/ -c -o procmon.o procmon.c
In file included from /lib/modules/3.0.0-12-generic/build/include/linux/kernel.h:13:0,
from procmon.c:22:
/lib/modules/3.0.0-12-generic/build/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
make: *** [procmon.o] Error 1
Im trying to compile using the make file of procmon system analysis module. I got the above message can any one help me out what is the problem?
you need to change your makefile like this:
obj-m :=procmon.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
install: all
rm -rf /dev/procmon
mknod /dev/procmon c 240 1
chmod 400 /dev/procmon
clean:
rm -f *.o
rm -f *~
rm -f a.out
rm -f test*
rm -f DEADJOE
dist: clean
cd .. ; tar cvzf procmon.tar.gz procmon
If you get any errors like devfs related, your procmon code will not work on recent kernels, you need to change the code accordingly