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

WIP: Initial attempt to have BANK switching on ESIL #22258

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions dist/plugins-cfg/plugins.def.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ debug.winkd
egg.exec
egg.xor
esil.null
esil.banksy
esil.dummy
esil.forth
fs.ext2
Expand Down
2 changes: 1 addition & 1 deletion libr/esil/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../config.mk

NAME=r_esil
R2DEPS+=r_util r_reg
R2DEPS+=r_util r_reg r_io

CFLAGS+=-DR2_PLUGIN_INCORE
CFLAGS:=-I.. -I$(LTOP)/asm/esil/include -DR2_PLUGIN_INCORE -Iesil -I$(TOP)/shlr $(CFLAGS)
Expand Down
9 changes: 9 additions & 0 deletions libr/esil/p/banksy.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OBJ_ESIL_BANKSY=esil_banksy.o

STATIC_OBJ+=${OBJ_ESIL_BANKSY}
TARGET_ESIL_BANKSY=esil_banksy.${EXT_SO}

ALL_TARGETS+=${TARGET_ESIL_BANKSY}

${TARGET_ESIL_BANKSY}: ${OBJ_ESIL_BANKSY}
${CC} -lr_io $(call libname,esil_banksy) ${LDFLAGS} ${CFLAGS} -o esil_banksy.${EXT_SO} ${OBJ_ESIL_BANKSY}
56 changes: 56 additions & 0 deletions libr/esil/p/esil_banksy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* radare2 - LGPL - Copyright 2021-2023 - pancake */

#define R_LOG_ORIGIN "esil.banksy"

#include <r_lib.h>
#include <r_core.h>
#include <r_anal.h>

char *obank = NULL;

static bool esil_banksy_operation(REsil *esil) {
RCore *core = (RCore *)esil->user;
int obank = core->io->bank;
char *src = r_esil_pop (esil);
if (src) {
RIOBank *b = r_io_bank_use_byname (core->io, src);
if (!b) {
R_LOG_WARN ("iobank mode on");
}
}
R_LOG_INFO ("BANK: Switch to bank %s from %s", src);
return true;
}

static void *r_esil_banksy_init(REsil *esil) {
r_esil_set_op (esil, "BANK", esil_banksy_operation,
0, 0, R_ESIL_OP_TYPE_CUSTOM);
R_LOG_INFO ("esil.banksy: Activated");
return NULL;
}

static void r_esil_banksy_fini(REsil *esil, void *user) {
REsilOp *op = r_esil_get_op (esil, "BANK");
if (op && op->code == esil_banksy_operation) {
r_esil_del_op (esil, "BANK");
}
R_LOG_INFO ("esil.banksy: Deactivated");
}

REsilPlugin r_esil_plugin_banksy = {
.meta = {
.name = "banky",
.desc = "switch banks",
.license = "LGPL3",
},
.init = r_esil_banksy_init,
.fini = r_esil_banksy_fini
};

#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ESIL,
.data = &r_esil_plugin_banksy,
.version = R2_VERSION
};
#endif
1 change: 1 addition & 0 deletions libr/include/r_esil.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ R_API void r_esil_trace_loopcount_increment(REsilTrace *etrace, ut64 addr);
extern REsilPlugin r_esil_plugin_null;
extern REsilPlugin r_esil_plugin_dummy;
extern REsilPlugin r_esil_plugin_forth;
extern REsilPlugin r_esil_plugin_banksy;

#ifdef __cplusplus
}
Expand Down
34 changes: 34 additions & 0 deletions libr/io/io_bank.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ R_API ut32 r_io_bank_first(RIO *io) {
return bankid;
}

typedef struct {
bool found;
RIO *io;
const char *name;
RIOBank *bank;
ut32 bank_id;
} BankByName;

static bool bank_byname(void *user, void *data, ut32 id) {
BankByName *bbn = (BankByName *)data;
RIOBank *b = r_io_bank_get (bbn->io, id);
if (b && !strcmp (b->name, bbn->name)) {
bbn->bank = b;
bbn->bank_id = id;
bbn->found = true;
return false;
}
return true;
}

R_API RIOBank *r_io_bank_use_byname(RIO *io, const char *name) {
BankByName bbn = {
.io = io,
.name = name,
.found = false,
};
r_id_storage_foreach (io->banks, bank_byname, &bbn);
if (bbn.found) {
r_io_bank_use (io, bbn.bank_id);
return bbn.bank;
}
return NULL;
}

R_API bool r_io_bank_use(RIO *io, ut32 bankid) {
r_return_val_if_fail (io, false);
RIOBank *bank = r_io_bank_get (io, bankid);
Expand Down
6 changes: 3 additions & 3 deletions libr/libs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
ifeq ($(LIBS0),)
LIBS0=util
LIBS1=socket reg cons bp config crypto syscall
LIBS2=search flag esil io
LIBS3=arch fs # esil depends on reg and esil
LIBS4=asm anal magic
LIBS2=search flag esil
LIBS3=arch io # esil depends on reg and esil
LIBS4=asm anal magic fs
LIBS5=lang egg bin
LIBS6=debug
LIBS7=core
Expand Down
Loading