richardphibel / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
813822
From d9947f2dc0c2cd812f8e64380d5f6f53705a5280 Mon Sep 17 00:00:00 2001
813822
From: Mark Wielaard <mark@klomp.org>
813822
Date: Tue, 19 Jan 2021 04:12:33 +0100
813822
Subject: [PATCH 6/6] debugedit: Handle DWARF-5 debug_line and debug_line_str.
813822
813822
Handle the new DWARF5 .debug_line tables and the new DW_FORM_line_strp.
813822
DWARF5 tables are handled separately from the earlier tables. They
813822
will never change size, but they do need updates to the .debug_str
813822
or .debug_line_str references.
813822
813822
Based on a patch from Jan Kratochvil <jan.kratochvil@redhat.com>
813822
---
813822
 tools/debugedit.c | 471 ++++++++++++++++++++++++++++++++++++++++------
813822
 1 file changed, 410 insertions(+), 61 deletions(-)
813822
813822
diff --git a/tools/debugedit.c b/tools/debugedit.c
813822
index be5fee85b..d6a0058e9 100644
813822
--- a/tools/debugedit.c
813822
+++ b/tools/debugedit.c
813822
@@ -103,6 +103,8 @@ static bool need_string_replacement = false;
813822
 /* Whether we need to do any updates of the string indexes (DW_FORM_strp)
813822
    in debug_info for string indexes. */
813822
 static bool need_strp_update = false;
813822
+/* Likewise for DW_FORM_line_strp. */
813822
+static bool need_line_strp_update = false;
813822
 /* If the debug_line changes size we will need to update the
813822
    DW_AT_stmt_list attributes indexes in the debug_info. */
813822
 static bool need_stmt_update = false;
813822
@@ -192,7 +194,7 @@ typedef struct
813822
   const char *filename;
813822
   int lastscn;
813822
   size_t phnum;
813822
-  struct strings strings;
813822
+  struct strings debug_str, debug_line_str;
813822
   struct debug_lines lines;
813822
   GElf_Shdr shdr[0];
813822
 } DSO;
813822
@@ -553,10 +555,11 @@ setup_relbuf (DSO *dso, debug_section *sec, int *reltype)
813822
       /* Relocations against section symbols are uninteresting in REL.  */
813822
       if (dso->shdr[i].sh_type == SHT_REL && sym.st_value == 0)
813822
 	continue;
813822
-      /* Only consider relocations against .debug_str, .debug_line
813822
-	 and .debug_abbrev.  */
813822
+      /* Only consider relocations against .debug_str, .debug_line,
813822
+	 .debug_line_str, and .debug_abbrev.  */
813822
       if (sym.st_shndx != debug_sections[DEBUG_STR].sec
813822
 	  && sym.st_shndx != debug_sections[DEBUG_LINE].sec
813822
+	  && sym.st_shndx != debug_sections[DEBUG_LINE_STR].sec
813822
 	  && sym.st_shndx != debug_sections[DEBUG_ABBREV].sec)
813822
 	continue;
813822
       rela.r_addend += sym.st_value;
