Blame SOURCES/gdb-rhbz1905701-DWARF-data_location.patch

0d202d
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
0d202d
From: Keith Seitz <keiths@redhat.com>
0d202d
Date: Wed, 2 Dec 2020 17:39:33 -0500
0d202d
Subject: gdb-rhbz1905701-DWARF-data_location.patch
0d202d
0d202d
;; Backport "fortran dynamic type related fixes"
0d202d
;; Andrew Burgess (RH BZ 1905701)
0d202d
0d202d
    commit e79eb02f2f09baecffb144bac6804f975065466f
0d202d
0d202d
    gdb/fortran: resolve dynamic types when readjusting after an indirection
0d202d
0d202d
    After dereferencing a pointer (in value_ind) or following a
0d202d
    reference (in coerce_ref) we call readjust_indirect_value_type to
0d202d
    "fixup" the type of the resulting value object.
0d202d
0d202d
    This fixup handles cases relating to the type of the resulting object
0d202d
    being different (a sub-class) of the original pointers target type.
0d202d
0d202d
    If we encounter a pointer to a dynamic type then after dereferencing a
0d202d
    pointer (in value_ind) the type of the object created will have had
0d202d
    its dynamic type resolved.  However, in readjust_indirect_value_type,
0d202d
    we use the target type of the original pointer to "fixup" the type of
0d202d
    the resulting value.  In this case, the target type will be a dynamic
0d202d
    type, so the resulting value object, once again has a dynamic type.
0d202d
0d202d
    This then triggers an assertion later within GDB.
0d202d
0d202d
    The solution I propose here is that we call resolve_dynamic_type on
0d202d
    the pointer's target type (within readjust_indirect_value_type) so
0d202d
    that the resulting value is not converted back to a dynamic type.
0d202d
0d202d
    The test case is based on the original test in the bug report.
0d202d
0d202d
    gdb/ChangeLog:
0d202d
0d202d
            PR fortran/23051
0d202d
            PR fortran/26139
0d202d
            * valops.c (value_ind): Pass address to
0d202d
            readjust_indirect_value_type.
0d202d
            * value.c (readjust_indirect_value_type): Make parameter
0d202d
            non-const, and add extra address parameter.  Resolve original type
0d202d
            before using it.
0d202d
            * value.h (readjust_indirect_value_type): Update function
0d202d
            signature and comment.
0d202d
0d202d
    gdb/testsuite/ChangeLog:
0d202d
0d202d
            PR fortran/23051
0d202d
            PR fortran/26139
0d202d
            * gdb.fortran/class-allocatable-array.exp: New file.
0d202d
            * gdb.fortran/class-allocatable-array.f90: New file.
0d202d
            * gdb.fortran/pointer-to-pointer.exp: New file.
0d202d
            * gdb.fortran/pointer-to-pointer.f90: New file.
