Page 1 of 1

Can no longer compile kernel modules on Raspbian with RPi 1B

Posted: Sun Jan 13, 2019 8:22 pm
by Aurelian_15
I have trouble compiling out-of-tree kernel modules on my 2012 Raspberry Pi 1B.

The system is a fresh installation of Raspbian. I have updated and upgraded all packages on the system, and installed the raspberrypi-kernel-headers package. uname -r gives 4.14.79+. The directory /usr/src/linux-headers-4.14.79+ exists, and /lib/modules/4.14.79+/build correctly symlinks to the former.

However, I get very weird error messages when trying to compile a kernel module.

Here is a minimal example:

Code: Select all

// module.c

#include <linux/module.h>

MODULE_LICENSE("GPL v2");

Code: Select all

# Makefile

KVERSION := $(shell uname -r)
PWD := $(shell pwd)

obj-m := module.o

all:
	+make -C /lib/modules/$(KVERSION)/build/ M=$(PWD) modules
clean:
	+make -C /lib/modules/$(KVERSION)/build/ M=$(PWD) clean
Running make in a directory with these two files results in the following messages:

Code: Select all

make -C /lib/modules/4.14.79+/build/ M=/home/pi modules
make[1]: Entering directory '/usr/src/linux-headers-4.14.79+'
  CC [M]  /home/pi/module.o
/home/pi/module.c:3:16: error: expected declaration specifiers or ‘...’ before string constant
 MODULE_LICENSE("GPL v2");
                ^~~~~~~~
scripts/Makefile.build:334: recipe for target '/home/pi/module.o' failed
make[2]: *** [/home/pi/module.o] Error 1
Makefile:1527: recipe for target '_module_/home/pi' failed
make[1]: *** [_module_/home/pi] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.14.79+'
Makefile:7: recipe for target 'all' failed
make: *** [all] Error 2
Compiling this minimal kernel module works without any problem on other computers I tested. I can however reproduce the same issue with Arch Linux ARM on the same Raspberry Pi, so my guess would be that this is a problem with the raspberrypi-kernel-headers package provided by the Raspberry Pi foundation.

So, my question, is this really a bug or am I just missing something?

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Posted: Sun Jan 13, 2019 10:09 pm
by DougieLawson
Try this: https://github.com/notro/rpi-source/wiki
rpi-source will get you a current kernel source tree that matches your running kernel.

I created ~/module.c and ~/Makefile from your samples and they worked OK.

Code: Select all

root@pioneer:~ # vi module.c
root@pioneer:~ # vi Makefile
root@pioneer:~ # make
make -C /lib/modules/4.14.92+/build/ M=/root modules
make[1]: Entering directory '/root/linux-4de3f6305841c2121257b8fdbda5c3c0bf77e669'
  CC [M]  /root/module.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/module.mod.o
  LD [M]  /root/module.ko
make[1]: Leaving directory '/root/linux-4de3f6305841c2121257b8fdbda5c3c0bf77e669'
root@pioneer:~ #
I even managed to get it loaded.

Code: Select all

[191058.250549] module: loading out-of-tree module taints kernel.
[191058.250786] module: module is already loaded
[191074.748224] module: module is already loaded

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Posted: Sun Jan 13, 2019 11:02 pm
by Aurelian_15
Thank you! Indeed, with the kernel source code fetched by rpi-source I can successfully compile my module.

However, my understanding was that rpi-source should no longer be necessary for building kernel modules now that raspberrypi-kernel-headers exists (unless someone is using a newer kernel fetched via rpi-update). So my guess would be that this is indeed a problem with raspberrypi-kernel-headers.

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Posted: Mon Jan 14, 2019 1:25 am
by DougieLawson
The kernel headers package seems to follow the raspberrypi-kernel package (current stable kernel). My systems are always a long way ahead of that (some are running 4.19 with BRANCH=next rpi-update). I like rpi-source as it's a simple way to get something that seems to work more often than not.