813822
@@ -768,6 +771,7 @@ no_memory:
813822
 	      || (form > DW_FORM_flag_present
813822
 		  && !(form == DW_FORM_ref_sig8
813822
 		       || form == DW_FORM_data16
813822
+		       || form == DW_FORM_line_strp
813822
 		       || form == DW_FORM_implicit_const
813822
 		       || form == DW_FORM_addrx
813822
 		       || form == DW_FORM_loclistx
813822
@@ -1049,17 +1053,20 @@ string_find_entry (struct strings *strings, size_t old_idx)
813822
    a replacement file string has been recorded for it, otherwise
813822
    returns false.  */
813822
 static bool
813822
-record_file_string_entry_idx (struct strings *strings, size_t old_idx)
813822
+record_file_string_entry_idx (bool line_strp, DSO *dso, size_t old_idx)
813822
 {
813822
+  struct strings *strings = line_strp ? &dso->debug_line_str : &dso->debug_str;
813822
   bool ret = false;
813822
   struct stridxentry *entry = string_find_new_entry (strings, old_idx);
813822
   if (entry != NULL)
813822
     {
813822
-      if (old_idx >= debug_sections[DEBUG_STR].size)
813822
-	error (1, 0, "Bad string pointer index %zd", old_idx);
813822
+      debug_section *sec = &debug_sections[line_strp
813822
+					   ? DEBUG_LINE_STR : DEBUG_STR];
813822
+      if (old_idx >= sec->size)
813822
+	error (1, 0, "Bad string pointer index %zd (%s)", old_idx, sec->name);
813822
 
813822
       Strent *strent;
813822
-      const char *old_str = (char *)debug_sections[DEBUG_STR].data + old_idx;
813822
+      const char *old_str = (char *)sec->data + old_idx;
813822
       const char *file = skip_dir_prefix (old_str, base_dir);
813822
       if (file == NULL)
813822
 	{
813822
@@ -1103,15 +1110,18 @@ record_file_string_entry_idx (struct strings *strings, size_t old_idx)
813822
    base_dir with dest_dir, just records the existing string associated
813822
    with the index. */
813822
 static void
813822
-record_existing_string_entry_idx (struct strings *strings, size_t old_idx)
813822
+record_existing_string_entry_idx (bool line_strp, DSO *dso, size_t old_idx)
813822
 {
813822
+  struct strings *strings = line_strp ? &dso->debug_line_str : &dso->debug_str;
813822
   struct stridxentry *entry = string_find_new_entry (strings, old_idx);
813822
   if (entry != NULL)
813822
     {
813822
-      if (old_idx >= debug_sections[DEBUG_STR].size)
813822
-	error (1, 0, "Bad string pointer index %zd", old_idx);
813822
+      debug_section *sec = &debug_sections[line_strp
813822
+					   ? DEBUG_LINE_STR : DEBUG_STR];
813822
+      if (old_idx >= sec->size)
813822
+	error (1, 0, "Bad string pointer index %zd (%s)", old_idx, sec->name);
813822
 
813822
-      const char *str = (char *)debug_sections[DEBUG_STR].data + old_idx;
813822
+      const char *str = (char *)sec->data + old_idx;
813822
       Strent *strent = strtab_add_len (strings->str_tab,
813822
 				       str, strlen (str) + 1);
813822
       if (strent == NULL)
813822
@@ -1244,13 +1254,28 @@ get_line_table (DSO *dso, size_t off, struct line_table **table)
813822
 
813822
   /* version */
813822
   t->version = read_16 (ptr);
813822
-  if (t->version != 2 && t->version != 3 && t->version != 4)
813822
+  if (t->version != 2 && t->version != 3 && t->version != 4 && t->version != 5)
813822
     {
813822
       error (0, 0, "%s: DWARF version %d unhandled", dso->filename,
813822
 	     t->version);
813822
       return false;
813822
     }
813822
 
813822
+  if (t->version >= 5)
813822
+    {
813822
+      /* address_size */
813822
+      assert (ptr_size != 0);
813822
+      if (ptr_size != read_8 (ptr))
813822
+	{
813822
+	  error (0, 0, "%s: .debug_line address size differs from .debug_info",
813822
+		 dso->filename);
813822
+	  return false;
813822
+	}
813822
+
813822
+      /* segment_selector_size */
813822
+      (void) read_8 (ptr);
813822
+    }
813822
+
813822
   /* header_length */
813822
   unsigned char *endprol = ptr + 4;
813822
   t->header_length = read_32 (ptr);
813822
@@ -1343,7 +1368,9 @@ edit_dwarf2_line (DSO *dso)
813822
 
813822
   linedata->d_size = dso->lines.debug_lines_len;
813822
   linedata->d_buf = dso->lines.line_buf;
813822
+  debug_sections[DEBUG_LINE].data = linedata->d_buf;
813822
   debug_sections[DEBUG_LINE].size = linedata->d_size;
813822
+  debug_sections[DEBUG_LINE].elf_data = linedata;
813822
 
813822
   /* Make sure the line tables are sorted on the old index. */
813822
   qsort (dso->lines.table, dso->lines.used, sizeof (struct line_table),
813822
@@ -1483,9 +1510,10 @@ edit_dwarf2_line (DSO *dso)
813822
     }
813822
 }
813822
 
813822
-/* Record or adjust (according to phase) DW_FORM_strp.  */
813822
+/* Record or adjust (according to phase) DW_FORM_strp or DW_FORM_line_strp.  */
813822
 static void
813822
-edit_strp (DSO *dso, unsigned char *ptr, int phase, bool handled_strp)
813822
+edit_strp (DSO *dso, bool line_strp, unsigned char *ptr, int phase,
813822
+	   bool handled_strp)
813822
 {
813822
   unsigned char *ptr_orig = ptr;
813822
 
813822
@@ -1500,15 +1528,18 @@ edit_strp (DSO *dso, unsigned char *ptr, int phase, bool handled_strp)
813822
       if (! handled_strp)
813822
 	{
813822
 	  size_t idx = do_read_32_relocated (ptr);
813822
-	  record_existing_string_entry_idx (&dso->strings, idx);
813822
+	  record_existing_string_entry_idx (line_strp, dso, idx);
813822
 	}
813822
     }
813822
-  else if (need_strp_update) /* && phase == 1 */
813822
+  else if (line_strp
813822
+	   ? need_line_strp_update : need_strp_update) /* && phase == 1 */
813822
     {
813822
       struct stridxentry *entry;
813822
       size_t idx, new_idx;
813822
+      struct strings *strings = (line_strp
813822
+				 ? &dso->debug_line_str : &dso->debug_str);
813822
       idx = do_read_32_relocated (ptr);
813822
-      entry = string_find_entry (&dso->strings, idx);
813822
+      entry = string_find_entry (strings, idx);
813822
       new_idx = strent_offset (entry->entry);
813822
       do_write_32_relocated (ptr, new_idx);
813822
     }
813822
@@ -1759,6 +1790,254 @@ read_dwarf4_line (DSO *dso, unsigned char *ptr, char *comp_dir,
813822
   return true;
813822
 }
813822
 
813822
+/* Called by read_dwarf5_line first for directories and then file
813822
+   names as they both have the same format.  */
813822
+static bool
813822
+read_dwarf5_line_entries (DSO *dso, unsigned char **ptrp,
813822
+			  struct line_table *table, int phase,
813822
+			  char ***dirs, int *ndir,
813822
+			  const char *entry_name)
813822
+{
813822
+  /* directory_entry_format_count */
813822
+  /* file_name_entry_format_count */
813822
+  unsigned format_count = read_8 (*ptrp);
813822
+
813822
+  unsigned char *formats = *ptrp;
813822
+
813822
+  /* directory_entry_format */
813822
+  /* file_name_entry_format */
813822
+  for (unsigned formati = 0; formati < format_count; ++formati)
813822
+    {
813822
+      read_uleb128 (*ptrp);
813822
+      read_uleb128 (*ptrp);
813822
+    }
813822
+
813822
+  /* directories_count */
813822
+  /* file_names_count */
813822
+  unsigned entry_count = read_uleb128 (*ptrp);
813822
+
813822
+  bool collecting_dirs = dest_dir && phase == 0 && *dirs == NULL;
813822
+  bool writing_files = dest_dir && phase == 0 && *dirs != NULL;
813822
+  if (collecting_dirs)
813822
+    {
813822
+      *ndir = entry_count;
813822
+      *dirs = malloc (entry_count * sizeof (char *));
813822
+      if (*dirs == NULL)
813822
+	error (1, errno, "%s: Could not allocate debug_line dirs",
813822
+	       dso->filename);
813822
+    }
813822
+
813822
+  /* directories */
813822
+  /* file_names */
813822
+  for (unsigned entryi = 0; entryi < entry_count; ++entryi)
813822
+    {
813822
+      char *dir = NULL;
813822
+      char *file = NULL;;
813822
+      unsigned char *format_ptr = formats;
813822
+      for (unsigned formati = 0; formati < format_count; ++formati)
813822
+	{
813822
+	  unsigned lnct = read_uleb128 (format_ptr);
813822
+	  unsigned form = read_uleb128 (format_ptr);
813822
+	  bool handled_form = false;
813822
+	  bool handled_strp = false;
813822
+	  bool line_strp = form == DW_FORM_line_strp;
813822
+	  if (lnct == DW_LNCT_path)
813822
+	    {
813822
+	      switch (form)
813822
+		{
813822
+		case DW_FORM_strp:
813822
+		case DW_FORM_line_strp:
813822
+		  if (dest_dir && phase == 0)
813822
+		    {
813822
+		      size_t idx = do_read_32_relocated (*ptrp);
813822
+		      if (record_file_string_entry_idx (line_strp, dso, idx))
813822
+			{
813822
+			  if (line_strp)
813822
+			    need_line_strp_update = true;
813822
+			  else
813822
+			    need_strp_update = true;
813822
+			}
813822
+		      handled_strp = true;
813822
+		      if (collecting_dirs || writing_files)
813822
+			{
813822
+			  debug_section *sec = &debug_sections[line_strp
813822
+                                           ? DEBUG_LINE_STR : DEBUG_STR];
813822
+			  if (collecting_dirs)
813822
+			    dir = (char *)sec->data + idx;
813822
+			  if (writing_files)
813822
+			    file = (char *)sec->data + idx;
813822
+			}
813822
+		    }
813822
+		  break;
813822
+		default:
813822
+		  error (0, 0, "%s: Unsupported "
813822
+			 ".debug_line %s %u path DW_FORM_0x%x",
813822
+			 dso->filename, entry_name, entryi, form);
813822
+		  return false;
813822
+		}
813822
+	    }
813822
+	  if (writing_files && lnct == DW_LNCT_directory_index)
813822
+	    {
813822
+	      int dirndx;
813822
+	      switch (form)
813822
+		{
813822
+		case DW_FORM_udata:
813822
+		  handled_form = true;
813822
+		  dirndx = read_uleb128 (*ptrp);
813822
+		  break;
813822
+		case DW_FORM_data1:
813822
+		  dirndx = **ptrp;
813822
+		  break;
813822
+		case DW_FORM_data2:
813822
+		  dirndx = do_read_16 (*ptrp);
813822
+		  break;
813822
+		case DW_FORM_data4:
813822
+		  dirndx = do_read_32 (*ptrp);
813822
+		  break;
813822
+		default:
813822
+		  error (0, 0, "%s: Unsupported "
813822
+			 ".debug_line %s %u dirndx DW_FORM_0x%x",
813822
+			 dso->filename, entry_name, entryi, form);
813822
+		  return false;
813822
+		}
813822
+
813822
+	      if (dirndx > *ndir)
813822
+		{
813822
+		  error (0, 0, "%s: Bad dir number %u in .debug_line %s",
813822
+			 dso->filename, entryi, entry_name);
813822
+		  return false;
813822
+		}
813822
+	      dir = (*dirs)[dirndx];
813822
+	    }
813822
+
813822
+	  switch (form)
813822
+	    {
813822
+	    case DW_FORM_strp:
813822
+	    case DW_FORM_line_strp:
813822
+	      edit_strp (dso, line_strp, *ptrp, phase, handled_strp);
813822
+	      break;
813822
+	    }
813822
+
813822
+	  if (!handled_form)
813822
+	    {
813822
+	      switch (skip_form (dso, &form, ptrp))
813822
+		{
813822
+		case FORM_OK:
813822
+		  break;
813822
+		case FORM_ERROR:
813822
+		  return false;
813822
+		case FORM_INDIRECT:
813822
+		  error (0, 0, "%s: Unsupported "
813822
+			 ".debug_line %s %u DW_FORM_indirect",
813822
+			 dso->filename, entry_name, entryi);
813822
+		  return false;
813822
+		}
813822
+	    }
813822
+	}
813822
+
813822
+      if (collecting_dirs)
813822
+	(*dirs)[entryi] = dir;
813822
+
813822
+      if (writing_files)
813822
+	{
813822
+	  char *comp_dir = (*dirs)[0];
813822
+	  size_t comp_dir_len = strlen(comp_dir);
813822
+	  size_t file_len = strlen (file);
813822
+	  size_t dir_len = strlen (dir);
813822
+
813822
+	  char *s = malloc (comp_dir_len + 1 + file_len + 1 + dir_len + 1);
813822
+	  if (s == NULL)
813822
+	    {
813822
+	      error (0, ENOMEM, "%s: Reading file table", dso->filename);
813822
+	      return false;
813822
+	    }
813822
+	  if (file[0] == '/')
813822
+	    {
813822
+	      memcpy (s, file, file_len + 1);
813822
+	    }
813822
+	  else if (dir[0] == '/')
813822
+	    {
813822
+	      memcpy (s, dir, dir_len);
813822
+	      s[dir_len] = '/';
813822
+	      memcpy (s + dir_len + 1, file, file_len + 1);
813822
+	    }
813822
+	  else
813822
+	    {
813822
+	      char *p = s;
813822
+	      if (comp_dir_len != 0)
813822
+		{
813822
+		  memcpy (s, comp_dir, comp_dir_len);
813822
+		  s[comp_dir_len] = '/';
813822
+		  p += comp_dir_len + 1;
813822
+		}
813822
+	      memcpy (p, dir, dir_len);
813822
+	      p[dir_len] = '/';
813822
+	      memcpy (p + dir_len + 1, file, file_len + 1);
813822
+	    }
813822
+	  canonicalize_path (s, s);
813822
+	  if (list_file_fd != -1)
813822
+	    {
813822
+	      const char *p = NULL;
813822
+	      if (base_dir == NULL)
813822
+		p = s;
813822
+	      else
813822
+		{
813822
+		  p = skip_dir_prefix (s, base_dir);
813822
+		  if (p == NULL && dest_dir != NULL)
813822
+		    p = skip_dir_prefix (s, dest_dir);
813822
+		}
813822
+
813822
+	      if (p)
813822
+		{
813822
+		  size_t size = strlen (p) + 1;
813822
+		  while (size > 0)
813822
+		    {
813822
+		      ssize_t ret = write (list_file_fd, p, size);
813822
+		      if (ret == -1)
813822
+			break;
813822
+		      size -= ret;
813822
+		      p += ret;
813822
+		    }
813822
+		}
813822
+	    }
813822
+
813822
+	  free (s);
813822
+	}
813822
+    }
813822
+
813822
+  return true;
813822
+}
813822
+
813822
+/* Part of read_dwarf2_line processing DWARF-5.  */
813822
+static bool
813822
+read_dwarf5_line (DSO *dso, unsigned char *ptr, struct line_table *table,
813822
+		  int phase)
813822
+{
813822
+  char **dirs = NULL;
813822
+  int ndir;
813822
+  /* Skip header.  */
813822
+  ptr += (4 /* unit len */
813822
+          + 2 /* version */
813822
+          + (table->version < 5 ? 0 : 0
813822
+             + 1 /* address_size */
813822
+             + 1 /* segment_selector*/)
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
+  bool retval = (read_dwarf5_line_entries (dso, &ptr, table, phase,
813822
+					   &dirs, &ndir, "directory")
813822
+		 && read_dwarf5_line_entries (dso, &ptr, table, phase,
813822
+					      &dirs, &ndir, "file name"));
813822
+  free (dirs);
813822
+  return retval;
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
@@ -1778,6 +2057,9 @@ read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
813822
   ptr = debug_sections[DEBUG_LINE].data + off;
813822
   ptr += (4 /* unit len */
813822
 	  + 2 /* version */
813822
+	  + (table->version < 5 ? 0 : 0
813822
+	     + 1 /* address_size */
813822
+	     + 1 /* segment_selector*/)
813822
 	  + 4 /* header len */
813822
 	  + 1 /* min instr len */
813822
 	  + (table->version >= 4) /* max op per instr, if version >= 4 */
813822
@@ -1787,8 +2069,13 @@ read_dwarf2_line (DSO *dso, uint32_t off, char *comp_dir)
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
+  /* DWARF version 5 line tables won't change size. But they might need
813822
+     [line]strp recording/updates. Handle that part later.  */
813822
+  if (table->version < 5)
813822
+    {
813822
+      if (! read_dwarf4_line (dso, ptr, comp_dir, table))
813822
+	return false;
813822
+    }
813822
 
813822
   dso->lines.debug_lines_len += 4 + table->unit_length + table->size_diff;
813822
   return table->replace_dirs || table->replace_files;
813822
@@ -1807,20 +2094,22 @@ 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
+/* Read DW_FORM_strp or DW_FORM_line_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
+edit_attributes_str_comp_dir (bool line_strp, DSO *dso, unsigned char **ptrp,
813822
+			      int phase, 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
+      debug_section *sec = &debug_sections[line_strp
813822
+					   ? DEBUG_LINE_STR : DEBUG_STR];
813822
+      if (sec->data == NULL || idx >= sec->size)
813822
+	error (1, 0, "%s: Bad string pointer index %zd for comp_dir (%s)",
813822
+	       dso->filename, idx, sec->name);
813822
+      dir = (char *) sec->data + idx;
813822
 
813822
       free (*comp_dirp);
813822
       *comp_dirp = strdup (dir);
813822
@@ -1828,8 +2117,13 @@ edit_attributes_str_comp_dir (DSO *dso, unsigned char **ptrp, int phase,
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
+      if (record_file_string_entry_idx (line_strp, dso, idx))
813822
+	{
813822
+	  if (line_strp)
813822
+	    need_line_strp_update = true;
813822
+	  else
813822
+	    need_strp_update = true;
813822
+	}
813822
       *handled_strpp = true;
813822
     }
813822
 }
813822
@@ -1937,17 +2231,24 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
813822
 			}
813822
 		    }
813822
 		}
813822
-	      else if (form == DW_FORM_strp &&
813822
-		       debug_sections[DEBUG_STR].data)
813822
-		edit_attributes_str_comp_dir (dso, &ptr, phase, &comp_dir,
813822
+	      else if (form == DW_FORM_strp)
813822
+		edit_attributes_str_comp_dir (false /* line_strp */, dso,
813822
+					      &ptr, phase, &comp_dir,
813822
 					      &handled_strp);
813822
+	      else if (form == DW_FORM_line_strp)
813822
+		edit_attributes_str_comp_dir (true /* line_strp */, dso, &ptr,
813822
+					      phase, &comp_dir, &handled_strp);
813822
 	    }
813822
 	  else if ((t->tag == DW_TAG_compile_unit
813822
 		    || t->tag == DW_TAG_partial_unit)
813822
-		   && t->attr[i].attr == DW_AT_name
813822
-		   && form == DW_FORM_strp
813822
-		   && debug_sections[DEBUG_STR].data)
813822
+		   && ((form == DW_FORM_strp
813822
+			&& debug_sections[DEBUG_STR].data)
813822
+		       || (form == DW_FORM_line_strp
813822
+			   && debug_sections[DEBUG_LINE_STR].data))
813822
+		   && t->attr[i].attr == DW_AT_name)
813822
 	    {
813822
+	      bool line_strp = form == DW_FORM_line_strp;
813822
+
813822
 	      /* DW_AT_name is the primary file for this compile
813822
 		 unit. If starting with / it is a full path name.
813822
 		 Note that we don't handle DW_FORM_string in this
813822
@@ -1957,11 +2258,14 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
813822
 	      /* In phase zero we will look for a comp_dir to use.  */
813822
 	      if (phase == 0)
813822
 		{
813822
-		  if (idx >= debug_sections[DEBUG_STR].size)
813822
+		  debug_section *sec = &debug_sections[line_strp
813822
+						       ? DEBUG_LINE_STR
813822
+						       : DEBUG_STR];
813822
+		  if (idx >= sec->size)
813822
 		    error (1, 0,
813822
-			   "%s: Bad string pointer index %zd for unit name",
813822
-			   dso->filename, idx);
813822
-		  char *name = (char *) debug_sections[DEBUG_STR].data + idx;
813822
+			   "%s: Bad string pointer index %zd for unit name (%s)",
813822
+			   dso->filename, idx, sec->name);
813822
+		  char *name = (char *) sec->data + idx;
813822
 		  if (*name == '/' && comp_dir == NULL)
813822
 		    {
813822
 		      char *enddir = strrchr (name, '/');
813822
@@ -1982,8 +2286,13 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
813822
 		 pass (1) stores it (the new index). */
813822
 	      if (dest_dir && phase == 0)
813822
 		{
813822
-		  if (record_file_string_entry_idx (&dso->strings, idx))
813822
-		    need_strp_update = true;
813822
+		  if (record_file_string_entry_idx (line_strp, dso, idx))
813822
+		    {
813822
+		      if (line_strp)
813822
+			need_line_strp_update = true;
813822
+		      else
813822
+			need_strp_update = true;
813822
+		    }
813822
 		  handled_strp = true;
813822
 		}
813822
 	    }
813822
@@ -1991,7 +2300,10 @@ edit_attributes (DSO *dso, unsigned char *ptr, struct abbrev_tag *t, int phase)
813822
 	  switch (form)
813822
 	    {
813822
 	    case DW_FORM_strp:
813822
-	      edit_strp (dso, ptr, phase, handled_strp);
813822
+	      edit_strp (dso, false /* line_strp */, ptr, phase, handled_strp);
813822
+	      break;
813822
+	    case DW_FORM_line_strp:
813822
+	      edit_strp (dso, true /* line_strp */, ptr, phase, handled_strp);
813822
 	      break;
813822
 	    }
813822
 
813822
@@ -2196,11 +2508,11 @@ edit_info (DSO *dso, int phase, struct debug_section *sec)
813822
 
813822
 /* Rebuild .debug_str.  */
813822
 static void
813822
-edit_dwarf2_any_str (DSO *dso)
813822
+edit_dwarf2_any_str (DSO *dso, struct strings *strings, debug_section *secp)
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
+  Strtab *strtab = strings->str_tab;
813822
+  Elf_Data *strdata = secp->elf_data;
813822
+  int strndx = secp->sec;
813822
   Elf_Scn *strscn = dso->scn[strndx];
813822
 
813822
   /* Out with the old. */
813822
@@ -2212,8 +2524,8 @@ edit_dwarf2_any_str (DSO *dso)
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
+  secp->size = strdata->d_size;
813822
+  strings->str_buf = strdata->d_buf;
813822
 }
813822
 
813822
 static int
813822
@@ -2359,12 +2671,14 @@ edit_dwarf2 (DSO *dso)
813822
   bool info_rel_updated = false;
813822
   bool types_rel_updated = false;
813822
   bool macro_rel_updated = false;
813822
+  bool line_rel_updated = false;
813822
 
813822
   for (phase = 0; phase < 2; phase++)
813822
     {
813822
       /* If we don't need to update anyhing, skip phase 1. */
813822
       if (phase == 1
813822
 	  && !need_strp_update
813822
+	  && !need_line_strp_update
813822
 	  && !need_string_replacement
813822
 	  && !need_stmt_update)
813822
 	break;
813822
@@ -2582,15 +2896,14 @@ edit_dwarf2 (DSO *dso)
813822
 		      if (phase == 0)
813822
 			{
813822
 			  size_t idx = read_32_relocated (ptr);
813822
-			  record_existing_string_entry_idx (&dso->strings,
813822
-							    idx);
813822
+			  record_existing_string_entry_idx (false, dso, idx);
813822
 			}
813822
 		      else
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
+			  entry = string_find_entry (&dso->debug_str, idx);
813822
 			  new_idx = strent_offset (entry->entry);
813822
 			  write_32_relocated (ptr, new_idx);
813822
 			}
813822
@@ -2610,24 +2923,50 @@ edit_dwarf2 (DSO *dso)
813822
 	    }
813822
 	}
813822
 
813822
-      /* Same for the debug_str section. Make sure everything is
813822
-	 in place for phase 1 updating of debug_info
813822
+
813822
+      /* Now handle all the DWARF5 line tables, they contain strp
813822
+	 and/or line_strp entries that need to be registered/rewritten.  */
813822
+      setup_relbuf(dso, &debug_sections[DEBUG_LINE], &reltype);
813822
+      rel_updated = false;
813822
+
813822
+      /* edit_dwarf2_line will have set this up, unless there are no
813822
+	 moved/resized (DWARF4) lines. In which case we can just use
813822
+	 the original section data. new_idx will have been setup
813822
+	 correctly, even if it is the same as old_idx.  */
813822
+      unsigned char *line_buf = (unsigned char *)dso->lines.line_buf;
813822
+      if (line_buf == NULL)
813822
+	line_buf = debug_sections[DEBUG_LINE].data;
813822
+      for (int ldx = 0; ldx < dso->lines.used; ldx++)
813822
+	{
813822
+	  struct line_table *t = &dso->lines.table[ldx];
813822
+	  if (t->version >= 5)
813822
+	    read_dwarf5_line (dso, line_buf + t->new_idx, t, phase);
813822
+	}
813822
+      if (rel_updated)
813822
+	line_rel_updated = true;
813822
+
813822
+      /* Same for the debug_str and debug_line_str sections.
813822
+	 Make sure everything is in place for phase 1 updating of debug_info
813822
 	 references. */
813822
       if (phase == 0 && need_strp_update)
813822
-	edit_dwarf2_any_str (dso);
813822
-
813822
+	edit_dwarf2_any_str (dso, &dso->debug_str,
813822
+			     &debug_sections[DEBUG_STR]);
813822
+      if (phase == 0 && need_line_strp_update)
813822
+	edit_dwarf2_any_str (dso, &dso->debug_line_str,
813822
+			     &debug_sections[DEBUG_LINE_STR]);
813822
     }
813822
 
813822
   /* After phase 1 we might have rewritten the debug_info with
813822
      new strp, strings and/or linep offsets.  */
813822
-  if (need_strp_update || need_string_replacement || need_stmt_update) {
813822
+  if (need_strp_update || need_line_strp_update
813822
+      || need_string_replacement || need_stmt_update) {
813822
     dirty_section (DEBUG_INFO);
813822
     if (debug_sections[DEBUG_TYPES].data != NULL)
813822
       dirty_section (DEBUG_TYPES);
813822
   }
813822
   if (need_strp_update || need_stmt_update)
813822
     dirty_section (DEBUG_MACRO);
813822
-  if (need_stmt_update)
813822
+  if (need_stmt_update || need_line_strp_update)
813822
     dirty_section (DEBUG_LINE);
813822
 
813822
   /* Update any relocations addends we might have touched. */
813822
@@ -2653,6 +2992,9 @@ edit_dwarf2 (DSO *dso)
813822
 	}
813822
     }
813822
 
813822
+  if (line_rel_updated)
813822
+    update_rela_data (dso, &debug_sections[DEBUG_LINE]);
813822
+
813822
   return 0;
813822
 }
813822
 
813822
@@ -2745,7 +3087,8 @@ fdopen_dso (int fd, const char *name)
813822
     }
