What happened before the Linux program was executed--the programmer's self-cultivation

Source: Internet
Author: User

is the kernel done with the loading and linking of the program?

Knowing that Linux has fork and exec system calls, can create new processes and execute executable files, fork is the kernel's own data structure maintenance, understandable, then exec specify a file, how to load files and dynamic links?

Interpreter
    • The kernel maps to user-space memory when executing elf files
    • Looking for special sub-segments in Elf interp
    • Kernel mapping interpreter to user space, control over to interpreter
    • Interpreter complete load, link, execute user program

Elf interpreteris specified in the Elf executable, and Linux is probably the /lib64/ld-linux-x86-64.so.2 full path of a library, and the kernel gives control to interpreter, an executable memory code. This is not part of the kernel, which is usually provided by GLIBC.

So, it is necessary to emphasize that Linux is only the kernel, and the user space is the operating system, starting from the GLIBC layer. In theory, the kernel just contracts a concept of interpreter Such an entry, to the user space Declaration, after calling exec Syscall, loading the executable file into memory, memory mapping, Then the PC (program counter) starts the application from interpreter, and the control is handed over to the user space.
Although an OS has only one interpreter, it is also possible for a third party to develop a single interpreter without using the interpreter provided by GLIBC. Interpreter conventions How the application orchestrates instructions (including whether dynamic linking is supported) Syscall already provides the possibility that MMAP provides the underlying implementation for dynamic linking.

Dynamic Link Library

The address space of each process covers the dynamic-link library, except that the dynamic-link library has only one copy in memory, and the memory map of the kernel reaches the effect that multiple process address spaces can cover.
By /proc/<pid>/maps looking at the mapped file, this mapping differs from loading, which means that the symbol linkingis required.

After kernel completes the elf file to the memory mapping, kernel the dynamic linker also mapping to the program address space. The kernel also mapping other such as BSS, putting parameters and environment on the stack. After determining the start address of the ld.so (or by reading a specific section), set the start address of the process. The starting position of the special section logger in Elf.

ld.so Check all the dynamic link libraries in the Elf file, mapping to memory, but not immediately link, because mapping is fast, and symbolic links are time-consuming.
ldd /bin/lsYou can view the elf's dynamic link library, note that LDD is a tool application, and ld.so is a library that is called interpreter and needs to complete link and load.

Dynamic link library Multiple programs are shared, why does the data not interfere?

There is a function in the library, the function has a local variable, and the function has only one copy, why do multiple programs call the same function, local variables do not cause interference?

Guess: Local variables are allocated on the stack, and each process's stack is independent. Declaring a local variable in a function requires allocating space, which is the space on the stack, and the access to the local variable in the subsequent instruction of the library function is offset by the stack base address, and even a variable name is not saved to the code snippet of the dynamic library. The variable "slot" needs to be prepared on the stack of the process before the library function is called

What happened before the Linux program was executed--the programmer's self-cultivation

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.