Seventh week of Linux experiment and summary

Source: Internet
Author: User

Jing Mengxin

Original works

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Seventh week how the Linux kernel loads and starts an executable program one: 1. How can the executable program be produced?

In Linux systems, executable programs are generally preprocessed, compiled, compiled, linked, executed, and so on.

Compilation process preprocessing: Gcc–e hello.c–o hello.i;  Gcc–e call CPP to generate intermediate file compilation: Gcc–s hello.i–o Hello.s;  Gcc–s call CCL translated into assembly file assembly: Gcc–c hello.s–o hello.o;  Gcc-c call as translated to relocatable destination file link: gcc hello.o–o hello;      Gcc-o call ld** to   create executable target file
64-bit machine available parameters-m32
CPP refers to preprocessing a compiled file

  

2. target file Format elf

The elf file is already a binary compatible file that adapts to a certain CPU architecture.

The default Elf header loading address is 0x8048000, with the head probably going to 0x48100 or 0x483000, after the executable file is loaded into memory

The first code address to execute

To view the contents of an executable file header:readelf -h;头部后是代码和数据。

. o files, executable files, are target files, generally using the same file format.
Common file formats:

A.out     COFF    pe-windows on the     elf-linux

ABI: Application Binary interface

There are three main file formats in the elf file format:

1: relocatable files    are mostly. o files, save code and appropriate data, and other object files to create an executable file or share a file 2: The executable file    holds a program to execute, indicating how exec (Ba_os) created the program process image. 3: Share the destination file    save code and appropriate data to link with the linker. The linker is divided into dynamic and static: Link editor, static link, and other relocatable, shared target files create other target file dynamic linker, even drink an executable file and other shared destination files to create a process image

File format

The object file participates in the program's join (creating a program) and the execution of the program (running a program).
The object file format provides a convenient and efficient way to view the contents of a file in parallel.

3 statically linked Elf executable file with process address space

The 32-bit x86 process address space is 4g,1g a kernel space.

A general static link places all the code in one code snippet, and a dynamically linked process has more than one code snippet.
II. executable programs, shared libraries, and dynamic links

1: New executable starting point:

  地址空间为0x8048000或0x8048300。

    work before loading executable programs:    The shell itself does not limit the number of command-line arguments, and the number of command-line arguments is limited by the command itself. The    shell invokes Execve to pass command-line arguments and environment parameters to the main executable function, passing and saving command-line arguments    and environment variables.

2: How do command line arguments and environment variables get into the stack of the new program?

The Execve:main function needs to construct his execution environment, execute the executable program, pass the line parameters and environment variables through the system call to the kernel handler, and then the kernel handler constructs a new executable to initialize the stack of new executables and return to the new executable program.

  

3: Statically linked executable program and dynamically linked executable program EXECVE system calls are returned differently

 静态链接:elf_entry指向可执行文件的头部,一般是main函数;

  动态链接:elf_entry指向ld的起点。

4:SYS_EXECVE Internal processing process

      int do_execve (struct filename *filename, in function return, make a do_execve const char __user *const __user *__argv,        {struct User_arg_ptr argv = {. Ptr.native = __argv};        struct User_arg_ptr envp = {. Ptr.native = __ENVP};      return Do_execve_common (filename, argv, ENVP);          } syscall_define3 (Execve, const char __user *, filename, const char __user *const __user *, argv,    const char __user *const __user *, envp) {return do_execve (getname (filename), argv, ENVP);       Go to do _ execve _ Common Function} #ifdef config_compat Compat_syscall_define3 (execve, const char __user *, filename, Const compat_uptr_t __user *, argv, const compat_uptr_t __user *, envp) {return Compat_do_execve (Getnam   E (filename), argv, ENVP);        } #endif if (!try_module_get (fmt->module)) continue;        Read_unlock (&binfmt_lock);        bprm->recursion_depth++; retval = Fmt->load_binary (BPRM);        Read_lock (&binfmt_lock);        PUT_BINFMT (FMT); bprm->recursion_depth--; in this loop is looking for the ability to parse the current executable file.

  

Three: Experiment gdb trace SYS_EXECVE kernel function processing process

1: Kernel Boot environment Build update command

2: Dynamic Link code executed in EXEC function




3:hello.c


4:GDB kernel tracking and setting breakpoints











Experimental Summary

The process of dynamic linking is primarily a dynamic linker that works, not the kernel.

Dynamic linking is divided into executable program loading dynamic link and runtime dynamic link

The new program begins execution, and the elf executable is loaded.





Seventh week of Linux 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.