📄 modules.txt
字号:
# Normal Makefile KERNELDIR := /lib/modules/`uname -r`/build all:: $(MAKE) -C $KERNELDIR M=`pwd` $@ # Module specific targets genbin: echo "X" > 8123_bin_shipped endif The trick here is to include the Kbuild file from Makefile, so if an older version of kbuild picks up the Makefile, the Kbuild file will be included.--- 4.2 Binary blobs included in a module Some external modules needs to include a .o as a blob. kbuild has support for this, but requires the blob file to be named <filename>_shipped. In our example the blob is named 8123_bin.o_shipped and when the kbuild rules kick in the file 8123_bin.o is created as a simple copy off the 8213_bin.o_shipped file with the _shipped part stripped of the filename. This allows the 8123_bin.o filename to be used in the assignment to the module. Example 4: obj-m := 8123.o 8123-y := 8123_if.o 8123_pci.o 8123_bin.o In example 4, there is no distinction between the ordinary .c/.h files and the binary file. But kbuild will pick up different rules to create the .o file.=== 5. Include filesInclude files are a necessity when a .c file uses something from other .cfiles (not strictly in the sense of C, but if good programming practice isused). Any module that consists of more than one .c file will have a .h filefor one of the .c files.- If the .h file only describes a module internal interface, then the .h file shall be placed in the same directory as the .c files.- If the .h files describe an interface used by other parts of the kernel located in different directories, the .h files shall be located in include/linux/ or other include/ directories as appropriate.One exception for this rule is larger subsystems that have their own directoryunder include/ such as include/scsi. Another exception is arch-specific.h files which are located under include/asm-$(ARCH)/*.External modules have a tendency to locate include files in a separate include/directory and therefore need to deal with this in their kbuild file.--- 5.1 How to include files from the kernel include dir When a module needs to include a file from include/linux/, then one just uses: #include <linux/modules.h> kbuild will make sure to add options to gcc so the relevant directories are searched. Likewise for .h files placed in the same directory as the .c file. #include "8123_if.h" will do the job.--- 5.2 External modules using an include/ dir External modules often locate their .h files in a separate include/ directory although this is not usual kernel style. When an external module uses an include/ dir then kbuild needs to be told so. The trick here is to use either EXTRA_CFLAGS (take effect for all .c files) or CFLAGS_$F.o (take effect only for a single file). In our example, if we move 8123_if.h to a subdirectory named include/ the resulting Kbuild file would look like: --> filename: Kbuild obj-m := 8123.o EXTRA_CFLAGS := -Iinclude 8123-y := 8123_if.o 8123_pci.o 8123_bin.o Note that in the assignment there is no space between -I and the path. This is a kbuild limitation: there must be no space present.--- 5.3 External modules using several directories If an external module does not follow the usual kernel style, but decides to spread files over several directories, then kbuild can handle this too. Consider the following example: | +- src/complex_main.c | +- hal/hardwareif.c | +- hal/include/hardwareif.h +- include/complex.h To build a single module named complex.ko, we then need the following kbuild file: Kbuild: obj-m := complex.o complex-y := src/complex_main.o complex-y += src/hal/hardwareif.o EXTRA_CFLAGS := -I$(src)/include EXTRA_CFLAGS += -I$(src)src/hal/include kbuild knows how to handle .o files located in another directory - although this is NOT recommended practice. The syntax is to specify the directory relative to the directory where the Kbuild file is located. To find the .h files, we have to explicitly tell kbuild where to look for the .h files. When kbuild executes, the current directory is always the root of the kernel tree (argument to -C) and therefore we have to tell kbuild how to find the .h files using absolute paths. $(src) will specify the absolute path to the directory where the Kbuild file are located when being build as an external module. Therefore -I$(src)/ is used to point out the directory of the Kbuild file and any additional path are just appended.=== 6. Module installationModules which are included in the kernel are installed in the directory: /lib/modules/$(KERNELRELEASE)/kernelExternal modules are installed in the directory: /lib/modules/$(KERNELRELEASE)/extra--- 6.1 INSTALL_MOD_PATH Above are the default directories, but as always, some level of customization is possible. One can prefix the 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 in the example above, can be specified on the command line when calling make. INSTALL_MOD_PATH has effect both when installing modules included in the kernel as well as when installing external modules.--- 6.2 INSTALL_MOD_DIR When installing external modules they are by default installed to a directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish to locate modules for a specific functionality in a separate directory. For this purpose, one can use INSTALL_MOD_DIR to specify an alternative name to 'extra'. $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \ M=`pwd` modules_install => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf=== 7. Module versioning & Module.symversModule versioning is enabled by the CONFIG_MODVERSIONS tag.Module versioning is used as a simple ABI consistency check. The Moduleversioning creates a CRC value of the full prototype for an exported symbol andwhen a module is loaded/used then the CRC values contained in the kernel arecompared with similar values in the module. If they are not equal, then thekernel refuses to load the module.Module.symvers contains a list of all exported symbols from a kernel build.--- 7.1 Symbols from the kernel (vmlinux + modules) During a kernel build, a file named Module.symvers will be generated. Module.symvers contains all exported symbols from the kernel and compiled modules. For each symbols, the corresponding CRC value is stored too. The syntax of the Module.symvers file is: <CRC> <Symbol> <module> Sample: 0x2d036834 scsi_remove_host drivers/scsi/scsi_mod For a kernel build without CONFIG_MODVERSIONS enabled, the crc would read: 0x00000000 Module.symvers serves two purposes: 1) It lists all exported symbols both from vmlinux and all modules 2) It lists the CRC if CONFIG_MODVERSIONS is enabled--- 7.2 Symbols and external modules When building an external module, the build system needs access to the symbols from the kernel to check if all external symbols are defined. This is done in the MODPOST step and to obtain all symbols, modpost reads Module.symvers from the kernel. If a Module.symvers file is present in the directory where the external module is being built, this file will be read too. During the MODPOST step, a new Module.symvers file will be written containing all exported symbols that were not defined in the kernel.--- 7.3 Symbols from another external module Sometimes, an external module uses exported symbols from another external module. Kbuild needs to have full knowledge on all symbols to avoid spitting out warnings about undefined symbols. Two solutions exist to let kbuild know all symbols of more than one external module. The method with a top-level kbuild file is recommended but may be impractical in certain situations. Use a top-level Kbuild file If you have two modules: 'foo' and 'bar', and 'foo' needs symbols from 'bar', then one can use a common top-level kbuild file so both modules are compiled in same build. Consider following directory layout: ./foo/ <= contains the foo module ./bar/ <= contains the bar module The top-level Kbuild file would then look like: #./Kbuild: (this file may also be named Makefile) obj-y := foo/ bar/ Executing: make -C $KDIR M=`pwd` will then do the expected and compile both modules with full knowledge on symbols from both modules. Use an extra Module.symvers file When an external module is built, a Module.symvers file is generated containing all exported symbols which are not defined in the kernel. To get access to symbols from module 'bar', one can copy the Module.symvers file from the compilation of the 'bar' module to the directory where the 'foo' module is built. During the module build, kbuild will read the Module.symvers file in the directory of the external module and when the build is finished, a new Module.symvers file is created containing the sum of all symbols defined and not part of the kernel.=== 8. Tips & Tricks--- 8.1 Testing for CONFIG_FOO_BAR Modules often need to check for certain CONFIG_ options to decide if a specific feature shall be included in the module. When kbuild is used this is done by referencing the CONFIG_ variable directly. #fs/ext2/Makefile obj-$(CONFIG_EXT2_FS) += ext2.o ext2-y := balloc.o bitmap.o dir.o ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o External modules have traditionally used grep to check for specific CONFIG_ settings directly in .config. This usage is broken. As introduced before, external modules shall use kbuild when building and therefore can use the same methods as in-kernel modules when testing for CONFIG_ definitions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -