blob: 799b6835993f7cd1684c0ed0ed0b51d1aa254cd1 [file] [log] [blame]
matt mooneyefdf02c2010-09-18 18:33:57 -07001Building External Modules
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
matt mooneyefdf02c2010-09-18 18:33:57 -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 mooneyefdf02c2010-09-18 18:33:57 -07008 === 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 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 mooneyefdf02c2010-09-18 18:33:57 -070051=== 2. How-to Build External Modules
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
matt mooneyefdf02c2010-09-18 18:33:57 -070053To build external modules, you must have a pre-built kernel available
54that 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 mooneyefdf02c2010-09-18 18:33:57 -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 mooneyefdf02c2010-09-18 18:33:57 -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 mooneyefdf02c2010-09-18 18:33:57 -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
124 /lib/modules/<kernel_release>/extra, but a prefix may
125 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 mooneyefdf02c2010-09-18 18:33:57 -0700167The examples below demonstrate how-to create a build file for the
168module 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:
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 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
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 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
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 Torvalds1da177e2005-04-16 15:20:36 -0700299
300
301=== 5. Include files
302
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200303Include files are a necessity when a .c file uses something from other .c
304files (not strictly in the sense of C, but if good programming practice is
305used). Any module that consists of more than one .c file will have a .h file
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400306for one of the .c files.
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200307
308- If the .h file only describes a module internal interface, then the .h file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 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
314One exception for this rule is larger subsystems that have their own directory
315under include/ such as include/scsi. Another exception is arch-specific
316.h files which are located under include/asm-$(ARCH)/*.
317
318External modules have a tendency to locate include files in a separate include/
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200319directory and therefore need to deal with this in their kbuild file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321--- 5.1 How to include files from the kernel include dir
322
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200323 When a module needs to include a file from include/linux/, then one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200344 In our example, if we move 8123_if.h to a subdirectory named include/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 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 Strand98a1e442005-11-22 01:23:08 +0000353 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 Torvalds1da177e2005-04-16 15:20:36 -0700355
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100356--- 5.3 External modules using several directories
357
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200358 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 Ravnborg253dfa62006-01-06 20:33:41 +0100361
362 Consider the following example:
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400363
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100364 |
365 +- src/complex_main.c
366 | +- hal/hardwareif.c
367 | +- hal/include/hardwareif.h
368 +- include/complex.h
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400369
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200370 To build a single module named complex.ko, we then need the following
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100371 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200383 although this is NOT recommended practice. The syntax is to specify
Sam Ravnborg253dfa62006-01-06 20:33:41 +0100384 the directory relative to the directory where the Kbuild file is
385 located.
386
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200387 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 Ravnborg253dfa62006-01-06 20:33:41 +0100389 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 Torvalds1da177e2005-04-16 15:20:36 -0700395
396=== 6. Module installation
397
Brian Strand98a1e442005-11-22 01:23:08 +0000398Modules which are included in the kernel are installed in the directory:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /lib/modules/$(KERNELRELEASE)/kernel
401
402External modules are installed in the directory:
403
404 /lib/modules/$(KERNELRELEASE)/extra
405
406--- 6.1 INSTALL_MOD_PATH
407
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200408 Above are the default directories, but as always, some level of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200416 example above, can be specified on the command line when calling make.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200422 When installing external modules they are by default installed to a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish
424 to locate modules for a specific functionality in a separate
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200425 directory. For this purpose, one can use INSTALL_MOD_DIR to specify an
426 alternative name to 'extra'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \
429 M=`pwd` modules_install
430 => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf
431
432
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100433=== 7. Module versioning & Module.symvers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Brian Strand98a1e442005-11-22 01:23:08 +0000435Module versioning is enabled by the CONFIG_MODVERSIONS tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437Module versioning is used as a simple ABI consistency check. The Module
438versioning creates a CRC value of the full prototype for an exported symbol and
439when a module is loaded/used then the CRC values contained in the kernel are
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200440compared with similar values in the module. If they are not equal, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441kernel refuses to load the module.
442
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100443Module.symvers contains a list of all exported symbols from a kernel build.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Robert P. J. Day99c8b942006-09-25 15:55:51 -0400445--- 7.1 Symbols from the kernel (vmlinux + modules)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100446
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200447 During a kernel build, a file named Module.symvers will be generated.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100448 Module.symvers contains all exported symbols from the kernel and
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200449 compiled modules. For each symbols, the corresponding CRC value
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100450 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. Day2e99f312006-09-21 09:39:41 -0400457 For a kernel build without CONFIG_MODVERSIONS enabled, the crc
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100458 would read: 0x00000000
459
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200460 Module.symvers serves two purposes:
461 1) It lists all exported symbols both from vmlinux and all modules
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400462 2) It lists the CRC if CONFIG_MODVERSIONS is enabled
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100463
464--- 7.2 Symbols and external modules
465
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200466 When building an external module, the build system needs access to
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100467 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200469 symbols, modpost reads Module.symvers from the kernel.
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100470 If a Module.symvers file is present in the directory where
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200471 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. Day2e99f312006-09-21 09:39:41 -0400474
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100475--- 7.3 Symbols from another external module
476
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200477 Sometimes, an external module uses exported symbols from another
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100478 external module. Kbuild needs to have full knowledge on all symbols
479 to avoid spitting out warnings about undefined symbols.
Richard Hacker0d96fb22008-02-28 09:40:58 +0100480 Three solutions exist to let kbuild know all symbols of more than
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100481 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200486 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 Ravnborg040fcc82006-01-28 22:15:55 +0100489
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. Day2e99f312006-09-21 09:39:41 -0400494
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100495 #./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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200505 When an external module is built, a Module.symvers file is
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100506 generated containing all exported symbols which are not
507 defined in the kernel.
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200508 To get access to symbols from module 'bar', one can copy the
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100509 Module.symvers file from the compilation of the 'bar' module
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200510 to the directory where the 'foo' module is built.
511 During the module build, kbuild will read the Module.symvers
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100512 file in the directory of the external module and when the
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200513 build is finished, a new Module.symvers file is created
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100514 containing the sum of all symbols defined and not part of the
515 kernel.
Robert P. J. Day2e99f312006-09-21 09:39:41 -0400516
Richard Hacker0d96fb22008-02-28 09:40:58 +0100517 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 Torvalds1da177e2005-04-16 15:20:36 -0700524=== 8. Tips & Tricks
525
526--- 8.1 Testing for CONFIG_FOO_BAR
527
Jan Engelhardtd9a7ff62006-07-27 22:14:29 +0200528 Modules often need to check for certain CONFIG_ options to decide if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 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 Engelhardtd9a7ff62006-07-27 22:14:29 +0200540 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 Torvalds1da177e2005-04-16 15:20:36 -0700543