File fix-rhbz2403580-misplaced-symtabs-due-to-dwz.patch of Package gdb

From 1a9251041357be4c94971113101086be025affbd Mon Sep 17 00:00:00 2001
From: Andrew Burgess <aburgess@redhat.com>
Date: Tue, 10 Feb 2026 11:47:12 +0100
Subject: [PATCH 1/4] fix rhbz2403580 - misplaced symtabs due to dwz

Backport upstream commit 70b66cf338b14336 to fix RH BZ 2403580.

Bug: https://bugzilla.redhat.com/show_bug.cgi?id=2403580

Cherry-picked from fedora @ 34c5cd8.
---
 gdb/dwarf2/line-header.c | 43 ++++++++++++++++++++++++++++++++++++++++
 gdb/dwarf2/line-header.h | 13 +++++++++++-
 gdb/dwarf2/read.c        | 15 ++++++++------
 3 files changed, 64 insertions(+), 7 deletions(-)

diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
index eddb2ef7ae8..0f4407f2fb2 100644
--- a/gdb/dwarf2/line-header.c
+++ b/gdb/dwarf2/line-header.c
@@ -418,3 +418,46 @@ dwarf_decode_line_header  (sect_offset sect_off, bool is_dwz,
 
   return lh;
 }
+
+/* See dwarf2/line-header.h.  */
+
+struct symtab *
+file_entry::symtab (dwarf2_cu &cu)
+{
+  if (m_symtab == nullptr)
+    {
+      buildsym_compunit *builder = cu.get_builder ();
+      compunit_symtab *cust = builder->get_compunit_symtab ();
+
+      {
+	std::string filename_holder;
+	const char *filename = this->name;
+	const char *dirname = cu.line_header->include_dir_at (this->d_index);
+
+	/* In order not to lose the line information directory,
+	   we concatenate it to the filename when it makes sense.
+	   Note that the Dwarf3 standard says (speaking of filenames in line
+	   information): ``The directory index is ignored for file names
+	   that represent full path names''.  Thus ignoring dirname in the
+	   `else' branch below isn't an issue.  */
+
+	if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
+	  {
+	    filename_holder = path_join (dirname, filename);
+	    filename = filename_holder.c_str ();
+	  }
+
+	std::string filename_for_id = cu.line_header->file_file_name (*this);
+	cu.get_builder ()->start_subfile (filename, filename_for_id.c_str ());
+      }
+
+      subfile *sf = builder->get_current_subfile ();
+      if (sf->symtab == nullptr)
+	sf->symtab = allocate_symtab (cust, sf->name.c_str (),
+				      sf->name_for_id.c_str ());
+
+      m_symtab = sf->symtab;
+    }
+
+  return m_symtab;
+}
diff --git a/gdb/dwarf2/line-header.h b/gdb/dwarf2/line-header.h
index 30809f69acc..f9b5f81eecd 100644
--- a/gdb/dwarf2/line-header.h
+++ b/gdb/dwarf2/line-header.h
@@ -23,6 +23,7 @@
 #include "dwarf2/types.h"
 
 struct dwarf2_per_objfile;
+struct dwarf2_cu;
 
 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
    later.  */
@@ -65,8 +66,18 @@ struct file_entry
 
   unsigned int length {};
 
+  /* Get the symtab for this file_entry.  If no symtab has yet been created
+     or set (see set_symtab) for this file_entry then a new one will be
+     created.  */
+  struct symtab *symtab (struct dwarf2_cu &cu);
+
+  /* Set the symtab for this file_entry.  */
+  void set_symtab (struct symtab *s)
+  { m_symtab = s; }
+
+private:
   /* The associated symbol table, if any.  */
-  struct symtab *symtab {};
+  struct symtab *m_symtab {};
 };
 
 /* The line number information for a compilation unit (found in the
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index f8f411c195a..f4a1eaf0f53 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -7669,8 +7669,8 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
 	      sf->symtab = allocate_symtab (cust, name, name_for_id);
 	    }
 
-	  fe.symtab = b->get_current_subfile ()->symtab;
-	  tug_unshare->symtabs[i] = fe.symtab;
+	  fe.set_symtab (b->get_current_subfile ()->symtab);
+	  tug_unshare->symtabs[i] = fe.symtab (*this);
 	}
     }
   else
@@ -7688,7 +7688,7 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
       for (i = 0; i < file_names.size (); ++i)
 	{
 	  file_entry &fe = file_names[i];
-	  fe.symtab = tug_unshare->symtabs[i];
+	  fe.set_symtab (tug_unshare->symtabs[i]);
 	}
     }
 
@@ -13291,7 +13291,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 	    {
 	      /* Any related symtab will do.  */
 	      symtab
-		= cu->line_header->file_names ()[0].symtab;
+		= cu->line_header->file_names ()[0].symtab (*cu);
 	    }
 	  else
 	    {
@@ -18968,6 +18968,9 @@ dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
   if (decode_mapping)
     dwarf_decode_lines_1 (lh, cu, lowpc);
 
+  if (cu->per_cu->is_dwz)
+    return;
+
   /* Make sure a symtab is created for every file, even files
      which contain only variables (i.e. no code with associated
      line numbers).  */
@@ -18983,7 +18986,7 @@ dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
 	sf->symtab = allocate_symtab (cust, sf->name.c_str (),
 				      sf->name_for_id.c_str ());
 
-      fe.symtab = sf->symtab;
+      fe.set_symtab (sf->symtab);
     }
 }
 
@@ -19256,7 +19259,7 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
 	  if (fe == NULL)
 	    complaint (_("file index out of range"));
 	  else
-	    sym->set_symtab (fe->symtab);
+	    sym->set_symtab (fe->symtab (*file_cu));
 	}
 
       switch (die->tag)

base-commit: 8b5dc3da60d6c07991821c447f183288c4aac69e
-- 
2.51.0

openSUSE Build Service is sponsored by