Blame SOURCES/gdb-rhbz1934673-fortran-nameless-modules.patch

599b31
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
599b31
From: Keith Seitz <keiths@redhat.com>
599b31
Date: Wed, 5 May 2021 13:57:13 -0700
599b31
Subject: gdb-rhbz1934673-fortran-nameless-modules.patch
599b31
599b31
;; Fix segfault with nameless fortran modules.
599b31
;; Bernhard Heckel, RH BZ 1943673
599b31
599b31
    Dwarf: Don't add nameless modules to partial symbol table
599b31
599b31
    A name for BLOCK DATA in Fortran is optional.  If no name has been
599b31
    assigned, GDB crashes during read-in of DWARF when BLOCK DATA is
599b31
    represented via DW_TAG_module.  BLOCK DATA is used for one-time
599b31
    initialization of non-pointer variables in named common blocks.
599b31
599b31
    As of now there is no issue when gfortran is used as DW_TAG_module is
599b31
    not emitted.  However, with Intel ifort the nameless DW_TAG_module is
599b31
    present and has the following form:
599b31
599b31
     ...
599b31
      <1>
: Abbrev Number: 7 (DW_TAG_module)
599b31
         <de>   DW_AT_decl_line   : 46
599b31
         <df>   DW_AT_decl_file   : 1
599b31
         <e0>   DW_AT_description : (indirect string, offset: 0x110): block
599b31
     data
599b31
         <e4>   DW_AT_high_pc     : 0x402bb7
599b31
         <ec>   DW_AT_low_pc      : 0x402bb7
599b31
     ...
599b31
599b31
    The missing name leads to a crash in add_partial_symbol, during length
599b31
    calculation.
599b31
599b31
    gdb/ChangeLog:
599b31
    2019-06-11  Bernhard Heckel  <bernhard.heckel@intel.com>
599b31
599b31
            * dwarf2read.c (add_partial_symbol): Skip nameless modules.
599b31
599b31
    gdb/testsuite/Changelog:
599b31
    2019-06-11  Bernhard Heckel  <bernhard.heckel@intel.com>
599b31
599b31
            * gdb.fortran/block-data.f: New.
599b31
            * gdb.fortran/block-data.exp: New.
599b31
599b31
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
599b31
--- a/gdb/dwarf2read.c
599b31
+++ b/gdb/dwarf2read.c
599b31
@@ -8936,11 +8936,15 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
599b31
 			   0, cu->language, objfile);
599b31
       break;
599b31
     case DW_TAG_module:
599b31
-      add_psymbol_to_list (actual_name, strlen (actual_name),
599b31
-			   built_actual_name != NULL,
599b31
-			   MODULE_DOMAIN, LOC_TYPEDEF,
599b31
-			   &objfile->global_psymbols,
599b31
-			   0, cu->language, objfile);
599b31
+      /* With Fortran 77 there might be a "BLOCK DATA" module
599b31
+         available without any name.  If so, we skip the module as it
599b31
+         doesn't bring any value.  */
599b31
+      if (actual_name != nullptr)
599b31
+	add_psymbol_to_list (actual_name, strlen (actual_name),
599b31
+			     built_actual_name != NULL,
599b31
+			     MODULE_DOMAIN, LOC_TYPEDEF,
599b31
+			     &objfile->global_psymbols,
599b31
+			     0, cu->language, objfile);
599b31
       break;
599b31
     case DW_TAG_class_type:
599b31
     case DW_TAG_interface_type:
599b31
@@ -16777,9 +16781,6 @@ read_module_type (struct die_info *die, struct dwarf2_cu *cu)
599b31
   struct type *type;
599b31
 
599b31
   module_name = dwarf2_name (die, cu);
599b31
-  if (!module_name)
599b31
-    complaint (_("DW_TAG_module has no name, offset %s"),
599b31
-               sect_offset_str (die->sect_off));
599b31
   type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
599b31
 
599b31
   return set_die_type (die, type, cu);
