b9080c
commit 75f2de448f311807e2493f2a37a980e2d872b229
b9080c
Author: Mark Wielaard <mark@klomp.org>
b9080c
Date:   Thu Nov 3 13:38:45 2022 +0100
b9080c
b9080c
    readelf: Check phdr != NULL or shdr != NULL in handle_dynamic.
b9080c
    
b9080c
    The compiler doesn't know that when use_dynamic_segment is true,
b9080c
    then phdr should/will be non-NULL and otherwise shdr is non-NULL.
b9080c
    Add explicit checks to help the compiler out and in case an error
b9080c
    is made calling the handle_dynamic function.
b9080c
    
b9080c
    Signed-off-by: Mark Wielaard <mark@klomp.org>
b9080c
b9080c
diff --git a/src/readelf.c b/src/readelf.c
b9080c
index 0e0b05c4..e721a209 100644
b9080c
--- a/src/readelf.c
b9080c
+++ b/src/readelf.c
b9080c
@@ -1828,7 +1828,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
b9080c
   size_t dyn_ents;
b9080c
 
b9080c
   /* Get the data of the section.  */
b9080c
-  if (use_dynamic_segment)
b9080c
+  if (use_dynamic_segment && phdr != NULL)
b9080c
     data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
b9080c
 				phdr->p_filesz, ELF_T_DYN);
b9080c
   else
b9080c
@@ -1840,7 +1840,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
b9080c
   /* Get the dynamic section entry number */
b9080c
   dyn_ents = get_dyn_ents (data);
b9080c
 
b9080c
-  if (!use_dynamic_segment)
b9080c
+  if (!use_dynamic_segment && shdr != NULL)
b9080c
     {
b9080c
       /* Get the section header string table index.  */
b9080c
       if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
b9080c
@@ -1862,7 +1862,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
b9080c
 	      (int) shdr->sh_link,
b9080c
 	      elf_strptr (ebl->elf, shstrndx, glink->sh_name));
b9080c
     }
b9080c
-  else
b9080c
+  else if (phdr != NULL)
b9080c
     {
b9080c
       printf (ngettext ("\
b9080c
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
b9080c
@@ -1879,7 +1879,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
b9080c
   /* if --use-dynamic option is enabled,
b9080c
      use the string table to get the related library info.  */
b9080c
   Elf_Data *strtab_data = NULL;
b9080c
-  if (use_dynamic_segment)
b9080c
+  if (use_dynamic_segment && phdr != NULL)
b9080c
     {
b9080c
       strtab_data = get_dynscn_strtab(ebl->elf, phdr);
b9080c
       if (strtab_data == NULL)
b9080c
@@ -1903,7 +1903,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
b9080c
 	  || dyn->d_tag == DT_RPATH
b9080c
 	  || dyn->d_tag == DT_RUNPATH)
b9080c
 	{
b9080c
-	  if (! use_dynamic_segment)
b9080c
+	  if (! use_dynamic_segment && shdr != NULL)
b9080c
 	    name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
b9080c
 	  else if (dyn->d_un.d_val < strtab_data->d_size
b9080c
 		   && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
b9080c
b9080c
commit b0a0235771906e3bcd6174c4e3c020b5522b0be5
b9080c
Author: Mark Wielaard <mark@klomp.org>
b9080c
Date:   Thu Nov 3 13:44:35 2022 +0100
b9080c
b9080c
    libdw: Don't dereference and assign values we are skipping
b9080c
    
b9080c
    We don't use the FDE address encoding byte, so no reason
b9080c
    to read and store it. Just skip past it.
b9080c
    
b9080c
    Signed-off-by: Mark Wielaard <mark@klomp.org>
b9080c
b9080c
diff --git a/libdw/dwarf_next_cfi.c b/libdw/dwarf_next_cfi.c
b9080c
index 23b16885..be08984f 100644
b9080c
--- a/libdw/dwarf_next_cfi.c
b9080c
+++ b/libdw/dwarf_next_cfi.c
b9080c
@@ -226,7 +226,7 @@ dwarf_next_cfi (const unsigned char e_ident[],
b9080c
 	      if (sized_augmentation)
b9080c
 		{
b9080c
 		  /* Skip FDE address encoding byte.  */
b9080c
-		  encoding = *bytes++;
b9080c
+		  bytes++;
b9080c
 		  continue;
b9080c
 		}
b9080c
 	      break;
b9080c
b9080c
commit 52a6a3110e019d696284fdd822c2a2f0987dded2
b9080c
Author: Mark Wielaard <mark@klomp.org>
b9080c
Date:   Thu Nov 3 13:52:32 2022 +0100
b9080c
b9080c
    readelf: Check gelf_getdyn doesn't return NULL
b9080c
    
b9080c
    Signed-off-by: Mark Wielaard <mark@klomp.org>
b9080c
b9080c
diff --git a/src/readelf.c b/src/readelf.c
b9080c
index e721a209..3dafb041 100644
b9080c
--- a/src/readelf.c
b9080c
+++ b/src/readelf.c
b9080c
@@ -4910,7 +4910,7 @@ get_dynscn_addrs(Elf *elf, GElf_Phdr *phdr, GElf_Addr addrs[i_max])
b9080c
     GElf_Dyn dyn_mem;
b9080c
     GElf_Dyn *dyn = gelf_getdyn(data, dyn_idx, &dyn_mem);
b9080c
     /* DT_NULL Marks end of dynamic section.  */
b9080c
-    if (dyn->d_tag == DT_NULL)
b9080c
+    if (dyn == NULL || dyn->d_tag == DT_NULL)
b9080c
       break;
b9080c
 
b9080c
     switch (dyn->d_tag) {