How the Linux kernel loads and launches an executable program

Source: Internet
Author: User
Tags git clone

Wang Chenguang "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Name: Wang Chenguang

Study No.: 20133232

One, the network course Note:

1) preprocessing, compiling and linking

Elf head at the beginning of the file, describe the overall format of the file, save the roadmap, describe the organization of the file, that is, the size of the word that generated the file system and the byte order Segment Header table is used to describe the mapping between Elf executables and contiguous buckets. The section Header table contains information describing the section area of a file, each of which has an item in the table, giving the name of the section, the size of the section area. The target file for the link (the redirected file) must contain the section Header table, and the executable file may not.

(2) Execution Environment of executable program

command-line arguments and shell environments, typically we execute a shell environment for a program, and our experiment directly uses the EXECVE system call.

$ ls-l/usr/bin Listing directory information under/usr/bin

The shell itself does not limit the number of command-line arguments, the number of command-line arguments is limited by the command itself

For example, int main (int argc, char*argv[])

Also, int main (int argc, char*argv[], Char *envp[])

The shell calls Execve to pass command-line arguments and environment parameters to the main function of the executable program.

int Execve (const char *FILENAME,CHAR * CONST argv[],CHAR * Const envp[]);

The library function exec* are EXECVE encapsulation routines

Loading of executable programs

(3)

Program Compile link process

Preprocessing: (. C. cpp)

GCC-E-O hello.cpp hello.c-m32

Compiling: (. cpp-S compilation)

Gcc-x cpp-output-s-o hello.s hello.cpp-m32

Compile: (. s. o binary target code)

Gcc-x assembler-c Hello.s-o Hello.o-m32

Link: (. O-a.out) shared library

Gcc-o Hello Hello.o-m32

Static compilation:

Gcc-o hello.static hello.o-m32-static

(4) Analysis of dynamic link executable program loading

1. You can focus on interp and dynamic in the ELF format.

2, the dynamic link library loading process is a graph of the traversal.

3. After loading and connecting, LD gives the CPU control to the executable program.

Ii. contents of the experiment

Open the virtual machine in the lab building, run the following command in the shell, get the code for this experiment, compile and run

CD Linuxkernel

RM MENU-RF

git clone https://github.com/mengning/menu.git

CD Menu

MV TEST_EXEC.C test.c

Make Rootfs

Close the Qemu window, in the Shell window, the CD Linuxkernel back to the Linuxkernel directory, start the kernel with the following command and stop for debugging before the CPU runs the code:

Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S

Next, we can split a new shell window horizontally, then start GDB debugging with the following command

Gdb

(gdb) file Linux-3.18.6/vmlinux

(GDB) Target remote:1234

and set breakpoints at the entrance of the system call SYS_EXECVE

(GDB) B sys_execve

Continue running the program, enter exec in the Qemu window, and the system will stop at the breakpoint set above.

Related to the following:

Next we can step through the kernel code of SYS_EXECVE, or you can set the following breakpoint

b load_elf_binary

b start_thread

To completely track the process's creation and startup code!

Three, experimental problems and understanding

1. Where do the new executable programs start?

When the EXECVE () system call terminates and the process resumes its execution in the user state, the execution context is drastically changed, and the new program to be executed has been mapped to process space, starting with the program entry point in the ELF header to execute the new program.

2. Why does the new executable program execute smoothly after the EXECVE system call returns?

The new executable executes the library function, which belongs to its process space: code snippet, data segment, kernel stack, user stack, etc., need to run parameters and system resources. If the condition is met, then the new executable program will be in the operational state, as long as it is dispatched, it can execute normally.

3. What is the difference between a static-linked executable program and a dynamically-linked executable program when the EXECVE system call returns?

For statically linked executable programs, Elf_entry is the starting point for the new program's execution. For dynamically linked executable programs, you need to load the linker ld first,
Elf_entry = Load_elf_interp (...)
The CPU control is given to the LD to load the dependent library, and the LD returns the CPU control to the new process after the loading work is completed.

Iv. Summary

The operation of the experiment is very simple, but it is very difficult to understand the actual content, referring to the cloud classroom courseware and the previous people have written about the relevant blog, only a certain understanding.

In Linux, fork is the only way for a process to create another process. Only the first process is the process called Init that needs to be created manually. All other processes are created with the fork, the system call. The fork system call simply replicates the data and stack of the parent process and shares the text area between the two processes. The fork system calls in a smarter way-write-time copy (copy-on-write) technology, so that the fork after the end does not immediately copy the contents of the parent process, but to the real practical when the copy, so that the efficiency greatly improved. After the fork function creates a child process, the child process calls the Exec family function to execute another program.
After a multi-process, multi-user, virtual storage operating system appears, the loading process of the executable file becomes very complex. The virtual address space of the process is introduced, and then based on how the operating system allocates code, data, heaps, stacks, and the process address space of the program, how they are distributed, and finally, the program maps the process virtual address space in a page-map manner.
Dynamic linking is a different concept from static linker, that is, a single executable module is split into several modules, a way to link when the program runs. Then, according to the practical example, Do_exece () analyzes the approximate process of elf loading, and realizes dynamic link in the middle.

How the Linux kernel loads and launches an executable program

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.