From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Emit DWARF expressions from dwarf-to-dwarf-assembler.py
Date: Fri, 22 May 2026 11:18:56 -0600 [thread overview]
Message-ID: <20260522171856.3378166-1-tromey@adacore.com> (raw)
This changes dwarf-to-dwarf-assembler.py to attempt to emit DWARF
expressions. It is not close to perfect, since it relies on the
elftools formatting to some extent, but it does at least eliminate the
need to do a separate "readelf" step to try to work on the expression
by hand.
---
gdb/contrib/dwarf-to-dwarf-assembler.py | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/gdb/contrib/dwarf-to-dwarf-assembler.py b/gdb/contrib/dwarf-to-dwarf-assembler.py
index 4832c6c8cb1..5f990f79569 100755
--- a/gdb/contrib/dwarf-to-dwarf-assembler.py
+++ b/gdb/contrib/dwarf-to-dwarf-assembler.py
@@ -52,6 +52,7 @@ from elftools.construct.lib.container import ListContainer
from elftools.dwarf.compileunit import CompileUnit as RawCompileUnit
from elftools.dwarf.die import DIE as RawDIE
from elftools.dwarf.die import AttributeValue
+from elftools.dwarf.dwarf_expr import DWARFExprParser
from elftools.dwarf.enums import ENUM_DW_ATE, ENUM_DW_LANG
from elftools.elf.elffile import ELFFile
@@ -118,19 +119,27 @@ class DWARFAttribute:
def __init__(
self,
+ cu,
die_offset: int,
name: str,
value: str | bytes | int | bool,
form=None,
):
+ self.cu = cu
self.die_offset = die_offset
self.name = name
self.value = value
self.form = form
- def _format_expr_value(self) -> str:
- self.form = "SPECIAL_expr"
- return "{ MANUAL: Fill expr list }"
+ def _format_expr_value(self, intro, indent_count: int) -> str:
+ result = indent(intro, indent_count) + "{\n"
+ parser = DWARFExprParser(self.cu.structs)
+ for op in parser.parse_expr(self.value):
+ result += indent(f"{op.op_name} 0x{op.op:02x}", indent_count + 1)
+ if op.args:
+ result += f" {op.args}"
+ result += "\n"
+ return result + indent("} SPECIAL_expr", indent_count)
def _needs_escaping(self, str_value: str) -> bool:
charset = set(str_value)
@@ -151,9 +160,7 @@ class DWARFAttribute:
def _format_value(
self, offset_die_lookup: dict[int, "DWARFDIE"], indent_count: int = 0
) -> str:
- if self.form in EXPR_ATTRIBUTE_FORMS:
- return self._format_expr_value()
- elif isinstance(self.value, bool):
+ if isinstance(self.value, bool):
return str(int(self.value))
elif isinstance(self.value, int):
if self.form == "DW_FORM_ref4":
@@ -218,6 +225,10 @@ class DWARFAttribute:
s += "@" + LANG_NAME[self.value]
elif self.name == "DW_AT_encoding" and isinstance(self.value, int):
s += "@" + ATE_NAME[self.value]
+ elif self.form in EXPR_ATTRIBUTE_FORMS:
+ # This returns a complete description that is already
+ # indented.
+ return self._format_expr_value(s, indent_count)
else:
s += self._format_value(offset_die_lookup)
@@ -491,7 +502,7 @@ class DWARFParser:
self.referenced_offsets.add(referenced_die.offset)
processed_attrs[attr_name] = DWARFAttribute(
- raw_die.offset, attr_name, actual_value, attr_value.form
+ die_cu, raw_die.offset, attr_name, actual_value, attr_value.form
)
if raw_die.tag == DWARFCompileUnit.compile_unit_tag:
base-commit: 1678d7d3d2e4cf83cda5a96334a0326573ad0f7c
--
2.54.0
next reply other threads:[~2026-05-22 17:19 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-22 17:18 Tom Tromey [this message]
2026-06-05 21:11 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260522171856.3378166-1-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox