modules.rst 14 KB

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