2.4 Clerk (ARM Edition) Makefile Analysis

xiaoxiao2021-03-06  46

First, the classification of the Makefile file in the kernel source code

Makefiles in the Linux-Roy kernel source code (excluding dynamically generated .flags file) is mainly divided into the following four categories:

1. Makefile under the main directory (may be called main-makefile)

It mainly has two functions: generate VMLinux (kernel images) and modules.

2. Rules.make under the main directory

General rules are defined in Rules.make for main-makefile and subdir-makefiles calls.

The variable subdir-y is a directory for building a VMLinux (kernel), and the variable subdir-m is a directory for building modules; Subdir-n and Subdir-in the directory will not participate in the build.

The variable OBJ-Y is the list of target files for the kernel, and it is assigned to each subDIR-Y. Such as: LIB / Makefile's line 13-14. Variable OBJ-M is a list of target files for each module, and it is assigned to each subDIR-M. As in DRIVERS / Makefile, the SubDIR-Y is assigned to SubDIR-Y at line 13, and this list is copied to subdir-m on page 14. Subdir-y and subdir-m are started from the 17th line, and the establishment of these two lists is completed. All goals in OBJ-N and OBJ- will be ignored.

3. Makefile under Arch / * / directory (may be called Arch-makefiles)

Arch-makefiles is related to a specific architecture that is Makefiles for a particular CPU.

4. Makefile under the submit outside ARCH (may be called subdir-makefiles)

Subdir-makefiles is only responsible for generating target files in their own directory. They accept the information from the upper Make passed, and construct a list of files that require compilation and submitted to Rules.make. Subdir-makefiles is relatively short because universal rules are defined in Rules.make. The main job of general subDir-makefiles is to assign an OBJ- * or SUBDIR- * variable.

Second, the process of built in the core and module

The dynamic process of the kernel compilation is analyzed below.

Step 1: make arch = arm menuconfig

Definition of target MenuConfig at No. 306 of Main-makefile. Main-makefile's 308th line adjustment MenuConfig tool (Scripts / MenuConfig), let it generate .config files in the main directory according to the contents of the Arch / $ (Arch) /Config.in file (Main-makefile, 37) Row).

Step 2: make Arch = arm cross_compile = arm-linux- dep

INCLUDE / Linux / AutoConf.h is generated according to the .config file, and its content is a number #define and #undef macro. These macro notification compilers which components do not need to be compiled, is it to compile into the kernel or compiled into a module. The variables in these two files are corresponding to each other. For example, the fourth line of .config is:

Config_arm = y

This is the case of compiling into the kernel. Accordingly, the 5th line of INCLUDE / Linux / AutoConf.h is:

#define config_arm 1

Examples of compilation into modules See. 1166 lines of Config:

Config_binfmt_aout = m

Accordingly, the 217th and 218 rows of AutoConf.h are: #undef config_binfmt_aout

#define config_binfmt_aout_module 1

Next, Main-makefile calls the SPLIT-INCLUDE tool (Scripts / split-incrude.c) (MAIN-MAKEFILE), and segments the AUTOCONF.H and generates the corresponding header file in the include / config directory. The path of AutoConf.h passes the first parameter to split-include (Scripts / split-incdude.c on section 71), according to the contents of the 5th line mentioned by AutoConf.h, Split-Include Tools in Include / config / directory generates ARM.H file, its content is one line:

#define config_arm 1

Include / Linux / config.h's fourth line embedded in AutoConf.h, and many C files in the kernel source were embedded in this config.h (such as the row of Kernel / Printk.c), so indirect embedded AutoConf. H, it can be seen that CONFIG.H is a bridge.

In this step, Makefile will call the MKDEP tool (scripts / mkdep.c) to generate two files containing dependencies: .hdepend and .depend.

.hdepend only one, under the home directory, it represents the corresponding header files depend on which other header files are dependent. . HDepend is generated after the implementation of the 496th line of Main-makefile. If there is. HDepend file, Rules.make will embed it (Rules.make No. 295-297).

The .depend file has a lot, it represents the corresponding target files depend on which source files (* .c) and header file (* .h). The.depend file under the main directory is generated after the 497th line of Main-makefile. MKDEP reads * .c files to analyze the files they rely on. The following is an example of MM / PAGE_ALLOC.C. Page_alloc.c's 15th ("..." means omitted):