0d202d
0d202d
diff --git a/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.c b/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.c
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.c
0d202d
@@ -0,0 +1,63 @@
0d202d
+/* Copyright 2014-2020 Free Software Foundation, Inc.
0d202d
+
0d202d
+   This file is part of GDB.
0d202d
+
0d202d
+   This program is free software; you can redistribute it and/or modify
0d202d
+   it under the terms of the GNU General Public License as published by
0d202d
+   the Free Software Foundation; either version 3 of the License, or
0d202d
+   (at your option) any later version.
0d202d
+
0d202d
+   This program is distributed in the hope that it will be useful,
0d202d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+   GNU General Public License for more details.
0d202d
+
0d202d
+   You should have received a copy of the GNU General Public License
0d202d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
0d202d
+
0d202d
+/* This C file simulates the implementation of object pointers in
0d202d
+   GraalVM Java native images where the object data is not addressed
0d202d
+   directly. It serves as a regression test for a bug where printing
0d202d
+   of such redirected data structures suffers from a gdb exception.
0d202d
+
0d202d
+   Debugging information on how to decode an object pointer to
0d202d
+   identify the address of the underlying data will be generated
0d202d
+   separately by the testcase using that file. */
0d202d
+
0d202d
+#include <stdlib.h>
0d202d
+#include <string.h>
0d202d
+
0d202d
+struct Object {
0d202d
+  struct Object *next;
0d202d
+  int val;
0d202d
+};
0d202d
+
0d202d
+struct Object *testOop;
0d202d
+
0d202d
+extern int debugMe() {
0d202d
+  return 0;
0d202d
+}
0d202d
+
0d202d
+struct Object *newObject() {
0d202d
+  char *bytes = malloc(sizeof(struct Object));
0d202d
+  return (struct Object *)bytes;
0d202d
+}
0d202d
+
0d202d
+int
0d202d
+main (void)
0d202d
+{
0d202d
+  struct Object *obj1 = newObject();
0d202d
+  struct Object *obj2 = newObject();
0d202d
+  struct Object *obj3 = newObject();
0d202d
+  obj1->val = 0;
0d202d
+  obj2->val = 1;
0d202d
+  obj3->val = 2;
0d202d
+
0d202d
+  obj1->next = obj2;
0d202d
+  obj2->next = obj3;
0d202d
+  obj3->next = obj1;
0d202d
+
0d202d
+  testOop = obj1;
0d202d
+
0d202d
+  return debugMe();
0d202d
+}
0d202d
diff --git a/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.exp b/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.exp
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.dwarf2/graalvm-data-loc2.exp
0d202d
@@ -0,0 +1,122 @@
0d202d
+# Copyright 2014-2020 Free Software Foundation, Inc.
0d202d
+
0d202d
+# This program is free software; you can redistribute it and/or modify
0d202d
+# it under the terms of the GNU General Public License as published by
0d202d
+# the Free Software Foundation; either version 3 of the License, or
0d202d
+# (at your option) any later version.
0d202d
+#
0d202d
+# This program is distributed in the hope that it will be useful,
0d202d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+# GNU General Public License for more details.
0d202d
+#
0d202d
+# You should have received a copy of the GNU General Public License
0d202d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0d202d
+load_lib dwarf.exp
0d202d
+
0d202d
+# This test can only be run on targets which support DWARF-2 and use gas.
0d202d
+if {![dwarf2_support]} {
0d202d
+    return 0
0d202d
+}
0d202d
+
0d202d
+standard_testfile .c -dw.S
0d202d
+
0d202d
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+# Make some DWARF for the test.
0d202d
+set asm_file [standard_output_file $srcfile2]
0d202d
+Dwarf::assemble $asm_file {
0d202d
+    
0d202d
+    cu {} {
0d202d
+ 	DW_TAG_compile_unit {
0d202d
+                {DW_AT_language @DW_LANG_C99}
0d202d
+                {DW_AT_name     data-loc2.c}
0d202d
+                {DW_AT_comp_dir /tmp}
0d202d
+        } {
0d202d
+            declare_labels integer_label struct_label pointer_label
0d202d
+
0d202d
+            integer_label: DW_TAG_base_type {
0d202d
+                {DW_AT_byte_size 4 DW_FORM_sdata}
0d202d
+                {DW_AT_encoding  @DW_ATE_signed}
0d202d
+                {DW_AT_name      integer}
0d202d
+            }
0d202d
+
0d202d
+	    struct_label: DW_TAG_structure_type {
0d202d
+		{DW_AT_name "Object"}
0d202d
+		{DW_AT_byte_size 20 DW_FORM_sdata}
0d202d
+                {DW_AT_data_location {
0d202d
+                    DW_OP_push_object_address
0d202d
+                } SPECIAL_expr}
0d202d
+	    } {
0d202d
+		member {
0d202d
+		    {name next}
0d202d
+		    {type :$pointer_label}
0d202d
+		    {data_member_location 0 data1}
0d202d
+		}
0d202d
+		member {
0d202d
+		    {name val}
0d202d
+		    {type :$integer_label}
0d202d
+		    {data_member_location 8 data1}
0d202d
+		}
0d202d
+	    }
0d202d
+	    pointer_label: DW_TAG_pointer_type {
0d202d
+		{DW_AT_byte_size 4 DW_FORM_sdata}
0d202d
+		{DW_AT_type  :$struct_label}
0d202d
+	    }
0d202d
+            DW_TAG_variable {
0d202d
+                {DW_AT_name testOop}
0d202d
+                {DW_AT_type :$pointer_label}
0d202d
+                {DW_AT_location {
0d202d
+                    DW_OP_addr [gdb_target_symbol testOop]
0d202d
+                } SPECIAL_expr}
0d202d
+                {external 1 flag}
0d202d
+            }
0d202d
+	}
0d202d
+    }
0d202d
+}
0d202d
+
0d202d
+# Now that we've generated the DWARF debugging info, rebuild our
0d202d
+# program using our debug info instead of the info generated by
0d202d
+# the compiler.
0d202d
+
0d202d
+if { [prepare_for_testing "failed to prepare" ${testfile} \
0d202d
+	  [list $srcfile $asm_file] {nodebug}] } {
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+if ![runto_main] {
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+# ensure the object network is set up as expected and check that
0d202d
+# printing of structs which employ the data_location does not
0d202d
+# fail with a gdb exception
0d202d
+
0d202d
+gdb_test "break debugMe" \
0d202d
+         "Breakpoint .*" \
0d202d
+         "set breakpoint at debugMe"
0d202d
+
0d202d
+gdb_continue_to_breakpoint "continue to debugMe"
0d202d
+
0d202d
+gdb_test "print testOop->val" \
0d202d
+         ".* = 0"
0d202d
+
0d202d
+gdb_test "print testOop->next->val" \
0d202d
+         ".* = 1"
0d202d
+
0d202d
+gdb_test "print testOop->next->next->val" \
0d202d
+         ".* = 2"
0d202d
+
0d202d
+gdb_test "print *testOop" \
0d202d
+    ".* = {next = .*, val = 0}" \
0d202d
+    "print contents of struct"
0d202d
+
0d202d
+gdb_test "print *testOop->next" \
0d202d
+    ".* = {next = .*, val = 1}" \
0d202d
+    "print contents of an indirect struct"
0d202d
+
0d202d
+gdb_test "print *testOop->next->next" \
0d202d
+    ".* = {next = .*, val = 2}" \
0d202d
+    "print contents of a double indirect struct"
0d202d
diff --git a/gdb/testsuite/gdb.fortran/class-allocatable-array.exp b/gdb/testsuite/gdb.fortran/class-allocatable-array.exp
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.fortran/class-allocatable-array.exp
0d202d
@@ -0,0 +1,43 @@
0d202d
+# Copyright 2020 Free Software Foundation, Inc.
0d202d
+
0d202d
+# This program is free software; you can redistribute it and/or modify
0d202d
+# it under the terms of the GNU General Public License as published by
0d202d
+# the Free Software Foundation; either version 3 of the License, or
0d202d
+# (at your option) any later version.
0d202d
+#
0d202d
+# This program is distributed in the hope that it will be useful,
0d202d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+# GNU General Public License for more details.
0d202d
+#
0d202d
+# You should have received a copy of the GNU General Public License
0d202d
+# along with this program.  If not, see <http://www.gnu.org/licenses/> .
0d202d
+
0d202d
+# Test that GDB can print an allocatable array that is a data field
0d202d
+# within a class like type.
0d202d
+
0d202d
+if {[skip_fortran_tests]} { return -1 }
0d202d
+
0d202d
+standard_testfile ".f90"
0d202d
+load_lib fortran.exp
0d202d
+
0d202d
+if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
0d202d
+	 {debug f90}]} {
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+if {![runto MAIN__]} {
0d202d
+    untested main"could not run to main"
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+gdb_breakpoint [gdb_get_line_number "Break Here"]
0d202d
+gdb_continue_to_breakpoint "Break Here"
0d202d
+
0d202d
+# If this first test fails then the Fortran compiler being used uses
0d202d
+# different names, or maybe a completely different approach, for
0d202d
+# representing class like structures.  The following tests are
0d202d
+# cetainly going to fail.
0d202d
+gdb_test "print this" " = \\( _data = \[^\r\n\]+, _vptr = \[^\r\n\]+\\)"
0d202d
+gdb_test "print this%_data" " = \\(PTR TO -> \\( Type test_type \\)\\) \[^\r\n\]+"
0d202d
+gdb_test "print this%_data%b" " = \\(\\( 1, 2, 3\\) \\( 4, 5, 6\\) \\)"
0d202d
diff --git a/gdb/testsuite/gdb.fortran/class-allocatable-array.f90 b/gdb/testsuite/gdb.fortran/class-allocatable-array.f90
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.fortran/class-allocatable-array.f90
0d202d
@@ -0,0 +1,54 @@
0d202d
+! Copyright 2020 Free Software Foundation, Inc.
0d202d
+!
0d202d
+! This program is free software; you can redistribute it and/or modify
0d202d
+! it under the terms of the GNU General Public License as published by
0d202d
+! the Free Software Foundation; either version 3 of the License, or
0d202d
+! (at your option) any later version.
0d202d
+!
0d202d
+! This program is distributed in the hope that it will be useful,
0d202d
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+! GNU General Public License for more details.
0d202d
+!
0d202d
+! You should have received a copy of the GNU General Public License
0d202d
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
0d202d
+
0d202d
+module test_module
0d202d
+  type test_type
0d202d
+     integer a
0d202d
+     real, allocatable :: b (:, :)
0d202d
+   contains
0d202d
+     procedure :: test_proc
0d202d
+  end type test_type
0d202d
+
0d202d
+contains
0d202d
+
0d202d
+  subroutine test_proc (this)
0d202d
+    class(test_type), intent (inout) :: this
0d202d
+    allocate (this%b (3, 2))
0d202d
+    call fill_array_2d (this%b)
0d202d
+    print *, ""		! Break Here
0d202d
+  contains
0d202d
+    ! Helper subroutine to fill 2-dimensional array with unique
0d202d
+    ! values.
0d202d
+    subroutine fill_array_2d (array)
0d202d
+      real, dimension (:,:) :: array
0d202d
+      real :: counter
0d202d
+
0d202d
+      counter = 1.0
0d202d
+      do i=LBOUND (array, 2), UBOUND (array, 2), 1
0d202d
+         do j=LBOUND (array, 1), UBOUND (array, 1), 1
0d202d
+            array (j,i) = counter
0d202d
+            counter = counter + 1
0d202d
+         end do
0d202d
+      end do
0d202d
+    end subroutine fill_array_2d
0d202d
+  end subroutine test_proc
0d202d
+end module
0d202d
+
0d202d
+program test
0d202d
+  use test_module
0d202d
+  implicit none
0d202d
+  type(test_type) :: t
0d202d
+  call t%test_proc ()
0d202d
+end program test
0d202d
diff --git a/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp b/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.fortran/pointer-to-pointer.exp
0d202d
@@ -0,0 +1,46 @@
0d202d
+# Copyright 2020 Free Software Foundation, Inc.
0d202d
+
0d202d
+# This program is free software; you can redistribute it and/or modify
0d202d
+# it under the terms of the GNU General Public License as published by
0d202d
+# the Free Software Foundation; either version 3 of the License, or
0d202d
+# (at your option) any later version.
0d202d
+#
0d202d
+# This program is distributed in the hope that it will be useful,
0d202d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+# GNU General Public License for more details.
0d202d
+#
0d202d
+# You should have received a copy of the GNU General Public License
0d202d
+# along with this program.  If not, see <http://www.gnu.org/licenses/> .
0d202d
+
0d202d
+# Test for GDB printing a pointer to a type containing a buffer.
0d202d
+
0d202d
+if {[skip_fortran_tests]} { return -1 }
0d202d
+
0d202d
+standard_testfile ".f90"
0d202d
+load_lib fortran.exp
0d202d
+
0d202d
+if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
0d202d
+	 {debug f90}]} {
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+if {![runto MAIN__]} {
0d202d
+    untested "could not run to main"
0d202d
+    return -1
0d202d
+}
0d202d
+
0d202d
+gdb_breakpoint [gdb_get_line_number "Break Here"]
0d202d
+gdb_continue_to_breakpoint "Break Here"
0d202d
+
0d202d
+gdb_test "print *buffer" \
0d202d
+    " = \\( alpha = \\(1\\.5, 2\\.5, 3\\.5, 4\\.5, 5\\.5\\) \\)"
0d202d
+
0d202d
+set l_buffer_type [multi_line \
0d202d
+		       "Type l_buffer" \
0d202d
+		       "    real\\(kind=4\\) :: alpha\\(.\\)" \
0d202d
+		       "End Type l_buffer" ]
0d202d
+
0d202d
+gdb_test "ptype buffer" "type = PTR TO -> \\( ${l_buffer_type} \\)"
0d202d
+gdb_test "ptype *buffer" "type = ${l_buffer_type}"
0d202d
+gdb_test "ptype buffer%alpha" "type = real\\(kind=4\\) \\(5\\)"
0d202d
diff --git a/gdb/testsuite/gdb.fortran/pointer-to-pointer.f90 b/gdb/testsuite/gdb.fortran/pointer-to-pointer.f90
0d202d
new file mode 100644
0d202d
--- /dev/null
0d202d
+++ b/gdb/testsuite/gdb.fortran/pointer-to-pointer.f90
0d202d
@@ -0,0 +1,34 @@
0d202d
+! Copyright 2020 Free Software Foundation, Inc.
0d202d
+!
0d202d
+! This program is free software; you can redistribute it and/or modify
0d202d
+! it under the terms of the GNU General Public License as published by
0d202d
+! the Free Software Foundation; either version 3 of the License, or
0d202d
+! (at your option) any later version.
0d202d
+!
0d202d
+! This program is distributed in the hope that it will be useful,
0d202d
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
0d202d
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0d202d
+! GNU General Public License for more details.
0d202d
+!
0d202d
+! You should have received a copy of the GNU General Public License
0d202d
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
0d202d
+
0d202d
+program allocate_array
0d202d
+
0d202d
+  type l_buffer
0d202d
+     real, dimension(:), pointer :: alpha
0d202d
+  end type l_buffer
0d202d
+  type(l_buffer), pointer :: buffer
0d202d
+
0d202d
+  allocate (buffer)
0d202d
+  allocate (buffer%alpha (5))
0d202d
+
0d202d
+  buffer%alpha (1) = 1.5
0d202d
+  buffer%alpha (2) = 2.5
0d202d
+  buffer%alpha (3) = 3.5
0d202d
+  buffer%alpha (4) = 4.5
0d202d
+  buffer%alpha (5) = 5.5
0d202d
+
0d202d
+  print *, buffer%alpha	! Break Here.
0d202d
+
0d202d
+end program allocate_array
0d202d
diff --git a/gdb/valops.c b/gdb/valops.c
0d202d
--- a/gdb/valops.c
0d202d
+++ b/gdb/valops.c
0d202d
@@ -1573,42 +1573,34 @@ value_ind (struct value *arg1)
0d202d
   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
0d202d
     {
0d202d
       struct type *enc_type;
0d202d
-      CORE_ADDR addr;
0d202d
-
0d202d
-      if (type_not_associated (base_type))
0d202d
-        error (_("Attempt to take contents of a not associated pointer."));
0d202d
-
0d202d
-      if (NULL != TYPE_DATA_LOCATION (TYPE_TARGET_TYPE (base_type)))
0d202d
-	addr = value_address (arg1);
0d202d
-      else
0d202d
-	addr = value_as_address (arg1);
0d202d
-
0d202d
-      if (addr != 0)
0d202d
-	TYPE_TARGET_TYPE (base_type) =
0d202d
-	    resolve_dynamic_type (TYPE_TARGET_TYPE (base_type), NULL, addr);
0d202d
 
0d202d
       /* We may be pointing to something embedded in a larger object.
0d202d
          Get the real type of the enclosing object.  */
0d202d
       enc_type = check_typedef (value_enclosing_type (arg1));
0d202d
       enc_type = TYPE_TARGET_TYPE (enc_type);
0d202d
 
0d202d
+      CORE_ADDR base_addr;
0d202d
       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
0d202d
 	  || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
0d202d
-	/* For functions, go through find_function_addr, which knows
0d202d
-	   how to handle function descriptors.  */
0d202d
-	arg2 = value_at_lazy (enc_type, 
0d202d
-			      find_function_addr (arg1, NULL));
0d202d
+	{
0d202d
+	  /* For functions, go through find_function_addr, which knows
0d202d
+	     how to handle function descriptors.  */
0d202d
+	  base_addr = find_function_addr (arg1, NULL);
0d202d
+	}
0d202d
       else
0d202d
-	/* Retrieve the enclosing object pointed to.  */
0d202d
-	arg2 = value_at_lazy (enc_type, 
0d202d
-			      (addr - value_pointed_to_offset (arg1)));
0d202d
+	{
0d202d
+	  /* Retrieve the enclosing object pointed to.  */
0d202d
+	  base_addr = (value_as_address (arg1)
0d202d
+		       - value_pointed_to_offset (arg1));
0d202d
+	}
0d202d
 
0d202d
+      arg2 = value_at_lazy (enc_type, base_addr);
0d202d
       enc_type = value_type (arg2);
0d202d
-      return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
0d202d
+      return readjust_indirect_value_type (arg2, enc_type, base_type,
0d202d
+					   arg1, base_addr);
0d202d
     }
0d202d
 
0d202d
   error (_("Attempt to take contents of a non-pointer value."));
0d202d
-  return 0;			/* For lint -- never reached.  */
0d202d
 }
