"Programmer self-accomplishment" reading notes-executable file loading and process

Source: Internet
Author: User

1 Load Mode: page map. The operating system divides physical memory into pages (such as 16K per page size), and executable files are divided by page. Each time a page is needed, it is loaded into a free page in physical memory. If there is no free page, a certain policy can be taken to swap out a page in physical memory.

2 The process of loading executable files.

(1) Create a virtual address space. Assign a page directory. This step is to establish the mapping between virtual space and physical space.

(2) Read the executable file header and establish a mapping between the virtual space and the executable file. The purpose of this mapping relationship is that when a page fault occurs later, the operating system is able to know the location of the missing pages in the executable file from this mapping relationship.

(3) Set the instruction register of the CPU to the entry of the executable file and start running.

3 The concept of VMA. such as read-only data placed in a VMA, readable executable placed in a VMA, so VMA is the process of virtual space for a continuous paragraph of the salutation.

4 linking views and execution views. In an executable file, from a linked view, it is stored by section. Because each section occupies several pages at the time of loading, even if a section has only dozens of bytes to occupy a page, this can cause a waste of physical memory. So, in fact, the operating system centralizes the same sections with the same permissions, called segment. So from the execution view of the executable file, it is divided according to segment.

There are many segment in the 5 executable, and the contents of each segment include:

(1) P_type: type. One of the most important types is load. Only this type of segment is loaded into physical memory.

(2) P_offset: offset in file

(3) P_vaddr: Offset in the process virtual space

(4) P_filesz: size in file

(5) P_memse: The size of the virtual process space (this is the same as P_filesz)

(6) P_flags: Permission, read, write, execute

(7) P_align: Alignment Properties

6 stacks and stacks. In addition to the segment load in the executable file is a VMA, there are heaps and stacks are also two VMA. The heap grows upward and the stack grows downward

7 The alignment of the segment. Assume that the size of each page is 4096 bytes. In fact, segment in the executable file after loading, not every segment of the fact that the address is a multiple of 4096. Because there are a lot of segment are very small, this will also cause a lot of waste. This is actually done by storing each segment sequentially into physical memory, so that a page in one physical memory may contain two (or more) segment, so the page is mapped two times, respectively, to each segment corresponding VMA in the process virtual space. (Executables have several segment, there are several VMA in the process virtual space).

8 Linux Mounts the elf executable process. First Bash creates a process by fork, and the new process invokes the Elf executed by Execve. The overall process is to read the file header, judging by the magic number is the elf file, or Java file, or what script file. In the case of an elf file, the program that loads the elf is called, the program gets the number of segment, and then loads the return address of the system call to the entry address of the executable file. Then it returns, and the executable begins execution.

"Programmer self-accomplishment" reading notes-executable file loading and process

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.