linux下Documentation\kbuild\modules.txt的英文原版及个人部分翻译和注释


 

前言:

       这是第二篇翻译关于makefile的文章。比起第一篇makefile.txt的翻译,这篇翻译的更具体一点,希望能对需要的童鞋提供帮助。如果不合适的地方望指出。

 

使用姿势:

       在linux下的各种makefile如果有不懂的关键字,可以搜索定位到文中的相应部分,从而有一个更好的理解。

 

正文:

 

Inthis document you will find information about:

-how to build external modules

-how to make your module use the kbuild infrastructure

-how kbuild will install a kernel

-how to install modules in a non-standard location

(

-怎么建立外部模块

-怎么使用kbuild架构来make自己的模块

-kbuild如何安装一个内核

-怎么安装模块再一个随意的位置)

 

===Table of Contents

 

       === 1 Introduction

       === 2 How to build external modules

         --- 2.1 Building external modules

         --- 2.2 Available targets

         --- 2.3 Available options

         --- 2.4 Preparing the kernel tree for module build

         --- 2.5 Building separate files for a module

       === 3. Example commands

       === 4. Creating a kbuild file for anexternal module

       === 5. Include files

         --- 5.1 How to include files from the kernel include dir

         --- 5.2 External modules using an include/ dir

         --- 5.3 External modules using several directories

       === 6. Module installation

         --- 6.1 INSTALL_MOD_PATH

         --- 6.2 INSTALL_MOD_DIR

       === 7. Module versioning &Module.symvers

         --- 7.1 Symbols from the kernel (vmlinux + modules)

         --- 7.2 Symbols and external modules

         --- 7.3 Symbols from another external module

       === 8. Tips & Tricks

         --- 8.1 Testing for CONFIG_FOO_BAR

 

 

 

===1. Introduction

 

kbuildincludes functionality for building modules both

withinthe kernel source tree and outside the kernel source tree.

Thelatter is usually referred to as external or "out-of-tree"

modulesand is used both during development and for modules that

arenot planned to be included in the kernel tree.

 

Whatis covered within this file is mainly information to authors

ofmodules. The author of an external module should supply

amakefile that hides most of the complexity, so one only has to type

'make'to build the module. A complete example will be presented in

chapter4, "Creating a kbuild file for an external module".

(kbuild包含着一些功能提供给具有内核源码树和内核源码树外部的建立模块过程。

后者通常称为外部额外的或者”out-of-tree”模块,使用于开发过程和不打算包含进源码树的模块。

此文档主要写给些模块的人。写外部模块的人应该提供一个隐藏复杂内容的makefile,然后直接用make命令就可以编译模块。一个完整的过程会在章节4展示)

 

===2. How to build external modules

 

kbuildoffers functionality to build external modules, with the

prerequisitethat there is a pre-built kernel available with full source.

Asubset of the targets available when building the kernel is available

whenbuilding an external module.

(kbuild给建立外部模块提供功能,包含含有完整源码的预编译的一个内核的先决条件。

当编译内核时候,当编译外部模块的时候,一个目标的子集会生效)

 

---2.1 Building external modules

 

       Use the following command to build anexternal module:

(使用下列指令去建立一个外部模块)

 

              make -C <path-to-kernel>M=`pwd`

 

       For the running kernel use:

       (跑内核则使用)

 

              make -C /lib/modules/`uname-r`/build M=`pwd`

 

       For the above command to succeed, thekernel must have been

       built with modules enabled.

       (为了使上述指令生效,必须用模块启动内核)

 

       To install the modules that were justbuilt:

       (安装刚刚编译好的模块)

 

              make -C <path-to-kernel>M=`pwd` modules_install

 

       More complex examples will be shown later,the above should

       be enough to get you started.

       (一些复杂的例子将会在后面展示,这些只是开端)

 

---2.2 Available targets

 

       $KDIR refers to the path to the kernelsource top-level directory

       ($KDIR引用一个关于内核源代码顶层目录的路径)

 

       make -C $KDIR M=`pwd`

              Will build the module(s) locatedin current directory.

              All output files will be locatedin the same directory

              as the module source.

              No attempts are made to update thekernel source, and it is

              a precondition that a successfulmake has been executed

              for the kernel.

              (将会在当前目录编译模块。

所有输出文件会在模块资源所在目录。

这里没有更新内核源码的尝试,这只是一个已经在内核执行的成功make的先决条件)

 

       make -C $KDIR M=`pwd` modules

              The modules target is implied whenno target is given.

              Same functionality as if no targetwas specified.

              See description above.

