Kernel image formation-kbuild System (1)

Source: Internet
Author: User
Document directory
  • 2.1.1 kernel Objectives
2.1 kbuild System

Since Linux kernel 2.6, kbuild is used for compiling the Linux kernel, which is very different from the previous compiling system, especially for compiling the Linux kernel module. In the new system, the Linux compiling system will scan the Linux makefile twice: first, the compiling system will read the makefile at the top layer of the Linux kernel, then, compile the Linux Kernel Based on the read content and read the makefile of kbuild for the second time.

 

Kbuild is a compilation system built on the gun make mechanism. To understand the kbuild system, you must first understand gun make, this is why we need to talk about makefile preparation in the previous section.

 

The makefile used in the kbuild System of the Linux kernel is divided into five parts:

 

(1) Top-level makefile

 

The kernel makefile is located in the top-level directory of the Linux kernel source code, also called top makefile. It is mainly used to specify the Linux kernel target file (vmlinux) and module ). This is used to compile the kernel or module. This file will be read first and the environment variables will be compiled according to the content. For kernel or driver developers, there is almost no need to modify this file.

 

(2). config

 

The Kernel configuration file. After menuconfig is configured, A. config file will be generated in the main directory. Let's take a look at some content of this file:
......
Config_need_per_cpu_embed_first_chunk = y
Config_need_per_cpu_page_first_chunk = y
Config_have_cpumask_of_cpu_map = y
......

 

(3) arch/$ (ARCH)/makefile

 

The makefile of the Specific CPU architecture is located in arch/$ (ARCH)/makefile, which is the makefile of the system's corresponding platform. The kernel top makefile contains this file to specify platform-related information. Only platform developers care about this file.

 

(4) scripts/makefile .*

 

Kbuild uses common rules for all kbuild makefiles, including all definitions and rules. These files are used to compile the kbuild makefile-Based Kernel.

 

(5) kbuild makefiles

 

There are about hundreds of such files in the kernel source code. Generally, each directory has a makefile, which corresponds to a kconfig with some default compilation options. Each subdirectory has a kbuild MAKEFILE file to execute commands passed from its upper directory. Note: kbuild makefile is not directly executed as a makefile, but is extracted from the. config file to generate a list of files required for kbuild to complete kernel compilation.

 

2.1.1 kernel Objectives

The core part of kbuild makefile is the definition of the kernel target. It mainly defines the files to be compiled, all the options, and the subdirectories to perform recursive operations.

For example, if a directory in the source code contains Foo. C and needs to be compiled into an object, the kbuild makefile in the directory must have a line of form as follows:
OBJ-$ (config_foo) + = Foo. o

 

$ (Config_foo) is from the. config Kernel configuration file, which can be y (compiled into the kernel) or M (compiled into a module ). If config_foo is not y and m, the file will not be compiled and joined.

 

(1) Compile to the kernel

 

Kbuild makefile specifies that all the target files compiled into the kernel are in the $ (obj-y) list. These lists depend on Kernel configurations.

 

Kbuild compiles all $ (obj-y) files. Then, call "$ (LD)-R" to merge them into a build-in.o file. Later, the build-in.o will be linked to vmlinux by the top-level makefile.

 

Note that the files in $ (obj-y) are ordered. Repeated items in the list are fine, because when the first file is joined to the built-in.o, the rest of the files are ignored.

In addition, links are ordered because some functions (module_init ()/_ initcall) will be called in the order they appear at startup. Therefore, remember that changing the order of links may change the order of your system and cause damage to your hardware. Pay attention to this.

 

(2) compile it into a module

 

I don't need to talk about the differences between compiling a module and a kernel, because the module can be loaded with commands such as inmod. $ (Obj-m) lists the files to be compiled into a load-able module. A module can be compiled by one or more files. If it is a source file, kbuildmakefile simply needs to be added to $ (obj-m.

If the kernel module is compiled from multiple source files, you need to declare the module you want to compile in one way:
OBJ-$ (config_foo) + = ISDN. o

 

To explain this method, kbuild needs to know which files your compiled module is based on, so you need to tell it through the variable $ (<module_name>-objs:
# Drivers/ISDN/i4l/makefile
OBJ-$ (config _ Foo) + = ISDN. o
ISDN-objs: = isdn_net_lib.o isdn_v110.o isdn_common.o

 

In the above example, ISDN. O is the module name. kbuild will compile all the files listed in $ (ISDN-objs) and then generate ISDN using "$ (LD)-R. o.

 

Kbuild can identify the suffix-objs and suffix-y used to form the target file. Therefore, kbuildmakefile can use the config _ symbol to determine whether the object is used to combine objects:
# Fs/ext2/makefile
OBJ-$ (config_ext2_fs) + = ext2.o
Ext2-y: = balloc. O bitmap. o
Ext2-$ (config_ext2_fs_xattr) + = xattr. o

 

In this example, if $ (config_ext2_fs_xattr) is 'y', xattr. O will be part of the composite object ext2.o.

 

Note: Of course, the above syntax applies when you want to compile it into the kernel. So, if your config_ext2_fs = Y, kbuild will generate the ext2.o file as you expected and then link it to the built-in.o.

 

(3) compile it into a library file

 

Files listed in obj-* are used to compile a module or link to a built-in.o in a specific directory to compile the program into the kernel. You can also list files that will be included in library Lib.. The files listed in Lib-y are used to form a library file under this directory.

 

Objects listed in both obj-y and Lib-y are accessible, so the object is not included in the library file. In the same case, files in Lib-m must be included in library Lib..

 

Note: A kbuild makefile can list the files to be compiled into the kernel and the files to be compiled into the library at the same time. Therefore, the built-in.o and Lib. A files can exist simultaneously in a directory:
# Arch/x86/lib/makefile
Lib-Y: = chechsum. O delay. o

 

This will create a library file Lib. A by using the checksum. O and delay. O files. To make kbuild realize that there is a library file Lib. A to be created, the directory where it is located should be added to the libs-y list. Lib-y is generally restricted to LIB/and arch/*/lib.

 

(4) recursively accessing the Directory

 

A kbuild makefile is only responsible for the objects in the directory where the files are compiled. The file compilation in the subdirectory should be managed by the makefile in the subdirectory where it is located. As long as you let kbuild know that it should be recursive, the system will automatically call the make recursive operation in its subdirectory. This is the role of obj-y and obj-M.

 

For example, if ext2 is placed in a separate directory, makefile in the FS directory will tell kbuild to perform a downward recursive operation using the following values:
# Fs/makefile
OBJ-$ (config_ext2_fs) + = ext2/

 

If config_ext2_fs is set to 'y' (compiled into the kernel) or 'M' (compiled into a module), the corresponding obj-variable will be set, in addition, kbuild recursively accesses the ext2 directory. Kbuild only uses this information to determine whether it needs to access the directory, and the specific compilation is determined by the makefile in the directory.

 

Note: Setting config _ variable to directory name is a good programming habit. This allows kbuild to ignore directories where the corresponding config _ values are not 'y' and 'M.

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.