I didn't explain those fields because it would've taken a while and I didn't think it was as important as the purpose the segments served. Lucky for you, I can answer your questions:
Offset is the location in the ELF file where the segment is defined. Align, just like data alignment(by DWORD or something else), is usually a power of 2 which states that the (VirtAddr - Offset) is divisible by Align.
That's a good question. I'm going to assume you know what virtual memory is. In the libc source code, virt addr is used to memmap the segment into memory at that address (w/ alignment and offset in the calculation). I don't think the modern libc code even uses physaddr anymore which makes me assume it's an entity reserved for when there's no memory virtualization, which probably is the case for embedded applications but I'm not an embedded systems programmer ¯_(ツ)_/¯
yes, the physaddr field is used for e.g. when e.g. a bootloader loads a kernel binary in ELF format, but on an OS with no direct access to physical addresses it is completely ignored. you can actually write anything into it
Thanks for answering. I get the PhysAddr part, but still a bit confused about the Alignment part.
From what I understand, you are saying Offset is the physical position and VirtAddr is the loaded position. Then this must leads to (VirtAddr - Offset) being a multiple of PageSize already (says 4KB), how exactly does Align play a role here ? Can I change it per segment and what would happen ?
Err, not quite. Offset is the location in the ELF file, not the executable. However, the ELF file gets mapped into the executable in a read-only segment most of the time. The alignment is just a requirement of the ABI, and it does not have to be a page-size alignment. The page-size alignment assumption you have may come from the LOAD segments but this is not true for all segments. You don't want to change alignment for LOAD because it's used for memmapping - you'll get in trouble because memmap will straight up die if you don't pass it an addr that is page-aligned.
2
u/cloveistaken May 24 '20
In the program header section I think you didn't explain every fields so I have a couple of questions:
What are Offset and Align here ?
What is the difference between VirtAddr and PhysAddr and when they are different in value ?