Experimental steps
1. Update menu, cover TEST_EXEC.C with test.c
2. Put init and hello in the rootfs.img directory, and execute the EXEC command to automatically load the Hello program
3. Executive EXEC
4. Run the Stopped menu
5. GDB for tracking analysis
Summarize
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 (), and the Sys_execve () service routine modifies the execution context of the current process;
When the Elf is load_elf_binary () loaded, the function returns to Do_execve () in return to Sys_execve (). The entry point of the Elf executable depends on how the program is linked:
1. Static Link: Elf_entry is a pointer to the executable file inside the specified head, 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.
preprocessing, compiling, linking, and formatting of the destination file
1. Preprocessing phase: The compiler compiles the header files contained in the C source code and performs the macro substitution work.
GCC-E-o XX.cpp xx.c-m32 (XX.cpp is a preprocessing file)
2. Compiler generation assembly code phase: GCC First to check the code of the normative, whether there is a syntax error, to determine the actual code to do the work, after the check is correct, gcc to translate the code into assembly language.
Gcc-x cpp-output-s-o hello.s hello.cpp-m32 (xx.s is assembly code)
3. Assembler generates the target code stage: The Xx.s file generated during the compilation phase is converted to binary target code.
Gcc-x assembler-c hello.s-o hello.o-m32 (XX.O is the target code)
4. The linker generates the executable phase (link the compiled output XX.O file to the final executable file).
Gcc-o hello.static hello.c-m32-static
5. Run (if the link is not specified by-O, the resulting executable file defaults to a.out)
./a.out
Format of the destination file Elf:
A.out is the oldest executable file, and most of the current Windows systems are Elf on the Pe,linux system. The elf file is already a binary compatible file that adapts to a certain CPU architecture
Elf Format categories:
relocatable files. O, used to create executables and shared files with other object files
Executable file that indicates where the execution should begin
Shared files, mostly. So files, used by link editors and dynamic linker links
A description of the ELF header tells the system how to create a memory image of a process, and the section Header table contains information describing the file sections. When creating or adding a process image, it is theoretically possible to copy the segment to a segment in virtual memory
The head of the elf file specifies a number of binary compatibility-related information. So when loading elf files, you must first load the head and analyze the elf's specific information.
Entry represents the entry address of the program after the new executable has just been loaded, the header is code and data, the process address space is 4G, the above 1G is the kernel, the following 3G is the program used. The default ELF header load address is 0x8048000
When creating a new user-state stack, it is actually passing the contents of the command-line arguments and the contents of the environment variables through pointers to the kernel handlers of the system calls. When a new executable stack is created, the kernel processing function copies the contents of the command-line arguments and the contents of the environment variables into the user-state stack to initialize the context of the execution of the new execution program.
Linux kernel and analysis seventh airplanes loading of the execution program