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

H.J. Lu hjl.tools@gmail.com
Wed Dec 22 14:38:54 GMT 2021


On Wed, Dec 22, 2021 at 12:57 AM Alan Modra <amodra@gmail.com> wrote:
>
> 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?

I choose commonpagesize due to this glibc bug:

https://sourceware.org/bugzilla/show_bug.cgi?id=28688

It has been fixed in glibc 2.35.  But linker output must work on
existing glibc binaries.

> 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?
>

Here is the v2 patch to set p_align to the minimum page size, instead of
the common page size.

OK for master?

Thanks.

--
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v2-0001-elf-Set-p_align-to-the-minimum-page-size-if-possi.patch
Type: application/x-patch
Size: 13030 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20211222/15536548/attachment-0001.bin>


More information about the Binutils mailing list