Building Kernel Modules on the Raspberry Pi is Ridiculously Hard!

I don’t think I’m alone on this issue… it seems almost impossible to build a working kernel driver on the Pi. After much trial and error, and on the verge of giving up completely, I finally succeeded!

I started with cross compilation on Ubuntu but couldn’t get it to work initially, it kept giving “Invalid module format”. Compiling directly on the Pi, again I was only able to obtain “Invalid module format” errors and, finally, a module that loaded but crashed the OS. The .config and Module.symvers seem correct, but it just would not build a working module. It makes me suspect some compiler toolchain difference between the kernel and the tools used to build the module.

Finally, cross compilation worked. Here is the magical recipe which  worked for me under Ubuntu, cross compiling for 3.10.33+ (mostly in case I need it again myself). First, set up the environment variables and download tools as described here.

git clone --depth 1 git://github.com/raspberrypi/linux.git
cd linux
cp your/raspi/config .config
wget https://github.com/raspberrypi/firmware/raw/master/extra/Module.symvers
ARCH=arm CROSS_COMPILE=${CCPREFIX} make oldconfig
ARCH=arm CROSS_COMPILE=${CCPREFIX} make menuconfig
# Save.. Exit..
ARCH=arm CROSS_COMPILE=${CCPREFIX} make modules_prepare
# Note: can specify -j9 for 8 core compile..

Well, now that it finally works, I’m not updating the Pi again until I’ve finished my driver 😉

Incidentally, here’s an easy way to find Module.symvers: https://github.com/raspberrypi/firmware/commits/master/extra/Module.symvers

Leave a Reply

Your email address will not be published. Required fields are marked *