From dff9a7e4a14984705db8fb18550af5a4fa9ce3d5 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 21 Aug 2021 14:33:13 -0700 Subject: fix negative relative branches (again!!! +- is bad!!!) --- src/long_mode/display.rs | 18 +++++++++++++++--- src/protected_mode/display.rs | 18 +++++++++++++++--- src/real_mode/display.rs | 18 +++++++++++++++--- test/long_mode/mod.rs | 3 ++- test/protected_mode/mod.rs | 3 ++- test/real_mode/mod.rs | 3 ++- 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/long_mode/display.rs b/src/long_mode/display.rs index 59efe38..7bbd83f 100644 --- a/src/long_mode/display.rs +++ b/src/long_mode/display.rs @@ -3331,7 +3331,11 @@ fn contextualize_intel(instr: &Instruction, colors: out.write_str(instr.opcode.name())?; if instr.opcode == Opcode::XBEGIN { - return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + if (instr.imm as i32) >= 0 { + return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + } else { + return write!(out, " ${}", colors.number(signed_i32_hex(instr.imm as i32))); + } } if instr.operand_count > 0 { @@ -3356,10 +3360,18 @@ fn contextualize_intel(instr: &Instruction, colors: if RELATIVE_BRANCHES.contains(&instr.opcode) { return match x { Operand::ImmediateI8(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel as i32))) + } } Operand::ImmediateI32(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel))) + } } _ => { unreachable!() } }; diff --git a/src/protected_mode/display.rs b/src/protected_mode/display.rs index 72bfcb4..33c0cda 100644 --- a/src/protected_mode/display.rs +++ b/src/protected_mode/display.rs @@ -3344,7 +3344,11 @@ fn contextualize_intel(instr: &Instruction, colors: out.write_str(instr.opcode.name())?; if instr.opcode == Opcode::XBEGIN { - return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + if (instr.imm as i32) >= 0 { + return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + } else { + return write!(out, " ${}", colors.number(signed_i32_hex(instr.imm as i32))); + } } if instr.operand_count > 0 { @@ -3369,10 +3373,18 @@ fn contextualize_intel(instr: &Instruction, colors: if RELATIVE_BRANCHES.contains(&instr.opcode) { return match x { Operand::ImmediateI8(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel as i32))) + } } Operand::ImmediateI32(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel))) + } } _ => { unreachable!() } }; diff --git a/src/real_mode/display.rs b/src/real_mode/display.rs index f514974..3a9fddc 100644 --- a/src/real_mode/display.rs +++ b/src/real_mode/display.rs @@ -3344,7 +3344,11 @@ fn contextualize_intel(instr: &Instruction, colors: out.write_str(instr.opcode.name())?; if instr.opcode == Opcode::XBEGIN { - return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + if (instr.imm as i32) >= 0 { + return write!(out, " $+{}", colors.number(signed_i32_hex(instr.imm as i32))); + } else { + return write!(out, " ${}", colors.number(signed_i32_hex(instr.imm as i32))); + } } if instr.operand_count > 0 { @@ -3369,10 +3373,18 @@ fn contextualize_intel(instr: &Instruction, colors: if RELATIVE_BRANCHES.contains(&instr.opcode) { return match x { Operand::ImmediateI8(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel as i32))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel as i32))) + } } Operand::ImmediateI32(rel) => { - write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + if rel >= 0 { + write!(out, "$+{}", colors.number(signed_i32_hex(rel))) + } else { + write!(out, "${}", colors.number(signed_i32_hex(rel))) + } } _ => { unreachable!() } }; diff --git a/test/long_mode/mod.rs b/test/long_mode/mod.rs index 015f1c6..dab4e91 100644 --- a/test/long_mode/mod.rs +++ b/test/long_mode/mod.rs @@ -1212,6 +1212,7 @@ fn test_prefixes() { fn test_control_flow() { test_display(&[0x73, 0x31], "jnb $+0x31"); test_display(&[0x72, 0x5a], "jb $+0x5a"); + test_display(&[0x72, 0xf0], "jb $-0x10"); test_display(&[0x0f, 0x86, 0x8b, 0x01, 0x00, 0x00], "jna $+0x18b"); test_display(&[0x74, 0x47], "jz $+0x47"); test_display(&[0xff, 0x15, 0x7e, 0x72, 0x24, 0x00], "call qword [rip + 0x24727e]"); @@ -1225,7 +1226,7 @@ fn test_control_flow() { test_display(&[0xe1, 0x12], "loopz $+0x12"); test_display(&[0xe2, 0x12], "loop $+0x12"); test_display(&[0xe3, 0x12], "jrcxz $+0x12"); - test_display(&[0xe3, 0xf0], "jrcxz $+-0x10"); + test_display(&[0xe3, 0xf0], "jrcxz $-0x10"); test_display(&[0xc3], "ret"); } diff --git a/test/protected_mode/mod.rs b/test/protected_mode/mod.rs index 5ed69ed..41ecbb7 100644 --- a/test/protected_mode/mod.rs +++ b/test/protected_mode/mod.rs @@ -1096,6 +1096,7 @@ fn test_prefixes() { fn test_control_flow() { test_display(&[0x73, 0x31], "jnb $+0x31"); test_display(&[0x72, 0x5a], "jb $+0x5a"); + test_display(&[0x72, 0xf0], "jb $-0x10"); test_display(&[0x0f, 0x86, 0x8b, 0x01, 0x00, 0x00], "jna $+0x18b"); test_display(&[0x74, 0x47], "jz $+0x47"); test_display(&[0xff, 0x15, 0x7e, 0x72, 0x24, 0x00], "call dword [0x24727e]"); @@ -1109,7 +1110,7 @@ fn test_control_flow() { test_display(&[0xe1, 0x12], "loopz $+0x12"); test_display(&[0xe2, 0x12], "loop $+0x12"); test_display(&[0xe3, 0x12], "jecxz $+0x12"); - test_display(&[0xe3, 0xf0], "jecxz $+-0x10"); + test_display(&[0xe3, 0xf0], "jecxz $-0x10"); test_display(&[0xc3], "ret"); } diff --git a/test/real_mode/mod.rs b/test/real_mode/mod.rs index 8543fae..dfbe4e8 100644 --- a/test/real_mode/mod.rs +++ b/test/real_mode/mod.rs @@ -16927,6 +16927,7 @@ fn test_real_mode() { test_display(&[0x68, 0x7f, 0x63], "push 0x637f"); test_display(&[0x6b, 0x43, 0x6f, 0x6d], "imul ax, word [bp + di + 0x6f], 0x6d"); test_display(&[0x72, 0x5a], "jb $+0x5a"); + test_display(&[0x72, 0xf0], "jb $-0x10"); test_display(&[0x73, 0x31], "jnb $+0x31"); test_display(&[0x74, 0x47], "jz $+0x47"); test_display(&[0x81, 0xec, 0x10, 0x03], "sub sp, 0x310"); @@ -17758,7 +17759,7 @@ fn test_real_mode() { test_display(&[0xe1, 0x12], "loopz $+0x12"); test_display(&[0xe2, 0x12], "loop $+0x12"); test_display(&[0xe3, 0x12], "jcxz $+0x12"); - test_display(&[0xe3, 0xf0], "jcxz $+-0x10"); + test_display(&[0xe3, 0xf0], "jcxz $-0x10"); test_display(&[0xe4, 0x99], "in al, 0x99"); test_display(&[0xe5, 0x99], "in ax, 0x99"); test_display(&[0xe6, 0x99], "out 0x99, al"); -- cgit v1.1