Hello! How does Linux allocate memory for a process when launching a binary file?

For example, if I have a file of 100M in size, does this mean that when it starts, it will be re-read by the system, placed entirely in memory / virtual memory and run from there (without accessing the disk)? Or is there a section in the file that describes the necessary resources to run the file and as it runs, certain parts of the file are loaded from disk into memory?

1 answer 1

If you don’t go into details, the kernel reads the headers and some service information from the ELF, and then displays some sections in the virtual memory of the process, in particular .text (with "attributes" rx), .rodata (r--) and .data (rw-)). If some pages are already loaded in the page cache (for example, the file remains in it after the usual read / write), then they are immediately connected to the process; if not, they will be loaded only on the first access (see below). After that, control is transferred to the entry point and the process starts. Further, as soon as a process accesses a page that is not loaded into memory, a page interrupt occurs, which is processed by the kernel. It pauses the process and loads the necessary page from disk and / or creates its mapping from virtual to physical memory.

In reality, most executables are linked by dynamic. Therefore, in addition to themselves, their libraries are loaded in a similar way, although the display is not initiated by the kernel itself, but by the dynamic linker (/lib/ld-linux*.so.*) from user space. At the same time, ro-data and library code remain common to all processes.