[PATCH] elf: Set p_align to the common page size if possible

Alan Modra amodra@gmail.com
Wed Dec 22 08:57:10 GMT 2021


On Mon, Dec 20, 2021 at 07:50:30AM -0800, H.J. Lu wrote:
> On Wed, Dec 15, 2021 at 8:32 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > Currently, on 32-bit and 64-bit ARM, it seems that ld generates p_align
> > values of 0x10000 even if no section alignment is greater than 0x1000.
> > The issue is more general and probably affects other targets with
> > multiple common page sizes.
> >
> > While file layout absolutely must take 64K page size into account, that
> > does not have to be reflected in the p_align value.  If running on a 64K
> > kernel, the file will be loaded at a 64K page boundary by necessity. On
> > a 4K kernel, 64K alignment is not needed.
> >
> > The glibc loader has been fixed to honor p_align:
> >
> > https://sourceware.org/bugzilla/show_bug.cgi?id=28676
> >
> > similar to kernel:
> >
> > commit ce81bb256a224259ab686742a6284930cbe4f1fa
> > Author: Chris Kennelly <ckennelly@google.com>
> > Date:   Thu Oct 15 20:12:32 2020 -0700
> >
> >     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

The ELF gABI says of p_align:
p_align
    As ``Program Loading'' describes in this chapter of the processor
    supplement, loadable process segments must have congruent values
    for p_vaddr and p_offset, modulo the page size. This member gives
    the value to which the segments are aligned in memory and in the
    file. Values 0 and 1 mean no alignment is required. Otherwise,
    p_align should be a positive, integral power of 2, and p_vaddr
    should equal p_offset, modulo p_align.

Notice the parallel between page size and p_align.  I believe it was
reasonable for ld to set p_align to the maximum page size supported by
the segment layout, if you treat p_align as a hint.  Now that p_align
is treated as required alignment, I agree that we should not set
p_align to maxpagesize.  However, why choose commonpagesize?  Is
anything more than the maximum section alignment in any given segment
needed?

Imagine a binary created with maxpagesize of 64k, commonpagesize of
16k, and running with actual page size of 4k.  If section alignment
requirement is less than 4k, why should it be aligned to 16k?

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list