aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/kbuild/modules.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/kbuild/modules.txt')
-rw-r--r--Documentation/kbuild/modules.txt161
1 files changed, 81 insertions, 80 deletions
diff --git a/Documentation/kbuild/modules.txt b/Documentation/kbuild/modules.txt
index 61fc079eb966..2e7702e94a78 100644
--- a/Documentation/kbuild/modules.txt
+++ b/Documentation/kbuild/modules.txt
@@ -1,7 +1,7 @@
1 1
2In this document you will find information about: 2In this document you will find information about:
3- how to build external modules 3- how to build external modules
4- how to make your module use kbuild infrastructure 4- how to make your module use the kbuild infrastructure
5- how kbuild will install a kernel 5- how kbuild will install a kernel
6- how to install modules in a non-standard location 6- how to install modules in a non-standard location
7 7
@@ -24,7 +24,7 @@ In this document you will find information about:
24 --- 6.1 INSTALL_MOD_PATH 24 --- 6.1 INSTALL_MOD_PATH
25 --- 6.2 INSTALL_MOD_DIR 25 --- 6.2 INSTALL_MOD_DIR
26 === 7. Module versioning & Module.symvers 26 === 7. Module versioning & Module.symvers
27 --- 7.1 Symbols fron the kernel (vmlinux + modules) 27 --- 7.1 Symbols from the kernel (vmlinux + modules)
28 --- 7.2 Symbols and external modules 28 --- 7.2 Symbols and external modules
29 --- 7.3 Symbols from another external module 29 --- 7.3 Symbols from another external module
30 === 8. Tips & Tricks 30 === 8. Tips & Tricks
@@ -36,13 +36,13 @@ In this document you will find information about:
36 36
37kbuild includes functionality for building modules both 37kbuild includes functionality for building modules both
38within the kernel source tree and outside the kernel source tree. 38within the kernel source tree and outside the kernel source tree.
39The latter is usually referred to as external modules and is used 39The latter is usually referred to as external or "out-of-tree"
40both during development and for modules that are not planned to be 40modules and is used both during development and for modules that
41included in the kernel tree. 41are not planned to be included in the kernel tree.
42 42
43What is covered within this file is mainly information to authors 43What is covered within this file is mainly information to authors
44of modules. The author of an external modules should supply 44of modules. The author of an external module should supply
45a makefile that hides most of the complexity so one only has to type 45a makefile that hides most of the complexity, so one only has to type
46'make' to build the module. A complete example will be present in 46'make' to build the module. A complete example will be present in
47chapter 4, "Creating a kbuild file for an external module". 47chapter 4, "Creating a kbuild file for an external module".
48 48
@@ -63,14 +63,15 @@ when building an external module.
63 For the running kernel use: 63 For the running kernel use:
64 make -C /lib/modules/`uname -r`/build M=`pwd` 64 make -C /lib/modules/`uname -r`/build M=`pwd`
65 65
66 For the above command to succeed the kernel must have been built with 66 For the above command to succeed, the kernel must have been
67 modules enabled. 67 built with modules enabled.
68 68
69 To install the modules that were just built: 69 To install the modules that were just built:
70 70
71 make -C <path-to-kernel> M=`pwd` modules_install 71 make -C <path-to-kernel> M=`pwd` modules_install
72 72
73 More complex examples later, the above should get you going. 73 More complex examples will be shown later, the above should
74 be enough to get you started.
74 75
75--- 2.2 Available targets 76--- 2.2 Available targets
76 77
@@ -89,13 +90,13 @@ when building an external module.
89 Same functionality as if no target was specified. 90 Same functionality as if no target was specified.
90 See description above. 91 See description above.
91 92
92 make -C $KDIR M=$PWD modules_install 93 make -C $KDIR M=`pwd` modules_install
93 Install the external module(s). 94 Install the external module(s).
94 Installation default is in /lib/modules/<kernel-version>/extra, 95 Installation default is in /lib/modules/<kernel-version>/extra,
95 but may be prefixed with INSTALL_MOD_PATH - see separate 96 but may be prefixed with INSTALL_MOD_PATH - see separate
96 chapter. 97 chapter.
97 98
98 make -C $KDIR M=$PWD clean 99 make -C $KDIR M=`pwd` clean
99 Remove all generated files for the module - the kernel 100 Remove all generated files for the module - the kernel
100 source directory is not modified. 101 source directory is not modified.
101 102
@@ -129,29 +130,28 @@ when building an external module.
129 130
130 To make sure the kernel contains the information required to 131 To make sure the kernel contains the information required to
131 build external modules the target 'modules_prepare' must be used. 132 build external modules the target 'modules_prepare' must be used.
132 'module_prepare' solely exists as a simple way to prepare 133 'module_prepare' exists solely as a simple way to prepare
133 a kernel for building external modules. 134 a kernel source tree for building external modules.
134 Note: modules_prepare will not build Module.symvers even if 135 Note: modules_prepare will not build Module.symvers even if
135 CONFIG_MODULEVERSIONING is set. 136 CONFIG_MODULEVERSIONING is set. Therefore a full kernel build
136 Therefore a full kernel build needs to be executed to make 137 needs to be executed to make module versioning work.
137 module versioning work.
138 138
139--- 2.5 Building separate files for a module 139--- 2.5 Building separate files for a module
140 It is possible to build single files which is part of a module. 140 It is possible to build single files which are part of a module.
141 This works equal for the kernel, a module and even for external 141 This works equally well for the kernel, a module and even for
142 modules. 142 external modules.
143 Examples (module foo.ko, consist of bar.o, baz.o): 143 Examples (module foo.ko, consist of bar.o, baz.o):
144 make -C $KDIR M=`pwd` bar.lst 144 make -C $KDIR M=`pwd` bar.lst
145 make -C $KDIR M=`pwd` bar.o 145 make -C $KDIR M=`pwd` bar.o
146 make -C $KDIR M=`pwd` foo.ko 146 make -C $KDIR M=`pwd` foo.ko
147 make -C $KDIR M=`pwd` / 147 make -C $KDIR M=`pwd` /
148 148
149 149
150=== 3. Example commands 150=== 3. Example commands
151 151
152This example shows the actual commands to be executed when building 152This example shows the actual commands to be executed when building
153an external module for the currently running kernel. 153an external module for the currently running kernel.
154In the example below the distribution is supposed to use the 154In the example below, the distribution is supposed to use the
155facility to locate output files for a kernel compile in a different 155facility to locate output files for a kernel compile in a different
156directory than the kernel source - but the examples will also work 156directory than the kernel source - but the examples will also work
157when the source and the output files are mixed in the same directory. 157when the source and the output files are mixed in the same directory.
@@ -170,14 +170,14 @@ the following commands to build the module:
170 O=/lib/modules/`uname-r`/build \ 170 O=/lib/modules/`uname-r`/build \
171 M=`pwd` 171 M=`pwd`
172 172
173Then to install the module use the following command: 173Then, to install the module use the following command:
174 174
175 make -C /usr/src/`uname -r`/source \ 175 make -C /usr/src/`uname -r`/source \
176 O=/lib/modules/`uname-r`/build \ 176 O=/lib/modules/`uname-r`/build \
177 M=`pwd` \ 177 M=`pwd` \
178 modules_install 178 modules_install
179 179
180If one looks closely you will see that this is the same commands as 180If you look closely you will see that this is the same command as
181listed before - with the directories spelled out. 181listed before - with the directories spelled out.
182 182
183The above are rather long commands, and the following chapter 183The above are rather long commands, and the following chapter
@@ -230,7 +230,7 @@ following files:
230 230
231 endif 231 endif
232 232
233 In example 1 the check for KERNELRELEASE is used to separate 233 In example 1, the check for KERNELRELEASE is used to separate
234 the two parts of the Makefile. kbuild will only see the two 234 the two parts of the Makefile. kbuild will only see the two
235 assignments whereas make will see everything except the two 235 assignments whereas make will see everything except the two
236 kbuild assignments. 236 kbuild assignments.
@@ -255,7 +255,7 @@ following files:
255 echo "X" > 8123_bin_shipped 255 echo "X" > 8123_bin_shipped
256 256
257 257
258 In example 2 we are down to two fairly simple files and for simple 258 In example 2, we are down to two fairly simple files and for simple
259 files as used in this example the split is questionable. But some 259 files as used in this example the split is questionable. But some
260 external modules use Makefiles of several hundred lines and here it 260 external modules use Makefiles of several hundred lines and here it
261 really pays off to separate the kbuild part from the rest. 261 really pays off to separate the kbuild part from the rest.
@@ -282,9 +282,9 @@ following files:
282 282
283 endif 283 endif
284 284
285 The trick here is to include the Kbuild file from Makefile so 285 The trick here is to include the Kbuild file from Makefile, so
286 if an older version of kbuild picks up the Makefile the Kbuild 286 if an older version of kbuild picks up the Makefile, the Kbuild
287 file will be included. 287 file will be included.
288 288
289--- 4.2 Binary blobs included in a module 289--- 4.2 Binary blobs included in a module
290 290
@@ -301,18 +301,19 @@ following files:
301 obj-m := 8123.o 301 obj-m := 8123.o
302 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 302 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
303 303
304 In example 4 there is no distinction between the ordinary .c/.h files 304 In example 4, there is no distinction between the ordinary .c/.h files
305 and the binary file. But kbuild will pick up different rules to create 305 and the binary file. But kbuild will pick up different rules to create
306 the .o file. 306 the .o file.
307 307
308 308
309=== 5. Include files 309=== 5. Include files
310 310
311Include files are a necessity when a .c file uses something from another .c 311Include files are a necessity when a .c file uses something from other .c
312files (not strictly in the sense of .c but if good programming practice is 312files (not strictly in the sense of C, but if good programming practice is
313used). Any module that consist of more than one .c file will have a .h file 313used). Any module that consists of more than one .c file will have a .h file
314for one of the .c files. 314for one of the .c files.
315- If the .h file only describes a module internal interface then the .h file 315
316- If the .h file only describes a module internal interface, then the .h file
316 shall be placed in the same directory as the .c files. 317 shall be placed in the same directory as the .c files.
317- If the .h files describe an interface used by other parts of the kernel 318- If the .h files describe an interface used by other parts of the kernel
318 located in different directories, the .h files shall be located in 319 located in different directories, the .h files shall be located in
@@ -323,11 +324,11 @@ under include/ such as include/scsi. Another exception is arch-specific
323.h files which are located under include/asm-$(ARCH)/*. 324.h files which are located under include/asm-$(ARCH)/*.
324 325
325External modules have a tendency to locate include files in a separate include/ 326External modules have a tendency to locate include files in a separate include/
326directory and therefore needs to deal with this in their kbuild file. 327directory and therefore need to deal with this in their kbuild file.
327 328
328--- 5.1 How to include files from the kernel include dir 329--- 5.1 How to include files from the kernel include dir
329 330
330 When a module needs to include a file from include/linux/ then one 331 When a module needs to include a file from include/linux/, then one
331 just uses: 332 just uses:
332 333
333 #include <linux/modules.h> 334 #include <linux/modules.h>
@@ -348,7 +349,7 @@ directory and therefore needs to deal with this in their kbuild file.
348 The trick here is to use either EXTRA_CFLAGS (take effect for all .c 349 The trick here is to use either EXTRA_CFLAGS (take effect for all .c
349 files) or CFLAGS_$F.o (take effect only for a single file). 350 files) or CFLAGS_$F.o (take effect only for a single file).
350 351
351 In our example if we move 8123_if.h to a subdirectory named include/ 352 In our example, if we move 8123_if.h to a subdirectory named include/
352 the resulting Kbuild file would look like: 353 the resulting Kbuild file would look like:
353 354
354 --> filename: Kbuild 355 --> filename: Kbuild
@@ -362,19 +363,19 @@ directory and therefore needs to deal with this in their kbuild file.
362 363
363--- 5.3 External modules using several directories 364--- 5.3 External modules using several directories
364 365
365 If an external module does not follow the usual kernel style but 366 If an external module does not follow the usual kernel style, but
366 decide to spread files over several directories then kbuild can 367 decides to spread files over several directories, then kbuild can
367 support this too. 368 handle this too.
368 369
369 Consider the following example: 370 Consider the following example:
370 371
371 | 372 |
372 +- src/complex_main.c 373 +- src/complex_main.c
373 | +- hal/hardwareif.c 374 | +- hal/hardwareif.c
374 | +- hal/include/hardwareif.h 375 | +- hal/include/hardwareif.h
375 +- include/complex.h 376 +- include/complex.h
376 377
377 To build a single module named complex.ko we then need the following 378 To build a single module named complex.ko, we then need the following
378 kbuild file: 379 kbuild file:
379 380
380 Kbuild: 381 Kbuild:
@@ -387,12 +388,12 @@ directory and therefore needs to deal with this in their kbuild file.
387 388
388 389
389 kbuild knows how to handle .o files located in another directory - 390 kbuild knows how to handle .o files located in another directory -
390 although this is NOT reccommended practice. The syntax is to specify 391 although this is NOT recommended practice. The syntax is to specify
391 the directory relative to the directory where the Kbuild file is 392 the directory relative to the directory where the Kbuild file is
392 located. 393 located.
393 394
394 To find the .h files we have to explicitly tell kbuild where to look 395 To find the .h files, we have to explicitly tell kbuild where to look
395 for the .h files. When kbuild executes current directory is always 396 for the .h files. When kbuild executes, the current directory is always
396 the root of the kernel tree (argument to -C) and therefore we have to 397 the root of the kernel tree (argument to -C) and therefore we have to
397 tell kbuild how to find the .h files using absolute paths. 398 tell kbuild how to find the .h files using absolute paths.
398 $(src) will specify the absolute path to the directory where the 399 $(src) will specify the absolute path to the directory where the
@@ -412,7 +413,7 @@ External modules are installed in the directory:
412 413
413--- 6.1 INSTALL_MOD_PATH 414--- 6.1 INSTALL_MOD_PATH
414 415
415 Above are the default directories, but as always some level of 416 Above are the default directories, but as always, some level of
416 customization is possible. One can prefix the path using the variable 417 customization is possible. One can prefix the path using the variable
417 INSTALL_MOD_PATH: 418 INSTALL_MOD_PATH:
418 419
@@ -420,17 +421,17 @@ External modules are installed in the directory:
420 => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel 421 => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel
421 422
422 INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the 423 INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the
423 example above be specified on the command line when calling make. 424 example above, can be specified on the command line when calling make.
424 INSTALL_MOD_PATH has effect both when installing modules included in 425 INSTALL_MOD_PATH has effect both when installing modules included in
425 the kernel as well as when installing external modules. 426 the kernel as well as when installing external modules.
426 427
427--- 6.2 INSTALL_MOD_DIR 428--- 6.2 INSTALL_MOD_DIR
428 429
429 When installing external modules they are default installed in a 430 When installing external modules they are by default installed to a
430 directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish 431 directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish
431 to locate modules for a specific functionality in a separate 432 to locate modules for a specific functionality in a separate
432 directory. For this purpose one can use INSTALL_MOD_DIR to specify an 433 directory. For this purpose, one can use INSTALL_MOD_DIR to specify an
433 alternative name than 'extra'. 434 alternative name to 'extra'.
434 435
435 $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \ 436 $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \
436 M=`pwd` modules_install 437 M=`pwd` modules_install
@@ -444,16 +445,16 @@ Module versioning is enabled by the CONFIG_MODVERSIONS tag.
444Module versioning is used as a simple ABI consistency check. The Module 445Module versioning is used as a simple ABI consistency check. The Module
445versioning creates a CRC value of the full prototype for an exported symbol and 446versioning creates a CRC value of the full prototype for an exported symbol and
446when a module is loaded/used then the CRC values contained in the kernel are 447when a module is loaded/used then the CRC values contained in the kernel are
447compared with similar values in the module. If they are not equal then the 448compared with similar values in the module. If they are not equal, then the
448kernel refuses to load the module. 449kernel refuses to load the module.
449 450
450Module.symvers contains a list of all exported symbols from a kernel build. 451Module.symvers contains a list of all exported symbols from a kernel build.
451 452
452--- 7.1 Symbols fron the kernel (vmlinux + modules) 453--- 7.1 Symbols fron the kernel (vmlinux + modules)
453 454
454 During a kernel build a file named Module.symvers will be generated. 455 During a kernel build, a file named Module.symvers will be generated.
455 Module.symvers contains all exported symbols from the kernel and 456 Module.symvers contains all exported symbols from the kernel and
456 compiled modules. For each symbols the corresponding CRC value 457 compiled modules. For each symbols, the corresponding CRC value
457 is stored too. 458 is stored too.
458 459
459 The syntax of the Module.symvers file is: 460 The syntax of the Module.symvers file is:
@@ -461,27 +462,27 @@ Module.symvers contains a list of all exported symbols from a kernel build.
461 Sample: 462 Sample:
462 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod 463 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod
463 464
464 For a kernel build without CONFIG_MODVERSIONING enabled the crc 465 For a kernel build without CONFIG_MODVERSIONS enabled, the crc
465 would read: 0x00000000 466 would read: 0x00000000
466 467
467 Module.symvers serve two purposes. 468 Module.symvers serves two purposes:
468 1) It list all exported symbols both from vmlinux and all modules 469 1) It lists all exported symbols both from vmlinux and all modules
469 2) It list CRC if CONFIG_MODVERSION is enabled 470 2) It lists the CRC if CONFIG_MODVERSIONS is enabled
470 471
471--- 7.2 Symbols and external modules 472--- 7.2 Symbols and external modules
472 473
473 When building an external module the build system needs access to 474 When building an external module, the build system needs access to
474 the symbols from the kernel to check if all external symbols are 475 the symbols from the kernel to check if all external symbols are
475 defined. This is done in the MODPOST step and to obtain all 476 defined. This is done in the MODPOST step and to obtain all
476 symbols modpost reads Module.symvers from the kernel. 477 symbols, modpost reads Module.symvers from the kernel.
477 If a Module.symvers file is present in the directory where 478 If a Module.symvers file is present in the directory where
478 the external module is being build this file will be read too. 479 the external module is being built, this file will be read too.
479 During the MODPOST step a new Module.symvers file will be written 480 During the MODPOST step, a new Module.symvers file will be written
480 containing all exported symbols that was not defined in the kernel. 481 containing all exported symbols that were not defined in the kernel.
481 482
482--- 7.3 Symbols from another external module 483--- 7.3 Symbols from another external module
483 484
484 Sometimes one external module uses exported symbols from another 485 Sometimes, an external module uses exported symbols from another
485 external module. Kbuild needs to have full knowledge on all symbols 486 external module. Kbuild needs to have full knowledge on all symbols
486 to avoid spitting out warnings about undefined symbols. 487 to avoid spitting out warnings about undefined symbols.
487 Two solutions exist to let kbuild know all symbols of more than 488 Two solutions exist to let kbuild know all symbols of more than
@@ -490,15 +491,15 @@ Module.symvers contains a list of all exported symbols from a kernel build.
490 impractical in certain situations. 491 impractical in certain situations.
491 492
492 Use a top-level Kbuild file 493 Use a top-level Kbuild file
493 If you have two modules: 'foo', 'bar' and 'foo' needs symbols 494 If you have two modules: 'foo' and 'bar', and 'foo' needs
494 from 'bar' then one can use a common top-level kbuild file so 495 symbols from 'bar', then one can use a common top-level kbuild
495 both modules are compiled in same build. 496 file so both modules are compiled in same build.
496 497
497 Consider following directory layout: 498 Consider following directory layout:
498 ./foo/ <= contains the foo module 499 ./foo/ <= contains the foo module
499 ./bar/ <= contains the bar module 500 ./bar/ <= contains the bar module
500 The top-level Kbuild file would then look like: 501 The top-level Kbuild file would then look like:
501 502
502 #./Kbuild: (this file may also be named Makefile) 503 #./Kbuild: (this file may also be named Makefile)
503 obj-y := foo/ bar/ 504 obj-y := foo/ bar/
504 505
@@ -509,23 +510,23 @@ Module.symvers contains a list of all exported symbols from a kernel build.
509 knowledge on symbols from both modules. 510 knowledge on symbols from both modules.
510 511
511 Use an extra Module.symvers file 512 Use an extra Module.symvers file
512 When an external module is build a Module.symvers file is 513 When an external module is built, a Module.symvers file is
513 generated containing all exported symbols which are not 514 generated containing all exported symbols which are not
514 defined in the kernel. 515 defined in the kernel.
515 To get access to symbols from module 'bar' one can copy the 516 To get access to symbols from module 'bar', one can copy the
516 Module.symvers file from the compilation of the 'bar' module 517 Module.symvers file from the compilation of the 'bar' module
517 to the directory where the 'foo' module is build. 518 to the directory where the 'foo' module is built.
518 During the module build kbuild will read the Module.symvers 519 During the module build, kbuild will read the Module.symvers
519 file in the directory of the external module and when the 520 file in the directory of the external module and when the
520 build is finished a new Module.symvers file is created 521 build is finished, a new Module.symvers file is created
521 containing the sum of all symbols defined and not part of the 522 containing the sum of all symbols defined and not part of the
522 kernel. 523 kernel.
523 524
524=== 8. Tips & Tricks 525=== 8. Tips & Tricks
525 526
526--- 8.1 Testing for CONFIG_FOO_BAR 527--- 8.1 Testing for CONFIG_FOO_BAR
527 528
528 Modules often needs to check for certain CONFIG_ options to decide if 529 Modules often need to check for certain CONFIG_ options to decide if
529 a specific feature shall be included in the module. When kbuild is used 530 a specific feature shall be included in the module. When kbuild is used
530 this is done by referencing the CONFIG_ variable directly. 531 this is done by referencing the CONFIG_ variable directly.
531 532
@@ -537,7 +538,7 @@ Module.symvers contains a list of all exported symbols from a kernel build.
537 538
538 External modules have traditionally used grep to check for specific 539 External modules have traditionally used grep to check for specific
539 CONFIG_ settings directly in .config. This usage is broken. 540 CONFIG_ settings directly in .config. This usage is broken.
540 As introduced before external modules shall use kbuild when building 541 As introduced before, external modules shall use kbuild when building
541 and therefore can use the same methods as in-kernel modules when testing 542 and therefore can use the same methods as in-kernel modules when
542 for CONFIG_ definitions. 543 testing for CONFIG_ definitions.
543 544