Virtual address space

In theory, the size of the virtual address space depends on the number of bits in the CPU. The 32-bit CPU has a maximum virtual address space of 322-1. The number of bits in the pointer size is the same as the number of bits in the virtual space. 32-bit is 4 bytes, and 64-bit is 8 bytes. A virtual space is controlled by the operating system, and not the entire virtual space can be freely accessed by processes.

loading

Instructions and data need to be loaded into memory (physical memory) before they can be executed. The memory required by a process is often greater than the amount of physical memory, requiring a dynamic loading mechanism.

1. Overwrite loading

Programs are divided into small modules, with additional helpers to manage module loading, and module dependencies appear in a tree structure.

2. Page mapping

Instructions and data blocks in memory and on disk are divided into pages of the same size and loaded in pages as the smallest unit.

Process establishment

  • Create a separate virtual space.
  • Read executable header to establish the mapping between virtual space and executable.
  • Set the instruction register to the entry address of the executable file and start running.

Creating a virtual address space is actually creating a page table that stores the mapping between virtual space and physical memory space.

The program header table is the attribute information of each Segment after the ELF file is divided into segments. The object file will not be loaded, so there is no program header table

Elf file type is EXEC (Executable file)
Entry point 0x8048736
There are 6 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0xa11c1 0xa11c1 R E 0x1000
  LOAD           0x0a1f5c 0x080eaf5c 0x080eaf5c 0x01024 0x01e48 RW  0x1000
  NOTE           0x0000f4 0x080480f4 0x080480f4 0x00044 0x00044 R   0x4
  TLS            0x0a1f5c 0x080eaf5c 0x080eaf5c 0x00010 0x00028 R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x0a1f5c 0x080eaf5c 0x080eaf5c 0x000a4 0x000a4 R   0x1
Copy the code

Distribution of process virtual space

Section and Segment

ELF files are stored by Section in link terms, and by Segment in load terms. If the Section is the smallest unit and is divided into several pages for loading, there will be a lot of in-page fragmentation. Several sections can be combined into one Segment for paging (same permissions, attributes).

View the process virtual space

08048000-080ea000 r-xp 00000000 08:01 3149407    /home/apollo/Desktop/source.elf
080ea000-080ec000 rw-p 000a1000 08:01 3149407    /home/apollo/Desktop/source.elf
080ec000-080ed000 rw-p 00000000 00:00 0 
082fa000-0831c000 rw-p 00000000 00:00 0          [heap]
b7fe8000-b7feb000 r--p 00000000 00:00 0          [vvar]
b7feb000-b7fed000 r-xp 00000000 00:00 0          [vdso]
bf9a8000-bf9c9000 rw-p 00000000 00:00 0          [stack]
Copy the code