This commit is contained in:
janis 2026-07-09 14:17:15 +02:00
parent c48c4261da
commit 2f7282577a
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8
2 changed files with 22 additions and 15 deletions

View file

@ -1884,7 +1884,7 @@ encode_inst:
call write_non_zero_bytes
pop rax
shl eax, 6
shr eax, 6
dec eax
cmp eax, 2
jge .imm
@ -1937,11 +1937,11 @@ write_imm:
jmp write_bytes
write_disp:
cmp edi, 0xff
cmp edi, 0xff ; if disp > 0xff, write 4 bytes, else write 1 byte
seta sil
shl sil, 1
inc sil
movzx rsi, sil
movzx esi, sil
lea esi, [rsi + rsi * 2]
inc esi
jmp write_bytes
write_bytes:
@ -1986,8 +1986,8 @@ compute_modrm_sib:
je .done_mem
and eax, 0x37 ; clear mod bits, mod = 00b
cmp dword [rsi + operand.type], KIND_MEM
jne .done_mem
test dword [rsi + operand.type], KIND_MEM
jz .done_mem
cmp dword [rsi + operand.disp], 0
jne .sib ; mod = 01b or 10b
@ -2023,7 +2023,7 @@ compute_modrm_sib:
mov ecx, dword [rsi + operand.index + register.num]
cmp ecx, -1
jne .done_mem
je .done_mem
and ecx, 7
shl ecx, 3 ; index bits
@ -2154,8 +2154,8 @@ struc LabelTable
endstruc
section .data
labels dq 0
label_refs dq 0
labels dq 0, 0
label_refs dq 0, 0
section .text
;; calculates the hash of a byte sequence in $rdi of length $rsi, returning it in $eax
@ -2174,6 +2174,7 @@ fasthash:
.done:
ret
global init_label_tables
init_label_tables:
mov edi, 0x100
mov esi, 8
@ -2199,19 +2200,19 @@ push_lbl_ref_by_hash:
push r12
push r13
mov rbx, [rel label_refs]
lea rbx, [rel label_refs]
mov r13d, esi ; label offset
mov r12d, edi ; label hash
jmp push_lbl_common.hash_in_r12d
;; push label $rdi with length $rsi and offset $rdx into the label ref table
push_lbl_ref:
mov rcx, [rel label_refs]
lea rcx, [rel label_refs]
jmp push_lbl_common
;; push label $rdi with length $rsi and offset $edx into the label table
push_lbl:
mov rcx, [rel labels]
lea rcx, [rel labels]
push_lbl_common:
push rbx
push r12
@ -2255,7 +2256,7 @@ find_lbl:
push r12
push r13
mov rbx, [rel labels]
lea rbx, [rel labels]
call fasthash
mov r12d, eax ; store the hash in r12d

View file

@ -14,6 +14,8 @@ unsafe extern "C" {
#[link_name = "encode_inst"]
unsafe fn encode_inst_impl(_: *mut Instruction) -> usize;
unsafe fn init_label_tables();
unsafe fn fasthash(_: *const u8, _: usize) -> u32;
#[link_name = "buf"]
@ -143,6 +145,9 @@ impl Register {
}
}
static mut TABLES_INIT: std::cell::LazyCell<()> =
std::cell::LazyCell::new(|| unsafe { init_label_tables() });
#[cfg(test)]
mod tests {
use super::*;
@ -157,7 +162,8 @@ mod tests {
#[test]
fn parse_inst() {
let res = Instruction::parse(b"mov byte [rax], 4\0");
_ = unsafe { *TABLES_INIT };
let res = Instruction::parse(b"mov byte ['rax], 4\0");
// assert_eq!(
// res,
// Some((