Building External Modules

来源:互联网 发布:enum java valueof 编辑:程序博客网 时间:2024/06/11 14:14
from:https://www.kernel.org/doc/Documentation/kbuild/modules.txt
Building External ModulesThis document describes how to build an out-of-tree kernel module.=== Table of Contents=== 1 Introduction=== 2 How to Build External Modules   --- 2.1 Command Syntax   --- 2.2 Options   --- 2.3 Targets   --- 2.4 Building Separate Files=== 3. Creating a Kbuild File for an External Module   --- 3.1 Shared Makefile   --- 3.2 Separate Kbuild file and Makefile   --- 3.3 Binary Blobs   --- 3.4 Building Multiple Modules=== 4. Include Files   --- 4.1 Kernel Includes   --- 4.2 Single Subdirectory   --- 4.3 Several Subdirectories=== 5. Module Installation   --- 5.1 INSTALL_MOD_PATH   --- 5.2 INSTALL_MOD_DIR=== 6. Module Versioning   --- 6.1 Symbols From the Kernel (vmlinux + modules)   --- 6.2 Symbols and External Modules   --- 6.3 Symbols From Another External Module=== 7. Tips & Tricks   --- 7.1 Testing for CONFIG_FOO_BAR=== 1. Introduction"kbuild" is the build system used by the Linux kernel. Modules must usekbuild to stay compatible with changes in the build infrastructure andto pick up the right flags to "gcc." Functionality for building modulesboth in-tree and out-of-tree is provided. The method for buildingeither is similar, and all modules are initially developed and builtout-of-tree.Covered in this document is information aimed at developers interestedin building out-of-tree (or "external") modules. The author of anexternal module should supply a makefile that hides most of thecomplexity, so one only has to type "make" to build the module. This iseasily accomplished, and a complete example will be presented insection 3.=== 2. How to Build External ModulesTo build external modules, you must have a prebuilt kernel availablethat contains the configuration and header files used in the build.Also, the kernel must have been built with modules enabled. If you areusing a distribution kernel, there will be a package for the kernel youare running provided by your distribution.An alternative is to use the "make" target "modules_prepare." This willmake sure the kernel contains the information required. The targetexists solely as a simple way to prepare a kernel source tree forbuilding external modules.NOTE: "modules_prepare" will not build Module.symvers even ifCONFIG_MODVERSIONS is set; therefore, a full kernel build needs to beexecuted to make module versioning work.--- 2.1 Command SyntaxThe command to build an external module is:$ make -C <path_to_kernel_src> M=$PWDThe kbuild system knows that an external module is being builtdue to the "M=<dir>" option given in the command.To build against the running kernel use:$ make -C /lib/modules/`uname -r`/build M=$PWDThen to install the module(s) just built, add the target"modules_install" to the command:$ make -C /lib/modules/`uname -r`/build M=$PWD modules_install--- 2.2 Options($KDIR refers to the path of the kernel source directory.)make -C $KDIR M=$PWD-C $KDIRThe directory where the kernel source is located."make" will actually change to the specified directorywhen executing and will change back when finished.M=$PWDInforms kbuild that an external module is being built.The value given to "M" is the absolute path of thedirectory where the external module (kbuild file) islocated.--- 2.3 TargetsWhen building an external module, only a subset of the "make"targets are available.make -C $KDIR M=$PWD [target]The default will build the module(s) located in the currentdirectory, so a target does not need to be specified. Alloutput files will also be generated in this directory. Noattempts are made to update the kernel source, and it is aprecondition that a successful "make" has been executed for thekernel.modulesThe default target for external modules. It has thesame functionality as if no target was specified. Seedescription above.modules_installInstall the external module(s). The default location is/lib/modules/<kernel_release>/extra/, but a prefix maybe added with INSTALL_MOD_PATH (discussed in section 5).cleanRemove all generated files in the module directory only.helpList the available targets for external modules.--- 2.4 Building Separate FilesIt is possible to build single files that are part of a module.This works equally well for the kernel, a module, and even forexternal modules.Example (The module foo.ko, consist of bar.o and baz.o):make -C $KDIR M=$PWD bar.lstmake -C $KDIR M=$PWD baz.omake -C $KDIR M=$PWD foo.komake -C $KDIR M=$PWD /=== 3. Creating a Kbuild File for an External ModuleIn the last section we saw the command to build a module for therunning kernel. The module is not actually built, however, because abuild file is required. Contained in this file will be the name ofthe module(s) being built, along with the list of requisite sourcefiles. The file may be as simple as a single line:obj-m := <module_name>.oThe kbuild system will build <module_name>.o from <module_name>.c,and, after linking, will result in the kernel module <module_name>.ko.The above line can be put in either a "Kbuild" file or a "Makefile."When the module is built from multiple sources, an additional line isneeded listing the files:<module_name>-y := <src1>.o <src2>.o ...NOTE: Further documentation describing the syntax used by kbuild islocated in Documentation/kbuild/makefiles.txt.The examples below demonstrate how to create a build file for themodule 8123.ko, which is built from the following files:8123_if.c8123_if.h8123_pci.c8123_bin.o_shipped<= Binary blob--- 3.1 Shared MakefileAn external module always includes a wrapper makefile thatsupports building the module using "make" with no arguments.This target is not used by kbuild; it is only for convenience.Additional functionality, such as test targets, can be includedbut should be filtered out from kbuild due to possible nameclashes.Example 1:--> filename: Makefileifneq ($(KERNELRELEASE),)# kbuild part of makefileobj-m  := 8123.o8123-y := 8123_if.o 8123_pci.o 8123_bin.oelse# normal makefileKDIR ?= /lib/modules/`uname -r`/builddefault:$(MAKE) -C $(KDIR) M=$$PWD# Module specific targetsgenbin:echo "X" > 8123_bin.o_shippedendifThe check for KERNELRELEASE is used to separate the two partsof the makefile. In the example, kbuild will only see the twoassignments, whereas "make" will see everything except thesetwo assignments. This is due to two passes made on the file:the first pass is by the "make" instance run on the commandline; the second pass is by the kbuild system, which isinitiated by the parameterized "make" in the default target.--- 3.2 Separate Kbuild File and MakefileIn newer versions of the kernel, kbuild will first look for afile named "Kbuild," and only if that is not found, will itthen look for a makefile. Utilizing a "Kbuild" file allows usto split up the makefile from example 1 into two files:Example 2:--> filename: Kbuildobj-m  := 8123.o8123-y := 8123_if.o 8123_pci.o 8123_bin.o--> filename: MakefileKDIR ?= /lib/modules/`uname -r`/builddefault:$(MAKE) -C $(KDIR) M=$$PWD# Module specific targetsgenbin:echo "X" > 8123_bin.o_shippedThe split in example 2 is questionable due to the simplicity ofeach file; however, some external modules use makefilesconsisting of several hundred lines, and here it really paysoff to separate the kbuild part from the rest.The next example shows a backward compatible version.Example 3:--> filename: Kbuildobj-m  := 8123.o8123-y := 8123_if.o 8123_pci.o 8123_bin.o--> filename: Makefileifneq ($(KERNELRELEASE),)# kbuild part of makefileinclude Kbuildelse# normal makefileKDIR ?= /lib/modules/`uname -r`/builddefault:$(MAKE) -C $(KDIR) M=$$PWD# Module specific targetsgenbin:echo "X" > 8123_bin.o_shippedendifHere the "Kbuild" file is included from the makefile. Thisallows an older version of kbuild, which only knows ofmakefiles, to be used when the "make" and kbuild parts aresplit into separate files.--- 3.3 Binary BlobsSome external modules need to include an object file as a blob.kbuild has support for this, but requires the blob file to benamed <filename>_shipped. When the kbuild rules kick in, a copyof <filename>_shipped is created with _shipped stripped off,giving us <filename>. This shortened filename can be used inthe assignment to the module.Throughout this section, 8123_bin.o_shipped has been used tobuild the kernel module 8123.ko; it has been included as8123_bin.o.8123-y := 8123_if.o 8123_pci.o 8123_bin.oAlthough there is no distinction between the ordinary sourcefiles and the binary file, kbuild will pick up different ruleswhen creating the object file for the module.--- 3.4 Building Multiple Moduleskbuild supports building multiple modules with a single buildfile. For example, if you wanted to build two modules, foo.koand bar.ko, the kbuild lines would be:obj-m := foo.o bar.ofoo-y := <foo_srcs>bar-y := <bar_srcs>It is that simple!=== 4. Include FilesWithin the kernel, header files are kept in standard locationsaccording to the following rule:* If the header file only describes the internal interface of a  module, then the file is placed in the same directory as the  source files.* If the header file describes an interface used by other parts  of the kernel that are located in different directories, then  the file is placed in include/linux/.  NOTE: There are two notable exceptions to this rule: larger  subsystems have their own directory under include/, such as  include/scsi; and architecture specific headers are located  under arch/$(ARCH)/include/.--- 4.1 Kernel IncludesTo include a header file located under include/linux/, simplyuse:#include <linux/module.h>kbuild will add options to "gcc" so the relevant directoriesare searched.--- 4.2 Single SubdirectoryExternal modules tend to place header files in a separateinclude/ directory where their source is located, although thisis not the usual kernel style. To inform kbuild of thedirectory, use either ccflags-y or CFLAGS_<filename>.o.Using the example from section 3, if we moved 8123_if.h to asubdirectory named include, the resulting kbuild file wouldlook like:--> filename: Kbuildobj-m := 8123.occflags-y := -Iinclude8123-y := 8123_if.o 8123_pci.o 8123_bin.oNote that in the assignment there is no space between -I andthe path. This is a limitation of kbuild: there must be nospace present.--- 4.3 Several Subdirectorieskbuild can handle files that are spread over several directories.Consider the following example:.|__ src|   |__ complex_main.c|   |__ hal||__ hardwareif.c||__ include|    |__ hardwareif.h|__ include    |__ complex.hTo build the module complex.ko, we then need the followingkbuild file:--> filename: Kbuildobj-m := complex.ocomplex-y := src/complex_main.ocomplex-y += src/hal/hardwareif.occflags-y := -I$(src)/includeccflags-y += -I$(src)/src/hal/includeAs you can see, kbuild knows how to handle object files locatedin other directories. The trick is to specify the directoryrelative to the kbuild file's location. That being said, thisis NOT recommended practice.For the header files, kbuild must be explicitly told where tolook. When kbuild executes, the current directory is always theroot of the kernel tree (the argument to "-C") and therefore anabsolute path is needed. $(src) provides the absolute path bypointing to the directory where the currently executing kbuildfile is located.=== 5. Module InstallationModules which are included in the kernel are installed in thedirectory:/lib/modules/$(KERNELRELEASE)/kernel/And external modules are installed in:/lib/modules/$(KERNELRELEASE)/extra/--- 5.1 INSTALL_MOD_PATHAbove are the default directories but as always some level ofcustomization is possible. A prefix can be added to theinstallation path using the variable INSTALL_MOD_PATH:$ make INSTALL_MOD_PATH=/frodo modules_install=> Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/INSTALL_MOD_PATH may be set as an ordinary shell variable or,as shown above, can be specified on the command line whencalling "make." This has effect when installing both in-treeand out-of-tree modules.--- 5.2 INSTALL_MOD_DIRExternal modules are by default installed to a directory under/lib/modules/$(KERNELRELEASE)/extra/, but you may wish tolocate modules for a specific functionality in a separatedirectory. For this purpose, use INSTALL_MOD_DIR to specify analternative name to "extra."$ make INSTALL_MOD_DIR=gandalf -C $KDIR \       M=$PWD modules_install=> Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/=== 6. Module VersioningModule versioning is enabled by the CONFIG_MODVERSIONS tag, and is usedas a simple ABI consistency check. A CRC value of the full prototypefor an exported symbol is created. When a module is loaded/used, theCRC values contained in the kernel are compared with similar values inthe module; if they are not equal, the kernel refuses to load themodule.Module.symvers contains a list of all exported symbols from a kernelbuild.--- 6.1 Symbols From the Kernel (vmlinux + modules)During a kernel build, a file named Module.symvers will begenerated. Module.symvers contains all exported symbols fromthe kernel and compiled modules. For each symbol, thecorresponding CRC value is also stored.The syntax of the Module.symvers file is:<CRC>    <Symbol>       <module>0x2d036834  scsi_remove_host   drivers/scsi/scsi_modFor a kernel build without CONFIG_MODVERSIONS enabled, the CRCwould read 0x00000000.Module.symvers serves two purposes:1) It lists all exported symbols from vmlinux and all modules.2) It lists the CRC if CONFIG_MODVERSIONS is enabled.--- 6.2 Symbols and External ModulesWhen building an external module, the build system needs accessto the symbols from the kernel to check if all external symbolsare defined. This is done in the MODPOST step. modpost obtainsthe symbols by reading Module.symvers from the kernel sourcetree. If a Module.symvers file is present in the directorywhere the external module is being built, this file will beread too. During the MODPOST step, a new Module.symvers filewill be written containing all exported symbols that were notdefined in the kernel.--- 6.3 Symbols From Another External ModuleSometimes, an external module uses exported symbols fromanother external module. kbuild needs to have full knowledge ofall symbols to avoid spitting out warnings about undefinedsymbols. Three solutions exist for this situation.NOTE: The method with a top-level kbuild file is recommendedbut may be impractical in certain situations.Use a top-level kbuild fileIf you have two modules, foo.ko and bar.ko, wherefoo.ko needs symbols from bar.ko, you can use acommon top-level kbuild file so both modules arecompiled in the same build. Consider the followingdirectory layout:./foo/ <= contains foo.ko./bar/ <= contains bar.koThe top-level kbuild file would then look like:#./Kbuild (or ./Makefile):obj-y := foo/ bar/And executing$ make -C $KDIR M=$PWDwill then do the expected and compile both modules withfull knowledge of symbols from either module.Use an extra Module.symvers fileWhen an external module is built, a Module.symvers fileis generated containing all exported symbols which arenot defined in the kernel. To get access to symbolsfrom bar.ko, copy the Module.symvers file from thecompilation of bar.ko to the directory where foo.ko isbuilt. During the module build, kbuild will read theModule.symvers file in the directory of the externalmodule, and when the build is finished, a newModule.symvers file is created containing the sum ofall symbols defined and not part of the kernel.Use "make" variable KBUILD_EXTRA_SYMBOLSIf it is impractical to copy Module.symvers fromanother module, you can assign a space separated listof files to KBUILD_EXTRA_SYMBOLS in your build file.These files will be loaded by modpost during theinitialization of its symbol tables.=== 7. Tips & Tricks--- 7.1 Testing for CONFIG_FOO_BARModules often need to check for certain CONFIG_ options todecide if a specific feature is included in the module. Inkbuild this is done by referencing the CONFIG_ variabledirectly.#fs/ext2/Makefileobj-$(CONFIG_EXT2_FS) += ext2.oext2-y := balloc.o bitmap.o dir.oext2-$(CONFIG_EXT2_FS_XATTR) += xattr.oExternal modules have traditionally used "grep" to check forspecific CONFIG_ settings directly in .config. This usage isbroken. As introduced before, external modules should usekbuild for building and can therefore use the same methods asin-tree modules when testing for CONFIG_ definitions.
0 0
原创粉丝点击