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