Questions about p_align and base address

27 views
Skip to first unread message

Matthew Dempsky

unread,
Jun 18, 2014, 5:34:10 PM6/18/14
to gener...@googlegroups.com
I have a few questions about the ELF program header that I'm hoping
someone can help clarify:

1. Does p_align have any significance during program loading? E.g.,
on a system using 4k pages, if a LOAD segment specifies a p_align of
8k, is the system under any obligation to load that segment on an 8k
boundary? (If not, it seems like the p_align is purely informative;
the system only need care whether p_vaddr and p_offset are congruent
modulo 4k, and it wouldn't even matter what p_align's value is.)

2. Must/should p_align be the same for all LOAD segments?

3. The base address is defined as "the difference between the
truncated memory address and the truncated p_vaddr value." What's the
point of specifying "truncated" here? I would think the memory
address and p_vaddr value are constrained to be congruent modulo the
page size; and if not, I'm unable to imagine what the use of
specifying base address would be.

Thanks!

Cary Coutant

unread,
Jun 18, 2014, 6:22:33 PM6/18/14
to gener...@googlegroups.com
> 1. Does p_align have any significance during program loading? E.g.,
> on a system using 4k pages, if a LOAD segment specifies a p_align of
> 8k, is the system under any obligation to load that segment on an 8k
> boundary? (If not, it seems like the p_align is purely informative;
> the system only need care whether p_vaddr and p_offset are congruent
> modulo 4k, and it wouldn't even matter what p_align's value is.)

The exact behavior is really ABI dependent, but, in general, yes,
p_align gives the alignment requirements for the segment when it's
loaded. The linker will have laid out the sections within the segment
to guarantee any alignment requests for those sections, but those
alignments can only be preserved if the loader also preserves the
maximum alignment for the segment as a whole. This is really just a
way of saying that the program loader can map whole pages of the file
directly to memory.

Some loaders may not support or honor p_align values larger than the
largest supported page size. Many systems support only a limited
selection of page sizes.

> 2. Must/should p_align be the same for all LOAD segments?

In general, no, but individual ABIs may differ. In practice, for
systems where the relative position of the load segments remains
constant, the loader will need to honor the largest p_align of all the
load segments.

> 3. The base address is defined as "the difference between the
> truncated memory address and the truncated p_vaddr value." What's the
> point of specifying "truncated" here? I would think the memory
> address and p_vaddr value are constrained to be congruent modulo the
> page size; and if not, I'm unable to imagine what the use of
> specifying base address would be.

First, "base address" is a misnomer. It's not a base address for
anything -- it's a "relocation factor", the difference between
link-time virtual address and load-time virtual address. Specifying
"truncated" addresses is just a way of simplifying the description by
referring to the actual page boundaries that the loader considers.
You're right that, given the congruent-modulo-page-size requirement,
the difference between the truncated values is exactly the same as the
difference between the original values. From the loader's point of
view, though, we're talking about whole pages, so that's really what
matters.

Note that on some systems that use a segmented address space rather
than a flat address space, there's not a single relocation factor for
the whole image, but a separate one for each segment. (E.g., PA-RISC,
Power.)

-cary

Matthew Dempsky

unread,
Jun 20, 2014, 7:58:12 PM6/20/14
to gener...@googlegroups.com
Thanks Cary! That was helpful.
> --
> You received this message because you are subscribed to the Google Groups "Generic System V Application Binary Interface" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to generic-abi...@googlegroups.com.
> To post to this group, send email to gener...@googlegroups.com.
> Visit this group at http://groups.google.com/group/generic-abi.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages