69f0c79
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
69f0c79
From: Andrew Burgess <aburgess@redhat.com>
69f0c79
Date: Mon, 19 Dec 2022 15:57:59 +0000
69f0c79
Subject: gdb-rhbz2152431-label-symbol-value.patch
69f0c79
69f0c79
Backport these two upstream commits to fix bug 2152431:
69f0c79
69f0c79
  commit 38665d717a3e65c70e6432243d5eed9728a4888a
69f0c79
  Date:   Mon Dec 12 14:09:40 2022 +0000
69f0c79
69f0c79
      gdb: use gdb_assert not internal_error
69f0c79
69f0c79
And:
69f0c79
69f0c79
  commit c3efaf0afd9d37004c42cdfd3ce0c1bfa979c45e
69f0c79
  Date:   Mon Dec 12 14:05:22 2022 +0000
69f0c79
69f0c79
      gdb: fix crash when getting the value of a label symbol
69f0c79
69f0c79
diff --git a/gdb/findvar.c b/gdb/findvar.c
69f0c79
--- a/gdb/findvar.c
69f0c79
+++ b/gdb/findvar.c
69f0c79
@@ -152,11 +152,7 @@ extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
69f0c79
 CORE_ADDR
69f0c79
 extract_typed_address (const gdb_byte *buf, struct type *type)
69f0c79
 {
69f0c79
-  if (!type->is_pointer_or_reference ())
69f0c79
-    internal_error (__FILE__, __LINE__,
69f0c79
-		    _("extract_typed_address: "
69f0c79
-		    "type is not a pointer or reference"));
69f0c79
-
69f0c79
+  gdb_assert (type->is_pointer_or_reference ());
69f0c79
   return gdbarch_pointer_to_address (type->arch (), type, buf);
69f0c79
 }
69f0c79
 
69f0c79
@@ -205,11 +201,7 @@ template void store_integer (gdb_byte *addr, int len,
69f0c79
 void
69f0c79
 store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
69f0c79
 {
69f0c79
-  if (!type->is_pointer_or_reference ())
69f0c79
-    internal_error (__FILE__, __LINE__,
69f0c79
-		    _("store_typed_address: "
69f0c79
-		    "type is not a pointer or reference"));
69f0c79
-
69f0c79
+  gdb_assert (type->is_pointer_or_reference ());
69f0c79
   gdbarch_address_to_pointer (type->arch (), type, buf, addr);
69f0c79
 }
69f0c79
 
69f0c79
@@ -634,19 +626,32 @@ language_defn::read_var_value (struct symbol *var,
69f0c79
 
69f0c79
     case LOC_LABEL:
69f0c79
       /* Put the constant back in target format.  */
69f0c79
-      v = allocate_value (type);
69f0c79
-      if (overlay_debugging)
69f0c79
-	{
69f0c79
-	  struct objfile *var_objfile = symbol_objfile (var);
69f0c79
-	  addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
69f0c79
-					   var->obj_section (var_objfile));
69f0c79
-	  store_typed_address (value_contents_raw (v).data (), type, addr);
69f0c79
-	}
69f0c79
-      else
69f0c79
-	store_typed_address (value_contents_raw (v).data (), type,
69f0c79
-			      SYMBOL_VALUE_ADDRESS (var));
69f0c79
-      VALUE_LVAL (v) = not_lval;
69f0c79
-      return v;
69f0c79
+      {
69f0c79
+	/* Put the constant back in target format.  */
69f0c79
+	if (overlay_debugging)
69f0c79
+	  {
69f0c79
+	    struct objfile *var_objfile = symbol_objfile (var);
69f0c79
+	    addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
69f0c79
+					     var->obj_section (var_objfile));
69f0c79
+	  }
69f0c79
+	else
69f0c79
+	  addr = SYMBOL_VALUE_ADDRESS (var);
69f0c79
+
69f0c79
+	/* First convert the CORE_ADDR to a function pointer type, this
69f0c79
+	   ensures the gdbarch knows what type of pointer we are
69f0c79
+	   manipulating when value_from_pointer is called.  */
69f0c79
+	type = builtin_type (symbol_arch (var))->builtin_func_ptr;
69f0c79
+	v = value_from_pointer (type, addr);
69f0c79
+
69f0c79
+	/* But we want to present the value as 'void *', so cast it to the
69f0c79
+	   required type now, this will not change the values bit
69f0c79
+	   representation.  */
69f0c79
+	struct type *void_ptr_type
69f0c79
+	  = builtin_type (symbol_arch (var))->builtin_data_ptr;
69f0c79
+	v = value_cast_pointers (void_ptr_type, v, 0);
69f0c79
+	VALUE_LVAL (v) = not_lval;
69f0c79
+	return v;
69f0c79
+      }
69f0c79
 
