]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Fix elf_begin.c build on 32bit arches.
authorMark Wielaard <mark@klomp.org>
Tue, 14 Nov 2023 20:34:50 +0000 (21:34 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 14 Nov 2023 20:34:50 +0000 (21:34 +0100)
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 <ppluzhnikov@google.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf_begin.c

index 9f8196b6dc0167698450e36715cec78461fba3fa..dcaad8eeab8f63a715cb85a2f5028953591942ea 100644 (file)
@@ -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)
            {