From: Claudiu Zissulescu <claudiu.zissulescu-ianculescu@amd.com>
To: <gdb-patches@sourceware.org>
Cc: <pedro@palves.net>, <Lancelot.Six@amd.com>,
Claudiu Zissulescu-Ianculescu
<claudiu.zissulescu-ianculescu@amd.com>
Subject: [PATCH] gdb: return the object's address for dynamic_cast<void *>
Date: Thu, 5 Nov 2026 13:08:54 +0200 [thread overview]
Message-ID: <20261105110854.56434-1-claudiu.zissulescu-ianculescu@amd.com> (raw)
From: Claudiu Zissulescu-Ianculescu <claudiu.zissulescu-ianculescu@amd.com>
value_dynamic_cast computes the address of the most derived object, but
for a cast to "void *" it then returns value_at_lazy (type, addr): the
"void *" stored at that address, i.e. the first word of the object. For
a polymorphic class that is the vtable pointer:
(gdb) print lrp
$1 = (LR *) 0x416020
(gdb) print dynamic_cast<void *> (rp)
$2 = (void *) 0x4012f0 <vtable for LR+16>
The cast to "void *" must give the address itself. Use
value_from_pointer, as value_addr does to make a pointer from an
address.
gdb.cp/casts.exp only checked dynamic_cast<void *> on a null pointer,
which returns early. Add checks through the most derived class and
through both of its bases, one of which is at a non-zero offset.
Tested on x86_64-linux (GCC): gdb.cp/casts.exp 70 PASS / 3 FAIL before
(the new checks), 73 PASS after.
No regressions in gdb.cp/casts.exp, debug-expr.exp,
rvalue-ref-casts.exp, rtti.exp, typeid.exp, class2.exp, virtbase.exp,
derivation.exp and gdb.python/py-value.exp (Value.dynamic_cast uses
the same function): 615 PASS, 0 FAIL.
This is a complementary issue to the Bugzilla's gdb/29011 bug.
---
gdb/testsuite/gdb.cp/casts.exp | 6 ++++++
gdb/valops.c | 2 +-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp
index 67246ac7b5c..1291af33be6 100644
--- a/gdb/testsuite/gdb.cp/casts.exp
+++ b/gdb/testsuite/gdb.cp/casts.exp
@@ -213,6 +213,12 @@ gdb_test "print &dynamic_cast<VirtualRight &> (*vlr) == vr" " = true"
gdb_test "print &dynamic_cast<VirtualRight &> (*vl) == vr" " = true"
gdb_test "print &dynamic_cast<VirtualRight &> (*vr) == vr" " = true"
+# dynamic_cast<void *> gives the address of the most derived object,
+# also through a base at a non-zero offset.
+gdb_test "print dynamic_cast<void *> (vlr) == vlr" " = true"
+gdb_test "print dynamic_cast<void *> (vl) == vlr" " = true"
+gdb_test "print dynamic_cast<void *> (vr) == vlr" " = true"
+
if {[prepare_for_testing "failed to prepare" ${testfile}03 $srcfile2 \
{debug c++ additional_flags=-std=c++03}]} {
return
diff --git a/gdb/valops.c b/gdb/valops.c
index 55f891c7e72..52d0bb98198 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -945,7 +945,7 @@ value_dynamic_cast (struct type *type, struct value *arg)
most-derived object. */
if (resolved_type->code () == TYPE_CODE_PTR
&& resolved_type->target_type ()->code () == TYPE_CODE_VOID)
- return value_at_lazy (type, addr);
+ return value_from_pointer (type, addr);
tem = value_at (resolved_type->target_type (), addr);
type = (is_ref
--
2.56.0
next reply other threads:[~2026-11-05 11:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 11:08 Claudiu Zissulescu [this message]
2026-11-05 16:36 ` Tom Tromey
2026-11-06 12:01 ` Zissulescu-Ianculescu, Claudiu
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=20261105110854.56434-1-claudiu.zissulescu-ianculescu@amd.com \
--to=claudiu.zissulescu-ianculescu@amd.com \
--cc=Lancelot.Six@amd.com \
--cc=gdb-patches@sourceware.org \
--cc=pedro@palves.net \
/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