0d202d
 
0d202d
 /* Create a value for an array by allocating space in GDB, copying the
0d202d
diff --git a/gdb/value.c b/gdb/value.c
0d202d
--- a/gdb/value.c
0d202d
+++ b/gdb/value.c
0d202d
@@ -3611,10 +3611,19 @@ coerce_ref_if_computed (const struct value *arg)
0d202d
 struct value *
0d202d
 readjust_indirect_value_type (struct value *value, struct type *enc_type,
0d202d
 			      const struct type *original_type,
0d202d
-			      const struct value *original_value)
0d202d
+			      struct value *original_value,
0d202d
+			      CORE_ADDR original_value_address)
0d202d
 {
0d202d
+  gdb_assert (TYPE_CODE (original_type) == TYPE_CODE_PTR
0d202d
+	      || TYPE_IS_REFERENCE (original_type));
0d202d
+
0d202d
+  struct type *original_target_type = TYPE_TARGET_TYPE (original_type);
0d202d
+  struct type *resolved_original_target_type
0d202d
+    = resolve_dynamic_type (original_target_type, NULL,
0d202d
+			    original_value_address);
0d202d
+
0d202d
   /* Re-adjust type.  */
0d202d
-  deprecated_set_value_type (value, TYPE_TARGET_TYPE (original_type));
0d202d
+  deprecated_set_value_type (value, resolved_original_target_type);
0d202d
 