#include

#include

...

#include

MKDEP will write in line 110-118 in mm / .depend:

Page_alloc.o: Page_alloc.c /

/usr/src/arm-linux/linux-roy/include/linux/mm.h /

...

/usr/src/arm-linux/linux-roy/include/linux/module.h /

But the next line 119 and the above ways are somewhat different:

$ (Wildcard /usr/src/arm-linux/linux-roy/include/config/discontigmem.h) /

This is because the contents of the 244th line in Page_alloc.c are as follows:

#ifndef config_discontigmem

This needs to determine the existence of discontigmem.h, and the 119th line of the.depend file in the MM directory is played with such a role (if there is, return the file name, if there is no existence, return empty).

Step 3: make Arch = arm cross_compile = arm-linux- clean

Target Clean defines line 444-444-449 of Main-Makefile, which is assigned to the file and directory list (clean_files and clean_dirs) assigned to 203-229 rows.

Step 4: make Arch = arm cross_compile = arm-linux- zimage

Make zimage will generate a kernel compressed with Gzip. For the ARM type CPU, the target zimage is defined in the 146th line of Arch / ARM / Boot / Makefile. It relies on Compressed / VMLinux. At Chapter 152, the compressed / vmlinux relies on VMLinux in the main directory. Its generation rules are in Main-makefile, 281-289:

VMLinux: include / Linux / Version.h $ (configuration) init / main.o init / version.o /

INIT / DO_MOUNTS.O LinuxSubdirs

$ (LD) $ (LINKFLAGS) $ (head) init / main.o init / version.o init / do_mounts.o /

--Start-Group /

$ (More_files) /

$ (Drivers) /

$ (NetWorks) /

$ (LIBS) /

--end-group /

-o vmlinux

Zldflags is defined in the 14th line of Arch / ARM / Boot / Makefile. If we specify Cross_Compile as ARM-Linux-, LD is ARM-Linux-Ld (MAIN-Makefile 29). LinkFlags Definitions in the 10th line of Arch / Arm / Makefile:

LinkFlags: = - p-x -t arch / arm / vmlinux.lds

It specifies the link script as Arch / ARM / VMLinux.lds.

Make starts from the primary directory Makefile, obtains variables and dependencies that are independent of architecture, and obtain information such as system-specific variables from Arch-Makefiles, which inherit and extend the variables provided by the primary Makefile. First, all variables assigned by ": =" are assigned during this process, then all variables assigned by "=" assignment (actually similar to macro replacement). The main-makefile then hands the Makefile in the subdirectory, and passes the partial variable to the child catalog Makefile. At this point, the subdirectalog Makefile that built the kernel is determined to compile which source files according to the configuration information, and build a list of files that require compilation. When Make Zimage, Make will recursively enter the list of list subDir-y, determine the compilation method of these files according to its defined compilation rules. Then, Make will generate a series of target files based on the dependency tree, and finally link these target files into VMLinux through the link script according to the generation rules of VMLinux (lines 281-289). (Such as Arch / ARM / Makefile 267th line).

Step 5: Make Arch = arm cross_compile = arm-linux- modules

Make Module is used to generate kernel modules (modules), which is defined in Main-makefile 388-389 lines. When Make Modules, Make will recursively enter the list of lists SubDIR-M.

VMLinux:

Step 6: make arch = arm cross_compile = arm-linux- install_mod_path = modules_installmake module is used to install the kernel module, and its rules are defined in main-makefile 396 lines.

Third, a few points

Main-makefile's 37th line is:

Makefiles = $ (TOPDIR) /. Config

Makefiles is an environment variable if you define makefiles in your current environment, then make the value in this variable to an Include action. The value in this variable is the other makefile, separated by spaces. However, it is different from the include that the target in the Makefile introduced from this environment will not work. If the file defined in the environment variable is discovered, Make will ignore. Makefiles in the subdirectory and Rules.make are not embedded .config files, they are passing the MakeFiles variables by main-makefile to get relevant information.

