Kernel Module vermagic, modversions CRCs & Timestamped dmesg Triage
Trace finit_module() .modinfo vermagic and __versions CRC checks, separate -ENOEXEC from -ENOENT insmod failures, and pair CONFIG_PRINTK_TIME dmesg logs with KDIR hygiene.
What you'll learn
- Inspect
.ko.modinfosections (modinfo drv.ko) to verifyvermagicrelease,SMP/preempt,ARMv7,p2v8, andlicense=GPLfields. - Differentiate
insmod-ENOEXEC(Invalid module format) from-ENOENT(Unknown symbol in module) usingCONFIG_PRINTK_TIME=ydmesglogs. - Audit out-of-tree Kbuild
Makefilesto prevent host-uname -rKDIRcontamination andCONFIG_LOCALVERSION_AUTO-dirtyversion skew.
Why Kernel Modules Carry Zero Binary Compatibility Across Kernel Builds
A Hello World user-space ARM binary compiled ten years ago still runs unmodified on Linux kernel 6.12 because user space crosses swi/svc #0 with stable integer syscall numbers. Loadable kernel modules (.ko, ELF ET_REL relocatable objects) enjoy zero such insulation: once finit_module() relocates drv.ko into kernel virtual memory, its instructions dereference struct file, struct device, and spinlock_t directly in SVC (PL1) privileged mode!
Enabling CONFIG_PREEMPT_RT or CONFIG_DEBUG_SPINLOCK changes the byte size of spinlock_t inside every kernel struct. To prevent silent Ring 0 heap corruption, Kbuild bakes a vermagic= string into every .ko's .modinfo section — e.g., 6.12.0-ksa SMP preempt mod_unload modversions ARMv7 p2v8 — and finit_module() rejects any mismatch with -ENOEXEC (Invalid module format), logging the exact diff to dmesg!
Figure 6.2a. insmod prints only a generic errno string; the exact mismatched vermagic token or symbol prints in dmesg!
mental model: A user-space binary talks to the kernel across a standardized wall socket (syscall ABI lasts decades), whereas a .ko kernel module plugs directly onto the live CPU backplane bus — if a single struct pin shifted by 4 bytes, vermagic refuses insertion before privileged memory explodes.
Where it stops working: Matching vermagic strings proves build-configuration identity, NOT driver bug freedom; force-removing vermagic checks (modprobe --force) risks immediate Oops kernel memory corruption!
definition ( (.modinfo)): A NUL-terminated key-value entry (vermagic=...) generated by scripts/mod/modpost into a loadable .ko module's .modinfo ELF section that concatenates UTS_RELEASE with ABI-critical Kconfig tokens (SMP, preempt, mod_unload, modversions, ARMv7, p2v8) so finit_module() can verify structural binary compatibility with the running kernel.
- Step 1. Never let an embedded driver Makefile fall back silently to /lib/modules/$(shell uname -r)/build!
- Step 2. Check three lines every time: license: GPL, vermagic: <release> ... ARMv7 p2v8, and depends:.
- Step 3. Raw insmod does NOT load 'depends:' modules automatically; modprobe consults modules.dep!
- Step 4. Without timestamps, engineers frequently debug a stale dmesg error from 20 minutes ago!
Figure 6.2b. Pin KDIR + ARCH=arm + CROSS_COMPILE, verify modinfo vermagic == target uname -r, and read timestamped dmesg.
Inside .modinfo NUL Records, __versions CRCs, and KDIR Traps
Open any .ko file in a hex viewer or objdump -s -j .modinfo drv.ko and you will find a flat array of NUL-terminated ('\0') ASCII key=value strings: license=GPL\0, depends=regmap-i2c\0, vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0 (note the trailing space before '\0' placed by <linux/vermagic.h>!). Notice the token p2v8: on 32-bit ARMv7-A, CONFIG_ARM_PATCH_PHYS_VIRT patches virt_to_phys() instructions at boot with 8-bit rotated constants, so loading a non-p2v8 module would compute bogus DMA physical addresses!
When CONFIG_MODVERSIONS=y is enabled, genksyms hashes the expanded C AST of every EXPORT_SYMBOL function into a 32-bit CRC stored inside the .ko's __versions section (struct modversion_info: crc + name[56]). Even if UTS_RELEASE strings match, editing a kernel header struct changes module_layout or kmalloc CRCs, triggering disagrees about version of symbol ... (-ENOEXEC). Meanwhile, editing a single tracked file in the kernel git repo when CONFIG_LOCALVERSION_AUTO=y appends -g<hash>-dirty to UTS_RELEASE, breaking vermagic until both zImage and .ko are rebuilt/deployed together!
Figure 6.2b. Every .modinfo tag is a simple NUL-terminated ASCII key=value string readable on any host without ARM hardware.
insmod User-Space Error | dmesg Kernel Log Line | Exact Cause | Clean Engineering Fix |
|---|---|---|---|
Invalid module format (-ENOEXEC) | version magic '6.12.0-dirty ...' should be '6.12.0 ...' | KDIR gained uncommitted changes (LOCALVERSION_AUTO) after zImage flashed | Commit/clean KDIR or deploy matching zImage + .ko build pair |
Invalid module format (-ENOEXEC) | not an Elf32_Ehdr / wrong architecture | Makefile omitted ARCH=arm or defaulted KDIR to host /lib/modules/$(uname -r) | Require explicit KDIR + ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- |
Invalid module format (-ENOEXEC) | disagrees about version of symbol module_layout | Kernel .config / struct headers changed (MODVERSIONS CRC mismatch) | Run make clean && make in out-of-tree driver against updated KDIR |
Unknown symbol in module (-ENOENT) | Unknown symbol regmap_init_i2c (err -2) or GPL-incompatible | Prerequisite regmap-i2c.ko not loaded, CONFIG_REGMAP_I2C=n, or missing GPL license | Add MODULE_LICENSE("GPL"), enable CONFIG_*, or run depmod -a && modprobe |
learner@ksa:~/gl-e00/m06-l02$ bat -H 4 -H 20 modinfo_vermagic_layout.c File: modinfo_vermagic_layout.c // SPDX-License-Identifier: GPL-2.0 /* * Out-of-tree Kbuild invocation contract (never fallback to host uname -r!): * make -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules * * Inside the resulting .ko, modpost emits .modinfo and optional __versions: */ #include <stdint.h> struct ksa_modversion_info { uint32_t crc; char name[64 - sizeof(uint32_t)]; }; /* Representative NUL-separated .modinfo section payload inside sensor_drv.ko */ static const char sample_modinfo_blob[] = "license=GPL\0" "description=KSA I2C Sensor Driver\0" "depends=regmap-i2c\0" "vermagic=6.12.0-ksa SMP preempt mod_unload modversions ARMv7 p2v8 \0"; static const struct ksa_modversion_info sample_versions[] = { { 0x8f3a1204U, "module_layout" }, { 0x41c90e11U, "__regmap_init_i2c" }, };- Explicit ARCH=arm and target KDIR prevent host x86_64 kernel headers from polluting the build.
- Every .modinfo attribute is a NUL-delimited ASCII key=value record checked by finit_module().
modinfo(8) / finit_module(2) / depmod(8)(8) GL-E00 Quick Reference modinfo(8) / finit_module(2) / depmod(8)(8) SYNOPSIS /sbin/modinfo drv.ko # Inspect vermagic, license, depends, parm on x86_64 host! depmod -a -b /srv/nfs/rootfs 6.12.0-ksa # Build /lib/modules/6.12.0-ksa/modules.dep on host dmesg -T | tail -n 15 # Or monotonic [sec.usec] via CONFIG_PRINTK_TIME=y RETURN ERRORS (finit_module) ENOEXEC (Invalid module format) ELF arch mismatch, vermagic string mismatch, or __versions CRC mismatch. ENOENT (Unknown symbol/param) Unloaded dependency .ko, disabled CONFIG_*, bad module param, or GPL block. Kernel Systems Academy Lesson 6.2 modinfo(8) / finit_module(2) / depmod(8)(8) GL-E00 quick reference · full manual page: man 8 modinfo(8) / finit_module(2) / depmod(8) Parsing .modinfo NUL Blobs and Symbol CRCs in Host C11
Because .modinfo is simply a contiguous byte slice of NUL-terminated key=value strings inside the .ko file, we can write a zero-dependency C11 auditor (verify.c) that extracts vermagic= and license=, compares them against a running utsname release + required ARMv7 p2v8 flags, checks __versions CRC32 hashes, and formats timestamped dmesg diagnostics matching Linux kernel 6.12.
This exact .modinfo scanner becomes the second pillar of our Module 6 preflight capstone.
learner@ksa:~/gl-e00/m06-l02$ bat -H 33 -H 40 verify.c File: verify.c #define _POSIX_C_SOURCE 200809L #include <errno.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> static const char *find_modinfo_val(const char *blob, size_t len, const char *key) { size_t klen = strlen(key); size_t pos = 0U; while (pos < len) { const char *entry = blob + pos; size_t rem = len - pos; const char *nul = memchr(entry, '\0', rem); if (!nul) break; if ((size_t)(nul - entry) > klen && strncmp(entry, key, klen) == 0 && entry[klen] == '=') return entry + klen + 1U; pos = (size_t)(nul - blob) + 1U; } return NULL; } static int sim_finit_module(const char *blob, size_t len, const char *uts_rel, uint32_t mod_crc, uint32_t kern_crc, bool dep_loaded) { const char *vm = find_modinfo_val(blob, len, "vermagic"); const char *lic = find_modinfo_val(blob, len, "license"); size_t rlen = strlen(uts_rel); if (!vm || strncmp(vm, uts_rel, rlen) != 0 || vm[rlen] != ' ') return -ENOEXEC; if (strstr(vm, "ARMv7") == NULL || strstr(vm, "p2v8") == NULL) return -ENOEXEC; if (mod_crc != kern_crc) return -ENOEXEC; if (!lic || strncmp(lic, "GPL", 3U) != 0 || !dep_loaded) return -ENOENT; return 0; } int main(void) { static const char ok_ko[] = "license=GPL\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; static const char dirty_ko[] = "license=GPL\0vermagic=6.12.0-ksa-dirty SMP preempt mod_unload ARMv7 p2v8 \0"; static const char prop_ko[] = "license=Proprietary\0vermagic=6.12.0-ksa SMP preempt mod_unload ARMv7 p2v8 \0"; int r_ok = sim_finit_module(ok_ko, sizeof(ok_ko) - 1U, "6.12.0-ksa", 0x1A2B3C4DU, 0x1A2B3C4DU, true); int r_dirty = sim_finit_module(dirty_ko, sizeof(dirty_ko) - 1U, "6.12.0-ksa", 0x1A2B3C4DU, 0x1A2B3C4DU, true); int r_crc = sim_finit_module(ok_ko, sizeof(ok_ko) - 1U, "6.12.0-ksa", 0xDEADBEEFU, 0x1A2B3C4DU, true); int r_gpl = sim_finit_module(prop_ko, sizeof(prop_ko) - 1U, "6.12.0-ksa", 0x1A2B3C4DU, 0x1A2B3C4DU, true); if (r_ok != 0 || r_dirty != -ENOEXEC || r_crc != -ENOEXEC || r_gpl != -ENOENT) return 1; printf("[ 142.810412] MOD_CHECK ok=%d dirty=%d(ENOEXEC) bad_crc=%d(ENOEXEC) non_gpl=%d(ENOENT)\n", r_ok, r_dirty, r_crc, r_gpl); return 0; }- Checks vm[rlen] == ' ' token boundary so '6.12.0-ksa-dirty' never sneaks past '6.12.0-ksa'!
- Separates vermagic/CRC -ENOEXEC (-8) from missing-dependency or GPL-only symbol -ENOENT (-2).
Observing Token-Boundary Release Checks and Errno Separation
Notice line 33 of verify.c: strncmp(vm, uts_rel, rlen) == 0 && vm[rlen] == ' '! Because 6.12.0-ksa is a prefix of 6.12.0-ksa-dirty, a sloppy strncmp-only check would falsely approve dirty_ko on the host, only for finit_module() on the board to reject it with -ENOEXEC! Requiring the space delimiter right after <release> catches -dirty and -rc1 suffixes with 100% precision.
Notice also the [ 142.810412] prefix format: enabling CONFIG_PRINTK_TIME=y in your kernel config gives every dmesg event microsecond-resolution ordering.
learner@ksa:~/gl-e00/m06-l02gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e00/m06-l02./verify[ 142.810412] MOD_CHECK ok=0 dirty=-8(ENOEXEC) bad_crc=-8(ENOEXEC) non_gpl=-2(ENOENT)learner@ksa:~/gl-e00/m06-l02learner@ksa:~/gl-e00/m06-l02strace -e trace=write,exit_group ./verifywrite(1, "[ 142.810412] MOD_CHECK ok=0 di"..., 87) = 87exit_group(0) = ?+++ exited with 0 +++Makefile KDIR Defaults and insmod-vs-modprobe Confusion
Adopt two permanent rules for every driver Makefile you write in GL-E04..GL-E07. First, fail fast with $(error KDIR is not set) if KDIR is missing or does not contain include/config/kernel.release — never default KDIR ?= /lib/modules/$(shell uname -r)/build in an embedded repository!
Second, install modules via make -C $(KDIR) M=$(PWD) INSTALL_MOD_PATH=/srv/nfs/rootfs modules_install so Kbuild places .ko files under /lib/modules/<release>/updates/ and runs depmod automatically.
Silent -dirty Suffix Added by Uncommitted DTS Edit in KDIR:
- Symptom: You tweak
1line in an.dtsfile insideKDIR, copy the new.dtb, then build a new.ko— andinsmodrejects the.kowithversion magic '6.12.0-dirty ...' should be '6.12.0 ...'! - Root Cause: With
CONFIG_LOCALVERSION_AUTO=y,scripts/setlocalversioninspectsgit statusinKDIR; modifying any tracked file appends-dirtytoinclude/config/kernel.releaseused by.kobuilds while the runningzImageon the board was built when the tree was clean! - Fix: Either commit changes cleanly, disable
CONFIG_LOCALVERSION_AUTO(# CONFIG_LOCALVERSION_AUTO is not set) during iterative bring-up, or redeployzImagealongside modules.
Using raw insmod on a Driver Depending on Industrial I/O or Regmap Modules:
- Symptom:
insmod ./ap3216c.kofails withUnknown symbol __iio_device_register (err -2)even thoughindustrialio.koexists under/lib/modules/. - Root Cause:
insmodloads only the single file path passed to it and never readsmodules.depto load prerequisite modules first. - Fix: Run
depmod -aonce after installing.kofiles and load modules by name withmodprobe ap3216c(which loadsindustrialio.koautomatically).
Guided Practice
Extend verify.c to extract and parse the comma-separated depends= field from a .modinfo blob (e.g., "depends=industrialio,regmap-i2c\0") and print the exact ordered list of prerequisite modules that modprobe must load before finit_module().
Test your parser on both an empty "depends=\0" module (0 prerequisites) and a 2-dependency sensor module.
Independent Practice
Write a 12-line out-of-tree Kbuild Makefile snippet guarded by ifeq ($(KERNELRELEASE),) that aborts with $(error Set KDIR to configured ARMv7 Linux 6.12 tree) whenever KDIR is unset, and prints the target kernel's $(shell cat $(KDIR)/include/config/kernel.release) before invoking $(MAKE) -C $(KDIR) M=$(CURDIR) ARCH=arm.
Verify with make -n on a dummy directory containing include/config/kernel.release that missing KDIR fails cleanly instead of touching /lib/modules/$(uname -r).
Verification Checks
Key takeaways
- During
finit_module(),Linux kernelcheck_modinfo()compares.modinfovermagic=<release> SMP preempt mod_unload ARMv7 p2v8againstutsname()->releaseand compile-time kernel tokens. - When
insmodfails with-ENOEXEC(Invalid module format), timestampeddmesg(CONFIG_PRINTK_TIME=y) reveals exactversion magicrelease/flag drift (such as-dirtyor wrong hostKDIR) orCONFIG_MODVERSIONSsymbolCRCdisagreement. - When
insmodfails with-ENOENT(Unknown symbol in module, or unknown parameter), an exported dependency.kois unloaded (resolved viadepmod -a+modprobe), aCONFIG_*subsystem is disabled, orMODULE_LICENSE("GPL")was omitted for anEXPORT_SYMBOL_GPLsymbol. - Out-of-tree driver
Makefilesmust pointKDIRat the exact configured/builtARMv7-ALinux kernel6.12 tree (ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-), never/lib/modules/$(uname -r)/build.