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

Platform:Itanium C++ ABI/RTTI and vtables

From ReverseEngineering.wiki

Layout of Itanium-ABI vtables (virtual-base offsets, offset-to-top, type_info pointer, then function pointers at the address point), VTTs and construction vtables, and of the type_info objects (__class_type_info, __si_class_type_info, __vmi_class_type_info with __base_class_type_info entries, __pbase_type_info) that name every polymorphic class even in stripped binaries.

Vtables

An object of a polymorphic class starts with a pointer to the address point of its vtable; the words before it are fixed:

Offset from address point Content
-(2+n+1) pointers ... virtual-base offsets (one per virtual base, nearest first), vcall offsets for virtual thunks
-2 pointers offset-to-top: distance from this vptr to the start of the complete object (0 in the primary vtable, negative in secondary vtables)
-1 pointer pointer to the class's type_info (0 when compiled with -fno-rtti)
0 ... virtual function pointers in declaration order, base classes' slots first

The symbol _ZTV<class> labels the start of the whole group, so the address point of the primary vtable is _ZTV + 2 pointers when the class has no virtual bases (119 of 124 _ZTV symbols in an Android ARM libc++ build, 187 of 214 in a MinGW x86-64 libstdc++ build; the others have virtual-base offsets in front). Constructors store &_ZTV... + 16 (x86-64) into the object: lea rax, [rip+_ZTVSt9exception+16]; mov [rdi], rax.

Observed in libstdc++ (MinGW x86-64, exported symbols): _ZTVSt13runtime_error = 0, &_ZTISt13runtime_error, ~runtime_error() D1, ~runtime_error() D0, what(). With multiple inheritance the group holds one more vtable per base that needs one, each with its negative offset-to-top and calls through thunks (_ZThn16_... non-virtual thunk: this -= 16, _ZTv0_n24_... virtual thunk: this += *(vptr - 24)). _ZTVSi (std::istream, virtual base std::basic_ios at +16): virtual-base offset 16, offset-to-top 0, &typeinfo, D1, D0 at the address point, then a secondary vtable for the virtual base with offset-to-top -16 and the virtual thunks _ZTv0_n24_NSiD1Ev, _ZTv0_n24_NSiD0Ev.

VTT (_ZTT): for classes with virtual bases, an array of vtable pointers handed to base-object constructors; it points into the class's own vtable group and into construction vtables _ZTC<derived><offset>_<base> (_ZTTSd starts with _ZTVSd+24, _ZTCSd0_Si+24, ..., _ZTCSd16_So+24).

Pure virtual slots point to __cxa_pure_virtual (deleted virtual functions to __cxa_deleted_virtual); GCC left both destructor slots 0 in the vtables of abstract classes in libstdc++ (std::error_category, std::pmr::memory_resource, the __codecvt_abstract_base and __ctype_abstract_base instantiations).

type_info objects

Every _ZTI<type> object starts with a vptr to one of the runtime's type_info class vtables (+2 pointers, i.e. its address point) and a pointer to the _ZTS name string; the class of the vptr gives the layout of the rest:

vtable referenced Layout after vptr, name Used for
_ZTVN10__cxxabiv117__class_type_infoE nothing classes without bases
_ZTVN10__cxxabiv120__si_class_type_infoE const __class_type_info *__base_type exactly one public, non-virtual base at offset 0
_ZTVN10__cxxabiv121__vmi_class_type_infoE unsigned __flags; unsigned __base_count; __base_class_type_info __base_info[__base_count] any other inheritance
_ZTVN10__cxxabiv119__pointer_type_infoE unsigned __flags; const type_info *__pointee pointer types (PKc)
_ZTVN10__cxxabiv129__pointer_to_member_type_infoE __flags; __pointee; const __class_type_info *__context pointers to members
_ZTVN10__cxxabiv123__fundamental_type_infoE nothing builtin types (only in the runtime)
__enum_type_info, __array_type_info, __function_type_info nothing enums, arrays, function types

