Move parse into arch. (#4342)

This commit is contained in:
Giovanni 2024-03-07 23:50:22 +08:00 committed by GitHub
parent 0de960d891
commit 64f8a5ae57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 186 additions and 546 deletions

View file

@ -15,7 +15,6 @@ rizin_exe = executable('rizin', 'rizin.c',
rz_bp_dep,
rz_reg_dep,
rz_syscall_dep,
rz_parse_dep,
rz_egg_dep,
rz_search_dep,
rz_hash_dep,

View file

@ -28,3 +28,10 @@ RZ_DEPRECATE RZ_API RZ_BORROW RzAnalysisPlugin *rz_arch_get_analysis_plugin(size
}
return arch_static_plugins[index]->p_analysis;
}
RZ_DEPRECATE RZ_API RZ_BORROW RzParsePlugin *rz_arch_get_parse_plugin(size_t index) {
if (index >= RZ_ARRAY_SIZE(arch_static_plugins)) {
return NULL;
}
return arch_static_plugins[index]->p_parse;
}

View file

@ -11,12 +11,21 @@
RzArchPlugin rz_arch_plugin_##name = { \
.p_asm = &rz_asm_plugin_##name, \
.p_analysis = &rz_analysis_plugin_##name, \
.p_parse = &rz_parse_plugin_##name##_pseudo, \
}
#define DEPRECATED_OLD_ARCH_NO_PARSE_PLUGIN(name) \
RzArchPlugin rz_arch_plugin_##name = { \
.p_asm = &rz_asm_plugin_##name, \
.p_analysis = &rz_analysis_plugin_##name, \
.p_parse = NULL, \
}
#define DEPRECATED_OLD_ARCH_ASM_ONLY_PLUGIN(name) \
RzArchPlugin rz_arch_plugin_##name = { \
.p_asm = &rz_asm_plugin_##name, \
.p_analysis = NULL, \
.p_parse = NULL, \
}
#ifndef RZ_PLUGIN_INCORE
@ -30,10 +39,14 @@
#define ARCH_PLUGIN_LIB_STRUCT(name)
#endif
#define RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(name) \
#define RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(name) \
DEPRECATED_OLD_ARCH_PLUGIN(name); \
ARCH_PLUGIN_LIB_STRUCT(name)
#define RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(name) \
DEPRECATED_OLD_ARCH_NO_PARSE_PLUGIN(name); \
ARCH_PLUGIN_LIB_STRUCT(name)
#define RZ_ARCH_ASM_ONLY_PLUGIN_DEFINE_DEPRECATED(name) \
DEPRECATED_OLD_ARCH_ASM_ONLY_PLUGIN(name); \
ARCH_PLUGIN_LIB_STRUCT(name)

View file

