matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 1 | Building External Modules |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 3 | This document describes how-to build an out-of-tree kernel module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | === Table of Contents |
| 6 | |
| 7 | === 1 Introduction |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 8 | === 2 How-to Build External Modules |
| 9 | --- 2.1 Command Syntax |
| 10 | --- 2.2 Options |
| 11 | --- 2.3 Targets |
| 12 | --- 2.4 Building Separate Files |
| 13 | === 3. Creating a Kbuild File for an External Module |
| 14 | --- 3.1 Shared Makefile |
| 15 | --- 3.2 Separate Kbuild file and Makefile |
| 16 | --- 3.3 Binary Blobs |
| 17 | --- 3.4 Building Multiple Modules |
| 18 | === 4. Include files |
| 19 | --- 4.1 How to include files from the kernel include dir |
| 20 | --- 4.2 External modules using an include/ dir |
| 21 | --- 4.3 External modules using several directories |
| 22 | === 5. Module installation |
| 23 | --- 5.1 INSTALL_MOD_PATH |
| 24 | --- 5.2 INSTALL_MOD_DIR |
| 25 | === 6. Module versioning & Module.symvers |
| 26 | --- 6.1 Symbols from the kernel (vmlinux + modules) |
| 27 | --- 6.2 Symbols and external modules |
| 28 | --- 6.3 Symbols from another external module |
| 29 | === 7. Tips & Tricks |
| 30 | --- 7.1 Testing for CONFIG_FOO_BAR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | |
| 34 | === 1. Introduction |
| 35 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 36 | "kbuild" is the build system used by the Linux kernel. Modules must use |
| 37 | kbuild to stay compatible with changes in the build infrastructure and |
| 38 | to pick up the right flags to "gcc." Functionality for building modules |
| 39 | both in-tree and out-of-tree is provided. The method for building |
| 40 | either is similar, and all modules are initially developed and built |
| 41 | out-of-tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 43 | Covered in this document is information aimed at developers interested |
| 44 | in building out-of-tree (or "external") modules. The author of an |
| 45 | external module should supply a makefile that hides most of the |
| 46 | complexity, so one only has to type "make" to build the module. This is |
| 47 | easily accomplished, and a complete example will be presented in |
| 48 | section 3. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 51 | === 2. How-to Build External Modules |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 53 | To build external modules, you must have a pre-built kernel available |
| 54 | that contains the configuration and header files used in the build. |
| 55 | Also, the kernel must have been built with modules enabled. If you are |
| 56 | using a distribution kernel, there will be a package for the kernel you |
| 57 | are running provided by your distribution. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 59 | An alternative is to use the "make" target "modules_prepare." This will |
| 60 | make sure the kernel contains the information required. The target |
| 61 | exists solely as a simple way to prepare a kernel source tree for |
| 62 | building external modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 64 | NOTE: "modules_prepare" will not build Module.symvers even if |
| 65 | CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be |
| 66 | executed to make module versioning work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 68 | --- 2.1 Command Syntax |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 70 | The command to build an external module is: |
Robert P. J. Day | 99c8b94 | 2006-09-25 15:55:51 -0400 | [diff] [blame] | 71 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 72 | make -C <path_to_kernel_src> M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 74 | The kbuild system knows that an external module is being built |
| 75 | due to the "M=<dir>" option given in the command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 77 | To build against the running kernel use: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 79 | make -C /lib/modules/`uname -r`/build M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 81 | Then to install the module(s) just built, add the target |
| 82 | "modules_install" to the command: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 84 | make -C /lib/modules/`uname -r`/build M=$PWD modules_install |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 86 | --- 2.2 Options |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 88 | ($KDIR refers to the path of the kernel source directory.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 90 | make -C $KDIR M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 92 | -C $KDIR |
| 93 | The directory where the kernel source is located. |
| 94 | "make" will actually change to the specified directory |
| 95 | when executing and will change back when finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 97 | M=$PWD |
| 98 | Informs kbuild that an external module is being built. |
| 99 | The value given to "M" is the absolute path of the |
| 100 | directory where the external module (kbuild file) is |
| 101 | located. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 103 | --- 2.3 Targets |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 105 | When building an external module, only a subset of the "make" |
| 106 | targets are available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 108 | make -C $KDIR M=$PWD [target] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 110 | The default will build the module(s) located in the current |
| 111 | directory, so a target does not need to be specified. All |
| 112 | output files will also be generated in this directory. No |
| 113 | attempts are made to update the kernel source, and it is a |
| 114 | precondition that a successful "make" has been executed for the |
| 115 | kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 117 | modules |
| 118 | The default target for external modules. It has the |
| 119 | same functionality as if no target was specified. See |
| 120 | description above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 122 | modules_install |
| 123 | Install the external module(s). The default location is |
| 124 | /lib/modules/<kernel_release>/extra, but a prefix may |
| 125 | be added with INSTALL_MOD_PATH (discussed in section 5). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 127 | clean |
| 128 | Remove all generated files in the module directory only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 130 | help |
| 131 | List the available targets for external modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 133 | --- 2.4 Building Separate Files |
| 134 | |
| 135 | It is possible to build single files that are part of a module. |
| 136 | This works equally well for the kernel, a module, and even for |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 137 | external modules. |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 138 | |
| 139 | Example (The module foo.ko, consist of bar.o and baz.o): |
| 140 | make -C $KDIR M=$PWD bar.lst |
| 141 | make -C $KDIR M=$PWD baz.o |
| 142 | make -C $KDIR M=$PWD foo.ko |
| 143 | make -C $KDIR M=$PWD / |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 146 | === 3. Creating a Kbuild File for an External Module |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 148 | In the last section we saw the command to build a module for the |
| 149 | running kernel. The module is not actually built, however, because a |
| 150 | build file is required. Contained in this file will be the name of |
| 151 | the module(s) being built, along with the list of requisite source |
| 152 | files. The file may be as simple as a single line: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 154 | obj-m := <module_name>.o |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 156 | The kbuild system will build <module_name>.o from <module_name>.c, |
| 157 | and, after linking, will result in the kernel module <module_name>.ko. |
| 158 | The above line can be put in either a "Kbuild" file or a "Makefile." |
| 159 | When the module is built from multiple sources, an additional line is |
| 160 | needed listing the files: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 162 | <module_name>-y := <src1>.o <src2>.o ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 164 | NOTE: Further documentation describing the syntax used by kbuild is |
| 165 | located in Documentation/kbuild/makefiles.txt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 167 | The examples below demonstrate how-to create a build file for the |
| 168 | module 8123.ko, which is built from the following files: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | 8123_if.c |
| 171 | 8123_if.h |
| 172 | 8123_pci.c |
| 173 | 8123_bin.o_shipped <= Binary blob |
| 174 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 175 | --- 3.1 Shared Makefile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 177 | An external module always includes a wrapper makefile that |
| 178 | supports building the module using "make" with no arguments. |
| 179 | This target is not used by kbuild; it is only for convenience. |
| 180 | Additional functionality, such as test targets, can be included |
| 181 | but should be filtered out from kbuild due to possible name |
| 182 | clashes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | Example 1: |
| 185 | --> filename: Makefile |
| 186 | ifneq ($(KERNELRELEASE),) |
| 187 | # kbuild part of makefile |
| 188 | obj-m := 8123.o |
| 189 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 190 | |
| 191 | else |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 192 | # normal makefile |
| 193 | KDIR ?= /lib/modules/`uname -r`/build |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 195 | default: |
| 196 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | # Module specific targets |
| 199 | genbin: |
Brian Strand | 98a1e44 | 2005-11-22 01:23:08 +0000 | [diff] [blame] | 200 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | endif |
| 203 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 204 | The check for KERNELRELEASE is used to separate the two parts |
| 205 | of the makefile. In the example, kbuild will only see the two |
| 206 | assignments, whereas "make" will see everything except these |
| 207 | two assignments. This is due to two passes made on the file: |
| 208 | the first pass is by the "make" instance run on the |
| 209 | command line; the second pass is by the kbuild system, which is |
| 210 | initiated by the parameterized "make" in the default target. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 212 | --- 3.2 Separate Kbuild File and Makefile |
| 213 | |
| 214 | In newer versions of the kernel, kbuild will first look for a |
| 215 | file named "Kbuild", and only if that is not found, will it |
| 216 | then look for a makefile. Utilizing a "Kbuild" file allows us |
| 217 | to split up the makefile from example 1 into two files: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | Example 2: |
| 220 | --> filename: Kbuild |
| 221 | obj-m := 8123.o |
| 222 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 223 | |
| 224 | --> filename: Makefile |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 225 | KDIR ?= /lib/modules/`uname -r`/build |
| 226 | |
| 227 | default: |
| 228 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | # Module specific targets |
| 231 | genbin: |
Wolfram Sang | baa9187 | 2009-01-06 15:12:27 +0100 | [diff] [blame] | 232 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 234 | The split in example 2 is questionable due to the simplicity of |
| 235 | each file; however, some external modules use makefiles |
| 236 | consisting of several hundred lines, and here it really pays |
| 237 | off to separate the kbuild part from the rest. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 239 | The next example shows a backward compatible version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | Example 3: |
| 242 | --> filename: Kbuild |
| 243 | obj-m := 8123.o |
| 244 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 245 | |
| 246 | --> filename: Makefile |
| 247 | ifneq ($(KERNELRELEASE),) |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 248 | # kbuild part of makefile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | include Kbuild |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 251 | else |
| 252 | # normal makefile |
| 253 | KDIR ?= /lib/modules/`uname -r`/build |
| 254 | |
| 255 | default: |
| 256 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | # Module specific targets |
| 259 | genbin: |
Wolfram Sang | baa9187 | 2009-01-06 15:12:27 +0100 | [diff] [blame] | 260 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | endif |
| 263 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 264 | Here the "Kbuild" file is included from the makefile. This |
| 265 | allows an older version of kbuild, which only knows of |
| 266 | makefiles, to be used when the "make" and kbuild parts are |
| 267 | split into separate files. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 269 | --- 3.3 Binary Blobs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 271 | Some external modules need to include an object file as a blob. |
| 272 | kbuild has support for this, but requires the blob file to be |
| 273 | named <filename>_shipped. When the kbuild rules kick in, a copy |
| 274 | of <filename>_shipped is created with _shipped stripped off, |
| 275 | giving us <filename>. This shortened filename can be used in |
| 276 | the assignment to the module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 278 | Throughout this section, 8123_bin.o_shipped has been used to |
| 279 | build the kernel module 8123.ko; it has been included as |
| 280 | 8123_bin.o. |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 283 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame^] | 284 | Although there is no distinction between the ordinary source |
| 285 | files and the binary file, kbuild will pick up different rules |
| 286 | when creating the object file for the module. |
| 287 | |
| 288 | --- 3.4 Building Multiple Modules |
| 289 | |
| 290 | kbuild supports building multiple modules with a single build |
| 291 | file. For example, if you want to build two modules, foo and |
| 292 | bar, the kbuild lines would be: |
| 293 | |
| 294 | obj-m := foo.o bar.o |
| 295 | foo-y := <foo_srcs> |
| 296 | bar-y := <bar_srcs> |
| 297 | |
| 298 | It is that simple! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | |
| 301 | === 5. Include files |
| 302 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 303 | Include files are a necessity when a .c file uses something from other .c |
| 304 | files (not strictly in the sense of C, but if good programming practice is |
| 305 | used). Any module that consists of more than one .c file will have a .h file |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 306 | for one of the .c files. |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 307 | |
| 308 | - If the .h file only describes a module internal interface, then the .h file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | shall be placed in the same directory as the .c files. |
| 310 | - If the .h files describe an interface used by other parts of the kernel |
| 311 | located in different directories, the .h files shall be located in |
| 312 | include/linux/ or other include/ directories as appropriate. |
| 313 | |
| 314 | One exception for this rule is larger subsystems that have their own directory |
| 315 | under include/ such as include/scsi. Another exception is arch-specific |
| 316 | .h files which are located under include/asm-$(ARCH)/*. |
| 317 | |
| 318 | External modules have a tendency to locate include files in a separate include/ |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 319 | directory and therefore need to deal with this in their kbuild file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | --- 5.1 How to include files from the kernel include dir |
| 322 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 323 | When a module needs to include a file from include/linux/, then one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | just uses: |
| 325 | |
| 326 | #include <linux/modules.h> |
| 327 | |
| 328 | kbuild will make sure to add options to gcc so the relevant |
| 329 | directories 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 | |
| 336 | --- 5.2 External modules using an include/ dir |
| 337 | |
| 338 | External modules often locate their .h files in a separate include/ |
| 339 | directory although this is not usual kernel style. When an external |
| 340 | module uses an include/ dir then kbuild needs to be told so. |
| 341 | The trick here is to use either EXTRA_CFLAGS (take effect for all .c |
| 342 | files) or CFLAGS_$F.o (take effect only for a single file). |
| 343 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 344 | In our example, if we move 8123_if.h to a subdirectory named include/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | the resulting Kbuild file would look like: |
| 346 | |
| 347 | --> filename: Kbuild |
| 348 | obj-m := 8123.o |
| 349 | |
| 350 | EXTRA_CFLAGS := -Iinclude |
| 351 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 352 | |
Brian Strand | 98a1e44 | 2005-11-22 01:23:08 +0000 | [diff] [blame] | 353 | Note that in the assignment there is no space between -I and the path. |
| 354 | This is a kbuild limitation: there must be no space present. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 356 | --- 5.3 External modules using several directories |
| 357 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 358 | If an external module does not follow the usual kernel style, but |
| 359 | decides to spread files over several directories, then kbuild can |
| 360 | handle this too. |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 361 | |
| 362 | Consider the following example: |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 363 | |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 364 | | |
| 365 | +- src/complex_main.c |
| 366 | | +- hal/hardwareif.c |
| 367 | | +- hal/include/hardwareif.h |
| 368 | +- include/complex.h |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 369 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 370 | To build a single module named complex.ko, we then need the following |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 371 | kbuild file: |
| 372 | |
| 373 | Kbuild: |
| 374 | obj-m := complex.o |
| 375 | complex-y := src/complex_main.o |
| 376 | complex-y += src/hal/hardwareif.o |
| 377 | |
| 378 | EXTRA_CFLAGS := -I$(src)/include |
| 379 | EXTRA_CFLAGS += -I$(src)src/hal/include |
| 380 | |
| 381 | |
| 382 | kbuild knows how to handle .o files located in another directory - |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 383 | although this is NOT recommended practice. The syntax is to specify |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 384 | the directory relative to the directory where the Kbuild file is |
| 385 | located. |
| 386 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 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 |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | === 6. Module installation |
| 397 | |
Brian Strand | 98a1e44 | 2005-11-22 01:23:08 +0000 | [diff] [blame] | 398 | Modules which are included in the kernel are installed in the directory: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /lib/modules/$(KERNELRELEASE)/kernel |
| 401 | |
| 402 | External modules are installed in the directory: |
| 403 | |
| 404 | /lib/modules/$(KERNELRELEASE)/extra |
| 405 | |
| 406 | --- 6.1 INSTALL_MOD_PATH |
| 407 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 408 | Above are the default directories, but as always, some level of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | customization is possible. One can prefix the path using the variable |
| 410 | INSTALL_MOD_PATH: |
| 411 | |
| 412 | $ make INSTALL_MOD_PATH=/frodo modules_install |
| 413 | => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel |
| 414 | |
| 415 | INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 416 | example above, can be specified on the command line when calling make. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | INSTALL_MOD_PATH has effect both when installing modules included in |
| 418 | the kernel as well as when installing external modules. |
| 419 | |
| 420 | --- 6.2 INSTALL_MOD_DIR |
| 421 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 422 | When installing external modules they are by default installed to a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish |
| 424 | to locate modules for a specific functionality in a separate |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 425 | directory. For this purpose, one can use INSTALL_MOD_DIR to specify an |
| 426 | alternative name to 'extra'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \ |
| 429 | M=`pwd` modules_install |
| 430 | => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf |
| 431 | |
| 432 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 433 | === 7. Module versioning & Module.symvers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Brian Strand | 98a1e44 | 2005-11-22 01:23:08 +0000 | [diff] [blame] | 435 | Module versioning is enabled by the CONFIG_MODVERSIONS tag. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | Module versioning is used as a simple ABI consistency check. The Module |
| 438 | versioning creates a CRC value of the full prototype for an exported symbol and |
| 439 | when a module is loaded/used then the CRC values contained in the kernel are |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 440 | compared with similar values in the module. If they are not equal, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | kernel refuses to load the module. |
| 442 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 443 | Module.symvers contains a list of all exported symbols from a kernel build. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Robert P. J. Day | 99c8b94 | 2006-09-25 15:55:51 -0400 | [diff] [blame] | 445 | --- 7.1 Symbols from the kernel (vmlinux + modules) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 446 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 447 | During a kernel build, a file named Module.symvers will be generated. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 448 | Module.symvers contains all exported symbols from the kernel and |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 449 | compiled modules. For each symbols, the corresponding CRC value |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 450 | is stored too. |
| 451 | |
| 452 | The syntax of the Module.symvers file is: |
| 453 | <CRC> <Symbol> <module> |
| 454 | Sample: |
| 455 | 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod |
| 456 | |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 457 | For a kernel build without CONFIG_MODVERSIONS enabled, the crc |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 458 | would read: 0x00000000 |
| 459 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 460 | Module.symvers serves two purposes: |
| 461 | 1) It lists all exported symbols both from vmlinux and all modules |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 462 | 2) It lists the CRC if CONFIG_MODVERSIONS is enabled |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 463 | |
| 464 | --- 7.2 Symbols and external modules |
| 465 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 466 | When building an external module, the build system needs access to |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 467 | the symbols from the kernel to check if all external symbols are |
| 468 | defined. This is done in the MODPOST step and to obtain all |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 469 | symbols, modpost reads Module.symvers from the kernel. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 470 | If a Module.symvers file is present in the directory where |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 471 | the external module is being built, this file will be read too. |
| 472 | During the MODPOST step, a new Module.symvers file will be written |
| 473 | containing all exported symbols that were not defined in the kernel. |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 474 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 475 | --- 7.3 Symbols from another external module |
| 476 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 477 | Sometimes, an external module uses exported symbols from another |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 478 | external module. Kbuild needs to have full knowledge on all symbols |
| 479 | to avoid spitting out warnings about undefined symbols. |
Richard Hacker | 0d96fb2 | 2008-02-28 09:40:58 +0100 | [diff] [blame] | 480 | Three solutions exist to let kbuild know all symbols of more than |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 481 | one external module. |
| 482 | The method with a top-level kbuild file is recommended but may be |
| 483 | impractical in certain situations. |
| 484 | |
| 485 | Use a top-level Kbuild file |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 486 | If you have two modules: 'foo' and 'bar', and 'foo' needs |
| 487 | symbols from 'bar', then one can use a common top-level kbuild |
| 488 | file so both modules are compiled in same build. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 489 | |
| 490 | Consider following directory layout: |
| 491 | ./foo/ <= contains the foo module |
| 492 | ./bar/ <= contains the bar module |
| 493 | The top-level Kbuild file would then look like: |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 494 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 495 | #./Kbuild: (this file may also be named Makefile) |
| 496 | obj-y := foo/ bar/ |
| 497 | |
| 498 | Executing: |
| 499 | make -C $KDIR M=`pwd` |
| 500 | |
| 501 | will then do the expected and compile both modules with full |
| 502 | knowledge on symbols from both modules. |
| 503 | |
| 504 | Use an extra Module.symvers file |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 505 | When an external module is built, a Module.symvers file is |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 506 | generated containing all exported symbols which are not |
| 507 | defined in the kernel. |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 508 | To get access to symbols from module 'bar', one can copy the |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 509 | Module.symvers file from the compilation of the 'bar' module |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 510 | to the directory where the 'foo' module is built. |
| 511 | During the module build, kbuild will read the Module.symvers |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 512 | file in the directory of the external module and when the |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 513 | build is finished, a new Module.symvers file is created |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 514 | containing the sum of all symbols defined and not part of the |
| 515 | kernel. |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 516 | |
Richard Hacker | 0d96fb2 | 2008-02-28 09:40:58 +0100 | [diff] [blame] | 517 | Use make variable KBUILD_EXTRA_SYMBOLS in the Makefile |
| 518 | If it is impractical to copy Module.symvers from another |
| 519 | module, you can assign a space separated list of files to |
| 520 | KBUILD_EXTRA_SYMBOLS in your Makfile. These files will be |
| 521 | loaded by modpost during the initialisation of its symbol |
| 522 | tables. |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | === 8. Tips & Tricks |
| 525 | |
| 526 | --- 8.1 Testing for CONFIG_FOO_BAR |
| 527 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 528 | Modules often need to check for certain CONFIG_ options to decide if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | a specific feature shall be included in the module. When kbuild is used |
| 530 | this is done by referencing the CONFIG_ variable directly. |
| 531 | |
| 532 | #fs/ext2/Makefile |
| 533 | obj-$(CONFIG_EXT2_FS) += ext2.o |
| 534 | |
| 535 | ext2-y := balloc.o bitmap.o dir.o |
| 536 | ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o |
| 537 | |
| 538 | External modules have traditionally used grep to check for specific |
| 539 | CONFIG_ settings directly in .config. This usage is broken. |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 540 | As introduced before, external modules shall use kbuild when building |
| 541 | and therefore can use the same methods as in-kernel modules when |
| 542 | testing for CONFIG_ definitions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |