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

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