a5e32e
From 8cd4d5046d7cb1bc16f01e77a5ff50eca8d9da3d Mon Sep 17 00:00:00 2001
a5e32e
From: Jan Kratochvil <jan.kratochvil@redhat.com>
a5e32e
Date: Sat, 1 Aug 2020 10:45:47 +0200
a5e32e
Subject: [PATCH 3/6] debugedit: Fix missing relocation of .debug_types
a5e32e
 section.
a5e32e
a5e32e
---
a5e32e
 tools/debugedit.c | 123 ++++++++++++++++++++++++++++++----------------
a5e32e
 1 file changed, 80 insertions(+), 43 deletions(-)
a5e32e
a5e32e
diff --git a/tools/debugedit.c b/tools/debugedit.c
a5e32e
index cad0cc349..87c1cd622 100644
a5e32e
--- a/tools/debugedit.c
a5e32e
+++ b/tools/debugedit.c
a5e32e
@@ -433,7 +433,8 @@ typedef struct debug_section
a5e32e
     int sec, relsec;
a5e32e
     REL *relbuf;
a5e32e
     REL *relend;
a5e32e
-    struct debug_section *next; /* Only happens for COMDAT .debug_macro.  */
a5e32e
+    /* Only happens for COMDAT .debug_macro and .debug_types.  */
a5e32e
+    struct debug_section *next;
a5e32e
   } debug_section;
a5e32e
 
a5e32e
 static debug_section debug_sections[] =
a5e32e
@@ -1269,7 +1270,9 @@ static int dirty_elf;
a5e32e
 static void
a5e32e
 dirty_section (unsigned int sec)
a5e32e
 {
a5e32e
-  elf_flagdata (debug_sections[sec].elf_data, ELF_C_SET, ELF_F_DIRTY);
a5e32e
+  for (struct debug_section *secp = &debug_sections[sec]; secp != NULL;
a5e32e
+       secp = secp->next)
a5e32e
+    elf_flagdata (secp->elf_data, ELF_C_SET, ELF_F_DIRTY);
a5e32e
   dirty_elf = 1;
a5e32e
 }
a5e32e
 
a5e32e
@@ -1469,12 +1472,7 @@ read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
a5e32e
 
a5e32e
   if (get_line_table (dso, off, &table) == false
a5e32e
       || table == NULL)
a5e32e
-    {
a5e32e
-      if (table != NULL)
a5e32e
-	error (0, 0, ".debug_line offset 0x%x referenced multiple times",
a5e32e
-	       off);
a5e32e
-      return false;
a5e32e
-    }
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
@@ -1965,22 +1963,25 @@ line_rel_cmp (const void *a, const void *b)
a5e32e
 }
a5e32e
 
a5e32e
 static int