(当没有目标给出时候,目标模块是隐式的。

没有目标指定也是类似的功能。

看上面的描述?)

 

       make -C $KDIR M=`pwd` modules_install

              Install the external module(s).

              Installation default is in/lib/modules/<kernel-version>/extra,

              but may be prefixed withINSTALL_MOD_PATH - see separate

              chapter.

              (安装外部模块。

默认安装在/lib/modules/<kernel-version>/extra。

不过可能会使用INSTALL_MOD_PATH来预处理)

 

       make -C $KDIR M=`pwd` clean

              Remove all generated files for themodule - the kernel

              source directory is not modified.

              (移除所有给模块生成的文件-内核源码目录不会被改变)

 

       make -C $KDIR M=`pwd` help

              help will list the availabletarget when building external

              modules.

              (help会在编译外部模块时列出所有有效的目标)

 

---2.3 Available options:

 

       $KDIR refers to the path to the kernelsource top-level directory

 

       make -C $KDIR

              Used to specify where to find thekernel source.

              '$KDIR' represent the directorywhere the kernel source is.

              Make will actually changedirectory to the specified directory

              when executed but change back whenfinished.

              (用来指定内核源代码的位置。

$KDIR代表源码目录。

Make会改变默认目录去使用指定目录,当执行完后又改回原来目录)

 

       make -C $KDIR M=`pwd`

              M= is used to tell kbuild that anexternal module is

              being built.

              The option given to M= is thedirectory where the external

              module (kbuild file) is located.

              When an external module is beingbuilt only a subset of the

              usual targets are available.

              (M=用于告诉kbuild一个外部模块正在被建立。

M=的选项是外部模块(kbuild文件)所在目录。

当一个外部模块被编译时,只有一个通用目标的子集生效)

 

       make -C $KDIR SUBDIRS=`pwd`

              Same as M=. The SUBDIRS= syntax iskept for backwards

              Compatibility(兼容).

              (类似于M=,SUBDIRS=是为了后面兼容保留的?)

 

---2.4 Preparing the kernel tree for module build

 

       To make sure the kernel contains theinformation required to

       build external modules the target'modules_prepare' must be used.

       'modules_prepare' exists solely(单独的) asa simple way to prepare

       a kernel source tree for buildingexternal modules.

       Note: modules_prepare will not buildModule.symvers even if

       CONFIG_MODVERSIONS is set. Therefore afull kernel build

       needs to be executed to make moduleversioning work.

       (为了让内核包含着建立外部模块的信息,必须使用modules_prepare。

Module_prepare单独存在,作为一种方式去为了建立外部模块而准备内核树

注意:modules_prepare不会编译module.symvers即使设置了

CONFIG_MODVERSIONS。因此为了模块对应版本,需要一个完整的内核编译。)

 

---2.5 Building separate files for a module

       It is possible to build single fileswhich are part of a module.

       This works equally well for the kernel, amodule and even for

       external modules.

       (可以去建立一个文件作为模块的一部分。这个文件会正常工作)

       Examples (module foo.ko, consist ofbar.o, baz.o):

              make -C $KDIR M=`pwd` bar.lst

              make -C $KDIR M=`pwd` bar.o

              make -C $KDIR M=`pwd` foo.ko

              make -C $KDIR M=`pwd` /

 

 

===3. Example commands

 

Thisexample shows the actual commands to be executed when building

anexternal module for the currently running kernel.

Inthe example below, the distribution(分布) is supposed to(认为) usethe

facility(设施,便利)to locate output files for a kernel compile in a different

directorythan the kernel source - but the examples will also work

whenthe source and the output files are mixed in the same directory.

(这个例子展示当编译外部模块给当前运行的内核的时候,实际执行的命令行。

下列例子中,这样分布被认为是使用这些设置去安放输出文件给内核编译器,发生在不同的目录里而不是内核源码-但这个例子中当源码和输出文件在同一个目录时会照样工作)

 

