elf

p_align: How do ELF Loaders handle the possibility of two loadable segments occupying same virtual page?


It's from my understanding that the 'p_align' field of program headers allows the loader to easily determine if it can just do a simple mmap of file pages to memory pages in order load a section.

However, I was thinking of the following edge case: Suppose that the end of one loadable segment in main memory barely goes into a new page (lets call this page P). Then, another loadable segment needs to be loaded in memory, starting halfway in P.

The memory that these two segments occupy are non-overlapping, but the pages that they occupy are overlapping, so a simple mmap cannot be done.

Do loaders just need to check for this case and then manually load in contents to the pages containing multiple segments? Or is there some guarantee that can be made that segments don't share virtual pages?

I would imagine such a guarantee would greatly simplify the work of loaders, but maybe it wouldn't help that much since loaders need to anyways do checks to determine if an ELF file is valid?

As a side question, is the 'p_align' field even really needed? Couldn't the maximum alignment very quickly be determined by counting the trailing zeros in the binary representation of abs(p_vaddr - p_offset)? Furthermore, the man pages https://man7.org/linux/man-pages/man5/elf.5.html State that

Loadable process segments must have congruent values for p_vaddr and p_offset, modulo the page size.

So loaders don't even need to check if loadable segments can/can't be mmapped?


Solution

  • It's from my understanding that the 'p_align' field of program headers allows the loader to easily determine if it can just do a simple mmap of file pages to memory pages in order load a section.

    Your understanding (and the entire premise of your question) is wrong: the LOAD segments always tell the loader how to mmap segments into memory, and .p_align field has nothing to do with that.

    There is no "manually load contents" of any kind in the loader(s).