a5e32e
-edit_info (DSO *dso, int phase)
a5e32e
+edit_info (DSO *dso, int phase, struct debug_section *sec)
a5e32e
 {
a5e32e
   unsigned char *ptr, *endcu, *endsec;
a5e32e
   uint32_t value;
a5e32e
   htab_t abbrev;
a5e32e
   struct abbrev_tag tag, *t;
a5e32e
 
a5e32e
-  ptr = debug_sections[DEBUG_INFO].data;
a5e32e
-  setup_relbuf(dso, &debug_sections[DEBUG_INFO], &reltype);
a5e32e
-  endsec = ptr + debug_sections[DEBUG_INFO].size;
a5e32e
+  ptr = sec->data;
a5e32e
+  if (ptr == NULL)
a5e32e
+    return 0;
a5e32e
+
a5e32e
+  setup_relbuf(dso, sec, &reltype);
a5e32e
+  endsec = ptr + sec->size;
a5e32e
   while (ptr < endsec)
a5e32e
     {
a5e32e
-      if (ptr + 11 > endsec)
a5e32e
+      if (ptr + (sec == &debug_sections[DEBUG_INFO] ? 11 : 23) > endsec)
a5e32e
 	{
a5e32e
-	  error (0, 0, "%s: .debug_info CU header too small",
a5e32e
-		 dso->filename);
a5e32e
+	  error (0, 0, "%s: %s CU header too small",
a5e32e
+		 dso->filename, sec->name);
a5e32e
 	  return 1;
a5e32e
 	}
a5e32e
 
a5e32e
@@ -1994,7 +1995,7 @@ edit_info (DSO *dso, int phase)
a5e32e
 
a5e32e
       if (endcu > endsec)
a5e32e
 	{
a5e32e
-	  error (0, 0, "%s: .debug_info too small", dso->filename);
a5e32e
+	  error (0, 0, "%s: %s too small", dso->filename, sec->name);
a5e32e
 	  return 1;
a5e32e
 	}
a5e32e
 
a5e32e
@@ -2034,6 +2035,9 @@ edit_info (DSO *dso, int phase)
a5e32e
 	  return 1;
a5e32e
 	}
a5e32e
 
a5e32e
+      if (sec != &debug_sections[DEBUG_INFO])
a5e32e
+	ptr += 12; /* Skip type_signature and type_offset.  */
a5e32e
+
a5e32e
       abbrev = read_abbrev (dso,
a5e32e
 			    debug_sections[DEBUG_ABBREV].data + value);
a5e32e
       if (abbrev == NULL)
a5e32e
@@ -2095,7 +2099,7 @@ edit_dwarf2 (DSO *dso)
a5e32e
 		  struct debug_section *debug_sec = &debug_sections[j];
a5e32e
 		  if (debug_sections[j].data)
a5e32e
 		    {
a5e32e
-		      if (j != DEBUG_MACRO)
a5e32e
+		      if (j != DEBUG_MACRO && j != DEBUG_TYPES)
a5e32e
 			{
a5e32e
 			  error (0, 0, "%s: Found two copies of %s section",
a5e32e
 				 dso->filename, name);
a5e32e
@@ -2103,22 +2107,21 @@ edit_dwarf2 (DSO *dso)
a5e32e
 			}
a5e32e
 		      else
a5e32e
 			{
a5e32e
-			  /* In relocatable files .debug_macro might
a5e32e
-			     appear multiple times as COMDAT
a5e32e
-			     section.  */
a5e32e
+			  /* In relocatable files .debug_macro and .debug_types
a5e32e
+			     might appear multiple times as COMDAT section.  */
a5e32e
 			  struct debug_section *sec;
a5e32e
 			  sec = calloc (sizeof (struct debug_section), 1);
a5e32e
 			  if (sec == NULL)
a5e32e
 			    error (1, errno,
a5e32e
-				   "%s: Could not allocate more macro sections",
a5e32e
-				   dso->filename);
a5e32e
-			  sec->name = ".debug_macro";
a5e32e
+				   "%s: Could not allocate more %s sections",
a5e32e
+				   dso->filename, name);
a5e32e
+			  sec->name = name;
a5e32e
 
a5e32e
-			  struct debug_section *macro_sec = debug_sec;
a5e32e
-			  while (macro_sec->next != NULL)
a5e32e
-			    macro_sec = macro_sec->next;
a5e32e
+			  struct debug_section *multi_sec = debug_sec;
a5e32e
+			  while (multi_sec->next != NULL)
a5e32e
+			    multi_sec = multi_sec->next;
a5e32e
 
a5e32e
-			  macro_sec->next = sec;
a5e32e
+			  multi_sec->next = sec;
a5e32e
 			  debug_sec = sec;
a5e32e
 			}
a5e32e
 		    }
a5e32e
@@ -2155,23 +2158,23 @@ edit_dwarf2 (DSO *dso)
a5e32e
 			  + (dso->shdr[i].sh_type == SHT_RELA),
a5e32e
 			  debug_sections[j].name) == 0)