#Kernel source

/lib/modules/<kernel-version>/source-> /usr/src/linux-<version>

 

#Output from kernel compile

/lib/modules/<kernel-version>/build-> /usr/src/linux-<version>-up

 

Changeto the directory where the kbuild file is located and execute

thefollowing commands to build the module:

(改成kbuild文件所在目录和执行下列命令行去编译模块)

 

       cd /home/user/src/module

       make -C /usr/src/`uname -r`/source            \

              O=/lib/modules/`uname-r`/build       \

              M=`pwd`

 

Then,to install the module use the following command:

(然后使用下列指令安装模块)

 

       make -C /usr/src/`uname -r`/source            \

              O=/lib/modules/`uname-r`/build       \

              M=`pwd`                              \

              modules_install

 

Ifyou look closely you will see that this is the same command as

listedbefore - with the directories spelled out.

(如果你仔细看会发现这两个例子是同一个指令-列出了详细的目录)

 

Theabove are rather long commands, and the following chapter

listsa few tricks to make it all easier.

(上述例子用了较长的指令,接下来的章节有更简易的指令)

 

 

===4. Creating a kbuild file for an external module

 

kbuildis the build system for the kernel, and external modules

mustuse kbuild to stay compatible with changes in the build system

andto pick up the right flags to gcc etc.

(kbuild是内核的编译系统,外部模块必须使用kbuild去兼容各种编译系统的更改以及去使用正确的标志给gcc)

 

Thekbuild file used as input shall follow the syntax described

inDocumentation/kbuild/makefiles.txt. This chapter will introduce a few

moretricks to be used when dealing with external modules.

(kbuild文件当做输入shall来使用,Documentation/kbuild/makefiles.txt有详细说明。这个章节会介绍更多方法去处理外部模块)

 

Inthe following a Makefile will be created for a module with the

followingfiles:

(下面章节会介绍一个使用下列文件创建的给外部模块的makefile)

       8123_if.c

       8123_if.h

       8123_pci.c

       8123_bin.o_shipped   <= Binary blob

 

---4.1 Shared Makefile for module and kernel

 

       An external module always includes awrapper(包装) Makefile supporting

       building the module using 'make' with noarguments.

       The Makefile provided will most likelyinclude additional

       functionality such as test targets etc.and this part shall

       be filtered away from kbuild since it mayimpact kbuild if

       name clashes occurs.

(一个外部模块通常被一个makefile包装以便于无参数地使用make来编译外部模块。

Makefile提供的功能总是会包含一些其他功能例如测试目标等等。这部分会排除kbuild因为可能会因为命名冲突而影响kbuild)

 

       Example 1:

              --> filename: Makefile

              ifneq ($(KERNELRELEASE),)

              # kbuild part of makefile

              obj-m  := 8123.o

              8123-y := 8123_if.o 8123_pci.o 8123_bin.o

 

              else

              # Normal Makefile

 

              KERNELDIR := /lib/modules/`uname-r`/build

              all::

                     $(MAKE) -C $(KERNELDIR)M=`pwd` $@

 

              # Module specific targets

              genbin:

                     echo "X" >8123_bin.o_shipped

 

              endif

 

       In example 1, the check for KERNELRELEASEis used to separate

       the two parts of the Makefile. kbuildwill only see the two

       assignments whereas make will seeeverything except the two

       kbuild assignments.

(例子1中,确认是否使用了KERNELRELEASE去分开makefile的两个部分。Kbuild只看到两个参数然而make会看到两个参数以外的所有东西)

 

       In recent versions of the kernel, kbuildwill look for a file named

       Kbuild and as second option look for afile named Makefile.

       Utilising the Kbuild file makes us splitup the Makefile in example 1

       into two files as shown in example 2:

(现在版本的内核中,kbuild会优先寻找kbuild然后是makefile。使用kbuild文件会使例子1分离成两个文件,如下面例子所示)

 

       Example 2:

              --> filename: Kbuild

              obj-m  := 8123.o

              8123-y := 8123_if.o 8123_pci.o8123_bin.o

 

              --> filename: Makefile

              KERNELDIR := /lib/modules/`uname-r`/build

              all::

                     $(MAKE) -C $(KERNELDIR)M=`pwd` $@

 

              # Module specific targets

              genbin:

                     echo "X" >8123_bin.o_shipped

 

 

       In example 2, we are down to two fairlysimple files and for simple

       files as used in this example the splitis questionable. But some

       external modules use Makefiles of severalhundred lines and here it

       really pays off(分清) toseparate the kbuild part from the rest.

       Example 3 shows a backward compatibleversion.

