Blame SOURCES/0004-NFC-debugedit-Move-code-to-separate-functions.patch

a5e32e
From bab443ab4f756ef80f814af0353143f41e90e6a6 Mon Sep 17 00:00:00 2001
a5e32e
From: Jan Kratochvil <jan.kratochvil@redhat.com>
a5e32e
Date: Mon, 17 Aug 2020 21:58:19 +0200
a5e32e
Subject: [PATCH 4/6] [NFC] debugedit: Move code to separate functions.
a5e32e
a5e32e
New functions edit_strp, skip_form and edit_attributes_str_comp_dir
a5e32e
called by edit_attributes.
a5e32e
Split part of read_dwarf2_line into a read_dwarf4_line function.
a5e32e
New function edit_dwarf2_any_str called by edit_dwarf2 at the end of
a5e32e
phase 0 to rebuild .debug_str.
a5e32e
---
a5e32e
 tools/debugedit.c | 367 ++++++++++++++++++++++++++--------------------
a5e32e
 1 file changed, 212 insertions(+), 155 deletions(-)
a5e32e
a5e32e
diff --git a/tools/debugedit.c b/tools/debugedit.c
a5e32e
index 87c1cd622..7464883c5 100644
a5e32e
--- a/tools/debugedit.c
a5e32e
+++ b/tools/debugedit.c
a5e32e
@@ -1457,37 +1457,128 @@ edit_dwarf2_line (DSO *dso)
a5e32e
     }
a5e32e
 }
a5e32e
 
a5e32e
-/* Called during phase zero for each debug_line table referenced from
a5e32e
-   .debug_info.  Outputs all source files seen and records any
a5e32e
-   adjustments needed in the debug_list data structures. Returns true
a5e32e
-   if line_table needs to be rewrite either the dir or file paths. */
a5e32e
+/* Record or adjust (according to phase) DW_FORM_strp.  */
a5e32e
+static void
a5e32e
+edit_strp (DSO *dso, unsigned char *ptr, int phase, bool handled_strp)
a5e32e
+{
a5e32e
+  unsigned char *ptr_orig = ptr;
a5e32e
+
a5e32e
+  /* In the first pass we collect all strings, in the
a5e32e
+     second we put the new references back (if there are
a5e32e
+     any changes).  */
a5e32e
+  if (phase == 0)
a5e32e
+    {
a5e32e
+      /* handled_strp is set for attributes referring to
a5e32e
+	 files. If it is set the string is already
a5e32e
+	 recorded. */
a5e32e
+      if (! handled_strp)
a5e32e
+	{
a5e32e
+	  size_t idx = do_read_32_relocated (ptr);
a5e32e
+	  record_existing_string_entry_idx (&dso->strings, idx);
a5e32e
+	}
a5e32e
+    }
a5e32e
+  else if (need_strp_update) /* && phase == 1 */
a5e32e
+    {
a5e32e
+      struct stridxentry *entry;
a5e32e
+      size_t idx, new_idx;
a5e32e
+      idx = do_read_32_relocated (ptr);
a5e32e
+      entry = string_find_entry (&dso->strings, idx);
a5e32e
+      new_idx = strent_offset (entry->entry);
a5e32e
+      do_write_32_relocated (ptr, new_idx);
a5e32e
+    }
a5e32e
+
a5e32e
+  assert (ptr == ptr_orig);
a5e32e
+}
a5e32e
+
a5e32e
+/* Adjust *PTRP after the current *FORMP, update *FORMP for FORM_INDIRECT.  */
a5e32e
+static enum { FORM_OK, FORM_ERROR, FORM_INDIRECT }
a5e32e
+skip_form (DSO *dso, uint32_t *formp, unsigned char **ptrp)
a5e32e
+{
a5e32e
+  size_t len = 0;
a5e32e
+
a5e32e
+  switch (*formp)
a5e32e
+    {
a5e32e
+    case DW_FORM_ref_addr:
a5e32e
+      if (cu_version == 2)
a5e32e
+	*ptrp += ptr_size;
a5e32e
+      else
a5e32e
+	*ptrp += 4;
a5e32e
+      break;
a5e32e
+    case DW_FORM_flag_present:
a5e32e
+      break;
a5e32e
+    case DW_FORM_addr:
a5e32e
+      *ptrp += ptr_size;
a5e32e
+      break;
a5e32e
+    case DW_FORM_ref1:
a5e32e
+    case DW_FORM_flag:
a5e32e
+    case DW_FORM_data1:
a5e32e
+      ++*ptrp;
a5e32e
+      break;
a5e32e
+    case DW_FORM_ref2:
a5e32e
+    case DW_FORM_data2:
a5e32e
+      *ptrp += 2;
a5e32e
+      break;
a5e32e
+    case DW_FORM_ref4:
a5e32e
+    case DW_FORM_data4:
a5e32e
+    case DW_FORM_sec_offset:
a5e32e
+      *ptrp += 4;
a5e32e
+      break;
a5e32e
+    case DW_FORM_ref8:
a5e32e
+    case DW_FORM_data8:
a5e32e
+    case DW_FORM_ref_sig8:
a5e32e
+      *ptrp += 8;
a5e32e
+      break;
a5e32e
+    case DW_FORM_sdata:
a5e32e
+    case DW_FORM_ref_udata:
a5e32e
+    case DW_FORM_udata:
a5e32e
+      read_uleb128 (*ptrp);
a5e32e
+      break;
a5e32e
+    case DW_FORM_strp:
a5e32e
+      *ptrp += 4;
a5e32e
+      break;
a5e32e
+    case DW_FORM_string:
a5e32e
+      *ptrp = (unsigned char *) strchr ((char *)*ptrp, '\0') + 1;
a5e32e
+      break;
a5e32e
+    case DW_FORM_indirect:
a5e32e
+      *formp = read_uleb128 (*ptrp);
a5e32e
+      return FORM_INDIRECT;
a5e32e
+    case DW_FORM_block1:
a5e32e
+      len = *(*ptrp)++;
a5e32e
+      break;
a5e32e
+    case DW_FORM_block2:
a5e32e
+      len = read_16 (*ptrp);
a5e32e
+      *formp = DW_FORM_block1;
a5e32e
+      break;
a5e32e
+    case DW_FORM_block4:
a5e32e
+      len = read_32 (*ptrp);
a5e32e
+      *formp = DW_FORM_block1;
a5e32e
+      break;
a5e32e
+    case DW_FORM_block:
a5e32e
+    case DW_FORM_exprloc:
a5e32e
+      len = read_uleb128 (*ptrp);
a5e32e
+      *formp = DW_FORM_block1;
a5e32e
+      assert (len < UINT_MAX);
a5e32e
+      break;
a5e32e
+    default:
a5e32e
+      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename, *formp);
a5e32e
+      return FORM_ERROR;
a5e32e
+    }
a5e32e
+
a5e32e
+  if (*formp == DW_FORM_block1)
a5e32e
+    *ptrp += len;
a5e32e
+
a5e32e
+  return FORM_OK;
a5e32e
+}
a5e32e
+
a5e32e
+/* Part of read_dwarf2_line processing DWARF-4.  */
a5e32e
 static bool
