implref-const.exp revision 1.1.1.6 1 1.1.1.6 christos # Copyright 2016-2024 Free Software Foundation, Inc.
2 1.1 christos
3 1.1 christos # This program is free software; you can redistribute it and/or modify
4 1.1 christos # it under the terms of the GNU General Public License as published by
5 1.1 christos # the Free Software Foundation; either version 3 of the License, or
6 1.1 christos # (at your option) any later version.
7 1.1 christos #
8 1.1 christos # This program is distributed in the hope that it will be useful,
9 1.1 christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 1.1 christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 1.1 christos # GNU General Public License for more details.
12 1.1 christos #
13 1.1 christos # You should have received a copy of the GNU General Public License
14 1.1 christos # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 1.1 christos
16 1.1 christos # Test a C++ reference marked with DW_OP_GNU_implicit_pointer.
17 1.1 christos # The referenced value is a DW_AT_const_value.
18 1.1 christos
19 1.1.1.6 christos require allow_cplus_tests
20 1.1 christos
21 1.1 christos load_lib dwarf.exp
22 1.1 christos
23 1.1 christos # This test can only be run on targets which support DWARF-2 and use gas.
24 1.1.1.6 christos require dwarf2_support
25 1.1 christos
26 1.1 christos # We'll place the output of Dwarf::assemble in implref-const.S.
27 1.1 christos standard_testfile main.c .S
28 1.1 christos
29 1.1 christos # ${testfile} is now "implref-const". srcfile2 is "implref-const.S".
30 1.1 christos set executable ${testfile}
31 1.1 christos set asm_file [standard_output_file ${srcfile2}]
32 1.1 christos
33 1.1 christos # We need to know the size of integer and address types in order
34 1.1 christos # to write some of the debugging info we'd like to generate.
35 1.1 christos #
36 1.1 christos # For that, we ask GDB by debugging our implref-const program.
37 1.1 christos # Any program would do, but since we already have implref-const
38 1.1 christos # specifically for this testcase, might as well use that.
39 1.1.1.2 christos if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] {
40 1.1 christos return -1
41 1.1 christos }
42 1.1 christos
43 1.1 christos # Create the DWARF. We need a regular variable and a reference to it that'll
44 1.1 christos # be marked with DW_OP_GNU_implicit_pointer.
45 1.1 christos Dwarf::assemble ${asm_file} {
46 1.1 christos cu {} {
47 1.1 christos DW_TAG_compile_unit {
48 1.1 christos {DW_AT_language @DW_LANG_C_plus_plus}
49 1.1 christos } {
50 1.1 christos declare_labels int_label const_label variable_label ref_label
51 1.1 christos set int_size [get_sizeof "int" -1]
52 1.1 christos
53 1.1 christos # gdb always assumes references are implemented as pointers.
54 1.1 christos set addr_size [get_sizeof "void *" -1]
55 1.1 christos set var_value 42
56 1.1 christos
57 1.1 christos int_label: DW_TAG_base_type {
58 1.1 christos {DW_AT_byte_size ${int_size} DW_FORM_udata}
59 1.1 christos {DW_AT_encoding @DW_ATE_signed}
60 1.1 christos {DW_AT_name "int"}
61 1.1 christos }
62 1.1 christos
63 1.1 christos ref_label: DW_TAG_reference_type {
64 1.1 christos {DW_AT_byte_size ${addr_size} DW_FORM_udata}
65 1.1 christos {DW_AT_type :${int_label}}
66 1.1 christos }
67 1.1 christos
68 1.1 christos const_label: DW_TAG_const_type {
69 1.1 christos {DW_AT_type :${ref_label}}
70 1.1 christos }
71 1.1 christos
72 1.1 christos DW_TAG_subprogram {
73 1.1.1.4 christos {MACRO_AT_func { "main" }}
74 1.1 christos {DW_AT_type :${int_label}}
75 1.1 christos {DW_AT_external 1 DW_FORM_flag}
76 1.1 christos } {
77 1.1 christos variable_label: DW_TAG_variable {
78 1.1 christos {DW_AT_name "var"}
79 1.1 christos {DW_AT_type :${int_label}}
80 1.1 christos {DW_AT_const_value ${var_value} DW_FORM_udata}
81 1.1 christos }
82 1.1 christos
83 1.1 christos DW_TAG_variable {
84 1.1 christos {DW_AT_name "ref"}
85 1.1 christos {DW_AT_type :${const_label}}
86 1.1 christos {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr}
87 1.1 christos }
88 1.1 christos }
89 1.1 christos }
90 1.1 christos }
91 1.1 christos }
92 1.1 christos
93 1.1.1.2 christos if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] {
94 1.1 christos return -1
95 1.1 christos }
96 1.1 christos
97 1.1 christos # DW_OP_GNU_implicit_pointer implementation requires a valid frame.
98 1.1 christos if ![runto_main] {
99 1.1 christos return -1
100 1.1 christos }
101 1.1 christos
102 1.1 christos # Doing 'print ref' should show us e.g. '(int &) <synthetic pointer>: 42'.
103 1.1 christos gdb_test "print ref" " = \\(int &\\) <synthetic pointer>: \\\d+"
104 1.1 christos
105 1.1 christos # The variable isn't located in memory, thus we can't take its address.
106 1.1 christos gdb_test "print &var" "Can't take address of \"var\" which isn't an lvalue."
107 1.1 christos gdb_test "print &ref" "Attempt to take address of value not located in memory."
108 1.1 christos
109 1.1 christos # gdb assumes C++ references are implemented as pointers, and print &(&ref)
110 1.1 christos # shows us the underlying pointer's address.
111 1.1 christos # Since in this case there's no physical pointer, gdb should tell us so.
112 1.1 christos gdb_test "print &(&ref)" "Attempt to take address of value not located in memory."
113