modules.rst 16 KB

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