modules.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. In this document you will find information about:
  2. - how to build external modules
  3. - how to make your module use the kbuild infrastructure
  4. - how kbuild will install a kernel
  5. - how to install modules in a non-standard location
  6. === Table of Contents
  7. === 1 Introduction
  8. === 2 How to build external modules
  9. --- 2.1 Building external modules
  10. --- 2.2 Available targets
  11. --- 2.3 Available options
  12. --- 2.4 Preparing the kernel tree for module build
  13. --- 2.5 Building separate files for a module
  14. === 3. Example commands
  15. === 4. Creating a kbuild file for an external module
  16. === 5. Include files
  17. --- 5.1 How to include files from the kernel include dir
  18. --- 5.2 External modules using an include/ dir
  19. --- 5.3 External modules using several directories
  20. === 6. Module installation
  21. --- 6.1 INSTALL_MOD_PATH
  22. --- 6.2 INSTALL_MOD_DIR
  23. === 7. Module versioning & Module.symvers
  24. --- 7.1 Symbols from the kernel (vmlinux + modules)
  25. --- 7.2 Symbols and external modules
  26. --- 7.3 Symbols from another external module
  27. === 8. Tips & Tricks
  28. --- 8.1 Testing for CONFIG_FOO_BAR
  29. === 1. Introduction
  30. kbuild includes functionality for building modules both
  31. within the kernel source tree and outside the kernel source tree.
  32. The latter is usually referred to as external or "out-of-tree"
  33. modules and is used both during development and for modules that
  34. are not planned to be included in the kernel tree.
  35. What is covered within this file is mainly information to authors
  36. of modules. The author of an external module should supply
  37. a makefile that hides most of the complexity, so one only has to type
  38. 'make' to build the module. A complete example will be presented in
  39. chapter 4, "Creating a kbuild file for an external module".
  40. === 2. How to build external modules
  41. kbuild offers functionality to build external modules, with the
  42. prerequisite that there is a pre-built kernel available with full source.
  43. A subset of the targets available when building the kernel is available
  44. when building an external module.
  45. --- 2.1 Building external modules
  46. Use the following command to build an external module:
  47. make -C <path-to-kernel> M=`pwd`
  48. For the running kernel use:
  49. make -C /lib/modules/`uname -r`/build M=`pwd`
  50. For the above command to succeed, the kernel must have been
  51. built with modules enabled.
  52. To install the modules that were just built:
  53. make -C <path-to-kernel> M=`pwd` modules_install
  54. More complex examples will be shown later, the above should
  55. be enough to get you started.
  56. --- 2.2 Available targets
  57. $KDIR refers to the path to the kernel source top-level directory
  58. make -C $KDIR M=`pwd`
  59. Will build the module(s) located in current directory.
  60. All output files will be located in the same directory
  61. as the module source.
  62. No attempts are made to update the kernel source, and it is
  63. a precondition that a successful make has been executed
  64. for the kernel.
  65. make -C $KDIR M=`pwd` modules
  66. The modules target is implied when no target is given.
  67. Same functionality as if no target was specified.
  68. See description above.
  69. make -C $KDIR M=`pwd` modules_install
  70. Install the external module(s).
  71. Installation default is in /lib/modules/<kernel-version>/extra,
  72. but may be prefixed with INSTALL_MOD_PATH - see separate
  73. chapter.
  74. make -C $KDIR M=`pwd` clean
  75. Remove all generated files for the module - the kernel
  76. source directory is not modified.
  77. make -C $KDIR M=`pwd` help
  78. help will list the available target when building external
  79. modules.
  80. --- 2.3 Available options:
  81. $KDIR refers to the path to the kernel source top-level directory
  82. make -C $KDIR
  83. Used to specify where to find the kernel source.
  84. '$KDIR' represent the directory where the kernel source is.
  85. Make will actually change directory to the specified directory
  86. when executed but change back when finished.
  87. make -C $KDIR M=`pwd`
  88. M= is used to tell kbuild that an external module is
  89. being built.
  90. The option given to M= is the directory where the external
  91. module (kbuild file) is located.
  92. When an external module is being built only a subset of the
  93. usual targets are available.
  94. make -C $KDIR SUBDIRS=`pwd`
  95. Same as M=. The SUBDIRS= syntax is kept for backwards
  96. compatibility.
  97. --- 2.4 Preparing the kernel tree for module build
  98. To make sure the kernel contains the information required to
  99. build external modules the target 'modules_prepare' must be used.
  100. 'modules_prepare' exists solely as a simple way to prepare
  101. a kernel source tree for building external modules.
  102. Note: modules_prepare will not build Module.symvers even if
  103. CONFIG_MODVERSIONS is set. Therefore a full kernel build
  104. needs to be executed to make module versioning work.
  105. --- 2.5 Building separate files for a module
  106. It is possible to build single files which are part of a module.
  107. This works equally well for the kernel, a module and even for
  108. external modules.
  109. Examples (module foo.ko, consist of bar.o, baz.o):
  110. make -C $KDIR M=`pwd` bar.lst
  111. make -C $KDIR M=`pwd` bar.o
  112. make -C $KDIR M=`pwd` foo.ko
  113. make -C $KDIR M=`pwd` /
  114. === 3. Example commands
  115. This example shows the actual commands to be executed when building
  116. an external module for the currently running kernel.
  117. In the example below, the distribution is supposed to use the
  118. facility to locate output files for a kernel compile in a different
  119. directory than the kernel source - but the examples will also work
  120. when the source and the output files are mixed in the same directory.
  121. # Kernel source
  122. /lib/modules/<kernel-version>/source -> /usr/src/linux-<version>
  123. # Output from kernel compile
  124. /lib/modules/<kernel-version>/build -> /usr/src/linux-<version>-up
  125. Change to the directory where the kbuild file is located and execute
  126. the following commands to build the module:
  127. cd /home/user/src/module
  128. make -C /usr/src/`uname -r`/source \
  129. O=/lib/modules/`uname-r`/build \
  130. M=`pwd`
  131. Then, to install the module use the following command:
  132. make -C /usr/src/`uname -r`/source \
  133. O=/lib/modules/`uname-r`/build \
  134. M=`pwd` \
  135. modules_install
  136. If you look closely you will see that this is the same command as
  137. listed before - with the directories spelled out.
  138. The above are rather long commands, and the following chapter
  139. lists a few tricks to make it all easier.
  140. === 4. Creating a kbuild file for an external module
  141. kbuild is the build system for the kernel, and external modules
  142. must use kbuild to stay compatible with changes in the build system
  143. and to pick up the right flags to gcc etc.
  144. The kbuild file used as input shall follow the syntax described
  145. in Documentation/kbuild/makefiles.txt. This chapter will introduce a few
  146. more tricks to be used when dealing with external modules.
  147. In the following a Makefile will be created for a module with the
  148. following files:
  149. 8123_if.c
  150. 8123_if.h
  151. 8123_pci.c
  152. 8123_bin.o_shipped <= Binary blob
  153. --- 4.1 Shared Makefile for module and kernel
  154. An external module always includes a wrapper Makefile supporting
  155. building the module using 'make' with no arguments.
  156. The Makefile provided will most likely include additional
  157. functionality such as test targets etc. and this part shall
  158. be filtered away from kbuild since it may impact kbuild if
  159. name clashes occurs.
  160. Example 1:
  161. --> filename: Makefile
  162. ifneq ($(KERNELRELEASE),)
  163. # kbuild part of makefile
  164. obj-m := 8123.o
  165. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  166. else
  167. # Normal Makefile
  168. KERNELDIR := /lib/modules/`uname -r`/build
  169. all::
  170. $(MAKE) -C $(KERNELDIR) M=`pwd` $@
  171. # Module specific targets
  172. genbin:
  173. echo "X" > 8123_bin.o_shipped
  174. endif
  175. In example 1, the check for KERNELRELEASE is used to separate
  176. the two parts of the Makefile. kbuild will only see the two
  177. assignments whereas make will see everything except the two
  178. kbuild assignments.
  179. In recent versions of the kernel, kbuild will look for a file named
  180. Kbuild and as second option look for a file named Makefile.
  181. Utilising the Kbuild file makes us split up the Makefile in example 1
  182. into two files as shown in example 2:
  183. Example 2:
  184. --> filename: Kbuild
  185. obj-m := 8123.o
  186. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  187. --> filename: Makefile
  188. KERNELDIR := /lib/modules/`uname -r`/build
  189. all::
  190. $(MAKE) -C $KERNELDIR M=`pwd` $@
  191. # Module specific targets
  192. genbin:
  193. echo "X" > 8123_bin_shipped
  194. In example 2, we are down to two fairly simple files and for simple
  195. files as used in this example the split is questionable. But some
  196. external modules use Makefiles of several hundred lines and here it
  197. really pays off to separate the kbuild part from the rest.
  198. Example 3 shows a backward compatible version.
  199. Example 3:
  200. --> filename: Kbuild
  201. obj-m := 8123.o
  202. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  203. --> filename: Makefile
  204. ifneq ($(KERNELRELEASE),)
  205. include Kbuild
  206. else
  207. # Normal Makefile
  208. KERNELDIR := /lib/modules/`uname -r`/build
  209. all::
  210. $(MAKE) -C $KERNELDIR M=`pwd` $@
  211. # Module specific targets
  212. genbin:
  213. echo "X" > 8123_bin_shipped
  214. endif
  215. The trick here is to include the Kbuild file from Makefile, so
  216. if an older version of kbuild picks up the Makefile, the Kbuild
  217. file will be included.
  218. --- 4.2 Binary blobs included in a module
  219. Some external modules needs to include a .o as a blob. kbuild
  220. has support for this, but requires the blob file to be named
  221. <filename>_shipped. In our example the blob is named
  222. 8123_bin.o_shipped and when the kbuild rules kick in the file
  223. 8123_bin.o is created as a simple copy off the 8213_bin.o_shipped file
  224. with the _shipped part stripped of the filename.
  225. This allows the 8123_bin.o filename to be used in the assignment to
  226. the module.
  227. Example 4:
  228. obj-m := 8123.o
  229. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  230. In example 4, there is no distinction between the ordinary .c/.h files
  231. and the binary file. But kbuild will pick up different rules to create
  232. the .o file.
  233. === 5. Include files
  234. Include files are a necessity when a .c file uses something from other .c
  235. files (not strictly in the sense of C, but if good programming practice is
  236. used). Any module that consists of more than one .c file will have a .h file
  237. for one of the .c files.
  238. - If the .h file only describes a module internal interface, then the .h file
  239. shall be placed in the same directory as the .c files.
  240. - If the .h files describe an interface used by other parts of the kernel
  241. located in different directories, the .h files shall be located in
  242. include/linux/ or other include/ directories as appropriate.
  243. One exception for this rule is larger subsystems that have their own directory
  244. under include/ such as include/scsi. Another exception is arch-specific
  245. .h files which are located under include/asm-$(ARCH)/*.
  246. External modules have a tendency to locate include files in a separate include/
  247. directory and therefore need to deal with this in their kbuild file.
  248. --- 5.1 How to include files from the kernel include dir
  249. When a module needs to include a file from include/linux/, then one
  250. just uses:
  251. #include <linux/modules.h>
  252. kbuild will make sure to add options to gcc so the relevant
  253. directories are searched.
  254. Likewise for .h files placed in the same directory as the .c file.
  255. #include "8123_if.h"
  256. will do the job.
  257. --- 5.2 External modules using an include/ dir
  258. External modules often locate their .h files in a separate include/
  259. directory although this is not usual kernel style. When an external
  260. module uses an include/ dir then kbuild needs to be told so.
  261. The trick here is to use either EXTRA_CFLAGS (take effect for all .c
  262. files) or CFLAGS_$F.o (take effect only for a single file).
  263. In our example, if we move 8123_if.h to a subdirectory named include/
  264. the resulting Kbuild file would look like:
  265. --> filename: Kbuild
  266. obj-m := 8123.o
  267. EXTRA_CFLAGS := -Iinclude
  268. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  269. Note that in the assignment there is no space between -I and the path.
  270. This is a kbuild limitation: there must be no space present.
  271. --- 5.3 External modules using several directories
  272. If an external module does not follow the usual kernel style, but
  273. decides to spread files over several directories, then kbuild can
  274. handle this too.
  275. Consider the following example:
  276. |
  277. +- src/complex_main.c
  278. | +- hal/hardwareif.c
  279. | +- hal/include/hardwareif.h
  280. +- include/complex.h
  281. To build a single module named complex.ko, we then need the following
  282. kbuild file:
  283. Kbuild:
  284. obj-m := complex.o
  285. complex-y := src/complex_main.o
  286. complex-y += src/hal/hardwareif.o
  287. EXTRA_CFLAGS := -I$(src)/include
  288. EXTRA_CFLAGS += -I$(src)src/hal/include
  289. kbuild knows how to handle .o files located in another directory -
  290. although this is NOT recommended practice. The syntax is to specify
  291. the directory relative to the directory where the Kbuild file is
  292. located.
  293. To find the .h files, we have to explicitly tell kbuild where to look
  294. for the .h files. When kbuild executes, the current directory is always
  295. the root of the kernel tree (argument to -C) and therefore we have to
  296. tell kbuild how to find the .h files using absolute paths.
  297. $(src) will specify the absolute path to the directory where the
  298. Kbuild file are located when being build as an external module.
  299. Therefore -I$(src)/ is used to point out the directory of the Kbuild
  300. file and any additional path are just appended.
  301. === 6. Module installation
  302. Modules which are included in the kernel are installed in the directory:
  303. /lib/modules/$(KERNELRELEASE)/kernel
  304. External modules are installed in the directory:
  305. /lib/modules/$(KERNELRELEASE)/extra
  306. --- 6.1 INSTALL_MOD_PATH
  307. Above are the default directories, but as always, some level of
  308. customization is possible. One can prefix the path using the variable
  309. INSTALL_MOD_PATH:
  310. $ make INSTALL_MOD_PATH=/frodo modules_install
  311. => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel
  312. INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the
  313. example above, can be specified on the command line when calling make.
  314. INSTALL_MOD_PATH has effect both when installing modules included in
  315. the kernel as well as when installing external modules.
  316. --- 6.2 INSTALL_MOD_DIR
  317. When installing external modules they are by default installed to a
  318. directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish
  319. to locate modules for a specific functionality in a separate
  320. directory. For this purpose, one can use INSTALL_MOD_DIR to specify an
  321. alternative name to 'extra'.
  322. $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \
  323. M=`pwd` modules_install
  324. => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf
  325. === 7. Module versioning & Module.symvers
  326. Module versioning is enabled by the CONFIG_MODVERSIONS tag.
  327. Module versioning is used as a simple ABI consistency check. The Module
  328. versioning creates a CRC value of the full prototype for an exported symbol and
  329. when a module is loaded/used then the CRC values contained in the kernel are
  330. compared with similar values in the module. If they are not equal, then the
  331. kernel refuses to load the module.
  332. Module.symvers contains a list of all exported symbols from a kernel build.
  333. --- 7.1 Symbols from the kernel (vmlinux + modules)
  334. During a kernel build, a file named Module.symvers will be generated.
  335. Module.symvers contains all exported symbols from the kernel and
  336. compiled modules. For each symbols, the corresponding CRC value
  337. is stored too.
  338. The syntax of the Module.symvers file is:
  339. <CRC> <Symbol> <module>
  340. Sample:
  341. 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod
  342. For a kernel build without CONFIG_MODVERSIONS enabled, the crc
  343. would read: 0x00000000
  344. Module.symvers serves two purposes:
  345. 1) It lists all exported symbols both from vmlinux and all modules
  346. 2) It lists the CRC if CONFIG_MODVERSIONS is enabled
  347. --- 7.2 Symbols and external modules
  348. When building an external module, the build system needs access to
  349. the symbols from the kernel to check if all external symbols are
  350. defined. This is done in the MODPOST step and to obtain all
  351. symbols, modpost reads Module.symvers from the kernel.
  352. If a Module.symvers file is present in the directory where
  353. the external module is being built, this file will be read too.
  354. During the MODPOST step, a new Module.symvers file will be written
  355. containing all exported symbols that were not defined in the kernel.
  356. --- 7.3 Symbols from another external module
  357. Sometimes, an external module uses exported symbols from another
  358. external module. Kbuild needs to have full knowledge on all symbols
  359. to avoid spitting out warnings about undefined symbols.
  360. Two solutions exist to let kbuild know all symbols of more than
  361. one external module.
  362. The method with a top-level kbuild file is recommended but may be
  363. impractical in certain situations.
  364. Use a top-level Kbuild file
  365. If you have two modules: 'foo' and 'bar', and 'foo' needs
  366. symbols from 'bar', then one can use a common top-level kbuild
  367. file so both modules are compiled in same build.
  368. Consider following directory layout:
  369. ./foo/ <= contains the foo module
  370. ./bar/ <= contains the bar module
  371. The top-level Kbuild file would then look like:
  372. #./Kbuild: (this file may also be named Makefile)
  373. obj-y := foo/ bar/
  374. Executing:
  375. make -C $KDIR M=`pwd`
  376. will then do the expected and compile both modules with full
  377. knowledge on symbols from both modules.
  378. Use an extra Module.symvers file
  379. When an external module is built, a Module.symvers file is
  380. generated containing all exported symbols which are not
  381. defined in the kernel.
  382. To get access to symbols from module 'bar', one can copy the
  383. Module.symvers file from the compilation of the 'bar' module
  384. to the directory where the 'foo' module is built.
  385. During the module build, kbuild will read the Module.symvers
  386. file in the directory of the external module and when the
  387. build is finished, a new Module.symvers file is created
  388. containing the sum of all symbols defined and not part of the
  389. kernel.
  390. === 8. Tips & Tricks
  391. --- 8.1 Testing for CONFIG_FOO_BAR
  392. Modules often need to check for certain CONFIG_ options to decide if
  393. a specific feature shall be included in the module. When kbuild is used
  394. this is done by referencing the CONFIG_ variable directly.
  395. #fs/ext2/Makefile
  396. obj-$(CONFIG_EXT2_FS) += ext2.o
  397. ext2-y := balloc.o bitmap.o dir.o
  398. ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o
  399. External modules have traditionally used grep to check for specific
  400. CONFIG_ settings directly in .config. This usage is broken.
  401. As introduced before, external modules shall use kbuild when building
  402. and therefore can use the same methods as in-kernel modules when
  403. testing for CONFIG_ definitions.