Handle zero sh_entsize in get_soname_of_elf_file
authorMark Wielaard <mark@klomp.org>
Tue, 19 Jul 2022 23:01:14 +0000 (01:01 +0200)
committerDodji Seketeli <dodji@redhat.com>
Tue, 20 Sep 2022 08:33:24 +0000 (10:33 +0200)
Apparently guile produced ELF files don't set sh_entsize for the
dynamic section. Which would cause a divide by zero. Luckily we do
know how big an dynamic entry should be. So use gelf_fsize for
ELF_T_DYN if sh_entsize is zero.

  * src/abg-dwarf-reader.cc (get_soname_of_elf_file):
  Make sure entsize is non-zero before use.

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

Signed-off-by: Mark Wielaard <mark@klomp.org>
src/abg-dwarf-reader.cc

index 5690954017d9c0f5cee76a5fa8cb65ca2b53412c..695683ed39e9198db7f9aa49eef43864e98770fb 100644 (file)
@@ -16425,8 +16425,11 @@ get_soname_of_elf_file(const string& path, string &soname)
           Elf_Scn* scn = gelf_offscn (elf, phdr->p_offset);
           GElf_Shdr shdr_mem;
           GElf_Shdr* shdr = gelf_getshdr (scn, &shdr_mem);
+          size_t entsize = (shdr != NULL && shdr->sh_entsize != 0
+                            ? shdr->sh_entsize
+                            : gelf_fsize (elf, ELF_T_DYN, 1, EV_CURRENT));
           int maxcnt = (shdr != NULL
-                        ? shdr->sh_size / shdr->sh_entsize : INT_MAX);
+                        ? shdr->sh_size / entsize : INT_MAX);
           ABG_ASSERT (shdr == NULL || shdr->sh_type == SHT_DYNAMIC);
           Elf_Data* data = elf_getdata (scn, NULL);
           if (data == NULL)