Linux driver MAKEFILE file Analysis

Source: Internet
Author: User

 

Linux Kernel is a single kernel, but it is flexible and convenient to develop by dynamically loading modules. So how does it compile the kernel? We can start by analyzing its makefile. The following is a simple makefile for the hello kernel module.

ifneq ($(KERNELRELEASE),)obj-m:=hello.oelseKERNELDIR:=/lib/modules/$(shell uname -r)/buildPWD:=$(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:rm -rf *.o *.mod.c *.mod.o *.koendif

 

When we write a hello module, we only need to use the above makefile. Then make it. Suppose we put the source code of the hello module under/home/study/prog/MoD/Hello. How does make run when we run make in this directory? In Chapter 2 and section 4 "Compilation and loading" of ldd3, The makefile is executed twice. But what is the specific process?

First, because make has no targets, make will execute the first target in makefile not starting with "." as the default target. So default becomes the target of make. Make will execute $ (make)-C $ (kerneldir) M = $ (PWD) Modules shell is the internal function of make. Suppose the current kernel version is 2.6.13-study, therefore, the result of $ (shell uname-R) is 2.6.13-study.

make -C /lib/modules/2.6.13-study/build M=/home/study/prog/mod/hello/ modules

/Lib/modules/2.6.13-study/build is a symbolic link pointing to the kernel source code/usr/src/Linux. It can be seen that make is executed twice. The first execution is to read the makefile in the directory/home/s tudy/prog/MoD/Hello/where the source code of the hello module is located. The second execution is when makefile under/usr/src/Linux/is executed.

But there are still a lot of confusing questions: 1. This kernelrelease is also very confusing. What is it? This variable is not defined in/home/study/prog/MoD/He llo/makefile, so else... Endif. However, if you move the hello module to the kernel source code. For example, put it in/usr/src/Linux/driver/, and the kernelrelease is defined. In/usr/src/Linux/makefile, 162 kernelrelease =$ (Version). $ (patchlevel). $ (sublevel) $ (extraversion) $ (localversion)
At this time, the hello module is no longer compiled with make, but compiled with make modules in the kernel. In this way, the makefile can work normally during separate compilation and as part of the kernel compilation.

2. When will this obj-M: = Hello. o be executed? Run the following command:

make -C /lib/modules/2.6.13-study/build M=/home/study/prog/mod/hello/ modules

Make to/usr/src/Linux/makefile to find the target modules: 862. phony: modules 863 modules: $ (vmlinux-dirs) $ (if $ (kbuild_builtin), vmlinux) 864 @ echo 'Building modules, stage 2. '; 865 $ (q) $ (make)-rr-F $ (srctree)/scripts/makefile. modpost

We can see that there are two stages: 1. Compile the hello. o file. 2. generate hello. mod. O hello. ko calls make-F scripts/makefile in this process. build OBJ =/home/study/prog/MoD/Hello while in scripts/makefile. build contains many files: 011-include. config 012 013 include $ (if $ (wildcard $ (OBJ)/kbuild), $ (OBJ)/kbuild, $ (OBJ)/makefile)
Here,/home/study/prog/MoD/Hello/makefile exists. So the execution is: obj-M: = Hello. o

For more detailed process of make modules, you can find it in the notes of the scripts/makefile. modpost file. To view the entire execution process of make, run make-n.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.