813822
 
813822
   dso->filename = (const char *) strdup (name);
813822
-  setup_strings (&dso->strings);
813822
+  setup_strings (&dso->debug_str);
813822
+  setup_strings (&dso->debug_line_str);
813822
   setup_lines (&dso->lines);
813822
   return dso;
813822
 
813822
@@ -2753,7 +3096,8 @@ error_out:
813822
   if (dso)
813822
     {
813822
       free ((char *) dso->filename);
813822
-      destroy_strings (&dso->strings);
813822
+      destroy_strings (&dso->debug_str);
813822
+      destroy_strings (&dso->debug_line_str);
813822
       destroy_lines (&dso->lines);
813822
       free (dso);
813822
     }
813822
@@ -3034,7 +3378,9 @@ main (int argc, char *argv[])
813822
      in elfutils before 0.169 we will have to update and write out all
813822
      section data if any data has changed (when ELF_F_LAYOUT was
813822
      set). https://sourceware.org/bugzilla/show_bug.cgi?id=21199 */
813822
-  bool need_update = need_strp_update || need_stmt_update;
813822
+  bool need_update = (need_strp_update
813822
+		      || need_line_strp_update
813822
+		      || need_stmt_update);
813822
 
813822
 #if !_ELFUTILS_PREREQ (0, 169)
