richardphibel / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
DistroBaker 6ff1af
From 8b5dcb4c2175ac706a4e1c34ce83301213800689 Mon Sep 17 00:00:00 2001
DistroBaker 6ff1af
From: Jan Kratochvil <jan.kratochvil@redhat.com>
DistroBaker 6ff1af
Date: Mon, 18 Jan 2021 22:56:53 +0100
DistroBaker 6ff1af
Subject: [PATCH 5/6] debugedit: Implement DWARF-5 unit header and new forms
DistroBaker 6ff1af
 parsing.
DistroBaker 6ff1af
DistroBaker 6ff1af
Recognize the various new DWARF5 .debug sections.
DistroBaker 6ff1af
Parse and skip new DWARF5 forms in read_abbrev and skip_form.
DistroBaker 6ff1af
Read DWARF5 unit headers for compile and partial units in edit_info.
DistroBaker 6ff1af
DistroBaker 6ff1af
This is enough to be able to process gcc -gdwarf-5 produced binaries
DistroBaker 6ff1af
without the new DWARF5 .debug_line format (which isn't produced with
DistroBaker 6ff1af
binutils < 2.36).
DistroBaker 6ff1af
DistroBaker 6ff1af
Patches slightly edited/merged by Mark Wielaard <mark@klomp.org>
DistroBaker 6ff1af
---
DistroBaker 6ff1af
 tools/debugedit.c | 88 +++++++++++++++++++++++++++++++++++++++++++----
DistroBaker 6ff1af
 1 file changed, 81 insertions(+), 7 deletions(-)
DistroBaker 6ff1af
DistroBaker 6ff1af
diff --git a/tools/debugedit.c b/tools/debugedit.c
DistroBaker 6ff1af
index 7464883c5..be5fee85b 100644
DistroBaker 6ff1af
--- a/tools/debugedit.c
DistroBaker 6ff1af
+++ b/tools/debugedit.c
DistroBaker 6ff1af
@@ -453,6 +453,11 @@ static debug_section debug_sections[] =
DistroBaker 6ff1af
 #define DEBUG_TYPES	11
DistroBaker 6ff1af
 #define DEBUG_MACRO	12
DistroBaker 6ff1af
 #define DEBUG_GDB_SCRIPT	13
