pub struct Encoder { /* private fields */ }
Expand description
Encodes instructions decoded by the decoder or instructions created by other code.
See also BlockEncoder
which can encode any number of instructions.
use iced_x86::*;
// xchg ah,[rdx+rsi+16h]
let bytes = b"\x86\x64\x32\x16";
let mut decoder = Decoder::with_ip(64, bytes, 0x1234_5678, DecoderOptions::NONE);
let instr = decoder.decode();
let mut encoder = Encoder::new(64);
match encoder.encode(&instr, 0x5555_5555) {
Ok(len) => assert_eq!(4, len),
Err(err) => panic!("{}", err),
}
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(buffer, vec![0x86, 0x64, 0x32, 0x16]);
Implementations§
source§impl Encoder
impl Encoder
sourcepub fn encode(
&mut self,
instruction: &Instruction,
rip: u64
) -> Result<usize, IcedError>
pub fn encode( &mut self, instruction: &Instruction, rip: u64 ) -> Result<usize, IcedError>
Encodes an instruction and returns the size of the encoded instruction
Errors
Returns an error if it failed to encode the instruction.
Arguments
instruction
: Instruction to encoderip
:RIP
of the encoded instruction
Examples
use iced_x86::*;
// je short $+4
let bytes = b"\x75\x02";
let mut decoder = Decoder::with_ip(64, bytes, 0x1234_5678, DecoderOptions::NONE);
let instr = decoder.decode();
let mut encoder = Encoder::new(64);
// Use a different IP (orig rip + 0x10)
match encoder.encode(&instr, 0x1234_5688) {
Ok(len) => assert_eq!(2, len),
Err(err) => panic!("{}", err),
}
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(buffer, vec![0x75, 0xF2]);
sourcepub fn write_u8(&mut self, value: u8)
pub fn write_u8(&mut self, value: u8)
Writes a byte to the output buffer
Arguments
value
: Value to write
Examples
use iced_x86::*;
let mut encoder = Encoder::new(64);
let instr = Instruction::with2(Code::Add_r64_rm64, Register::R8, Register::RBP)?;
encoder.write_u8(0x90);
match encoder.encode(&instr, 0x5555_5555) {
Ok(len) => assert_eq!(3, len),
Err(err) => panic!("{}", err),
}
encoder.write_u8(0xCC);
// We're done, take ownership of the buffer
let buffer = encoder.take_buffer();
assert_eq!(buffer, vec![0x90, 0x4C, 0x03, 0xC5, 0xCC]);
sourcepub fn take_buffer(&mut self) -> Vec<u8> ⓘ
pub fn take_buffer(&mut self) -> Vec<u8> ⓘ
Returns the buffer and initializes the internal buffer to an empty vector. Should be called when
you’ve encoded all instructions and need the raw instruction bytes. See also set_buffer()
.
sourcepub fn set_buffer(&mut self, buffer: Vec<u8>)
pub fn set_buffer(&mut self, buffer: Vec<u8>)
Overwrites the buffer with a new vector. The old buffer is dropped. See also take_buffer()
.
sourcepub fn get_constant_offsets(&self) -> ConstantOffsets
pub fn get_constant_offsets(&self) -> ConstantOffsets
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.
sourcepub const fn prevent_vex2(&self) -> bool
pub const fn prevent_vex2(&self) -> bool
Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding
sourcepub fn set_prevent_vex2(&mut self, new_value: bool)
pub fn set_prevent_vex2(&mut self, new_value: bool)
Disables 2-byte VEX encoding and encodes all VEX instructions with the 3-byte VEX encoding
Arguments
new_value
: new value
sourcepub const fn vex_wig(&self) -> u32
pub const fn vex_wig(&self) -> u32
Value of the VEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
sourcepub fn set_vex_wig(&mut self, new_value: u32)
pub fn set_vex_wig(&mut self, new_value: u32)
Value of the VEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
Arguments
new_value
: new value (0 or 1)
sourcepub const fn vex_lig(&self) -> u32
pub const fn vex_lig(&self) -> u32
Value of the VEX.L
bit to use if it’s an instruction that ignores the bit. Default is 0.
sourcepub fn set_vex_lig(&mut self, new_value: u32)
pub fn set_vex_lig(&mut self, new_value: u32)
Value of the VEX.L
bit to use if it’s an instruction that ignores the bit. Default is 0.
Arguments
new_value
: new value (0 or 1)
sourcepub const fn evex_wig(&self) -> u32
pub const fn evex_wig(&self) -> u32
Value of the EVEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
sourcepub fn set_evex_wig(&mut self, new_value: u32)
pub fn set_evex_wig(&mut self, new_value: u32)
Value of the EVEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
Arguments
new_value
: new value (0 or 1)
sourcepub const fn evex_lig(&self) -> u32
pub const fn evex_lig(&self) -> u32
Value of the EVEX.L'L
bits to use if it’s an instruction that ignores the bits. Default is 0.
sourcepub fn set_evex_lig(&mut self, new_value: u32)
pub fn set_evex_lig(&mut self, new_value: u32)
Value of the EVEX.L'L
bits to use if it’s an instruction that ignores the bits. Default is 0.
Arguments
new_value
: new value (0 or 3)
sourcepub const fn mvex_wig(&self) -> u32
pub const fn mvex_wig(&self) -> u32
Value of the MVEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
sourcepub fn set_mvex_wig(&mut self, new_value: u32)
pub fn set_mvex_wig(&mut self, new_value: u32)
Value of the MVEX.W
bit to use if it’s an instruction that ignores the bit. Default is 0.
Arguments
new_value
: new value (0 or 1)