Blob Blame History Raw
commit 75f2de448f311807e2493f2a37a980e2d872b229
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu Nov 3 13:38:45 2022 +0100

    readelf: Check phdr != NULL or shdr != NULL in handle_dynamic.
    
    The compiler doesn't know that when use_dynamic_segment is true,
    then phdr should/will be non-NULL and otherwise shdr is non-NULL.
    Add explicit checks to help the compiler out and in case an error
    is made calling the handle_dynamic function.
    
    Signed-off-by: Mark Wielaard <mark@klomp.org>

diff --git a/src/readelf.c b/src/readelf.c
index 0e0b05c4..e721a209 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1828,7 +1828,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   size_t dyn_ents;
 
   /* Get the data of the section.  */
-  if (use_dynamic_segment)
+  if (use_dynamic_segment && phdr != NULL)
     data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
 				phdr->p_filesz, ELF_T_DYN);
   else
@@ -1840,7 +1840,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   /* Get the dynamic section entry number */
   dyn_ents = get_dyn_ents (data);
 
-  if (!use_dynamic_segment)
+  if (!use_dynamic_segment && shdr != NULL)
     {
       /* Get the section header string table index.  */
       if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
@@ -1862,7 +1862,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
 	      (int) shdr->sh_link,
 	      elf_strptr (ebl->elf, shstrndx, glink->sh_name));
     }
-  else
+  else if (phdr != NULL)
     {
       printf (ngettext ("\
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
@@ -1879,7 +1879,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   /* if --use-dynamic option is enabled,
      use the string table to get the related library info.  */
   Elf_Data *strtab_data = NULL;
-  if (use_dynamic_segment)
+  if (use_dynamic_segment && phdr != NULL)
     {
       strtab_data = get_dynscn_strtab(ebl->elf, phdr);
       if (strtab_data == NULL)
@@ -1903,7 +1903,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
 	  || dyn->d_tag == DT_RPATH
 	  || dyn->d_tag == DT_RUNPATH)
 	{
-	  if (! use_dynamic_segment)
+	  if (! use_dynamic_segment && shdr != NULL)
 	    name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
 	  else if (dyn->d_un.d_val < strtab_data->d_size
 		   && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',

commit b0a0235771906e3bcd6174c4e3c020b5522b0be5
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu Nov 3 13:44:35 2022 +0100

    libdw: Don't dereference and assign values we are skipping
    
    We don't use the FDE address encoding byte, so no reason
    to read and store it. Just skip past it.
    
    Signed-off-by: Mark Wielaard <mark@klomp.org>

diff --git a/libdw/dwarf_next_cfi.c b/libdw/dwarf_next_cfi.c
index 23b16885..be08984f 100644
--- a/libdw/dwarf_next_cfi.c
+++ b/libdw/dwarf_next_cfi.c
@@ -226,7 +226,7 @@ dwarf_next_cfi (const unsigned char e_ident[],
 	      if (sized_augmentation)
 		{
 		  /* Skip FDE address encoding byte.  */
-		  encoding = *bytes++;
+		  bytes++;
 		  continue;
 		}
 	      break;

commit 52a6a3110e019d696284fdd822c2a2f0987dded2
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu Nov 3 13:52:32 2022 +0100

    readelf: Check gelf_getdyn doesn't return NULL
    
    Signed-off-by: Mark Wielaard <mark@klomp.org>

diff --git a/src/readelf.c b/src/readelf.c
index e721a209..3dafb041 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -4910,7 +4910,7 @@ get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
     GElf_Dyn dyn_mem;
     GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
     /* DT_NULL Marks end of dynamic section.  */
-    if (dyn->d_tag == DT_NULL)
+    if (dyn == NULL || dyn->d_tag == DT_NULL)
       break;
 
     switch (dyn->d_tag) {