---
title: Kbuild and Modules
url: https://doc.liz6.com/en/linux-kernel/11-kernel-development-and-build/01-kbuild-and-modules
locale: en
area: linux-kernel
tags:
- linux-kernel
- kernel-development-and-build
date: 2026-06-30
modified: 2026-07-16
description: '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 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

```makefile
# 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

```bash
make defconfig          # Default configuration (architecture-specific)
make menuconfig         # Interactive configuration
make -j$(nproc)         # Parallel compilation → vmlinux + modules
make modules_install    # Install modules to /lib/modules/<version>/
make install            # Install kernel + initramfs
```

---

## Kernel Modules

### Basic Skeleton

```c
#include <linux/module.h>
#include <linux/kernel.h>

static int __init my_init(void) {
    pr_info("my_module: loaded\n");
    return 0;  // 0=success, -ENODEV=no device, -ENOMEM=no memory
}

static void __exit my_exit(void) {
    pr_info("my_module: unloaded\n");
}

module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Example kernel module");
MODULE_AUTHOR("Name <email>");
```

### Module Loading/Unloading

```bash
insmod mymodule.ko         # Load + dependency check
rmmod mymodule             # Unload
modprobe mymodule          # Load + automatically load dependent modules
modprobe -r mymodule       # Unload + recursively unload dependencies

# View loaded modules
lsmod
cat /proc/modules
```

### Module Parameters

```c
static int myparam = 42;
module_param(myparam, int, 0644);  # sysfs: /sys/module/mymod/parameters/myparam
// Usage: insmod mymodule.ko myparam=100
```

### Module Signing

```c
// 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

```c
// Export symbols (functions/variables) to the global symbol table:
EXPORT_SYMBOL(my_function);        // Available to all modules
EXPORT_SYMBOL_GPL(my_function2);   // 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

```makefile
# Kbuild for external modules (not in the kernel tree):
obj-m := mydriver.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
    make -C $(KDIR) M=$(PWD) modules

clean:
    make -C $(KDIR) M=$(PWD) clean
```

## 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*
