Platform:Microsoft Visual C++ ABI/RTTI
Layout of MSVC's run-time type information: the TypeDescriptor (type_info object with the decorated name), the CompleteObjectLocator found at vftable[-1], the ClassHierarchyDescriptor and the BaseClassArray of BaseClassDescriptors, for x86 (absolute pointers) and x64/ARM64 (RVAs).
Overview
With /GR (the default) cl.exe emits these records for each polymorphic class. Each has a decorated symbol name, shown by MAP files and PDBs and decoded by undname.exe:
| Record | Symbol | undname output |
|---|---|---|
| TypeDescriptor | ??_R0?AUDuck@zoo@@@8 |
struct zoo::Duck `RTTI Type Descriptor' |
| BaseClassDescriptor | ??_R1A@?0A@EA@Animal@zoo@@8 |
zoo::Animal::`RTTI Base Class Descriptor at (0,-1,0,64)' |
| BaseClassArray | ??_R2Duck@zoo@@8 |
zoo::Duck::`RTTI Base Class Array' |
| ClassHierarchyDescriptor | ??_R3Duck@zoo@@8 |
zoo::Duck::`RTTI Class Hierarchy Descriptor' |
| CompleteObjectLocator | ??_R4Duck@zoo@@6BSwimmer@1@@ |
const zoo::Duck::`RTTI Complete Object Locator'{for `zoo::Swimmer'} |
| vftable | ??_7Duck@zoo@@6BSwimmer@1@@ |
const zoo::Duck::`vftable'{for `zoo::Swimmer'} |
The four numbers in a BaseClassDescriptor's name are its mdisp, pdisp, vdisp and attributes fields (encoding: Platform:Microsoft Visual C++ ABI/Name decoration#Numbers), so the linker merges identical descriptors.
In the tables below, ref is a 32-bit field holding an absolute VA on x86 and an RVA on x64/ARM64. Offsets are the same on both except where noted.
TypeDescriptor
A type_info object, in .data.
| Offset | Size | Type | Name | Notes |
|---|---|---|---|---|
| 0x00 | 4/8 | void* |
pVFTable | always ??_7type_info@@6B@, the type_info vftable
|
| 4/8 | 4/8 | void* |
spare | 0 in the file; the runtime caches the undecorated name here |
| 8/16 | n+1 | char[] |
name | .?AV<class>@@, .?AU<struct>@@, .?AT<union>@@, .?AW4<enum>@@, or a type code for other types (.H int, .N double, .PEAD char* on x64)
|
CompleteObjectLocator
One per vftable; vftable[-1] points to it (a full pointer on both architectures).
| Offset | Size | Type | Name | Notes |
|---|---|---|---|---|
| 0x00 | 4 | uint32_t |
signature | 0 = x86 (absolute pointers), 1 = x64/ARM64 (RVAs) |
| 0x04 | 4 | uint32_t |
offset | offset of this vfptr in the complete object |
| 0x08 | 4 | uint32_t |
cdOffset | constructor displacement (vtordisp) offset, else 0 |
| 0x0C | 4 | ref | pTypeDescriptor | TypeDescriptor of the complete class |
| 0x10 | 4 | ref | pClassDescriptor | ClassHierarchyDescriptor |
| 0x14 | 4 | uint32_t |
pSelf | x64/ARM64 only: RVA of this locator (image base = its address - pSelf) |
Size 20 bytes on x86, 24 on x64.
ClassHierarchyDescriptor
| Offset | Size | Type | Name | Notes |
|---|---|---|---|---|
| 0x00 | 4 | uint32_t |
signature | 0 |
| 0x04 | 4 | uint32_t |
attributes | 1 = multiple inheritance, 2 = virtual inheritance in a branching hierarchy, 4 = ambiguous bases (clang notes cl.exe does not compute bit 4 reliably) |
| 0x08 | 4 | uint32_t |
numBaseClasses | entries in the base class array, including the class itself |
| 0x0C | 4 | ref | pBaseClassArray | array of refs to BaseClassDescriptors |
BaseClassArray: numBaseClasses refs followed by one zero entry. The first entry is the class itself, then every base in depth-first pre-order, one entry per path, so a virtual base reached twice appears twice (as the same descriptor). Observed: for struct Diamond : VLeft, VRight with VLeft : virtual Base and VRight : virtual Base the array is Diamond, VLeft, Base, VRight, Base, 0 and the attributes are 3; VLeft alone (one virtual base, no branching) has attributes 0.
BaseClassDescriptor
| Offset | Size | Type | Name | Notes |
|---|---|---|---|---|
| 0x00 | 4 | ref | pTypeDescriptor | the base's TypeDescriptor |
| 0x04 | 4 | uint32_t |
numContainedBases | how many of the following array entries are this base's own bases |
| 0x08 | 4 | int32_t |
mdisp | offset of the base inside the object (or inside its virtual base) |
| 0x0C | 4 | int32_t |
pdisp | offset of the vbptr, -1 when the base is not reached through a virtual base |
| 0x10 | 4 | int32_t |
vdisp | byte offset in the vbtable of the virtual base's displacement |
| 0x14 | 4 | uint32_t |
attributes | 0x01 and 0x08 private on the path, 0x02 ambiguous, 0x04 private, 0x10 virtual base, 0x40 has pClassDescriptor |
| 0x18 | 4 | ref | pClassDescriptor | the base's own ClassHierarchyDescriptor |
The base is at this + mdisp when pdisp == -1; otherwise at this + pdisp + *(int32_t*)(*(char**)(this + pdisp) + vdisp) + mdisp. Ordinary public bases have attributes 0x40, a public virtual base 0x50.
Runtime functions
typeid(*p)calls__RTtypeid(void*), which readsvftable[-1], then the locator's TypeDescriptor.dynamic_castcallsPVOID __RTDynamicCast(PVOID inptr, LONG VfDelta, PVOID SrcType, PVOID TargetType, BOOL isReference)with two TypeDescriptor addresses, so each call site names both classes;dynamic_cast<void*>calls__RTCastToVoid(PVOID).
Worked example
In the x64 test program struct zoo::Duck : Animal, Swimmer (Animal subobject 16 bytes) has two vftables: one for the Animal part (locator offset 0) and one for the Swimmer part (locator offset 16; 8 on x86). Both locators share one ClassHierarchyDescriptor (attributes 1, 3 entries: Duck, Animal at mdisp 0, Swimmer at mdisp 16). Raw x64 locator: 01 00 00 00 00 00 00 00 00 00 00 00 F0 FE 09 00 88 4E 09 00 58 4E 09 00 (signature 1, offset 0, cdOffset 0, TypeDescriptor RVA 0x9FEF0, hierarchy RVA 0x94E88, own RVA 0x94E58, which is where the MAP file puts ??_R4Duck@zoo@@6BAnimal@1@@).
Related pages
- Combining RTTI with vtables, Qt moc data and protobuf descriptors: Technique:Recovering C++ class metadata.
- The Itanium counterpart: Platform:Itanium C++ ABI/RTTI and vtables.
Evidence & references
- clang
MicrosoftCXXABI.cpp(llvm-project 8cdfc2de): RTTI types at lines 505-572, overview comment 3613-3641, BaseClassDescriptor flags 3662-3668, hierarchy flags 3735-3739, field values 3833-3840 (hierarchy), 3854-3857 (array padding), 3909-3920 (base descriptor), 3953-3962 (locator), 4037-4072 (TypeDescriptor). - clang
MicrosoftMangle.cpp:??_R04231,??_R14335-4345,??_R24348,??_R34357,??_R44364-4383. - Wine
dlls/msvcrt/cxx.h(7298a818):rtti_object_locator,rtti_object_hierarchy,rtti_base_descriptorin pointer and RVA forms, lines 258-331. - Checked with Code:Microsoft Visual C++ ABI/msvc_rtti_scan (Python) against the MAP files of x86 and x64 test builds: 28 of 28 vftables found at the
??_7addresses in each.