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

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