diff options
Diffstat (limited to 'Documentation')
-rw-r--r-- | Documentation/kbuild/modules.txt | 358 |
1 files changed, 177 insertions, 181 deletions
diff --git a/Documentation/kbuild/modules.txt b/Documentation/kbuild/modules.txt index 799b6835993f..b572db3c5fee 100644 --- a/Documentation/kbuild/modules.txt +++ b/Documentation/kbuild/modules.txt | |||
@@ -15,17 +15,17 @@ This document describes how-to build an out-of-tree kernel module. | |||
15 | --- 3.2 Separate Kbuild file and Makefile | 15 | --- 3.2 Separate Kbuild file and Makefile |
16 | --- 3.3 Binary Blobs | 16 | --- 3.3 Binary Blobs |
17 | --- 3.4 Building Multiple Modules | 17 | --- 3.4 Building Multiple Modules |
18 | === 4. Include files | 18 | === 4. Include Files |
19 | --- 4.1 How to include files from the kernel include dir | 19 | --- 4.1 Kernel Includes |
20 | --- 4.2 External modules using an include/ dir | 20 | --- 4.2 Single Subdirectory |
21 | --- 4.3 External modules using several directories | 21 | --- 4.3 Several Subdirectories |
22 | === 5. Module installation | 22 | === 5. Module Installation |
23 | --- 5.1 INSTALL_MOD_PATH | 23 | --- 5.1 INSTALL_MOD_PATH |
24 | --- 5.2 INSTALL_MOD_DIR | 24 | --- 5.2 INSTALL_MOD_DIR |
25 | === 6. Module versioning & Module.symvers | 25 | === 6. Module Versioning |
26 | --- 6.1 Symbols from the kernel (vmlinux + modules) | 26 | --- 6.1 Symbols From the Kernel (vmlinux + modules) |
27 | --- 6.2 Symbols and external modules | 27 | --- 6.2 Symbols and External Modules |
28 | --- 6.3 Symbols from another external module | 28 | --- 6.3 Symbols From Another External Module |
29 | === 7. Tips & Tricks | 29 | === 7. Tips & Tricks |
30 | --- 7.1 Testing for CONFIG_FOO_BAR | 30 | --- 7.1 Testing for CONFIG_FOO_BAR |
31 | 31 | ||
@@ -298,236 +298,232 @@ module 8123.ko, which is built from the following files: | |||
298 | It is that simple! | 298 | It is that simple! |
299 | 299 | ||
300 | 300 | ||
301 | === 5. Include files | 301 | === 4. Include Files |
302 | 302 | ||
303 | Include files are a necessity when a .c file uses something from other .c | 303 | Within the kernel, header files are kept in standard locations |
304 | files (not strictly in the sense of C, but if good programming practice is | 304 | according to the following rule: |
305 | used). Any module that consists of more than one .c file will have a .h file | ||
306 | for one of the .c files. | ||
307 | 305 | ||
308 | - If the .h file only describes a module internal interface, then the .h file | 306 | * If the header file only describes the internal interface of a |
309 | shall be placed in the same directory as the .c files. | 307 | module, then the file is placed in the same directory as the |
310 | - If the .h files describe an interface used by other parts of the kernel | 308 | source files. |
311 | located in different directories, the .h files shall be located in | 309 | * If the header file describes an interface used by other parts |
312 | include/linux/ or other include/ directories as appropriate. | 310 | of the kernel that are located in different directories, then |
311 | the file is placed in include/linux/. | ||
313 | 312 | ||
314 | One exception for this rule is larger subsystems that have their own directory | 313 | NOTE: There are two notable exceptions to this rule: larger |
315 | under include/ such as include/scsi. Another exception is arch-specific | 314 | subsystems have their own directory under include/, such as |
316 | .h files which are located under include/asm-$(ARCH)/*. | 315 | include/scsi; and architecture specific headers are located |
316 | under arch/$(ARCH)/include/. | ||
317 | 317 | ||
318 | External modules have a tendency to locate include files in a separate include/ | 318 | --- 4.1 Kernel Includes |
319 | directory and therefore need to deal with this in their kbuild file. | ||
320 | 319 | ||
321 | --- 5.1 How to include files from the kernel include dir | 320 | To include a header file located under include/linux/, simply |
322 | 321 | use: | |
323 | When a module needs to include a file from include/linux/, then one | ||
324 | just uses: | ||
325 | 322 | ||
326 | #include <linux/modules.h> | 323 | #include <linux/modules.h> |
327 | 324 | ||
328 | kbuild will make sure to add options to gcc so the relevant | 325 | kbuild will add options to "gcc" so the relevant directories |
329 | directories are searched. | 326 | are searched. |
330 | Likewise for .h files placed in the same directory as the .c file. | ||
331 | |||
332 | #include "8123_if.h" | ||
333 | |||
334 | will do the job. | ||
335 | 327 | ||
336 | --- 5.2 External modules using an include/ dir | 328 | --- 4.2 Single Subdirectory |
337 | 329 | ||
338 | External modules often locate their .h files in a separate include/ | 330 | External modules tend to place header files in a separate |
339 | directory although this is not usual kernel style. When an external | 331 | include/ directory where their source is located, although this |
340 | module uses an include/ dir then kbuild needs to be told so. | 332 | is not the usual kernel style. To inform kbuild of the |
341 | The trick here is to use either EXTRA_CFLAGS (take effect for all .c | 333 | directory use either ccflags-y or CFLAGS_<filename>.o. |
342 | files) or CFLAGS_$F.o (take effect only for a single file). | ||
343 | 334 | ||
344 | In our example, if we move 8123_if.h to a subdirectory named include/ | 335 | Using the example from section 3, if we moved 8123_if.h to a |
345 | the resulting Kbuild file would look like: | 336 | subdirectory named include, the resulting kbuild file would |
337 | look like: | ||
346 | 338 | ||
347 | --> filename: Kbuild | 339 | --> filename: Kbuild |
348 | obj-m := 8123.o | 340 | obj-m := 8123.o |
349 | 341 | ||
350 | EXTRA_CFLAGS := -Iinclude | 342 | ccflags-y := -Iinclude |
351 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o | 343 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
352 | 344 | ||
353 | Note that in the assignment there is no space between -I and the path. | 345 | Note that in the assignment there is no space between -I and |
354 | This is a kbuild limitation: there must be no space present. | 346 | the path. This is a limitation of kbuild: there must be no |
355 | 347 | space present. | |
356 | --- 5.3 External modules using several directories | ||
357 | 348 | ||
358 | If an external module does not follow the usual kernel style, but | 349 | --- 4.3 Several Subdirectories |
359 | decides to spread files over several directories, then kbuild can | ||
360 | handle this too. | ||
361 | 350 | ||
351 | kbuild can handle files that are spread over several directories. | ||
362 | Consider the following example: | 352 | Consider the following example: |
363 | 353 | ||
364 | | | 354 | . |
365 | +- src/complex_main.c | 355 | |__ src |
366 | | +- hal/hardwareif.c | 356 | | |__ complex_main.c |
367 | | +- hal/include/hardwareif.h | 357 | | |__ hal |
368 | +- include/complex.h | 358 | | |__ hardwareif.c |
369 | 359 | | |__ include | |
370 | To build a single module named complex.ko, we then need the following | 360 | | |__ hardwareif.h |
361 | |__ include | ||
362 | |__ complex.h | ||
363 | |||
364 | To build the module complex.ko, we then need the following | ||
371 | kbuild file: | 365 | kbuild file: |
372 | 366 | ||
373 | Kbuild: | 367 | --> filename: Kbuild |
374 | obj-m := complex.o | 368 | obj-m := complex.o |
375 | complex-y := src/complex_main.o | 369 | complex-y := src/complex_main.o |
376 | complex-y += src/hal/hardwareif.o | 370 | complex-y += src/hal/hardwareif.o |
377 | 371 | ||
378 | EXTRA_CFLAGS := -I$(src)/include | 372 | ccflags-y := -I$(src)/include |
379 | EXTRA_CFLAGS += -I$(src)src/hal/include | 373 | ccflags-y += -I$(src)/src/hal/include |
380 | 374 | ||
375 | As you can see, kbuild knows how to handle object files located | ||
376 | in other directories. The trick is to specify the directory | ||
377 | relative to the kbuild file's location. That being said, this | ||
378 | is NOT recommended practice. | ||
381 | 379 | ||
382 | kbuild knows how to handle .o files located in another directory - | 380 | For the header files, kbuild must be explicitly told where to |
383 | although this is NOT recommended practice. The syntax is to specify | 381 | look. When kbuild executes, the current directory is always the |
384 | the directory relative to the directory where the Kbuild file is | 382 | root of the kernel tree (the argument to "-C") and therefore an |
385 | located. | 383 | absolute path is needed. $(src) provides the absolute path by |
384 | pointing to the directory where the currently executing kbuild | ||
385 | file is located. | ||
386 | 386 | ||
387 | To find the .h files, we have to explicitly tell kbuild where to look | ||
388 | for the .h files. When kbuild executes, the current directory is always | ||
389 | the root of the kernel tree (argument to -C) and therefore we have to | ||
390 | tell kbuild how to find the .h files using absolute paths. | ||
391 | $(src) will specify the absolute path to the directory where the | ||
392 | Kbuild file are located when being build as an external module. | ||
393 | Therefore -I$(src)/ is used to point out the directory of the Kbuild | ||
394 | file and any additional path are just appended. | ||
395 | 387 | ||
396 | === 6. Module installation | 388 | === 5. Module Installation |
397 | 389 | ||
398 | Modules which are included in the kernel are installed in the directory: | 390 | Modules which are included in the kernel are installed in the |
391 | directory: | ||
399 | 392 | ||
400 | /lib/modules/$(KERNELRELEASE)/kernel | 393 | /lib/modules/$(KERNELRELEASE)/kernel |
401 | 394 | ||
402 | External modules are installed in the directory: | 395 | And external modules are installed in: |
403 | 396 | ||
404 | /lib/modules/$(KERNELRELEASE)/extra | 397 | /lib/modules/$(KERNELRELEASE)/extra |
405 | 398 | ||
406 | --- 6.1 INSTALL_MOD_PATH | 399 | --- 5.1 INSTALL_MOD_PATH |
407 | 400 | ||
408 | Above are the default directories, but as always, some level of | 401 | Above are the default directories but as always some level of |
409 | customization is possible. One can prefix the path using the variable | 402 | customization is possible. A prefix can be added to the |
410 | INSTALL_MOD_PATH: | 403 | installation path using the variable INSTALL_MOD_PATH: |
411 | 404 | ||
412 | $ make INSTALL_MOD_PATH=/frodo modules_install | 405 | $ make INSTALL_MOD_PATH=/frodo modules_install |
413 | => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel | 406 | => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel |
414 | 407 | ||
415 | INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the | 408 | INSTALL_MOD_PATH may be set as an ordinary shell variable or, |
416 | example above, can be specified on the command line when calling make. | 409 | as shown above, can be specified on the command line when |
417 | INSTALL_MOD_PATH has effect both when installing modules included in | 410 | calling "make." This has effect when installing both in-tree |
418 | the kernel as well as when installing external modules. | 411 | and out-of-tree modules. |
419 | 412 | ||
420 | --- 6.2 INSTALL_MOD_DIR | 413 | --- 5.2 INSTALL_MOD_DIR |
421 | 414 | ||
422 | When installing external modules they are by default installed to a | 415 | External modules are by default installed to a directory under |
423 | directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish | 416 | /lib/modules/$(KERNELRELEASE)/extra, but you may wish to locate |
424 | to locate modules for a specific functionality in a separate | 417 | modules for a specific functionality in a separate directory. |
425 | directory. For this purpose, one can use INSTALL_MOD_DIR to specify an | 418 | For this purpose, use INSTALL_MOD_DIR to specify an alternative |
426 | alternative name to 'extra'. | 419 | name to "extra." |
427 | 420 | ||
428 | $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \ | 421 | $ make INSTALL_MOD_DIR=gandalf -C $KDIR \ |
429 | M=`pwd` modules_install | 422 | M=$PWD modules_install |
430 | => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf | 423 | => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf |
431 | 424 | ||
432 | 425 | ||
433 | === 7. Module versioning & Module.symvers | 426 | === 6. Module Versioning |
434 | 427 | ||
435 | Module versioning is enabled by the CONFIG_MODVERSIONS tag. | 428 | Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used |
429 | as a simple ABI consistency check. A CRC value of the full prototype | ||
430 | for an exported symbol is created. When a module is loaded/used, the | ||
431 | CRC values contained in the kernel are compared with similar values in | ||
432 | the module; if they are not equal, the kernel refuses to load the | ||
433 | module. | ||
436 | 434 | ||
437 | Module versioning is used as a simple ABI consistency check. The Module | 435 | Module.symvers contains a list of all exported symbols from a kernel |
438 | versioning creates a CRC value of the full prototype for an exported symbol and | 436 | build. |
439 | when a module is loaded/used then the CRC values contained in the kernel are | ||
440 | compared with similar values in the module. If they are not equal, then the | ||
441 | kernel refuses to load the module. | ||
442 | 437 | ||
443 | Module.symvers contains a list of all exported symbols from a kernel build. | 438 | --- 6.1 Symbols From the Kernel (vmlinux + modules) |
444 | 439 | ||
445 | --- 7.1 Symbols from the kernel (vmlinux + modules) | 440 | During a kernel build, a file named Module.symvers will be |
446 | 441 | generated. Module.symvers contains all exported symbols from | |
447 | During a kernel build, a file named Module.symvers will be generated. | 442 | the kernel and compiled modules. For each symbol, the |
448 | Module.symvers contains all exported symbols from the kernel and | 443 | corresponding CRC value is also stored. |
449 | compiled modules. For each symbols, the corresponding CRC value | ||
450 | is stored too. | ||
451 | 444 | ||
452 | The syntax of the Module.symvers file is: | 445 | The syntax of the Module.symvers file is: |
453 | <CRC> <Symbol> <module> | 446 | <CRC> <Symbol> <module> |
454 | Sample: | 447 | |
455 | 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod | 448 | 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod |
456 | 449 | ||
457 | For a kernel build without CONFIG_MODVERSIONS enabled, the crc | 450 | For a kernel build without CONFIG_MODVERSIONS enabled, the CRC |
458 | would read: 0x00000000 | 451 | would read 0x00000000. |
459 | 452 | ||
460 | Module.symvers serves two purposes: | 453 | Module.symvers serves two purposes: |
461 | 1) It lists all exported symbols both from vmlinux and all modules | 454 | 1) It lists all exported symbols from vmlinux and all modules. |
462 | 2) It lists the CRC if CONFIG_MODVERSIONS is enabled | 455 | 2) It lists the CRC if CONFIG_MODVERSIONS is enabled. |
463 | 456 | ||
464 | --- 7.2 Symbols and external modules | 457 | --- 6.2 Symbols and External Modules |
465 | 458 | ||
466 | When building an external module, the build system needs access to | 459 | When building an external module, the build system needs access |
467 | the symbols from the kernel to check if all external symbols are | 460 | to the symbols from the kernel to check if all external symbols |
468 | defined. This is done in the MODPOST step and to obtain all | 461 | are defined. This is done in the MODPOST step. modpost obtains |
469 | symbols, modpost reads Module.symvers from the kernel. | 462 | the symbols by reading Module.symvers from the kernel source |
470 | If a Module.symvers file is present in the directory where | 463 | tree. If a Module.symvers file is present in the directory |
471 | the external module is being built, this file will be read too. | 464 | where the external module is being built, this file will be |
472 | During the MODPOST step, a new Module.symvers file will be written | 465 | read too. During the MODPOST step, a new Module.symvers file |
473 | containing all exported symbols that were not defined in the kernel. | 466 | will be written containing all exported symbols that were not |
474 | 467 | defined in the kernel. | |
475 | --- 7.3 Symbols from another external module | 468 | |
476 | 469 | --- 6.3 Symbols From Another External Module | |
477 | Sometimes, an external module uses exported symbols from another | 470 | |
478 | external module. Kbuild needs to have full knowledge on all symbols | 471 | Sometimes, an external module uses exported symbols from |
479 | to avoid spitting out warnings about undefined symbols. | 472 | another external module. kbuild needs to have full knowledge of |
480 | Three solutions exist to let kbuild know all symbols of more than | 473 | all symbols to avoid spitting out warnings about undefined |
481 | one external module. | 474 | symbols. Three solutions exist for this situation. |
482 | The method with a top-level kbuild file is recommended but may be | 475 | |
483 | impractical in certain situations. | 476 | NOTE: The method with a top-level kbuild file is recommended |
484 | 477 | but may be impractical in certain situations. | |
485 | Use a top-level Kbuild file | 478 | |
486 | If you have two modules: 'foo' and 'bar', and 'foo' needs | 479 | Use a top-level kbuild file |
487 | symbols from 'bar', then one can use a common top-level kbuild | 480 | If you have two modules, foo.ko and bar.ko, where |
488 | file so both modules are compiled in same build. | 481 | foo.ko needs symbols from bar.ko, then you can use a |
489 | 482 | common top-level kbuild file so both modules are | |
490 | Consider following directory layout: | 483 | compiled in the same build. Consider following |
491 | ./foo/ <= contains the foo module | 484 | directory layout: |
492 | ./bar/ <= contains the bar module | 485 | |
493 | The top-level Kbuild file would then look like: | 486 | ./foo/ <= contains foo.ko |
494 | 487 | ./bar/ <= contains bar.ko | |
495 | #./Kbuild: (this file may also be named Makefile) | 488 | |
489 | The top-level kbuild file would then look like: | ||
490 | |||
491 | #./Kbuild (or ./Makefile): | ||
496 | obj-y := foo/ bar/ | 492 | obj-y := foo/ bar/ |
497 | 493 | ||
498 | Executing: | 494 | And executing: |
499 | make -C $KDIR M=`pwd` | 495 | $ make -C $KDIR M=$PWD |
500 | 496 | ||
501 | will then do the expected and compile both modules with full | 497 | Will then do the expected and compile both modules with |
502 | knowledge on symbols from both modules. | 498 | full knowledge of symbols from either module. |
503 | 499 | ||
504 | Use an extra Module.symvers file | 500 | Use an extra Module.symvers file |
505 | When an external module is built, a Module.symvers file is | 501 | When an external module is built, a Module.symvers file |
506 | generated containing all exported symbols which are not | 502 | is generated containing all exported symbols which are |
507 | defined in the kernel. | 503 | not defined in the kernel. To get access to symbols |
508 | To get access to symbols from module 'bar', one can copy the | 504 | from bar.ko, copy the Module.symvers file from the |
509 | Module.symvers file from the compilation of the 'bar' module | 505 | compilation of bar.ko to the directory where foo.ko is |
510 | to the directory where the 'foo' module is built. | 506 | built. During the module build, kbuild will read the |
511 | During the module build, kbuild will read the Module.symvers | 507 | Module.symvers file in the directory of the external |
512 | file in the directory of the external module and when the | 508 | module, and when the build is finished, a new |
513 | build is finished, a new Module.symvers file is created | 509 | Module.symvers file is created containing the sum of |
514 | containing the sum of all symbols defined and not part of the | 510 | all symbols defined and not part of the kernel. |
515 | kernel. | 511 | |
516 | 512 | Use "make" variable KBUILD_EXTRA_SYMBOLS | |
517 | Use make variable KBUILD_EXTRA_SYMBOLS in the Makefile | 513 | If it is impractical to copy Module.symvers from |
518 | If it is impractical to copy Module.symvers from another | 514 | another module, you can assign a space separated list |
519 | module, you can assign a space separated list of files to | 515 | of files to KBUILD_EXTRA_SYMBOLS in your build |
520 | KBUILD_EXTRA_SYMBOLS in your Makfile. These files will be | 516 | file. These files will be loaded by modpost during the |
521 | loaded by modpost during the initialisation of its symbol | 517 | initialization of its symbol tables. |
522 | tables. | 518 | |
523 | 519 | === 7. Tips & Tricks | |
524 | === 8. Tips & Tricks | 520 | |
525 | 521 | --- 7.1 Testing for CONFIG_FOO_BAR | |
526 | --- 8.1 Testing for CONFIG_FOO_BAR | 522 | |
527 | 523 | Modules often need to check for certain CONFIG_ options to | |
528 | Modules often need to check for certain CONFIG_ options to decide if | 524 | decide if a specific feature is included in the module. In |
529 | a specific feature shall be included in the module. When kbuild is used | 525 | kbuild this is done by referencing the CONFIG_ variable |
530 | this is done by referencing the CONFIG_ variable directly. | 526 | directly. |
531 | 527 | ||
532 | #fs/ext2/Makefile | 528 | #fs/ext2/Makefile |
533 | obj-$(CONFIG_EXT2_FS) += ext2.o | 529 | obj-$(CONFIG_EXT2_FS) += ext2.o |
@@ -535,9 +531,9 @@ Module.symvers contains a list of all exported symbols from a kernel build. | |||
535 | ext2-y := balloc.o bitmap.o dir.o | 531 | ext2-y := balloc.o bitmap.o dir.o |
536 | ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o | 532 | ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o |
537 | 533 | ||
538 | External modules have traditionally used grep to check for specific | 534 | External modules have traditionally used "grep" to check for |
539 | CONFIG_ settings directly in .config. This usage is broken. | 535 | specific CONFIG_ settings directly in .config. This usage is |
540 | As introduced before, external modules shall use kbuild when building | 536 | broken. As introduced before, external modules should use |
541 | and therefore can use the same methods as in-kernel modules when | 537 | kbuild for building and can therefore use the same methods as |
542 | testing for CONFIG_ definitions. | 538 | in-tree modules when testing for CONFIG_ definitions. |
543 | 539 | ||