a5e32e
 	 	{
a5e32e
-		  if (j == DEBUG_MACRO)
a5e32e
+		  if (j == DEBUG_MACRO || j == DEBUG_TYPES)
a5e32e
 		    {
a5e32e
 		      /* Pick the correct one.  */
a5e32e
 		      int rel_target = dso->shdr[i].sh_info;
a5e32e
-		      struct debug_section *macro_sec = &debug_sections[j];
a5e32e
-		      while (macro_sec != NULL)
a5e32e
+		      struct debug_section *multi_sec = &debug_sections[j];
a5e32e
+		      while (multi_sec != NULL)
a5e32e
 			{
a5e32e
-			  if (macro_sec->sec == rel_target)
a5e32e
+			  if (multi_sec->sec == rel_target)
a5e32e
 			    {
a5e32e
-			      macro_sec->relsec = i;
a5e32e
+			      multi_sec->relsec = i;
a5e32e
 			      break;
a5e32e
 			    }
a5e32e
-			  macro_sec = macro_sec->next;
a5e32e
+			  multi_sec = multi_sec->next;
a5e32e
 			}
a5e32e
-		      if (macro_sec == NULL)
a5e32e
-			error (0, 1, "No .debug_macro reloc section: %s",
a5e32e
-			       dso->filename);
a5e32e
+		      if (multi_sec == NULL)
a5e32e
+			error (0, 1, "No %s reloc section: %s",
a5e32e
+			       debug_sections[j].name, dso->filename);
a5e32e
 		    }
a5e32e
 		  else
a5e32e
 		    debug_sections[j].relsec = i;
a5e32e
@@ -2203,12 +2206,10 @@ edit_dwarf2 (DSO *dso)
a5e32e
   if (debug_sections[DEBUG_INFO].data == NULL)
a5e32e
     return 0;
a5e32e
 
a5e32e
-  unsigned char *ptr, *endcu, *endsec;
a5e32e
-  uint32_t value;
a5e32e
-  htab_t abbrev;
a5e32e
-  struct abbrev_tag tag, *t;
a5e32e
+  unsigned char *ptr, *endsec;
a5e32e
   int phase;
a5e32e
   bool info_rel_updated = false;
a5e32e
+  bool types_rel_updated = false;
a5e32e
   bool macro_rel_updated = false;
a5e32e
 
a5e32e
   for (phase = 0; phase < 2; phase++)
a5e32e
@@ -2221,13 +2222,26 @@ edit_dwarf2 (DSO *dso)
a5e32e
 	break;
a5e32e
 
a5e32e
       rel_updated = false;
a5e32e
-      if (edit_info (dso, phase))
a5e32e
-       return 1;
a5e32e
+      if (edit_info (dso, phase, &debug_sections[DEBUG_INFO]))
a5e32e
+	return 1;
a5e32e
 
a5e32e
       /* Remember whether any .debug_info relocations might need
a5e32e
 	 to be updated. */
a5e32e
       info_rel_updated = rel_updated;
a5e32e
 
a5e32e
+      rel_updated = false;
a5e32e
+      struct debug_section *types_sec = &debug_sections[DEBUG_TYPES];
a5e32e
+      while (types_sec != NULL)
a5e32e
+	{
a5e32e
+	  if (edit_info (dso, phase, types_sec))
a5e32e
+	    return 1;
a5e32e
+	  types_sec = types_sec->next;
a5e32e
+	}
a5e32e
+
a5e32e
+      /* Remember whether any .debug_types relocations might need
a5e32e
+	 to be updated. */
a5e32e
+      types_rel_updated = rel_updated;
a5e32e
+
a5e32e
       /* We might have to recalculate/rewrite the debug_line
a5e32e
 	 section.  We need to do that before going into phase one
a5e32e
 	 so we have all new offsets.  We do this separately from
a5e32e
@@ -2475,8 +2489,11 @@ edit_dwarf2 (DSO *dso)
a5e32e
 
a5e32e
   /* After phase 1 we might have rewritten the debug_info with
a5e32e
      new strp, strings and/or linep offsets.  */
a5e32e
-  if (need_strp_update || need_string_replacement || need_stmt_update)
a5e32e
+  if (need_strp_update || need_string_replacement || need_stmt_update) {
a5e32e
     dirty_section (DEBUG_INFO);
a5e32e
+    if (debug_sections[DEBUG_TYPES].data != NULL)
a5e32e
+      dirty_section (DEBUG_TYPES);
a5e32e
+  }
a5e32e
   if (need_strp_update || need_stmt_update)
a5e32e
     dirty_section (DEBUG_MACRO);
a5e32e
   if (need_stmt_update)
a5e32e
@@ -2485,6 +2502,15 @@ edit_dwarf2 (DSO *dso)
a5e32e
   /* Update any relocations addends we might have touched. */
a5e32e
   if (info_rel_updated)
a5e32e
     update_rela_data (dso, &debug_sections[DEBUG_INFO]);
a5e32e
+  if (types_rel_updated)
a5e32e
+    {
a5e32e
+      struct debug_section *types_sec = &debug_sections[DEBUG_TYPES];
a5e32e
+      while (types_sec != NULL)
a5e32e
+	{
a5e32e
+	  update_rela_data (dso, types_sec);
a5e32e
+	  types_sec = types_sec->next;
a5e32e
+	}
a5e32e
+    }
a5e32e
 
a5e32e
   if (macro_rel_updated)
a5e32e
     {
a5e32e
@@ -3037,6 +3063,17 @@ main (int argc, char *argv[])
a5e32e
       macro_sec = next;
a5e32e
     }
a5e32e
 
a5e32e
+  /* In case there were multiple (COMDAT) .debug_types sections,
a5e32e
+     free them.  */
a5e32e
+  struct debug_section *types_sec = &debug_sections[DEBUG_TYPES];
a5e32e
+  types_sec = types_sec->next;
a5e32e
+  while (types_sec != NULL)
a5e32e
+    {
a5e32e
+      struct debug_section *next = types_sec->next;
a5e32e
+      free (types_sec);
a5e32e
+      types_sec = next;
a5e32e
+    }
a5e32e
+
a5e32e
   poptFreeContext (optCon);
a5e32e
 
a5e32e
   return 0;
a5e32e
-- 
a5e32e
2.18.4
a5e32e