Linux Kernel compilation details, Linux kernel details

Source: Internet
Author: User

Linux Kernel compilation details, Linux kernel details

After learning some online materials, I tried to find out and sort out this article.

We hope you will criticize and correct the mistakes due to their limited level and improper position.


Important references include:

Http://raspberrypi.stackexchange.com/questions/192/how-do-i-cross-compile-the-kernel-on-a-ubuntu-host

Http://blog.csdn.net/xdw1985829/article/details/6833319


Now, let's go to the topic.


I. Preparations



I will not go into details here about how to prepare for it.

A) first, you need to have a PC (this is not nonsense) and install Linux.

B) install GCC (host gcc is used to compile and generate tools running on PC programs), make, ncurses, and so on.

C) download a pure Linux kernel source code package and decompress it.

Note: If you compile the kernel for the current PC, it is best to use the source code package of the corresponding Linux release version.

However, this should not be necessary, because on my Fedora 13 (whose internal kernel is 2.6.33.3), I downloaded a standard internal kernel linux-2.6.32.65.tar. xz. The compilation and installation are successful, and the power-on and restart operations are OK. However, the. config configuration file I used is the configuration file of the built-in kernel of Fedora 13.

D) If you are porting Linux to an embedded system, you must install the cross-compilation tool chain. For example, if the CPU of your target board may be arm or mips, install the corresponding cross-compilation tool chain. After installation, add the toolchain path to the path environment variable. For example, if you install the arm tool chain, you can execute commands similar to the following in the shell. If there is similar output, it indicates that the tool has been installed.

[Root @ localhost linux-2.6.33.i686] # arm-linux-gcc -- version
Arm-linux-gcc (Buildroot 2010.11) 4.3.5
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE.


Ii. Set compilation targets


Before configuring or compiling the kernel, you must first determine the target CPU architecture and the tool chain used during compilation. This is the most basic information. You must first confirm it.

If you compile the kernel for the current PC, you do not need to set it.

Otherwise, you must specify the settings.

Here we use arm as an example to describe it.

There are two ways to set ():

A) Modify Makefile

Open the Makefile in the root directory of the kernel source code, modify the following two Makefile variables and save them.

ARCH: = arm
CROSS_COMPILE: = arm-linux-


B) each time you execute the make command, the command line parameters are used to pass in the information.

In fact, the command line parameter of the make tool is used to specify the value of the variable.

For example

Use

Make ARCH = arm CROSS_COMPILE = arm-linux-menuconfig

Used to compile the kernel

Make ARCH = arm CROSS_COMPILE = arm-linux-


Note: In fact, although the user does not specify the settings for compiling the PC kernel, these two items are not configured. If you do not set these two variables, the kernel source code top-level Makefile (located in the source code root directory) will generate the values of these two variables as follows.

SUBARCH: = $ (shell uname-m | sed-e s/I .86/i386/-e s/sun4u/sparc64 /\
-E s/arm. */arm/-e s/sa110/arm /\
-E s/s390x/s390/-e s/parisc64/parisc /\
-E s/ppc. */powerpc/-e s/mips. */mips /\
-E s/sh [234]. */sh /)

ARCH? = $ (SUBARCH)
CROSS_COMPILE? =

After the above Code, ARCH becomes the arch of the PC compiler, that is, SUBARCH. Therefore, if the uname-m output on the PC is ix86, the ARCH value is i386.

The value of CROSS_COMPILE, if not configured, is a null string.In this way, the name of the tool chain program used does not have a prefix like arm-linux-, which is equivalent to using gcc on the PC.

The ARCH value needs to be further generalized. Because the i386 directory does not exist under the arch directory of the kernel source code, and there is no directory such as sparc64.

Therefore, an SRCARCH variable is constructed in the top-level makefile and its value is generated using the following code. In this way, the SRCARCH variable finally matches a schema name in the kernel source code arch directory.


SRCARCH: = $ (ARCH)