0d202d
   /* Add embedding info.  */
0d202d
   set_value_enclosing_type (value, enc_type);
0d202d
@@ -3641,12 +3650,11 @@ coerce_ref (struct value *arg)
0d202d
   enc_type = check_typedef (value_enclosing_type (arg));
0d202d
   enc_type = TYPE_TARGET_TYPE (enc_type);
0d202d
 
0d202d
-  retval = value_at_lazy (enc_type,
0d202d
-                          unpack_pointer (value_type (arg),
0d202d
-                                          value_contents (arg)));
0d202d
+  CORE_ADDR addr = unpack_pointer (value_type (arg), value_contents (arg));
0d202d
+  retval = value_at_lazy (enc_type, addr);
0d202d
   enc_type = value_type (retval);
0d202d
-  return readjust_indirect_value_type (retval, enc_type,
0d202d
-                                       value_type_arg_tmp, arg);
0d202d
+  return readjust_indirect_value_type (retval, enc_type, value_type_arg_tmp,
0d202d
+				       arg, addr);
0d202d
 }
0d202d
 
0d202d
 struct value *
0d202d
diff --git a/gdb/value.h b/gdb/value.h
0d202d
--- a/gdb/value.h
0d202d
+++ b/gdb/value.h
0d202d
@@ -487,7 +487,9 @@ extern struct value *coerce_ref_if_computed (const struct value *arg);
0d202d
 
0d202d
 /* Setup a new value type and enclosing value type for dereferenced value VALUE.
0d202d
    ENC_TYPE is the new enclosing type that should be set.  ORIGINAL_TYPE and
0d202d
-   ORIGINAL_VAL are the type and value of the original reference or pointer.
0d202d
+   ORIGINAL_VAL are the type and value of the original reference or
0d202d
+   pointer.  ORIGINAL_VALUE_ADDRESS is the address within VALUE, that is
0d202d
+   the address that was dereferenced.
0d202d
 
0d202d
    Note, that VALUE is modified by this function.
0d202d
 
0d202d
@@ -496,7 +498,8 @@ extern struct value *coerce_ref_if_computed (const struct value *arg);
0d202d
 extern struct value * readjust_indirect_value_type (struct value *value,
0d202d
 						    struct type *enc_type,
0d202d
 						    const struct type *original_type,
0d202d
-						    const struct value *original_val);
0d202d
+						    struct value *original_val,
0d202d
+						    CORE_ADDR original_value_address);
0d202d
 
0d202d
 /* Convert a REF to the object referenced.  */
0d202d