From 9502499d2b760119e5ecdb56f949aa79f8c83810 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Sun, 22 Oct 2023 16:47:50 +0000 Subject: [PATCH 1/2] i#3544 RV64: Implement mangle_rel_addr() JAL is the only instruction in RISC-V with PC-relative address reference, which needs to be mangled. Loading a PC-relative address into the thread pointer (tp) register should be rare and we left it unimplemented for now. --- core/arch/riscv64/mangle.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/core/arch/riscv64/mangle.c b/core/arch/riscv64/mangle.c index 2f2836e725b..e1cbd842dbb 100644 --- a/core/arch/riscv64/mangle.c +++ b/core/arch/riscv64/mangle.c @@ -32,6 +32,12 @@ #include "../globals.h" #include "arch.h" +#include "instr_create_shared.h" +#include "instrument.h" /* instrlist_meta_preinsert */ +/* Make code more readable by shortening long lines. + * We mark everything we add as non-app instr. + */ +#define PRE instrlist_meta_preinsert void mangle_arch_init(void) @@ -190,8 +196,33 @@ instr_t * mangle_rel_addr(dcontext_t *dcontext, instrlist_t *ilist, instr_t *instr, instr_t *next_instr) { - /* FIXME i#3544: Not implemented */ - ASSERT_NOT_IMPLEMENTED(false); + opnd_t dst = instr_get_dst(instr, 0); + app_pc tgt; + ASSERT(instr_get_opcode(instr) == OP_auipc); + ASSERT(instr_has_rel_addr_reference(instr)); + instr_get_rel_data_or_instr_target(instr, &tgt); + ASSERT(opnd_is_reg(dst)); + ASSERT(opnd_is_rel_addr(instr_get_src(instr, 0))); + + ASSERT_NOT_IMPLEMENTED(!instr_uses_reg(instr, DR_REG_TP)); + + if (instr_uses_reg(instr, dr_reg_stolen)) { + dst = opnd_create_reg(DR_REG_A0); + PRE(ilist, next_instr, + instr_create_save_to_tls(dcontext, DR_REG_A0, TLS_REG0_SLOT)); + } + + insert_mov_immed_ptrsz(dcontext, (ptr_int_t)tgt, dst, ilist, next_instr, NULL, NULL); + + if (instr_uses_reg(instr, dr_reg_stolen)) { + PRE(ilist, next_instr, + instr_create_save_to_tls(dcontext, DR_REG_A0, TLS_REG_STOLEN_SLOT)); + PRE(ilist, next_instr, + instr_create_restore_from_tls(dcontext, DR_REG_A0, TLS_REG0_SLOT)); + } + + instrlist_remove(ilist, instr); + instr_destroy(dcontext, instr); return NULL; } From 0403d68fc29718a11a2332d5c163b937f97246e4 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Wed, 25 Oct 2023 05:00:12 +0000 Subject: [PATCH 2/2] Remove unnecessary header --- core/arch/riscv64/mangle.c | 1 - 1 file changed, 1 deletion(-) diff --git a/core/arch/riscv64/mangle.c b/core/arch/riscv64/mangle.c index e1cbd842dbb..2cd4da68a48 100644 --- a/core/arch/riscv64/mangle.c +++ b/core/arch/riscv64/mangle.c @@ -32,7 +32,6 @@ #include "../globals.h" #include "arch.h" -#include "instr_create_shared.h" #include "instrument.h" /* instrlist_meta_preinsert */ /* Make code more readable by shortening long lines. * We mark everything we add as non-app instr.