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

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