[PATCH 1/4] dwarf_begin_elf: decouple section searching from reading

Jonathan Lebon jlebon at redhat.com
Mon May 11 19:38:13 UTC 2015


To help legibility, we separate the section name matching from the
actual section reading. This also allows us to remove duplicate code in
cases of sections appearing twice or empty section data. There are no
changes in functionality. The indentation will be fixed in the next
commit, in the interest of keeping this commit easier to read.

Signed-off-by: Jonathan Lebon <jlebon at redhat.com>
---
 libdw/dwarf_begin_elf.c | 60 ++++++++++++++++++++++++-------------------------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c
index 4c49ce2..3950cf9 100644
--- a/libdw/dwarf_begin_elf.c
+++ b/libdw/dwarf_begin_elf.c
@@ -118,42 +118,45 @@ check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
 
   /* Recognize the various sections.  Most names start with .debug_.  */
   size_t cnt;
+  bool compressed = false;
   for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
     if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
-      {
-	/* Found it.  Remember where the data is.  */
-	if (unlikely (result->sectiondata[cnt] != NULL))
-	  /* A section appears twice.  That's bad.  We ignore the section.  */
-	  break;
-
-	/* Get the section data.  */
-	Elf_Data *data = elf_getdata (scn, NULL);
-	if (data != NULL && data->d_size != 0)
-	  /* Yep, there is actually data available.  */
-	  result->sectiondata[cnt] = data;
-
-	break;
-      }
-#if USE_ZLIB
+      break;
     else if (scnname[0] == '.' && scnname[1] == 'z'
 	     && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
       {
-	/* A compressed section.  */
+        compressed = true;
+        break;
+      }
 
-	if (unlikely (result->sectiondata[cnt] != NULL))
-	  /* A section appears twice.  That's bad.  We ignore the section.  */
-	  break;
+  if (cnt >= ndwarf_scnnames)
+    /* Not a debug section; ignore it. */
+    return result;
+
+  if (unlikely (result->sectiondata[cnt] != NULL))
+    /* A section appears twice.  That's bad.  We ignore the section.  */
+    return result;
+
+  /* Get the section data.  */
+  Elf_Data *data = elf_getdata (scn, NULL);
+  if (data == NULL || data->d_size == 0)
+    /* No data actually available, ignore it. */
+    return result;
+
+  /* We can now read the section data into results. */
+  if (!compressed)
+    result->sectiondata[cnt] = data;
+#if USE_ZLIB
+  else
+    {
+        /* A compressed section. */
 
-	/* Get the section data.  */
-	Elf_Data *data = elf_getdata (scn, NULL);
-	if (data != NULL && data->d_size != 0)
-	  {
 	    /* There is a 12-byte header of "ZLIB" followed by
 	       an 8-byte big-endian size.  */
 
 	    if (unlikely (data->d_size < 4 + 8)
 		|| unlikely (memcmp (data->d_buf, "ZLIB", 4) != 0))
-	      break;
+	      return result;
 
 	    uint64_t size;
 	    memcpy (&size, data->d_buf + 4, sizeof size);
@@ -163,11 +166,11 @@ check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
 	       enough memory for both the Elf_Data header and the
 	       uncompressed section data.  */
 	    if (unlikely (sizeof (Elf_Data) + size < size))
-	      break;
+	      return result;
 
 	    Elf_Data *zdata = malloc (sizeof (Elf_Data) + size);
 	    if (unlikely (zdata == NULL))
-	      break;
+	      return result;
 
 	    zdata->d_buf = &zdata[1];
 	    zdata->d_type = ELF_T_BYTE;
@@ -205,10 +208,7 @@ check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
 		result->sectiondata[cnt] = zdata;
 		result->sectiondata_gzip_mask |= 1U << cnt;
 	      }
-	  }
-
-	break;
-      }
+    }
 #endif
 
   return result;
-- 
2.1.0



More information about the elfutils-devel mailing list