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

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