Wang Zhao (with the name of the final application certificate must be consistent) + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
How the Linux kernel loads and launches an executable program
- The elf file is a standard file format for binary files, executables, target code, shared libraries, and core dumps, consisting of 4 parts, the elf header (elf header), the Program Header table, section, and section header tables (Sections Header table).
- Execution starting point and corresponding stack state of the new executable program:
-
- The execution starting point of the new executable program is the entry program address of the returned kernel program loaded to the user-State executable program, and the corresponding stack state is empty because it is re-init, so it can execute the program in the user state sequentially.
Summary: The Linux kernel is loaded with the corresponding executable program first allocated resources, the assembly process, the execution of the context settings, variable assignment and other operations, and then return to the user state of the main program, the environment to modify. In the user-state execution of the new executable program, the user-state program for its allocation register, scheduling resources to execute its code snippet program. After starting an executable program, the execution environment of the context for which it is prepared by the kernel State executes its program.
- The exec* system call Fork system call simply copies the environment of the parent process into the child process without using the child process to initialize the created child process, so it is not able to execute the new target program, which is necessary for programming. This UNIX system provides an execution image that is called by the exec system to replace the process with the specified target program. Most of the commands in the system are executed by exec. Not only the child processes created by the shell process use it to execute user commands, but the shell process itself and its ancestor processes are also executed with exec.
The exec system calls are available in a number of different formats, except that they have different names and parameters.
The exec call in the program enables the child process to execute its own code. Main () {
printf ("==system execl testing ==\n"), Execl ("/bin/date", "/bin/date", 0); printf ("Execl failure!\n");}
One of the execution results is as follows:
If the Execl line is written as execl ("date", "date", 0), the following results will be executed: ===system execl testing=== execl failure!
The first and third lines of the program output the judgment information, and when the EXECL call succeeds, the third line statement cannot be output because the statement in the program is not executed, and the information is output only if the EXECL call fails. Therefore, the implementation of the program obtained the above two different results.
The second execl call failed because the first parameter of the EXECL call must be the full pathname of the destination file, not just the file name.
How the Linux kernel loads and launches an executable program