Blame SOURCES/gdb-gdb27743-psymtab-imported-unit.patch

132741
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
132741
From: Tom Tromey <tromey@adacore.com>
132741
Date: Fri, 23 Apr 2021 11:28:48 -0600
132741
Subject: gdb-gdb27743-psymtab-imported-unit.patch
132741
132741
;; Backport "Fix crash when expanding partial symtabs with DW_TAG_imported_unit"
132741
;; (Tom Tromey, gdb/27743)
132741
132741
   From e7d77ce0c408e7019f9885b8be64c9cdb46dd312 Mon Sep 17 00:00:00 2001
132741
   Subject: [PATCH] Fix crash when expanding partial symtabs with
132741
 DW_TAG_imported_unit
132741
132741
PR gdb/27743 points out a gdb crash when expanding partial symtabs,
132741
where one of the compilation units uses DW_TAG_imported_unit.
132741
132741
The bug is that partial_map_expand_apply expects only to be called for
132741
the outermost psymtab.  However, filename searching doesn't (and
132741
probably shouldn't) guarantee this.  The fix is to walk upward to find
132741
the outermost CU.
132741
132741
A new test case is included.  It is mostly copied from other test
132741
cases, which really sped up the effort.
132741
132741
This bug does not occur on trunk.  There,
132741
psym_map_symtabs_matching_filename is gone, replaced by
132741
psymbol_functions::expand_symtabs_matching.  When this find a match,
132741
it calls psymtab_to_symtab, which does this same upward walk.
132741
132741
Tested on x86-64 Fedora 32.
132741
132741
I propose checking in this patch on the gdb-10 branch, and just the
132741
new test case on trunk.
132741
132741
gdb/ChangeLog
132741
2021-04-23  Tom Tromey  <tromey@adacore.com>
132741
132741
	PR gdb/27743:
132741
	* psymtab.c (partial_map_expand_apply): Expand outermost psymtab.
132741
132741
gdb/testsuite/ChangeLog
132741
2021-04-23  Tom Tromey  <tromey@adacore.com>
132741
132741
	PR gdb/27743:
132741
	* gdb.dwarf2/imported-unit-bp.exp: New file.
132741
	* gdb.dwarf2/imported-unit-bp-main.c: New file.
132741
	* gdb.dwarf2/imported-unit-bp-alt.c: New file.