Ifeq ($ (ARCH), i386)
SRCARCH: = x86
Endif


Ifeq ($ (ARCH), x86_64)
SRCARCH: = x86
Endif

Ifeq ($ (ARCH), sparc64)
SRCARCH: = iSCSI
Endif

Ifeq ($ (ARCH), sh64)
SRCARCH: = sh
Endif


3. Configure the kernel


With so many kernel functions, we can configure what we need, how each part is compiled into (compiled into the kernel or compiled into a module), and how the working parameters of each part are. Therefore, before compiling, we need to build a configuration list, put it in the root directory of the kernel source code, and name it. config file, and then according to this. the config file to compile the required kernel.

However, there are too many Kernel configuration items and one configuration is too troublesome. In addition, the set of configuration items that can be configured varies with the CPU architecture. For example, a configuration item that is not supported by a certain feature of a CPU is a configuration item related to the CPU architecture. Therefore, the kernel provides a simple configuration method.

Take arm as an example. The procedure is as follows.

A) based on our target CPU architecture, find the configuration file closest to the target system from the kernel source code arch/arm/configs directory (for example, s3c2410_defconfig ), copy to the root directory of the kernel source code and name it. config.

Note: If you compile the kernel for the current PC, it is best to copy the following file to the kernel source code root directory as the initial configuration file. This file is the configuration file used for compiling the kernel currently running on the PC.

/Lib/modules/'uname-R'/build/. config

By the way, the configuration file of the PC kernel has many features. I don't know if I don't know. The Linux issuer may want to make the released Linux meet various user needs.

B) Execute make menuconfig to modify the configuration. When you exit, save the configuration and update it to The. config file.

Note-1: When we perform this operation, the kernel opens a set of configuration items. Let's configure them. This set of configuration items is determined by the CPU architecture we set earlier. To be more specific, open the arch/arm/Kconfig file in the configuration system (a line such as "scripts/kconfig/mconf arch/arm/Kconfig" can be seen during the execution of make menuconfig ), this file contains the Kconfig file of other kernel subsystems (the file name may also be another name), the Kconfig file of other subsystems, and the lower-layer Kconfig file, all configuration items are generated. For each configuration item, the current set value (for example, whether it is compiled into the kernel, compiled into a module, or a parameter) is under the root directory of the kernel source code. config file.

Note-2: Even if you do not need to modify the configuration, you must execute make menuconfig and then exit and save it on the configuration page. Otherwise, the subsequent compilation may encounter problems. I have encountered this problem. I guess the reason may be that the initial configuration file is based on the kernel of the old version, and some basic functions may be added to the kernel of the new version, as a result, the dependency between function items changes. For example, if you select a feature in the old configuration file, the implementation in the new kernel may depend on more features. Therefore, you need to adjust the old initial configuration file to ensure that the dependencies of each function item are met. After making menuconfig, I found that the content of the. config file has indeed changed.


4. Compile the kernel


Compiling itself is simple. For kernels later than version 2.6, execute the following command.

Make

We may take some time to understand the kernel compilation mechanism.

A) how to use the config file in the kernel

The. config file is generated earlier. This is a text file, which is similar to the following content:

CONFIG_YENTA_ENE_TUNE = y
CONFIG_YENTA_TOSHIBA = y
CONFIG_PD6729 = m
CONFIG_I82092 = m

CONFIG_MTDRAM_ERASE_SIZE = 128

It can be seen that some are set to compile a function into the kernel, some are set to compile a function into a module, and some are set to a parameter of a function.

The syntax of this file is actually the syntax for defining makefile variables. That's right. This is makefile.

When we execute make to start compiling the kernel, the compilation system will generate another config file, that is, include/config/auto. conf. The content is similar to. config, but the content is less.

During kernel compilation, the top-level Makefile (located in the source code root directory) will contain the above config file.

In this way, the corresponding makefile variable is obtained to know how to compile each part of the kernel.

From the top-level makefile, you can see the following code:

