diff options
| author | Glenn Smith <glenn@vector35.com> | 2024-03-07 21:32:43 -0500 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2024-03-07 21:32:43 -0500 |
| commit | 4620519194636807511824023284542848735054 (patch) | |
| tree | 79f124def81e468f1e83a305174725ca1ff34fa0 /python/lowlevelil.py | |
| parent | 72cc84aabd188e4add6e5012df86414b65d95bc8 (diff) | |
Python: More IL expr functions
- XLILFunction.get_expr: Get an expr by index
- XLILFunction.copy_expr: Copy an IL expr to a new expr
- XLILInstruction.raw_operands: the straight integers from the core
- Extra accessors on XLILLabel
Diffstat (limited to 'python/lowlevelil.py')
| -rw-r--r-- | python/lowlevelil.py | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/python/lowlevelil.py b/python/lowlevelil.py index 16ffbd88..02586113 100644 --- a/python/lowlevelil.py +++ b/python/lowlevelil.py @@ -69,6 +69,26 @@ class LowLevelILLabel: else: self.handle = handle + @property + def ref(self) -> bool: + return self.handle[0].ref + + @ref.setter + def ref(self, value): + self.handle[0].ref = value + + @property + def resolved(self) -> bool: + return self.handle[0].resolved + + @property + def operand(self) -> InstructionIndex: + return InstructionIndex(self.handle[0].operand) + + @operand.setter + def operand(self, value: InstructionIndex): + self.handle[0].operand = int(value) + @dataclass(frozen=True) class ILRegister: @@ -671,6 +691,11 @@ class LowLevelILInstruction(BaseILInstruction): return result @property + def raw_operands(self) -> OperandsType: + """Raw operand expression indices as specified by the core structure (read-only)""" + return self.instr.operands + + @property def operands(self) -> List[LowLevelILOperandType]: """Operands for the instruction""" return list(map(lambda x: x[1], self.detailed_operands)) @@ -3232,7 +3257,7 @@ class LowLevelILFunction: def __hash__(self): return hash(ctypes.addressof(self.handle.contents)) - def __getitem__(self, i:ExpressionIndex) -> LowLevelILInstruction: + def __getitem__(self, i: InstructionIndex) -> LowLevelILInstruction: if isinstance(i, slice) or isinstance(i, tuple): raise IndexError("expected integer instruction index") if i < -len(self) or i >= len(self): @@ -3725,10 +3750,39 @@ class LowLevelILFunction: """ ``get_expr_count`` gives a the total number of expressions in this IL function - You can use this to enumerate all expressions in conjunction with LowLevelILInstruction.create() + You can use this to enumerate all expressions in conjunction with :py:func:`get_expr` + + .. warning :: Not all IL expressions are valid, even if their index is within the returned value from this, + they might not be used by the function and might not contain properly structured data. + + :return: The number of expressions in the function """ return core.BNGetLowLevelILExprCount(self.handle) + def get_expr(self, index: ExpressionIndex) -> Optional[LowLevelILInstruction]: + """ + ``get_expr`` retrieves the IL expression at a given expression index in the function. + + .. warning :: Not all IL expressions are valid, even if their index is within the bounds of the function, + they might not be used by the function and might not contain properly structured data. + + :param index: Index of desired expression in function + :return: A LowLevelILInstruction object for the expression, if it exists. Otherwise, None + """ + if index >= self.get_expr_count(): + return None + + return LowLevelILInstruction.create(self, index) + + def copy_expr(self, original: LowLevelILInstruction) -> ExpressionIndex: + """ + ``copy_expr`` adds an expression to the function which is equivalent to the given expression + + :param LowLevelILInstruction original: the original IL Instruction you want to copy + :return: The index of the newly copied expression + """ + return self.expr(original.operation, original.raw_operands[0], original.raw_operands[1], original.raw_operands[2], original.raw_operands[3], original.size, original.flags) + def replace_expr(self, original: InstructionOrExpression, new: InstructionOrExpression) -> None: """ ``replace_expr`` allows modification of expressions but ONLY during lifting. |
