1 # Copyright 2017-2024 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 require dwarf2_support
25 # Choose suitable integer registers for the test.
27 set dwarf_regnum {0 1}
29 if { [is_aarch64_target] } {
31 } elseif { [is_aarch32_target]
32 || [istarget "s390*-*-*" ]
33 || [istarget "powerpc*-*-*"]
34 || [istarget "rs6000*-*-aix*"] } {
36 } elseif { [is_x86_like_target] } {
38 } elseif { [is_amd64_regs_target] } {
41 verbose "Skipping $gdb_test_file_name."
45 standard_testfile .c -dw.S
47 # Make some DWARF for the test.
49 set asm_file [standard_output_file $srcfile2]
50 Dwarf::assemble $asm_file {
51 global dwarf_regnum regname srcfile
53 set buf_var [gdb_target_symbol buf]
60 declare_labels char_type_label
61 declare_labels int_type_label short_type_label
62 declare_labels array_a8_label struct_s_label struct_t_label
63 declare_labels struct_st_label
66 char_type_label: base_type {
68 {encoding @DW_ATE_unsigned_char}
69 {byte_size 1 DW_FORM_sdata}
73 int_type_label: base_type {
75 {encoding @DW_ATE_signed}
76 {byte_size 4 DW_FORM_sdata}
80 array_a8_label: array_type {
81 {type :$char_type_label}
84 {type :$int_type_label}
85 {upper_bound 7 DW_FORM_udata}
89 # struct s { char a, b, c, d; };
90 struct_s_label: structure_type {
92 {byte_size 4 DW_FORM_sdata}
96 {type :$char_type_label}
97 {data_member_location 0 DW_FORM_udata}
101 {type :$char_type_label}
102 {data_member_location 1 DW_FORM_udata}
106 {type :$char_type_label}
107 {data_member_location 2 DW_FORM_udata}
111 {type :$char_type_label}
112 {data_member_location 3 DW_FORM_udata}
116 # struct t { int u, x:9, y:13, z:10; };
117 struct_t_label: structure_type {
119 {byte_size 8 DW_FORM_sdata}
123 {type :$int_type_label}
127 {type :$int_type_label}
128 {data_member_location 4 DW_FORM_udata}
129 {bit_size 9 DW_FORM_udata}
133 {type :$int_type_label}
134 {data_bit_offset 41 DW_FORM_udata}
135 {bit_size 13 DW_FORM_udata}
139 {type :$int_type_label}
140 {data_bit_offset 54 DW_FORM_udata}
141 {bit_size 10 DW_FORM_udata}
145 # struct st { struct s s; struct t t; };
146 struct_st_label: structure_type {
148 {byte_size 12 DW_FORM_udata}
152 {type :$struct_s_label}
156 {type :$struct_t_label}
157 {data_member_location 4 DW_FORM_udata}
162 {MACRO_AT_func { main }}
163 {DW_AT_external 1 flag}
165 # Simple memory location.
168 {type :$array_a8_label}
173 # Memory pieces: two bytes from &buf[2], and two bytes
177 {type :$struct_s_label}
186 # Register- and memory pieces: one byte each from r0,
187 # &buf[4], r1, and &buf[5].
190 {type :$struct_s_label}
192 regx [lindex $dwarf_regnum 0]
196 regx [lindex $dwarf_regnum 1]
202 # Memory pieces for bitfield access: 8 bytes optimized
203 # out, 3 bytes from &buf[3], and 1 byte from &buf[1].
206 {type :$struct_st_label}
215 # Register pieces for bitfield access: 4 bytes optimized
216 # out, 3 bytes from r0, and 1 byte from r1.
219 {type :$struct_t_label}
222 regx [lindex $dwarf_regnum 0]
224 regx [lindex $dwarf_regnum 1]
228 # One piece per bitfield, using piece offsets: 32 bits of
229 # an implicit value, 9 bits of a stack value, 13 bits of
230 # r0, and 10 bits of buf.
233 {type :$struct_t_label}
235 implicit_value 0x12 0x34 0x56 0x78 0x9a
240 regx [lindex $dwarf_regnum 0]
251 if { [prepare_for_testing ${testfile}.exp ${testfile} \
252 [list $srcfile $asm_file] {nodebug}] } {
260 # Determine byte order.
261 set endian [get_endianness]
263 # Byte-aligned memory pieces.
264 gdb_test "print/d s1" " = \\{a = 2, b = 3, c = 0, d = 1\\}" \
265 "s1 == re-ordered buf"
266 gdb_test_no_output "set var s1.a = 63"
267 gdb_test "print/d s1" " = \\{a = 63, b = 3, c = 0, d = 1\\}" \
269 gdb_test "print/d a" " = \\{0, 1, 63, 3, 4, 5, 6, 7\\}" \
270 "verify s1.a through a"
271 gdb_test_no_output "set var s1.b = 42"
272 gdb_test "print/d s1" " = \\{a = 63, b = 42, c = 0, d = 1\\}" \
274 gdb_test "print/d a" " = \\{0, 1, 63, 42, 4, 5, 6, 7\\}" \
275 "verify s1.b through a"
277 # Byte-aligned register- and memory pieces.
278 gdb_test_no_output "set var \$[lindex $regname 0] = 81" \
280 gdb_test_no_output "set var \$[lindex $regname 1] = 28" \
282 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 28, d = 5\\}" \
283 "initialized s2 from mem and regs"
284 gdb_test_no_output "set var s2.c += s2.a + s2.b - s2.d"
285 gdb_test "print/u s2" " = \\{a = 81, b = 4, c = 108, d = 5\\}" \
287 gdb_test "print/u \$[lindex $regname 1]" " = 108" \
288 "verify s2.c through reg"
289 gdb_test_no_output "set var s2 = {191, 73, 231, 123}" \
291 gdb_test "print/u s2" " = \\{a = 191, b = 73, c = 231, d = 123\\}" \
292 "verify re-initialized s2"
294 # Unaligned bitfield access through byte-aligned pieces.
295 gdb_test_no_output "set var a = { 0 }"
296 gdb_test_no_output "set var st1.t.x = -7"
297 gdb_test_no_output "set var st1.t.z = 340"
298 gdb_test_no_output "set var st1.t.y = 1234"
299 gdb_test "print st1.t" " = \\{u = <optimized out>, x = -7, y = 1234, z = 340\\}" \
302 little {set val "0x55, 0x0, 0xf9, 0xa5, 0x9"}
303 big {set val "0x54, 0x0, 0xfc, 0x93, 0x49"}
305 # | -- | z:2-9 | -- | x:0-7 | x:8 y:0-6 | y:7-12 z:0-1 | -- | -- |
306 # \_______________________________________________/
308 gdb_test "print/x a" " = \\{0x0, ${val}, 0x0, 0x0\\}" \
309 "verify st1 through a"
311 switch $endian { big {set val 0x7ffc} little {set val 0x3ffe00} }
312 gdb_test_no_output "set var \$[lindex $regname 0] = $val" \
313 "init t2, first piece"
314 gdb_test_no_output "set var \$[lindex $regname 1] = 0" \
315 "init t2, second piece"
316 gdb_test "print/d t2" " = \\{u = <optimized out>, x = 0, y = -1, z = 0\\}" \
317 "initialized t2 from regs"
318 gdb_test_no_output "set var t2.y = 2641"
319 gdb_test_no_output "set var t2.z = -400"
320 gdb_test_no_output "set var t2.x = 200"
321 gdb_test "print t2.x + t2.y + t2.z" " = 2441"
323 # Bitfield access through pieces with nonzero piece offsets.
324 gdb_test_no_output "set var \$[lindex $regname 0] = 0xa8000" \
326 gdb_test_no_output "set var *(char \[2\] *) (a + 5) = { 70, 82 }" \
329 little {set val "u = -1484430527, x = -70, y = 42, z = 145"}
330 big {set val "u = 591751049, x = -70, y = 42, z = 101"}
332 gdb_test "print t3" " = \\{$val\\}" \
333 "initialized t3 from reg and mem"
334 gdb_test_no_output "set var t3.y = -1" \
336 gdb_test "print/x \$[lindex $regname 0]" " = 0x7ffc000" \
337 "verify t3.y through reg"
338 gdb_test_no_output "set var t3.z = -614" \
340 switch $endian {big {set val "0x59, 0xa2"} little {set val "0x6a, 0x56"}}
341 gdb_test "print/x *(char \[2\] *) (a + 5)" " = \\{$val\\}" \
342 "verify t3.z through mem"