What exactly is the purpose of the p_align field? Every source online has only said something vague about it saying the "required alignment" of segment or only that its a value so that p_vaddr = p_offset mod p_allign. Using readelf, I can see that LOAD segments have p_align of 4KB. The modulo statement with p_align being 4KB mathematically guarantees its possible to mmap file pages to memory pages to correctly load the segment. This is the only utility I see for p_align. But it is anyways required for p_align of LOAD segments to be page-sized, so is there no purpose?
What exactly is the point of the p_align field in the program headers of an ELF file
60 Views Asked by jellojar At
1
To make it possible to use
mmapto bringLOADsegments into memory.Some architectures have a different page size. For example
powerpc64page size is 64KB, and binaries linked for it will havep_align == 0x10000.Further,
x86_64supports multiple page sizes, and if you want to be able to use e.g. 2MB pages for your binary, then you need to set alignment appropriately:Indeed, 2MB pages used to be the default on
x86_64, precisely because someone might decide to use 2MB (aka huge) pages, until the default was changed to 4KB in this commit.