how the Linux kernel loads and launches an executable program
"20135224 Chen Shi + original works reproduced please specify the source +" Linux kernel analysis "MOOC course http://mooc.study.163.com/course/USTC-1000029000"
First Part
Creation of executable files-preprocessing, compiling and linking simple hello.c preprocessing, compiling, and linking methods (corresponding to the diagram for operation steps):
shiyanlou:code/$ vi hello.c
shiyanlou:code/$ gcc-e-O hello.cpp hello.c-m32
Pretreatment
shiyanlou:code/$ vi hello.cpp
shiyanlou:code/$ gcc-x cpp-output-s-o hello.s hello.cpp-m32
Compiler
shiyanlou:code/$ vi HELLO.S
shiyanlou:code/$ gcc-x assembler-c hello.s-o hello.o-m32
shiyanlou:code/$ vi hello.o
shiyanlou:code/$ gcc-o Hello Hello.o-m32
Link
About Links:
To generate an elf file:
For the target file, there is at least a compiled machine instruction code, data, also includes some information needed to link, such as symbol table, debugging information, string and so on. Linux is able to support a variety of executable file formats, all the information in the format is stored in a linked list, where the load_binary is a function pointer, corresponding to the format of the executable file loading mode; To support a new executable file, You only need to register a new format struct with the linked list, which is similar to the observer pattern and is very extensible.
Part II
GDB Trace View:
Experiment about zombie virtual machine boot kernel: (QEMU)
Set breakpoints at Sys_execve:
、
Program running:
command-line arguments and shell environments, typically we execute a shell environment for a program, and our experiment directly uses the EXECVE system call. The shell itself does not limit the number of command-line arguments, and the number of command-line arguments is limited to the command itself, for example, int main (int argc, char *argv[], int main (int argc, char *argv[], Char *envp[]), Sh Ell calls Execve to pass command-line arguments and environment parameters to the main function int Execve (const char * filename,char * CONST argv[],CHAR * Const envp[]) of the executable program; library functions exec* It's all execve. Package Routines Sys_execve Internal Parse executable format do_execve, Do_execve_common, EXEC_BINPRM
Summary:
For Linux how to load and start most of the steps are: first processing the file necessary advance, through the GCC tool to understand the CPU can read and execute the file format, and then through the Execve API to start a new process, which is called SYS_EXECVE system call, Responsible for replacing the new program code and data into the new process, opening the executable file, loading the dependent library file, requesting new memory space, finally executing start_thread (regs, Elf_entry, bprm->p), setting new_ip, NEW_SP, Complete the code and data substitution for the new process, and then return to the next step of executing the new process code.
How the Linux kernel loads and launches an executable program