On this page
Kbuild and Modules
Coverage: Kconfig/Kbuild → Makefile syntax → kernel module compilation → out-of-tree module → module signing → EXPORT_SYMBOL → module loading Kernel versions: 2.6 ~ 6.x
Kbuild Build System
Kconfig: Configuration Items
Each subdirectory: Kconfig file → defines menus, options, dependencies
→ make menuconfig/nconfig → reads Kconfig → generates .config
config entries:
config MY_FEATURE
tristate "My Feature" ← y (built-in), m (module), n (not compiled)
depends on PCI
select CRC32
help
This feature does...
boolean: y/n
tristate: y/m/n
int/hex/string: value
Kbuild Makefiles
# Subdirectory Makefile:
obj-y += core.o # Built into the kernel (CONFIG_XXX=y)
obj-m += driver.o # Compiled as a module (CONFIG_XXX=m)
obj-$(CONFIG_MYDRV) += mydrv.o # Decides y/m/n based on config
# Multi-file module:
obj-m := mymod.o
mymod-objs := mod-main.o mod-helper.o
Compilation Process
Kernel Modules
Basic Skeleton
static int __init
static void __exit
;
;
;
;
;
Module Loading/Unloading
# View loaded modules
Module Parameters
static int myparam = 42;
; # sysfs: /sys/module/mymod/parameters/myparam
// Usage: insmod mymodule.ko myparam=100
Module Signing
// CONFIG_MODULE_SIG=y → Sign modules during compilation
// At kernel boot: verify signature → mismatch → reject loading (unless sig_enforce=0)
// UEFI Secure Boot: Enforces signature verification
// Kernel keyring:
// .builtin_trusted_keys: Keys embedded at compile time
// .secondary_trusted_keys: Added at runtime (requires builtin signature)
EXPORT_SYMBOL
// Export symbols (functions/variables) to the global symbol table:
; // Available to all modules
; // Available only to GPL modules (license check)
// Mechanism: At compile time, symbols are recorded in the __ksymtab section → resolved at module load time
Out-of-Tree Module Build
# Kbuild for external modules (not in the kernel tree):
obj-m := mydriver.o
KDIR := /lib/modules/$(shell )/build
PWD := $(shell )
:
:
References
- Source Code:
scripts/kconfig/,scripts/Makefile.*,kernel/module/ - Kernel Documentation:
Documentation/kbuild/,Documentation/kbuild/modules.rst
Keywords: Kconfig, Kbuild, kernel module, EXPORT_SYMBOL, module param, module signing, out-of-tree