Encoder¶
- class iced_x86.Encoder(bitness, capacity=0)¶
Encodes instructions decoded by the decoder or instructions created by other code.
See also
BlockEncoderwhich can encode any number of instructions.- Parameters:
- Raises:
ValueError – If bitness is invalid
Examples:
from iced_x86 import * # xchg ah,[rdx+rsi+16h] data = b"\x86\x64\x32\x16" decoder = Decoder(64, data, ip=0x1234_5678) instr = decoder.decode() encoder = Encoder(64) try: instr_len = encoder.encode(instr, 0x5555_5555) assert instr_len == 4 except ValueError as ex: print(f"Failed to encode the instruction: {ex}") raise # We're done, take ownership of the buffer buffer = encoder.take_buffer() assert buffer == b"\x86\x64\x32\x16"
- encode(instruction, rip)¶
Encodes an instruction and returns the size of the encoded instruction
- Parameters:
instruction (Instruction) – Instruction to encode
rip (int) – (
u64)RIPof the encoded instruction
- Returns:
Size of the encoded instruction
- Return type:
- Raises:
ValueError – If it failed to encode the instruction (eg. a target branch / RIP-rel operand is too far away)
Examples:
from iced_x86 import * # je short $+4 data = b"\x75\x02" decoder = Decoder(64, data, ip=0x1234_5678) instr = decoder.decode() encoder = Encoder(64) try: # Use a different IP (orig rip + 0x10) instr_len = encoder.encode(instr, 0x1234_5688) assert instr_len == 2 except ValueError as ex: print(f"Failed to encode the instruction: {ex}") raise # We're done, take ownership of the buffer buffer = encoder.take_buffer() assert buffer == b"\x75\xF2"
- evex_lig¶
(
u8) Value of theEVEX.L'Lbits to use if it’s an instruction that ignores the bits. Default is 0.- Type:
- evex_wig¶
(
u8) Value of theEVEX.Wbit to use if it’s an instruction that ignores the bit. Default is 0.- Type:
- get_constant_offsets()¶
Gets the offsets of the constants (memory displacement and immediate) in the encoded instruction.
The caller can use this information to add relocations if needed.
- Returns:
Offsets and sizes of immediates
- Return type:
- mvex_wig¶
(
u8) Value of theMVEX.Wbit to use if it’s an instruction that ignores the bit. Default is 0.- Type:
- prevent_vex2¶
Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding
- Type:
- take_buffer()¶
Returns the buffer and initializes the internal buffer to an empty array.
Should be called when you’ve encoded all instructions and need the raw instruction bytes.
- Returns:
The encoded instructions
- Return type:
- vex_lig¶
(
u8) Value of theVEX.Lbit to use if it’s an instruction that ignores the bit. Default is 0.- Type:
- vex_wig¶
(
u8) Value of theVEX.Wbit to use if it’s an instruction that ignores the bit. Default is 0.- Type:
- write_u8(value)¶
Writes a byte to the output buffer
- Parameters:
value (int) – (
u8) Value to write
Examples:
from iced_x86 import * # je short $+4 data = b"\x75\x02" decoder = Decoder(64, data, ip=0x1234_5678) instr = decoder.decode() encoder = Encoder(64) # Add a random byte encoder.write_u8(0x90) try: # Use a different IP (orig rip + 0x10) instr_len = encoder.encode(instr, 0x1234_5688) assert instr_len == 2 except ValueError as ex: print(f"Failed to encode the instruction: {ex}") raise # Add a random byte encoder.write_u8(0x90) # We're done, take ownership of the buffer buffer = encoder.take_buffer() assert buffer == b"\x90\x75\xF2\x90"