Data Structure Recovery
Reverse Engineering Basics · Reverse Engineering track
4 min read
In this module
Beyond Control Flow
Reconstructing control flow shows what a function does step by step. Understanding a whole program needs more: you have to recover its data structures too. The structs, arrays, linked lists, and objects that move data around. Compiled code carries no type information (just bytes at addresses), so the only way in is to infer the shapes from how memory gets accessed.
Recognizing Structs
When code touches a base pointer plus a series of constant offsets, you're looking at struct field accesses:
mov eax, [rbx] ; 4-byte field at offset 0mov rcx, [rbx + 0x8] ; 8-byte field at offset 8mov edx, [rbx + 0x10] ; 4-byte field at offset 16The offsets and register widths sketch out the struct layout for you. Offset 0 is read as a 4-byte integer and offset 8 as an 8-byte value; if rcx is later dereferenced, it's a pointer. Nothing touches offsets 4 to 7, which fits the padding an 8-byte pointer needs to stay aligned (or a field this function never uses). You've already started defining the struct:
struct recovered { int field_0; /* offset 0 */ /* 4 bytes of padding */ void *field_8; /* offset 8 */ int field_10; /* offset 16 (0x10) */}; /* sizeof == 24, rounded up to 8-byte alignment */Here's that layout drawn to scale, each field paired with the instruction that gave it away and the padding in a band of its own:
struct recovered (x86-64)
| Offset | Field | Size | Notes |
|---|---|---|---|
| 0x0 (0) | int field_0 | 4 | mov eax, [rbx]: a 4-byte read at offset 0 |
| 0x4 (4) | padding | 4 | |
| 0x8 (8) | void* field_8 | 8 | mov rcx, [rbx + 0x8]: an 8-byte read, later dereferenced, so a pointer |
| 0x10 (16) | int field_10 | 4 | mov edx, [rbx + 0x10]: a 4-byte read at offset 16 |
| 0x14 (20) | padding | 4 |
sizeof = 24 bytes, 8 of them padding; alignment 8.
Watch the same offset pattern appear across multiple functions and your confidence climbs. The base register (RBX here) holds the struct pointer, which ties straight back to the pointer work in Addresses & Pointers.
Below are the 24 bytes of one struct recovered as they'd sit in memory. Select 4 bytes at 0x00, then 8 bytes at 0x08, and read off the fields. Then open the array view and pick int32_t: the same bytes become six ints, and the pointer splits into two halves that mean nothing on their own. Change a byte and watch which readings move.
struct recovered in memory
An x86-64 struct: int field_0 at 0x00, 4 padding bytes, a pointer at 0x08, int field_10 at 0x10, then 4 bytes of tail padding. The pointer value is made up.
Click a byte to select 4 bytes starting there.
The ASCII column shows printable characters and a dot for every other byte.
Select some bytes to see what they could mean.
Read every byte as an array
Little-endian, as on x86 and most ARM systems.
| index | offset | value |
|---|---|---|
| [0] | 0x00 | 1000 |
| [1] | 0x04 | 0 |
| [2] | 0x08 | 1581001232 |
| [3] | 0x0c | 32765 |
| [4] | 0x10 | 4294967294 |
| [5] | 0x14 | 0 |
Compilers insert padding bytes between struct fields to satisfy alignment requirements (Variables & Data Types). A struct with a char (1 byte) followed by an int (4 bytes) will have 3 bytes of padding between them, making the int start at offset 4. Seeing unexpected gaps in field offsets is a sign of alignment padding, not missing fields.
Recognizing Arrays
Arrays show up as memory accesses with a scaled index: a base address plus a register multiplied by the element size. lea and indexed addressing modes put this on display:
mov eax, [rbx + rcx*4] ; array of 4-byte intsmov al, [rbx + rcx] ; array of bytes (chars)mov rax, [rbx + rcx*8] ; array of 8-byte pointersThe scale factor (1, 2, 4, or 8) gives away the element size for free. Spot a loop that bumps an index register and dereferences memory with a scaled offset, and you've caught array iteration in the act.
Linked Lists and Dynamic Structures
Linked lists betray themselves with repeated pointer dereferences inside a loop. The shape is distinctive: load a pointer from memory, NULL-check it, follow it to the next node. Every node access reuses the same offset set (the struct fields), and one of those offsets holds the next-pointer.
Vtables and C++ Objects
Compiled C++ brings vtables (virtual method tables) into the picture. Any object with virtual methods carries a hidden pointer at offset 0 that points to a table of function pointers. A virtual method call looks like:
mov rax, [rdi] ; load vtable pointer from objectcall [rax + 0x10] ; call 3rd virtual method (offset 0x10 = 16 = index 2 * 8)Finding vtables pays off: every entry is a method address, and the table layout reveals class hierarchy. If class B's vtable shares the first three entries with class A's and tacks more on the end, B almost certainly inherits from A.
Strings and Constants
Strings are the lowest-hanging fruit in any binary. Look for references into .rodata (read-only data). Error messages, format strings, debug output: most of it survives compilation unchanged. A function pulling on the string "failed to open file" is dealing with file I/O, and you didn't have to read a single instruction to know that. String cross-references are often the fastest way to find your bearings in an unfamiliar binary.