a5e32e
-read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
a5e32e
+read_dwarf4_line (DSO *dso, unsigned char *ptr, char *comp_dir,
a5e32e
+		  struct line_table *table)
a5e32e
 {
a5e32e
-  unsigned char *ptr, *dir;
a5e32e
   unsigned char **dirt;
a5e32e
   uint32_t value, dirt_cnt;
a5e32e
   size_t comp_dir_len = !comp_dir ? 0 : strlen (comp_dir);
a5e32e
-  struct line_table *table;
a5e32e
-
a5e32e
-  if (get_line_table (dso, off, &table) == false
a5e32e
-      || table == NULL)
a5e32e
-    return false;
a5e32e
-
a5e32e
-  /* Skip to the directory table. The rest of the header has already
a5e32e
-     been read and checked by get_line_table. */
a5e32e
-  ptr = debug_sections[DEBUG_LINE].data + off;
a5e32e
-  ptr += (4 /* unit len */
a5e32e
-	  + 2 /* version */
a5e32e
-	  + 4 /* header len */
a5e32e
-	  + 1 /* min instr len */
a5e32e
-	  + (table->version >= 4) /* max op per instr, if version >= 4 */
a5e32e
-	  + 1 /* default is stmt */
a5e32e
-	  + 1 /* line base */
a5e32e
-	  + 1 /* line range */
a5e32e
-	  + 1 /* opcode base */
a5e32e
-	  + table->opcode_base - 1); /* opcode len table */
a5e32e
-  dir = ptr;
a5e32e
+  unsigned char *dir = ptr;
a5e32e
 
a5e32e
   /* dir table: */
a5e32e
   value = 1;
a5e32e
@@ -1620,6 +1711,40 @@ read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
a5e32e
       read_uleb128 (ptr);
a5e32e
     }
