1# Copyright 2016-2020 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Test accessing "non-variable" variables, i.e., variables which are
17# optimized to a constant DWARF location expression and/or
18# partially/fully optimized out.
19
20load_lib dwarf.exp
21
22if {![dwarf2_support]} { return 0 }
23
24standard_testfile main.c nonvar-access-dw.S
25
26# Make some DWARF for the test.
27set asm_file [standard_output_file $srcfile2]
28
29Dwarf::assemble $asm_file {
30    cu {} {
31	compile_unit {
32	    {DW_AT_name main.c}
33	} {
34	    declare_labels int_type_label char_type_label \
35		struct_s_label struct_t_label array_a9_label \
36		char_ptr_label implicit_a_label stack_b_label
37
38	    int_type_label: base_type {
39		{name "int"}
40		{encoding @DW_ATE_signed}
41		{byte_size 4 DW_FORM_sdata}
42	    }
43	    char_type_label: base_type {
44		{name "char"}
45		{encoding @DW_ATE_unsigned}
46		{byte_size 1 DW_FORM_sdata}
47	    }
48	    char_ptr_label: pointer_type {
49		{type :$char_type_label}
50	    }
51
52	    struct_s_label: structure_type {
53		{name s}
54		{byte_size 4 DW_FORM_sdata}
55	    } {
56		member {
57		    {name a}
58		    {type :$int_type_label}
59		    {data_member_location 0 DW_FORM_udata}
60		    {bit_size 8 DW_FORM_udata}
61		}
62		member {
63		    {name b}
64		    {type :$int_type_label}
65		    {data_bit_offset 8 DW_FORM_udata}
66		    {bit_size 24 DW_FORM_udata}
67		}
68	    }
69
70	    struct_t_label: structure_type {
71		{name t}
72		{byte_size 4 DW_FORM_sdata}
73	    } {
74		member {
75		    {name a}
76		    {type :$int_type_label}
77		    {data_member_location 0 DW_FORM_udata}
78		    {bit_size 9 DW_FORM_udata}
79		}
80		member {
81		    {name b}
82		    {type :$int_type_label}
83		    {data_bit_offset 9 DW_FORM_udata}
84		    {bit_size 23 DW_FORM_udata}
85		}
86	    }
87
88	    array_a9_label: array_type {
89		{type :$char_type_label}
90	    } {
91		subrange_type {
92		    {type :$int_type_label}
93		    {upper_bound 8 DW_FORM_udata}
94		}
95	    }
96
97	    DW_TAG_subprogram {
98		{MACRO_AT_func {main}}
99		{DW_AT_external 1 flag}
100	    } {
101		# Simple variable without location.
102		DW_TAG_variable {
103		    {name undef_int}
104		    {type :$int_type_label}
105		}
106		# Struct variable without location.
107		DW_TAG_variable {
108		    {name undef_s}
109		    {type :$struct_s_label}
110		}
111		# Composite location: byte-aligned pieces.
112		DW_TAG_variable {
113		    {name def_s}
114		    {type :$struct_s_label}
115		    {location {
116			const1u 0
117			stack_value
118			bit_piece 8 0
119			const1s -1
120			stack_value
121			bit_piece 24 0
122		    } SPECIAL_expr}
123		}
124		# Composite location: non-byte-aligned pieces.
125		DW_TAG_variable {
126		    {name def_t}
127		    {type :$struct_t_label}
128		    {location {
129			const2s -184
130			stack_value
131			bit_piece 9 0
132			const4u 1752286
133			stack_value
134			bit_piece 23 0
135		    } SPECIAL_expr}
136		}
137		# Composite location with some empty pieces.
138		DW_TAG_variable {
139		    {name part_def_a}
140		    {type :$array_a9_label}
141		    {location {
142			piece 3
143			const4u 0xf1927314
144			stack_value
145			piece 4
146			piece 2
147		    } SPECIAL_expr}
148		}
149		# Implicit location: immediate value.
150		DW_TAG_variable {
151		    {name def_implicit_s}
152		    {type :$struct_s_label}
153		    {location {
154			implicit_value 0x12 0x34 0x56 0x78
155		    } SPECIAL_expr}
156		}
157		# Implicit location: immediate value for whole array, with
158		# excess bytes.
159		implicit_a_label: DW_TAG_variable {
160		    {name def_implicit_a}
161		    {type :$array_a9_label}
162		    {location {
163			implicit_value 0x1 0x12 0x23 0x34 0x45 \
164			    0x56 0x67 0x78 0x89 0x9a 0xab
165		    } SPECIAL_expr}
166		}
167		# Implicit pointer into immediate value.
168		DW_TAG_variable {
169		    {name implicit_a_ptr}
170		    {type :$char_ptr_label}
171		    {location {
172			implicit_pointer $implicit_a_label 5
173		    } SPECIAL_expr}
174		}
175		# Stack-value location.
176		stack_b_label: DW_TAG_variable {
177		    {name def_stack_b}
178		    {type :$struct_t_label}
179		    {location {
180			const4u 0x1a2b3c4d
181			stack_value
182		    } SPECIAL_expr}
183		}
184		# Implicit pointer into stack value.
185		DW_TAG_variable {
186		    {name implicit_b_ptr}
187		    {type :$char_ptr_label}
188		    {location {
189			implicit_pointer $stack_b_label 1
190		    } SPECIAL_expr}
191		}
192	    }
193	}
194    }
195}
196
197if { [prepare_for_testing "failed to prepare" ${testfile} \
198	  [list $srcfile $asm_file] {nodebug}] } {
199    return -1
200}
201
202if ![runto_main] {
203    return -1
204}
205
206# Determine byte order.
207set endian [get_endianness]
208
209# Byte-aligned objects with simple location descriptions.
210switch $endian { big {set val 0x345678} little {set val 0x785634} }
211gdb_test "print/x def_implicit_s" " = \\{a = 0x12, b = $val\\}"
212gdb_test "print/x def_implicit_s.b" " = $val"
213gdb_test "print/x def_implicit_a" \
214    " = \\{0x1, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89\\}"
215gdb_test "print/x def_implicit_a\[5\]" " = 0x56"
216gdb_test "print/x *(char (*)\[5\]) implicit_a_ptr" \
217    " = \\{0x56, 0x67, 0x78, 0x89, 0x9a\\}"
218switch $endian {
219    big {set val "a = 52, b = 2833485"}
220    little {set val "a = 77, b = 857502"}
221}
222gdb_test "print def_stack_b" " = \\{$val\\}"
223switch $endian {big {set val 0x2b} little {set val 0x3c}}
224gdb_test "print/x *implicit_b_ptr" " = $val"
225
226# Byte-aligned fields, pieced together from DWARF stack values.
227gdb_test "print def_s" " = \\{a = 0, b = -1\\}"
228switch $endian { big {set val 0x92} little {set val 0x73} }
229gdb_test "print/x part_def_a\[4\]" " = $val"
230gdb_test "print/x part_def_a\[8\]" " = <optimized out>"
231
232# Non-byte-aligned fields, pieced together from DWARF stack values.
233gdb_test "print def_t" " = \\{a = -184, b = 1752286\\}"
234
235# Simple variable without location.
236gdb_test "print undef_int" " = <optimized out>"
237
238# Member of a structure without location.
239gdb_test "print undef_s.a" " = <optimized out>"
240