Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Xtensa arch support #4654

Merged
merged 29 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0d974d9
Add xtensa arch capstone plugin
imbillow Sep 28, 2024
930d626
Remove xtensa-gnu
imbillow Oct 2, 2024
01a9948
fix: `asm.cpu` is not set correctly, when there is no “,” in asm plug…
imbillow Nov 2, 2024
8e6b4b5
fix: xtensa tests
imbillow Nov 2, 2024
3840931
fix: xtensa_analyze_op
imbillow Nov 5, 2024
7727496
fix core_disassembly when n_bytes==0
imbillow Nov 5, 2024
6491c55
fix xtensa_analyze_op_esil
imbillow Nov 5, 2024
82320c9
fix xtensa esil pc mod
imbillow Nov 5, 2024
87d7f6e
fix xtensa esil l32r
imbillow Nov 5, 2024
35614fc
fix xtensa esil call0
imbillow Nov 5, 2024
62231db
fix xtensa esil extui
imbillow Nov 5, 2024
43377af
fix xtensa esil store
imbillow Nov 5, 2024
4b3a654
fmt
imbillow Nov 5, 2024
c31e2d4
fix xtensa test
imbillow Nov 5, 2024
96cbff4
fix xtensa stack inc and ret
imbillow Nov 5, 2024
e3eada2
keep `asm.cpu` when valid in cb_asmarch
imbillow Nov 5, 2024
509c504
fix cb_asmarch
imbillow Nov 5, 2024
92eb1a4
fix cb_asmarch
imbillow Nov 5, 2024
9b56ddc
fix max op size
imbillow Nov 5, 2024
ade5947
xtensa: add cpu select
imbillow Nov 6, 2024
008492e
xtensa: mark PUSH POP
imbillow Nov 6, 2024
c154ef7
xtensa: set src dst
imbillow Nov 6, 2024
3896ec3
xtensa: set dir
imbillow Nov 6, 2024
616fb65
xtensa: fix stackframe?
imbillow Nov 6, 2024
df8143c
xtensa: add author
imbillow Nov 6, 2024
37b1273
xtensa: clean
imbillow Nov 6, 2024
97c5fa9
fix test/db/cmd/cmd_list
imbillow Nov 6, 2024
9b8f9ef
xtensa: fix
imbillow Nov 7, 2024
86e2766
Update capstone-next.wrap
XVilka Nov 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions librz/arch/isa/xtensa/xtensa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2024 billow <billow.fun@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only

#include "xtensa.h"

bool xtensa_init(void **user) {
if (*user) {
return true;
}
XtensaContext *ctx = RZ_NEW0(XtensaContext);
if (!ctx) {
return false;
}
*user = ctx;
return true;
}

bool xtensa_fini(void *user) {
XtensaContext *ctx = user;
cs_close(&ctx->handle);
free(ctx);
return true;
}

bool xtensa_open(XtensaContext *ctx, const char *cpu, bool big_endian) {
if (!ctx) {
return false;
}
cs_mode mode = big_endian ? CS_MODE_BIG_ENDIAN : CS_MODE_LITTLE_ENDIAN;
if (RZ_STR_ISEMPTY(cpu)) {
mode |= xtensa_cpu_modes[0].mode;
} else {
for (int i = 0; i < RZ_ARRAY_SIZE(xtensa_cpu_modes); ++i) {
if (RZ_STR_EQ(cpu, xtensa_cpu_modes[i].cpu)) {
mode |= xtensa_cpu_modes[i].mode;
break;
}
}
}
if (mode == ctx->mode) {
return true;
}
if (cs_open(CS_ARCH_XTENSA, mode, &ctx->handle) != CS_ERR_OK) {
return false;
}
ctx->mode = mode;
if (cs_option(ctx->handle, CS_OPT_DETAIL, CS_OPT_ON) != CS_ERR_OK) {
return false;
}
return true;
}

bool xtensa_disassemble(XtensaContext *self, const ut8 *buf, int len, ut64 addr) {
self->count = cs_disasm(self->handle, buf, len, addr, 1, &self->insn);
if (self->count == 0) {
return false;
}
return true;
}

void xtensa_disassemble_fini(XtensaContext *self) {
if (!self->insn) {
return;
}
cs_free(self->insn, self->count);
self->insn = NULL;
self->count = 0;
}
67 changes: 67 additions & 0 deletions librz/arch/isa/xtensa/xtensa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: 2024 billow <billow.fun@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only

#ifndef RZ_XTENSA_H
#define RZ_XTENSA_H

#include <capstone/capstone.h>
#include <rz_asm.h>

typedef struct {
const char *cpu;
cs_mode mode;
} XtensaCPUMode;

static const XtensaCPUMode xtensa_cpu_modes[] = {
{ .cpu = "esp32", .mode = CS_MODE_XTENSA_ESP32 },
{ .cpu = "esp32s2", .mode = CS_MODE_XTENSA_ESP32S2 },
{ .cpu = "esp8266", .mode = CS_MODE_XTENSA_ESP8266 },
};

typedef struct xtensa_context_t {
cs_mode mode;
csh handle;
cs_insn *insn;
size_t count;
} XtensaContext;

bool xtensa_init(void **user);
bool xtensa_fini(void *user);
bool xtensa_open(XtensaContext *ctx, const char *cpu, bool big_endian);
bool xtensa_disassemble(XtensaContext *self, const ut8 *buf, int len, ut64 addr);
void xtensa_disassemble_fini(XtensaContext *self);
void xtensa_analyze_op_esil(XtensaContext *ctx, RzAnalysisOp *op);

static inline cs_xtensa_op_mem *xtensa_op_mem(cs_insn *insn, unsigned int index) {
cs_xtensa_op *op = &insn->detail->xtensa.operands[index];
rz_warn_if_fail(op->type == XTENSA_OP_MEM);
return &op->mem;
}

static inline xtensa_reg xtensa_op_reg(cs_insn *insn, unsigned int index) {
cs_xtensa_op *op = &insn->detail->xtensa.operands[index];
rz_warn_if_fail(op->type == XTENSA_OP_REG);
return op->reg;
}

static inline int32_t xtensa_op_imm(cs_insn *insn, unsigned int index) {
cs_xtensa_op *op = &insn->detail->xtensa.operands[index];
rz_warn_if_fail(op->type == XTENSA_OP_IMM);
return op->imm;
}

static inline int32_t xtensa_op_l32r(cs_insn *insn, unsigned int index) {
cs_xtensa_op *op = &insn->detail->xtensa.operands[index];
rz_warn_if_fail(op->type == XTENSA_OP_L32R);
return op->imm;
}

#define XOP(I) (ctx->insn->detail->xtensa.operands + I)
#define MEM(I) xtensa_op_mem(ctx->insn, I)
#define REGI(I) xtensa_op_reg(ctx->insn, I)
#define REGN(I) cs_reg_name(ctx->handle, (xtensa_op_reg(ctx->insn, I)))
#define IMM(I) xtensa_op_imm(ctx->insn, I)
#define L32R(I) xtensa_op_l32r(ctx->insn, I)
#define INSN_SIZE (ctx->insn->size)

#endif // RZ_XTENSA_H
Loading
Loading