a5e32e
 
a5e32e
+  return true;
a5e32e
+}
a5e32e
+
a5e32e
+/* Called during phase zero for each debug_line table referenced from
a5e32e
+   .debug_info.  Outputs all source files seen and records any
a5e32e
+   adjustments needed in the debug_list data structures. Returns true
a5e32e
+   if line_table needs to be rewrite either the dir or file paths. */
a5e32e
+static bool
a5e32e
+read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
a5e32e
+{
a5e32e
+  unsigned char *ptr;
a5e32e
+  struct line_table *table;
a5e32e
+
a5e32e
+  if (get_line_table (dso, off, &table) == false
a5e32e
+      || table == NULL)
a5e32e
+    return false;
a5e32e
+
a5e32e
+  /* Skip to the directory table. The rest of the header has already
a5e32e
+     been read and checked by get_line_table. */
a5e32e
+  ptr = debug_sections[DEBUG_LINE].data + off;
a5e32e
+  ptr += (4 /* unit len */
a5e32e
+	  + 2 /* version */
a5e32e
+	  + 4 /* header len */
a5e32e
+	  + 1 /* min instr len */
a5e32e
+	  + (table->version >= 4) /* max op per instr, if version >= 4 */
a5e32e
+	  + 1 /* default is stmt */
a5e32e
+	  + 1 /* line base */
a5e32e
+	  + 1 /* line range */
a5e32e
+	  + 1 /* opcode base */
a5e32e
+	  + table->opcode_base - 1); /* opcode len table */
a5e32e
+
a5e32e
+  if (! read_dwarf4_line (dso, ptr, comp_dir, table))
a5e32e
+   return false;
a5e32e
+
a5e32e
   dso->lines.debug_lines_len += 4 + table->unit_length + table->size_diff;
a5e32e
   return table->replace_dirs || table->replace_files;
a5e32e
 }
a5e32e
@@ -1637,6 +1762,33 @@ find_new_list_offs (struct debug_lines *lines, size_t idx)
a5e32e
   return table->new_idx;
a5e32e
 }
a5e32e
 
a5e32e
+/* Read DW_FORM_strp collecting compilation directory.  */
a5e32e
+static void
a5e32e
+edit_attributes_str_comp_dir (DSO *dso, unsigned char **ptrp, int phase,
a5e32e
+			      char **comp_dirp, bool *handled_strpp)
a5e32e
+{
a5e32e
+  const char *dir;
a5e32e
+  size_t idx = do_read_32_relocated (*ptrp);
a5e32e
+  /* In phase zero we collect the comp_dir.  */
a5e32e
+  if (phase == 0)
a5e32e
+    {
a5e32e
+      if (idx >= debug_sections[DEBUG_STR].size)
a5e32e
+	error (1, 0, "%s: Bad string pointer index %zd for comp_dir",
a5e32e
+	       dso->filename, idx);
a5e32e
+      dir = (char *) debug_sections[DEBUG_STR].data + idx;
a5e32e
+
a5e32e
+      free (*comp_dirp);
a5e32e
+      *comp_dirp = strdup (dir);
a5e32e
+    }
a5e32e
+
a5e32e
+  if (dest_dir != NULL && phase == 0)
a5e32e
+    {
a5e32e
+      if (record_file_string_entry_idx (&dso->strings, idx))
a5e32e
+	need_strp_update = true;
a5e32e
+      *handled_strpp = true;
a5e32e
+    }
a5e32e
+}
a5e32e
+
a5e32e
 /* This scans the attributes of one DIE described by the given abbrev_tag.
a5e32e
    PTR points to the data in the debug_info. It will be advanced till all
a5e32e
    abbrev data is consumed. In phase zero data is collected, in phase one
a5e32e
@@ -1655,7 +1807,6 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
a5e32e
   for (i = 0; i < t->nattr; ++i)
a5e32e
     {
a5e32e
       uint32_t form = t->attr[i].form;
a5e32e
-      size_t len = 0;
a5e32e
       while (1)
a5e32e
 	{
a5e32e
 	  /* Whether we already handled a string as file for this
a5e32e
@@ -1743,29 +1894,8 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
a5e32e
 		}
a5e32e
 	      else if (form == DW_FORM_strp &&
a5e32e
 		       debug_sections[DEBUG_STR].data)
a5e32e
-		{
a5e32e
-		  const char *dir;
a5e32e
-		  size_t idx = do_read_32_relocated (ptr);
a5e32e
-		  /* In phase zero we collect the comp_dir.  */
a5e32e
-		  if (phase == 0)
a5e32e
-		    {
a5e32e
-		      if (idx >= debug_sections[DEBUG_STR].size)
a5e32e
-			error (1, 0,
a5e32e
-			       "%s: Bad string pointer index %zd for comp_dir",
a5e32e
-			       dso->filename, idx);
a5e32e
-		      dir = (char *) debug_sections[DEBUG_STR].data + idx;
a5e32e
-
a5e32e
-		      free (comp_dir);
a5e32e
-		      comp_dir = strdup (dir);
a5e32e
-		    }
a5e32e
-
a5e32e
-		  if (dest_dir != NULL && phase == 0)
a5e32e
-		    {
a5e32e
-		      if (record_file_string_entry_idx (&dso->strings, idx))
a5e32e
-			need_strp_update = true;
a5e32e
-		      handled_strp = true;
a5e32e
-		    }
a5e32e
-		}
a5e32e
+		edit_attributes_str_comp_dir (dso, &ptr, phase, &comp_dir,
a5e32e
+					      &handled_strp);
a5e32e
 	    }
