]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libebl: Explicitly check Ehdr can be used in eblopenbackend
authorMark Wielaard <mark@klomp.org>
Tue, 7 Apr 2026 13:25:53 +0000 (15:25 +0200)
committerMark Wielaard <mark@klomp.org>
Tue, 7 Apr 2026 13:25:53 +0000 (15:25 +0200)
Make sure Ehdr is really read into memory, don't use internal elf
state.

* libebl/eblopenbackend.c (openbackend): Use gelf_getehdr
instead of accessing elf state ehdr field directly.

https://sourceware.org/bugzilla/show_bug.cgi?id=34007

Signed-off-by: Mark Wielaard <mark@klomp.org>
libebl/eblopenbackend.c

index dbe4b94741d5e96d4c4a777e6c0741916740f84a..e2a503214713412ff6f0bc7c675c327632eddf61 100644 (file)
@@ -309,17 +309,9 @@ openbackend (Elf *elf, const char *emulation, GElf_Half machine)
        /* Well, we know the emulation name now.  */
        result->emulation = machines[cnt].emulation;
 
-       /* We access some data structures directly.  Make sure the 32 and
-          64 bit variants are laid out the same.  */
-       eu_static_assert (offsetof (Elf32_Ehdr, e_machine)
-                         == offsetof (Elf64_Ehdr, e_machine));
-       eu_static_assert (sizeof (((Elf32_Ehdr *) 0)->e_machine)
-                         == sizeof (((Elf64_Ehdr *) 0)->e_machine));
-       eu_static_assert (offsetof (Elf, state.elf32.ehdr)
-                         == offsetof (Elf, state.elf64.ehdr));
-
        /* Prefer taking the information from the ELF file.  */
-       if (elf == NULL)
+       GElf_Ehdr ehdr;
+       if (elf == NULL || gelf_getehdr (elf, &ehdr) == NULL)
          {
            result->machine = machines[cnt].em;
            result->class = machines[cnt].class;
@@ -327,9 +319,9 @@ openbackend (Elf *elf, const char *emulation, GElf_Half machine)
          }
        else
          {
-           result->machine = elf->state.elf32.ehdr->e_machine;
-           result->class = elf->state.elf32.ehdr->e_ident[EI_CLASS];
-           result->data = elf->state.elf32.ehdr->e_ident[EI_DATA];
+           result->machine = ehdr.e_machine;
+           result->class = ehdr.e_ident[EI_CLASS];
+           result->data = ehdr.e_ident[EI_DATA];
          }
 
         if (machines[cnt].init &&