From 8716bd1991557570ebc75bca122118906d289274 Mon Sep 17 00:00:00 2001 From: Alexis Engelke Date: Sun, 14 Jun 2020 13:55:59 +0200 Subject: [PATCH] format: Handle offset operands properly --- format.c | 10 +++++++++- tests/decode-jmp.txt | 8 ++++---- tests/driver.c | 3 +-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/format.c b/format.c index b0f8776..9642612 100644 --- a/format.c +++ b/format.c @@ -55,7 +55,7 @@ fd_format(const FdInstr* instr, char* buffer, size_t len) if (op_type == FD_OT_NONE) break; - const char* op_type_name = &"reg\0imm\0mem"[op_type * 4] - 4; + const char* op_type_name = &"reg\0imm\0mem\0off"[op_type * 4] - 4; FMT_CONCAT(buf, end, " %s%u:", op_type_name, FD_OP_SIZE(instr, i)); switch (op_type) @@ -70,6 +70,14 @@ fd_format(const FdInstr* instr, char* buffer, size_t len) else FMT_CONCAT(buf, end, "r%u", FD_OP_REG(instr, i)); break; + case FD_OT_OFF: + if (FD_OP_SIZE(instr, i) == 2) + FMT_CONCAT(buf, end, "ip+"); + else if (FD_OP_SIZE(instr, i) == 4) + FMT_CONCAT(buf, end, "eip+"); + else if (FD_OP_SIZE(instr, i) == 8) + FMT_CONCAT(buf, end, "rip+"); + // fallthrough case FD_OT_IMM: immediate = FD_OP_IMM(instr, i); if (FD_OP_SIZE(instr, i) == 1) diff --git a/tests/decode-jmp.txt b/tests/decode-jmp.txt index fab1bfc..7999c32 100644 --- a/tests/decode-jmp.txt +++ b/tests/decode-jmp.txt @@ -1,4 +1,4 @@ -decode32 e900000000 [JMP imm4:0x1234005] -decode32 66e90100 [JMP imm2:0x4005] -decode64 e900000000 [JMP imm8:0x1234005] -decode64 66e900000000 [JMP imm8:0x1234006] +decode32 e900000000 [JMP off4:eip+0x0] +decode32 66e90100 [JMP off2:ip+0x1] +decode64 e900000000 [JMP off8:rip+0x0] +decode64 66e900000000 [JMP off8:rip+0x0] diff --git a/tests/driver.c b/tests/driver.c index b4b0a14..acc44cb 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -58,8 +58,7 @@ main(int argc, char** argv) while (current_off != length) { size_t remaining = length - current_off; - int retval = fd_decode(code + current_off, remaining, mode, - 0x1234000, &instr); + int retval = fd_decode(code + current_off, remaining, mode, 0, &instr); if (retval < 0) goto fail; current_off += retval;