a5e32e
 	  else if ((t->tag == DW_TAG_compile_unit
a5e32e
 		    || t->tag == DW_TAG_partial_unit)
a5e32e
@@ -1815,99 +1945,21 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
a5e32e
 
a5e32e
 	  switch (form)
a5e32e
 	    {
a5e32e
-	    case DW_FORM_ref_addr:
a5e32e
-	      if (cu_version == 2)
a5e32e
-		ptr += ptr_size;
a5e32e
-	      else
a5e32e
-		ptr += 4;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_flag_present:
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_addr:
a5e32e
-	      ptr += ptr_size;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_ref1:
a5e32e
-	    case DW_FORM_flag:
a5e32e
-	    case DW_FORM_data1:
a5e32e
-	      ++ptr;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_ref2:
a5e32e
-	    case DW_FORM_data2:
a5e32e
-	      ptr += 2;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_ref4:
a5e32e
-	    case DW_FORM_data4:
a5e32e
-	    case DW_FORM_sec_offset:
a5e32e
-	      ptr += 4;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_ref8:
a5e32e
-	    case DW_FORM_data8:
a5e32e
-	    case DW_FORM_ref_sig8:
a5e32e
-	      ptr += 8;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_sdata:
a5e32e
-	    case DW_FORM_ref_udata:
a5e32e
-	    case DW_FORM_udata:
a5e32e
-	      read_uleb128 (ptr);
a5e32e
-	      break;
a5e32e
 	    case DW_FORM_strp:
a5e32e
-	      /* In the first pass we collect all strings, in the
a5e32e
-		 second we put the new references back (if there are
a5e32e
-		 any changes).  */
a5e32e
-	      if (phase == 0)
a5e32e
-		{
a5e32e
-		  /* handled_strp is set for attributes referring to
a5e32e
-		     files. If it is set the string is already
a5e32e
-		     recorded. */
a5e32e
-		  if (! handled_strp)
a5e32e
-		    {
a5e32e
-		      size_t idx = do_read_32_relocated (ptr);
a5e32e
-		      record_existing_string_entry_idx (&dso->strings, idx);
a5e32e
-		    }
a5e32e
-		}
a5e32e
-	      else if (need_strp_update) /* && phase == 1 */
a5e32e
-		{
a5e32e
-		  struct stridxentry *entry;
a5e32e
-		  size_t idx, new_idx;
a5e32e
-		  idx = do_read_32_relocated (ptr);
a5e32e
-		  entry = string_find_entry (&dso->strings, idx);
a5e32e
-		  new_idx = strent_offset (entry->entry);
a5e32e
-		  do_write_32_relocated (ptr, new_idx);
a5e32e
-		}
a5e32e
-	      ptr += 4;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_string:
a5e32e
-	      ptr = (unsigned char *) strchr ((char *)ptr, '\0') + 1;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_indirect:
a5e32e
-	      form = read_uleb128 (ptr);
a5e32e
-	      continue;
a5e32e
-	    case DW_FORM_block1:
a5e32e
-	      len = *ptr++;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_block2:
a5e32e
-	      len = read_16 (ptr);
a5e32e
-	      form = DW_FORM_block1;
a5e32e
+	      edit_strp (dso, ptr, phase, handled_strp);
a5e32e
 	      break;
a5e32e
-	    case DW_FORM_block4:
a5e32e
-	      len = read_32 (ptr);
a5e32e
-	      form = DW_FORM_block1;
a5e32e
-	      break;
a5e32e
-	    case DW_FORM_block:
a5e32e
-	    case DW_FORM_exprloc:
a5e32e
-	      len = read_uleb128 (ptr);
a5e32e
-	      form = DW_FORM_block1;
a5e32e
-	      assert (len < UINT_MAX);
a5e32e
+	    }
a5e32e
+
a5e32e
+	  switch (skip_form (dso, &form, &ptr))
a5e32e
+	    {
a5e32e
+	    case FORM_OK:
a5e32e
 	      break;
a5e32e
-	    default:
a5e32e
-	      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename,
a5e32e
-		     form);
a5e32e
+	    case FORM_ERROR:
a5e32e
 	      return NULL;
a5e32e
+	    case FORM_INDIRECT:
a5e32e
+	      continue;
a5e32e
 	    }
