Hi Mark,

On Mon, Nov 23, 2026 at 11:18 AM Mark Wielaard <[email protected]> wrote:
>
> If the Elf file was mmapped and of the same endianness we can use the
> phdr table directly when correctly aligned, or if ALLOW_UNALIGNED. If
> the Elf file was mmapped and of different endianness then we convert
> the phdrs directly when the mmapped headers were correctly aligned, or
> if ALLOWED_UNALIGNED, otherwise we made a copy first. It is unlikely
> these tables are unaligned in a proper Elf file, so drop the
> ALLOW_UNALIGNED condition.
>
>         * libelf/elf32_getphdr.c (getphdr_wrlock): Drop the
>         ALLOW_UNALIGNED condition.
>
> Signed-off-by: Mark Wielaard <[email protected]>
> ---
>  libelf/elf32_getphdr.c | 17 ++++++-----------
>  1 file changed, 6 insertions(+), 11 deletions(-)
>
> diff --git a/libelf/elf32_getphdr.c b/libelf/elf32_getphdr.c
> index e41c660daf54..e9828b46f97b 100644
> --- a/libelf/elf32_getphdr.c
> +++ b/libelf/elf32_getphdr.c
> @@ -103,9 +103,8 @@ __elfw2(LIBELFBITS,getphdr_wrlock) (Elf *elf)
>           void *file_phdr = ((char *) elf->map_address
>                              + elf->start_offset + ehdr->e_phoff);
>           if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
> -             && (ALLOW_UNALIGNED
> -                 || ((uintptr_t) file_phdr
> -                     & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0))
> +             && ((uintptr_t) file_phdr
> +                 & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) == 0)
>             /* Simply use the mapped data.  */
>             elf->state.ELFW(elf,LIBELFBITS).phdr = file_phdr;
>           else
> @@ -129,16 +128,12 @@ __elfw2(LIBELFBITS,getphdr_wrlock) (Elf *elf)
>                  byte order.  */
>
>               if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
> -               {
> -                 assert (! ALLOW_UNALIGNED);
> -                 memcpy (phdr, file_phdr, size);
> -               }
> +               memcpy (phdr, file_phdr, size);
>               else
>                 {
> -                 bool copy = ! (ALLOW_UNALIGNED
> -                                || ((uintptr_t) file_phdr
> -                                    & (__alignof__ (ElfW2(LIBELFBITS,Phdr))
> -                                       - 1)) == 0);
> +                 bool copy = ((uintptr_t) file_phdr
> +                               & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1
> +                                  != 0));

The closing brackets are misplaced. As written this is equivalent to
  bool copy = (uintptr_t) file_phdr & 1;
which sets copy to true iff file_phdr is odd. Odd addresses are always
misaligned but this skips copying a misaligned file_phdr when the
address is even.

It should instead be written
  ((uintptr_t) file_phdr
   & (__alignof__ (ElfW2(LIBELFBITS,Phdr)) - 1)) != 0;

The rest of the patches in this series LGTM except there is still one
ALLOW_UNALIGNED remaining in gelf_xlate.c which should also be removed
in this series.

Aaron

>                   if (! copy)
>                     notcvt = file_phdr;
>                   else
> --
> 2.56.1
>

Reply via email to