Linux驱动模块编译

本文详细解读Makefile在内核模块开发中的应用,包括变量设置、目标文件定义、模块编译与装载、清理操作,以及.kbuild中的关键配置。理解makefile逻辑和modules.txt的作用,助力Linux模块开发实践。
摘要由CSDN通过智能技术生成

1.makefile

1.1.modules模块上层makefile

 1.2.内核模块makefile

2. makefile解析

2.1 定义变量:

KERNELDIR内核top makefile所在路径。

CURRENT_PATH模块所在的文件夹路径。

ATOS_KCROSS_COMPILE定义编译工具链。

ATOS_ARCH定义当前的目标架构平台。

INCLUDE内核中应用的头文件所在的路径。

EXTRA_CFLAGS表示在编译模块时,需要添加的目录。编译器会从这些目录中找到所需要的头文件。

补充:如果在模块中引用了其他模块导出到内核符号表的函数,则需要包含该模块产生的符号表文件Module.sysvers。例如,modules模块中如果引用了同级文件夹中另一模块的函数则需要在modules模块makefile增加这一行定义:KBUILD_EXTRA_SYMBOL=$(obj)/../add_sub/Module.symvers

2.2 指定编译的目标文件

obj-m:=peripheral_modules.o表示把文件peripheral_modules.o作为"模块"进行编译,不会编译到内核,但是会生成一个独立的 "peripheral_modules.ko" 文件;

如果有一个模块名为 peripheral_modules.ko, 是来自多个源文件(如 file1.c 和 file2.c ), 正确的书写应当是:

obj-m := peripheral_modules.o

peripheral_modules-objs := file1.o file2.o

第19行即是定义模块peripheral_modules.ko 。

2.3 模块编译

第10行 ~ 11行,编译模块。

2.4 模块装载

第12行 ~ 13行,modules_install指令是把编译的ko拷贝到根目录下的/lib/modules/4.1.15/目录中。指定的安装目录主要是为了放置需要安装的镜像和map(符号表)文件,系统的启动需要这些文件的参与。

2.5 清除编译中间文件和编译结果

第15行 ~ 16行,modules_install指令是清除编译中间文件和编译结果。相当于以下指令:

rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order

2.6 PHONY

.PHONY是一个伪目标,可以防止在Makefile中定义的目标和工作目录下的实际文件出现名字冲突,提高执行makefile时的效率。

2.6 makefile执行逻辑

这个 makefile 在一次执行中要被读 2 次。

KERNELRELEASE是在内核源码的顶层Makefile中定义的一个变量,在第一次读取执行此Makefile时,KERNELRELEASE没有被定义,所以make将读入、执行ifeq之后的内容,如果make的目标是clean,直接执行clean操作,然后结束。当make的目标为all时,-C $(KERNELDIR)指明跳转到内核源码目录下读取那里的Makefile;M=$(CURRENT_PATH) 表明返回到当前目录继续读入、执行当前的Makefile。当从内核源码目录返回时,KERNELRELEASE已被定义,make将继续读取else之后的内容。else之后的内容为kbuild语法的语句,指明模块源码中各文件的依赖关系,以及要生成的目标模块名,kbuild被启动去解析kbuild语法的语句。obj-m := peripheral_modules.o表示编译连接后将生成peripheral_modules.ko模块。

3. obj-m等在/kbuild/modules.txt中的定义

3.1  obj-m := <module_name>.o

在modules.txt中提到:

The 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 is

needed listing the files:

<module_name>-y := <src1>.o <src2>.o ...

3.2  -C $KDIR and M=$PWD

-C $KDIR

The directory where the kernel source is located.

"make" will actually change to the specified directory

when executing and will change back when finished.

M=$PWD

Informs kbuild that an external module is being built.

The value given to "M" is the absolute path of the

directory where the external module (kbuild file) is

located.

3.3  modules

When 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 current

directory, so a target does not need to be specified. All

output files will also be generated in this directory. No

attempts are made to update the kernel source, and it is a

precondition that a successful "make" has been executed for the

kernel.

modules

The default target for external modules. It has the

same functionality as if no target was specified. See

description above.

3.4  modules_install

Install the external module(s). The default location is

/lib/modules/<kernel_release>/extra/, but a prefix may

be added with INSTALL_MOD_PATH (discussed in section 5).

3.5  clean

Remove all generated files in the module directory only.

3.6  help

List the available targets for external modules.

参考:https://blog.csdn.net/u012247418/article/details/83684214

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值