A brief analysis of the Linux Kernel makefile make Zimage

Source: Internet
Author: User

This article will briefly analyze the process of Linux kernel compiling zimage. The reader needs to have GNU make, Bash Shell, Python feet

The basics of this, compiler, linker, and more. Although the focus is on the analysis of the construction process of kernel, but also in passing analysis of some

Other small points of knowledge. We take a bus to travel, appreciate the scenery along the way, will not hinder our final arrival at our destination, not

It? 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0061.gif "alt=" J_0061.gif "/>


Describe the specific development environment first:

. Host Os:ubuntu 14.04 Server LTS

. Cross Toolchain:crosstool-ng generated crossover compiler

. Target:tegra, JESTON-TK1

. kernel:3.10.40


Before you begin the analysis, make the necessary notes:

. During the parsing process, all file paths are relative to the kernel source root directory.


OK, next we'll start analyzing the zimage compilation process.

Start the terminal and run the command in the kernel source root directory . envsetup.sh Set up the compilation environment. The contents of envsetup.sh such as

Under

#/bin/bash## Filename:envsetup.shexport Arch=armexport cross_compile=~/tegra/tk1/crosstool/crosstool-ng/install/ bin/arm-cortex_a9-linux-gnueabi-

Then run the command make zimage o=output compile kernel. Make reads the makefile under the kernel source root directory and sees

The following code fragment:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/AD/wKiom1mD4wCgi3kiAADVybJtaiQ952.png "title=" 1.png "alt=" Wkiom1md4wcgi3kiaadvybjtaiq952.png "/>

Figure 1

This will first determine if the value of kbuild_src is empty, because KBUILD_SRC is not yet defined, so this is a judgment.

The kbuild_output is then assigned the value of the variable O passed from the command line, namely:

Kbuild_output: = $ (O) # Kbuild_output: = OUTPUT

The accompanying variable saved-output is also assigned the same value output, and then the value of Kbuild_output is updated to the end of the output directory

The entire path. kbuild_output is the directory used to store the output generated by the compilation.

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/AE/wKiom1mD50HjdmQmAAArOjHGcZ8930.png "title=" 1.png "alt=" Wkiom1md50hjdmqmaaarojhgcz8930.png "/>

Figure 2

This is a very important fragment and is the beginning of the final goal of building. The code snippet in Figure 2 above is now expanded to:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/AE/wKiom1mD6ZDxg6TgAAAiFxKN4yQ160.png "title=" 1.png "alt=" Wkiom1md6zdxg6tgaaaifxkn4yq160.png "/>

Figure 3

Please note that /path/to/output and /current/path are not real directory names, depending on the actual path of the kernel source root directory. /current/path The value at this time is the path where the kernel source root directory resides.

Finally, we see zimage , the ultimate goal of our compilation, which is dependent on sub-make . But remember, make is looking at

To the Zimage rule and its dependent target rules, the two rules will not be executed immediately, but only the first zimage and

Sub-make of the dependency relationship. Why is that? The original make execution is a 2-pass scanning process, which is similar to some compilers

The compilation process, not once in place, but divided into several stages. Make is only going to create the variable at the time of the first scan (and

Expand certain types of variables), establish the target dependency tree, and only the second time will be to follow the rules to target the creation of

Construction process. So here the Zimage and Sub-make rules are not executed immediately, but only in making the corresponding records, and

Then continue scanning makefile the rest of the content.

Here is a very important variable skip-makefile, which is assigned a value of 1, which is closely related to our next analysis. I

Keep looking at the rest of the makefile:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/AE/wKioL1mD7kWAqEh6AAAu6dsVt64465.png "title=" 1.png "alt=" Wkiol1md7kwaqeh6aaau6dsvt64465.png "/>

Figure 4

Here with ... Omitted a lot of content, the middle content is very important and core, but for our current 1th time is to be skipped, because

For this time, the value of skip-makefile is 1, so ifeq ($ (skip-makefile)) is not tenable, and the content contained therein is naturally

Skip over.

Force This special empty rule, in kernel all the makefile, can see its figure at any time, so it is necessary to simple

to explain its purpose. Force is common at the end of a rule-dependent list, which is to ensure that the target that relies on it is always updated. Reason

is force does not correspond to any actual files (. Phony pseudo-target) and does not rely on any of its targets, make will assume that such targets are always updated

, the target that depends on it will naturally be updated.

With a spirit, our play is coming soon. Make completes the 1th series scan of the kernel top makefile, the next step