@ -3,12 +3,12 @@
// SPDX-FileCopyrightText: 2009-2019 maijin <maijin21@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only
#include "rz_util/rz_str.h"
#include <rz_util/rz_regex.h>
#include <stdio.h>
#include <rz_util/rz_str.h>
#include <rz_util/rz_regex.h>
#include <rz_types.h>
#include <rz_parse.h>
#include <rz_arch.h>
#define isx86separator(x) ( \
(x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r' || (x) == ' ' || \

View file

@ -360,6 +360,7 @@ arch_common_sources = [
'data.c',
'dwarf_process.c',
'fcn.c',
'filter.c',
'function.c',
'hint.c',
'il_trace.c',
@ -367,6 +368,8 @@ arch_common_sources = [
'labels.c',
'meta.c',
'op.c',
'parse.c',
'parse_helper.c',
'pdb_process.c',
'platform_profile.c',
'platform_target_index.c',
@ -415,7 +418,6 @@ rz_arch = library('rz_arch', rz_arch_sources,
rz_flag_dep,
rz_hash_dep,
rz_diff_dep,
rz_parse_dep,
rz_bin_dep,
rz_type_dep,
rz_il_dep,
@ -445,7 +447,6 @@ modules += { 'rz_arch': {
'rz_flag',
'rz_hash',
'rz_il',
'rz_parse',
'rz_reg',
'rz_search',
'rz_syscall',

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_6502.c"
#include "asm/asm_6502.c"
#include "parse/parse_6502_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(6502);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(6502);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_arm_cs.c"
#include "asm/asm_arm_cs.c"
#include "parse/parse_arm_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(arm_cs);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(arm_cs);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_avr.c"
#include "asm/asm_avr.c"
#include "parse/parse_avr_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(avr);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(avr);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_chip8.c"
#include "asm/asm_chip8.c"
#include "parse/parse_chip8_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(chip8);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(chip8);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_dalvik.c"
#include "asm/asm_dalvik.c"
#include "parse/parse_dalvik_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(dalvik);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(dalvik);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_m68k_cs.c"
#include "asm/asm_m68k_cs.c"
#include "parse/parse_m68k_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(m68k_cs);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(m68k_cs);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_mips_cs.c"
#include "asm/asm_mips_cs.c"
#include "parse/parse_mips_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(mips_cs);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(mips_cs);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_ppc_cs.c"
#include "asm/asm_ppc_cs.c"
#include "parse/parse_ppc_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(ppc_cs);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(ppc_cs);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_sh.c"
#include "asm/asm_sh.c"
#include "parse/parse_sh_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(sh);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(sh);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_tms320.c"
#include "asm/asm_tms320.c"
#include "parse/parse_tms320_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(tms320);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(tms320);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_v850.c"
#include "asm/asm_v850.c"
#include "parse/parse_v850_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(v850);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(v850);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_wasm.c"
#include "asm/asm_wasm.c"
#include "parse/parse_wasm_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(wasm);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(wasm);

View file

@ -6,5 +6,6 @@
#include "analysis/analysis_x86_cs.c"
#include "asm/asm_x86_cs.c"
#include "parse/parse_x86_pseudo.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(x86_cs);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(x86_cs);

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *_6502_tokenize(const char *assembly, size_t length);
@ -123,11 +123,3 @@ RzParsePlugin rz_parse_plugin_6502_pseudo = {
.desc = "6502 pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_6502_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -13,7 +13,7 @@
#include <rz_util/rz_regex.h>
#include <rz_vector.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *arm_tokenize(const char *assembly, size_t length);
@ -392,17 +392,9 @@ static bool subvar(RzParse *p, RzAnalysisFunction *f, RzAnalysisOp *op, char *da
return true;
}
RzParsePlugin rz_parse_plugin_arm_pseudo = {
RzParsePlugin rz_parse_plugin_arm_cs_pseudo = {
.name = "arm.pseudo",
.desc = "ARM/ARM64 pseudo syntax",
.parse = parse,
.subvar = &subvar,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_arm_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *avr_tokenize(const char *assembly, size_t length);
@ -155,11 +155,3 @@ RzParsePlugin rz_parse_plugin_avr_pseudo = {
.desc = "AVR pseudo syntax",
.parse = parse
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_avr_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -112,11 +112,3 @@ RzParsePlugin rz_parse_plugin_chip8_pseudo = {
.desc = "chip8 pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_chip8_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *dalvik_tokenize(const char *assembly, size_t length);
@ -264,11 +264,3 @@ RzParsePlugin rz_parse_plugin_dalvik_pseudo = {
.fini = NULL,
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_dalvik_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *m68k_tokenize(const char *assembly, size_t length);
@ -99,16 +99,8 @@ static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
return res;
}
RzParsePlugin rz_parse_plugin_m68k_pseudo = {
RzParsePlugin rz_parse_plugin_m68k_cs_pseudo = {
.name = "m68k.pseudo",
.desc = "M68K pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_m68k_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,10 +11,10 @@
#include <rz_flag.h>
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include <rz_util/rz_regex.h>
#include "parse_helper.h"
static RzList /*<char *>*/ *mips_tokenize(const char *assembly, size_t length);
static const RzPseudoGrammar mips_lexicon[] = {
@ -253,7 +253,7 @@ static bool subvar(RzParse *p, RzAnalysisFunction *f, RzAnalysisOp *op, char *da
return ret;
}
RzParsePlugin rz_parse_plugin_mips_pseudo = {
RzParsePlugin rz_parse_plugin_mips_cs_pseudo = {
.name = "mips.pseudo",
.desc = "MIPS pseudo syntax",
.init = NULL,
@ -261,11 +261,3 @@ RzParsePlugin rz_parse_plugin_mips_pseudo = {
.parse = parse,
.subvar = subvar,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_mips_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -96,18 +96,6 @@
#define PPC_UT64(x) (strtol(x, NULL, 16))
#define PPC_UT32(x) ((ut32)PPC_UT64(x))
static ut64 mask64(ut64 mb, ut64 me) {
ut64 maskmb = UT64_MAX >> mb;
ut64 maskme = UT64_MAX << (63 - me);
return (mb <= me) ? maskmb & maskme : maskmb | maskme;
}
static ut32 mask32(ut32 mb, ut32 me) {
ut32 maskmb = UT32_MAX >> mb;
ut32 maskme = UT32_MAX << (31 - me);
return (mb <= me) ? maskmb & maskme : maskmb | maskme;
}
static int can_replace(const char *str, int idx, int max_operands) {
if (str[idx] < 'A' || str[idx] > 'J') {
return false;
@ -121,7 +109,7 @@ static int can_replace(const char *str, int idx, int max_operands) {
return true;
}
static const char *getspr(const char *reg) {
static const char *getspr_pseudo(const char *reg) {
static char cspr[16];
ut32 spr = 0;
if (!reg) {
@ -1587,7 +1575,7 @@ static int replace(int argc, const char *argv[], char *newstr) {
break;
}
} else if ((i == 44 && letter == 2) || (i == 45 && letter == 1)) { // spr
w = getspr(w);
w = getspr_pseudo(w);
}
if (w != NULL) {
strcpy(newstr + k, w);
@ -1754,16 +1742,8 @@ static bool parse(RzParse *p, const char *data, RzStrBuf *sb) {
return true;
}
RzParsePlugin rz_parse_plugin_ppc_pseudo = {
RzParsePlugin rz_parse_plugin_ppc_cs_pseudo = {
.name = "ppc.pseudo",
.desc = "PowerPC pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_ppc_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *sh_tokenize(const char *assembly, size_t length);
@ -178,11 +178,3 @@ RzParsePlugin rz_parse_plugin_sh_pseudo = {
.desc = "SH-4 pseudo syntax",
.parse = parse
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_sh_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -7,7 +7,7 @@
#include <rz_parse.h>
// https://www.ti.com/lit/ug/spru732j/spru732j.pdf
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *tms320_tokenize(const char *assembly, size_t length);
@ -122,11 +122,3 @@ RzParsePlugin rz_parse_plugin_tms320_pseudo = {
.desc = "tms320 pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_tms320_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -8,7 +8,7 @@
// https://www.renesas.com/us/en/doc/products/mpumcu/doc/v850/r01us0037ej0100_v850e2.pdf
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *v850_tokenize(const char *assembly, size_t length);
@ -141,11 +141,3 @@ RzParsePlugin rz_parse_plugin_v850_pseudo = {
.desc = "v850 pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_v850_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -41,11 +41,3 @@ RzParsePlugin rz_parse_plugin_wasm_pseudo = {
.desc = "WASM pseudo syntax",
.subvar = &subvar,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_wasm_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -461,17 +461,9 @@ static bool subvar(RzParse *p, RzAnalysisFunction *f, RzAnalysisOp *op, char *da
return ret;
}
RzParsePlugin rz_parse_plugin_x86_pseudo = {
RzParsePlugin rz_parse_plugin_x86_cs_pseudo = {
.name = "x86.pseudo",
.desc = "X86 pseudo syntax",
.parse = &parse,
.subvar = &subvar,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_x86_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -10,5 +10,6 @@
#include "analysis/analysis_riscv_gnu.c"
#include "asm/asm_riscv_gnu.c"
#include "parse/parse_riscv_gnu.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(riscv_gnu);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(riscv_gnu);

View file

@ -6,7 +6,9 @@
#include "../isa_gnu/z80/z80.c"
#include "../isa_gnu/z80/z80asm.c"
#include "analysis/analysis_z80_gnu.c"
#include "asm/asm_z80_gnu.c"
#include "parse/parse_z80_gnu.c"
RZ_ARCH_PLUGIN_DEFINE_DEPRECATED(z80_gnu);
RZ_ARCH_WITH_PARSE_PLUGIN_DEFINE_DEPRECATED(z80_gnu);

View file

@ -219,16 +219,8 @@ static bool parse(RzParse *p, const char *data, RzStrBuf *sb) {
return true;
}
RzParsePlugin rz_parse_plugin_riscv_pseudo = {
RzParsePlugin rz_parse_plugin_riscv_gnu_pseudo = {
.name = "riscv.pseudo",
.desc = "riscv pseudo syntax",
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_riscv_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -11,7 +11,7 @@
#include <rz_analysis.h>
#include <rz_parse.h>
#include "parse_common.c"
#include "parse_helper.h"
static RzList /*<char *>*/ *z80_tokenize(const char *assembly, size_t length);
@ -153,18 +153,10 @@ static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
return rz_pseudo_convert(&z80_config, assembly, sb);
}
RzParsePlugin rz_parse_plugin_z80_pseudo = {
RzParsePlugin rz_parse_plugin_z80_gnu_pseudo = {
.name = "z80.pseudo",
.desc = "z80 pseudo syntax",
.init = NULL,
.fini = NULL,
.parse = parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_z80_pseudo,
.version = RZ_VERSION
};
#endif

View file

@ -6,16 +6,10 @@
#include <stdio.h>
#include <rz_types.h>
#include <rz_parse.h>
#include <rz_arch.h>
#include <rz_lib.h>
#include "rz_parse_plugins.h"
RZ_LIB_VERSION(rz_parse);
static RzParsePlugin *parse_static_plugins[] = { RZ_PARSE_STATIC_PLUGINS };
RZ_API RzParse *rz_parse_new(void) {
int i;
RzParse *p = RZ_NEW0(RzParse);
if (!p) {
return NULL;
@ -32,8 +26,14 @@ RZ_API RzParse *rz_parse_new(void) {
p->subtail = false;
p->minval = 0x100;
p->localvar_only = false;
for (i = 0; i < RZ_ARRAY_SIZE(parse_static_plugins); i++) {
rz_parse_plugin_add(p, parse_static_plugins[i]);
const size_t n_plugins = rz_arch_get_n_plugins();
for (size_t i = 0; i < n_plugins; i++) {
RzParsePlugin *plugin = rz_arch_get_parse_plugin(i);
if (!plugin) {
continue;
}
rz_parse_plugin_add(p, plugin);
}
return p;
}

View file

@ -1,7 +1,9 @@
// SPDX-FileCopyrightText: 2018-2021 deroad <wargio@libero.it>
// SPDX-FileCopyrightText: 2018-2024 deroad <wargio@libero.it>
// SPDX-License-Identifier: LGPL-3.0-only
/** \file parse_common.c
#include "parse_helper.h"
/** \file parse_helper.c
* This file contains a common code that can be used to convert any asm code
* into a pseudo code, via a generic grammar.
*
@ -27,80 +29,7 @@
* and
*/
typedef struct {
const char *mnemonic;
size_t mnemonic_length;
const char *grammar;
} RzPseudoGrammar;
typedef struct {
const char *expected;
const char *pseudo;
} RzPseudoDirect;
typedef struct {
const char *expected;
const char *replace;
int flag; // 0 for first match, 1 for all matches
} RzPseudoReplace;
typedef struct {
const RzPseudoDirect *direct;
size_t direct_length;
const RzPseudoReplace *replace;
size_t replace_length;
const RzPseudoGrammar *lexicon;
size_t lexicon_length;
int max_args;
RzList /*<char *>*/ *(*tokenize)(const char *assembly, size_t length);
} RzPseudoConfig;
#define RZ_PSEUDO_DEFINE_GRAMMAR(x, y) \
{ .mnemonic = x, .mnemonic_length = sizeof(x) - 1, .grammar = y }
#define RZ_PSEUDO_DEFINE_DIRECT(x, y) \
{ .expected = x, .pseudo = y }
#define RZ_PSEUDO_DEFINE_REPLACE(x, y, f) \
{ .expected = x, .replace = y, .flag = f }
#define RZ_PSEUDO_DEFINE_CONFIG(d, l, r, m, t) \
{ \
.direct = d, \
.direct_length = RZ_ARRAY_SIZE(d), \
.replace = r, \
.replace_length = RZ_ARRAY_SIZE(r), \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
#define RZ_PSEUDO_DEFINE_CONFIG_NO_DIRECT(l, r, m, t) \
{ \
.direct = NULL, \
.direct_length = 0, \
.replace = r, \
.replace_length = RZ_ARRAY_SIZE(r), \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
#define RZ_PSEUDO_DEFINE_CONFIG_ONLY_LEXICON(l, m, t) \
{ \
.direct = NULL, \
.direct_length = 0, \
.replace = NULL, \
.replace_length = 0, \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
static bool rz_pseudo_convert(const RzPseudoConfig *config, const char *assembly, RzStrBuf *sb) {
RZ_IPI bool rz_pseudo_convert(const RzPseudoConfig *config, const char *assembly, RzStrBuf *sb) {
rz_return_val_if_fail(config && config->tokenize && config->lexicon, false);
size_t i, p;

84
librz/arch/parse_helper.h Normal file
View file

@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2024 deroad <wargio@libero.it>
// SPDX-License-Identifier: LGPL-3.0-only
#ifndef PARSE_HELPER_H
#define PARSE_HELPER_H
#include <rz_parse.h>
typedef struct {
const char *mnemonic;
size_t mnemonic_length;
const char *grammar;
} RzPseudoGrammar;
typedef struct {
const char *expected;
const char *pseudo;
} RzPseudoDirect;
typedef struct {
const char *expected;
const char *replace;
int flag; // 0 for first match, 1 for all matches
} RzPseudoReplace;
typedef struct {
const RzPseudoDirect *direct;
size_t direct_length;
const RzPseudoReplace *replace;
size_t replace_length;
const RzPseudoGrammar *lexicon;
size_t lexicon_length;
int max_args;
RzList /*<char *>*/ *(*tokenize)(const char *assembly, size_t length);
} RzPseudoConfig;
#define RZ_PSEUDO_DEFINE_GRAMMAR(x, y) \
{ .mnemonic = x, .mnemonic_length = sizeof(x) - 1, .grammar = y }
#define RZ_PSEUDO_DEFINE_DIRECT(x, y) \
{ .expected = x, .pseudo = y }
#define RZ_PSEUDO_DEFINE_REPLACE(x, y, f) \
{ .expected = x, .replace = y, .flag = f }
#define RZ_PSEUDO_DEFINE_CONFIG(d, l, r, m, t) \
{ \
.direct = d, \
.direct_length = RZ_ARRAY_SIZE(d), \
.replace = r, \
.replace_length = RZ_ARRAY_SIZE(r), \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
#define RZ_PSEUDO_DEFINE_CONFIG_NO_DIRECT(l, r, m, t) \
{ \
.direct = NULL, \
.direct_length = 0, \
.replace = r, \
.replace_length = RZ_ARRAY_SIZE(r), \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
#define RZ_PSEUDO_DEFINE_CONFIG_ONLY_LEXICON(l, m, t) \
{ \
.direct = NULL, \
.direct_length = 0, \
.replace = NULL, \
.replace_length = 0, \
.lexicon = l, \
.lexicon_length = RZ_ARRAY_SIZE(l), \
.max_args = m, \
.tokenize = t, \
}
RZ_IPI bool rz_pseudo_convert(const RzPseudoConfig *config, const char *assembly, RzStrBuf *sb);
#endif /* PARSE_HELPER_H */

View file

@ -42,11 +42,8 @@ CB(hash, hash)
static bool lib_arch_cb(RzLibPlugin *pl, void *user, void *data) {
RzArchPlugin *hand = (RzArchPlugin *)data;
RzCore *core = (RzCore *)user;
if (!hand->p_asm && !hand->p_analysis) {
// TODO: add new structure.
// return rz_arch_plugin_add(core->arch, hand);
return false;
}
// TODO: add new structure.
// return rz_arch_plugin_add(core->arch, hand);
if (hand->p_asm && !rz_asm_plugin_add(core->rasm, hand->p_asm)) {
// deprecated structure
return false;
@ -55,6 +52,10 @@ static bool lib_arch_cb(RzLibPlugin *pl, void *user, void *data) {
// deprecated structure
return false;
}
if (hand->p_parse && !rz_parse_plugin_add(core->parser, hand->p_parse)) {
// deprecated structure
return false;
}
return true;
}

View file

@ -148,7 +148,6 @@ rz_core_deps = [
rz_reg_dep,
rz_bp_dep,
rz_syscall_dep,
rz_parse_dep,
rz_egg_dep,
rz_search_dep,
rz_sign_dep,
@ -199,7 +198,6 @@ modules += { 'rz_core': {
'rz_lang',
'rz_hash',
'rz_flag',
'rz_parse',
'rz_egg',
'rz_debug',
'rz_crypto',

View file

@ -8,6 +8,7 @@
#include <rz_types.h>
#include <rz_analysis.h>
#include <rz_asm.h>
#include <rz_parse.h>
#ifdef __cplusplus
extern "C" {
@ -18,11 +19,13 @@ RZ_LIB_VERSION_HEADER(rz_arch);
typedef struct rz_arch_plugin_t {
RZ_DEPRECATE RzAsmPlugin *p_asm; ///< Assembly Plugin
RZ_DEPRECATE RzAnalysisPlugin *p_analysis; ///< Analysis Plugin
RZ_DEPRECATE RzParsePlugin *p_parse; ///< Parse Plugin
} RzArchPlugin;
RZ_DEPRECATE RZ_API const size_t rz_arch_get_n_plugins();
RZ_DEPRECATE RZ_API RZ_BORROW RzAsmPlugin *rz_arch_get_asm_plugin(size_t index);
RZ_DEPRECATE RZ_API RZ_BORROW RzAnalysisPlugin *rz_arch_get_analysis_plugin(size_t index);
RZ_DEPRECATE RZ_API RZ_BORROW RzParsePlugin *rz_arch_get_parse_plugin(size_t index);
#ifdef __cplusplus
}

View file

@ -13,8 +13,6 @@
extern "C" {
#endif
RZ_LIB_VERSION_HEADER(rz_parse);
typedef struct rz_parse_t {
void *user;
RzSpace *flagspace;

View file

@ -26,7 +26,6 @@ rz_main_deps = [
rz_reg_dep,
rz_bp_dep,
rz_syscall_dep,
rz_parse_dep,
rz_arch_dep,
rz_egg_dep,
rz_search_dep,

View file

@ -53,7 +53,6 @@ static int rz_main_version_verify(int show) {
#if !USE_LIB_MAGIC
{ "rz_magic", rz_magic_version },
#endif
{ "rz_parse", rz_parse_version },
{ "rz_reg", rz_reg_version },
{ "rz_sign", rz_sign_version },
{ "rz_search", rz_search_version },

View file

@ -21,7 +21,6 @@ subdir('reg')
subdir('type')
subdir('bin')
subdir('config')
subdir('parse')
subdir('lang')
subdir('il')
subdir('arch')

View file

View file

@ -1,81 +0,0 @@
parse_plugins_list = [
'6502_pseudo',
'arm_pseudo',
'att2intel',
'avr_pseudo',
'chip8_pseudo',
'dalvik_pseudo',
'm68k_pseudo',
'mips_pseudo',
'ppc_pseudo',
'sh_pseudo',
'tms320_pseudo',
'v850_pseudo',
'wasm_pseudo',
'x86_pseudo',
]
rz_parse_sources = [
'filter.c',
'parse.c',
'p/parse_6502_pseudo.c',
'p/parse_arm_pseudo.c',
'p/parse_att2intel.c',
'p/parse_avr_pseudo.c',
'p/parse_chip8_pseudo.c',
'p/parse_dalvik_pseudo.c',
'p/parse_m68k_pseudo.c',
'p/parse_mips_pseudo.c',
'p/parse_ppc_pseudo.c',
'p/parse_sh_pseudo.c',
'p/parse_tms320_pseudo.c',
'p/parse_v850_pseudo.c',
'p/parse_wasm_pseudo.c',
'p/parse_x86_pseudo.c',
]
if get_option('use_gpl')
parse_plugins_list += [
'riscv_pseudo',
'z80_pseudo',
]
rz_parse_sources += [
'p/parse_riscv_pseudo.c',
'p/parse_z80_pseudo.c',
]
endif
parse_plugins = {
'base_name': 'rz_parse',
'base_struct': 'RzParsePlugin',
'list': parse_plugins_list,
}
rz_parse = library('rz_parse', rz_parse_sources,
include_directories: platform_inc,
dependencies: [
rz_util_dep,
rz_flag_dep,
rz_syscall_dep,
rz_reg_dep,
rz_cons_dep,
],
install: true,
implicit_include_directories: false,
install_rpath: rpath_lib,
soversion: rizin_libversion,
version: rizin_version,
name_suffix: lib_name_suffix,
name_prefix: lib_name_prefix,
)
rz_parse_dep = declare_dependency(link_with: rz_parse,
include_directories: platform_inc)
meson.override_dependency('rz_parse', rz_parse_dep)
modules += { 'rz_parse': {
'target': rz_parse,
'dependencies': ['rz_util', 'rz_flag', 'rz_syscall', 'rz_reg', 'rz_cons'],
'plugins': [parse_plugins]
}}

View file

@ -1,189 +0,0 @@
// SPDX-FileCopyrightText: 2011 pancake <pancake@nopcode.org>
// SPDX-License-Identifier: LGPL-3.0-only
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rz_lib.h>
#include <rz_util.h>
#include <rz_flag.h>
#include <rz_analysis.h>
#include <rz_parse.h>
static int replace(int argc, const char *argv[], char *newstr) {
int i, j, k;
struct {
char *op;
char *str;
} ops[] = {
{ "cmpl", "cmp 2, 1" },
{ "testl", "test 2, 1" },
{ "leal", "lea 2, 1" },
{ "movl", "mov 2, 1" },
{ "xorl", "xor 2, 1" },
{ "andl", "and 2, 1" },
{ "orl", "or 2, 1" },
{ "addl", "add 2, 1" },
{ "incl", "inc 1" },
{ "decl", "dec 1" },
{ "subl", "sub 2, 1" },
{ "mull", "mul 2, 1" },
{ "divl", "div 2, 1" },
{ "pushl", "push 1" },
{ "popl", "pop 1" },
{ "ret", "ret" },
{ NULL }
};
for (i = 0; ops[i].op != NULL; i++) {
if (!strcmp(ops[i].op, argv[0])) {
if (newstr != NULL) {
for (j = k = 0; ops[i].str[j] != '\0'; j++, k++) {
if (ops[i].str[j] >= '0' && ops[i].str[j] <= '9') {
const char *w = argv[ops[i].str[j] - '0'];
if (w != NULL) {
strcpy(newstr + k, w);
k += strlen(w) - 1;
}
} else {
newstr[k] = ops[i].str[j];
}
}
newstr[k] = '\0';
}
return true;
}
}
/* TODO: this is slow */
if (newstr != NULL) {
newstr[0] = '\0';
for (i = 0; i < argc; i++) {
strcat(newstr, argv[i]);
strcat(newstr, (i == 0 || i == argc - 1) ? " " : ",");
}
}
return false;
}
static bool parse(RzParse *p, const char *data, RzStrBuf *sb) {
int i, n;
char w0[32];
char w1[32];
char w2[32];
char w3[32];
char str[1024] = { 0 };
char *buf, *ptr, *optr, *num;
// malloc can be slow here :?
buf = strdup(data);
if (!buf) {
return false;
}
rz_str_trim_head(buf);
ptr = strchr(buf, '#');
if (ptr) {
*ptr = 0;
rz_str_trim(buf);
}
if (*buf == '.' || buf[strlen(buf) - 1] == ':') {
free(buf);
strcpy(str, data);
return true;
}
rz_str_replace_char(buf, '$', 0);
rz_str_replace_char(buf, '%', 0);
rz_str_replace_char(buf, '\t', ' ');
rz_str_replace_char(buf, '(', '[');
rz_str_replace_char(buf, ')', ']');
ptr = strchr(buf, '[');
if (ptr) {
*ptr = 0;
num = (char *)rz_str_lchr(buf, ' ');
if (!num) {
num = (char *)rz_str_lchr(buf, ',');
}
if (num) {
n = atoi(num + 1);
*ptr = '[';
rz_str_cpy(num + 1, ptr);
ptr = (char *)rz_str_lchr(buf, ']');
if (n && ptr) {
char *rest = strdup(ptr + 1);
size_t dist = strlen(data) + 1 - (ptr - buf);
snprintf(ptr, dist, "%+d]%s", n, rest);
free(rest);
}
} else {
*ptr = '[';
}
}
if (*buf) {
*w0 = *w1 = *w2 = *w3 = 0;
ptr = strchr(buf, ' ');
if (!ptr) {
ptr = strchr(buf, '\t');
}
if (ptr) {
*ptr = '\0';
for (++ptr; *ptr == ' '; ptr++) {
;
}
strncpy(w0, buf, sizeof(w0) - 1);
strncpy(w1, ptr, sizeof(w1) - 1);
optr = ptr;
ptr = strchr(ptr, ',');
if (ptr) {
*ptr = '\0';
for (++ptr; *ptr == ' '; ptr++) {
;
}
strncpy(w1, optr, sizeof(w1) - 1);
strncpy(w2, ptr, sizeof(w2) - 1);
ptr = strchr(ptr, ',');
if (ptr) {
*ptr = '\0';
for (++ptr; *ptr == ' '; ptr++) {
;
}
strncpy(w2, optr, sizeof(w2) - 1);
strncpy(w3, ptr, sizeof(w3) - 1);
}
}
}
{
const char *wa[] = { w0, w1, w2, w3 };
int nw = 0;
for (i = 0; i < 4; i++) {
if (wa[i][0] != '\0') {
nw++;
}
}
replace(nw, wa, str);
}
}
free(buf);
rz_strbuf_set(sb, str);
return true;
}
RzParsePlugin rz_parse_plugin_att2intel = {
.name = "att2intel",
.desc = "X86 att 2 intel plugin",
.init = NULL,
.fini = NULL,
.parse = &parse,
};
#ifndef RZ_PLUGIN_INCORE
RZ_API RzLibStruct rizin_plugin = {
.type = RZ_LIB_TYPE_PARSE,
.data = &rz_parse_plugin_att2intel,
.version = RZ_VERSION
};
#endif

View file

@ -827,6 +827,5 @@ summary({
'IO Plugins': io_plugins.get('list'),
'Lang Plugins': lang_plugins.get('list'),
'Hash Plugins': comb_hash_plugins,
'Parse Plugins': parse_plugins.get('list'),
'Demangler Plugins': demangler_plugins.get('list'),
}, section: 'Plugins', list_sep: ', ')

View file

@ -4,7 +4,6 @@ CMDS=Lp
EXPECT=<<EOF
6502.pseudo
arm.pseudo
att2intel
avr.pseudo
chip8.pseudo
dalvik.pseudo
@ -25,7 +24,7 @@ NAME=Print the parser plugins in JSON
FILE==
CMDS=Lpj
EXPECT=<<EOF
[{"name":"6502.pseudo"},{"name":"arm.pseudo"},{"name":"att2intel"},{"name":"avr.pseudo"},{"name":"chip8.pseudo"},{"name":"dalvik.pseudo"},{"name":"m68k.pseudo"},{"name":"mips.pseudo"},{"name":"ppc.pseudo"},{"name":"sh.pseudo"},{"name":"tms320.pseudo"},{"name":"v850.pseudo"},{"name":"wasm.pseudo"},{"name":"x86.pseudo"},{"name":"riscv.pseudo"},{"name":"z80.pseudo"}]
[{"name":"6502.pseudo"},{"name":"arm.pseudo"},{"name":"avr.pseudo"},{"name":"chip8.pseudo"},{"name":"dalvik.pseudo"},{"name":"m68k.pseudo"},{"name":"mips.pseudo"},{"name":"ppc.pseudo"},{"name":"sh.pseudo"},{"name":"tms320.pseudo"},{"name":"v850.pseudo"},{"name":"wasm.pseudo"},{"name":"x86.pseudo"},{"name":"riscv.pseudo"},{"name":"z80.pseudo"}]
EOF
RUN

View file

@ -53,7 +53,6 @@ if get_option('enable_tests')
rz_reg_dep,
rz_syscall_dep,
rz_type_dep,
rz_parse_dep,
rz_egg_dep,
rz_search_dep,
rz_hash_dep,

View file

@ -138,7 +138,6 @@ if get_option('enable_tests')
rz_reg_dep,
rz_syscall_dep,
rz_type_dep,
rz_parse_dep,
rz_egg_dep,
rz_search_dep,
rz_hash_dep,