1========================= 2Building External Modules 3========================= 4 5This document describes how to build an out-of-tree kernel module. 6 7.. Table of Contents 8 9 === 1 Introduction 10 === 2 How to Build External Modules 11 --- 2.1 Command Syntax 12 --- 2.2 Options 13 --- 2.3 Targets 14 --- 2.4 Building Separate Files 15 === 3. Creating a Kbuild File for an External Module 16 --- 3.1 Shared Makefile 17 --- 3.2 Separate Kbuild file and Makefile 18 --- 3.3 Binary Blobs 19 --- 3.4 Building Multiple Modules 20 === 4. Include Files 21 --- 4.1 Kernel Includes 22 --- 4.2 Single Subdirectory 23 --- 4.3 Several Subdirectories 24 --- 4.4 UAPI Headers Installation 25 === 5. Module Installation 26 --- 5.1 INSTALL_MOD_PATH 27 --- 5.2 INSTALL_MOD_DIR 28 === 6. Module Versioning 29 --- 6.1 Symbols From the Kernel (vmlinux + modules) 30 --- 6.2 Symbols and External Modules 31 --- 6.3 Symbols From Another External Module 32 === 7. Tips & Tricks 33 --- 7.1 Testing for CONFIG_FOO_BAR 34 35 36 371. Introduction 38=============== 39 40"kbuild" is the build system used by the Linux kernel. Modules must use 41kbuild to stay compatible with changes in the build infrastructure and 42to pick up the right flags to "gcc." Functionality for building modules 43both in-tree and out-of-tree is provided. The method for building 44either is similar, and all modules are initially developed and built 45out-of-tree. 46 47Covered in this document is information aimed at developers interested 48in building out-of-tree (or "external") modules. The author of an 49external module should supply a makefile that hides most of the 50complexity, so one only has to type "make" to build the module. This is 51easily accomplished, and a complete example will be presented in 52section 3. 53 54 552. How to Build External Modules 56================================ 57 58To build external modules, you must have a prebuilt kernel available 59that contains the configuration and header files used in the build. 60Also, the kernel must have been built with modules enabled. If you are 61using a distribution kernel, there will be a package for the kernel you 62are running provided by your distribution. 63 64An alternative is to use the "make" target "modules_prepare." This will 65make sure the kernel contains the information required. The target 66exists solely as a simple way to prepare a kernel source tree for 67building external modules. 68 69NOTE: "modules_prepare" will not build Module.symvers even if 70CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be 71executed to make module versioning work. 72 732.1 Command Syntax 74================== 75 76 The command to build an external module is:: 77 78 $ make -C <path_to_kernel_src> M=$PWD 79 80 The kbuild system knows that an external module is being built 81 due to the "M=<dir>" option given in the command. 82 83 To build against the running kernel use:: 84 85 $ make -C /lib/modules/`uname -r`/build M=$PWD 86 87 Then to install the module(s) just built, add the target 88 "modules_install" to the command:: 89 90 $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install 91 922.2 Options 93=========== 94 95 ($KDIR refers to the path of the kernel source directory.) 96 97 make -C $KDIR M=$PWD 98 99 -C $KDIR 100 The directory where the kernel source is located. 101 "make" will actually change to the specified directory 102 when executing and will change back when finished. 103 104 M=$PWD 105 Informs kbuild that an external module is being built. 106 The value given to "M" is the absolute path of the 107 directory where the external module (kbuild file) is 108 located. 109 1102.3 Targets 111=========== 112 113 When building an external module, only a subset of the "make" 114 targets are available. 115 116 make -C $KDIR M=$PWD [target] 117 118 The default will build the module(s) located in the current 119 directory, so a target does not need to be specified. All 120 output files will also be generated in this directory. No 121 attempts are made to update the kernel source, and it is a 122 precondition that a successful "make" has been executed for the 123 kernel. 124 125 modules 126 The default target for external modules. It has the 127 same functionality as if no target was specified. See 128 description above. 129 130 modules_install 131 Install the external module(s). The default location is 132 /lib/modules/<kernel_release>/extra/, but a prefix may 133 be added with INSTALL_MOD_PATH (discussed in section 5). 134 135 headers_install 136 Export headers in a format suitable for userspace. The default 137 location is $PWD/usr. INSTALL_HDR_PATH can change this path. 138 139 clean 140 Remove all generated files in the module directory only. 141 142 help 143 List the available targets for external modules. 144 1452.4 Building Separate Files 146=========================== 147 148 It is possible to build single files that are part of a module. 149 This works equally well for the kernel, a module, and even for 150 external modules. 151 152 Example (The module foo.ko, consist of bar.o and baz.o):: 153 154 make -C $KDIR M=$PWD bar.lst 155 make -C $KDIR M=$PWD baz.o 156 make -C $KDIR M=$PWD foo.ko 157 make -C $KDIR M=$PWD ./ 158 159 1603. Creating a Kbuild File for an External Module 161================================================ 162 163In the last section we saw the command to build a module for the 164running kernel. The module is not actually built, however, because a 165build file is required. Contained in this file will be the name of 166the module(s) being built, along with the list of requisite source 167files. The file may be as simple as a single line:: 168 169 obj-m := <module_name>.o 170 171The kbuild system will build <module_name>.o from <module_name>.c, 172and, after linking, will result in the kernel module <module_name>.ko. 173The above line can be put in either a "Kbuild" file or a "Makefile." 174When the module is built from multiple sources, an additional line is 175needed listing the files:: 176 177 <module_name>-y := <src1>.o <src2>.o ... 178 179NOTE: Further documentation describing the syntax used by kbuild is 180located in Documentation/kbuild/makefiles.rst. 181 182The examples below demonstrate how to create a build file for the 183module 8123.ko, which is built from the following files:: 184 185 8123_if.c 186 8123_if.h 187 8123_pci.c 188 8123_bin.o_shipped <= Binary blob 189 1903.1 Shared Makefile 191------------------- 192 193 An external module always includes a wrapper makefile that 194 supports building the module using "make" with no arguments. 195 This target is not used by kbuild; it is only for convenience. 196 Additional functionality, such as test targets, can be included 197 but should be filtered out from kbuild due to possible name 198 clashes. 199 200 Example 1:: 201 202 --> filename: Makefile 203 ifneq ($(KERNELRELEASE),) 204 # kbuild part of makefile 205 obj-m := 8123.o 206 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 207 208 else 209 # normal makefile 210 KDIR ?= /lib/modules/`uname -r`/build 211 212 default: 213 $(MAKE) -C $(KDIR) M=$$PWD 214 215 # Module specific targets 216 genbin: 217 echo "X" > 8123_bin.o_shipped 218 219 endif 220 221 The check for KERNELRELEASE is used to separate the two parts 222 of the makefile. In the example, kbuild will only see the two 223 assignments, whereas "make" will see everything except these 224 two assignments. This is due to two passes made on the file: 225 the first pass is by the "make" instance run on the command 226 line; the second pass is by the kbuild system, which is 227 initiated by the parameterized "make" in the default target. 228 2293.2 Separate Kbuild File and Makefile 230------------------------------------- 231 232 In newer versions of the kernel, kbuild will first look for a 233 file named "Kbuild," and only if that is not found, will it 234 then look for a makefile. Utilizing a "Kbuild" file allows us 235 to split up the makefile from example 1 into two files: 236 237 Example 2:: 238 239 --> filename: Kbuild 240 obj-m := 8123.o 241 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 242 243 --> filename: Makefile 244 KDIR ?= /lib/modules/`uname -r`/build 245 246 default: 247 $(MAKE) -C $(KDIR) M=$$PWD 248 249 # Module specific targets 250 genbin: 251 echo "X" > 8123_bin.o_shipped 252 253 The split in example 2 is questionable due to the simplicity of 254 each file; however, some external modules use makefiles 255 consisting of several hundred lines, and here it really pays 256 off to separate the kbuild part from the rest. 257 258 The next example shows a backward compatible version. 259 260 Example 3:: 261 262 --> filename: Kbuild 263 obj-m := 8123.o 264 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 265 266 --> filename: Makefile 267 ifneq ($(KERNELRELEASE),) 268 # kbuild part of makefile 269 include Kbuild 270 271 else 272 # normal makefile 273 KDIR ?= /lib/modules/`uname -r`/build 274 275 default: 276 $(MAKE) -C $(KDIR) M=$$PWD 277 278 # Module specific targets 279 genbin: 280 echo "X" > 8123_bin.o_shipped 281 282 endif 283 284 Here the "Kbuild" file is included from the makefile. This 285 allows an older version of kbuild, which only knows of 286 makefiles, to be used when the "make" and kbuild parts are 287 split into separate files. 288 2893.3 Binary Blobs 290---------------- 291 292 Some external modules need to include an object file as a blob. 293 kbuild has support for this, but requires the blob file to be 294 named <filename>_shipped. When the kbuild rules kick in, a copy 295 of <filename>_shipped is created with _shipped stripped off, 296 giving us <filename>. This shortened filename can be used in 297 the assignment to the module. 298 299 Throughout this section, 8123_bin.o_shipped has been used to 300 build the kernel module 8123.ko; it has been included as 301 8123_bin.o:: 302 303 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 304 305 Although there is no distinction between the ordinary source 306 files and the binary file, kbuild will pick up different rules 307 when creating the object file for the module. 308 3093.4 Building Multiple Modules 310============================= 311 312 kbuild supports building multiple modules with a single build 313 file. For example, if you wanted to build two modules, foo.ko 314 and bar.ko, the kbuild lines would be:: 315 316 obj-m := foo.o bar.o 317 foo-y := <foo_srcs> 318 bar-y := <bar_srcs> 319 320 It is that simple! 321 322 3234. Include Files 324================ 325 326Within the kernel, header files are kept in standard locations 327according to the following rule: 328 329 * If the header file only describes the internal interface of a 330 module, then the file is placed in the same directory as the 331 source files. 332 * If the header file describes an interface used by other parts 333 of the kernel that are located in different directories, then 334 the file is placed in include/linux/. 335 336 NOTE: 337 There are two notable exceptions to this rule: larger 338 subsystems have their own directory under include/, such as 339 include/scsi; and architecture specific headers are located 340 under arch/$(SRCARCH)/include/. 341 3424.1 Kernel Includes 343------------------- 344 345 To include a header file located under include/linux/, simply 346 use:: 347 348 #include <linux/module.h> 349 350 kbuild will add options to "gcc" so the relevant directories 351 are searched. 352 3534.2 Single Subdirectory 354----------------------- 355 356 External modules tend to place header files in a separate 357 include/ directory where their source is located, although this 358 is not the usual kernel style. To inform kbuild of the 359 directory, use either ccflags-y or CFLAGS_<filename>.o. 360 361 Using the example from section 3, if we moved 8123_if.h to a 362 subdirectory named include, the resulting kbuild file would 363 look like:: 364 365 --> filename: Kbuild 366 obj-m := 8123.o 367 368 ccflags-y := -Iinclude 369 8123-y := 8123_if.o 8123_pci.o 8123_bin.o 370 371 Note that in the assignment there is no space between -I and 372 the path. This is a limitation of kbuild: there must be no 373 space present. 374 3754.3 Several Subdirectories 376-------------------------- 377 378 kbuild can handle files that are spread over several directories. 379 Consider the following example:: 380 381 . 382 |__ src 383 | |__ complex_main.c 384 | |__ hal 385 | |__ hardwareif.c 386 | |__ include 387 | |__ hardwareif.h 388 |__ include 389 |__ complex.h 390 391 To build the module complex.ko, we then need the following 392 kbuild file:: 393 394 --> filename: Kbuild 395 obj-m := complex.o 396 complex-y := src/complex_main.o 397 complex-y += src/hal/hardwareif.o 398 399 ccflags-y := -I$(src)/include 400 ccflags-y += -I$(src)/src/hal/include 401 402 As you can see, kbuild knows how to handle object files located 403 in other directories. The trick is to specify the directory 404 relative to the kbuild file's location. That being said, this 405 is NOT recommended practice. 406 407 For the header files, kbuild must be explicitly told where to 408 look. When kbuild executes, the current directory is always the 409 root of the kernel tree (the argument to "-C") and therefore an 410 absolute path is needed. $(src) provides the absolute path by 411 pointing to the directory where the currently executing kbuild 412 file is located. 413 4144.4 UAPI Headers Installation 415----------------------------- 416 417 External modules may export headers to userspace in a similar 418 fashion to the in-tree counterpart drivers. kbuild supports 419 running headers_install target in an out-of-tree. The location 420 where kbuild searches for headers is $(M)/include/uapi and 421 $(M)/arch/$(SRCARCH)/include/uapi. 422 423 See also Documentation/kbuild/headers_install.rst. 424 425 4265. Module Installation 427====================== 428 429Modules which are included in the kernel are installed in the 430directory: 431 432 /lib/modules/$(KERNELRELEASE)/kernel/ 433 434And external modules are installed in: 435 436 /lib/modules/$(KERNELRELEASE)/extra/ 437 4385.1 INSTALL_MOD_PATH 439-------------------- 440 441 Above are the default directories but as always some level of 442 customization is possible. A prefix can be added to the 443 installation path using the variable INSTALL_MOD_PATH:: 444 445 $ make INSTALL_MOD_PATH=/frodo modules_install 446 => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/ 447 448 INSTALL_MOD_PATH may be set as an ordinary shell variable or, 449 as shown above, can be specified on the command line when 450 calling "make." This has effect when installing both in-tree 451 and out-of-tree modules. 452 4535.2 INSTALL_MOD_DIR 454------------------- 455 456 External modules are by default installed to a directory under 457 /lib/modules/$(KERNELRELEASE)/extra/, but you may wish to 458 locate modules for a specific functionality in a separate 459 directory. For this purpose, use INSTALL_MOD_DIR to specify an 460 alternative name to "extra.":: 461 462 $ make INSTALL_MOD_DIR=gandalf -C $KDIR \ 463 M=$PWD modules_install 464 => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/ 465 466 4676. Module Versioning 468==================== 469 470Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used 471as a simple ABI consistency check. A CRC value of the full prototype 472for an exported symbol is created. When a module is loaded/used, the 473CRC values contained in the kernel are compared with similar values in 474the module; if they are not equal, the kernel refuses to load the 475module. 476 477Module.symvers contains a list of all exported symbols from a kernel 478build. 479 4806.1 Symbols From the Kernel (vmlinux + modules) 481----------------------------------------------- 482 483 During a kernel build, a file named Module.symvers will be 484 generated. Module.symvers contains all exported symbols from 485 the kernel and compiled modules. For each symbol, the 486 corresponding CRC value is also stored. 487 488 The syntax of the Module.symvers file is:: 489 490 <CRC> <Symbol> <Module> <Export Type> <Namespace> 491 492 0xe1cc2a05 usb_stor_suspend drivers/usb/storage/usb-storage EXPORT_SYMBOL_GPL USB_STORAGE 493 494 The fields are separated by tabs and values may be empty (e.g. 495 if no namespace is defined for an exported symbol). 496 497 For a kernel build without CONFIG_MODVERSIONS enabled, the CRC 498 would read 0x00000000. 499 500 Module.symvers serves two purposes: 501 502 1) It lists all exported symbols from vmlinux and all modules. 503 2) It lists the CRC if CONFIG_MODVERSIONS is enabled. 504 5056.2 Symbols and External Modules 506-------------------------------- 507 508 When building an external module, the build system needs access 509 to the symbols from the kernel to check if all external symbols 510 are defined. This is done in the MODPOST step. modpost obtains 511 the symbols by reading Module.symvers from the kernel source 512 tree. During the MODPOST step, a new Module.symvers file will be 513 written containing all exported symbols from that external module. 514 5156.3 Symbols From Another External Module 516---------------------------------------- 517 518 Sometimes, an external module uses exported symbols from 519 another external module. Kbuild needs to have full knowledge of 520 all symbols to avoid spitting out warnings about undefined 521 symbols. Two solutions exist for this situation. 522 523 NOTE: The method with a top-level kbuild file is recommended 524 but may be impractical in certain situations. 525 526 Use a top-level kbuild file 527 If you have two modules, foo.ko and bar.ko, where 528 foo.ko needs symbols from bar.ko, you can use a 529 common top-level kbuild file so both modules are 530 compiled in the same build. Consider the following 531 directory layout:: 532 533 ./foo/ <= contains foo.ko 534 ./bar/ <= contains bar.ko 535 536 The top-level kbuild file would then look like:: 537 538 #./Kbuild (or ./Makefile): 539 obj-m := foo/ bar/ 540 541 And executing:: 542 543 $ make -C $KDIR M=$PWD 544 545 will then do the expected and compile both modules with 546 full knowledge of symbols from either module. 547 548 Use "make" variable KBUILD_EXTRA_SYMBOLS 549 If it is impractical to add a top-level kbuild file, 550 you can assign a space separated list 551 of files to KBUILD_EXTRA_SYMBOLS in your build file. 552 These files will be loaded by modpost during the 553 initialization of its symbol tables. 554 555 5567. Tips & Tricks 557================ 558 5597.1 Testing for CONFIG_FOO_BAR 560------------------------------ 561 562 Modules often need to check for certain `CONFIG_` options to 563 decide if a specific feature is included in the module. In 564 kbuild this is done by referencing the `CONFIG_` variable 565 directly:: 566 567 #fs/ext2/Makefile 568 obj-$(CONFIG_EXT2_FS) += ext2.o 569 570 ext2-y := balloc.o bitmap.o dir.o 571 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o 572 573 External modules have traditionally used "grep" to check for 574 specific `CONFIG_` settings directly in .config. This usage is 575 broken. As introduced before, external modules should use 576 kbuild for building and can therefore use the same methods as 577 in-tree modules when testing for `CONFIG_` definitions. 578