Overview
Binary Ninja provides multiple IL representations for analysis. These examples show how to:- Parse LLIL and MLIL instruction trees
- Use visitor patterns for expression traversal
- Extract operands and constants from IL
- Use templated accessors for type-safe IL access
Low Level IL Parser (C++)
This example parses LLIL instructions and demonstrates generic tree traversal.Complete Source Code
Medium Level IL Parser (C++)
MLIL provides a higher-level representation with variables and types.Key Differences from LLIL
Key Concepts Explained
For C++ headless applications, you must initialize the plugin system before using Binary Ninja APIs.
Ref<LowLevelILFunction> il = func->GetLowLevelIL();
Ref<MediumLevelILFunction> mlil = func->GetMediumLevelIL();
for (auto& operand : instr.GetOperands())
{
switch (operand.GetType())
{
case ExprLowLevelOperand:
PrintILExpr(operand.GetExpr(), indent);
break;
// ...
}
}
IL instructions form expression trees. Recursively traverse using
GetOperands() to access child expressions.instr.VisitExprs([&](const LowLevelILInstruction& expr) {
if (expr.operation == LLIL_CONST)
{
printf("Found constant 0x%" PRIx64 "\n", expr.GetConstant());
return false; // Stop traversing this branch
}
return true; // Continue to subexpressions
});
true to continue traversing subexpressionsfalse to stop traversing current branchif (expr.operation == LLIL_LOAD)
{
auto sourceExpr = expr.GetSourceExpr<LLIL_LOAD>();
if (sourceExpr.operation == LLIL_CONST_PTR)
{
uint64_t addr = sourceExpr.GetConstant<LLIL_CONST_PTR>();
}
}
Templated accessors provide compile-time type safety for accessing specific operands based on operation type.
Building and Running
LLIL Parser
MLIL Parser
Expected Output
Use Cases
- Custom Analysis - Build analysis passes over IL
- Pattern Matching - Find specific code patterns
- Optimization Detection - Identify compiler optimizations
- Vulnerability Research - Search for dangerous patterns
- IL Understanding - Learn Binary Ninja’s IL design
Related Examples
- Custom IL Visitor - Advanced visitor implementations
- Function Iteration - Basic IL access patterns
- Workflow Extension - IL-based analysis workflows