Seventh week of Linux kernel analysis learn to summarize executable program loading

Source: Internet
Author: User

Seventh Week, executable program loading

How is the executable process generated?

(1). c file gcc compilations form. S and. ASM Assembly Code;

(2) Assembly code through gas into. o target file;

(3) The target file becomes an executable file;

(4) The executable file is stored after loader.

Preprocessing: Gcc–e–o hello.cpp hello.c–m32 compilation: Gcc–x cpp-output–s–o Hello.s hello.cpp–m32  //compiled as assembly code      gcc–x assembler– C Hello.s–o hello.o–m32  //Compile as Target code link: gcc–o Hello hello.o–m32 static chain: Gcc–o hello.static hello.o–m32–static

 

Second, the target file format elf

    1. Three forms of the target file

(1) relocatable file (used to create the following two types of files with other object files) ——. o file

(2) Executable file (indicating where to start execution)

(3) shared files (mostly. So files, used to be linked to editors and dynamic linker chains)

2.ELF format:

(the left half is the elf format and the right half is the execution-time format)

Where does the new executable 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

Maps 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.

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

Execve and Fork are a special point of the system call: The general is to fall into the kernel state and then return to the user state. Fork parent process and general process scheduling, the child process returns to a specific point ret_from_fork, the child process starts from ret_from_fork and then returns to the user state; EXECVE Special: Execute to executable program--fall into the kernel--construct new executable file--Overwrite the original executables--return to the new executor, as a starting point (that is, the main function), need to construct his execution environment.

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

Static Link: elf_entry points to the head of the executable file, usually the main function;

Dynamic Link: elf_entry points to the beginning of the LD.

Six, the experimental process:

1. Update Menu:

2. Put init and hello in the rootfs.img directory, so when executing the EXEC command, it is equivalent to automatically loading the Hello program:

3.GDB for tracking Analysis:

4. After setting the breakpoint, enter exec for debugging in Menoos:

Vii. Summary: How the Linux kernel loads and launches an executable program:

1. Create a new process 2. The new process calls the EXECVE () system call to execute the specified elf file 3. Call the kernel's ingress function sys_execve (), the Sys_execve () service routine modifies the execution context of the current process, and when the elf is loaded by the load_elf_binary (), the function returns to Do_execve () back to Sys_execve ( )。 The entry point of the Elf executable depends on how the program is linked: 1. Static Link: Elf_entry is the head that is defined in the executable file, that is, the main function. 2. Dynamic Link: The executable is dependent on other dynamic link libraries, Elf_entry is the starting point for the dynamic linker. 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.

  

Seventh week of Linux kernel analysis learn to summarize executable program loading

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.