69f0c79
     case LOC_CONST_BYTES:
69f0c79
       if (is_dynamic_type (type))
69f0c79
diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.c b/gdb/testsuite/gdb.python/py-label-symbol-value.c
69f0c79
new file mode 100644
69f0c79
--- /dev/null
69f0c79
+++ b/gdb/testsuite/gdb.python/py-label-symbol-value.c
69f0c79
@@ -0,0 +1,38 @@
69f0c79
+/* This testcase is part of GDB, the GNU debugger.
69f0c79
+
69f0c79
+   Copyright 2022 Free Software Foundation, Inc.
69f0c79
+
69f0c79
+   This program is free software; you can redistribute it and/or modify
69f0c79
+   it under the terms of the GNU General Public License as published by
69f0c79
+   the Free Software Foundation; either version 3 of the License, or
69f0c79
+   (at your option) any later version.
69f0c79
+
69f0c79
+   This program is distributed in the hope that it will be useful,
69f0c79
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
69f0c79
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
69f0c79
+   GNU General Public License for more details.
69f0c79
+
69f0c79
+   You should have received a copy of the GNU General Public License
69f0c79
+   along with this program.  If not, see  <http://www.gnu.org/licenses/>.  */
69f0c79
+
69f0c79
+volatile int global_var = 1;
69f0c79
+
69f0c79
+int
69f0c79
+get_value ()
69f0c79
+{
69f0c79
+  return global_var;
69f0c79
+}
69f0c79
+
69f0c79
+int
69f0c79
+main (void)
69f0c79
+{
69f0c79
+  int value = get_value ();
69f0c79
+  if (value > 0)
69f0c79
+    goto some_label;
69f0c79
+
69f0c79
+  return 1;
69f0c79
+
69f0c79
+ some_label:
69f0c79
+
69f0c79
+  return 0;
69f0c79
+}
69f0c79
diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.exp b/gdb/testsuite/gdb.python/py-label-symbol-value.exp
69f0c79
new file mode 100644
69f0c79
--- /dev/null
69f0c79
+++ b/gdb/testsuite/gdb.python/py-label-symbol-value.exp
69f0c79
@@ -0,0 +1,39 @@
69f0c79
+# Copyright 2022 Free Software Foundation, Inc.
69f0c79
+
69f0c79
+# This program is free software; you can redistribute it and/or modify
69f0c79
+# it under the terms of the GNU General Public License as published by
69f0c79
+# the Free Software Foundation; either version 3 of the License, or
69f0c79
+# (at your option) any later version.
69f0c79
+#
69f0c79
+# This program is distributed in the hope that it will be useful,
69f0c79
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
69f0c79
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
69f0c79
+# GNU General Public License for more details.
69f0c79
+#
69f0c79
+# You should have received a copy of the GNU General Public License
69f0c79
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
69f0c79
+
69f0c79
+# Check that GDB handles the user asking for the value of a label
69f0c79
+# symbol (i.e. a symbol for a goto label).
69f0c79
+
69f0c79
+load_lib gdb-python.exp
69f0c79
+standard_testfile
69f0c79
+
69f0c79
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
69f0c79
+    return -1
69f0c79
+}
69f0c79
+
69f0c79
+# Skip all tests if Python scripting is not enabled.
69f0c79
+if { [skip_python_tests] } { continue }
69f0c79
+
69f0c79
+if ![runto_main] {
69f0c79
+   return -1
69f0c79
+}
69f0c79
+
69f0c79
+# Use Python to print the value of the 'some_label' symbol.
69f0c79
+gdb_test "python frame = gdb.selected_frame()"
69f0c79
+gdb_test "python frame_pc = frame.pc()"
69f0c79
+gdb_test "python block = gdb.current_progspace().block_for_pc(frame_pc)"
69f0c79
+gdb_test "python symbol,_ = gdb.lookup_symbol('some_label', block, gdb.SYMBOL_LABEL_DOMAIN)"
69f0c79
+gdb_test "python print(str(symbol.value()))" "$hex <main\\+$decimal>"
69f0c79
+gdb_test "python print(str(symbol.value().type))" "void \\*"