blob: 3fb39e0116b4c8e42d40009357ed5cf13c1f2888 [file] [log] [blame]
matt mooneyefdf02c2010-09-18 18:33:57 -07001Building External Modules
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
matt mooney57932102010-10-01 21:21:55 -07003This document describes how to build an out-of-tree kernel module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5=== Table of Contents
6
7 === 1 Introduction
matt mooney57932102010-10-01 21:21:55 -07008 === 2 How to Build External Modules
matt mooneyefdf02c2010-09-18 18:33:57 -07009 --- 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
matt mooney9f021862010-09-19 23:06:36 -070018 === 4. Include Files
19 --- 4.1 Kernel Includes
20 --- 4.2 Single Subdirectory
21 --- 4.3 Several Subdirectories
22 === 5. Module Installation
matt mooneyefdf02c2010-09-18 18:33:57 -070023 --- 5.1 INSTALL_MOD_PATH
24 --- 5.2 INSTALL_MOD_DIR
matt mooney9f021862010-09-19 23:06:36 -070025 === 6. Module Versioning
26 --- 6.1 Symbols From the Kernel (vmlinux + modules)
27 --- 6.2 Symbols and External Modules
28 --- 6.3 Symbols From Another External Module
matt mooneyefdf02c2010-09-18 18:33:57 -070029 === 7. Tips & Tricks
30 --- 7.1 Testing for CONFIG_FOO_BAR
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32
33
34=== 1. Introduction
35
matt mooneyefdf02c2010-09-18 18:33:57 -070036"kbuild" is the build system used by the Linux kernel. Modules must use
37kbuild to stay compatible with changes in the build infrastructure and
38to pick up the right flags to "gcc." Functionality for building modules
39both in-tree and out-of-tree is provided. The method for building
40either is similar, and all modules are initially developed and built
41out-of-tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
matt mooneyefdf02c2010-09-18 18:33:57 -070043Covered in this document is information aimed at developers interested
44in building out-of-tree (or "external") modules. The author of an
45external module should supply a makefile that hides most of the
46complexity, so one only has to type "make" to build the module. This is
47easily accomplished, and a complete example will be presented in
48section 3.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50
matt mooney57932102010-10-01 21:21:55 -070051=== 2. How to Build External Modules
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
matt mooney57932102010-10-01 21:21:55 -070053To build external modules, you must have a prebuilt kernel available
matt mooneyefdf02c2010-09-18 18:33:57 -070054that contains the configuration and header files used in the build.
55Also, the kernel must have been built with modules enabled. If you are
56using a distribution kernel, there will be a package for the kernel you
57are running provided by your distribution.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
matt mooneyefdf02c2010-09-18 18:33:57 -070059An alternative is to use the "make" target "modules_prepare." This will
60make sure the kernel contains the information required. The target
61exists solely as a simple way to prepare a kernel source tree for
62building external modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
matt mooneyefdf02c2010-09-18 18:33:57 -070064NOTE: "modules_prepare" will not build Module.symvers even if
65CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be
66executed to make module versioning work.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
matt mooneyefdf02c2010-09-18 18:33:57 -070068--- 2.1 Command Syntax
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
matt mooneyefdf02c2010-09-18 18:33:57 -070070 The command to build an external module is:
Robert P. J. Day99c8b942006-09-25 15:55:51 -040071
matt mooney57932102010-10-01 21:21:55 -070072 $ make -C <path_to_kernel_src> M=$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
matt mooneyefdf02c2010-09-18 18:33:57 -070074 The kbuild system knows that an external module is being built
75 due to the "M=<dir>" option given in the command.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
matt mooneyefdf02c2010-09-18 18:33:57 -070077 To build against the running kernel use:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
matt mooney57932102010-10-01 21:21:55 -070079 $ make -C /lib/modules/`uname -r`/build M=$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
matt mooneyefdf02c2010-09-18 18:33:57 -070081 Then to install the module(s) just built, add the target
82 "modules_install" to the command:
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
matt mooney57932102010-10-01 21:21:55 -070084 $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
matt mooneyefdf02c2010-09-18 18:33:57 -070086--- 2.2 Options
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
matt mooneyefdf02c2010-09-18 18:33:57 -070088 ($KDIR refers to the path of the kernel source directory.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
matt mooneyefdf02c2010-09-18 18:33:57 -070090 make -C $KDIR M=$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
matt mooneyefdf02c2010-09-18 18:33:57 -070092 -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 Torvalds1da177e2005-04-16 15:20:36 -070096
matt mooneyefdf02c2010-09-18 18:33:57 -070097 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 Torvalds1da177e2005-04-16 15:20:36 -0700102
matt mooneyefdf02c2010-09-18 18:33:57 -0700103--- 2.3 Targets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
matt mooneyefdf02c2010-09-18 18:33:57 -0700105 When building an external module, only a subset of the "make"
106 targets are available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
matt mooneyefdf02c2010-09-18 18:33:57 -0700108 make -C $KDIR M=$PWD [target]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
matt mooneyefdf02c2010-09-18 18:33:57 -0700110 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 Torvalds1da177e2005-04-16 15:20:36 -0700116
matt mooneyefdf02c2010-09-18 18:33:57 -0700117 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 Torvalds1da177e2005-04-16 15:20:36 -0700121
matt mooneyefdf02c2010-09-18 18:33:57 -0700122 modules_install
123 Install the external module(s). The default location is
matt mooney57932102010-10-01 21:21:55 -0700124 /lib/modules/<kernel_release>/extra/, but a prefix may
matt mooneyefdf02c2010-09-18 18:33:57 -0700125 be added with INSTALL_MOD_PATH (discussed in section 5).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
matt mooneyefdf02c2010-09-18 18:33:57 -0700127 clean
128 Remove all generated files in the module directory only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
matt mooneyefdf02c2010-09-18 18:33:57 -0700130 help
131 List the available targets for external modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
matt mooneyefdf02c2010-09-18 18:33:57 -0700133--- 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. Day2e99f312006-09-21 09:39:41 -0400137 external modules.
matt mooneyefdf02c2010-09-18 18:33:57 -0700138
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. Day2e99f312006-09-21 09:39:41 -0400144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
matt mooneyefdf02c2010-09-18 18:33:57 -0700146=== 3. Creating a Kbuild File for an External Module
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
matt mooneyefdf02c2010-09-18 18:33:57 -0700148In the last section we saw the command to build a module for the
149running kernel. The module is not actually built, however, because a
150build file is required. Contained in this file will be the name of
151the module(s) being built, along with the list of requisite source
152files. The file may be as simple as a single line:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
matt mooneyefdf02c2010-09-18 18:33:57 -0700154 obj-m := <module_name>.o
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
matt mooneyefdf02c2010-09-18 18:33:57 -0700156The kbuild system will build <module_name>.o from <module_name>.c,
157and, after linking, will result in the kernel module <module_name>.ko.
158The above line can be put in either a "Kbuild" file or a "Makefile."
159When the module is built from multiple sources, an additional line is
160needed listing the files:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
matt mooneyefdf02c2010-09-18 18:33:57 -0700162 <module_name>-y := <src1>.o <src2>.o ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
matt mooneyefdf02c2010-09-18 18:33:57 -0700164NOTE: Further documentation describing the syntax used by kbuild is
165located in Documentation/kbuild/makefiles.txt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
matt mooney57932102010-10-01 21:21:55 -0700167The examples below demonstrate how to create a build file for the
matt mooneyefdf02c2010-09-18 18:33:57 -0700168module 8123.ko, which is built from the following files:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 8123_if.c
171 8123_if.h
172 8123_pci.c
173 8123_bin.o_shipped <= Binary blob
174
matt mooneyefdf02c2010-09-18 18:33:57 -0700175--- 3.1 Shared Makefile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
matt mooneyefdf02c2010-09-18 18:33:57 -0700177 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 Torvalds1da177e2005-04-16 15:20:36 -0700183
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 mooneyefdf02c2010-09-18 18:33:57 -0700192 # normal makefile
193 KDIR ?= /lib/modules/`uname -r`/build
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
matt mooneyefdf02c2010-09-18 18:33:57 -0700195 default:
196 $(MAKE) -C $(KDIR) M=$$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 # Module specific targets
199 genbin:
Brian Strand98a1e442005-11-22 01:23:08 +0000200 echo "X" > 8123_bin.o_shipped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 endif
203
matt mooneyefdf02c2010-09-18 18:33:57 -0700204 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:
matt mooney57932102010-10-01 21:21:55 -0700208 the first pass is by the "make" instance run on the command
209 line; the second pass is by the kbuild system, which is
matt mooneyefdf02c2010-09-18 18:33:57 -0700210 initiated by the parameterized "make" in the default target.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
matt mooneyefdf02c2010-09-18 18:33:57 -0700212--- 3.2 Separate Kbuild File and Makefile
213
214 In newer versions of the kernel, kbuild will first look for a
matt mooney57932102010-10-01 21:21:55 -0700215 file named "Kbuild," and only if that is not found, will it
matt mooneyefdf02c2010-09-18 18:33:57 -0700216 then look for a makefile. Utilizing a "Kbuild" file allows us
217 to split up the makefile from example 1 into two files:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
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 mooneyefdf02c2010-09-18 18:33:57 -0700225 KDIR ?= /lib/modules/`uname -r`/build
226
227 default:
228 $(MAKE) -C $(KDIR) M=$$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 # Module specific targets
231 genbin:
Wolfram Sangbaa91872009-01-06 15:12:27 +0100232 echo "X" > 8123_bin.o_shipped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
matt mooneyefdf02c2010-09-18 18:33:57 -0700234 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 Torvalds1da177e2005-04-16 15:20:36 -0700238
matt mooneyefdf02c2010-09-18 18:33:57 -0700239 The next example shows a backward compatible version.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
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 mooneyefdf02c2010-09-18 18:33:57 -0700248 # kbuild part of makefile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 include Kbuild
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
matt mooneyefdf02c2010-09-18 18:33:57 -0700251 else
252 # normal makefile
253 KDIR ?= /lib/modules/`uname -r`/build
254
255 default:
256 $(MAKE) -C $(KDIR) M=$$PWD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 # Module specific targets
259 genbin:
Wolfram Sangbaa91872009-01-06 15:12:27 +0100260 echo "X" > 8123_bin.o_shipped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 endif
263
matt mooneyefdf02c2010-09-18 18:33:57 -0700264 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 Torvalds1da177e2005-04-16 15:20:36 -0700268
matt mooneyefdf02c2010-09-18 18:33:57 -0700269--- 3.3 Binary Blobs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
matt mooneyefdf02c2010-09-18 18:33:57 -0700271 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 Torvalds1da177e2005-04-16 15:20:36 -0700277
matt mooneyefdf02c2010-09-18 18:33:57 -0700278 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 Torvalds1da177e2005-04-16 15:20:36 -0700282 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
283
matt mooneyefdf02c2010-09-18 18:33:57 -0700284 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
matt mooney57932102010-10-01 21:21:55 -0700291 file. For example, if you wanted to build two modules, foo.ko
292 and bar.ko, the kbuild lines would be:
matt mooneyefdf02c2010-09-18 18:33:57 -0700293
294 obj-m := foo.o bar.o
295 foo-y := <foo_srcs>
296 bar-y := <bar_srcs>
297
298 It is that simple!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300
matt mooney9f021862010-09-19 23:06:36 -0700301=== 4. Include Files
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
matt mooney9f021862010-09-19 23:06:36 -0700303Within the kernel, header files are kept in standard locations
304according to the following rule:
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200305
matt mooney9f021862010-09-19 23:06:36 -0700306 * If the header file only describes the internal interface of a
307 module, then the file is placed in the same directory as the
308 source files.
309 * If the header file describes an interface used by other parts
310 of the kernel that are located in different directories, then
311 the file is placed in include/linux/.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
matt mooney9f021862010-09-19 23:06:36 -0700313 NOTE: There are two notable exceptions to this rule: larger
314 subsystems have their own directory under include/, such as
315 include/scsi; and architecture specific headers are located
316 under arch/$(ARCH)/include/.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
matt mooney9f021862010-09-19 23:06:36 -0700318--- 4.1 Kernel Includes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
matt mooney9f021862010-09-19 23:06:36 -0700320 To include a header file located under include/linux/, simply
321 use:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
matt mooney57932102010-10-01 21:21:55 -0700323 #include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
matt mooney9f021862010-09-19 23:06:36 -0700325 kbuild will add options to "gcc" so the relevant directories
326 are searched.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
matt mooney9f021862010-09-19 23:06:36 -0700328--- 4.2 Single Subdirectory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
matt mooney9f021862010-09-19 23:06:36 -0700330 External modules tend to place header files in a separate
331 include/ directory where their source is located, although this
332 is not the usual kernel style. To inform kbuild of the
matt mooney57932102010-10-01 21:21:55 -0700333 directory, use either ccflags-y or CFLAGS_<filename>.o.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
matt mooney9f021862010-09-19 23:06:36 -0700335 Using the example from section 3, if we moved 8123_if.h to a
336 subdirectory named include, the resulting kbuild file would
337 look like:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 --> filename: Kbuild
matt mooney9f021862010-09-19 23:06:36 -0700340 obj-m := 8123.o
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
matt mooney9f021862010-09-19 23:06:36 -0700342 ccflags-y := -Iinclude
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
344
matt mooney9f021862010-09-19 23:06:36 -0700345 Note that in the assignment there is no space between -I and
346 the path. This is a limitation of kbuild: there must be no
347 space present.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
matt mooney9f021862010-09-19 23:06:36 -0700349--- 4.3 Several Subdirectories
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100350
matt mooney9f021862010-09-19 23:06:36 -0700351 kbuild can handle files that are spread over several directories.
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100352 Consider the following example:
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400353
matt mooney9f021862010-09-19 23:06:36 -0700354 .
355 |__ src
356 | |__ complex_main.c
357 | |__ hal
358 | |__ hardwareif.c
359 | |__ include
360 | |__ hardwareif.h
361 |__ include
362 |__ complex.h
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400363
matt mooney9f021862010-09-19 23:06:36 -0700364 To build the module complex.ko, we then need the following
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100365 kbuild file:
366
matt mooney9f021862010-09-19 23:06:36 -0700367 --> filename: Kbuild
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100368 obj-m := complex.o
369 complex-y := src/complex_main.o
370 complex-y += src/hal/hardwareif.o
371
matt mooney9f021862010-09-19 23:06:36 -0700372 ccflags-y := -I$(src)/include
373 ccflags-y += -I$(src)/src/hal/include
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.
379
380 For the header files, kbuild must be explicitly told where to
381 look. When kbuild executes, the current directory is always the
382 root of the kernel tree (the argument to "-C") and therefore an
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.
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100386
387
matt mooney9f021862010-09-19 23:06:36 -0700388=== 5. Module Installation
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100389
matt mooney9f021862010-09-19 23:06:36 -0700390Modules which are included in the kernel are installed in the
391directory:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
matt mooney57932102010-10-01 21:21:55 -0700393 /lib/modules/$(KERNELRELEASE)/kernel/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
matt mooney9f021862010-09-19 23:06:36 -0700395And external modules are installed in:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
matt mooney57932102010-10-01 21:21:55 -0700397 /lib/modules/$(KERNELRELEASE)/extra/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
matt mooney9f021862010-09-19 23:06:36 -0700399--- 5.1 INSTALL_MOD_PATH
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
matt mooney9f021862010-09-19 23:06:36 -0700401 Above are the default directories but as always some level of
402 customization is possible. A prefix can be added to the
403 installation path using the variable INSTALL_MOD_PATH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 $ make INSTALL_MOD_PATH=/frodo modules_install
matt mooney57932102010-10-01 21:21:55 -0700406 => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
matt mooney9f021862010-09-19 23:06:36 -0700408 INSTALL_MOD_PATH may be set as an ordinary shell variable or,
409 as shown above, can be specified on the command line when
410 calling "make." This has effect when installing both in-tree
411 and out-of-tree modules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
matt mooney9f021862010-09-19 23:06:36 -0700413--- 5.2 INSTALL_MOD_DIR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
matt mooney9f021862010-09-19 23:06:36 -0700415 External modules are by default installed to a directory under
matt mooney57932102010-10-01 21:21:55 -0700416 /lib/modules/$(KERNELRELEASE)/extra/, but you may wish to
417 locate modules for a specific functionality in a separate
418 directory. For this purpose, use INSTALL_MOD_DIR to specify an
419 alternative name to "extra."
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
matt mooney9f021862010-09-19 23:06:36 -0700421 $ make INSTALL_MOD_DIR=gandalf -C $KDIR \
422 M=$PWD modules_install
matt mooney57932102010-10-01 21:21:55 -0700423 => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425
matt mooney9f021862010-09-19 23:06:36 -0700426=== 6. Module Versioning
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
matt mooney9f021862010-09-19 23:06:36 -0700428Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used
429as a simple ABI consistency check. A CRC value of the full prototype
430for an exported symbol is created. When a module is loaded/used, the
431CRC values contained in the kernel are compared with similar values in
432the module; if they are not equal, the kernel refuses to load the
433module.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
matt mooney9f021862010-09-19 23:06:36 -0700435Module.symvers contains a list of all exported symbols from a kernel
436build.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
matt mooney9f021862010-09-19 23:06:36 -0700438--- 6.1 Symbols From the Kernel (vmlinux + modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
matt mooney9f021862010-09-19 23:06:36 -0700440 During a kernel build, a file named Module.symvers will be
441 generated. Module.symvers contains all exported symbols from
442 the kernel and compiled modules. For each symbol, the
443 corresponding CRC value is also stored.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100444
445 The syntax of the Module.symvers file is:
matt mooney9f021862010-09-19 23:06:36 -0700446 <CRC> <Symbol> <module>
447
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100448 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod
449
matt mooney9f021862010-09-19 23:06:36 -0700450 For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
451 would read 0x00000000.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100452
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200453 Module.symvers serves two purposes:
matt mooney9f021862010-09-19 23:06:36 -0700454 1) It lists all exported symbols from vmlinux and all modules.
455 2) It lists the CRC if CONFIG_MODVERSIONS is enabled.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100456
matt mooney9f021862010-09-19 23:06:36 -0700457--- 6.2 Symbols and External Modules
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100458
matt mooney9f021862010-09-19 23:06:36 -0700459 When building an external module, the build system needs access
460 to the symbols from the kernel to check if all external symbols
461 are defined. This is done in the MODPOST step. modpost obtains
462 the symbols by reading Module.symvers from the kernel source
463 tree. If a Module.symvers file is present in the directory
464 where the external module is being built, this file will be
465 read too. During the MODPOST step, a new Module.symvers file
466 will be written containing all exported symbols that were not
467 defined in the kernel.
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400468
matt mooney9f021862010-09-19 23:06:36 -0700469--- 6.3 Symbols From Another External Module
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100470
matt mooney9f021862010-09-19 23:06:36 -0700471 Sometimes, an external module uses exported symbols from
472 another external module. kbuild needs to have full knowledge of
473 all symbols to avoid spitting out warnings about undefined
474 symbols. Three solutions exist for this situation.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100475
matt mooney9f021862010-09-19 23:06:36 -0700476 NOTE: The method with a top-level kbuild file is recommended
477 but may be impractical in certain situations.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100478
matt mooney9f021862010-09-19 23:06:36 -0700479 Use a top-level kbuild file
480 If you have two modules, foo.ko and bar.ko, where
matt mooney57932102010-10-01 21:21:55 -0700481 foo.ko needs symbols from bar.ko, you can use a
matt mooney9f021862010-09-19 23:06:36 -0700482 common top-level kbuild file so both modules are
matt mooney57932102010-10-01 21:21:55 -0700483 compiled in the same build. Consider the following
matt mooney9f021862010-09-19 23:06:36 -0700484 directory layout:
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400485
matt mooney9f021862010-09-19 23:06:36 -0700486 ./foo/ <= contains foo.ko
487 ./bar/ <= contains bar.ko
488
489 The top-level kbuild file would then look like:
490
491 #./Kbuild (or ./Makefile):
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100492 obj-y := foo/ bar/
493
matt mooney57932102010-10-01 21:21:55 -0700494 And executing
495
matt mooney9f021862010-09-19 23:06:36 -0700496 $ make -C $KDIR M=$PWD
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100497
matt mooney57932102010-10-01 21:21:55 -0700498 will then do the expected and compile both modules with
matt mooney9f021862010-09-19 23:06:36 -0700499 full knowledge of symbols from either module.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100500
501 Use an extra Module.symvers file
matt mooney9f021862010-09-19 23:06:36 -0700502 When an external module is built, a Module.symvers file
503 is generated containing all exported symbols which are
504 not defined in the kernel. To get access to symbols
505 from bar.ko, copy the Module.symvers file from the
506 compilation of bar.ko to the directory where foo.ko is
507 built. During the module build, kbuild will read the
508 Module.symvers file in the directory of the external
509 module, and when the build is finished, a new
510 Module.symvers file is created containing the sum of
511 all symbols defined and not part of the kernel.
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400512
matt mooney9f021862010-09-19 23:06:36 -0700513 Use "make" variable KBUILD_EXTRA_SYMBOLS
514 If it is impractical to copy Module.symvers from
515 another module, you can assign a space separated list
matt mooney57932102010-10-01 21:21:55 -0700516 of files to KBUILD_EXTRA_SYMBOLS in your build file.
517 These files will be loaded by modpost during the
matt mooney9f021862010-09-19 23:06:36 -0700518 initialization of its symbol tables.
Richard Hacker0d96fb22008-02-28 09:40:58 +0100519
matt mooney57932102010-10-01 21:21:55 -0700520
matt mooney9f021862010-09-19 23:06:36 -0700521=== 7. Tips & Tricks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
matt mooney9f021862010-09-19 23:06:36 -0700523--- 7.1 Testing for CONFIG_FOO_BAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
matt mooney9f021862010-09-19 23:06:36 -0700525 Modules often need to check for certain CONFIG_ options to
526 decide if a specific feature is included in the module. In
527 kbuild this is done by referencing the CONFIG_ variable
528 directly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 #fs/ext2/Makefile
531 obj-$(CONFIG_EXT2_FS) += ext2.o
532
533 ext2-y := balloc.o bitmap.o dir.o
534 ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o
535
matt mooney9f021862010-09-19 23:06:36 -0700536 External modules have traditionally used "grep" to check for
537 specific CONFIG_ settings directly in .config. This usage is
538 broken. As introduced before, external modules should use
539 kbuild for building and can therefore use the same methods as
540 in-tree modules when testing for CONFIG_ definitions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541