From a8143d8fb24ae5ef82d0f5fcd553654ea28b2dee Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 29 Mar 2023 16:56:31 +0200 Subject: [PATCH] Migrate the pyc plugin ##arch --- dist/plugins-cfg/plugins.def.cfg | 2 +- dist/plugins-cfg/plugins.mingw.cfg | 2 +- dist/plugins-cfg/plugins.nocs.cfg | 2 +- dist/plugins-cfg/plugins.termux.cfg | 2 +- dist/plugins-cfg/plugins.wasi.cfg | 2 +- libr/anal/meson.build | 6 - libr/anal/p/pyc.mk | 17 --- libr/arch/meson.build | 7 ++ libr/arch/p/pyc.mk | 15 +++ libr/{asm/arch => arch/p}/pyc/opcode.c | 0 libr/{asm/arch => arch/p}/pyc/opcode.h | 0 libr/{asm/arch => arch/p}/pyc/opcode_10.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_11.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_12.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_13.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_14.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_15.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_16.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_20.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_21.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_22.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_23.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_24.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_25.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_26.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_27.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_2x.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_30.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_31.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_32.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_33.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_34.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_35.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_36.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_37.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_38.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_39.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_3x.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_all.c | 0 libr/{asm/arch => arch/p}/pyc/opcode_anal.c | 0 .../{asm/arch => arch/p}/pyc/opcode_arg_fmt.c | 0 .../p/anal_pyc.c => arch/p/pyc/plugin.c} | 117 +++++++----------- libr/{asm/arch => arch/p}/pyc/pyc_dis.c | 0 libr/{asm/arch => arch/p}/pyc/pyc_dis.h | 0 libr/include/r_anal.h | 1 - libr/include/r_arch.h | 1 + libr/meson.build | 2 +- 47 files changed, 77 insertions(+), 99 deletions(-) delete mode 100644 libr/anal/p/pyc.mk create mode 100644 libr/arch/p/pyc.mk rename libr/{asm/arch => arch/p}/pyc/opcode.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode.h (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_10.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_11.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_12.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_13.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_14.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_15.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_16.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_20.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_21.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_22.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_23.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_24.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_25.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_26.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_27.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_2x.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_30.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_31.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_32.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_33.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_34.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_35.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_36.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_37.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_38.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_39.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_3x.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_all.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_anal.c (100%) rename libr/{asm/arch => arch/p}/pyc/opcode_arg_fmt.c (100%) rename libr/{anal/p/anal_pyc.c => arch/p/pyc/plugin.c} (60%) rename libr/{asm/arch => arch/p}/pyc/pyc_dis.c (100%) rename libr/{asm/arch => arch/p}/pyc/pyc_dis.h (100%) diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index 68c0b76804..72cfd8eb14 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -44,7 +44,7 @@ arch.pic anal.ppc_cs anal.ppc_gnu arch.propeller -anal.pyc +arch.pyc arch.riscv_cs anal.s390_cs anal.s390_gnu diff --git a/dist/plugins-cfg/plugins.mingw.cfg b/dist/plugins-cfg/plugins.mingw.cfg index 8cc8de7bdb..87a4c378cf 100644 --- a/dist/plugins-cfg/plugins.mingw.cfg +++ b/dist/plugins-cfg/plugins.mingw.cfg @@ -54,7 +54,7 @@ arch.snes arch.riscv arch.riscv_cs arch.pic -anal.pyc +arch.pyc arch.propeller arch.null arch.i4004 diff --git a/dist/plugins-cfg/plugins.nocs.cfg b/dist/plugins-cfg/plugins.nocs.cfg index 0df00e10a4..1f5d4b4f88 100644 --- a/dist/plugins-cfg/plugins.nocs.cfg +++ b/dist/plugins-cfg/plugins.nocs.cfg @@ -42,7 +42,7 @@ arch.6502 arch.snes arch.riscv arch.pic -anal.pyc +arch.pyc arch.propeller esil.dummy asm.null diff --git a/dist/plugins-cfg/plugins.termux.cfg b/dist/plugins-cfg/plugins.termux.cfg index 96b85e0d18..f276a30a90 100644 --- a/dist/plugins-cfg/plugins.termux.cfg +++ b/dist/plugins-cfg/plugins.termux.cfg @@ -31,7 +31,7 @@ arch.riscv arch.riscv_cs arch.pic arch.wasm -anal.pyc +arch.pyc arch.null arch.rsp arch.i4004 diff --git a/dist/plugins-cfg/plugins.wasi.cfg b/dist/plugins-cfg/plugins.wasi.cfg index 5161bafb7b..770801f767 100644 --- a/dist/plugins-cfg/plugins.wasi.cfg +++ b/dist/plugins-cfg/plugins.wasi.cfg @@ -3,7 +3,7 @@ arch.x86_nz arch.arm anal.dalvik arch.wasm -anal.pyc +arch.pyc anal.bf arch.bpf arch.riscv diff --git a/libr/anal/meson.build b/libr/anal/meson.build index cb957d05b6..59940ac778 100644 --- a/libr/anal/meson.build +++ b/libr/anal/meson.build @@ -77,7 +77,6 @@ r_anal_sources = [ 'p/anal_null.c', 'p/anal_ppc_cs.c', 'p/anal_ppc_gnu.c', - 'p/anal_pyc.c', 'p/anal_s390_cs.c', 'p/anal_s390_gnu.c', '../asm/arch/s390/gnu/s390-dis.c', @@ -122,11 +121,6 @@ r_anal_sources = [ '../asm/arch/tms320/c55x_plus/decode_funcs.c', '../asm/arch/tms320/c55x_plus/hashtable.c', '../asm/arch/tms320/c55x_plus/utils.c', - '../asm/arch/pyc/opcode_all.c', - '../asm/arch/pyc/opcode_anal.c', - '../asm/arch/pyc/opcode_arg_fmt.c', - '../asm/arch/pyc/opcode.c', - '../asm/arch/pyc/pyc_dis.c' ] incdirs = [ 'arch' diff --git a/libr/anal/p/pyc.mk b/libr/anal/p/pyc.mk deleted file mode 100644 index 04a3c6dc96..0000000000 --- a/libr/anal/p/pyc.mk +++ /dev/null @@ -1,17 +0,0 @@ -PYC_ASM_ROOT=../../asm/arch/pyc/ -OBJ_PYC=anal_pyc.o -OBJ_PYC+=$(PYC_ASM_ROOT)/opcode_all.o -OBJ_PYC+=$(PYC_ASM_ROOT)/opcode_anal.o -OBJ_PYC+=$(PYC_ASM_ROOT)/opcode_arg_fmt.o -OBJ_PYC+=$(PYC_ASM_ROOT)/opcode.o -OBJ_PYC+=$(PYC_ASM_ROOT)/pyc_dis.o - -STATIC_OBJ+=$(OBJ_PYC) -TARGET_PYC=anal_pyc.$(EXT_SO) - -ALL_TARGETS+=$(TARGET_PYC) -PYC_ROOT=../asm/arch/pyc -CFLAGS+=-I$(PYC_ROOT) - -$(TARGET_PYC): $(OBJ_PYC) - $(CC) $(call libname,anal_pyc) $(CFLAGS) $(LDFLAGS) -o $(TARGET_PYC) $(OBJ_PYC) -lr_util diff --git a/libr/arch/meson.build b/libr/arch/meson.build index 0b3830e43f..f633f6aa17 100644 --- a/libr/arch/meson.build +++ b/libr/arch/meson.build @@ -85,6 +85,13 @@ r_arch_sources = [ 'p/ebc/ebc_disas.c', 'p/msp430/plugin.c', 'p/msp430/msp430_disas.c', + # python + 'p/pyc/plugin.c', + 'p/pyc/opcode_all.c', + 'p/pyc/opcode_anal.c', + 'p/pyc/opcode_arg_fmt.c', + 'p/pyc/opcode.c', + 'p/pyc/pyc_dis.c' ] if arch_plugins.contains('x86_nz') diff --git a/libr/arch/p/pyc.mk b/libr/arch/p/pyc.mk new file mode 100644 index 0000000000..92c282f908 --- /dev/null +++ b/libr/arch/p/pyc.mk @@ -0,0 +1,15 @@ +PYC_ROOT=p/pyc +OBJ_PYC=p/pyc/plugin.o +OBJ_PYC+=$(PYC_ROOT)/opcode_all.o +OBJ_PYC+=$(PYC_ROOT)/opcode_anal.o +OBJ_PYC+=$(PYC_ROOT)/opcode_arg_fmt.o +OBJ_PYC+=$(PYC_ROOT)/opcode.o +OBJ_PYC+=$(PYC_ROOT)/pyc_dis.o + +STATIC_OBJ+=$(OBJ_PYC) +TARGET_PYC=arch_pyc.$(EXT_SO) + +ALL_TARGETS+=$(TARGET_PYC) + +$(TARGET_PYC): $(OBJ_PYC) + $(CC) $(call libname,arch_pyc) $(CFLAGS) $(LDFLAGS) -o $(TARGET_PYC) $(OBJ_PYC) -lr_util diff --git a/libr/asm/arch/pyc/opcode.c b/libr/arch/p/pyc/opcode.c similarity index 100% rename from libr/asm/arch/pyc/opcode.c rename to libr/arch/p/pyc/opcode.c diff --git a/libr/asm/arch/pyc/opcode.h b/libr/arch/p/pyc/opcode.h similarity index 100% rename from libr/asm/arch/pyc/opcode.h rename to libr/arch/p/pyc/opcode.h diff --git a/libr/asm/arch/pyc/opcode_10.c b/libr/arch/p/pyc/opcode_10.c similarity index 100% rename from libr/asm/arch/pyc/opcode_10.c rename to libr/arch/p/pyc/opcode_10.c diff --git a/libr/asm/arch/pyc/opcode_11.c b/libr/arch/p/pyc/opcode_11.c similarity index 100% rename from libr/asm/arch/pyc/opcode_11.c rename to libr/arch/p/pyc/opcode_11.c diff --git a/libr/asm/arch/pyc/opcode_12.c b/libr/arch/p/pyc/opcode_12.c similarity index 100% rename from libr/asm/arch/pyc/opcode_12.c rename to libr/arch/p/pyc/opcode_12.c diff --git a/libr/asm/arch/pyc/opcode_13.c b/libr/arch/p/pyc/opcode_13.c similarity index 100% rename from libr/asm/arch/pyc/opcode_13.c rename to libr/arch/p/pyc/opcode_13.c diff --git a/libr/asm/arch/pyc/opcode_14.c b/libr/arch/p/pyc/opcode_14.c similarity index 100% rename from libr/asm/arch/pyc/opcode_14.c rename to libr/arch/p/pyc/opcode_14.c diff --git a/libr/asm/arch/pyc/opcode_15.c b/libr/arch/p/pyc/opcode_15.c similarity index 100% rename from libr/asm/arch/pyc/opcode_15.c rename to libr/arch/p/pyc/opcode_15.c diff --git a/libr/asm/arch/pyc/opcode_16.c b/libr/arch/p/pyc/opcode_16.c similarity index 100% rename from libr/asm/arch/pyc/opcode_16.c rename to libr/arch/p/pyc/opcode_16.c diff --git a/libr/asm/arch/pyc/opcode_20.c b/libr/arch/p/pyc/opcode_20.c similarity index 100% rename from libr/asm/arch/pyc/opcode_20.c rename to libr/arch/p/pyc/opcode_20.c diff --git a/libr/asm/arch/pyc/opcode_21.c b/libr/arch/p/pyc/opcode_21.c similarity index 100% rename from libr/asm/arch/pyc/opcode_21.c rename to libr/arch/p/pyc/opcode_21.c diff --git a/libr/asm/arch/pyc/opcode_22.c b/libr/arch/p/pyc/opcode_22.c similarity index 100% rename from libr/asm/arch/pyc/opcode_22.c rename to libr/arch/p/pyc/opcode_22.c diff --git a/libr/asm/arch/pyc/opcode_23.c b/libr/arch/p/pyc/opcode_23.c similarity index 100% rename from libr/asm/arch/pyc/opcode_23.c rename to libr/arch/p/pyc/opcode_23.c diff --git a/libr/asm/arch/pyc/opcode_24.c b/libr/arch/p/pyc/opcode_24.c similarity index 100% rename from libr/asm/arch/pyc/opcode_24.c rename to libr/arch/p/pyc/opcode_24.c diff --git a/libr/asm/arch/pyc/opcode_25.c b/libr/arch/p/pyc/opcode_25.c similarity index 100% rename from libr/asm/arch/pyc/opcode_25.c rename to libr/arch/p/pyc/opcode_25.c diff --git a/libr/asm/arch/pyc/opcode_26.c b/libr/arch/p/pyc/opcode_26.c similarity index 100% rename from libr/asm/arch/pyc/opcode_26.c rename to libr/arch/p/pyc/opcode_26.c diff --git a/libr/asm/arch/pyc/opcode_27.c b/libr/arch/p/pyc/opcode_27.c similarity index 100% rename from libr/asm/arch/pyc/opcode_27.c rename to libr/arch/p/pyc/opcode_27.c diff --git a/libr/asm/arch/pyc/opcode_2x.c b/libr/arch/p/pyc/opcode_2x.c similarity index 100% rename from libr/asm/arch/pyc/opcode_2x.c rename to libr/arch/p/pyc/opcode_2x.c diff --git a/libr/asm/arch/pyc/opcode_30.c b/libr/arch/p/pyc/opcode_30.c similarity index 100% rename from libr/asm/arch/pyc/opcode_30.c rename to libr/arch/p/pyc/opcode_30.c diff --git a/libr/asm/arch/pyc/opcode_31.c b/libr/arch/p/pyc/opcode_31.c similarity index 100% rename from libr/asm/arch/pyc/opcode_31.c rename to libr/arch/p/pyc/opcode_31.c diff --git a/libr/asm/arch/pyc/opcode_32.c b/libr/arch/p/pyc/opcode_32.c similarity index 100% rename from libr/asm/arch/pyc/opcode_32.c rename to libr/arch/p/pyc/opcode_32.c diff --git a/libr/asm/arch/pyc/opcode_33.c b/libr/arch/p/pyc/opcode_33.c similarity index 100% rename from libr/asm/arch/pyc/opcode_33.c rename to libr/arch/p/pyc/opcode_33.c diff --git a/libr/asm/arch/pyc/opcode_34.c b/libr/arch/p/pyc/opcode_34.c similarity index 100% rename from libr/asm/arch/pyc/opcode_34.c rename to libr/arch/p/pyc/opcode_34.c diff --git a/libr/asm/arch/pyc/opcode_35.c b/libr/arch/p/pyc/opcode_35.c similarity index 100% rename from libr/asm/arch/pyc/opcode_35.c rename to libr/arch/p/pyc/opcode_35.c diff --git a/libr/asm/arch/pyc/opcode_36.c b/libr/arch/p/pyc/opcode_36.c similarity index 100% rename from libr/asm/arch/pyc/opcode_36.c rename to libr/arch/p/pyc/opcode_36.c diff --git a/libr/asm/arch/pyc/opcode_37.c b/libr/arch/p/pyc/opcode_37.c similarity index 100% rename from libr/asm/arch/pyc/opcode_37.c rename to libr/arch/p/pyc/opcode_37.c diff --git a/libr/asm/arch/pyc/opcode_38.c b/libr/arch/p/pyc/opcode_38.c similarity index 100% rename from libr/asm/arch/pyc/opcode_38.c rename to libr/arch/p/pyc/opcode_38.c diff --git a/libr/asm/arch/pyc/opcode_39.c b/libr/arch/p/pyc/opcode_39.c similarity index 100% rename from libr/asm/arch/pyc/opcode_39.c rename to libr/arch/p/pyc/opcode_39.c diff --git a/libr/asm/arch/pyc/opcode_3x.c b/libr/arch/p/pyc/opcode_3x.c similarity index 100% rename from libr/asm/arch/pyc/opcode_3x.c rename to libr/arch/p/pyc/opcode_3x.c diff --git a/libr/asm/arch/pyc/opcode_all.c b/libr/arch/p/pyc/opcode_all.c similarity index 100% rename from libr/asm/arch/pyc/opcode_all.c rename to libr/arch/p/pyc/opcode_all.c diff --git a/libr/asm/arch/pyc/opcode_anal.c b/libr/arch/p/pyc/opcode_anal.c similarity index 100% rename from libr/asm/arch/pyc/opcode_anal.c rename to libr/arch/p/pyc/opcode_anal.c diff --git a/libr/asm/arch/pyc/opcode_arg_fmt.c b/libr/arch/p/pyc/opcode_arg_fmt.c similarity index 100% rename from libr/asm/arch/pyc/opcode_arg_fmt.c rename to libr/arch/p/pyc/opcode_arg_fmt.c diff --git a/libr/anal/p/anal_pyc.c b/libr/arch/p/pyc/plugin.c similarity index 60% rename from libr/anal/p/anal_pyc.c rename to libr/arch/p/pyc/plugin.c index 1fba898086..46801a7051 100644 --- a/libr/anal/p/anal_pyc.c +++ b/libr/arch/p/pyc/plugin.c @@ -1,45 +1,38 @@ -/* radare - LGPL3 - Copyright 2016-2022 - FXTi */ +/* radare - LGPL3 - Copyright 2016-2023 - FXTi, pancake */ #include -#include -#include "../../asm/arch/pyc/pyc_dis.h" - -static R_TH_LOCAL pyc_opcodes *ops = NULL; - -static int disassemble(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len) { - RList *shared = NULL; - - RBin *bin = a->binb.bin; +#include +#include "pyc_dis.h" +static bool disassemble(RArchSession *s, RAnalOp *op, RArchDecodeMask mask) { + RBin *bin = s->arch->binb.bin; RBinPlugin *plugin = bin && bin->cur && bin->cur->o? bin->cur->o->plugin: NULL; - - if (plugin) { - if (!strcmp (plugin->name, "pyc")) { - shared = bin->cur->o->bin_obj; - } - } + RList *shared = (plugin && !strcmp (plugin->name, "pyc"))? + bin->cur->o->bin_obj: NULL; RList *cobjs = NULL; RList *interned_table = NULL; if (shared) { cobjs = r_list_get_n (shared, 0); interned_table = r_list_get_n (shared, 1); } - if (!ops || !pyc_opcodes_equal (ops, a->config->cpu)) { - ops = get_opcode_by_version (a->config->cpu); + pyc_opcodes *ops = s->data; + if (!ops || !pyc_opcodes_equal (ops, s->config->cpu)) { + ops = get_opcode_by_version (s->config->cpu); if (!ops) { ops = get_opcode_by_version ("v3.9.0"); if (!ops) { - return 0; + return false; } } - ops->bits = a->config->bits; + ops->bits = s->config->bits; } - int r = r_pyc_disasm (op, buf, cobjs, interned_table, addr, ops); + s->data = ops; + int r = r_pyc_disasm (op, op->bytes, cobjs, interned_table, op->addr, ops); op->size = r; - return r; + return r > 0; } -static int archinfo(RAnal *anal, int query) { +static int archinfo(RArchSession *anal, ut32 query) { switch (query) { case R_ANAL_ARCHINFO_MIN_OP_SIZE: return (anal->config->bits == 16)? 1: 2; @@ -50,7 +43,7 @@ static int archinfo(RAnal *anal, int query) { } } -static char *get_reg_profile(RAnal *anal) { +static char *regs(RArchSession *as) { return strdup ( "=PC pc\n" "=BP bp\n" @@ -71,68 +64,59 @@ static char *get_reg_profile(RAnal *anal) { ); } -static bool set_reg_profile(RAnal *anal) { - char *rp = get_reg_profile (anal); - if (rp) { - bool b = r_reg_set_profile_string (anal->reg, rp); - free (rp); - return b; - } - return false; -} - -static RList *get_pyc_code_obj(RAnal *anal) { - RBin *b = anal->binb.bin; +static RList *get_pyc_code_obj(RArchSession *as) { + RBin *b = as->arch->binb.bin; RBinPlugin *plugin = b->cur && b->cur->o? b->cur->o->plugin: NULL; bool is_pyc = (plugin && strcmp (plugin->name, "pyc") == 0); return is_pyc? b->cur->o->bin_obj: NULL; } -static int pyc_op(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *data, int len, RAnalOpMask mask) { - RList *pyobj = get_pyc_code_obj (a); +static bool decode(RArchSession *as, RAnalOp *op, RArchDecodeMask mask) { + RList *pyobj = get_pyc_code_obj (as); if (!pyobj) { - return -1; + return false; } + const ut64 addr = op->addr; + const ut8 *data = op->bytes; RList *cobjs = r_list_get_n (pyobj, 0); RListIter *iter = NULL; pyc_code_object *func = NULL, *t = NULL; r_list_foreach (cobjs, iter, t) { - if (R_BETWEEN (t->start_offset, addr, t->end_offset - 1)) { // addr in [start_offset, end_offset) + if (R_BETWEEN (t->start_offset, addr, t->end_offset - 1)) { func = t; break; } } if (!func) { - return -1; + return false; } if (mask & R_ARCH_OP_MASK_DISASM) { - disassemble (a, op, addr, data, len); + disassemble (as, op, mask); } ut64 func_base = func->start_offset; ut32 extended_arg = 0, oparg = 0; ut8 op_code = data[0]; - op->addr = addr; op->sign = true; op->type = R_ANAL_OP_TYPE_ILL; op->id = op_code; - if (!ops || !pyc_opcodes_equal (ops, a->config->cpu)) { - if (!(ops = get_opcode_by_version (a->config->cpu))) { - return -1; + pyc_opcodes *ops = as->data; + if (!ops || !pyc_opcodes_equal (ops, as->config->cpu)) { + if (!(ops = get_opcode_by_version (as->config->cpu))) { + return false; } } - int bits = a->config->bits; + const int bits = as->config->bits; bool is_python36 = bits == 8; pyc_opcode_object *op_obj = &ops->opcodes[op_code]; if (!op_obj->op_name) { op->type = R_ANAL_OP_TYPE_ILL; op->size = 1; - goto anal_end; + goto beach; } op->size = is_python36? 2: ((op_code >= ops->have_argument)? 3: 1); - if (op_code >= ops->have_argument) { if (!is_python36) { oparg = data[1] + data[2] * 256 + extended_arg; @@ -149,7 +133,7 @@ static int pyc_op(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *data, int len, RA op->type = R_ANAL_OP_TYPE_CJMP; op->fail = addr + ((is_python36)? 2: 3); } - goto anal_end; + goto beach; } if (op_obj->type & HASJREL) { op->type = R_ANAL_OP_TYPE_JMP; @@ -160,47 +144,42 @@ static int pyc_op(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *data, int len, RA op->type = R_ANAL_OP_TYPE_CJMP; //op->fail = addr + ((is_python36)? 2: 3); } - //goto anal_end; } - if (op_obj->type & HASCOMPARE) { op->type = R_ANAL_OP_TYPE_CMP; - goto anal_end; + goto beach; } anal_pyc_op (op, op_obj, oparg); - -anal_end: - //free_opcode (ops); - return op->size; +beach: + return op->size > 0; } -static int finish(void *user) { +static bool finish(RArchSession *s) { + pyc_opcodes *ops = s->data; if (ops) { free_opcode (ops); - ops = NULL; + s->data = NULL; } - return 0; + return true; } -RAnalPlugin r_anal_plugin_pyc = { +RArchPlugin r_arch_plugin_pyc = { .name = "pyc", .desc = "Python bytecode analysis plugin", .license = "LGPL3", .arch = "pyc", - .bits = 32, - .archinfo = archinfo, - .get_reg_profile = get_reg_profile, - .set_reg_profile = &set_reg_profile, - .op = &pyc_op, - .esil = false, + .bits = R_SYS_BITS_PACK1 (32), + .info = archinfo, + .regs = regs, + .decode = &decode, .fini = &finish, }; #ifndef R2_PLUGIN_INCORE R_API RLibStruct radare_plugin = { - .type = R_LIB_TYPE_ANAL, - .data = &r_anal_plugin_pyc, + .type = R_LIB_TYPE_ARCH, + .data = &r_arch_plugin_pyc, .version = R2_VERSION }; #endif diff --git a/libr/asm/arch/pyc/pyc_dis.c b/libr/arch/p/pyc/pyc_dis.c similarity index 100% rename from libr/asm/arch/pyc/pyc_dis.c rename to libr/arch/p/pyc/pyc_dis.c diff --git a/libr/asm/arch/pyc/pyc_dis.h b/libr/arch/p/pyc/pyc_dis.h similarity index 100% rename from libr/asm/arch/pyc/pyc_dis.h rename to libr/arch/p/pyc/pyc_dis.h diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index 3970e3e4bc..9b6913f748 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -1594,7 +1594,6 @@ extern RAnalPlugin r_anal_plugin_x86_im; extern RAnalPlugin r_anal_plugin_x86_simple; extern RAnalPlugin r_anal_plugin_x86_udis; extern RAnalPlugin r_anal_plugin_xcore_cs; -extern RAnalPlugin r_anal_plugin_pyc; extern RAnalPlugin r_anal_plugin_pickle; extern RAnalPlugin r_anal_plugin_evm_cs; extern RAnalPlugin r_anal_plugin_hppa_gnu; diff --git a/libr/include/r_arch.h b/libr/include/r_arch.h index e49160a72b..3102ed10cd 100644 --- a/libr/include/r_arch.h +++ b/libr/include/r_arch.h @@ -325,6 +325,7 @@ extern RArchPlugin r_arch_plugin_pdp11; extern RArchPlugin r_arch_plugin_lh5801; extern RArchPlugin r_arch_plugin_ebc; extern RArchPlugin r_arch_plugin_msp430; +extern RArchPlugin r_arch_plugin_pyc; #ifdef __cplusplus } diff --git a/libr/meson.build b/libr/meson.build index dcdd8fdfcb..070f893c3f 100644 --- a/libr/meson.build +++ b/libr/meson.build @@ -7,6 +7,7 @@ asm_plugins = [ 'null' ] anal_plugins = [ 'null' ] arch_plugins = [ 'null', # 'arc', + 'pyc', 'lh5801', 'ebc', 'msp430', @@ -152,7 +153,6 @@ endif # else if no_user_plugins -# TODO: add the pyc plugin for meson builds too anal_plugins += [ '6502_cs', '8051',