Add support for compressed nops in riscv ##arch

This commit is contained in:
pancake 2023-11-30 19:24:38 +01:00 committed by pancake
parent ef94001419
commit 5abf716a56
4 changed files with 14 additions and 6 deletions

View File

@ -715,7 +715,7 @@ static bool riscv_decode(RArchSession *s, RAnalOp *op, RArchDecodeMask mask) {
op->type = R_ANAL_OP_TYPE_OR;
} else if (is_any ("not")) {
op->type = R_ANAL_OP_TYPE_NOT;
} else if (is_any ("c.nop")) {
} else if (is_any ("c.nop", "nop", "cnop")) {
op->type = R_ANAL_OP_TYPE_NOP;
} else if (is_any ("mul", "mulh", "mulhu", "mulhsu", "mulw")) {
op->type = R_ANAL_OP_TYPE_MUL;

View File

@ -33,7 +33,7 @@ const char * const * const riscv_fpr_names = riscv_fpr_names_abi;
static void arg_p(char *buf, unsigned long val, const char* const* array, size_t size) {
const char *s = (val >= size || array[val]) ? array[val] : "unknown";
sprintf (buf+strlen (buf), "%s", s);
sprintf (buf + strlen (buf), "%s", s);
}
/* Print insn arguments for 32/64-bit code. */
@ -43,7 +43,7 @@ static void get_insn_args(char *buf, const char *d, insn_t l, uint64_t pc) {
uint64_t target;
if (*d != '\0') {
sprintf (buf+strlen (buf), " ");
sprintf (buf + strlen (buf), " ");
}
for (; *d != '\0'; d++) {

View File

@ -93,6 +93,7 @@ static struct {
int n;
int x;
} ops[] = {
{ 0x1, "c.nop", 'N', 0, 0, 0 }, // c.nop
{ 0x13, "nop", 'N', 0, 0, 0 }, // addi x0, x0, 0 // 13010100 (mov sp, sp)
{ 0x37, "lui", 'I', 2, 0, 0 }, // lui x0, 33
// TODO { 0x37, "li", 'I', 2, 0, 0 }, // lui x0, 33
@ -230,6 +231,9 @@ R_IPI int riscv_assemble(const char *str, ut64 pc, ut8 *out) {
memset (out, 0, 4);
out[0] = ops[i].op;
free (s);
if (r_str_startswith (ops[i].name, "c.")) {
return 2;
}
return 4;
default:
R_LOG_ERROR ("Unknown type");

View File

@ -10,11 +10,15 @@
static bool r_core_hack_riscv(RCore *core, const char *op, const RAnalOp *analop) {
// TODO honor analop->size
if (!strcmp (op, "nop")) {
if (analop->size < 4) {
if (analop->size < 2) {
R_LOG_ERROR ("Can't nop <4 byte instructions");
return false;
}
r_core_cmdf (core, "wx 13000000");
if (analop->size < 4) {
r_core_cmd0 (core, "wx 0100");
} else {
r_core_cmd0 (core, "wx 13000000");
}
return true;
}
if (!strcmp (op, "jinf")) {
@ -22,7 +26,7 @@ static bool r_core_hack_riscv(RCore *core, const char *op, const RAnalOp *analop
R_LOG_ERROR ("Minimum jinf is 2 byte");
return false;
}
r_core_cmdf (core, "wx 01a0");
r_core_cmd0 (core, "wx 01a0");
return true;
}
R_LOG_ERROR ("Unsupported operation '%s'", op);