Linux Kernel Analysis Seventh Week study summary: Executable loader

Source: Internet
Author: User
Tags call back

One, to get an executable procedure 1. preprocessing, compiling, linking

GCC Hello.c-o hello.exe
GCC compiles the source code to generate the final executable binaries, and the GCC background implicitly performs four stage steps.
preprocessing = compilation = compilation = Link
Preprocessing: The compiler compiles the header files contained in the C source code and performs the macro substitution work.
GCC-E Hello.c-o hello.i
Compile: 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 checking the correct, GCC to translate the code into assembly language.
Gcc–s Hello.i–o Hello.s
-S: This option compiles without assembly, generating assembly code.
Assembly: Turns the. s file generated during the compile phase into binary target code. GCC–C Hello.s–o hello.o
Link: the compiled output. o file is linked to the final executable file.
GCC Hello.o–o Hello
Run: If the link is not specified by-O, the resulting executable file defaults to A.out
./hello

2. Destination file format

(1) file format
A.out is the oldest executable file format
Note: abi--Application Binary interface
(2) Elf classification
relocatable Files: Save code and appropriate data to create an executable file or a shared file with other object files.
Executable: holds a program to execute, which indicates how EXEC (BA_OS) creates the process image of the program.
Shared files: Save the code and the appropriate data to be linked by the following two linker. • The first is the connection editor [see LD (SD_CMD)], which can be used to relocate and share an object file to create an additional object.
The second is a dynamic linker that unites an executable file and other shared object files to create a process image.
The object file participates in the link (creation) and execution of the program.
(3) Elf head
View the elf file header: readelf
At the beginning of the file Save:

    • Roadmap: Describe the organization of the document
    • Program Header table: tells the system how to create a memory image of a process
    • Section Header table: Describes the section information for a file. (each section has a portal in this table, which gives it information)
      When a process image is created or added, the system theoretically copies a segment of a file into a virtual memory segment.

      3. Statically linked elf executable file and process address space

      Entry point: The program starts with 0x804800.
      The first line of code that the executable file loads into memory to begin execution.
      A general static link will put all the code in the same code snippet.
      A dynamically connected process will have multiple code snippets.

      Ii. execution Environment for executable procedures 1. command-line arguments and the shell environment

      List directory information under/usr/bin
      $ ls-l/usr/bin
      The shell itself does not limit the number of command-line arguments, the number of command-line arguments is limited by the command itself
      int main (int argc, char argv[], char envp[])
      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[]);
      The library function exec* are EXECVE encapsulation routines

      2. Saving and passing command-line arguments and shell environment variables

      Shell program = Execve = Sys_execve
      command-line arguments and environment strings are placed in the user-state stack

Copy the new program stack when it is initialized

    1. Executable program dynamic Link

(1) Dynamic Link

In fact, the loading process is a breadth traversal, and the object to traverse is the "Tree of dependency".

The main process is dynamic linker completion, User Configuration completed.

(2) dynamic Link when loading
/ Prepare. So file /
Shlibexample.h (1.3 KB)-Interface of Shared Lib Example
SHLIBEXAMPLE.C (1.2 KB)-Implement of Shared Lib Example

/ compile into libshlibexample.so file /
$ gcc-shared Shlibexample.c-o Libshlibexample.so-m32

/ Use the library file (because the header file is already included so you can call the function directly)/
Sharedlibapi ();

(3) dynamic link at runtime
Dllibexample.h (1.3 KB)-Interface of dynamical Loading Lib Example
DLLIBEXAMPLE.C (1.3 KB)-Implement of dynamical Loading Lib Example

/ compile into libdllibexample.so file /
$ gcc-shared Dllibexample.c-o Libdllibexample.so-m32

/ Use library files /
void * handle = Dlopen ("libdllibexample.so", rtld_now);//load in first
Int (*func) (void);//declaring a function pointer
Func = Dlsym (handle, "DYNAMICALLOADINGLIBAPI");//Find function pointers by name
Func (); Invoke declared function

(4) Operation
$ gcc Main.c-o main-l/path/to/your/dir-lshlibexample-ldl-m32
$ Export Ld_library_path= $PWD
/Add the current directory to the default path, otherwise main cannot find the dependent library file, and of course you can copy the library file to the default path. /

Iii. Loading of executable programs
    1. SYS_EXECVE Kernel processing process

(1) New executable program starting point
Typically the address space is 0x8048000 or 0x8048300

(2) Execve and fork
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 two times back, the first return to the parent process continues to execute downward, the second is the child process returned to ret_from_fork and then normal return to the user state.

When the EXECVE executes, it falls into the kernel state, overwrites the currently executing program with the program loaded in the EXECVE, and returns to the new executable starting point when the system call returns.

Execve
Execute to executable program, fall into kernel
To construct a new executable file, overwrite the original runnable program
Returns to the new executable as a starting point (that is, the main function)
Need to construct its execution environment;
The shell calls Execve to pass the command-line arguments and environment parameters to the executable's main function, first the function call parameter passing, and then the system calls the parameter pass.

(3) Statically linked executable program and dynamically linked executable program EXECVE system calls are returned in different
Static Link: elf_entry points to the head of the executable file, typically the main function, which is the starting point for the new program execution.
Dynamic Link: elf_entry points to the starting point of the LD (dynamic linker) and loads the Load_elf_interp

    1. Loading of dynamically linked executable programs

(1) Where is the starting point for executable file execution? How can I get the EXECVE system call back to the user state when the new program is executed?
Modify the EIP of int 0x80 into the kernel stack by modifying the value of the EIP in the kernel stack as the starting point for the new program.

(2) How the Linux kernel supports a variety of different executable file formats
static struct LINUX_BINFMT elf_format//declares a global variable = {
. module = This_module,
. load_binary = load_elf_binary,//Viewer automatic execution
. Load_shlib = Load_elf_library,
. Core_dump = Elf_core_dump,
. Min_coredump = Elf_exec_pagesize,
};

static int __iit init_elf_binfmt (void)
{N
REGISTER_BINFMT (&elf_format);//Register the variable into the kernel list and find the format of the file in the list
return 0;
}

(3) Dynamic Link
The executable needs to rely on a dynamic-link library, and the dynamic-link library may rely on other libraries to form a diagram-the dynamic-link library generates a dependency tree.
Rely on the dynamic linker to load the library and parse it (this is the traversal of a graph), load all the required dynamic link libraries, and then LD give the CPU control to the executable program
The process of dynamic linking is primarily a dynamic linker that works, not the kernel.

Four, the experiment

Initialize the environment:

Tracking:

Five, impressions

The content of this week's study on the semester in Lou teachers involved, so also more cordial, the process of generating executable files, but also learned the static library, dynamic library. The four steps of preprocessing, compiling into assembly instructions, becoming binary code, and finally executing executable files are deeply imprinted in our minds.

Linux Kernel Analysis Seventh Week study summary: Executable loader

Related Article

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.