Home > Blockchain >  How does the ELF loader mmap sections smaller than a page size?
How does the ELF loader mmap sections smaller than a page size?

Time:02-24

The mmap man page states "A file is mapped in multiples of the page size.", which on my system is 4096 bytes (0x1000). Yet ELF files can have sections smaller than 0x1000, for example:

[Sections]

nth paddr        size vaddr       vsize perm name
―――――――――――――――――――――――――――――――――――――――――――――――――
0   0x00000000    0x0 0x00000000    0x0 ---- 
1   0x00001000  0xb93 0x00401000  0xb93 -r-x .text
2   0x00002000  0x660 0x00402000  0x660 -r-- .rodata
3   0x00002660  0x304 0x00402660  0x304 -r-- .eh_frame
4   0x00003000   0xe6 0x00404000   0xe6 -rw- .data
5   0x000030e6    0x0 0x00404100  0x100 -rw- .bss
6   0x000030e6   0x2e 0x00000000   0x2e ---- .shstrtab

How does ELF load and mmap them? It can't naively round up to 0x1000, because then the next section would fail. Does it somehow detect this and group the contiguous pages together? What if they have different permissions?

CodePudding user response:

How does ELF load and mmap them?

The ELF loader doesn't look at, or cares about any sections. In fact section headers can be fully stripped, and the executable will continue to work just fine. Sections are used only at (static) link time.

What the loader does care about are segments. You can see segments (and mapping of sections to segments) with readelf -Wl a.out.

And yes: segments can have non-even page length. The loader doesn't care -- it simply performs mmap() (the kernel rounds the mapping up to whole page size).

See also this partially relevant answer.

CodePudding user response:

It will probably just dedicate a full page to your request, but as you stated, the amount of memory conceeded is only the amount you requested. This is made to neither conflict with the possibility of arbrtrary requests, and to be system independent. In case you resize the amount of memory, to take into account the exact amount of memory you have asked for.

  • Related