The eighth day of the 10-day learning Linux kernel---build the Linux kernel

Source: Internet
Author: User

Today is Laba Festival, said the woman ticket to give me to do the Laba porridge is so bad, so sad, good-hearted acid ah, it seems that the code is really annoying drops, so I warned you technical male wake alarm, do not want me to the same, or you can only and the Code as companion ~ ~ said no Laba porridge but there is code, and you have the support of readers , so you have to continue to write down, calm down, complete the Linux kernel learning, adhere to, refueling ~

So far, we've known the Linux kernel subsystem, explored the initialization of the system, and explored the Start_kernel () function in depth, as well, it's important to understand the creation of the kernel image, and then we'll discuss the process of compiling and linking the kernel image. So, of course, the tool chain is needed, and the toolchain contains the compiler, assembler, linker, a set of programs that create the Linux kernel image, and the chain relationship of the toolchain:

Elf binary target file

Executable elf target files include: Elf header, Program Header table (for loading sections), 1th section, section 2nd .... Section Header table (optional)

Elf header file

typedef struct elf32_hdr{  unsigned char e_ident[ei_nident];//Identify if the file is an elf file  elf32_half    e_type;  Specify the target file type, such as executable file, relocate file, share the target file  elf32_half    e_machine;   The architecture of the system in which the file is compiled  elf32_word    e_version;//version  elf32_addr e_entry of the target file    ;  /* Entry Point *  //The starting address of the program  Elf32_off     E_phoff;   The offset of the saved Program Header table in the file is  elf32_off     e_shoff;   Save section Header table offset in file  elf32_word    e_flags;   stored in a particular processor-specific logo  elf32_half    e_ehsize;  The field holds the size of the elf head  elf32_half    e_phentsize;  Save the size of each item in the Program Header table  elf32_half    e_phnum;  The number of table items in the program header is  elf32_half    e_shentsize;  The size of each item in the section Header table is  elf32_half    e_shnum;   Saves the number of items in the section header, indicating how many sections elf32_half e_shstrndx in the file    ;  Save index of section string in section header} ELF32_EHDR;

Section Header table

typedef struct ELF32_SHDR {  elf32_word    sh_name;   Contains section name  elf32_word    sh_type;   Contains the contents of the section  Elf32_word    sh_flags;  The contents of various attributes  elf32_addr    sh_addr;   section in memory image address   elf32_off     sh_offset;//Save the Elf file an offset of the initial byte in this section  Elf32_word    sh_size;   Contains the size of the section  Elf32_word    sh_link;   Table links indexed  Elf32_word    sh_info;   Contains additional information  Elf32_word    sh_addralign;  Contains the address to which the constraint  elf32_word    sh_entsize;  The size of each item in the section} ELF32_SHDR;

Non-executable elf file section

Node Description
. Data Initialized data
Bss The data that is initialized
. hash Symbol Hash List
. Init Initialize Code
. symtab Symbol table
. text Executable instructions
. plt Process link Table
. rodata Read-only data
Dynamic Dynamic Link Information

Program Header table

typedef struct ELF64_PHDR {  Elf64_word p_type;    Describe the type of the segment  Elf64_word p_flags;   Elf64_off P_offset is determined by P_type  ;   <span style= "font-family:arial, Helvetica, Sans-serif;" > The beginning of the paragraph relative to the beginning of the file offset </span>  elf64_addr p_vaddr;   Segment virtual address  elf64_addr p_paddr;   The virtual address of the segment    Elf64_xword P_filesz;//The number of bytes in this segment in the file image  Elf64_xword P_memsz;  The number of bytes in the memory image for that segment  Elf64_xword p_align;  Describes how the segment to be aligned is aligned in memory, which is a power of 2 for the entire number of times    
} ELF64_PHDR;

With this information, the system function exec () and the linker work together to create a process image for the executable in memory, as follows:

    • Adding segments of an executable to memory
    • Load all required shared libraries
    • redirect executable files and their shared objects when needed
    • Handing control over to the program

  

So how does the kernel get compiled into binary files, and how to load memory before execution. The following will begin the introduction of the compiled kernel source code. The memory start starts from executing the real-mode assembly code in the arch/x86/boot/directory. View the arch/x86/kernel/setup_32.c file to see how the protected mode kernel obtains the information collected by the real-mode kernel. The first piece of information comes from code in INIT/MAIN.C, digging deeper init/calibrate.c can understand bogomips calibration more clearly, while Include/asm-your-arch/bugs.h contains architecture-related checks.

The time service in the kernel consists of the architecture-related parts residing in arch/your-arch/kernel/and the common parts implemented in KERNEL/TIMER.C. The relevant definitions can be obtained from the include/linux/time*.h header file.

The jiffies is defined in the Linux/jiffies.h file. The value of Hz is processor-dependent and can be found from include/asm-your-arch/param.h, where the memory management source code is stored in the top-level mm/directory.

The official source code publishing site for Linux is www.kernel.org. Its source code directory structure is as follows:

Using the kernel Configuration tool to automatically generate the kernel configuration file for. config, which is the first step in compiling, the. config file is located in the source code directory, and its options are sorted according to their location in the kernel Configuration tool, let's take a look at an excerpt from a. config file:

# 4 config_x86=y 5 config_mmu=y 6 config_uid16=y 7 config_generic_isa_dma= #12 config_experimental=  Y13 config_clean_compile=14 config_standalone=y15 config_broken_on_smp= #20 config_swap= y21config_ sysvipc=Y22 #CONFIG_POSIX_MQUEUE is not set23 config_bsd_process_acct=y//This 4 line is in the Common Settings Options menu   

  

Finally to a rough introduction of the Linux kernel makefile file, but also can simply introduce, this is a serious problem, here I say a little bit, will be specific to learn. The Linux kernel is a monolithic kernel, but it is very flexible and easy to develop by dynamically loading the module. So, how does it compile the kernel? We can start by analyzing its makefile. The following is a simple Hello kernel module makefile.

Ifneq ($ (kernelrelease),)
obj-m:=hello.o
Else
kerneldir:=/lib/modules/$ (shell uname-r)/build
pwd:=$ (shell pwd)
Default
$ (make)-C $ (Kerneldir) m=$ (PWD) modules
Clean
RM-RF *.o *.mod.c *.MOD.O *.ko
endif

First, because there is no target behind make, the first goal in Makefile is not to start with the. Target as the default target execution. So default becomes the goal of make. Make performs a function of $ (made)-C $ (Kerneldir) m=$ (PWD) modules Shell is the make internal, make executes two times.

The first time to execute is to read the source code of the Hello module in the directory/home/s tudy/prog/mod/hello/under makefile.

The second execution is when the makefile under/usr/src/linux/is executed.

It's complicated, and I don't know what to say. A more detailed procedure for make modules can be found in the comments in the Scripts/makefile.modpost file. But I found a Daniel wrote with me to learn makefile blog, I put the blog address below, for your reference: http://blog.csdn.net/haoel/article/details/2886/

  Summary

This chapter explores the compilation of the target file, the link process, and the structure of the target file, in order to understand the final form of executable code, build the Linux kernel covers the kernel to compile the necessary tools, and finally simply describe the makefile, these are difficult, and have to be more learning, Although today did not eat laba porridge, but turn the pot is very force, eat until now is not hungry, is an unforgettable day ~ ~

All rights reserved, reprint please specify reprint address: http://www.cnblogs.com/lihuidashen/p/4253752.html

The eighth day of the 10-day learning Linux kernel---build the Linux kernel

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.