DistroBaker 6ff1af
+#define DEBUG_RNGLISTS	14
DistroBaker 6ff1af
+#define DEBUG_LINE_STR	15
DistroBaker 6ff1af
+#define DEBUG_ADDR	16
DistroBaker 6ff1af
+#define DEBUG_STR_OFFSETS	17
DistroBaker 6ff1af
+#define DEBUG_LOCLISTS	18
DistroBaker 6ff1af
     { ".debug_info", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
     { ".debug_abbrev", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
     { ".debug_line", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
@@ -467,6 +472,11 @@ static debug_section debug_sections[] =
DistroBaker 6ff1af
     { ".debug_types", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
     { ".debug_macro", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
     { ".debug_gdb_scripts", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
+    { ".debug_rnglists", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
+    { ".debug_line_str", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
+    { ".debug_addr", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
+    { ".debug_str_offsets", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
+    { ".debug_loclists", NULL, NULL, 0, 0, 0 },
DistroBaker 6ff1af
     { NULL, NULL, NULL, 0, 0, 0 }
DistroBaker 6ff1af
   };
DistroBaker 6ff1af
 
DistroBaker 6ff1af
@@ -755,12 +765,28 @@ no_memory:
DistroBaker 6ff1af
 	    }
DistroBaker 6ff1af
 	  form = read_uleb128 (ptr);
DistroBaker 6ff1af
 	  if (form == 2
DistroBaker 6ff1af
-	      || (form > DW_FORM_flag_present && form != DW_FORM_ref_sig8))
DistroBaker 6ff1af
+	      || (form > DW_FORM_flag_present
DistroBaker 6ff1af
+		  && !(form == DW_FORM_ref_sig8
DistroBaker 6ff1af
+		       || form == DW_FORM_data16
DistroBaker 6ff1af
+		       || form == DW_FORM_implicit_const
DistroBaker 6ff1af
+		       || form == DW_FORM_addrx
DistroBaker 6ff1af
+		       || form == DW_FORM_loclistx
DistroBaker 6ff1af
+		       || form == DW_FORM_rnglistx
DistroBaker 6ff1af
+		       || form == DW_FORM_addrx1
DistroBaker 6ff1af
+		       || form == DW_FORM_addrx2
DistroBaker 6ff1af
+		       || form == DW_FORM_addrx3
DistroBaker 6ff1af
+		       || form == DW_FORM_addrx4)))
DistroBaker 6ff1af
 	    {
DistroBaker 6ff1af
-	      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename, form);
DistroBaker 6ff1af
+	      error (0, 0, "%s: Unknown DWARF DW_FORM_0x%x", dso->filename,
DistroBaker 6ff1af
+		     form);
DistroBaker 6ff1af
 	      htab_delete (h);
DistroBaker 6ff1af
 	      return NULL;
DistroBaker 6ff1af
 	    }
DistroBaker 6ff1af
+	  if (form == DW_FORM_implicit_const)
DistroBaker 6ff1af
+	    {
DistroBaker 6ff1af
+	      /* It is SLEB128 but the value is dropped anyway.  */
DistroBaker 6ff1af
+	      read_uleb128 (ptr);
DistroBaker 6ff1af
+	    }
DistroBaker 6ff1af
 
DistroBaker 6ff1af
 	  t->attr[t->nattr].attr = attr;
DistroBaker 6ff1af
 	  t->attr[t->nattr++].form = form;
DistroBaker 6ff1af
@@ -1505,6 +1531,7 @@ skip_form (DSO *dso, uint32_t *formp, unsigned char **ptrp)
DistroBaker 6ff1af
 	*ptrp += 4;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     case DW_FORM_flag_present:
DistroBaker 6ff1af
+    case DW_FORM_implicit_const:
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     case DW_FORM_addr:
DistroBaker 6ff1af
       *ptrp += ptr_size;
DistroBaker 6ff1af
@@ -1512,14 +1539,24 @@ skip_form (DSO *dso, uint32_t *formp, unsigned char **ptrp)
DistroBaker 6ff1af
     case DW_FORM_ref1:
DistroBaker 6ff1af
     case DW_FORM_flag:
DistroBaker 6ff1af
     case DW_FORM_data1:
DistroBaker 6ff1af
+    case DW_FORM_strx1:
DistroBaker 6ff1af
+    case DW_FORM_addrx1:
DistroBaker 6ff1af
       ++*ptrp;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     case DW_FORM_ref2:
DistroBaker 6ff1af
     case DW_FORM_data2:
DistroBaker 6ff1af
+    case DW_FORM_strx2:
DistroBaker 6ff1af
+    case DW_FORM_addrx2:
DistroBaker 6ff1af
       *ptrp += 2;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
+    case DW_FORM_strx3:
DistroBaker 6ff1af
+    case DW_FORM_addrx3:
DistroBaker 6ff1af
+      *ptrp += 3;
DistroBaker 6ff1af
+      break;
DistroBaker 6ff1af
     case DW_FORM_ref4:
DistroBaker 6ff1af
     case DW_FORM_data4:
DistroBaker 6ff1af
+    case DW_FORM_strx4:
DistroBaker 6ff1af
+    case DW_FORM_addrx4:
DistroBaker 6ff1af
     case DW_FORM_sec_offset:
DistroBaker 6ff1af
       *ptrp += 4;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
@@ -1528,12 +1565,20 @@ skip_form (DSO *dso, uint32_t *formp, unsigned char **ptrp)
DistroBaker 6ff1af
     case DW_FORM_ref_sig8:
DistroBaker 6ff1af
       *ptrp += 8;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
+    case DW_FORM_data16:
DistroBaker 6ff1af
+      *ptrp += 16;
DistroBaker 6ff1af
+      break;
DistroBaker 6ff1af
     case DW_FORM_sdata:
DistroBaker 6ff1af
     case DW_FORM_ref_udata:
DistroBaker 6ff1af
     case DW_FORM_udata:
DistroBaker 6ff1af
+    case DW_FORM_strx:
DistroBaker 6ff1af
+    case DW_FORM_loclistx:
DistroBaker 6ff1af
+    case DW_FORM_rnglistx:
DistroBaker 6ff1af
+    case DW_FORM_addrx:
DistroBaker 6ff1af
       read_uleb128 (*ptrp);
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     case DW_FORM_strp:
DistroBaker 6ff1af
+    case DW_FORM_line_strp:
DistroBaker 6ff1af
       *ptrp += 4;
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     case DW_FORM_string:
DistroBaker 6ff1af
@@ -1560,7 +1605,7 @@ skip_form (DSO *dso, uint32_t *formp, unsigned char **ptrp)
DistroBaker 6ff1af
       assert (len < UINT_MAX);
DistroBaker 6ff1af
       break;
DistroBaker 6ff1af
     default:
DistroBaker 6ff1af
-      error (0, 0, "%s: Unknown DWARF DW_FORM_%d", dso->filename, *formp);
DistroBaker 6ff1af
+      error (0, 0, "%s: Unknown DWARF DW_FORM_0x%x", dso->filename, *formp);
DistroBaker 6ff1af
       return FORM_ERROR;
DistroBaker 6ff1af
     }
DistroBaker 6ff1af
 
DistroBaker 6ff1af
@@ -2030,7 +2075,10 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
DistroBaker 6ff1af
   endsec = ptr + sec->size;
DistroBaker 6ff1af
   while (ptr < endsec)
DistroBaker 6ff1af
     {
DistroBaker 6ff1af
-      if (ptr + (sec == &debug_sections[DEBUG_INFO] ? 11 : 23) > endsec)
DistroBaker 6ff1af
+      unsigned char *cu_start = ptr;
DistroBaker 6ff1af
+
DistroBaker 6ff1af
+      /* header size, version, unit_type, ptr_size.  */
DistroBaker 6ff1af
+      if (ptr + 4 + 2 + 1 + 1 > endsec)
DistroBaker 6ff1af
 	{
DistroBaker 6ff1af
 	  error (0, 0, "%s: %s CU header too small",
DistroBaker 6ff1af
 		 dso->filename, sec->name);
DistroBaker 6ff1af
@@ -2052,13 +2100,36 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
DistroBaker 6ff1af
 	}
DistroBaker 6ff1af
 
DistroBaker 6ff1af
       cu_version = read_16 (ptr);
DistroBaker 6ff1af
-      if (cu_version != 2 && cu_version != 3 && cu_version != 4)
DistroBaker 6ff1af
+      if (cu_version != 2 && cu_version != 3 && cu_version != 4
DistroBaker 6ff1af
+	  && cu_version != 5)
DistroBaker 6ff1af
 	{
DistroBaker 6ff1af
 	  error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
DistroBaker 6ff1af
 		 cu_version);
DistroBaker 6ff1af
 	  return 1;
DistroBaker 6ff1af
 	}
DistroBaker 6ff1af
 
DistroBaker 6ff1af
+      int cu_ptr_size = 0;
DistroBaker 6ff1af
+
DistroBaker 6ff1af
+      if (cu_version >= 5)
DistroBaker 6ff1af
+	{
DistroBaker 6ff1af
+	  uint8_t unit_type = read_8 (ptr);
DistroBaker 6ff1af
+	  if (unit_type != DW_UT_compile && unit_type != DW_UT_partial)
DistroBaker 6ff1af
+	    {
DistroBaker 6ff1af
+	      error (0, 0, "%s: Unit type %u unhandled", dso->filename,
DistroBaker 6ff1af
+		     unit_type);
DistroBaker 6ff1af
+	      return 1;
DistroBaker 6ff1af
+	    }
DistroBaker 6ff1af
+
DistroBaker 6ff1af
+	  cu_ptr_size = read_8 (ptr);
DistroBaker 6ff1af
+	}
DistroBaker 6ff1af
+
DistroBaker 6ff1af
+      unsigned char *header_end = (cu_start + 23 + (cu_version < 5 ? 0 : 1));
DistroBaker 6ff1af
+      if (header_end > endsec)
DistroBaker 6ff1af
+	{
DistroBaker 6ff1af
+	  error (0, 0, "%s: %s CU header too small", dso->filename, sec->name);
DistroBaker 6ff1af
+	  return 1;
DistroBaker 6ff1af
+	}
DistroBaker 6ff1af
+
DistroBaker 6ff1af
       value = read_32_relocated (ptr);
DistroBaker 6ff1af
       if (value >= debug_sections[DEBUG_ABBREV].size)
DistroBaker 6ff1af
 	{
DistroBaker 6ff1af
@@ -2070,9 +2141,12 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
DistroBaker 6ff1af
 	  return 1;
DistroBaker 6ff1af
 	}
DistroBaker 6ff1af
 
DistroBaker 6ff1af
+      if (cu_version < 5)
DistroBaker 6ff1af
+	cu_ptr_size = read_8 (ptr);
DistroBaker 6ff1af
+
DistroBaker 6ff1af
       if (ptr_size == 0)
DistroBaker 6ff1af
 	{
DistroBaker 6ff1af
-	  ptr_size = read_8 (ptr);
DistroBaker 6ff1af
+	  ptr_size = cu_ptr_size;
DistroBaker 6ff1af
 	  if (ptr_size != 4 && ptr_size != 8)
DistroBaker 6ff1af
 	    {
DistroBaker 6ff1af
 	      error (0, 0, "%s: Invalid DWARF pointer size %d",
DistroBaker 6ff1af
@@ -2080,7 +2154,7 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
DistroBaker 6ff1af
 	      return 1;
DistroBaker 6ff1af
 	    }
DistroBaker 6ff1af
 	}
DistroBaker 6ff1af
-      else if (read_8 (ptr) != ptr_size)
DistroBaker 6ff1af
+      else if (cu_ptr_size != ptr_size)
DistroBaker 6ff1af
 	{
DistroBaker 6ff1af
 	  error (0, 0, "%s: DWARF pointer size differs between CUs",
DistroBaker 6ff1af
 		 dso->filename);
DistroBaker 6ff1af
-- 
DistroBaker 6ff1af
2.18.4
DistroBaker 6ff1af