20135302 Wei quiet--linux course Seventh Week experiment and summary

Source: Internet
Author: User
Tags terminates

Seventh week of Linux Course experiment and summaryExperiment and study summary1. Compile the link process and the ELF executable format (in the case of Hello) the GNU compilation system compiles the source code:
    • First, run the C preprocessor (CPP)and translate the. c file into a. i file--gcc-e-o hello.cpp hello.c-m32
    • Next, run the C compiler (CC1)and translate the. i file into an ASCII assembly language file. S file--gcc-s-o Hello.s hello.cpp-m32
    • Then, run the assembler (AS)and translate the. s file into a relocatable destination file. o File--gcc-c hello.s-o hello.o-m32
    • Finally, run the Linker (LD)and combine each. o file to create an executable target file--ld-o Hello hello.o xxx.o
    • Flow chart:
Elf executable file format
The elf file consists of 4 parts, namely the Elf header (elf header), the Program Header table, the section Header table, and the section headers tables.
  • We use the readelf-h command to see Hello's elf file header, you can see: (in addition to Objump or readelf can directly view the elf file node information)
  • Elf File Header code entry meaning: The first is a 16-byte e_ident, which contains the characters used to represent the elf file, as well as some other machine-independent information. The first 4 byte values are fixed, and are 0x7f and elf three characters.E_typeit identifies the type of the file. E_machineindicates the architecture required to run the program. e_versionrepresents the version of a file. E_entrythe entry address of the program. E_phoffrepresents the offset (in bytes) of the Program Header table in the file. E_shoffrepresents the offset (in bytes) of the section Header table in the file. e_flags For IA32, this is 0.e_ehsizerepresents the ELF header size (in bytes). e_phentsizerepresents the size of each entry in a Program Header table. E_phnumindicates how many entries are in the Program Header table. e_shentsizerepresents the size of each entry in a section Header table.   e_shnum Represents how many entries are in a section header table.
    E_shstrndxThe string that contains the section name is the first section (counting from zero)
2. GDB Debug EXECVE system call and new executable program starting point new executable program starting point
一般是地址空间为0x8048000或0x8048300;
    • In contrast, when returning to the user state, New_ip saves the address of the first instruction that returns the user state.
EXECVE system Call
    • The shell calls Execve to pass command-line arguments and environment parameters to the main function of the executable program.

      • int Execve (const char * filename,char * CONST argv[],CHAR * Const envp[]);

        • Execve () is used to execute the file path represented by the argument filename string, the second parameter is passed to the execution file using an array of pointers, and it needs to end with a null pointer (null), and the last parameter is an array of new environment variables passed to the execution file.
      • The library function exec* are EXECVE encapsulation routines

execve和fork都是特殊一点的系统调用:一般的都是陷入到内核态再返回到用户态。fork父进程和一般进程调度一样,子进程返回到一个特定的点ret_from_fork,子进程是从ret_from_fork开始执行然后返回到用户态;execve特殊:执行到可执行程序--陷入内核--构造新的可执行文件--覆盖掉原可执行程序--返回到新的可执行程序,作为起点(也就是main函数) ,需要构造他的执行环境;
    • We can take a look at the entry and return value of the EXECVE system call in the experiment

    • Sys_execve () calls Do_execve () after some parameters are checked for replication.
Do_execve () First checks the executed file, when DO_EXECVE () reads the 128-byte file header, and then calls Search_binary_handle () "RetVal = Search_binary_handler ( Bprm,regs) "
To search for and match the appropriate executable file loading process. All supported executable formats in Linux have a corresponding loading process, and Search_binary_handle () determines the file format by determining the number of magic in the head of the file.
and calls the appropriate loading process. such as Elf with Load_elf_binary (), a.out with Load_aout_binary (), script with Load_script (). The main steps of the elf loading process are:

① checks the validity of the elf executable format, such as the number of magic numbers and the middle of the program header (Segment).

② find the dynamically linked ". Interp" segment, which saves the path to the dynamic linker required by the executable, and sets the dynamic linker path.

③ maps elf files, such as code, data, and read-only data, according to the description of the Program Header table of the Elf executable.

④ initializes the ELF process environment, such as the address of the EDX register at the start of the process, which should be the address of the Dt_fini (end code address).

