Cross-compiling kernel modules for Raspbian

This is a follow-up post to my post about compiling out-of-tree kernel modules for Raspbian / Raspberry Pi from last week. In that post I talked about compiling a kernel module on the Raspberry Pi itself. This time, I did the same thing, but I did it on my i386 Ubuntu workstation, using a cross-compiler.

For this to work, you need the following software:

  • The kernel source
  • A cross-compiler, and it has to be the same version that your actual Raspbian-kernel was compiled with
  • A Module.symvers file, or the kernel and all modules built

The easiest thing is to create a directory that you keep all the Raspberry Pi stuff in. If you haven’t downloaded the kernel source yet, clone the Git repository; it allows you to stay up to date. Also download the pre-built compiler toolchain using Git:

[code] mkdir ~/raspberrypi
cd ~/raspberrypi
git clone https://github.com/raspberrypi/linux.git
git clone https://github.com/raspberrypi/tools.git
[/code]

Just for fun & exercise, instead of using the downloaded Modules.symvers, build the kernel. See http://elinux.org/RPi_Kernel_Compilation#compiling for more detailed information.

[code] cd linux
zcat /proc/config.gz > .config
make -j 6 ARCH=arm CROSS_COMPILE=$HOME/raspberrypi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- oldconfig
make -j 6 ARCH=arm CROSS_COMPILE=$HOME/raspberrypi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
make -j 6 ARCH=arm CROSS_COMPILE=$HOME/raspberrypi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- modules
[/code]

Please note the ‘-j 6’, which sets the number of concurrent jobs to 6, which may be less than ideal if you have a slow (single/dual core) machine. If your computer has a quad-core processor or better, -j 6 should be fine. It will make your build go a whole lot faster.

Note: if you don’t feel like waiting for a kernel build to complete, you can also use the provided Module.symvers file, prepare and configure the kernel tree and build your module from there. Please see my previous post on how to do that; just add the cross-compilation flags.

Now, you are ready to cross-compile your out-of-tree kernel module:

[code] cd /path/to/module/source
make -C $HOME/raspberrypi/linux ARCH=arm \
CROSS_COMPILE=$HOME/raspberrypi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi- \
SUBDIRS=$PWD modules
[/code]