* [PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and implicit values
@ 2026-11-11 12:27 Stephan Rohr
2026-11-11 12:27 ` [PATCH v4 1/1] " Stephan Rohr
2026-11-25 13:54 ` [PING][PATCH v4 0/1] " Rohr, Stephan
0 siblings, 2 replies; 3+ messages in thread
From: Stephan Rohr @ 2026-11-11 12:27 UTC (permalink / raw)
To: gdb-patches; +Cc: tom
From: "Rohr, Stephan" <stephan.rohr@intel.com>
Hi Tom, all,
thanks for the feedback on v3. Sorry for the delay, this somewhat got
lost.
v3 was posted here:
https://sourceware.org/pipermail/gdb-patches/2024-March/207603.html
Tom provided feedback:
https://sourceware.org/pipermail/gdb-patches/2024-April/208168.html
This version addresses the concerns raised by Tom: the bug is not in
"value::primitive_field". GDB doesn't resolve the dynamic type for
a DW_OP_stack_value or DW_OP_implicit_value. It is properly resolved
for values in memory.
This patch addresses this by resolving the dynamic type in these two
cases instead of turning the assertion into an error.
Changes since v3:
* Keep the assert in "value::primitive_field".
* Resolve the dynamic type in "dwarf_expr_context::fetch_result" for
DW_OP_stack_value and DW_OP_implicit_value.
* Mark DW_OP_stack_value and DW_OP_implicit_value arrays whose bounds
resolve to optimized out as optimized out, as for values in memory.
* Adjust the testcase to cover both cases.
Tested with the unix, native-gdbserver and native-extended-gdbserver
board files; there are no regressions.
I appreciate your feedback.
Thanks
Stephan
Rohr, Stephan (1):
gdb: resolve dynamic types of DWARF stack and implicit values
gdb/dwarf2/expr.c | 14 +-
.../locexpr-data-member-dynamic-location.exp | 147 ++++++++++++++++++
gdb/value.c | 30 ++++
gdb/value.h | 16 ++
4 files changed, 200 insertions(+), 7 deletions(-)
create mode 100644 gdb/testsuite/gdb.dwarf2/locexpr-data-member-dynamic-location.exp
--
2.43.0
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v4 1/1] gdb: resolve dynamic types of DWARF stack and implicit values
2026-11-11 12:27 [PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and implicit values Stephan Rohr
@ 2026-11-11 12:27 ` Stephan Rohr
2026-11-25 13:54 ` [PING][PATCH v4 0/1] " Rohr, Stephan
1 sibling, 0 replies; 3+ messages in thread
From: Stephan Rohr @ 2026-11-11 12:27 UTC (permalink / raw)
To: gdb-patches; +Cc: tom
From: "Rohr, Stephan" <stephan.rohr@intel.com>
GDB asserts that the data location of a value's field with a dynamic
data location is resolved when fetching the field's value in
"value::primitive_field".
For values with a memory location, the dynamic type is properly resolved
in "dwarf_expr_context::fetch_result". When evaluating a DWARF expression
for a struct whose value either sits on top of the DWARF stack
(DW_OP_stack_value) or is embedded in the DWARF (DW_OP_implicit_value), the
dynamic type is not resolved. This triggers an assert in
"value::primitive_field".
Fix this by resolving the dynamic type of values on the DWARF stack and
of literal values, using their contents. As for values in memory, arrays
whose bounds resolve to optimized out are now marked as optimized out.
---
gdb/dwarf2/expr.c | 14 +-
.../locexpr-data-member-dynamic-location.exp | 147 ++++++++++++++++++
gdb/value.c | 30 ++++
gdb/value.h | 16 ++
4 files changed, 200 insertions(+), 7 deletions(-)
create mode 100644 gdb/testsuite/gdb.dwarf2/locexpr-data-member-dynamic-location.exp
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index f2e4deffd16..b14d51f33bf 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -1112,14 +1112,13 @@ dwarf_expr_context::fetch_result (struct type *type, struct type *subobj_type,
if (subobj_offset + len > max)
invalid_synthetic_pointer ();
- retval = value::allocate (subobj_type);
-
/* The given offset is relative to the actual object. */
if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
subobj_offset += n - max;
- copy (val->contents_all ().slice (subobj_offset, len),
- retval->contents_raw ());
+ retval = value_from_implicit_contents
+ (subobj_type, val->contents_all ().slice (subobj_offset, len),
+ this->m_frame);
}
break;
@@ -1130,9 +1129,10 @@ dwarf_expr_context::fetch_result (struct type *type, struct type *subobj_type,
if (subobj_offset + n > this->m_len)
invalid_synthetic_pointer ();
- retval = value::allocate (subobj_type);
- bfd_byte *contents = retval->contents_raw ().data ();
- memcpy (contents, this->m_data + subobj_offset, n);
+ retval = value_from_implicit_contents
+ (subobj_type,
+ gdb::make_array_view (this->m_data + subobj_offset, n),
+ this->m_frame);
}
break;
diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-dynamic-location.exp b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-dynamic-location.exp
new file mode 100644
index 00000000000..dee5762f913
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-dynamic-location.exp
@@ -0,0 +1,147 @@
+# Copyright 2024-2026 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/>.
+
+# Tests that the dynamic type of a not_lval value is properly resolved
+# when evaluating the DWARF expression. Also cover the case that the upper
+# bound of an array is optimized out. We expect GDB to print optimized out
+# for the whole array, too.
+
+load_lib dwarf.exp
+
+require dwarf2_support
+
+standard_testfile implref-array.c -dw.S
+
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
+ return
+}
+
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+ cu {} {
+ DW_TAG_compile_unit {} {
+ declare_labels int_label array_label struct_label \
+ opt_out_bound_label opt_out_array_label
+ set int_size [get_sizeof "int" 4]
+
+ int_label: DW_TAG_base_type {
+ DW_AT_byte_size $int_size DW_FORM_sdata
+ DW_AT_encoding @DW_ATE_signed
+ DW_AT_name int
+ }
+
+ # An array of five ints. The elements are at ARRAY plus a
+ # 4-byte offset read from the object by DW_OP_deref_size. The
+ # offset allows constant contents, as needed for
+ # DW_OP_implicit_value.
+ array_label: DW_TAG_array_type {
+ DW_AT_type :$int_label
+ DW_AT_data_location {
+ DW_OP_push_object_address
+ DW_OP_deref_size 4
+ DW_OP_addr [gdb_target_symbol array]
+ DW_OP_plus
+ } SPECIAL_expr
+ } {
+ DW_TAG_subrange_type {
+ DW_AT_type :$int_label
+ DW_AT_upper_bound 4 DW_FORM_udata
+ }
+ }
+
+ struct_label: DW_TAG_structure_type {
+ DW_AT_byte_size 4 DW_FORM_udata
+ } {
+ DW_TAG_member {
+ DW_AT_name data
+ DW_AT_type :$array_label
+ DW_AT_data_member_location 0 DW_FORM_udata
+ }
+ }
+
+ # Define a variable for the struct type. It lives on the DWARF
+ # stack. The offset is 0.
+ DW_TAG_variable {
+ DW_AT_name var_a_stack_value
+ DW_AT_type :$struct_label
+ DW_AT_location {
+ DW_OP_lit0
+ DW_OP_stack_value
+ } SPECIAL_expr
+ }
+
+ # Same with implicit value. The offset is 0.
+ DW_TAG_variable {
+ DW_AT_name var_a_implicit_value
+ DW_AT_type :$struct_label
+ DW_AT_location {
+ DW_OP_implicit_value 0x00000000
+ } SPECIAL_expr
+ }
+
+ # Add a variable without a DWARF location for the upper bound.
+ # GDB resolves this to "optimized out" when reading its value.
+ opt_out_bound_label: DW_TAG_variable {
+ DW_AT_name opt_out_bound
+ DW_AT_type :$int_label
+ DW_AT_declaration 1 flag
+ }
+
+ # Add a type that uses the "opt_out_bound" variable for the upper
+ # bound.
+ opt_out_array_label: DW_TAG_array_type {
+ DW_AT_type :$int_label
+ } {
+ DW_TAG_subrange_type {
+ DW_AT_type :$int_label
+ DW_AT_upper_bound :$opt_out_bound_label
+ }
+ }
+
+ # Optimized out variable on the DWARF stack.
+ DW_TAG_variable {
+ DW_AT_name opt_out_stack_value
+ DW_AT_type :$opt_out_array_label
+ DW_AT_location {
+ DW_OP_lit1
+ DW_OP_stack_value
+ } SPECIAL_expr
+ }
+
+ # Same with implicit value.
+ DW_TAG_variable {
+ DW_AT_name opt_out_implicit_value
+ DW_AT_type :$opt_out_array_label
+ DW_AT_location {
+ DW_OP_implicit_value 0x01 0x00 0x00 0x00
+ } SPECIAL_expr
+ }
+ }
+ }
+}
+
+if { [prepare_for_testing "failed to prepare" ${testfile} \
+ [list $srcfile $asm_file] {nodebug}] } {
+ return
+}
+
+if {![runto_main]} {
+ return
+}
+
+gdb_test "print var_a_stack_value" " = \\{data = \\{0, 1, 2, 3, 4\\}\\}"
+gdb_test "print var_a_implicit_value" " = \\{data = \\{0, 1, 2, 3, 4\\}\\}"
+gdb_test "print opt_out_stack_value" " = <optimized out>"
+gdb_test "print opt_out_implicit_value" " = <optimized out>"
diff --git a/gdb/value.c b/gdb/value.c
index 87a1f804c01..1de6c8bb873 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -3695,6 +3695,36 @@ value_from_contents (struct type *type, const gdb_byte *contents)
return result;
}
+/* See value.h. */
+
+struct value *
+value_from_implicit_contents (struct type *type,
+ gdb::array_view<const gdb_byte> contents,
+ const frame_info_ptr &frame)
+{
+ /* There is no address, so pass 0. DW_OP_push_object_address then
+ pushes 0 and dwarf_expr_context::read_mem serves reads of the
+ object from CONTENTS. This returns TYPE if it is not dynamic. */
+ type = resolve_dynamic_type (type, contents, 0, &frame);
+
+ /* Resolution found that the bounds are optimized out. In this case,
+ mark the array itself as optimized-out. */
+ struct type *real_type = check_typedef (type);
+ if (real_type->code () == TYPE_CODE_ARRAY
+ && real_type->bound_optimized_out ())
+ return value::allocate_optimized_out (type);
+
+ value *retval = value::allocate (type);
+ size_t len = std::min (contents.size (), (size_t) type->length ());
+ copy (contents.slice (0, len), retval->contents_raw ().slice (0, len));
+
+ /* If only part of the data was loaded, mark the rest as unavailable. */
+ if (len < type->length ())
+ retval->mark_bytes_unavailable (len, type->length () - len);
+
+ return retval;
+}
+
/* Extract a value from the history file. Input will be of the form
$digits or $$digits. See block comment above 'write_dollar_variable'
for details. */
diff --git a/gdb/value.h b/gdb/value.h
index 598d252abac..4c0252da7f5 100644
--- a/gdb/value.h
+++ b/gdb/value.h
@@ -1155,6 +1155,22 @@ extern struct value *value_from_contents_and_address
const frame_info_ptr &frame = nullptr);
extern struct value *value_from_contents (struct type *, const gdb_byte *);
+/* Return a new not_lval value of type TYPE holding CONTENTS, for an
+ object that has no address in target memory, e.g. the result of
+ DW_OP_stack_value or DW_OP_implicit_value. FRAME is the frame used
+ to evaluate the dynamic properties of TYPE.
+
+ If TYPE is dynamic, it is resolved using CONTENTS for reads relative
+ to the object, so that for instance a DW_AT_data_location of
+ "DW_OP_push_object_address; DW_OP_deref" yields the pointer stored
+ in CONTENTS. Resolving may change the length of the type; bytes of
+ the resolved type that are not covered by CONTENTS are marked
+ unavailable. If TYPE is an array whose bounds resolve to optimized
+ out, the returned value is optimized out. */
+extern struct value *value_from_implicit_contents
+ (struct type *type, gdb::array_view<const gdb_byte> contents,
+ const frame_info_ptr &frame);
+
extern value *default_value_from_register (gdbarch *gdbarch, type *type,
int regnum,
const frame_info_ptr &this_frame);
--
2.43.0
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PING][PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and implicit values
2026-11-11 12:27 [PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and implicit values Stephan Rohr
2026-11-11 12:27 ` [PATCH v4 1/1] " Stephan Rohr
@ 2026-11-25 13:54 ` Rohr, Stephan
1 sibling, 0 replies; 3+ messages in thread
From: Rohr, Stephan @ 2026-11-25 13:54 UTC (permalink / raw)
To: gdb-patches; +Cc: tom
Kindly pinging.
Thanks
Stephan
> -----Original Message-----
> From: Stephan Rohr <stephan.rohr@intel.com>
> Sent: Wednesday, 11 November 2026 13:27
> To: gdb-patches@sourceware.org
> Cc: tom@tromey.com
> Subject: [PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and
> implicit values
>
> From: "Rohr, Stephan" <stephan.rohr@intel.com>
>
> Hi Tom, all,
>
> thanks for the feedback on v3. Sorry for the delay, this somewhat got
> lost.
>
> v3 was posted here:
> https://sourceware.org/pipermail/gdb-patches/2024-March/207603.html
>
> Tom provided feedback:
> https://sourceware.org/pipermail/gdb-patches/2024-April/208168.html
>
> This version addresses the concerns raised by Tom: the bug is not in
> "value::primitive_field". GDB doesn't resolve the dynamic type for
> a DW_OP_stack_value or DW_OP_implicit_value. It is properly resolved
> for values in memory.
>
> This patch addresses this by resolving the dynamic type in these two
> cases instead of turning the assertion into an error.
>
> Changes since v3:
>
> * Keep the assert in "value::primitive_field".
> * Resolve the dynamic type in "dwarf_expr_context::fetch_result" for
> DW_OP_stack_value and DW_OP_implicit_value.
> * Mark DW_OP_stack_value and DW_OP_implicit_value arrays whose bounds
> resolve to optimized out as optimized out, as for values in memory.
> * Adjust the testcase to cover both cases.
>
> Tested with the unix, native-gdbserver and native-extended-gdbserver
> board files; there are no regressions.
>
> I appreciate your feedback.
>
> Thanks
> Stephan
>
> Rohr, Stephan (1):
> gdb: resolve dynamic types of DWARF stack and implicit values
>
> gdb/dwarf2/expr.c | 14 +-
> .../locexpr-data-member-dynamic-location.exp | 147
> ++++++++++++++++++
> gdb/value.c | 30 ++++
> gdb/value.h | 16 ++
> 4 files changed, 200 insertions(+), 7 deletions(-)
> create mode 100644 gdb/testsuite/gdb.dwarf2/locexpr-data-member-
> dynamic-location.exp
>
> --
> 2.43.0
>
> ________________________________________
> Intel Deutschland GmbH
>
> Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
>
> Tel: +49 (89) 99143-0
>
> www.intel.de
>
> Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran
> Sitaraman
>
> Chairperson of the Supervisory Board: Sonja Pierer
>
> Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB
> 186928
>
> This e-mail and any attachments may contain confidential material for
> the sole use of the intended recipient(s). Any review or distribution
> by others is strictly prohibited. If you are not the intended
> recipient, please contact the sender and delete all copies.
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 13:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-11 12:27 [PATCH v4 0/1] gdb: resolve dynamic types of DWARF stack and implicit values Stephan Rohr
2026-11-11 12:27 ` [PATCH v4 1/1] " Stephan Rohr
2026-11-25 13:54 ` [PING][PATCH v4 0/1] " Rohr, Stephan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox