teknoraver / rpms / rpm

Forked from rpms/rpm 3 months ago
Clone

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

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