into make to the 2nd stage of the Makefile process, the dependency tree built on stage 1 begins to build our ultimate goal step-by-step

Zimage. The starting point is the position shown in Figure 2 , we look at Figure 2 after the expansion of Figure 3,zimage dependent on sub-make, so sub-make it

Rule . Let's take a look at the expanded content of the sub-make rule:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9D/B1/wKioL1mEEnvRuhI_AAAOFnVV2Po492.png "title=" 1.png "alt=" Wkiol1meenvruhi_aaaofnvv2po492.png "/>

Figure 5

What exactly did this rule do? This rule completes 4 actions:

. Make has switched its current directory to /path/to/output

Set KBUILD_SRC to the current directory (that is, the kernel source root directory)

. Make will reread kernel's top-level makefile (that is, the makefile we are currently executing)

. The tectonic target is still zimage.

Here you may have questions, how come back to the makefile we are executing? It's not a dead loop, it's endless.

, huh? That is not the case. Let's take a step-by-step explanation of why. After executing the statement for the sub-make rule, make re-reads from the beginning

Kernel's top makefile, time passes, make's footsteps go back to the code snippet shown in Figure 1, where the conditions branch

Whether the value of the broken kbuild_src is empty. Do you remember? Just before we re-entered the makefile, we had set the kbuild_src

The value is the path of the kernel top-level makefile (also the path where the kernel source root is located), so this is not true for kbuild_src , and the statements within the natural branch will not be executed. Therefore, there are different execution paths when executing this makefile with the 1th pass.

Make continues to the code snippet shown in Figure 4 to determine where we're going next, based on the value of Skip-makefile.

And this time,skip-makefile is not defined, so ifeq ($ (skip-makefile), ) is determined, so make will execute

The code fragment contained by the conditional branch, which will makefile the core content of our entire top-level analysis. There is a lot of content in this, we

Segment-By-section analysis, and fragment interception of code fragments is also piecemeal.

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/B2/wKioL1mEG8iS9E_CAACQLCVfCvw612.png "title=" 1.png "alt=" Wkiol1meg8is9e_caacqlcvfcvw612.png "/>

Figure 6

This code works very simply and completes several variable settings. In my development environment, these variables were given the following

Value (as for how to get, please crossing yourself analysis):

Srctree =/path/to/kernel/source/baseobjtree =/PATH/TO/KERNEL/SOURCE/BASE/OUTPUTSRC =/path/to/kernel/source/ Baseobj =/path/to/kernel/source/base/outputvpath =/path/to/kernel/source/basesubarch = x86

It is worth noting thatSrctree objtree VPATH was export. Keep looking down:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/B3/wKioL1mEH8HBaE3HAAC-Pc2z7RU326.png "title=" 1.png "alt=" Wkiol1meh8hbae3haac-pc2z7ru326.png "/>

Figure 7 (previous Figure 6)

This code defines a number of variables that will get the values listed below:

ARCH = Armcross_compile = ~/tegra/tk1/crosstool/crosstool-ng/install/bin/arm-cortex_a9-linux-gnueabi-uts_machine = Armsrcarch = Armhdr-arch = Armkconfig_config =. Configconfig_shell =/bin/bash

The importance of these variables, I do not think I have to say more. The road is long, the revolution has not been successful, we continue to travel.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9D/B4/wKioL1mEJpDyCyTMAAAMCLlnavo274.png "title=" 1.png "alt=" Wkiol1mejpdycytmaaamcllnavo274.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/B4/wKioL1mEJu6QSiNZAAB47JgyDmw161.png "title=" 1.png "alt=" Wkiol1meju6qsinzaab47jgydmw161.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/9D/B4/wKiom1mEJ2-SpFFiAACOYi-rPXE986.png "title=" 1.png "alt=" Wkiom1mej2-spffiaacoyi-rpxe986.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/B4/wKioL1mEJ9yhOXV7AACNTYTjCYg654.png "title=" 1.png "alt=" Wkiol1mej9yhoxv7aacntytjcyg654.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/B4/wKiom1mEJreyY7C8AAADf0elG3w556.png "title=" 1.png "alt=" Wkiom1mejreyy7c8aaadf0elg3w556.png "/>

(not to be continued)

This article is from the "Jimokuangxiangqu" blog, make sure to keep this source http://4594296.blog.51cto.com/4584296/1953612

A brief analysis of the Linux Kernel makefile make Zimage

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.