__base_class_type_info is {const __class_type_info *__base_type; long __offset_flags;}: bit 0 virtual, bit 1 public, bits 8 and up a signed offset: for a non-virtual base its offset in the object, for a virtual base the (negative) offset in the vtable of the slot holding its virtual-base offset. __vmi_class_type_info::__flags: 1 non-diamond repeated base, 2 diamond-shaped. Example from libstdc++: _ZTISd (std::iostream) = vmi, flags 2, two bases: _ZTISi offset_flags 0x2 (public, +0), _ZTISo 0x1002 (public, +16); _ZTISi = vmi, flags 0, one base _ZTISt9basic_iosIcSt11char_traitsIcEE offset_flags 0xFFFFFFFFFFFFE803 (virtual, public, vtable offset -24).

Type-name strings are the mangled type without _Z, NUL-terminated, usually in .rodata: St9exception, NSt6__ndk112system_errorE, N12_GLOBAL__N_116itanium_demangle8DtorNameE (a class in an anonymous namespace). Comparing type_info objects compares these names (by pointer when the names are unique, by strcmp when the platform merges them by name).

Recovery recipe

  1. Collect every pointer-sized word in the data sections (after applying dynamic relocations for ELF shared objects and PIEs).
  2. A word that points to a string which parses as a mangled type is a type_info's __type_name; the type_info starts one pointer earlier.
  3. Classify it by its vptr (symbol of the relocation, or an export) or, in a stripped file, by shape: third word pointing to another type_info = si; flags <= 3, count >= 1 and base entries pointing to type_infos = vmi; a name starting with P = pointer.
  4. A location holding the type_info's address with 0 or a small negative number one word before it, and a code pointer one word after it, is the type_info slot of a vtable; the address point is one word later.

Code:Itanium C++ ABI/itanium_rtti_scan (Python) implements this. On the test files it found every non-fundamental _ZTI symbol (148/148 in an ARM libc++ test library with full symbols, 182/182 of the exported ones in its stripped twin, 291/291 in MinGW libstdc++-6.dll) with no wrong name, and an address point in every _ZTV symbol but one (123/124, 111/111, 214/214).

Related pages

Pitfalls

  • In RELA files (x86-64, AArch64) the type_info and vtable words are 0 in the file; read them from .rela.dyn. Imported vptrs are R_X86_64_64/R_AARCH64_ABS64 relocations against the __cxxabiv1 vtable symbol with addend 2 pointers. In MIPS64 files they are combined R_MIPS_REL32/R_MIPS_64 relocations with the addend 16 in place.
  • On Win64 (LLP64) __offset_flags is 8 bytes in GCC/MinGW and Clang/MinGW output even though long is 4 bytes there (libstdc++ uses long long under _GLIBCXX_LLP64).
  • Two vtables can be adjacent with no gap, and catch-clause type tables also hold type_info addresses after zero words: require a code pointer in the first slot (or accept 0 only for abstract classes in read-only data).
  • Classes compiled with -fno-rtti have a 0 in slot -1 and no type_info; their vtables can only be found through constructors.

Evidence & references

  • llvm-project 4bd435d59f89ded8e537e5c6e0b560ad89518c3f libcxxabi/src/private_typeinfo.h lines 134-251; clang/lib/CodeGen/ItaniumCXXABI.cpp lines 4215-4268 (offset_flags encoding and the LLP64 long long rule).
  • gcc-mirror/gcc 9e5dc38543418730291b90de2d03263a60ce5b8f libstdc++-v3/libsupc++/cxxabi.h lines 270-294 (__pbase_type_info masks), 374-407 (__base_class_type_info, long long on LLP64), 541-564 (__vmi_class_type_info flags).
  • Byte dumps of the exported _ZTV, _ZTI and _ZTT objects of a MinGW-w64 GCC x86-64 libstdc++-6.dll, and of pyelftools' public-domain test files at commit 5a04389392e25d8128e1acb59828a0a85be791b1.