a5e32e
 
a5e32e
-	  if (form == DW_FORM_block1)
a5e32e
-	    ptr += len;
a5e32e
-
a5e32e
 	  break;
a5e32e
 	}
a5e32e
     }
a5e32e
@@ -2068,6 +2120,28 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
a5e32e
   return 0;
a5e32e
 }
a5e32e
 
a5e32e
+/* Rebuild .debug_str.  */
a5e32e
+static void
a5e32e
+edit_dwarf2_any_str (DSO *dso)
a5e32e
+{
a5e32e
+  Strtab *strtab = dso->strings.str_tab;
a5e32e
+  Elf_Data *strdata = debug_sections[DEBUG_STR].elf_data;
a5e32e
+  int strndx = debug_sections[DEBUG_STR].sec;
a5e32e
+  Elf_Scn *strscn = dso->scn[strndx];
a5e32e
+
a5e32e
+  /* Out with the old. */
a5e32e
+  strdata->d_size = 0;
a5e32e
+  /* In with the new. */
a5e32e
+  strdata = elf_newdata (strscn);
a5e32e
+
a5e32e
+  /* We really should check whether we had enough memory,
a5e32e
+     but the old ebl version will just abort on out of
a5e32e
+     memory... */
a5e32e
+  strtab_finalize (strtab, strdata);
a5e32e
+  debug_sections[DEBUG_STR].size = strdata->d_size;
a5e32e
+  dso->strings.str_buf = strdata->d_buf;
a5e32e
+}
a5e32e
+
a5e32e
 static int
a5e32e
 edit_dwarf2 (DSO *dso)
a5e32e
 {
a5e32e
@@ -2466,24 +2540,7 @@ edit_dwarf2 (DSO *dso)
a5e32e
 	 in place for phase 1 updating of debug_info
a5e32e
 	 references. */
a5e32e
       if (phase == 0 && need_strp_update)
a5e32e
-	{
a5e32e
-	  Strtab *strtab = dso->strings.str_tab;
a5e32e
-	  Elf_Data *strdata = debug_sections[DEBUG_STR].elf_data;
a5e32e
-	  int strndx = debug_sections[DEBUG_STR].sec;
a5e32e
-	  Elf_Scn *strscn = dso->scn[strndx];
a5e32e
-
a5e32e
-	  /* Out with the old. */
a5e32e
-	  strdata->d_size = 0;
a5e32e
-	  /* In with the new. */
a5e32e
-	  strdata = elf_newdata (strscn);
a5e32e
-
a5e32e
-	  /* We really should check whether we had enough memory,
a5e32e
-	     but the old ebl version will just abort on out of
a5e32e
-	     memory... */
a5e32e
-	  strtab_finalize (strtab, strdata);
a5e32e
-	  debug_sections[DEBUG_STR].size = strdata->d_size;
a5e32e
-	  dso->strings.str_buf = strdata->d_buf;
a5e32e
-	}
a5e32e
+	edit_dwarf2_any_str (dso);
a5e32e
 
a5e32e
     }
a5e32e
 
a5e32e
-- 
a5e32e
2.18.4
a5e32e