132741
132741
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
132741
--- a/gdb/psymtab.c
132741
+++ b/gdb/psymtab.c
132741
@@ -127,9 +127,10 @@ partial_map_expand_apply (struct objfile *objfile,
132741
 {
132741
   struct compunit_symtab *last_made = objfile->compunit_symtabs;
132741
 
132741
-  /* Shared psymtabs should never be seen here.  Instead they should
132741
-     be handled properly by the caller.  */
132741
-  gdb_assert (pst->user == NULL);
132741
+  /* We may see a shared psymtab here, but we want to expand the
132741
+     outermost symtab.  */
132741
+  while (pst->user != nullptr)
132741
+    pst = pst->user;
132741
 
132741
   /* Don't visit already-expanded psymtabs.  */
132741
   if (pst->readin_p (objfile))
132741
diff --git a/gdb/testsuite/gdb.dwarf2/imported-unit-bp-alt.c b/gdb/testsuite/gdb.dwarf2/imported-unit-bp-alt.c
132741
new file mode 100644
132741
--- /dev/null
132741
+++ b/gdb/testsuite/gdb.dwarf2/imported-unit-bp-alt.c
132741
@@ -0,0 +1,50 @@
132741
+/* Copyright 2020-2021 Free Software Foundation, Inc.
132741
+
132741
+   This program is free software; you can redistribute it and/or modify
132741
+   it under the terms of the GNU General Public License as published by
132741
+   the Free Software Foundation; either version 3 of the License, or
132741
+   (at your option) any later version.
132741
+
132741
+   This program is distributed in the hope that it will be useful,
132741
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
132741
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132741
+   GNU General Public License for more details.
132741
+
132741
+   You should have received a copy of the GNU General Public License
132741
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
132741
+
132741
+/* Used to insert labels with which we can build a fake line table.  */
132741
+#define LL(N) asm ("line_label_" #N ": .globl line_label_" #N)
132741
+
132741
+volatile int var;
132741
+volatile int bar;
132741
+
132741
+/* Generate some code to take up some space.  */
132741
+#define FILLER do { \
132741
+    var = 99;	    \
132741
+} while (0)
132741
+
132741
+int
132741
+func (void)
132741
+{					/* func prologue */
132741
+  asm ("func_label: .globl func_label");
132741
+  LL (1);	// F1, Ln 16
132741
+  FILLER;
132741
+  LL (2);	// F1, Ln 17
132741
+  FILLER;
132741
+  LL (3);	// F2, Ln 21
132741
+  FILLER;
132741
+  LL (4);	// F2, Ln 22 // F1, Ln 18, !S
132741
+  FILLER;
132741
+  LL (5);	// F1, Ln 19 !S
132741
+  FILLER;
132741
+  LL (6);	// F1, Ln 20
132741
+  FILLER;
132741
+  LL (7);
132741
+  FILLER;
132741
+  return 0;				/* func end */
132741
+}
132741
+
132741
+#ifdef WITHMAIN
132741
+int main () { return 0; }
132741
+#endif
132741
diff --git a/gdb/testsuite/gdb.dwarf2/imported-unit-bp-main.c b/gdb/testsuite/gdb.dwarf2/imported-unit-bp-main.c
132741
new file mode 100644
132741
--- /dev/null
132741
+++ b/gdb/testsuite/gdb.dwarf2/imported-unit-bp-main.c
132741
@@ -0,0 +1,24 @@
132741
+/* This testcase is part of GDB, the GNU debugger.
132741
+
132741
+   Copyright 2004-2021 Free Software Foundation, Inc.
132741
+
132741
+   This program is free software; you can redistribute it and/or modify
132741
+   it under the terms of the GNU General Public License as published by
132741
+   the Free Software Foundation; either version 3 of the License, or
132741
+   (at your option) any later version.
132741
+
132741
+   This program is distributed in the hope that it will be useful,
132741
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
132741
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132741
+   GNU General Public License for more details.
132741
+
132741
+   You should have received a copy of the GNU General Public License
132741
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
132741
+
132741
+extern int func (void);
132741
+
132741
+int
132741
+main()
132741
+{
132741
+  return func ();
132741
+}
132741
diff --git a/gdb/testsuite/gdb.dwarf2/imported-unit-bp.exp b/gdb/testsuite/gdb.dwarf2/imported-unit-bp.exp
132741
new file mode 100644
132741
--- /dev/null
132741
+++ b/gdb/testsuite/gdb.dwarf2/imported-unit-bp.exp
132741
@@ -0,0 +1,128 @@
132741
+# Copyright 2020-2021 Free Software Foundation, Inc.
132741
+
132741
+# This program is free software; you can redistribute it and/or modify
132741
+# it under the terms of the GNU General Public License as published by
132741
+# the Free Software Foundation; either version 3 of the License, or
132741
+# (at your option) any later version.
132741
+#
132741
+# This program is distributed in the hope that it will be useful,
132741
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
132741
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132741
+# GNU General Public License for more details.
132741
+#
132741
+# You should have received a copy of the GNU General Public License
132741
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
132741
+
132741
+# Test that "break /absolute/file:line" works ok with imported CUs.
132741
+
132741
+load_lib dwarf.exp
132741
+
132741
+# This test can only be run on targets which support DWARF-2 and use gas.
132741
+if {![dwarf2_support]} {
132741
+    return 0
132741
+}
132741
+
132741
+# The .c files use __attribute__.
132741
+if [get_compiler_info] {
132741
+    return -1
132741
+}
132741
+if !$gcc_compiled {
132741
+    return 0
132741
+}
132741
+
132741
+standard_testfile imported-unit-bp-alt.c .S imported-unit-bp-main.c
132741
+
132741
+set build_options {nodebug optimize=-O1}
132741
+
132741
+set asm_file [standard_output_file $srcfile2]
132741
+Dwarf::assemble $asm_file {
132741
+    global srcdir subdir srcfile srcfile
132741
+    global build_options
132741
+    declare_labels lines_label callee_subprog_label cu_label
132741
+
132741
+    get_func_info func "$build_options additional_flags=-DWITHMAIN"
132741
+
132741
+    cu {} {
132741
+	compile_unit {
132741
+	    {language @DW_LANG_C}
132741
+	    {name "<artificial>"}
132741
+	} {
132741
+	    imported_unit {
132741
+		{import %$cu_label}
132741
+	    }
132741
+	}
132741
+    }
132741
+
132741
+    cu {} {
132741
+	cu_label: compile_unit {
132741
+	    {producer "gcc"}
132741
+	    {language @DW_LANG_C}
132741
+	    {name ${srcfile}}
132741
+	    {comp_dir "/tmp"}
132741
+	    {low_pc 0 addr}
132741
+	    {stmt_list ${lines_label} DW_FORM_sec_offset}
132741
+	} {
132741
+	    callee_subprog_label: subprogram {
132741
+		{external 1 flag}
132741
+		{name callee}
132741
+		{inline 3 data1}
132741
+	    }
132741
+	    subprogram {
132741
+		{external 1 flag}
132741
+		{name func}
132741
+		{low_pc $func_start addr}
132741
+		{high_pc "$func_start + $func_len" addr}
132741
+	    } {
132741
+	    }
132741
+	}
132741
+    }
132741
+
132741
+    lines {version 2 default_is_stmt 1} lines_label {
132741
+	include_dir "/tmp"
132741
+	file_name "$srcfile" 1
132741
+
132741
+	program {
132741
+	    {DW_LNE_set_address line_label_1}
132741
+	    {DW_LNS_advance_line 15}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_2}
132741
+	    {DW_LNS_advance_line 1}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_3}
132741
+	    {DW_LNS_advance_line 4}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_4}
132741
+	    {DW_LNS_advance_line 1}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNS_advance_line -4}
132741
+	    {DW_LNS_negate_stmt}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_5}
132741
+	    {DW_LNS_advance_line 1}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_6}
132741
+	    {DW_LNS_advance_line 1}
132741
+	    {DW_LNS_negate_stmt}
132741
+	    {DW_LNS_copy}
132741
+
132741
+	    {DW_LNE_set_address line_label_7}
132741
+	    {DW_LNE_end_sequence}
132741
+	}
132741
+    }
132741
+}
132741
+
132741
+if { [prepare_for_testing "failed to prepare" ${testfile} \
132741
+	  [list $srcfile $asm_file $srcfile3] $build_options] } {
132741
+    return -1
132741
+}
132741
+
132741
+gdb_reinitialize_dir /tmp
132741
+
132741
+# Using an absolute path is important to see the bug.
132741
+gdb_test "break /tmp/${srcfile}:19" "Breakpoint .* file $srcfile, line .*"