Reverse Engineering C++ Binaries
Advanced Reverse Engineering Techniques · Advanced Reverse Engineering track
6 min read
In this module
C++ Adds Layers of Complexity
Reversing a C binary is mostly tractable. Functions, structs, control flow — they map onto assembly with reasonable fidelity, and a careful afternoon will get you most of what you need. C++ is a different beast. The OO features land in the binary as a thicket of patterns the compiler bolted together, and reading the original design out of the bytes means learning the patterns first.
Name Mangling
Overloading, namespaces, and templates mean two C++ functions can share the same source name and still need to be different at link time. The compiler bakes the full signature into the linker symbol — that's name mangling. Under the Itanium ABI that GCC and Clang both use, a mangled name opens with _Z, then walks through nested name components and type encodings.
Source: MyNamespace::MyClass::process(int, std::string const&)Mangled: _ZN11MyNamespace7MyClass7processEiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEc++filt will reverse the encoding for you. Stripping removes the static symbol table, but some mangled names survive: the dynamic symbols a shared library exports or imports (.dynsym), and the mangled type names stored as strings for RTTI (more on that below). The unwind tables don't carry names. Sweep .rodata for strings that look like mangled type names; they're a goldmine of type information that the strip pass leaves behind.
Vtable Recovery
When a class has virtual methods, the compiler emits a vtable: an array of function pointers, one per virtual method, in declaration order. Every instance of the class carries a hidden vptr (usually at offset 0) pointing to that vtable. A virtual call compiles down to a fixed three-step dance: load vptr from object, index into vtable, indirect call.
; Virtual call: obj->virtualMethod()mov rax, [rdi] ; load vptr from object (rdi = this pointer)call [rax + 0x10] ; call third virtual method (offset 0x10 = index 2 * 8)Once you've recovered vtables, the class hierarchy falls out almost for free. Every class has its own vtable. If Derived inherits from Base, Derived's vtable opens with the same methods as Base's (sometimes with the pointers overridden), then appends Derived's new virtuals. Diff two vtables and the inheritance shows itself.
In Ghidra, hunt for arrays of function pointers in .data.rel.ro. Those are almost always vtables. Just before the vtable, at negative offsets, you'll find a tiny header: the offset-to-top value (which only matters under multiple inheritance) and a pointer to the RTTI type_info structure.
Here's the whole chain for a small hierarchy, as GCC and Clang lay it out on x86-64 Linux (the Itanium C++ ABI):
struct Base { virtual void a(); virtual void b(); int x; };struct Derived : Base { void b() override; virtual void c(); int y; };Start at the object. Its first 8 bytes are the vptr, and Derived's own field y slots into the 4 bytes of tail padding Base left behind, so the whole object is 16 bytes.
A Derived object
| Offset | Field | Size | Notes |
|---|---|---|---|
| 0x0 (0) | void* vptr | 8 | Points at slot 0 of Derived's vtable, not at its start. |
| 0x8 (8) | int x | 4 | Inherited from Base. |
| 0xc (12) | int y | 4 | Derived's own field. |
16 bytes shown.
The vptr lands in the middle of the vtable symbol (_ZTV7Derived), 16 bytes in. Negative offsets from there hold the header; positive offsets are the slots that call [rax + 0x10] indexes.
vtable for Derived (offsets from the vptr)
| Offset | Field | Size | Notes |
|---|---|---|---|
| -0x10 (-16) | ptrdiff_t offset-to-top | 8 | 0 here. Non-zero only for secondary bases under multiple inheritance. |
| -0x8 (-8) | type_info* typeinfo | 8 | Points at _ZTI7Derived. |
| 0x0 (0) | void* slot 0 | 8 | Base::a, inherited unchanged. |
| 0x8 (8) | void* slot 1 | 8 | Derived::b, the override replaces Base::b. |
| 0x10 (16) | void* slot 2 | 8 | Derived::c, new in Derived. This is [rax + 0x10]. |
40 bytes shown.
Follow the typeinfo pointer and you reach the class name and its parent. With a single public, non-virtual base, the structure is a __si_class_type_info:
typeinfo for Derived (_ZTI7Derived)
| Offset | Field | Size | Notes |
|---|---|---|---|
| 0x0 (0) | void* vptr | 8 | Into the vtable of __cxxabiv1::__si_class_type_info. |
| 0x8 (8) | const char* __name | 8 | Points at "7Derived", the mangled name string (_ZTS7Derived). |
| 0x10 (16) | type_info* __base_type | 8 | Points at _ZTI4Base. Follow these to rebuild the hierarchy. |
24 bytes shown.
If the binary was not compiled with -fno-rtti, it contains Run-Time Type Information. Each class has a type_info structure with the mangled class name and pointers to base class type_info structures. These structures are referenced from vtables and from exception handling tables. Even in stripped binaries, RTTI provides class names and inheritance chains. Look for strings starting with digits (like "7MyClass") in .rodata - these are mangled type names inside type_info.
Exception Handling Structures
On most platforms, C++ exceptions ride the Itanium ABI's zero-cost exception model. The compiler emits .gcc_except_table entries that map PC ranges to catch handlers and cleanup actions, with type_info references hooked up so the runtime can match the thrown type.
Keep the two tables apart. Almost every function gets a .eh_frame entry, because the unwinder needs to step through any frame an exception passes, so having one tells you little. A function that also has a language-specific data area (LSDA) in .gcc_except_table, referenced from its .eh_frame entry, is one with real work to do during unwinding: a try/catch, or locals that need destructors run.
For a reverse engineer, those tables are pure information:
- Functions with an LSDA catch exceptions or have cleanups to run
- Type_info references in catch clauses spell out exactly what they catch
- Cleanup landing pads call destructors during stack unwinding, which tells you which locals were objects with non-trivial destructors
Everything above is the Itanium ABI. MSVC binaries, the norm for Windows C++, do the same jobs differently: mangled names start with ? (?process@MyClass@MyNamespace@@QEAAXHAEBV?$basic_string@...), RTTI type descriptors hold names like .?AVMyClass@@, and exception handling is described by FuncInfo structures reached from the x64 unwind data in .pdata/.xdata. The reasoning carries over; the structures and tools (undname rather than c++filt) change.
Template Instantiation Patterns
C++ templates expand at compile time, one fresh copy per argument set. std::vector<int> and std::vector<std::string> end up as entirely different functions in the binary. You'll see clusters of functions with the same shape but different types and sizes — that's the fingerprint of a template instantiated several ways.
Knowing the common STL container shapes is half the battle. std::vector is a three-pointer struct: begin, end, capacity. std::map is a red-black tree, with left/right/parent pointers and a colour bit. std::unordered_map is a hash table with buckets and chains. Spot the layout, and you've identified the container without needing a single symbol.
Mangled template names encode the template arguments themselves, so even partial symbol recovery (from RTTI or exception tables) often reveals the full parameterised type. A string like St6vectorIiSaIiEE decodes to std::vector<int, std::allocator<int>>.
Practical Approach
A working order of operations for a real C++ binary: demangle whatever symbols survived to recover class and method names. Locate vtables in .data.rel.ro and cross-reference them against RTTI to name the classes. Read constructors carefully — they set up the vptr and write the initial fields, which is where object layouts come from. Use the exception tables to map destructor calls and cleanup order. Pattern-match STL container layouts on what's left. Most of this drudgery is automated by Ghidra's C++ class recovery scripts and IDA's FLIRT signatures for the standard library; learn them and let them do the boring parts.