BlockEncoder¶
- class iced_x86.BlockEncoder¶
Encodes instructions
Encodercan only encode one instruction at a time. This class can encode any number of instructions and can also fix short branches if the target is too far away.It will fail if there’s an instruction with an RIP-relative operand (
[rip+123h]) and the target is too far away. A workaround is to use a new base RIP of the encoded instructions that is close (+/-2GB) to the original location.- Parameters:
- Raises:
ValueError – If bitness is invalid
Examples:
from iced_x86 import * data = b"\x86\x64\x32\x16\xF0\xF2\x83\x00\x5A\x62\xC1\xFE\xCB\x6F\xD3" decoder = Decoder(64, data, ip=0x1234_5678) instrs = [instr for instr in decoder] encoder = BlockEncoder(64) # Add an instruction encoder.add(instrs[0]) # Add more instructions encoder.add_many(instrs[1:]) try: # Encode all added instructions and get the raw bytes raw_data = encoder.encode(0x3456_789A) except ValueError as ex: print("Could not encode all instructions") raise # It has no IP-relative instructions (eg. branches or [rip+xxx] ops) # so the result should be identical to the original code. assert data == raw_data
- add(instruction)¶
Adds an instruction that will be encoded when
BlockEncoder.encodeis called.The input instruction can be a decoded instruction or an instruction created by the user, eg. Instruction.create*() methods.
- Parameters:
instruction (Instruction) – Next instruction to encode
- add_many(instructions)¶
Adds instructions that will be encoded when
BlockEncoder.encodeis called.- Parameters:
instructions (List[Instruction]) – Next instructions to encode
- encode(rip)¶
Encodes all instructions added by
BlockEncoder.add/BlockEncoder.add_manyand returns the raw bytes- Parameters:
rip (int) – (
u64) Base IP of all encoded instructions- Returns:
All encoded instructions
- Return type:
- Raises:
ValueError – If one or more instructions couldn’t be encoded