813822
   /* string replacements or build_id updates don't change section size. */
813822
@@ -3106,10 +3452,12 @@ main (int argc, char *argv[])
813822
 	      GElf_Xword sec_size = shdr->sh_size;
813822
 
813822
 	      /* We might have changed the size (and content) of the
813822
-		 debug_str or debug_line section. */
813822
+		 debug_str, debug_line_str or debug_line section. */
813822
 	      size_t secnum = elf_ndxscn (scn);
813822
 	      if (secnum == debug_sections[DEBUG_STR].sec)
813822
 		sec_size = debug_sections[DEBUG_STR].size;
813822
+	      if (secnum == debug_sections[DEBUG_LINE_STR].sec)
813822
+		sec_size = debug_sections[DEBUG_LINE_STR].size;
813822
 	      if (secnum == debug_sections[DEBUG_LINE].sec)
813822
 		sec_size = debug_sections[DEBUG_LINE].size;
813822
 
813822
@@ -3179,7 +3527,8 @@ main (int argc, char *argv[])
813822
   chmod (file, stat_buf.st_mode);
813822
 
813822
   free ((char *) dso->filename);
813822
-  destroy_strings (&dso->strings);
813822
+  destroy_strings (&dso->debug_str);
813822
+  destroy_strings (&dso->debug_line_str);
813822
   destroy_lines (&dso->lines);
813822
   free (dso);
813822
 
813822
-- 
813822
2.18.4
813822