Read kernel sentiment-Linux kernel startup-kernel generation

Source: Internet
Author: User

During this time, I read "Linux kernel source code Scenario Analysis" and wrote some insights. Reading the kernel source code is very interesting. It is like a line that concatenates basic computer courses such as operating systems, compilation principles, C language, data structures and algorithms, and computer architecture.

I think the kernel source code is using lxr + glimpse (you don't have to stand it on your own, you can directly access the lxr website inside and outside the school. You can also use source insight in windows. The current path below is the kernel source code path, usually/usr/src/Linux. The kernel version is 2.6.13 and the platform is x86.

Okay. Let's start the Linux kernel journey.

The starting point is that when the CPU is powered on, the system is in the 16-bit real address mode, the kernel starts to run start_kernel (), and the system is in the 32-bit page addressing protection mode. At that time, the kernel image bzimage has been decompressed and runs in the kernel state. The system already has a process named Swapper, with its own kernel stack, which is much easier to understand. (Although it is more complicated than user-state programs, including direct hardware operations, initialization of various kernel-state data structures, and operations on page tables ). However, do some preparation first.

First, what is the kernel? Currently, only after compiling the kernel is known, a compressed kernel image called bzimage is generated. It is different from any common executable program. We don't even know where it starts. Just put it to/boot/next, and write relevant information to the configuration file of Boot Loader (for example, menu. LST of grub). Then the machine starts smoothly. Therefore, I am very interested in its generation process. So I checked the relevant materials. The most direct information is from makefile under ARCH/i386/boot. You can see from makefile. The bzimage generation process is as follows: however, I am not satisfied with this. So I thought of seeing makefile under ARCH/i386/boot. We can see from arch/i386/boot/makefile and arch/i386/boot/compressed/makefile (the specific process is omitted ,)

1. vmlinux. This is an elf executable file.

2. Convert objcopy to arch/i386/boot/compressed/vmlinux. bin, and remove useless sections and other information from the original ELF File.

3.gzip compression: ARCH/i386/boot/compressed/vmlinux.bin.gz

4. link the compressed file as a data segment to arch/i386/boot/compressed/piggy. o

5. Link: ARCH/i386/boot/compressed/vmlinux = head. O + Misc. O + piggy. o

Here, head. O and Misc. O are used for decompression.

6. Convert objcopy to arch/i386/boot/vmlinux. Bin and Remove useless section information from the original ELF File.

7. Use ARCH/i386/boot/tools/build. C to splice bzimage = bootsect + setup + vmlinux. Bin

The process is complicated.

The objcopy command is used to convert an object file to another format. Here, objcopy is used to remove the elf header and useless section information from the original ELF File. Why? Because the elf header and some sections in the ELF file are used to tell elf loader how to load elf executable files. However, Linux kernel, as a special ELF File, needs a special discount Helper Program to load it. Usually, its load address is fixed. At this time, the elf header and some sections that exist to ensure the universality have no significance for Kernel loading. In addition, in order to make the kernel as small as possible, we simply remove this information.

Let's take a look at vmlinux and arch/i386/boot/compressed/vmlinux. Run the file command to view the files that are also executable files of elf. There is no main function.

 

Refer:

Documentation/kbuild/makefiles.txt

Documentation/kbuild/modules.txt

For more information, see: http://www.top-e.org/jiaoshi/html? 192. html

 

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.