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

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