From: Mark Wielaard Date: Tue, 14 Nov 2023 20:34:50 +0000 (+0100) Subject: libelf: Fix elf_begin.c build on 32bit arches. X-Git-Tag: elfutils-0.191~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f84f1cd7e296bf223cb45b5469978d4bea82cec0;p=thirdparty%2Felfutils.git libelf: Fix elf_begin.c build on 32bit arches. On 32bit architectures gcc produces an error: elf_begin.c: In function ‘file_read_elf’: elf_begin.c:495:30: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast] elf->state.elf64.shdr = (Elf64_Shdr *) (ehdr + e_shoff); ^ This is because we are adding an uintptr to an Elf64_Off which promotes the result to a 64bit value. Fix this by casting the e_shoff to an ptrdiff_t. This is fine since the mmap of the file would have failed if it didn't fit in the 32bit address space and we check that e_shoff fits inside the image. * libelf/elf_begin.c (file_read_elf): Cast e_shoff to ptrdiff_t before adding to ehdr. Suggested-by: Paul Pluzhnikov Signed-off-by: Mark Wielaard --- diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c index 9f8196b6d..dcaad8eea 100644 --- a/libelf/elf_begin.c +++ b/libelf/elf_begin.c @@ -492,7 +492,7 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident, goto free_and_out; if (scncnt > 0) - elf->state.elf64.shdr = (Elf64_Shdr *) (ehdr + e_shoff); + elf->state.elf64.shdr = (Elf64_Shdr *) (ehdr + (ptrdiff_t) e_shoff); for (size_t cnt = 0; cnt < scncnt; ++cnt) {