Decompiler Overview
Ghidra’s decompiler converts x86 assembly to C pseudocode by:- Lifting machine code to P-CODE intermediate representation
- Analysis to recover control flow (loops, branches, switches)
- Type inference from usage patterns and API signatures
- Simplification to collapse redundant operations
Reading Decompiled Code
Function Signature
- Address comment tracks original location
- Return type inferred from assembly (
voidif noeaxusage) - Parameters reconstructed from stack/register access
Local Variables
Ghidra generates names likelocal_XX based on stack offset:
Global Access
Ghidra represents globals asDAT_ addresses:
- Base address:
0x004908d4 - Stride:
0x360bytes per entry - Type:
floatat offset0x00
Pointer Arithmetic
Decompiler shows raw pointer math:projectile_pool[local_e8].pos_x (offset 0x08) and add vel_x * delta_time.
Common Patterns
Pool Iteration
Switch Statement
if statements depending on compiler optimization.
Function Calls
- Parameters passed via stack or registers (
cdecl,fastcall,thiscall) - Return value in
eax(integers) orst(0)(floats)
Float Casts
Floating-point math may show explicit casts:float32 explicitly to match original precision.
Identifying Data Structures
Struct Access
Repeated offsets reveal struct layout:Array-of-Structs
Structure-of-Arrays
Control Flow
Loops
While loop:Branches
je, jne, jg).
Nested Conditions
Deeply nestedif statements may indicate state machines or complex branching logic:
Type Inference Challenges
Ambiguous Types
Without debug symbols, Ghidra guesses types from context:Pointer Confusion
Float vs Int
Memory operations may show wrong type:fadd, fmul) indicates float.
Working with Decompiled Output
Keep Original Addresses
Preserve address comments for cross-referencing:LAB_00420c45 map to assembly locations.
Annotate with Evidence
Add comments linking to runtime captures:Split Complex Functions
Large functions (1000+ lines) benefit from extraction:work/ directory for detailed annotation.
Example: Projectile Update
Let’s walk through a real decompiled function:Raw Decompile
After Symbol Recovery
With Type Definitions
Related Pages
Ghidra Workflow
Complete static analysis process
Struct Recovery
Reconstructing data structures
Frida Capture
Validating decompiled logic with runtime data