1 # Copyright 2017-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test reading/writing variables with non-trivial DWARF locations. In
17 # particular the test uses register- and memory locations as well as
18 # composite locations with register- and memory pieces.
22 # This test can only be run on targets which support DWARF-2 and use gas.
23 if {![dwarf2_support]} {
27 # Choose suitable integer registers for the test.
29 set dwarf_regnum {0 1}
31 if { [is_aarch64_target] } {
33 } elseif { [is_aarch32_target]
34 || [istarget "s390*-*-*" ]
35 || [istarget "powerpc*-*-*"]
36 || [istarget "rs6000*-*-aix*"] } {
38 } elseif { [is_x86_like_target] } {
40 } elseif { [is_amd64_regs_target] } {
43 verbose "Skipping $gdb_test_file_name."
47 standard_testfile .c -dw.S
49 # Make some DWARF for the test.
51 set asm_file [standard_output_file $srcfile2]
52 Dwarf::assemble $asm_file {
53 global dwarf_regnum regname srcfile
55 set buf_var [gdb_target_symbol buf]
62 declare_labels char_type_label
63 declare_labels int_type_label short_type_label
64 declare_labels array_a8_label struct_s_label struct_t_label
65 declare_labels struct_st_label
68 char_type_label: base_type {
70 {encoding @DW_ATE_unsigned_char}
71 {byte_size 1 DW_FORM_sdata}
75 int_type_label: base_type {
77 {encoding @DW_ATE_signed}
78 {byte_size 4 DW_FORM_sdata}
82 array_a8_label: array_type {
83 {type :$char_type_label}
86 {type :$int_type_label}
87 {upper_bound 7 DW_FORM_udata}
91 # struct s { char a, b, c, d; };
92 struct_s_label: structure_type {
94 {byte_size 4 DW_FORM_sdata}
98 {type :$char_type_label}
99 {data_member_location 0 DW_FORM_udata}
103 {type :$char_type_label}
104 {data_member_location 1 DW_FORM_udata}
108 {type :$char_type_label}
109 {data_member_location 2 DW_FORM_udata}
113 {type :$char_type_label}
114 {data_member_location 3 DW_FORM_udata}
118 # struct t { int u, x:9, y:13, z:10; };
119 struct_t_label: structure_type {
121 {byte_size 8 DW_FORM_sdata}
125 {type :$int_type_label}
129 {type :$int_type_label}
130 {data_member_location 4 DW_FORM_udata}
131 {bit_size 9 DW_FORM_udata}
135 {type :$int_type_label}
136 {data_bit_offset 41 DW_FORM_udata}
137 {bit_size 13 DW_FORM_udata}
141 {type :$int_type_label}
142 {data_bit_offset 54 DW_FORM_udata}
143 {bit_size 10 DW_FORM_udata}
147 # struct st { struct s s; struct t t; };
148 struct_st_label: structure_type {
150 {byte_size 12 DW_FORM_udata}
154 {type :$struct_s_label}
158 {type :$struct_t_label}
159 {data_member_location 4 DW_FORM_udata}
164 {MACRO_AT_func { main }}
165 {DW_AT_external 1 flag}
167 # Simple memory location.
170 {type :$array_a8_label}
175 # Memory pieces: two bytes from &buf[2], and two bytes
179 {type :$struct_s_label}
188 # Register- and memory pieces: one byte each from r0,
189 # &buf[4], r1, and &buf[5].
192 {type :$struct_s_label}
194 regx [lindex $dwarf_regnum 0]
198 regx [lindex $dwarf_regnum 1]
204 # Memory pieces for bitfield access: 8 bytes optimized
205 # out, 3 bytes from &buf[3], and 1 byte from &buf[1].
208 {type :$struct_st_label}
217 # Register pieces for bitfield access: 4 bytes optimized
218 # out, 3 bytes from r0, and 1 byte from r1.
221 {type :$struct_t_label}
224 regx [lindex $dwarf_regnum 0]
226 regx [lindex $dwarf_regnum 1]
230 # One piece per bitfield, using piece offsets: 32 bits of
231 # an implicit value, 9 bits of a stack value, 13 bits of
232 # r0, and 10 bits of buf.
235 {type :$struct_t_label}
237 implicit_value 0x12 0x34 0x56 0x78 0x9a
242 regx [lindex $dwarf_regnum 0]
253 if { [prepare_for_testing ${testfile}.exp ${testfile} \
254 [list $srcfile $asm_file] {nodebug}] } {
262 # Determine byte order.
263 set endian [get_endianness]
265 # Byte-aligned memory pieces.
266 gdb_test "print/d s1" " = \\{a = 2, b = 3, c = 0, d = 1\\}" \
267 "s1 == re-ordered buf"
268 gdb_test_no_output "set var s1.a = 63"
269 gdb_test "print/d s1" " = \\{a = 63, b = 3, c = 0, d = 1\\}" \
271 gdb_test "print/d a" " = \\{0, 1, 63, 3, 4, 5, 6, 7\\}" \
272 "verify s1.a through a"
273 gdb_test_no_output "set var s1.b = 42"
274 gdb_test "print/d s1" " = \\{a = 63, b = 42, c = 0, d = 1\\}" \
276 gdb_test "print/d a" " = \\{0, 1, 63, 42, 4, 5, 6, 7\\}" \
277 "verify s1.b through a"
279 # Byte-aligned register- and memory pieces.
280 gdb_test_no_output "set var \$[lindex $regname 0] = 81" \
282 gdb_test_no_output "set var \$[lindex $regname 1] = 28" \
284 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 28, d = 5\\}" \
285 "initialized s2 from mem and regs"
286 gdb_test_no_output "set var s2.c += s2.a + s2.b - s2.d"
287 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 108, d = 5\\}" \
289 gdb_test "print/u \$[lindex $regname 1]" " = 108" \
290 "verify s2.c through reg"
291 gdb_test_no_output "set var s2 = {191, 73, 231, 123}" \
293 gdb_test "print/u s2" " = \\{a = 191, b = 73, c = 231, d = 123\\}" \
294 "verify re-initialized s2"
296 # Unaligned bitfield access through byte-aligned pieces.
297 gdb_test_no_output "set var a = { 0 }"
298 gdb_test_no_output "set var st1.t.x = -7"
299 gdb_test_no_output "set var st1.t.z = 340"
300 gdb_test_no_output "set var st1.t.y = 1234"
301 gdb_test "print st1.t" " = \\{u = <optimized out>, x = -7, y = 1234, z = 340\\}" \
304 little {set val "0x55, 0x0, 0xf9, 0xa5, 0x9"}
305 big {set val "0x54, 0x0, 0xfc, 0x93, 0x49"}
307 # | -- | z:2-9 | -- | x:0-7 | x:8 y:0-6 | y:7-12 z:0-1 | -- | -- |
308 # \_______________________________________________/
310 gdb_test "print/x a" " = \\{0x0, ${val}, 0x0, 0x0\\}" \
311 "verify st1 through a"
313 switch $endian { big {set val 0x7ffc} little {set val 0x3ffe00} }
314 gdb_test_no_output "set var \$[lindex $regname 0] = $val" \
315 "init t2, first piece"
316 gdb_test_no_output "set var \$[lindex $regname 1] = 0" \
317 "init t2, second piece"
318 gdb_test "print/d t2" " = \\{u = <optimized out>, x = 0, y = -1, z = 0\\}" \
319 "initialized t2 from regs"
320 gdb_test_no_output "set var t2.y = 2641"
321 gdb_test_no_output "set var t2.z = -400"
322 gdb_test_no_output "set var t2.x = 200"
323 gdb_test "print t2.x + t2.y + t2.z" " = 2441"
325 # Bitfield access through pieces with nonzero piece offsets.
326 gdb_test_no_output "set var \$[lindex $regname 0] = 0xa8000" \
328 gdb_test_no_output "set var *(char \[2\] *) (a + 5) = { 70, 82 }" \
331 little {set val "u = -1484430527, x = -70, y = 42, z = 145"}
332 big {set val "u = 591751049, x = -70, y = 42, z = 101"}
334 gdb_test "print t3" " = \\{$val\\}" \
335 "initialized t3 from reg and mem"
336 gdb_test_no_output "set var t3.y = -1" \
338 gdb_test "print/x \$[lindex $regname 0]" " = 0x7ffc000" \
339 "verify t3.y through reg"
340 gdb_test_no_output "set var t3.z = -614" \
342 switch $endian {big {set val "0x59, 0xa2"} little {set val "0x6a, 0x56"}}
343 gdb_test "print/x *(char \[2\] *) (a + 5)" " = \\{$val\\}" \
344 "verify t3.z through mem"