599b31
diff --git a/gdb/testsuite/gdb.fortran/block-data.exp b/gdb/testsuite/gdb.fortran/block-data.exp
599b31
new file mode 100644
599b31
--- /dev/null
599b31
+++ b/gdb/testsuite/gdb.fortran/block-data.exp
599b31
@@ -0,0 +1,63 @@
599b31
+# Copyright 2016-2019 Free Software Foundation, Inc.
599b31
+#
599b31
+# This program is free software; you can redistribute it and/or modify
599b31
+# it under the terms of the GNU General Public License as published by
599b31
+# the Free Software Foundation; either version 3 of the License, or
599b31
+# (at your option) any later version.
599b31
+#
599b31
+# This program is distributed in the hope that it will be useful,
599b31
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
599b31
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
599b31
+# GNU General Public License for more details.
599b31
+#
599b31
+# You should have received a copy of the GNU General Public License
599b31
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
599b31
+
599b31
+# Test anonymous block-data statements.
599b31
+
599b31
+# A name for BLOCK DATA in Fortran is optional.  BLOCK DATA is used
599b31
+# for one-time initialization of non-pointer variables in named common
599b31
+# blocks.  GDB used to crash with 'Intel ifort'-generated code, which
599b31
+# outputs nameless DW_TAG_module, unlike with gfortran which just
599b31
+# doesn't emit DW_TAG_module in this case.
599b31
+
599b31
+if { [skip_fortran_tests] } { return -1 }
599b31
+
599b31
+standard_testfile .f
599b31
+load_lib "fortran.exp"
599b31
+
599b31
+if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug f90}]} {
599b31
+    return -1
599b31
+}
599b31
+
599b31
+if ![runto MAIN__] then {
599b31
+    untested "couldn't run to breakpoint MAIN__"
599b31
+    return -1
599b31
+}
599b31
+
599b31
+with_test_prefix "default values" {
599b31
+    gdb_test "print doub1" "= 1\.11\\d+"
599b31
+    gdb_test "print doub2" "= 2\.22\\d+"
599b31
+    gdb_test "print char1" "= 'abcdef'"
599b31
+    gdb_test "print char2" "= 'ghijkl'"
599b31
+}
599b31
+
599b31
+gdb_breakpoint [gdb_get_line_number "! BP_BEFORE_SUB"]
599b31
+gdb_continue_to_breakpoint "! BP_BEFORE_SUB" ".*! BP_BEFORE_SUB.*"
599b31
+
599b31
+with_test_prefix "before sub" {
599b31
+    gdb_test "print doub1" "= 11\.11\\d+"
599b31
+    gdb_test "print doub2" "= 22\.22\\d+"
599b31
+    gdb_test "print char1" "= 'ABCDEF'"
599b31
+    gdb_test "print char2" "= 'GHIJKL'"
599b31
+}
599b31
+
599b31
+gdb_breakpoint [gdb_get_line_number "! BP_SUB"]
599b31
+gdb_continue_to_breakpoint "! BP_SUB" ".*! BP_SUB.*"
599b31
+
599b31
+with_test_prefix "in sub" {
599b31
+    gdb_test "print doub1" "= 11\.11\\d+"
599b31
+    gdb_test "print doub2" "= 22\.22\\d+"
599b31
+    gdb_test "print char1" "= 'ABCDEF'"
599b31
+    gdb_test "print char2" "= 'GHIJKL'"
599b31
+}
599b31
diff --git a/gdb/testsuite/gdb.fortran/block-data.f b/gdb/testsuite/gdb.fortran/block-data.f
599b31
new file mode 100644
599b31
--- /dev/null
599b31
+++ b/gdb/testsuite/gdb.fortran/block-data.f
599b31
@@ -0,0 +1,56 @@
599b31
+! Copyright 2016-2019 Free Software Foundation, Inc.
599b31
+!
599b31
+! This program is free software; you can redistribute it and/or modify
599b31
+! it under the terms of the GNU General Public License as published by
599b31
+! the Free Software Foundation; either version 3 of the License, or
599b31
+! (at your option) any later version.
599b31
+!
599b31
+! This program is distributed in the hope that it will be useful,
599b31
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
599b31
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
599b31
+! GNU General Public License for more details.
599b31
+!
599b31
+! You should have received a copy of the GNU General Public License
599b31
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
599b31
+!
599b31
+! Check that GDB can handle block data with no global name.
599b31
+!
599b31
+! MAIN
599b31
+        PROGRAM bdata
599b31
+        DOUBLE PRECISION doub1, doub2
599b31
+        CHARACTER*6 char1, char2
599b31
+
599b31
+        COMMON /BLK1/ doub1, char1
599b31
+        COMMON /BLK2/ doub2, char2
599b31
+
599b31
+        doub1 = 11.111
599b31
+        doub2 = 22.222
599b31
+        char1 = 'ABCDEF'
599b31
+        char2 = 'GHIJKL'
599b31
+        CALL sub_block_data      ! BP_BEFORE_SUB
599b31
+        STOP
599b31
+        END
599b31
+
599b31
+! BLOCK DATA
599b31
+        BLOCK DATA
599b31
+
599b31
+        DOUBLE PRECISION doub1, doub2
599b31
+        CHARACTER*6 char1, char2
599b31
+
599b31
+        COMMON /BLK1/ doub1, char1
599b31
+        COMMON /BLK2/ doub2, char2
599b31
+        DATA doub1, doub2 /1.111, 2.222/
599b31
+        DATA char1, char2 /'abcdef', 'ghijkl'/
599b31
+        END
599b31
+
599b31
+! SUBROUTINE
599b31
+        SUBROUTINE sub_block_data
599b31
+
599b31
+        DOUBLE PRECISION doub1, doub2
599b31
+        CHARACTER*6 char1, char2
599b31
+
599b31
+        COMMON /BLK1/ doub1, char1
599b31
+        COMMON /BLK2/ doub2, char2
599b31
+
599b31
+        char1 = char2;    ! BP_SUB
599b31
+        END