Back to home page

OSCL-LXR

 
 

    


0001 =========================
0002 Building External Modules
0003 =========================
0004 
0005 This document describes how to build an out-of-tree kernel module.
0006 
0007 .. Table of Contents
0008 
0009         === 1 Introduction
0010         === 2 How to Build External Modules
0011            --- 2.1 Command Syntax
0012            --- 2.2 Options
0013            --- 2.3 Targets
0014            --- 2.4 Building Separate Files
0015         === 3. Creating a Kbuild File for an External Module
0016            --- 3.1 Shared Makefile
0017            --- 3.2 Separate Kbuild file and Makefile
0018            --- 3.3 Binary Blobs
0019            --- 3.4 Building Multiple Modules
0020         === 4. Include Files
0021            --- 4.1 Kernel Includes
0022            --- 4.2 Single Subdirectory
0023            --- 4.3 Several Subdirectories
0024         === 5. Module Installation
0025            --- 5.1 INSTALL_MOD_PATH
0026            --- 5.2 INSTALL_MOD_DIR
0027         === 6. Module Versioning
0028            --- 6.1 Symbols From the Kernel (vmlinux + modules)
0029            --- 6.2 Symbols and External Modules
0030            --- 6.3 Symbols From Another External Module
0031         === 7. Tips & Tricks
0032            --- 7.1 Testing for CONFIG_FOO_BAR
0033 
0034 
0035 
0036 1. Introduction
0037 ===============
0038 
0039 "kbuild" is the build system used by the Linux kernel. Modules must use
0040 kbuild to stay compatible with changes in the build infrastructure and
0041 to pick up the right flags to "gcc." Functionality for building modules
0042 both in-tree and out-of-tree is provided. The method for building
0043 either is similar, and all modules are initially developed and built
0044 out-of-tree.
0045 
0046 Covered in this document is information aimed at developers interested
0047 in building out-of-tree (or "external") modules. The author of an
0048 external module should supply a makefile that hides most of the
0049 complexity, so one only has to type "make" to build the module. This is
0050 easily accomplished, and a complete example will be presented in
0051 section 3.
0052 
0053 
0054 2. How to Build External Modules
0055 ================================
0056 
0057 To build external modules, you must have a prebuilt kernel available
0058 that contains the configuration and header files used in the build.
0059 Also, the kernel must have been built with modules enabled. If you are
0060 using a distribution kernel, there will be a package for the kernel you
0061 are running provided by your distribution.
0062 
0063 An alternative is to use the "make" target "modules_prepare." This will
0064 make sure the kernel contains the information required. The target
0065 exists solely as a simple way to prepare a kernel source tree for
0066 building external modules.
0067 
0068 NOTE: "modules_prepare" will not build Module.symvers even if
0069 CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be
0070 executed to make module versioning work.
0071 
0072 2.1 Command Syntax
0073 ==================
0074 
0075         The command to build an external module is::
0076 
0077                 $ make -C <path_to_kernel_src> M=$PWD
0078 
0079         The kbuild system knows that an external module is being built
0080         due to the "M=<dir>" option given in the command.
0081 
0082         To build against the running kernel use::
0083 
0084                 $ make -C /lib/modules/`uname -r`/build M=$PWD
0085 
0086         Then to install the module(s) just built, add the target
0087         "modules_install" to the command::
0088 
0089                 $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install
0090 
0091 2.2 Options
0092 ===========
0093 
0094         ($KDIR refers to the path of the kernel source directory.)
0095 
0096         make -C $KDIR M=$PWD
0097 
0098         -C $KDIR
0099                 The directory where the kernel source is located.
0100                 "make" will actually change to the specified directory
0101                 when executing and will change back when finished.
0102 
0103         M=$PWD
0104                 Informs kbuild that an external module is being built.
0105                 The value given to "M" is the absolute path of the
0106                 directory where the external module (kbuild file) is
0107                 located.
0108 
0109 2.3 Targets
0110 ===========
0111 
0112         When building an external module, only a subset of the "make"
0113         targets are available.
0114 
0115         make -C $KDIR M=$PWD [target]
0116 
0117         The default will build the module(s) located in the current
0118         directory, so a target does not need to be specified. All
0119         output files will also be generated in this directory. No
0120         attempts are made to update the kernel source, and it is a
0121         precondition that a successful "make" has been executed for the
0122         kernel.
0123 
0124         modules
0125                 The default target for external modules. It has the
0126                 same functionality as if no target was specified. See
0127                 description above.
0128 
0129         modules_install
0130                 Install the external module(s). The default location is
0131                 /lib/modules/<kernel_release>/extra/, but a prefix may
0132                 be added with INSTALL_MOD_PATH (discussed in section 5).
0133 
0134         clean
0135                 Remove all generated files in the module directory only.
0136 
0137         help
0138                 List the available targets for external modules.
0139 
0140 2.4 Building Separate Files
0141 ===========================
0142 
0143         It is possible to build single files that are part of a module.
0144         This works equally well for the kernel, a module, and even for
0145         external modules.
0146 
0147         Example (The module foo.ko, consist of bar.o and baz.o)::
0148 
0149                 make -C $KDIR M=$PWD bar.lst
0150                 make -C $KDIR M=$PWD baz.o
0151                 make -C $KDIR M=$PWD foo.ko
0152                 make -C $KDIR M=$PWD ./
0153 
0154 
0155 3. Creating a Kbuild File for an External Module
0156 ================================================
0157 
0158 In the last section we saw the command to build a module for the
0159 running kernel. The module is not actually built, however, because a
0160 build file is required. Contained in this file will be the name of
0161 the module(s) being built, along with the list of requisite source
0162 files. The file may be as simple as a single line::
0163 
0164         obj-m := <module_name>.o
0165 
0166 The kbuild system will build <module_name>.o from <module_name>.c,
0167 and, after linking, will result in the kernel module <module_name>.ko.
0168 The above line can be put in either a "Kbuild" file or a "Makefile."
0169 When the module is built from multiple sources, an additional line is
0170 needed listing the files::
0171 
0172         <module_name>-y := <src1>.o <src2>.o ...
0173 
0174 NOTE: Further documentation describing the syntax used by kbuild is
0175 located in Documentation/kbuild/makefiles.rst.
0176 
0177 The examples below demonstrate how to create a build file for the
0178 module 8123.ko, which is built from the following files::
0179 
0180         8123_if.c
0181         8123_if.h
0182         8123_pci.c
0183         8123_bin.o_shipped      <= Binary blob
0184 
0185 3.1 Shared Makefile
0186 -------------------
0187 
0188         An external module always includes a wrapper makefile that
0189         supports building the module using "make" with no arguments.
0190         This target is not used by kbuild; it is only for convenience.
0191         Additional functionality, such as test targets, can be included
0192         but should be filtered out from kbuild due to possible name
0193         clashes.
0194 
0195         Example 1::
0196 
0197                 --> filename: Makefile
0198                 ifneq ($(KERNELRELEASE),)
0199                 # kbuild part of makefile
0200                 obj-m  := 8123.o
0201                 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
0202 
0203                 else
0204                 # normal makefile
0205                 KDIR ?= /lib/modules/`uname -r`/build
0206 
0207                 default:
0208                         $(MAKE) -C $(KDIR) M=$$PWD
0209 
0210                 # Module specific targets
0211                 genbin:
0212                         echo "X" > 8123_bin.o_shipped
0213 
0214                 endif
0215 
0216         The check for KERNELRELEASE is used to separate the two parts
0217         of the makefile. In the example, kbuild will only see the two
0218         assignments, whereas "make" will see everything except these
0219         two assignments. This is due to two passes made on the file:
0220         the first pass is by the "make" instance run on the command
0221         line; the second pass is by the kbuild system, which is
0222         initiated by the parameterized "make" in the default target.
0223 
0224 3.2 Separate Kbuild File and Makefile
0225 -------------------------------------
0226 
0227         In newer versions of the kernel, kbuild will first look for a
0228         file named "Kbuild," and only if that is not found, will it
0229         then look for a makefile. Utilizing a "Kbuild" file allows us
0230         to split up the makefile from example 1 into two files:
0231 
0232         Example 2::
0233 
0234                 --> filename: Kbuild
0235                 obj-m  := 8123.o
0236                 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
0237 
0238                 --> filename: Makefile
0239                 KDIR ?= /lib/modules/`uname -r`/build
0240 
0241                 default:
0242                         $(MAKE) -C $(KDIR) M=$$PWD
0243 
0244                 # Module specific targets
0245                 genbin:
0246                         echo "X" > 8123_bin.o_shipped
0247 
0248         The split in example 2 is questionable due to the simplicity of
0249         each file; however, some external modules use makefiles
0250         consisting of several hundred lines, and here it really pays
0251         off to separate the kbuild part from the rest.
0252 
0253         The next example shows a backward compatible version.
0254 
0255         Example 3::
0256 
0257                 --> filename: Kbuild
0258                 obj-m  := 8123.o
0259                 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
0260 
0261                 --> filename: Makefile
0262                 ifneq ($(KERNELRELEASE),)
0263                 # kbuild part of makefile
0264                 include Kbuild
0265 
0266                 else
0267                 # normal makefile
0268                 KDIR ?= /lib/modules/`uname -r`/build
0269 
0270                 default:
0271                         $(MAKE) -C $(KDIR) M=$$PWD
0272 
0273                 # Module specific targets
0274                 genbin:
0275                         echo "X" > 8123_bin.o_shipped
0276 
0277                 endif
0278 
0279         Here the "Kbuild" file is included from the makefile. This
0280         allows an older version of kbuild, which only knows of
0281         makefiles, to be used when the "make" and kbuild parts are
0282         split into separate files.
0283 
0284 3.3 Binary Blobs
0285 ----------------
0286 
0287         Some external modules need to include an object file as a blob.
0288         kbuild has support for this, but requires the blob file to be
0289         named <filename>_shipped. When the kbuild rules kick in, a copy
0290         of <filename>_shipped is created with _shipped stripped off,
0291         giving us <filename>. This shortened filename can be used in
0292         the assignment to the module.
0293 
0294         Throughout this section, 8123_bin.o_shipped has been used to
0295         build the kernel module 8123.ko; it has been included as
0296         8123_bin.o::
0297 
0298                 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
0299 
0300         Although there is no distinction between the ordinary source
0301         files and the binary file, kbuild will pick up different rules
0302         when creating the object file for the module.
0303 
0304 3.4 Building Multiple Modules
0305 =============================
0306 
0307         kbuild supports building multiple modules with a single build
0308         file. For example, if you wanted to build two modules, foo.ko
0309         and bar.ko, the kbuild lines would be::
0310 
0311                 obj-m := foo.o bar.o
0312                 foo-y := <foo_srcs>
0313                 bar-y := <bar_srcs>
0314 
0315         It is that simple!
0316 
0317 
0318 4. Include Files
0319 ================
0320 
0321 Within the kernel, header files are kept in standard locations
0322 according to the following rule:
0323 
0324         * If the header file only describes the internal interface of a
0325           module, then the file is placed in the same directory as the
0326           source files.
0327         * If the header file describes an interface used by other parts
0328           of the kernel that are located in different directories, then
0329           the file is placed in include/linux/.
0330 
0331           NOTE:
0332               There are two notable exceptions to this rule: larger
0333               subsystems have their own directory under include/, such as
0334               include/scsi; and architecture specific headers are located
0335               under arch/$(SRCARCH)/include/.
0336 
0337 4.1 Kernel Includes
0338 -------------------
0339 
0340         To include a header file located under include/linux/, simply
0341         use::
0342 
0343                 #include <linux/module.h>
0344 
0345         kbuild will add options to "gcc" so the relevant directories
0346         are searched.
0347 
0348 4.2 Single Subdirectory
0349 -----------------------
0350 
0351         External modules tend to place header files in a separate
0352         include/ directory where their source is located, although this
0353         is not the usual kernel style. To inform kbuild of the
0354         directory, use either ccflags-y or CFLAGS_<filename>.o.
0355 
0356         Using the example from section 3, if we moved 8123_if.h to a
0357         subdirectory named include, the resulting kbuild file would
0358         look like::
0359 
0360                 --> filename: Kbuild
0361                 obj-m := 8123.o
0362 
0363                 ccflags-y := -Iinclude
0364                 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
0365 
0366         Note that in the assignment there is no space between -I and
0367         the path. This is a limitation of kbuild: there must be no
0368         space present.
0369 
0370 4.3 Several Subdirectories
0371 --------------------------
0372 
0373         kbuild can handle files that are spread over several directories.
0374         Consider the following example::
0375 
0376                 .
0377                 |__ src
0378                 |   |__ complex_main.c
0379                 |   |__ hal
0380                 |       |__ hardwareif.c
0381                 |       |__ include
0382                 |           |__ hardwareif.h
0383                 |__ include
0384                 |__ complex.h
0385 
0386         To build the module complex.ko, we then need the following
0387         kbuild file::
0388 
0389                 --> filename: Kbuild
0390                 obj-m := complex.o
0391                 complex-y := src/complex_main.o
0392                 complex-y += src/hal/hardwareif.o
0393 
0394                 ccflags-y := -I$(src)/include
0395                 ccflags-y += -I$(src)/src/hal/include
0396 
0397         As you can see, kbuild knows how to handle object files located
0398         in other directories. The trick is to specify the directory
0399         relative to the kbuild file's location. That being said, this
0400         is NOT recommended practice.
0401 
0402         For the header files, kbuild must be explicitly told where to
0403         look. When kbuild executes, the current directory is always the
0404         root of the kernel tree (the argument to "-C") and therefore an
0405         absolute path is needed. $(src) provides the absolute path by
0406         pointing to the directory where the currently executing kbuild
0407         file is located.
0408 
0409 
0410 5. Module Installation
0411 ======================
0412 
0413 Modules which are included in the kernel are installed in the
0414 directory:
0415 
0416         /lib/modules/$(KERNELRELEASE)/kernel/
0417 
0418 And external modules are installed in:
0419 
0420         /lib/modules/$(KERNELRELEASE)/extra/
0421 
0422 5.1 INSTALL_MOD_PATH
0423 --------------------
0424 
0425         Above are the default directories but as always some level of
0426         customization is possible. A prefix can be added to the
0427         installation path using the variable INSTALL_MOD_PATH::
0428 
0429                 $ make INSTALL_MOD_PATH=/frodo modules_install
0430                 => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/
0431 
0432         INSTALL_MOD_PATH may be set as an ordinary shell variable or,
0433         as shown above, can be specified on the command line when
0434         calling "make." This has effect when installing both in-tree
0435         and out-of-tree modules.
0436 
0437 5.2 INSTALL_MOD_DIR
0438 -------------------
0439 
0440         External modules are by default installed to a directory under
0441         /lib/modules/$(KERNELRELEASE)/extra/, but you may wish to
0442         locate modules for a specific functionality in a separate
0443         directory. For this purpose, use INSTALL_MOD_DIR to specify an
0444         alternative name to "extra."::
0445 
0446                 $ make INSTALL_MOD_DIR=gandalf -C $KDIR \
0447                        M=$PWD modules_install
0448                 => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/
0449 
0450 
0451 6. Module Versioning
0452 ====================
0453 
0454 Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used
0455 as a simple ABI consistency check. A CRC value of the full prototype
0456 for an exported symbol is created. When a module is loaded/used, the
0457 CRC values contained in the kernel are compared with similar values in
0458 the module; if they are not equal, the kernel refuses to load the
0459 module.
0460 
0461 Module.symvers contains a list of all exported symbols from a kernel
0462 build.
0463 
0464 6.1 Symbols From the Kernel (vmlinux + modules)
0465 -----------------------------------------------
0466 
0467         During a kernel build, a file named Module.symvers will be
0468         generated. Module.symvers contains all exported symbols from
0469         the kernel and compiled modules. For each symbol, the
0470         corresponding CRC value is also stored.
0471 
0472         The syntax of the Module.symvers file is::
0473 
0474                 <CRC>       <Symbol>         <Module>                         <Export Type>     <Namespace>
0475 
0476                 0xe1cc2a05  usb_stor_suspend drivers/usb/storage/usb-storage  EXPORT_SYMBOL_GPL USB_STORAGE
0477 
0478         The fields are separated by tabs and values may be empty (e.g.
0479         if no namespace is defined for an exported symbol).
0480 
0481         For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
0482         would read 0x00000000.
0483 
0484         Module.symvers serves two purposes:
0485 
0486         1) It lists all exported symbols from vmlinux and all modules.
0487         2) It lists the CRC if CONFIG_MODVERSIONS is enabled.
0488 
0489 6.2 Symbols and External Modules
0490 --------------------------------
0491 
0492         When building an external module, the build system needs access
0493         to the symbols from the kernel to check if all external symbols
0494         are defined. This is done in the MODPOST step. modpost obtains
0495         the symbols by reading Module.symvers from the kernel source
0496         tree. During the MODPOST step, a new Module.symvers file will be
0497         written containing all exported symbols from that external module.
0498 
0499 6.3 Symbols From Another External Module
0500 ----------------------------------------
0501 
0502         Sometimes, an external module uses exported symbols from
0503         another external module. Kbuild needs to have full knowledge of
0504         all symbols to avoid spitting out warnings about undefined
0505         symbols. Two solutions exist for this situation.
0506 
0507         NOTE: The method with a top-level kbuild file is recommended
0508         but may be impractical in certain situations.
0509 
0510         Use a top-level kbuild file
0511                 If you have two modules, foo.ko and bar.ko, where
0512                 foo.ko needs symbols from bar.ko, you can use a
0513                 common top-level kbuild file so both modules are
0514                 compiled in the same build. Consider the following
0515                 directory layout::
0516 
0517                         ./foo/ <= contains foo.ko
0518                         ./bar/ <= contains bar.ko
0519 
0520                 The top-level kbuild file would then look like::
0521 
0522                         #./Kbuild (or ./Makefile):
0523                                 obj-m := foo/ bar/
0524 
0525                 And executing::
0526 
0527                         $ make -C $KDIR M=$PWD
0528 
0529                 will then do the expected and compile both modules with
0530                 full knowledge of symbols from either module.
0531 
0532         Use "make" variable KBUILD_EXTRA_SYMBOLS
0533                 If it is impractical to add a top-level kbuild file,
0534                 you can assign a space separated list
0535                 of files to KBUILD_EXTRA_SYMBOLS in your build file.
0536                 These files will be loaded by modpost during the
0537                 initialization of its symbol tables.
0538 
0539 
0540 7. Tips & Tricks
0541 ================
0542 
0543 7.1 Testing for CONFIG_FOO_BAR
0544 ------------------------------
0545 
0546         Modules often need to check for certain `CONFIG_` options to
0547         decide if a specific feature is included in the module. In
0548         kbuild this is done by referencing the `CONFIG_` variable
0549         directly::
0550 
0551                 #fs/ext2/Makefile
0552                 obj-$(CONFIG_EXT2_FS) += ext2.o
0553 
0554                 ext2-y := balloc.o bitmap.o dir.o
0555                 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o
0556 
0557         External modules have traditionally used "grep" to check for
0558         specific `CONFIG_` settings directly in .config. This usage is
0559         broken. As introduced before, external modules should use
0560         kbuild for building and can therefore use the same methods as
0561         in-tree modules when testing for `CONFIG_` definitions.