⑤ changes the return address of the system call to the entry point of the Elf executable, depending on how the program is linked,

    • Statically linked executable programs and dynamically linked executable programs EXECVE system calls are returned:


对于静态链接的ELF可执行文件,这个程序入口就是ELF文件的文件头中e_enEry所指的地址;对于动态链接的ELF可执行文件,程序入口点是动态链接器。
  当ELF被load_elf_binary()装载完成后,函数返回至do_execve()在返回至sys_execve()。在load_elf_binary()中系统调用的返回地址已经被改成ELF程序的入口地址了。
所以当sys_execve()系统调用从内核态返回到用户态时,EIP寄存器直接跳转到了ELF程序的入口地址,于是新的程序开始执行,ELF可执行文件装载完成。
3. The system call processing kernel corresponding to the exec* function implements the system call through the following steps:

(1) The Exec system call requires an executable file name in the form of a parameter and stores the parameter for future use. Along with the file name, additional parameters are provided and stored, for example, if the shell command is "ls-l", then LS is the file name, and the-l as an option is stored together for future use.

(2) Now, the kernel parses the file pathname to get the index node number of the file. Then, access and read the index node. The kernel knows that for any shell command, it first searches in the/bin directory.

(3) The kernel determines the user class (owner, group, or other). You then get the Execute (X) permission on the user class of the executable file from the index node. The kernel checks whether the process is authorized to execute the file. If not, the kernel prompts for an error message and exits.

(4) If everything is OK, it accesses the header of the executable file.

(5) The kernel now loads the executable file of the program that it expects to use (for example, LS in this example) into the zone of the child process. However, the size of the different areas required for "LS" differs from the areas where the child processes already exist because they are copied from the parent process. Therefore, the kernel frees all the areas associated with the child process. This is the zone in which the new program in the executable image is ready to be loaded into the child process. Frees space after calling a storage parameter for the system that is only stored in memory. They are stored to avoid the "LS" executable code overwriting them and causing them to be lost. Store it in the right place, depending on how it is implemented. For example, if "LS" is a command, "-L" is its parameter, then "-l" is stored in the kernel area. The binary code of the "LS" utility in the/bin directory is the content that the kernel will load into the child process memory space.

(6) Then, the kernel queries the executable file (for example, LS) after the head of the mirror to allocate a new area of the desired size. At this point, establish a link between the regional table and the page map table.

(7) The kernel Associates these zones with sub-processes, which is the creation of links between the regional table and the P-zone table.

(8) The kernel then loads the contents of the actual zone into the allocated memory.

(9) The kernel creates a save register context using the Register initial value in the header of the executable file.

(10) At this point, the child process ("LS" program) has been run. Therefore, the kernel plugs into the appropriate location in the Ready process list based on the child process priority. Finally, the child process is dispatched.

(11) After the child process is dispatched, the context of the process is generated by the Save register context described in the preceding (9). Then, the PC, SP and so on have the correct value.

(12) The kernel then jumps to the address indicated by the PC. This is the address of the first executable instruction in the program to be executed. Now start a new program such as "LS". The kernel gets the parameters from the predetermined area stored in step (5) and generates the desired output. If the child process executes in the foreground, the parent process waits until the child process terminates, otherwise it continues execution.

(13) The child process terminates, into the zombie state, the expected use of the program has been completed. Now the kernel sends a signal to the parent process indicating "child process death" so that the parent process can now be awakened.

If the child process opens a new file, the user File descriptor table, open file list, and INODE tables structure of the child process are different from the parent process. If the child process calls another subroutine, the Execute/branch process is repeated. This creates a process structure at a different depth level.

4. Summary--"Linux kernel Load: Dynamic link and static link"
    • Static link library creation:
Gcc-c FUN.C

AR cqs libfun.a fun.o

GCC call.c-static-l.-lfun-o fun_static_call

./fun_static_call



    • Dynamic Link library creation:
/* Generate a dynamic-link library */
GCC Fun.c-fpic-shared-o libfun.so
/*-L specifies the path to find the dynamic link library,-lfun is actually looking for libfun.so*/
    • 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.

Resources:
    • Loading of executable programs: http://blog.chinaunix.net/uid-28281877-id-3751107.html
    • Baidu Library

20135302 Wei quiet--linux course Seventh Week experiment and summary

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.