Ifeq ($ (dot-config), 1)
# Read in config
-Include/config/auto. conf

However, the relationship between the two config files is not clarified. For details, please kindly advise :)


B) how to compile each subsystem or module in the kernel

As you can see from the previous step, through the config file, the kernel top-level makefile has generated a large number of makefile variables.

On the other hand, each subsystem or module has a Makefile in its source code Directory, which defines the content to be compiled by the sub-system or module.

Next, the make tool can take a large number of makefile variables generated in the top-level makefile into the directories where subsystems or modules are located, compile the content defined in Makefile in each directory.

Makefile in these directories is very simple.

If a directory contains only one module hello, this module has only one. c file, such as xxx. c. The Makefile contains only one row.

Obj-$ (CONFIG_HELLO): = hello. o

If the hello module consists of three main. c a. c B. c files, Makefile only needs two lines of content.

Obj-$ (CONFIG_HELLO): = hello. o

Hello-objs: = main. o a. o B. o

If a directory contains C files of multiple modules, such as hello, hello2, and hello3.
Composition of the hello module: main. c a. c B. c
Composition of hello2 module: main2.c a2.c b2.c
Composition of the hello3 module: hello3.c
In this case, Makefile only needs five lines of content.

Obj-$ (CONFIG_HELLO) + = hello. o

Obj-$ (CONFIG_HELLO2) + = hello2.o

Obj-$ (CONFIG_HELLO3) + = hello3.o

Hello-objs: = main. o a. o B. o
Hello2-objs: = main2.o a2.o b2.o


Because the top-level Makefile contains a large number of variables, the $ (CONFIG_HELLO) variable in the Makefile in the subdirectory is parsed and then changed to y or m. In this case, after the Makefile in each subdirectory is parsed, only a variable named obj-m or obj-y is defined.

The value of the variable obj-m or obj-y is a list of. o files. Each item in the table represents a function item. If the variable name is obj-m, this function is compiled into a module. If the variable name is obj-y, this function is compiled into the kernel.


C) In the kernel code, how do I know whether a function is configured or not?


When we execute make to compile the kernel, the compilation system will also generate a C Language header file include/generated/autoconf. h

This file is similar to the following content:

# Define CONFIG_DM9000 1
# Define CONFIG_DM9000_DEBUGLEVEL 4
# Define CONFIG_SND_RAWMIDI_SEQ_MODULE 1
The first line indicates that the user has selected to compile the DM9000 driver into the kernel, and the second line is a parameter of the driver. If the user chooses to compile the DM9000 into a module, the contents of the first line will become the following form.
# Define CONFIG_DM9000_MODULE 1


With this header file, if the. c file of a kernel source code contains this header file, you can use # ifdef CONFIG_XXX to check whether the user has configured the XXX function.


Okay, the kernel compilation mechanism is limited to understand in the next step. I will not talk about it here. ^_^


5. Install the kernel

A) install the kernel for the current PC

Run the following two commands in sequence to install the module and kernel respectively.

Make modules_install

Make install


Start boot/grub. conf, and an additional entry is displayed.

Change the timeout value to 5, so that the kernel can be started for 5 seconds at startup.

Finally, restart the computer. When the bootloader interface appears, choose to start the new kernel.


B) install the kernel for the Embedded System

This is not a sentence. For specific questions, please refer to the relevant information. ^_^

For general arm boards, a common method is to connect a PC to a single board serial port through SecureCrt, connect a single board network port through a network cable, and start the tftp server on the PC, place the kernel image zImage file in the tftp download directory. Restart the board. When you see the u-boot countdown in SecureCrt, press any key to enter the u-boot interactive interface. Then, download the zImage file of the kernel image through the relevant commands on this interface, and then run the command to burn the downloaded zImage to the FLASH of the Board. Restart the board.


As for the module installation, it is considered during the creation of the root file system.

I have a limited understanding of this knowledge, so I won't say much about it. ^_^



Related Article

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.