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

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