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

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions librz/arch/isa/xtensa/xtensa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2024 billow <[email protected]>
// 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;
}
if (RZ_STR_ISNOTEMPTY(cpu) && RZ_STR_NE(cpu, "xtensa")) {
return false;
}
cs_mode mode = big_endian ? CS_MODE_BIG_ENDIAN : CS_MODE_LITTLE_ENDIAN;
mode |= CS_MODE_XTENSA_ESP32;
if (cs_open(CS_ARCH_XTENSA, mode, &ctx->handle) != CS_ERR_OK) {
return false;
}
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing {}

return;
cs_free(self->insn, self->count);
self->insn = NULL;
self->count = 0;
}
54 changes: 54 additions & 0 deletions librz/arch/isa/xtensa/xtensa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2024 billow <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#ifndef RIZIN_XTENSA_H
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use just RZ_XTENSA_H maybe?

#define RIZIN_XTENSA_H

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

typedef struct xtensa_context_t {
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 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 // RIZIN_XTENSA_H
Loading
Loading