Issue 250506.3: Replace DW_AT_vtable_elem_location Attribute with DW_AT_vtable_elem_index

Author: Cary Coutant
Champion: Cary Coutant
Date submitted: 2025-05-06
Date revised: 2025-09-24
Date closed: 2025-10-13
Type: Enhancement
Status: Accepted
DWARF version: 6

This is the third 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. The second part, 250506.2, 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. This 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 third request in PR 7081 was to identify a function's vtable entry, which was already supported in DWARF at the time, but hadn't yet been implemented in GCC at the time of the PR.

Looking at the DWARF generated by GCC (and I'm guessing LLVM does the same), I see DW_AT_vtable_elem_location attributes that look like this:

<1b8>   DW_AT_vtable_elem_location: 2 byte block: 10 0 (DW_OP_constu: 0)

This is not correct DWARF! It's supposed to be a location description, and this is merely a DWARF expression that evaluates to the index of the vtable slot. The description of the attribute says that address of an object of the enclosing type is pushed onto the expression stack, so there really ought to be a DW_OP_deref to get the vtable pointer on the stack, followed by a DW_OP_constu with a byte offset, and a DW_OP_add to compute the final location.

Proposal

There's no clean way to legitimize what GCC and LLVM are generating, and there is no compelling reason to require a location expression for locating vtable slots when an index is sufficient. I propose to deprecate the misused attribute and replace it with a new one that provides the simple slot index.

In Section 2.2, "Attribute Types," in Table 2.2, add "(deprecated)" to the row for DW_AT_vtable_elem_location, and add a new row:

Attribute Usage
DW_AT_vtable_elem_index Virtual function vtable slot index

In Section 5.7.8, "Member Function Entries," replace the sixth paragraph:

An entry for a virtual function also has a DW_AT_vtable_elem_location attribute whose value contains a location description yielding the address of the slot for the function within the virtual function table for the enclosing class. The address of an object of the enclosing type is pushed onto the expression stack before the location description is evaluated.

with:

An entry for a virtual function also has a DW_AT_vtable_elem_index attribute whose value contains a constant, which is the zero-based index of the slot for the function within the virtual table for the enclosing class.

The DW_AT_vtable_elem_location attribute from earlier versions of DWARF is deprecated.

In Section 7.5.4, "Attribute Encodings," Table 7.5, replace DW_AT_vtable_elem_location with a note that it has been deprecated, and add a new row:

Attribute name Value Classes
DW_AT_vtable_elem_index TBD constant

In Section 7.31, "Type Signature Computation," Table 7.32, replace DW_AT_vtable_elem_location with DW_AT_vtable_elem_index.

In Appendix A, "Attributes by Tag," Table 7.32, Table A.1, in the row for DW_TAG_subprogram, replace DW_AT_vtable_elem_location with DW_AT_vtable_elem_index.


2025-09-24: Revised. Deprecate DW_AT_vtable_elem_location and add DW_AT_vtable_elem_index.

2025-10-06: Revised. Added changes to Tables 7.32 and A.1.

2025-10-13: Accepted (changed title).