kbuild

Makefile

Makefile 語法簡介
Makefile 語法筆記

KBuild

modules
kbuild 參數 list

Pre require

  • prebuilt kernel
  • kernel 設定可載入 module
  • 可能是 CONFIG_PROC_KCORE and CONFIG_DEBUG_INFO

2 語法

基本語法
1
make -C <path_to_kernel_src> M=$PWD
使用運行中的 kernel
1
make -C /lib/modules/`uname -r`/build M=$PWD
通用使用法
1
make -C $KDIR M=$PWD [target]
target
  • modules
    預設 target(不輸入時),產出物會在本目錄下
  • modules_install
    安裝到 /lib/modules/<kernel_release>/extra/
    INSTALL_MOD_PATH
  • clean
    移除本目錄下的產出物
  • help

3 Creating a Kbuild File for an External Module

新版 kernel 會先去掃過一遍 Kbuild

include object file as blob,需要將檔案命名成 <filename>_shipped,如 8123_bin.o_shipped

分離 Kbuild Makefile,且向前相容

include to module
1
2
3
4
8123_if.c
8123_if.h
8123_pci.c
8123_bin.o_shipped <= Binary blob
Kbuild
1
2
obj-m  := 8123.o
8123-y := 8123_if.o 8123_pci.o 8123_bin.o
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
include Kbuild

else
# normal makefile
KDIR ?= /lib/modules/`uname -r`/build

default:
$(MAKE) -C $(KDIR) M=$$PWD

# Module specific targets
genbin:
echo "X" > 8123_bin.o_shipped

endif
Building Multiple Modules
1
2
3
obj-m := foo.o bar.o
foo-y := <foo_srcs> # foo.ko
bar-y := <bar_srcs> # bar.ko

4 Header file

include/linux/
arch/$(ARCH)/include/

Kbuildz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# .
# |__ src
# | |__ complex_main.c
# | |__ hal
# | |__ hardwareif.c
# | |__ include
# | |__ hardwareif.h
# |__ include
# |__ complex.h
obj-m := complex.o
complex-y := src/complex_main.o
complex-y += src/hal/hardwareif.o

ccflags-y := -I$(src)/include
ccflags-y += -I$(src)/src/hal/include

5 skip

6 Symbol

build 過程中,會產生 Module.symvers,它包含所有 compiled module 的全部 exported symbols。

Module.symvers
1
2
3
<CRC>       <Symbol>           <module>

0x2d036834 scsi_remove_host drivers/scsi/scsi_mod

For a kernel build without CONFIG_MODVERSIONS enabled, the CRC would read 0x00000000.

Module.symvers serves two purposes:

1) It lists all exported symbols from vmlinux and all modules.
2) It lists the CRC if CONFIG_MODVERSIONS is enabled.