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

H.J. Lu hjl.tools@gmail.com
Sat Dec 25 03:08:57 GMT 2021


On Fri, Dec 24, 2021 at 7:04 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Dec 24, 2021 at 06:50:30PM -0800, H.J. Lu wrote:
> > On Fri, Dec 24, 2021 at 5:03 PM Alan Modra <amodra@gmail.com> wrote:
> > > I don't see a way to change DT_LOAD p_align that is backwards
> > > compatible, except perhaps using a p_align of zero if the required
> > > segment alignment (as calculated from section alignment) is less than
> > > maxpagesize.  That horrible hack just happens to work for older glibc,
> > > but may break other loaders.
> >
> > Neither 0 nor 1 work for older glibc:
> >
> >           if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0))
> >             {
> >               errstring = N_("ELF load command alignment not page-aligned");
> >               goto lose;
> >             }
> >           if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
> >                                  & (ph->p_align - 1)) != 0))
> >             {
> >               errstring
> >                 = N_("ELF load command address/offset not properly aligned");
> >               goto lose;
> >             }
>
> Zero in p_align triggers neither of these conditions.  Binaries with
> zero p_align run.  I checked.
>

I tried setting p_align to 0 and got

ELF load command address/offset not properly aligned

For

 if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset)
                                 & (ph->p_align - 1)) != 0))


If ph->p_vaddr - ph->p_offset == 0, it will trigger this
error:

  Type           Offset   VirtAddr           PhysAddr
FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000
0x000578 0x000578 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000
0x00012d 0x00012d R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000
0x0000a4 0x0000a4 R   0x1000
  LOAD           0x002df8 0x0000000000003df8 0x0000000000003df8
0x00022c 0x000230 RW  0x1000

-- 
H.J.


More information about the Binutils mailing list