modules.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. Building External Modules
  2. This document describes how to build an out-of-tree kernel module.
  3. === Table of Contents
  4. === 1 Introduction
  5. === 2 How to Build External Modules
  6. --- 2.1 Command Syntax
  7. --- 2.2 Options
  8. --- 2.3 Targets
  9. --- 2.4 Building Separate Files
  10. === 3. Creating a Kbuild File for an External Module
  11. --- 3.1 Shared Makefile
  12. --- 3.2 Separate Kbuild file and Makefile
  13. --- 3.3 Binary Blobs
  14. --- 3.4 Building Multiple Modules
  15. === 4. Include Files
  16. --- 4.1 Kernel Includes
  17. --- 4.2 Single Subdirectory
  18. --- 4.3 Several Subdirectories
  19. === 5. Module Installation
  20. --- 5.1 INSTALL_MOD_PATH
  21. --- 5.2 INSTALL_MOD_DIR
  22. === 6. Module Versioning
  23. --- 6.1 Symbols From the Kernel (vmlinux + modules)
  24. --- 6.2 Symbols and External Modules
  25. --- 6.3 Symbols From Another External Module
  26. === 7. Tips & Tricks
  27. --- 7.1 Testing for CONFIG_FOO_BAR
  28. === 1. Introduction
  29. "kbuild" is the build system used by the Linux kernel. Modules must use
  30. kbuild to stay compatible with changes in the build infrastructure and
  31. to pick up the right flags to "gcc." Functionality for building modules
  32. both in-tree and out-of-tree is provided. The method for building
  33. either is similar, and all modules are initially developed and built
  34. out-of-tree.
  35. Covered in this document is information aimed at developers interested
  36. in building out-of-tree (or "external") modules. The author of an
  37. external module should supply a makefile that hides most of the
  38. complexity, so one only has to type "make" to build the module. This is
  39. easily accomplished, and a complete example will be presented in
  40. section 3.
  41. === 2. How to Build External Modules
  42. To build external modules, you must have a prebuilt kernel available
  43. that contains the configuration and header files used in the build.
  44. Also, the kernel must have been built with modules enabled. If you are
  45. using a distribution kernel, there will be a package for the kernel you
  46. are running provided by your distribution.
  47. An alternative is to use the "make" target "modules_prepare." This will
  48. make sure the kernel contains the information required. The target
  49. exists solely as a simple way to prepare a kernel source tree for
  50. building external modules.
  51. NOTE: "modules_prepare" will not build Module.symvers even if
  52. CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be
  53. executed to make module versioning work.
  54. --- 2.1 Command Syntax
  55. The command to build an external module is:
  56. $ make -C <path_to_kernel_src> M=$PWD
  57. The kbuild system knows that an external module is being built
  58. due to the "M=<dir>" option given in the command.
  59. To build against the running kernel use:
  60. $ make -C /lib/modules/`uname -r`/build M=$PWD
  61. Then to install the module(s) just built, add the target
  62. "modules_install" to the command:
  63. $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install
  64. --- 2.2 Options
  65. ($KDIR refers to the path of the kernel source directory.)
  66. make -C $KDIR M=$PWD
  67. -C $KDIR
  68. The directory where the kernel source is located.
  69. "make" will actually change to the specified directory
  70. when executing and will change back when finished.
  71. M=$PWD
  72. Informs kbuild that an external module is being built.
  73. The value given to "M" is the absolute path of the
  74. directory where the external module (kbuild file) is
  75. located.
  76. --- 2.3 Targets
  77. When building an external module, only a subset of the "make"
  78. targets are available.
  79. make -C $KDIR M=$PWD [target]
  80. The default will build the module(s) located in the current
  81. directory, so a target does not need to be specified. All
  82. output files will also be generated in this directory. No
  83. attempts are made to update the kernel source, and it is a
  84. precondition that a successful "make" has been executed for the
  85. kernel.
  86. modules
  87. The default target for external modules. It has the
  88. same functionality as if no target was specified. See
  89. description above.
  90. modules_install
  91. Install the external module(s). The default location is
  92. /lib/modules/<kernel_release>/extra/, but a prefix may
  93. be added with INSTALL_MOD_PATH (discussed in section 5).
  94. clean
  95. Remove all generated files in the module directory only.
  96. help
  97. List the available targets for external modules.
  98. --- 2.4 Building Separate Files
  99. It is possible to build single files that are part of a module.
  100. This works equally well for the kernel, a module, and even for
  101. external modules.
  102. Example (The module foo.ko, consist of bar.o and baz.o):
  103. make -C $KDIR M=$PWD bar.lst
  104. make -C $KDIR M=$PWD baz.o
  105. make -C $KDIR M=$PWD foo.ko
  106. make -C $KDIR M=$PWD /
  107. === 3. Creating a Kbuild File for an External Module
  108. In the last section we saw the command to build a module for the
  109. running kernel. The module is not actually built, however, because a
  110. build file is required. Contained in this file will be the name of
  111. the module(s) being built, along with the list of requisite source
  112. files. The file may be as simple as a single line:
  113. obj-m := <module_name>.o
  114. The kbuild system will build <module_name>.o from <module_name>.c,
  115. and, after linking, will result in the kernel module <module_name>.ko.
  116. The above line can be put in either a "Kbuild" file or a "Makefile."
  117. When the module is built from multiple sources, an additional line is
  118. needed listing the files:
  119. <module_name>-y := <src1>.o <src2>.o ...
  120. NOTE: Further documentation describing the syntax used by kbuild is
  121. located in Documentation/kbuild/makefiles.txt.
  122. The examples below demonstrate how to create a build file for the
  123. module 8123.ko, which is built from the following files:
  124. 8123_if.c
  125. 8123_if.h
  126. 8123_pci.c
  127. 8123_bin.o_shipped <= Binary blob
  128. --- 3.1 Shared Makefile
  129. An external module always includes a wrapper makefile that
  130. supports building the module using "make" with no arguments.
  131. This target is not used by kbuild; it is only for convenience.
  132. Additional functionality, such as test targets, can be included
  133. but should be filtered out from kbuild due to possible name
  134. clashes.
  135. Example 1:
  136. --> filename: Makefile
  137. ifneq ($(KERNELRELEASE),)
  138. # kbuild part of makefile
  139. obj-m := 8123.o
  140. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  141. else
  142. # normal makefile
  143. KDIR ?= /lib/modules/`uname -r`/build
  144. default:
  145. $(MAKE) -C $(KDIR) M=$$PWD
  146. # Module specific targets
  147. genbin:
  148. echo "X" > 8123_bin.o_shipped
  149. endif
  150. The check for KERNELRELEASE is used to separate the two parts
  151. of the makefile. In the example, kbuild will only see the two
  152. assignments, whereas "make" will see everything except these
  153. two assignments. This is due to two passes made on the file:
  154. the first pass is by the "make" instance run on the command
  155. line; the second pass is by the kbuild system, which is
  156. initiated by the parameterized "make" in the default target.
  157. --- 3.2 Separate Kbuild File and Makefile
  158. In newer versions of the kernel, kbuild will first look for a
  159. file named "Kbuild," and only if that is not found, will it
  160. then look for a makefile. Utilizing a "Kbuild" file allows us
  161. to split up the makefile from example 1 into two files:
  162. Example 2:
  163. --> filename: Kbuild
  164. obj-m := 8123.o
  165. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  166. --> filename: Makefile
  167. KDIR ?= /lib/modules/`uname -r`/build
  168. default:
  169. $(MAKE) -C $(KDIR) M=$$PWD
  170. # Module specific targets
  171. genbin:
  172. echo "X" > 8123_bin.o_shipped
  173. The split in example 2 is questionable due to the simplicity of
  174. each file; however, some external modules use makefiles
  175. consisting of several hundred lines, and here it really pays
  176. off to separate the kbuild part from the rest.
  177. The next example shows a backward compatible version.
  178. Example 3:
  179. --> filename: Kbuild
  180. obj-m := 8123.o
  181. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  182. --> filename: Makefile
  183. ifneq ($(KERNELRELEASE),)
  184. # kbuild part of makefile
  185. include Kbuild
  186. else
  187. # normal makefile
  188. KDIR ?= /lib/modules/`uname -r`/build
  189. default:
  190. $(MAKE) -C $(KDIR) M=$$PWD
  191. # Module specific targets
  192. genbin:
  193. echo "X" > 8123_bin.o_shipped
  194. endif
  195. Here the "Kbuild" file is included from the makefile. This
  196. allows an older version of kbuild, which only knows of
  197. makefiles, to be used when the "make" and kbuild parts are
  198. split into separate files.
  199. --- 3.3 Binary Blobs
  200. Some external modules need to include an object file as a blob.
  201. kbuild has support for this, but requires the blob file to be
  202. named <filename>_shipped. When the kbuild rules kick in, a copy
  203. of <filename>_shipped is created with _shipped stripped off,
  204. giving us <filename>. This shortened filename can be used in
  205. the assignment to the module.
  206. Throughout this section, 8123_bin.o_shipped has been used to
  207. build the kernel module 8123.ko; it has been included as
  208. 8123_bin.o.
  209. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  210. Although there is no distinction between the ordinary source
  211. files and the binary file, kbuild will pick up different rules
  212. when creating the object file for the module.
  213. --- 3.4 Building Multiple Modules
  214. kbuild supports building multiple modules with a single build
  215. file. For example, if you wanted to build two modules, foo.ko
  216. and bar.ko, the kbuild lines would be:
  217. obj-m := foo.o bar.o
  218. foo-y := <foo_srcs>
  219. bar-y := <bar_srcs>
  220. It is that simple!
  221. === 4. Include Files
  222. Within the kernel, header files are kept in standard locations
  223. according to the following rule:
  224. * If the header file only describes the internal interface of a
  225. module, then the file is placed in the same directory as the
  226. source files.
  227. * If the header file describes an interface used by other parts
  228. of the kernel that are located in different directories, then
  229. the file is placed in include/linux/.
  230. NOTE: There are two notable exceptions to this rule: larger
  231. subsystems have their own directory under include/, such as
  232. include/scsi; and architecture specific headers are located
  233. under arch/$(ARCH)/include/.
  234. --- 4.1 Kernel Includes
  235. To include a header file located under include/linux/, simply
  236. use:
  237. #include <linux/module.h>
  238. kbuild will add options to "gcc" so the relevant directories
  239. are searched.
  240. --- 4.2 Single Subdirectory
  241. External modules tend to place header files in a separate
  242. include/ directory where their source is located, although this
  243. is not the usual kernel style. To inform kbuild of the
  244. directory, use either ccflags-y or CFLAGS_<filename>.o.
  245. Using the example from section 3, if we moved 8123_if.h to a
  246. subdirectory named include, the resulting kbuild file would
  247. look like:
  248. --> filename: Kbuild
  249. obj-m := 8123.o
  250. ccflags-y := -Iinclude
  251. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  252. Note that in the assignment there is no space between -I and
  253. the path. This is a limitation of kbuild: there must be no
  254. space present.
  255. --- 4.3 Several Subdirectories
  256. kbuild can handle files that are spread over several directories.
  257. Consider the following example:
  258. .
  259. |__ src
  260. | |__ complex_main.c
  261. | |__ hal
  262. | |__ hardwareif.c
  263. | |__ include
  264. | |__ hardwareif.h
  265. |__ include
  266. |__ complex.h
  267. To build the module complex.ko, we then need the following
  268. kbuild file:
  269. --> filename: Kbuild
  270. obj-m := complex.o
  271. complex-y := src/complex_main.o
  272. complex-y += src/hal/hardwareif.o
  273. ccflags-y := -I$(src)/include
  274. ccflags-y += -I$(src)/src/hal/include
  275. As you can see, kbuild knows how to handle object files located
  276. in other directories. The trick is to specify the directory
  277. relative to the kbuild file's location. That being said, this
  278. is NOT recommended practice.
  279. For the header files, kbuild must be explicitly told where to
  280. look. When kbuild executes, the current directory is always the
  281. root of the kernel tree (the argument to "-C") and therefore an
  282. absolute path is needed. $(src) provides the absolute path by
  283. pointing to the directory where the currently executing kbuild
  284. file is located.
  285. === 5. Module Installation
  286. Modules which are included in the kernel are installed in the
  287. directory:
  288. /lib/modules/$(KERNELRELEASE)/kernel/
  289. And external modules are installed in:
  290. /lib/modules/$(KERNELRELEASE)/extra/
  291. --- 5.1 INSTALL_MOD_PATH
  292. Above are the default directories but as always some level of
  293. customization is possible. A prefix can be added to the
  294. installation path using the variable INSTALL_MOD_PATH:
  295. $ make INSTALL_MOD_PATH=/frodo modules_install
  296. => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/
  297. INSTALL_MOD_PATH may be set as an ordinary shell variable or,
  298. as shown above, can be specified on the command line when
  299. calling "make." This has effect when installing both in-tree
  300. and out-of-tree modules.
  301. --- 5.2 INSTALL_MOD_DIR
  302. External modules are by default installed to a directory under
  303. /lib/modules/$(KERNELRELEASE)/extra/, but you may wish to
  304. locate modules for a specific functionality in a separate
  305. directory. For this purpose, use INSTALL_MOD_DIR to specify an
  306. alternative name to "extra."
  307. $ make INSTALL_MOD_DIR=gandalf -C $KDIR \
  308. M=$PWD modules_install
  309. => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/
  310. === 6. Module Versioning
  311. Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used
  312. as a simple ABI consistency check. A CRC value of the full prototype
  313. for an exported symbol is created. When a module is loaded/used, the
  314. CRC values contained in the kernel are compared with similar values in
  315. the module; if they are not equal, the kernel refuses to load the
  316. module.
  317. Module.symvers contains a list of all exported symbols from a kernel
  318. build.
  319. --- 6.1 Symbols From the Kernel (vmlinux + modules)
  320. During a kernel build, a file named Module.symvers will be
  321. generated. Module.symvers contains all exported symbols from
  322. the kernel and compiled modules. For each symbol, the
  323. corresponding CRC value is also stored.
  324. The syntax of the Module.symvers file is:
  325. <CRC> <Symbol> <module>
  326. 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod
  327. For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
  328. would read 0x00000000.
  329. Module.symvers serves two purposes:
  330. 1) It lists all exported symbols from vmlinux and all modules.
  331. 2) It lists the CRC if CONFIG_MODVERSIONS is enabled.
  332. --- 6.2 Symbols and External Modules
  333. When building an external module, the build system needs access
  334. to the symbols from the kernel to check if all external symbols
  335. are defined. This is done in the MODPOST step. modpost obtains
  336. the symbols by reading Module.symvers from the kernel source
  337. tree. If a Module.symvers file is present in the directory
  338. where the external module is being built, this file will be
  339. read too. During the MODPOST step, a new Module.symvers file
  340. will be written containing all exported symbols that were not
  341. defined in the kernel.
  342. --- 6.3 Symbols From Another External Module
  343. Sometimes, an external module uses exported symbols from
  344. another external module. kbuild needs to have full knowledge of
  345. all symbols to avoid spitting out warnings about undefined
  346. symbols. Three solutions exist for this situation.
  347. NOTE: The method with a top-level kbuild file is recommended
  348. but may be impractical in certain situations.
  349. Use a top-level kbuild file
  350. If you have two modules, foo.ko and bar.ko, where
  351. foo.ko needs symbols from bar.ko, you can use a
  352. common top-level kbuild file so both modules are
  353. compiled in the same build. Consider the following
  354. directory layout:
  355. ./foo/ <= contains foo.ko
  356. ./bar/ <= contains bar.ko
  357. The top-level kbuild file would then look like:
  358. #./Kbuild (or ./Makefile):
  359. obj-y := foo/ bar/
  360. And executing
  361. $ make -C $KDIR M=$PWD
  362. will then do the expected and compile both modules with
  363. full knowledge of symbols from either module.
  364. Use an extra Module.symvers file
  365. When an external module is built, a Module.symvers file
  366. is generated containing all exported symbols which are
  367. not defined in the kernel. To get access to symbols
  368. from bar.ko, copy the Module.symvers file from the
  369. compilation of bar.ko to the directory where foo.ko is
  370. built. During the module build, kbuild will read the
  371. Module.symvers file in the directory of the external
  372. module, and when the build is finished, a new
  373. Module.symvers file is created containing the sum of
  374. all symbols defined and not part of the kernel.
  375. Use "make" variable KBUILD_EXTRA_SYMBOLS
  376. If it is impractical to copy Module.symvers from
  377. another module, you can assign a space separated list
  378. of files to KBUILD_EXTRA_SYMBOLS in your build file.
  379. These files will be loaded by modpost during the
  380. initialization of its symbol tables.
  381. === 7. Tips & Tricks
  382. --- 7.1 Testing for CONFIG_FOO_BAR
  383. Modules often need to check for certain CONFIG_ options to
  384. decide if a specific feature is included in the module. In
  385. kbuild this is done by referencing the CONFIG_ variable
  386. directly.
  387. #fs/ext2/Makefile
  388. obj-$(CONFIG_EXT2_FS) += ext2.o
  389. ext2-y := balloc.o bitmap.o dir.o
  390. ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o
  391. External modules have traditionally used "grep" to check for
  392. specific CONFIG_ settings directly in .config. This usage is
  393. broken. As introduced before, external modules should use
  394. kbuild for building and can therefore use the same methods as
  395. in-tree modules when testing for CONFIG_ definitions.