(例子2中,我们将它变成了两个差不多的文件,对于这些文件的分离方法是有问题的。但一些外部模块会使用上百行的makefile,本例子算是把kbuild完整的提取出来了。

例子4展示了一个老版本情况下的例子。)

 

       Example 3:

              --> filename: Kbuild

              obj-m  := 8123.o

              8123-y := 8123_if.o 8123_pci.o8123_bin.o

 

              --> filename: Makefile

              ifneq ($(KERNELRELEASE),)

              include Kbuild

              else

              # Normal Makefile

 

              KERNELDIR := /lib/modules/`uname-r`/build

              all::

                     $(MAKE) -C $(KERNELDIR)M=`pwd` $@

 

              # Module specific targets

              genbin:

                     echo "X" >8123_bin.o_shipped

 

              endif

 

       The trick here is to include the Kbuildfile from Makefile, so

       if an older version of kbuild picks upthe Makefile, the Kbuild

       file will be included.

(这里不一样的地方是makefile include了kbuild,所以如果一个老版本的kbuild需要makefile,kbuild需要被include)

 

---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 theblob file to be named

       <filename>_shipped. In our examplethe blob is named

       8123_bin.o_shipped and when the kbuildrules kick in the file

       8123_bin.o is created as a simple copyoff the 8213_bin.o_shipped file

       with the _shipped part stripped of thefilename.

       This allows the 8123_bin.o filename to beused in the assignment to

       the module.

(一些外部模块需要包含一个.o文件作为二进制文件。Kbuild支持这种操作,但需要二进制文件命名为<filename>_shipped。在例子中二进制大文件命名为8123_bin.o_shipped,当kbuild规则放在文件里时候8123_bin.o就会被当做8213_bin.o_shipeed文件的备份创建只是把_shipped部分去掉而已。

下面例子允许在赋值模块时使用8123_bin.o名字)

 

       Example 4:

              obj-m  := 8123.o

              8123-y := 8123_if.o 8123_pci.o8123_bin.o

 

       In example 4, there is no distinctionbetween the ordinary .c/.h files

       and the binary file. But kbuild will pickup different rules to create

       the .o file.

(例子4中,.c/.h文件和二进制文件都没有限制。但kbuild会使用一个不同的规则去创建.o文件)

 

 

===5. Include files

 

Includefiles are a necessity when a .c file uses something from other .c

files(not strictly in the sense of C, but if good programming practice is

used).Any module that consists of more than one .c file will have a .h file

forone of the .c files.

(当.c文件引用其他.c文件(不只是.c文件)的东西的时候include文件是必要的。任何模块都由不止一个.c文件构成,这些.c文件还会有.h文件对应他们)

 

-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 .hfiles shall be located in

  include/linux/ or other include/ directoriesas appropriate.

(-如果.h文件描述了模块的内部接口,这个.h就应该像.c文件一样放在模块的同一个目录。

-如果.h文件描述的接口被内核其他部分在不同的目录中调用,.h文件就应该放在include/linux或者其他Include/目录。)

 

Oneexception for this rule is larger subsystems that have their own directory

underinclude/ such as include/scsi. Another exception is arch-specific

