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

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