Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Technique:Recovering C++ class metadata

From ReverseEngineering.wiki
Technique
On this wiki Facts · Code · Notes · Topics

How to get class names, inheritance and virtual-function tables out of a stripped C++ binary: compiler RTTI (MSVC and Itanium), vtables, Qt moc meta-object tables and protobuf descriptors, and how to combine them.

Overview

A C++ binary without symbols still carries several independent sources of class information. None needs decompilation; all are data tables that can be found by scanning for pointers and strings. Use as many as apply and cross-check: RTTI gives names and hierarchy, vtables give virtual methods, framework tables give member names and types.

Source Gives Present when Page
MSVC RTTI decorated class name (.?AVFoo@@), bases, offsets, vftable of each class polymorphic classes, /GR (default) Platform:Microsoft Visual C++ ABI/RTTI
Itanium RTTI mangled class name (3Foo, N2ns3BarE), single/multiple/virtual bases polymorphic classes, no -fno-rtti Platform:Itanium C++ ABI/RTTI and vtables
Vtables virtual method addresses, slot order, destructors, __cxa_pure_virtual/_purecall slots any class with virtual functions same pages
Qt moc data class name, signals, slots, properties, enums, Q_CLASSINFO, parent class Q_OBJECT/Q_GADGET classes Library:Qt/Meta-object data
Protobuf descriptors message and field names, types, numbers, services any generated protobuf code Library:Protocol Buffers/Embedded descriptors
Exception type tables classes that are thrown or caught catch clauses Platform:Itanium C++ ABI/LSDA, Platform:Itanium C++ ABI/Exception handling, Platform:Microsoft Visual C++ ABI/C++ exception handling

Workflow

  1. Identify the ABI from the file format and strings: PE with .?AV strings = MSVC; ELF/Mach-O with _Z-style names or St9exception-style type names = Itanium (see Platform:Itanium C++ ABI). A MinGW PE is Itanium.
  2. Find the type names. MSVC: search for the ASCII .?AV (class) and .?AU (struct) prefixes; each is the name field of a TypeDescriptor (pVFTable, spare, then the string). Itanium: the type-name strings are mangled types without _Z (7MyClass, N3foo3BarE, St9exception), NUL-terminated in .rodata; the type_info object holds a pointer to it as its second word.
  3. Walk to the hierarchy. MSVC: references to the TypeDescriptor from a CompleteObjectLocator lead to the ClassHierarchyDescriptor and its BaseClassArray (every base, with member displacement). Itanium: the vptr of the type_info object tells its kind (__class_type_info none, __si_class_type_info one base, __vmi_class_type_info list of bases with offset_flags).
  4. Find the vtable. MSVC: a pointer to the COL stored directly before the first virtual-function slot (vftable[-1]). Itanium: the word before the address point is the type_info pointer, the one before that the offset-to-top. Every constructor stores the address point into the object, so the vtable's cross-references are the constructors and destructors of the class.
  5. Name the virtual methods. Slot order is declaration order, base slots first; Itanium has two destructor entries (complete D1 and deleting D0) where MSVC has one (the scalar deleting destructor). Slots pointing to __cxa_pure_virtual / _purecall are pure virtual.
  6. Add framework metadata. For Qt classes the moc QMetaObject supplies method names and signatures that map to the indices handled by qt_static_metacall; for protobuf messages the descriptor table gives the field list of each generated class.
  7. Propagate. Name every function that stores a known vtable (constructor), every function that loads a vptr and calls through it (virtual call sites), and the base-class constructor chain.

Pitfalls

  • Classes without virtual functions have no RTTI and no vtable. Classes built with /GR- or -fno-rtti have a null RTTI slot; use vtable-to-constructor links only.
  • The RTTI name is the mangled type. Template arguments appear in the name on both ABIs; decode with the platform's demangler (Platform:Itanium C++ ABI/Name mangling, Platform:Microsoft Visual C++ ABI/Name decoration).
  • MSVC x64 RTTI records use 32-bit image-relative offsets; Itanium records on RELA targets (x86-64, AArch64) store zero in the file and the pointers only in relocations, so apply the relocations first.
  • Linkers can merge identical vtables and fold identical functions, so two classes may share a slot target.
  • Rust and Go are not C++: see Tool:rustc and Library:Go runtime for their own name tables.

See also

Platform:Microsoft Visual C++ ABI, Platform:Itanium C++ ABI, Code:Itanium C++ ABI/itanium rtti scan (Python).