Seventh week Linux how to load and start an executable file

Source: Internet
Author: User

Pan Heng Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

First, the contents of the experiment

1. Preprocessing, compiling, and linking practices

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 for executable programs

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 (intchar*argv[])

And AS

int Main (int.Char Char *envp[])

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

int execve (constchar *filename,charconst argv[],char  Const envp[]);

The library function exec* are EXECVE encapsulation routines

3. Loading of executable programs

(1) Do_execve ()

(2) Search_binary_handler ()

(3) Load_elf_binary ()

(4) Load_elf_interp ()

Second, the experimental steps and

    1. Updating the menu kernel
      RM MENU-RF
    2. View TEST.c file (Shift+g directly to end of file): You can see that the new exec system call has been added, and its source code is similar to the previous fork
    3. Start the kernel and verify the EXECV function
    4. Freeze the kernel and start GDB debugging
    5. For debugging
      • Stop at Sys_execve and set other breakpoints; Press C all the way down until the breakpoint Sys_execve
      • Press S to jump into function stepping
    6. Exit the debug state, enter redelf-h hello to see the EIF header of Hello

Third, the Linux system load executable program needed to understand the process of processing

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.

If the new program is statically linked, the program can run independently, and the entry address in the ELF header is the entry address of the program.

If the new program is dynamically linked, then the shared library needs to be loaded at this point, and the entry address of the ELF header is the entry address of the dynamic linker ld.

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

New executable program execution:
1. Required Library functions.
2. The process space that belongs to it: Code snippet, data segment, kernel stack, user stack, etc.
3. Required operating parameters.
4. Required system resources.
If the above 4 conditions are met, then the new executable program will be in the operational state, as long as it is dispatched, it can be executed normally.
Condition 1: If the new process is statically linked, the library function is already in the executable program file, and the condition is met. If it is a dynamic link, the entry address of the new process is the starting address of the dynamic linker ld, which can be loaded for the required library functions and satisfy the conditions.
Conditional 2:EXECVE system call by drastically modifying the execution context, emptying the user-state stack, replacing the process space of the old process with the process space of the new process, the new process inherits the required process space from the old process, and the condition is met.
Condition 3: We typically enter the parameters required by the executable program in the shell, and the shell program passes these parameters to the EXECVE system call in the way the function arguments are passed, and then execve the system call to Sys_execve in the way the system calls the parameters, and finally Sys_ Execve when initializing the user-state stack of a new program, place these parameters where the main function takes arguments. Conditions are met.
Condition 4: If the current system does not have the required resources, then the new process will be suspended until the resources have, wake up the new process, become operational state, the conditions can be met.
In conclusion, the new executable procedure can be implemented smoothly.

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

The EXECVE system call calls Sys_execve, then Sys_execve calls Do_execve, then Do_execve calls Do_execve_common, and Do_execve_common calls EXEC_BINPRM.

For the elf file format, the FMT function pointer actually executes load_elf_binary,load_elf_binary calls Start_thread, in Start_thread by modifying the value of the EIP in the kernel stack to point to the elf_ Entry, jump to Elf_entry execution.
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.

Four. Summary

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.

Seventh week how Linux mounts and launches an executable file

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.