[PATCH v2 36/47] gdb, dwarf, ze: add DW_OP_INTEL_regval_bits

Tankut Baris Aktemur
2024-12-13T16:27:27Z

From: Markus Metzger <markus.t.metzger@intel.com>

Add support for a new DWARF expression proposed in
https://dwarfstd.org/ShowIssue.php?issue=201007.1.

Cc: <binutils@sourceware.org>
---
binutils/dwarf.c | 6 ++++++
gdb/dwarf2/expr.c | 36 ++++++++++++++++++++++++++++++++++++
gdb/dwarf2/expr.h | 5 +++++
gdb/dwarf2/loc.c | 2 ++
include/dwarf2.def | 4 ++++
5 files changed, 53 insertions(+)

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index e8425b98d3660d531a0b4bff042fca0665a96f1b..fc3ecc3ab25db7c9810e36cf0219ac57fa8c204e 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -1703,6 +1703,12 @@ decode_location_expression (unsigned char * data,
printf ("DW_OP_PGI_omp_thread_num");
break;

+ /* Intel wide registers extension. */
+ case DW_OP_INTEL_regval_bits:
+ SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
+ printf ("DW_OP_INTEL_regval_bits: %lu", (unsigned long) uvalue);
+ break;
+
default:
if (op >= DW_OP_lo_user
&& op <= DW_OP_hi_user)
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index 5ad17eaaaff89b2d7f6f678e6e5b7e73e320f81c..96c6b258f263ad7a73599d631d24e03de06fa95f 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -862,6 +862,28 @@ dwarf_expr_context::read_mem (gdb_byte *buf, CORE_ADDR addr,

/* See expr.h. */

+void
+dwarf_expr_context::read_reg (gdb_byte *buf, size_t bitoffset,
+ size_t bitsize, int dwregnum)
+{
+ struct gdbarch * const gdbarch = get_frame_arch (this->m_frame);
+ const int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwregnum);
+
+ const ULONGEST regsize = register_size (gdbarch, regnum);
+ if ((regsize * 8) < (bitsize + bitoffset))
+ error (_("DWARF expr: error accessing %s[%" PRIu64 ":%" PRIu64 "]"),
+ gdbarch_register_name (gdbarch, regnum),
+ bitsize + bitoffset - 1, bitoffset);
+
+ gdb_byte * const regbuf = (gdb_byte *) alloca (regsize);
+ get_frame_register (this->m_frame, regnum, regbuf);
+
+ const enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
+ copy_bitwise (buf, 0, regbuf, bitoffset, bitsize, byte_order);
+}
+
+/* See expr.h. */
+
void
dwarf_expr_context::push_dwarf_reg_entry_value (call_site_parameter_kind kind,
call_site_parameter_u kind_u,
@@ -2330,6 +2352,20 @@ dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
}
break;

+ case DW_OP_INTEL_regval_bits:
+ {
+ uint8_t size = *op_ptr++;
+ const ULONGEST off = value_as_long (fetch (0));
+ pop ();
+ const LONGEST dwregnum = value_as_long (fetch (0));
+ pop ();
+
+ result = 0;
+ read_reg ((gdb_byte *) &result, off, size, dwregnum);
+ result_val = value_from_ulongest (address_type, result);
+ }
+ break;
+
case DW_OP_convert:
case DW_OP_GNU_convert:
case DW_OP_reinterpret:
diff --git a/gdb/dwarf2/expr.h b/gdb/dwarf2/expr.h
index b02cc53164065433d14d472b77a5a337125d10b9..8ecc4f130da1d6ba52dfc2cbb857d3ab0137c3fe 100644
--- a/gdb/dwarf2/expr.h
+++ b/gdb/dwarf2/expr.h
@@ -252,6 +252,11 @@ struct dwarf_expr_context
but with the address being 0. In this situation, we arrange for
memory reads to come from the passed-in buffer. */
void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t length);
+
+ /* Read BITSIZE bits from the register indicated by the DWARF register
+ number DWREGNUM starting at bit BITOFFSET into BUF. */
+ void read_reg (gdb_byte *buf, size_t bitoffset, size_t bitsize,
+ int dwregnum);
};

/* Return the value of register number REG (a DWARF register number),
diff --git a/gdb/dwarf2/loc.c b/gdb/dwarf2/loc.c
index 989d33901e01070d05c1009cdd80db60af4f0b2f..bd0848da452ed76548c60dee501239fc8e37be7f 100644
--- a/gdb/dwarf2/loc.c
+++ b/gdb/dwarf2/loc.c
@@ -2087,6 +2087,7 @@ dwarf2_get_symbol_read_needs (gdb::array_view<const gdb_byte> expr,
case DW_OP_GNU_parameter_ref:
case DW_OP_regval_type:
case DW_OP_GNU_regval_type:
+ case DW_OP_INTEL_regval_bits:
symbol_needs = SYMBOL_NEEDS_FRAME;
break;

@@ -3344,6 +3345,7 @@ disassemble_dwarf_expression (struct ui_file *stream,
break;

case DW_OP_const1u:
+ case DW_OP_INTEL_regval_bits:
ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
data += 1;
gdb_printf (stream, " %s", pulongest (ul));
diff --git a/include/dwarf2.def b/include/dwarf2.def
index 66c7fa1220f82f536617222545f7b9590626808d..4c2e27a155d833b53a8c43c34bf59f1efe32f5b1 100644
--- a/include/dwarf2.def
+++ b/include/dwarf2.def
@@ -688,6 +688,10 @@ DW_OP (DW_OP_GNU_const_index, 0xfc)
/* The GNU variable value extension.
See http://dwarfstd.org/ShowIssue.php?issue=161109.2 . */
DW_OP (DW_OP_GNU_variable_value, 0xfd)
+/* https://dwarfstd.org/ShowIssue.php?issue=201007.1
+
+ The DW_OP_regval_bits operation extracts a value from a register. */
+DW_OP (DW_OP_INTEL_regval_bits, 0xfe)
/* HP extensions. */
DW_OP_DUP (DW_OP_HP_unknown, 0xe0) /* Ouch, the same as GNU_push_tls_address. */
DW_OP (DW_OP_HP_is_value, 0xe1)