Hi Mark,
On Thu, Nov 26, 2026 at 8:55 PM 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]>
LGTM.
Aaron
> ---
>
> V2 simplified copy assignment to fix bracket matching
>
> libelf/elf32_getphdr.c | 16 +++++-----------
> 1 file changed, 5 insertions(+), 11 deletions(-)
>
> diff --git a/libelf/elf32_getphdr.c b/libelf/elf32_getphdr.c
> index e41c660daf54..54aab1778160 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,11 @@ __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);
> + size_t align = __alignof__ (ElfW2(LIBELFBITS,Phdr));
> + bool copy = ((uintptr_t) file_phdr & (align - 1)) != 0;
> if (! copy)
> notcvt = file_phdr;
> else
> --
> 2.56.1
>