aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite
diff options
context:
space:
mode:
authorMartin Galvan <martin.galvan@tallertechnologies.com>2016-05-31 15:54:01 -0300
committerMartin Galvan <martin.galvan@tallertechnologies.com>2016-05-31 15:56:34 -0300
commit3326303bf5ae4c92f2fbbff387ce231a16c1c8bf (patch)
tree6003faaf0747941817afc8d62f9f2a0bffcd3802 /gdb/testsuite
parentf7433f011ef27838551aded73b8666a86d26b8ec (diff)
downloadgdb-3326303bf5ae4c92f2fbbff387ce231a16c1c8bf.zip
gdb-3326303bf5ae4c92f2fbbff387ce231a16c1c8bf.tar.gz
gdb-3326303bf5ae4c92f2fbbff387ce231a16c1c8bf.tar.bz2
[PR gdb/19893] Fix handling of synthetic C++ references
https://sourceware.org/bugzilla/show_bug.cgi?id=19893 I've traced the main source of the problem to pieced_value_funcs.coerce_ref not being implemented. Since gdb always assumes references are implemented as pointers, this causes it to think that it's dealing with a NULL pointer, thus breaking any operations involving synthetic references. What I did here was implementing pieced_value_funcs.coerce_ref using some of the synthetic pointer handling code from indirect_pieced_value, as Pedro suggested. I also made a few adjustments to the reference printing code so that it correctly shows either the address of the referenced value or (if it's non-addressable) the "<synthetic pointer>" string. I also wrote some unit tests based on Dwarf::assemble; these took a while to make because in most cases I needed a synthetic reference to a physical variable. Additionally, I started working on a unit test for classes that have a vtable, but ran into a few issues so that'll probably go in a future patch. One thing that should definitely be fixed is that proc function_range (called for MACRO_AT_func) will always try to compile/link using gcc with the default options instead of g++, thus breaking C++ compilations that require e.g. libstdc++. gdb/ChangeLog: * dwarf2loc.c (coerce_pieced_ref, indirect_synthetic_pointer, fetch_const_value_from_synthetic_pointer): New functions. (indirect_pieced_value): Move lower half to indirect_synthetic_pointer. (pieced_value_funcs): Implement coerce_ref. * valops.c (value_addr): Call coerce_ref for synthetic references. * valprint.c (valprint_check_validity): Return true for synthetic references. Also, don't show "<synthetic pointer>" if they reference addressable values. (generic_val_print_ref): Handle synthetic references. Also move some code to print_ref_address. (print_ref_address, get_value_addr_contents): New functions. gdb/testsuite/ChangeLog: * gdb.dwarf2/implref.exp: Rename to... * gdb.dwarf2/implref-const.exp: ...this. Also add more test statements. * gdb.dwarf2/implref-array.c: New file. * gdb.dwarf2/implref-array.exp: Likewise. * gdb.dwarf2/implref-global.c: Likewise. * gdb.dwarf2/implref-global.exp: Likewise. * gdb.dwarf2/implref-struct.c: Likewise. * gdb.dwarf2/implref-struct.exp: Likewise.
Diffstat (limited to 'gdb/testsuite')
-rw-r--r--gdb/testsuite/ChangeLog12
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-array.c27
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-array.exp171
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-const.exp (renamed from gdb/testsuite/gdb.dwarf2/implref.exp)53
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-global.c27
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-global.exp125
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-struct.c43
-rw-r--r--gdb/testsuite/gdb.dwarf2/implref-struct.exp186
8 files changed, 628 insertions, 16 deletions
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index b91ddce..da4dd56 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,15 @@
+2016-05-31 Martin Galvan <martin.galvan@tallertechnologies.com>
+
+ PR c++/19893
+ * gdb.dwarf2/implref.exp: Rename to...
+ * gdb.dwarf2/implref-const.exp: ...this. Also add more test statements.
+ * gdb.dwarf2/implref-array.c: New file.
+ * gdb.dwarf2/implref-array.exp: Likewise.
+ * gdb.dwarf2/implref-global.c: Likewise.
+ * gdb.dwarf2/implref-global.exp: Likewise.
+ * gdb.dwarf2/implref-struct.c: Likewise.
+ * gdb.dwarf2/implref-struct.exp: Likewise.
+
2016-05-30 Antoine Tremblay <antoine.tremblay@ericsson.com>
* gdb.trace/trace-condition.exp: Add 64bit tests.
diff --git a/gdb/testsuite/gdb.dwarf2/implref-array.c b/gdb/testsuite/gdb.dwarf2/implref-array.c
new file mode 100644
index 0000000..2470412
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-array.c
@@ -0,0 +1,27 @@
+/* Copyright (C) 2016 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Test program for synthetic C++ references to arrays. */
+
+int array[5] = {0, 1, 2, 3, 4};
+
+int
+main (void)
+{
+ asm ("main_label: .globl main_label");
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/implref-array.exp b/gdb/testsuite/gdb.dwarf2/implref-array.exp
new file mode 100644
index 0000000..b612763
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-array.exp
@@ -0,0 +1,171 @@
+# Copyright 2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
+# The referenced value is a global array whose location is a DW_OP_addr.
+
+if [skip_cplus_tests] {
+ continue
+}
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if ![dwarf2_support] {
+ return 0
+}
+
+# We'll place the output of Dwarf::assemble in implref-array.S.
+standard_testfile .c .S
+
+# ${testfile} is now "implref-array". srcfile2 is "implref-array.S".
+set executable ${testfile}
+set asm_file [standard_output_file ${srcfile2}]
+
+# We need to know the size of integer and address types in order
+# to write some of the debugging info we'd like to generate.
+#
+# For that, we ask GDB by debugging our implref-array program.
+# Any program would do, but since we already have implref-array
+# specifically for this testcase, might as well use that.
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+ untested ${testfile}.exp
+ return -1
+}
+
+set array_length [get_valueof "/u" "sizeof(array) / sizeof(array\[0\])" -1]
+
+# Create the DWARF. We need a regular variable which represents the array, and
+# a reference to it that'll be marked with DW_OP_GNU_implicit_pointer.
+# The variable must be global so that its name is an exported symbol that we
+# can reference from the DWARF using gdb_target_symbol.
+Dwarf::assemble ${asm_file} {
+ global srcdir subdir srcfile array_length
+
+ cu {} {
+ DW_TAG_compile_unit {
+ {DW_AT_language @DW_LANG_C_plus_plus}
+ } {
+ declare_labels int_label sizetype_label array_label variable_label ref_label
+ set int_size [get_sizeof "int" -1]
+ set upper_bound [expr ${array_length} - 1]
+
+ # gdb always assumes references are implemented as pointers.
+ set addr_size [get_sizeof "void *" -1]
+
+ int_label: DW_TAG_base_type {
+ {DW_AT_byte_size ${int_size} DW_FORM_udata}
+ {DW_AT_encoding @DW_ATE_signed}
+ {DW_AT_name "int"}
+ }
+
+ sizetype_label: DW_TAG_base_type {
+ {DW_AT_byte_size ${int_size} DW_FORM_udata}
+ {DW_AT_encoding @DW_ATE_unsigned}
+ {DW_AT_name "sizetype"}
+ }
+
+ array_label: DW_TAG_array_type {
+ {DW_AT_type :${int_label}}
+ } {
+ DW_TAG_subrange_type {
+ {DW_AT_type :${sizetype_label}}
+ {DW_AT_lower_bound 0 DW_FORM_udata}
+ {DW_AT_upper_bound ${upper_bound} DW_FORM_udata}
+ }
+ }
+
+ ref_label: DW_TAG_reference_type {
+ {DW_AT_byte_size ${addr_size} DW_FORM_udata}
+ {DW_AT_type :${array_label}}
+ }
+
+ variable_label: DW_TAG_variable {
+ {DW_AT_name "array"}
+ {DW_AT_type :${array_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ {DW_AT_location {DW_OP_addr [gdb_target_symbol "array"]} SPECIAL_expr}
+ }
+
+ DW_TAG_subprogram {
+ {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }}
+ {DW_AT_type :${int_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ } {
+ DW_TAG_variable {
+ {DW_AT_name "ref"}
+ {DW_AT_type :${ref_label}}
+ {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
+ }
+ }
+ }
+ }
+}
+
+if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] {
+ return -1
+}
+
+# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
+if ![runto_main] {
+ return -1
+}
+
+# This matches e.g. '(int (&)[5])'
+set ref_type [format {\(int \(&\)\[%d\]\)} ${array_length}]
+
+# This matches e.g. '(int (*)[5])'
+set ptr_type [format {\(int \(\*\)\[%d\]\)} ${array_length}]
+
+# Contents of the array. Trim leading/trailing whitespace, '{' and '}'
+# since they confuse TCL to no end.
+set contents [get_valueof "" "array" ""]
+set contents [string trim ${contents}]
+set contents [string trim ${contents} "{}"]
+
+# Address of the referenced value.
+set address [get_hexadecimal_valueof "&array" ""]
+
+# Doing 'print ref' should show us e.g. '(int (&)[5]) 0xdeadbeef: {0, 1, 2, 3, 4}'.
+gdb_test "print ref" " = ${ref_type} @${address}: \\{${contents}\\}"
+
+# Doing 'print &ref' should show us e.g. '(int (*)[5]) 0xdeadbeef <array>'.
+gdb_test "print &ref" " = ${ptr_type} ${address} <array>"
+
+# gdb assumes C++ references are implemented as pointers, and print &(&ref)
+# shows us the underlying pointer's address. Since in this case there's no
+# physical pointer, gdb should tell us so.
+gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
+
+# Test assignment through the synthetic reference.
+set first_value 10
+gdb_test_no_output "set (ref\[0\] = ${first_value})"
+
+# This matches '{10, 1, 2, 3, 4}'.
+set new_contents [format {\{%d, 1, 2, 3, 4\}} ${first_value}]
+
+# Doing 'print ref' should now show us e.g.
+# '(int (&)[5]) <synthetic pointer>: {10, 1, 2, 3, 4}'.
+gdb_test "print ref" " = ${ref_type} @${address}: ${new_contents}" "print ref after assignment"
+gdb_test "print array" " = ${new_contents}" "print array after assignment"
+
+# Test treating the array as a pointer.
+set second_value 20
+set new_contents [format {\{%d, %d, 2, 3, 4\}} ${first_value} ${second_value}]
+
+gdb_test "print *ref" " = ${first_value}"
+gdb_test_no_output "set (*(ref + 1) = ${second_value})"
+gdb_test "print ref\[1\]" " = ${second_value}"
+gdb_test "print array" " = ${new_contents}" "print array after second assignment"
diff --git a/gdb/testsuite/gdb.dwarf2/implref.exp b/gdb/testsuite/gdb.dwarf2/implref-const.exp
index ca4766e..9b6d5fe 100644
--- a/gdb/testsuite/gdb.dwarf2/implref.exp
+++ b/gdb/testsuite/gdb.dwarf2/implref-const.exp
@@ -13,45 +13,62 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Test C++ references marked with DW_OP_GNU_implicit_pointer.
+# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
+# The referenced value is a DW_AT_const_value.
-# TODO: Add more test statements after fixing bug #19893:
-# https://sourceware.org/bugzilla/show_bug.cgi?id=19893.
+if [skip_cplus_tests] {
+ continue
+}
load_lib dwarf.exp
# This test can only be run on targets which support DWARF-2 and use gas.
-if {![dwarf2_support]} {
+if ![dwarf2_support] {
return 0
}
-# We'll place the output of Dwarf::assemble in implref.S.
+# We'll place the output of Dwarf::assemble in implref-const.S.
standard_testfile main.c .S
-# ${testfile} is now "implref". srcfile2 is "implref.S".
+# ${testfile} is now "implref-const". srcfile2 is "implref-const.S".
set executable ${testfile}
set asm_file [standard_output_file ${srcfile2}]
+# We need to know the size of integer and address types in order
+# to write some of the debugging info we'd like to generate.
+#
+# For that, we ask GDB by debugging our implref-const program.
+# Any program would do, but since we already have implref-const
+# specifically for this testcase, might as well use that.
+if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] {
+ untested ${testfile}.exp
+ return -1
+}
+
# Create the DWARF. We need a regular variable and a reference to it that'll
# be marked with DW_OP_GNU_implicit_pointer.
-Dwarf::assemble $asm_file {
+Dwarf::assemble ${asm_file} {
global srcdir subdir srcfile
- cu { version 3 addr_size 4 } {
+ cu {} {
DW_TAG_compile_unit {
- {DW_AT_producer "GNU C++ 4.8.4"}
{DW_AT_language @DW_LANG_C_plus_plus}
} {
declare_labels int_label const_label variable_label ref_label
+ set int_size [get_sizeof "int" -1]
+
+ # gdb always assumes references are implemented as pointers.
+ set addr_size [get_sizeof "void *" -1]
+ set var_value 42
int_label: DW_TAG_base_type {
- {DW_AT_byte_size 4 DW_FORM_udata}
+ {DW_AT_byte_size ${int_size} DW_FORM_udata}
{DW_AT_encoding @DW_ATE_signed}
{DW_AT_name "int"}
}
ref_label: DW_TAG_reference_type {
- {DW_AT_byte_size 4 DW_FORM_udata}
+ {DW_AT_byte_size ${addr_size} DW_FORM_udata}
{DW_AT_type :${int_label}}
}
@@ -67,7 +84,7 @@ Dwarf::assemble $asm_file {
variable_label: DW_TAG_variable {
{DW_AT_name "var"}
{DW_AT_type :${int_label}}
- {DW_AT_const_value 42 DW_FORM_udata}
+ {DW_AT_const_value ${var_value} DW_FORM_udata}
}
DW_TAG_variable {
@@ -80,7 +97,7 @@ Dwarf::assemble $asm_file {
}
}
-if [prepare_for_testing ${testfile}.exp ${executable} "${asm_file} ${srcfile}" {}] {
+if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] {
return -1
}
@@ -89,10 +106,14 @@ if ![runto_main] {
return -1
}
-gdb_test "print ref" " = \\(int &\\) <synthetic pointer>" "print ref"
-gdb_test "print &ref" " = \\(int \\*\\) <synthetic pointer>" "print &ref"
+# Doing 'print ref' should show us e.g. '(int &) <synthetic pointer>: 42'.
+gdb_test "print ref" " = \\(int &\\) <synthetic pointer>: \\\d+"
+
+# The variable isn't located in memory, thus we can't take its address.
+gdb_test "print &var" "Can't take address of \"var\" which isn't an lvalue."
+gdb_test "print &ref" "Attempt to take address of value not located in memory."
# gdb assumes C++ references are implemented as pointers, and print &(&ref)
# shows us the underlying pointer's address.
# Since in this case there's no physical pointer, gdb should tell us so.
-gdb_test "print &\(&ref\)" "Attempt to take address of value not located in memory." "print &(&ref)"
+gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
diff --git a/gdb/testsuite/gdb.dwarf2/implref-global.c b/gdb/testsuite/gdb.dwarf2/implref-global.c
new file mode 100644
index 0000000..7814a30
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-global.c
@@ -0,0 +1,27 @@
+/* Copyright (C) 2016 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Test program for synthetic C++ references to global variables. */
+
+int global_var = 42;
+
+int
+main (void)
+{
+ asm ("main_label: .globl main_label");
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/implref-global.exp b/gdb/testsuite/gdb.dwarf2/implref-global.exp
new file mode 100644
index 0000000..1f4cc05
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-global.exp
@@ -0,0 +1,125 @@
+# Copyright 2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
+# The referenced value is a global variable whose location is a DW_OP_addr.
+
+if [skip_cplus_tests] {
+ continue
+}
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if ![dwarf2_support] {
+ return 0
+}
+
+# We'll place the output of Dwarf::assemble in implref-global.S.
+standard_testfile .c .S
+
+# ${testfile} is now "implref-global". srcfile2 is "implref-global.S".
+set executable ${testfile}
+set asm_file [standard_output_file ${srcfile2}]
+
+# We need to know the size of integer and address types in order
+# to write some of the debugging info we'd like to generate.
+#
+# For that, we ask GDB by debugging our implref-global program.
+# Any program would do, but since we already have implref-global
+# specifically for this testcase, might as well use that.
+if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] {
+ untested ${testfile}.exp
+ return -1
+}
+
+# Create the DWARF. We need a regular variable and a reference to it that'll
+# be marked with DW_OP_GNU_implicit_pointer. The variable must be global so
+# that its name is an exported symbol that we can reference from the DWARF
+# using gdb_target_symbol.
+Dwarf::assemble ${asm_file} {
+ global srcdir subdir srcfile
+
+ cu {} {
+ DW_TAG_compile_unit {
+ {DW_AT_language @DW_LANG_C_plus_plus}
+ } {
+ declare_labels int_label variable_label ref_label
+ set int_size [get_sizeof "int" -1]
+
+ # gdb always assumes references are implemented as pointers.
+ set addr_size [get_sizeof "void *" -1]
+
+ int_label: DW_TAG_base_type {
+ {DW_AT_byte_size ${int_size} DW_FORM_udata}
+ {DW_AT_encoding @DW_ATE_signed}
+ {DW_AT_name "int"}
+ }
+
+ ref_label: DW_TAG_reference_type {
+ {DW_AT_byte_size ${addr_size} DW_FORM_udata}
+ {DW_AT_type :${int_label}}
+ }
+
+ variable_label: DW_TAG_variable {
+ {DW_AT_name "global_var"}
+ {DW_AT_type :${int_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ {DW_AT_location {DW_OP_addr [gdb_target_symbol "global_var"]} SPECIAL_expr}
+ }
+
+ DW_TAG_subprogram {
+ {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }}
+ {DW_AT_type :${int_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ } {
+ DW_TAG_variable {
+ {DW_AT_name "ref"}
+ {DW_AT_type :${ref_label}}
+ {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
+ }
+ }
+ }
+ }
+}
+
+if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] {
+ return -1
+}
+
+# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
+if ![runto_main] {
+ return -1
+}
+
+# Address of the referenced value.
+set address [get_hexadecimal_valueof "&global_var" ""]
+
+# Doing 'print ref' should show us e.g. '(int &) @0xdeadbeef: 42'.
+gdb_test "print ref" " = \\(int &\\) @${address}: \\\d+"
+
+# Doing 'print &ref' should show us e.g. '(int *) 0xdeadbeef <global_var>'.
+gdb_test "print &ref" " = \\(int \\*\\) ${address} <global_var>"
+
+# gdb assumes C++ references are implemented as pointers, and print &(&ref)
+# shows us the underlying pointer's address. Since in this case there's no
+# physical pointer, gdb should tell us so.
+gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
+
+# Test assignment through the synthetic reference.
+set new_value 10
+gdb_test_no_output "set (ref = ${new_value})"
+gdb_test "print ref" " = \\(int &\\) @${address}: ${new_value}" "print ref after assignment"
+gdb_test "print global_var" " = ${new_value}" "print global_var after assignment"
diff --git a/gdb/testsuite/gdb.dwarf2/implref-struct.c b/gdb/testsuite/gdb.dwarf2/implref-struct.c
new file mode 100644
index 0000000..cc4f730
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-struct.c
@@ -0,0 +1,43 @@
+/* Copyright (C) 2016 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Test program for synthetic C++ references to structs. */
+
+struct S {
+ int a;
+ int b;
+ int c;
+};
+
+struct S s1 = {
+ 0,
+ 1,
+ 2
+};
+
+struct S s2 = {
+ 10,
+ 11,
+ 12
+};
+
+int
+main (void)
+{
+ asm ("main_label: .globl main_label");
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/implref-struct.exp b/gdb/testsuite/gdb.dwarf2/implref-struct.exp
new file mode 100644
index 0000000..49967e3
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/implref-struct.exp
@@ -0,0 +1,186 @@
+# Copyright 2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
+# The referenced value is a global struct whose location is a DW_OP_addr.
+
+if [skip_cplus_tests] {
+ continue
+}
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if ![dwarf2_support] {
+ return 0
+}
+
+# We'll place the output of Dwarf::assemble in implref-struct.S.
+standard_testfile .c .S
+
+# ${testfile} is now "implref-struct". srcfile2 is "implref-struct.S".
+set executable ${testfile}
+set asm_file [standard_output_file ${srcfile2}]
+
+# We need to know the size of integer and address types in order
+# to write some of the debugging info we'd like to generate.
+#
+# For that, we ask GDB by debugging our implref-struct program.
+# Any program would do, but since we already have implref-struct
+# specifically for this testcase, might as well use that.
+if [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} {debug c++}] {
+ untested ${testfile}.exp
+ return -1
+}
+
+# Create the DWARF. We need a regular variable for the struct and a reference
+# to it that'll be marked with DW_OP_GNU_implicit_pointer. The variable must be
+# global so that its name is an exported symbol that can we can reference from
+# the DWARF using gdb_target_symbol.
+Dwarf::assemble ${asm_file} {
+ global srcdir subdir srcfile
+
+ cu {} {
+ DW_TAG_compile_unit {
+ {DW_AT_language @DW_LANG_C_plus_plus}
+ } {
+ declare_labels int_label struct_label variable_label ref_label
+ set int_size [get_sizeof "int" -1]
+
+ # gdb always assumes references are implemented as pointers.
+ set addr_size [get_sizeof "void *" -1]
+ set S_size [get_sizeof "S" -1]
+
+ # The compiler shouldn't introduce structure padding here.
+ set b_offset 4
+ set c_offset 8
+
+ int_label: DW_TAG_base_type {
+ {DW_AT_byte_size ${int_size} DW_FORM_udata}
+ {DW_AT_encoding @DW_ATE_signed}
+ {DW_AT_name "int"}
+ }
+
+ struct_label: DW_TAG_structure_type {
+ {DW_AT_name "S"}
+ {DW_AT_byte_size ${S_size} DW_FORM_udata}
+ } {
+ DW_TAG_member {
+ {DW_AT_name "a"}
+ {DW_AT_type :${int_label}}
+ {DW_AT_data_member_location 0 DW_FORM_udata}
+ }
+
+ DW_TAG_member {
+ {DW_AT_name "b"}
+ {DW_AT_type :${int_label}}
+ {DW_AT_data_member_location ${b_offset} DW_FORM_udata}
+ }
+
+ DW_TAG_member {
+ {DW_AT_name "c"}
+ {DW_AT_type :${int_label}}
+ {DW_AT_data_member_location ${c_offset} DW_FORM_udata}
+ }
+ }
+
+ ref_label: DW_TAG_reference_type {
+ {DW_AT_byte_size ${addr_size} DW_FORM_udata}
+ {DW_AT_type :${struct_label}}
+ }
+
+ variable_label: DW_TAG_variable {
+ {DW_AT_name "s1"}
+ {DW_AT_type :${struct_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ {DW_AT_location {DW_OP_addr [gdb_target_symbol "s1"]} SPECIAL_expr}
+ }
+
+ DW_TAG_variable {
+ {DW_AT_name "s2"}
+ {DW_AT_type :${struct_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ {DW_AT_location {DW_OP_addr [gdb_target_symbol "s2"]} SPECIAL_expr}
+ }
+
+ DW_TAG_subprogram {
+ {MACRO_AT_func { "main" "${srcdir}/${subdir}/${srcfile}" }}
+ {DW_AT_type :${int_label}}
+ {DW_AT_external 1 DW_FORM_flag}
+ } {
+ DW_TAG_variable {
+ {DW_AT_name "ref"}
+ {DW_AT_type :${ref_label}}
+ {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
+ }
+ }
+ }
+ }
+}
+
+if [prepare_for_testing ${testfile}.exp ${executable} [list ${asm_file} ${srcfile}] {}] {
+ return -1
+}
+
+# DW_OP_GNU_implicit_pointer implementation requires a valid frame.
+if ![runto_main] {
+ return -1
+}
+
+# Returns the struct members, e.g. '{a = 0, b = 1, c = 2}'.
+proc get_members {var} {
+ set members [get_valueof "" ${var} ""]
+
+ # Trim leading/trailing whitespace, '{' and '}' since they confuse TCL to no end.
+ set members [string trim ${members}]
+ set members [string trim ${members} "{}"]
+
+ return ${members}
+}
+
+# Values of the struct members.
+set s1_members [get_members "s1"]
+set s2_members [get_members "s2"]
+
+# Address of the referenced value.
+set address [get_hexadecimal_valueof "&s1" ""]
+
+# Test printing with both 'set print object off' and 'on', just to make sure
+# the output doesn't change.
+foreach_with_prefix print-object {"off" "on"} {
+ gdb_test_no_output "set print object ${print-object}"
+
+ # Doing 'print ref' should show us e.g.
+ # '(S &) @0xdeadbeef: {a = 0, b = 1, c = 2}'.
+ gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s1_members}\\}"
+
+ # Doing 'print &ref' should show us e.g. '(S *) 0xdeadbeef <s1>'.
+ gdb_test "print &ref" " = \\(S \\*\\) ${address} <s1>"
+
+ # gdb assumes C++ references are implemented as pointers, and
+ # print &(&ref) shows us the underlying pointer's address.
+ # Since in this case there's no physical pointer, gdb should tell us so.
+ gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
+}
+
+# Test assignment through the synthetic reference.
+gdb_test_no_output "set (ref = s2)"
+
+foreach_with_prefix print-object {"off" "on"} {
+ gdb_test_no_output "set print object ${print-object}"
+
+ gdb_test "print ref" " = \\(S &\\) @${address}: \\{${s2_members}\\}" "print ref after assignment"
+ gdb_test "print s1" " = \\{${s2_members}\\}" "print s1 after assignment"
+}