From 487a2eaf230d23ebe95cea0ee243a6bb0add5393 Mon Sep 17 00:00:00 2001 From: Dhruv Maroo Date: Sun, 17 Sep 2023 00:14:39 +0530 Subject: [PATCH] Add IL implementation for `FABS` * Also add `x86_il_{get,set}_st_reg` helper functions --- librz/analysis/arch/x86/common.c | 20 ++++++++++++++++++++ librz/analysis/arch/x86/common.h | 3 +++ librz/analysis/arch/x86/il_fp_ops.inc | 14 ++++++++++++++ librz/analysis/arch/x86/x86_il.c | 6 +++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/librz/analysis/arch/x86/common.c b/librz/analysis/arch/x86/common.c index b4e605e6839..884f272a6ea 100644 --- a/librz/analysis/arch/x86/common.c +++ b/librz/analysis/arch/x86/common.c @@ -1077,4 +1077,24 @@ RzILOpEffect *x86_il_set_flags(RZ_OWN RzILOpPure *val, unsigned int size) { return SEQ2(set_val, eff); } +static bool check_st_reg(X86Reg reg) { + return reg >= X86_REG_ST0 && reg <= X86_REG_ST7; +} + +RzILOpFloat *x86_il_get_st_reg(X86Reg reg) { + if (check_st_reg(reg)) { + return BV2F(RZ_FLOAT_IEEE754_BIN_64, VARG(x86_registers[reg])); + } + + return NULL; +} + +RzILOpEffect *x86_il_set_st_reg(X86Reg reg, RzILOpFloat *val) { + if (check_st_reg(reg)) { + return SETG(x86_registers[reg], F2BV(val)); + } + + return NULL; +} + #include diff --git a/librz/analysis/arch/x86/common.h b/librz/analysis/arch/x86/common.h index afede0a847b..6b4cba2af7f 100644 --- a/librz/analysis/arch/x86/common.h +++ b/librz/analysis/arch/x86/common.h @@ -104,4 +104,7 @@ RzILOpEffect *x86_il_set_arithmetic_flags_except_cf_bits(RZ_OWN RzILOpPure *res, RzILOpPure *x86_il_get_flags(unsigned int size); RzILOpEffect *x86_il_set_flags(RZ_OWN RzILOpPure *val, unsigned int size); +RzILOpFloat *x86_il_get_st_reg(X86Reg reg); +RzILOpEffect *x86_il_set_st_reg(X86Reg reg, RzILOpFloat *val); + #endif // X86_IL_COMMON_H diff --git a/librz/analysis/arch/x86/il_fp_ops.inc b/librz/analysis/arch/x86/il_fp_ops.inc index 4445e4cb2f4..0c27d4f8bf0 100644 --- a/librz/analysis/arch/x86/il_fp_ops.inc +++ b/librz/analysis/arch/x86/il_fp_ops.inc @@ -11,3 +11,17 @@ * - https://eng.libretexts.org/Bookshelves/Computer_Science/Programming_Languages/x86-64_Assembly_Language_Programming_with_Ubuntu_(Jorgensen)/18%3A_Floating-Point_Instructions * - https://en.wikibooks.org/wiki/X86_Assembly/Floating_Point#Floating-Point_Instruction_Set */ + +#include "common.h" +#include + +/* Arithmetic instructions */ + +/** + * FABS + * Clears the sign bit of st(0) to create absolute value + */ +IL_LIFTER(fabs) { + RzILOpFloat *abs_value = FABS(x86_il_get_st_reg(X86_REG_ST0)); + return x86_il_set_st_reg(X86_REG_ST0, abs_value); +} diff --git a/librz/analysis/arch/x86/x86_il.c b/librz/analysis/arch/x86/x86_il.c index a4aa12fc619..be309653a21 100644 --- a/librz/analysis/arch/x86/x86_il.c +++ b/librz/analysis/arch/x86/x86_il.c @@ -3,6 +3,7 @@ #include "x86_il.h" #include "il_ops.inc" +#include "il_fp_ops.inc" #define COMMON_REGS \ "cs", /* X86_REG_CS */ \ @@ -240,7 +241,10 @@ x86_il_ins x86_ins[X86_INS_ENDING] = { [X86_INS_INSW] = x86_il_unimpl, [X86_INS_OUTSB] = x86_il_unimpl, [X86_INS_OUTSW] = x86_il_unimpl, - [X86_INS_LEAVE] = x86_il_leave + [X86_INS_LEAVE] = x86_il_leave, + + /* floating-point instructions */ + [X86_INS_FABS] = x86_il_fabs }; void label_int(RzILVM *vm, RzILOpEffect *op);