本页目录
Kbuild 与模块
覆盖: Kconfig/Kbuild → Makefile 语法 → 内核模块编译 → out-of-tree module → module signing → EXPORT_SYMBOL → module loading 内核版本: 2.6 ~ 6.x
Kbuild 构建系统
Kconfig: 配置项
每个子目录: Kconfig 文件 → 定义菜单, 选项, 依赖
→ make menuconfig/nconfig → 读 Kconfig → 生成 .config
config 条目:
config MY_FEATURE
tristate "My Feature" ← y(内建), m(模块), n(不编译)
depends on PCI
select CRC32
help
This feature does...
boolean: y/n
tristate: y/m/n
int/hex/string: 值
Kbuild Makefiles
# 子目录 Makefile:
obj-y += core.o # 编入内核 (CONFIG_XXX=y)
obj-m += driver.o # 编译为模块 (CONFIG_XXX=m)
obj-$(CONFIG_MYDRV) += mydrv.o # 根据 config 决定 y/m/n
# 多文件模块:
obj-m := mymod.o
mymod-objs := mod-main.o mod-helper.o
编译流程
内核模块
基本骨架
static int __init
static void __exit
;
;
;
;
;
模块加载/卸载
# 查看已加载模块
模块参数
static int myparam = 42;
; // sysfs: /sys/module/mymod/parameters/myparam
// 使用: insmod mymodule.ko myparam=100
Module Signing
// CONFIG_MODULE_SIG=y → 编译时对模块签名
// 内核启动时: 验证签名 → 不匹配 → 拒绝加载 (除非 sig_enforce=0)
// UEFI Secure Boot: 强制启用签名验证
// 内核 keyring:
// .builtin_trusted_keys: 编译时嵌入的密钥
// .secondary_trusted_keys: 运行时添加 (需有 builtin 签名)
EXPORT_SYMBOL
// 将符号 (函数/变量) 导出到公共符号表:
; // 所有模块可用
; // 仅 GPL 模块可用 (许可证检查)
// 原理: 编译时把符号记录在 __ksymtab section → 模块加载时 resolve
Out-of-Tree Module Build
# 外部模块的 Kbuild (不在内核树中):
obj-m := mydriver.o
KDIR := /lib/modules/$(shell )/build
PWD := $(shell )
:
:
参考
- 源码:
scripts/kconfig/,scripts/Makefile.*,kernel/module/ - 内核文档:
Documentation/kbuild/,Documentation/kbuild/modules.rst
关键词: Kconfig, Kbuild, kernel module, EXPORT_SYMBOL, module param, module signing, out-of-tree