The work done in the 48-67 line of Main-makefile is this: First of all the ALL is defined as the first target, then judge the .config file and .depend file in the 55-58 row, if the existence is included Come in. If the .config file does not exist, the DO-IT-ALL is set to Config (line 66). The target Conifg is defined in line 310. If the .config file does not exist, the DO-IT-ALL is set to Depend (line 62). Target depend is defined in line 506. After executing make, the profile is generated in the primary directory .config (if there is a .config file, it is renamed .config.old).

MakemrProper is used to rebuild the kernel. It clears the job more thorough than Make Clean: In addition to Makeclean's work, it is necessary to delete .config, .depend and other files, restore the core source code to the most state. It is defined in line-makefile (MRPROPER_FILES, and MRPROPER_DIRS) assigned to 232-256 lines of files and directory lists.

The recursive make must use the Make variable instead of using the make command. Such as Main-makefile 279th line:

@ $ (Make) CFLAGS = "$ (cflags) $ (cflags_kernel)" -c Arch / $ (arch) / boot

When using the "-c" parameter to make, "- w" parameters will be opened automatically, ie processing makefile

Before and afterwards, the working directory is displayed, so you will see repeated ENTERING DIRECTORY [Some Dir] and Leaving Directory [some dir] on the screen.

The role of a pair of anti-quotes (`) corresponds to the shell function, the content inside is the system shell command (such as the No. 496 of Main-Make).

If the first step in compilation is XConfig, see the definition of line 302. In XConfig, the TK script under the Scripts directory is used to form a graphical interface to form a graphical interface for user configuration.

In the second step in compilation, why should I establish a dependency through the MKDEP and SPLIT-INCLUDE tools instead of adding the -mm parameter through GCC? The above mentioned, many C source files are embedded in autoconf.h through Linux / config.h, if the file-dependent relationship (.depend) is established in accordance with usual method, as long as AutoConf.h is updated, all source code will cause all source code. Rebate. To optimize the Make process, reduce unnecessary recompilation, Linux has developed a dedicated MKDEP tool, with it to generate the .depend file. When the kernel is configured, split-incrude will check the contents of the old subfiles, and determine whether it is to update them. This way, regardless of the date of autoconf.h modification, as long as it is configured, make will not recompile the kernel. Rules.make is called all the target files in the current directory as o_target, and its generation rules are in line 96-102, eventually generate a series of .o files; refer to all library files in the current directory called l_target, its generation Rule 114-116, eventually generate a series of .a files. Note that the 104-107 lines, 118-121 lines, 279-282 rows and 304-317 rows have realized a mechanism for incremental compilation. Make generates a .flags file per compiled a source file. For example, when compiling mm / swap.c, it will be generated in the same directory. Swap.Flags file. It is also a Makefile file. The function of its code is to test whether the current compilation option is made by the last time. If the change has changed his corresponding target file to the Files_Flags_up_to_date list. Generating this file is for the next compilation. When you compile it next time, when Make enters a subdirectory, you will search for the .flags file, if there is, it is embedded (line 309-312). Make will get a list of files_flags_up_to_date, and its content is no need to update the target. Thus, the system deletes them from the compilation object table, so that the Files_Flags_Changed list (line 314-317), the content of this list is the need to update.

MAIN-MAKEFILE line 281-289 rows to define the generation (link) rules for VMLinux. Vmlinux is composed of Head, Main.o, Version.o, Core_Files, Drivers, Networks, and Libs. Among them, Core_Files, Networks, Drivers, and Libs are defined in main-makefile, which is expanded by Arch-Makefiles. Head defines in Arch-makefiles (such as Arch / ARM / Makefile, para. 193). The target file in the HEAD list needs to be first linked to VMLinux.

The link script (* .lds file) is generated by * .lds.in file, for ARM, is VMLinux-Armo.lds.in. The 269-274 line of Arch / ARM / Makefile defines the .lds generation rules. Vmlinux-Armo.lds.in is actually a link script template, but the relevant parameters are not determined. The 274th line of Arch / ARM / Makefile is to complete the "Fill" work (for TextAddr, DataAddr, etc.), and save the full information to Arch / ARM / VMLinux.lds.

转载请注明原文地址:https://www.9cbs.com/read-78544.html

New Post(0)