I'm building a small practice OS, I'm writing linker scripts and using ld to produce ELF files that my OS loader will load manually.
Here’s my current understanding of the loading process for ELF executables:
e_phoff, e_phentsize, and e_phnum) etc.type == LOAD into memory.There is a PHDR entry listed in the program headers.
But we already know the program header table’s location and size from the ELF header itself.
So my questions are:
Why does the ELF file include a PHDR entry?
Isn’t it redundant since the ELF header already points to the program header table?
When actually loading the program, do we even use this PHDR segment?
The loaders just iterate through the headers and load segments where type == LOAD, so the PHDR doesn’t seem necessary.
What is the purpose of the PHDR segment?
Why does the ELF file include a PHDR entry? Isn’t it redundant since the ELF header already points to the program header table?
You are assuming that the file (and the Elf{32,64}_Ehdr within it) is available to whatever is looking at the PHDR, but that may not be the case.
When the executable starts, it has no idea which file it came from, and in fact the file may no longer exist on disk at all.
See also this answer.