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

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