Skip to content

Kernel Module Cross Compilation Notes

๐Ÿงญ Overview

External kernel module development requires a matching build environment with the target kernel.

Matching includes:

  • Kernel version (uname -r)
  • Configuration (.config)
  • Symbol version (Module.symvers)

๐Ÿ—๏ธ Two Worlds in Cross Compilation

Type Runs on Purpose
Host tools x86 (WSL) Build system (fixdep, modpost)
Target code ARM (Pi) Kernel module (.ko)

โš™๏ธ Kernel Release Matching

Kernel release format:

<version><LOCALVERSION>

Example:

6.12.47+rpt-rpi-2712

Key Config

CONFIG_LOCALVERSION
CONFIG_LOCALVERSION_AUTO

Remove extra '+'

make LOCALVERSION= kernelrelease

๐Ÿ”ง Build Preparation

make prepare
make modules_prepare

Purpose:

  • Generate headers
  • Build host tools
  • Prepare external module interface

๐Ÿงฉ Module.symvers

What it is

  • Symbol version database
  • Maps exported kernel symbols

Why it matters

Used during:

modpost

Without it:

  • Undefined symbols
  • Version mismatch
  • Module load failure

โ— Symbol Version Mismatch

Example error:

disagrees about version of symbol _printk

Cause

  • Kernel and module built with different symbol versions

Fix

  • Use exact Module.symvers from target kernel

๐Ÿ” vermagic

Check with:

modinfo hello.ko

Example:

vermagic: 6.12.47+rpt-rpi-2712 SMP preempt mod_unload

Note

Matching vermagic does NOT guarantee success.


โš ๏ธ Common Errors

Exec format error

Cause:

  • Running ARM binary on x86

Fix:

make prepare


Invalid module format

Cause:

  • Kernel mismatch
  • vermagic mismatch

Unknown symbol

Cause:

  • Missing or incorrect Module.symvers

๐Ÿงช Debugging Workflow

insmod hello.ko
dmesg | tail

Useful:

dmesg -w

๐Ÿงญ Best Practice

  • Always use target kernel config
  • Always align Module.symvers
  • Avoid mixing different kernel builds
  • Do not rely only on version string

๐ŸŽฏ Summary

A working kernel module requires matching:

  • Kernel version
  • Configuration
  • Symbol version