@@ -4,8 +4,8 @@ Building External Modules
4
4
5
5
This document describes how to build an out-of-tree kernel module.
6
6
7
- 1. Introduction
8
- ===============
7
+ Introduction
8
+ ============
9
9
10
10
"kbuild" is the build system used by the Linux kernel. Modules must use
11
11
kbuild to stay compatible with changes in the build infrastructure and
@@ -19,11 +19,11 @@ in building out-of-tree (or "external") modules. The author of an
19
19
external module should supply a makefile that hides most of the
20
20
complexity, so one only has to type "make" to build the module. This is
21
21
easily accomplished, and a complete example will be presented in
22
- section 3 .
22
+ section ` Creating a Kbuild File for an External Module `_ .
23
23
24
24
25
- 2. How to Build External Modules
26
- ================================
25
+ How to Build External Modules
26
+ =============================
27
27
28
28
To build external modules, you must have a prebuilt kernel available
29
29
that contains the configuration and header files used in the build.
@@ -40,8 +40,8 @@ NOTE: "modules_prepare" will not build Module.symvers even if
40
40
CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be
41
41
executed to make module versioning work.
42
42
43
- 2.1 Command Syntax
44
- ==================
43
+ Command Syntax
44
+ --------------
45
45
46
46
The command to build an external module is::
47
47
@@ -59,8 +59,8 @@ executed to make module versioning work.
59
59
60
60
$ make -C /lib/modules/`uname -r`/build M=$PWD modules_install
61
61
62
- 2.2 Options
63
- ===========
62
+ Options
63
+ -------
64
64
65
65
($KDIR refers to the path of the kernel source directory.)
66
66
@@ -77,8 +77,8 @@ executed to make module versioning work.
77
77
directory where the external module (kbuild file) is
78
78
located.
79
79
80
- 2.3 Targets
81
- ===========
80
+ Targets
81
+ -------
82
82
83
83
When building an external module, only a subset of the "make"
84
84
targets are available.
@@ -100,16 +100,17 @@ executed to make module versioning work.
100
100
modules_install
101
101
Install the external module(s). The default location is
102
102
/lib/modules/<kernel_release>/updates/, but a prefix may
103
- be added with INSTALL_MOD_PATH (discussed in section 5).
103
+ be added with INSTALL_MOD_PATH (discussed in section
104
+ `Module Installation `_).
104
105
105
106
clean
106
107
Remove all generated files in the module directory only.
107
108
108
109
help
109
110
List the available targets for external modules.
110
111
111
- 2.4 Building Separate Files
112
- ===========================
112
+ Building Separate Files
113
+ -----------------------
113
114
114
115
It is possible to build single files that are part of a module.
115
116
This works equally well for the kernel, a module, and even for
@@ -123,8 +124,8 @@ executed to make module versioning work.
123
124
make -C $KDIR M=$PWD ./
124
125
125
126
126
- 3. Creating a Kbuild File for an External Module
127
- ================================================
127
+ Creating a Kbuild File for an External Module
128
+ =============================================
128
129
129
130
In the last section we saw the command to build a module for the
130
131
running kernel. The module is not actually built, however, because a
@@ -153,8 +154,8 @@ module 8123.ko, which is built from the following files::
153
154
8123_pci.c
154
155
8123_bin.o_shipped <= Binary blob
155
156
156
- 3.1 Shared Makefile
157
- -------------------
157
+ Shared Makefile
158
+ ---------------
158
159
159
160
An external module always includes a wrapper makefile that
160
161
supports building the module using "make" with no arguments.
@@ -192,8 +193,8 @@ module 8123.ko, which is built from the following files::
192
193
line; the second pass is by the kbuild system, which is
193
194
initiated by the parameterized "make" in the default target.
194
195
195
- 3.2 Separate Kbuild File and Makefile
196
- -------------------------------------
196
+ Separate Kbuild File and Makefile
197
+ ---------------------------------
197
198
198
199
Kbuild will first look for a file named "Kbuild", and if it is not
199
200
found, it will then look for "Makefile". Utilizing a "Kbuild" file
@@ -220,8 +221,8 @@ module 8123.ko, which is built from the following files::
220
221
consisting of several hundred lines, and here it really pays
221
222
off to separate the kbuild part from the rest.
222
223
223
- 3.3 Binary Blobs
224
- ----------------
224
+ Binary Blobs
225
+ ------------
225
226
226
227
Some external modules need to include an object file as a blob.
227
228
kbuild has support for this, but requires the blob file to be
@@ -240,8 +241,8 @@ module 8123.ko, which is built from the following files::
240
241
files and the binary file, kbuild will pick up different rules
241
242
when creating the object file for the module.
242
243
243
- 3.4 Building Multiple Modules
244
- =============================
244
+ Building Multiple Modules
245
+ -------------------------
245
246
246
247
kbuild supports building multiple modules with a single build
247
248
file. For example, if you wanted to build two modules, foo.ko
@@ -254,8 +255,8 @@ module 8123.ko, which is built from the following files::
254
255
It is that simple!
255
256
256
257
257
- 4. Include Files
258
- ================
258
+ Include Files
259
+ =============
259
260
260
261
Within the kernel, header files are kept in standard locations
261
262
according to the following rule:
@@ -273,8 +274,8 @@ according to the following rule:
273
274
include/scsi; and architecture specific headers are located
274
275
under arch/$(SRCARCH)/include/.
275
276
276
- 4.1 Kernel Includes
277
- -------------------
277
+ Kernel Includes
278
+ ---------------
278
279
279
280
To include a header file located under include/linux/, simply
280
281
use::
@@ -284,8 +285,8 @@ according to the following rule:
284
285
kbuild will add options to "gcc" so the relevant directories
285
286
are searched.
286
287
287
- 4.2 Single Subdirectory
288
- -----------------------
288
+ Single Subdirectory
289
+ -------------------
289
290
290
291
External modules tend to place header files in a separate
291
292
include/ directory where their source is located, although this
@@ -302,8 +303,8 @@ according to the following rule:
302
303
ccflags-y := -I $(src)/include
303
304
8123-y := 8123_if.o 8123_pci.o 8123_bin.o
304
305
305
- 4.3 Several Subdirectories
306
- --------------------------
306
+ Several Subdirectories
307
+ ----------------------
307
308
308
309
kbuild can handle files that are spread over several directories.
309
310
Consider the following example::
@@ -342,8 +343,8 @@ according to the following rule:
342
343
file is located.
343
344
344
345
345
- 5. Module Installation
346
- ======================
346
+ Module Installation
347
+ ===================
347
348
348
349
Modules which are included in the kernel are installed in the
349
350
directory:
@@ -354,8 +355,8 @@ And external modules are installed in:
354
355
355
356
/lib/modules/$(KERNELRELEASE)/updates/
356
357
357
- 5.1 INSTALL_MOD_PATH
358
- --------------------
358
+ INSTALL_MOD_PATH
359
+ ----------------
359
360
360
361
Above are the default directories but as always some level of
361
362
customization is possible. A prefix can be added to the
@@ -369,8 +370,8 @@ And external modules are installed in:
369
370
calling "make." This has effect when installing both in-tree
370
371
and out-of-tree modules.
371
372
372
- 5.2 INSTALL_MOD_DIR
373
- -------------------
373
+ INSTALL_MOD_DIR
374
+ ---------------
374
375
375
376
External modules are by default installed to a directory under
376
377
/lib/modules/$(KERNELRELEASE)/updates/, but you may wish to
@@ -383,8 +384,8 @@ And external modules are installed in:
383
384
=> Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/
384
385
385
386
386
- 6. Module Versioning
387
- ====================
387
+ Module Versioning
388
+ =================
388
389
389
390
Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used
390
391
as a simple ABI consistency check. A CRC value of the full prototype
@@ -396,8 +397,8 @@ module.
396
397
Module.symvers contains a list of all exported symbols from a kernel
397
398
build.
398
399
399
- 6.1 Symbols From the Kernel (vmlinux + modules)
400
- -----------------------------------------------
400
+ Symbols From the Kernel (vmlinux + modules)
401
+ -------------------------------------------
401
402
402
403
During a kernel build, a file named Module.symvers will be
403
404
generated. Module.symvers contains all exported symbols from
@@ -421,8 +422,8 @@ build.
421
422
1) It lists all exported symbols from vmlinux and all modules.
422
423
2) It lists the CRC if CONFIG_MODVERSIONS is enabled.
423
424
424
- 6.2 Symbols and External Modules
425
- --------------------------------
425
+ Symbols and External Modules
426
+ ----------------------------
426
427
427
428
When building an external module, the build system needs access
428
429
to the symbols from the kernel to check if all external symbols
@@ -431,8 +432,8 @@ build.
431
432
tree. During the MODPOST step, a new Module.symvers file will be
432
433
written containing all exported symbols from that external module.
433
434
434
- 6.3 Symbols From Another External Module
435
- ----------------------------------------
435
+ Symbols From Another External Module
436
+ ------------------------------------
436
437
437
438
Sometimes, an external module uses exported symbols from
438
439
another external module. Kbuild needs to have full knowledge of
@@ -472,11 +473,11 @@ build.
472
473
initialization of its symbol tables.
473
474
474
475
475
- 7. Tips & Tricks
476
- ================
476
+ Tips & Tricks
477
+ =============
477
478
478
- 7.1 Testing for CONFIG_FOO_BAR
479
- ------------------------------
479
+ Testing for CONFIG_FOO_BAR
480
+ --------------------------
480
481
481
482
Modules often need to check for certain `CONFIG_ ` options to
482
483
decide if a specific feature is included in the module. In
0 commit comments