.hfiles which are located under include/asm-$(ARCH)/*.

(一个例外就是当有一个大型的子系统的时候,这个子系统有他们自己在include/下的目录例如include/scsi。另一个例外就是指定架构的文件arch-specific.h,在include/asm-$(ARCH))

 

Externalmodules have a tendency(趋势) to locate include filesin a separate include/

directoryand therefore need to deal with this in their kbuild file.

(外部模块经常放在一个单独的include/的include文件,因此需要在他们的kbuild文件里处理他们)

 

---5.1 How to include files from the kernel include dir

 

       When a module needs to include a filefrom include/linux/, then one

       just uses:

       (当一个模块需要包含一个include/linux/的文件时候,一种方法是:)

 

              #include <linux/modules.h>

 

       kbuild will make sure to add options togcc so the relevant

       directories are searched.

       Likewise for .h files placed in the samedirectory as the .c file.

       (kbuild会确认是否添加选项到gcc因此相关目录将会被搜寻。

同样的对于在与.c文件相同目录的.h文件的include如下)  

 

              #include "8123_if.h"

 

       will do the job.

 

---5.2 External modules using an include/ dir

 

       External modules often locate their .hfiles in a separate include/

       directory although this is not usualkernel style. When an external

       module uses an include/ dir then kbuildneeds to be told so.

       The trick here is to use eitherEXTRA_CFLAGS (take effect for all .c

       files) or CFLAGS_$F.o (take effect onlyfor a single file).

(外部模块通常把他们的.h文件放在单独的Include/目录中虽然这不是内核的一贯做法。当一个外部模块使用include/目录后必须告诉kbuild。

这里的方法是使用EXTRA_CFLAGS(对所有.c文件都适用)或者CFLAG_$F.o(对单个文件适用))

 

       In our example, if we move 8123_if.h to asubdirectory named include/

       the resulting Kbuild file would looklike:

(在例子中,如果我们移动了8123.h文件到一个叫include/的子目录,Kbuild就可以这样改)

 

              --> filename: Kbuild

              obj-m  := 8123.o

 

              EXTRA_CFLAGS := -Iinclude

              8123-y := 8123_if.o 8123_pci.o8123_bin.o

 

       Note that in the assignment there is nospace between -I and the path.

       This is a kbuild limitation:  there must be no space present.

       (注意这里的赋值在路径与-l之间没有空格。没有这个空格是kbuild的限制)

 

---5.3 External modules using several directories

 

       If an external module does not follow theusual kernel style, but

       decides to spread files over severaldirectories, then kbuild can

       handle this too.

(如果一个外部模块不遵循内核的一贯做法,把它的文件放到各种目录上,kbuild亦可以处理这种情况)

 

       Consider the following example:

 

       |

       +- src/complex_main.c

       |  +- hal/hardwareif.c

       |  +- hal/include/hardwareif.h

       +- include/complex.h

 

       To build a single module namedcomplex.ko, we then need the following

       kbuild file:

       (去编译一个叫complex.ko的模块,需要下面的kbuild文件)

 

       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 fileslocated in another directory -

       although this is NOT recommendedpractice. The syntax is to specify

       the directory relative to the directorywhere the Kbuild file is

       located.

(kbuild知道怎么去处理在其他目录.o文件虽然这不是推荐的做法。语法上最好是指定放在kbuild所在目录的棉裤路)

 

       To find the .h files, we have toexplicitly 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 filesusing absolute paths.

       $(src) will specify the absolute path tothe directory where the

       Kbuild file are located when being buildas an external module.

       Therefore -I$(src)/ is used to point outthe directory of the Kbuild

       file and any additional path are justappended.

(为了找到.h文件,我们必须去清晰的告诉kbuild哪里去寻找相关.h文件。当kbuild执行时候,当前目录就是内核树的根目录,因此我们必须去告诉kbuild怎么去使用绝对路径去找到.h

$(src)指定的是建立一个外部模块的时候,当前kbuild文件所在目录的绝对路径)

 

===6. Module installation

 

Moduleswhich are included in the kernel are installed in the directory:

(在内核里包含的模块会被安装到下面的目录里)

 

       /lib/modules/$(KERNELRELEASE)/kernel

 

Externalmodules are installed in the directory:

(外部模块会被安装到下面的目录)

 

       /lib/modules/$(KERNELRELEASE)/extra

 

---6.1 INSTALL_MOD_PATH

 

       Above are the default directories, but asalways, some level of

       Customization(定制) ispossible. One can prefix the path using the variable

       INSTALL_MOD_PATH:

(上面都是默认的安装目录,不过通常的一些级别上是可以定制的。一种方法就是使用预编译路径变量INSTALL_MOD_PATH)

 

              $ make INSTALL_MOD_PATH=/frodomodules_install

              就是安装在 Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel

 

       INSTALL_MOD_PATH may be set as anordinary shell variable or as in the

       example above, can be specified on thecommand line when calling make.

       INSTALL_MOD_PATH has effect both wheninstalling modules included in

       the kernel as well as when installingexternal modules.

(INSTALL_MOD_PATH如上述例子中,当做通用的shell变量来设置,可以在make的时候在命令行指定。

INSTALL_MOD_PATH在安装内核的模块以及安装外部模块时候起作用)

 

---6.2 INSTALL_MOD_DIR

 

       When installing external modules they areby default installed to a

       directory under/lib/modules/$(KERNELRELEASE)/extra, but one may wish

       to locate modules for a specificfunctionality in a separate

       directory. For this purpose, one can useINSTALL_MOD_DIR to specify an

       alternative name to 'extra'.

(当安装外部模块时默认安装在/lib/modules/版本号/extra,但一种方式是可以安装一个提供特定功能的模块到一个独立目录的。这种方法可以使用INSTALL_MOD_DIR去指定一个extra替代的目录)

 

              $ make INSTALL_MOD_DIR=gandalf -CKERNELDIR \

                     M=`pwd`modules_install

              就是安装在 Install dir:/lib/modules/$(KERNELRELEASE)/gandalf

      

      

===7. Module versioning & Module.symvers

 

Moduleversioning is enabled by the CONFIG_MODVERSIONS tag.

(模块的版本选择使用CONFIG_MODVERSIONS来使能)

 

Moduleversioning is used as a simple ABI consistency check. The Module

versioningcreates a CRC value of the full prototype for an exported symbol and

whena module is loaded/used then the CRC values contained in the kernel are

comparedwith similar values in the module. If they are not equal, then the

kernelrefuses to load the module.

(模块版本选择使用ABI(applicationbinary interface)标准。模块版本选择会新建一个关于整个输出原型的CRC校验值,在模块被加载/使用的时候CRC会被包含进内核然后去跟模块的CRC值进行比较。如果两个值不相等,内核会拒绝加载模块)

 

Module.symverscontains a list of all exported symbols from a kernel build.

(module.symvers包含了一个关于内核编译的一个关于所有输出原型的表(通俗说就是包含着所有函数或者变量等等的表))

 

---7.1 Symbols from the kernel (vmlinux + modules)

 

       During a kernel build, a file namedModule.symvers will be generated.

       Module.symvers contains all exportedsymbols from the kernel and

       compiled modules. For each symbols, thecorresponding CRC value

       is stored too.

(在内核编译期间,module.symvers会被创建。Module.symvers包含了所有内核和已编译模块的输出原型。对于每个原型,相应的CRC都会有植入进去)

 

       The syntax of the Module.symvers file is:

              <CRC>       <Symbol>           <module>

       Sample:

              0x2d036834  scsi_remove_host   drivers/scsi/scsi_mod

 

       For a kernel build withoutCONFIG_MODVERSIONS enabled, the crc

       would read: 0x00000000

       (如果在内核编译中不使用CONFIG_MODVERSIONS,crc会被读出来是0x00000000)

 

       Module.symvers serves two purposes:

       1) It lists all exported symbols bothfrom vmlinux and all modules

       2) It lists the CRC if CONFIG_MODVERSIONSis enabled

       (module.symvers提供两个目的:

1.   列出所有vmlinux和所有模块的所有原型。

2.   如果使用CONFIG_MODVERSIONS,列出CRC)

 

---7.2 Symbols and external modules

 

       When building an external module, thebuild system needs access to

       the symbols from the kernel to check ifall external symbols are

       defined. This is done in the MODPOST stepand to obtain all

       symbols, modpost reads Module.symversfrom the kernel.

       If a Module.symvers file is present inthe directory where

       the external module is being built, thisfile will be read too.

       During the MODPOST step, a newModule.symvers file will be written

       containing all exported symbols that werenot defined in the kernel.

(当编译一个外部模块的时候,编译系统需要知道内核的各个原型从而确认是否所有外部原型都被定义了。这个步骤在MODPOST完成,为了获取所有原型,modpost会读取内核的module.symvers。如果module.symvers文件已经在编译好的外部模块的目录,这个文件也会被读取。

在MODPOST步骤,一个新的module.symvers文件会被写入,包含着所有的不在内核定义的输出原型)

 

---7.3 Symbols from another external module

 

       Sometimes, an external module usesexported symbols from another

       external module. Kbuild needs to havefull knowledge on all symbols

       to avoid spitting out warnings aboutundefined symbols.

       Three solutions exist to let kbuild knowall symbols of more than

       one external module.

       The method with a top-level kbuild fileis recommended but may be

       impractical in certain situations.

(有时候,一个外部模块使用者其他外部模块的原型。Kbuild需要对所有的原型都有认识,以去避免报出未定义相关的警告。

有三那个方法去让kbuild知道所有原型。

推荐用顶层Kbuild文件的方法,但还多情况都说不准。)

 

       Use a top-level Kbuild file

              If you have two modules: 'foo' and'bar', and 'foo' needs

              symbols from 'bar', then one canuse a common top-level kbuild

              file so both modules are compiledin same build.

       (使用顶层Kbuild文件

如果你有foo和bar两个模块,foo需要bar的原型,可以使用顶层kbuild,这样就能让两个模块都在同一个编译中了。)

 

              Consider following directory layout:

              ./foo/ <= contains the foomodule

              ./bar/ <= contains the barmodule

              The top-level Kbuild file wouldthen look like:

 

              #./Kbuild: (this file may also benamed Makefile)

                     obj-y := foo/ bar/

 

              Executing:

                     make -C $KDIR M=`pwd`

 

              will then do the expected andcompile both modules with full

              knowledge on symbols from bothmodules.

              (这将有做上述希望的效果,而且会编译这两个模块所有的原型)

 

       Use an extra Module.symvers file

              When an external module is built,a Module.symvers file is

              generated containing all exported symbolswhich are not

              defined in the kernel.

              To get access to symbols frommodule 'bar', one can copy the

              Module.symvers file from thecompilation of the 'bar' module

              to the directory where the 'foo'module is built.

              During the module build, kbuildwill read the Module.symvers

              file in the directory of theexternal module and when the

              build is finished, a newModule.symvers file is created

              containing the sum of all symbolsdefined and not part of the

              kernel.

       (使用额外的module.symvers文件

当一个外部模块被编译时候,会生成一个包含着所有不在内核定义的输出原型的module.symvers文件。

为了知道bar里面的原型,一种方法是可以复制bar模块编译生成

的module.symvers文件,复制到编译foo模块的目录

在模块编译期间,kbuild会读外部模块的module.symvers文件,当编译完成后新的Module.symvers文件会被创建并包含着所有不在内核里的原型)

 

       Use make variable KBUILD_EXTRA_SYMBOLS inthe Makefile

              If it is impractical to copyModule.symvers from another

              module, you can assign a spaceseparated list of files to

              KBUILD_EXTRA_SYMBOLS in yourMakfile. These files will be

              loaded by modpost during theinitialisation of its symbol

              tables.

       (在makefile使用make一个叫做KBUILD_EXTRA_SYMBOLS的变量。

如果上述方法拷贝一个module.symvers到另一个模块是不切实际的,可以分配一个空间给一个单独的关于文件的表给KBUILD_EXTAR_SYMBOLS。这些文件会在原型表初始化时候被modpost加载)

 

 

===8. Tips & Tricks

 

---8.1 Testing for CONFIG_FOO_BAR

 

       Modules often need to check for certainCONFIG_ options to decide if

       a specific feature shall be included inthe module. When kbuild is used

       this is done by referencing the CONFIG_variable directly.

(模块经常需要测试,为了确认CONFIG_选项去决定是否需要包含一个指定的功能进模块里。当kbuild被使用时,这个工作已经被引用CONFIG_变量这个操作完成。)

 

              #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 usedgrep to check for specific

       CONFIG_ settings directly in .config.This usage is broken.

       As introduced before, external modulesshall use kbuild when building

       and therefore can use the same methods asin-kernel modules when

       testing for CONFIG_ definitions.

(外部模块传统的使用grep(检索目标指令)去确认指定的CONFIG_设置是否存在于.config文件。这个做法已经过时了。

如上述介绍所示,当编译时候外部模块使用kbuild,这样就可以使用跟内核模块一样的方法去测试CONFIG_的定义。)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值