modules.rst 18 KB

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