If there is no readonly section, we map:
- The ELF header at imageBase+maxPageSize
- Program headers at imageBase+maxPageSize+sizeof(Ehdr)
- The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD)).
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
// PHDR at 0x401034, should be 0x400034 PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4 // R PT_LOAD contains just Ehdr and program headers. // At 0x401000, should be 0x400000 LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000 LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
- createPhdrs allocates the headers to the R PT_LOAD.
- fixSectionAlignments assigns imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers) (formula: alignTo(dot, maxPageSize) + dot % config->maxPageSize) to addrExpr of .text
- allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
- allocateHeaders sets address of ELF header to addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in getInitialDot. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
From a double check, it seems like unconditional isn't true any more. In createPhdrs() nmagic and omagic don't add the ELF Headers into the loadable segment, looks like I missed updating the comment when adding omagic and nmagic.
Perhaps update the comment to: