Issue 250506.2: Improve Support for Downcasting Objects

Author: Cary Coutant
Champion: Cary Coutant
Date submitted: 2025-05-06
Date revised:
Date closed:
Type: Enhancement
Status: Open
DWARF version: 6

This is the second part of a three-part proposal. The first part, 250506.1, proposes a standard mechanism for locating the virtual function table (vtable) given an object of a polymorphic class. This second part proposes a standard mechanism for identifying the most-derived class of an object, given its vtable location, in order to support downcasting of pointers while debugging. The third part, 250506.3, proposes a fix to the DW_AT_vtable_elem_location attribute, which appears to be incorrectly implemented in compilers today.

Background

See 250506.1 for the full background and history.

Problem

The second request in PR 7081 was for a way to associate vtables with their class definitions, to make it easier to downcast pointers from the base class to the most-derived class, based on the vtable pointer. The clang and Rust approaches are similar, but not exactly the same, and the Concurrent approach puts a relocatable address of the vtable in the type DIE.

Adding an attribute directly to the type DIE that points to the vtable has the disadvantage that type de-duplication might discard all but one of several copies of a vtable, if multiple copies of the vtable remain after linking.

We also desire a fast lookup mechanism where a vtable address can be translated into the corresponding type DIE without requiring the consumer to parse all the DWARF info at once.

Proposal

To make the association between a vtable and a type DIE, we add a new DWARF tag for a vtable object, DW_TAG_vtable, whose DW_AT_location provides the address of the vtable, and whose (new) DW_AT_vtable_type attribute points to the type DIE corresponding to the vtable. Additional attributes may provide additional details about the vtable.

To provide for faster lookup of vtable addresses, we add a DW_AT_vtable_ranges attribute to the compilation unit DIEs.

In Section 2.1, "The Debugging Information Entry," add DW_TAG_vtable to Table 2.1.

In Section 2.2, "Attribute Types," add two rows to Table 2.2:

Attribute Usage
DW_AT_vtable_type Type corresponding to a vtable
DW_AT_vtable_ranges Ranges of vtable addresses

In Section 3.1.1, "Full and Partial Compilation Unit Entries," add a numbered item:

18. A DW_AT_vtable_ranges attribute, whose value is of class rnglist, which provides a range of addresses containing vtables defined by this compilation unit.

In Section 3.1.2, "Skeleton Compilation Unit Entries," add a numbered item:

9. A DW_AT_vtable_ranges attribute.

In Chapter 4, "Data Object and Object List Entries," add a new section:

4.4 Virtual Function Table (vtable) Entries

Virtual function tables (vtables) are represented by debugging information entries with the tag DW_TAG_vtable. It may have the following attributes:

  1. A DW_AT_location attribute, whose value describes the location of the vtable.

  2. A DW_AT_vtable_type attribute, which is a reference to the debugging information entry for the associated type.

  3. A DW_AT_artificial attribute, which is a flag indicating that the vtable entry represents an object created by the compiler that does not correspond directly to source code.

Virtual Function Table entries allow a debugger to associate vtables with the corresponding class type, in order to downcast a pointer whose type is "pointer-to-base-class" to a pointer with type "pointer-to-derived-class," using the vtable pointer of the object.

In Section 7.5.3, "Abbreviations Tables," add a row to Table 7.3:

TAG name Value
DW_TAG_vtable TBD

In Section 7.5.4, "Attribute Encodings," add two rows to Table 7.5:

Attribute name Value Classes
DW_AT_vtable_type TBD reference
DW_AT_vtable_ranges TBD rnglist

In Appendix A, "Attribute by Tag," add a row for DW_TAG_vtable:

TAG name Applicable attributes
DW_TAG_vtable DW_AT_artificial
DW_AT_location
DW_AT_vtable_type

In the same table, add DW_AT_vtable_ranges to the row for DW_TAG_compile_unit.

Optional Variations