radare2/libr/include/r_asm.h

261 lines
8.1 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2009-2017 - nibble, pancake */
2014-03-27 15:34:17 +00:00
#ifndef R2_ASM_H
#define R2_ASM_H
#include <r_types.h>
2012-07-21 10:11:21 +00:00
#include <r_bin.h> // only for binding, no hard dep required
#include <r_util.h>
#include <r_parse.h>
#ifdef __cplusplus
extern "C" {
#endif
R_LIB_VERSION_HEADER(r_asm);
#define R_ASM_OPCODES_PATH R2_PREFIX "/share/radare2/" R2_VERSION "/opcodes"
// XXX too big!
// the 256th character is left for the null terminator
#define R_ASM_BUFSIZE 255
/* backward compatibility */
#define R_ASM_ARCH_NONE R_SYS_ARCH_NONE
#define R_ASM_ARCH_X86 R_SYS_ARCH_X86
#define R_ASM_ARCH_ARM R_SYS_ARCH_ARM
#define R_ASM_ARCH_PPC R_SYS_ARCH_PPC
#define R_ASM_ARCH_M68K R_SYS_ARCH_M68K
#define R_ASM_ARCH_JAVA R_SYS_ARCH_JAVA
#define R_ASM_ARCH_LM32 R_SYS_ARCH_LM32
#define R_ASM_ARCH_MIPS R_SYS_ARCH_MIPS
#define R_ASM_ARCH_SPARC R_SYS_ARCH_SPARC
#define R_ASM_ARCH_XAP R_SYS_ARCH_XAP
#define R_ASM_ARCH_MSIL R_SYS_ARCH_MSIL
#define R_ASM_ARCH_OBJD R_SYS_ARCH_OBJD
#define R_ASM_ARCH_BF R_SYS_ARCH_BF
#define R_ASM_ARCH_SH R_SYS_ARCH_SH
#define R_ASM_ARCH_Z80 R_SYS_ARCH_Z80
#define R_ASM_ARCH_I8080 R_SYS_ARCH_I8080
#define R_ASM_ARCH_ARC R_SYS_ARCH_ARC
#define R_ASM_ARCH_HPPA R_SYS_ARCH_HPPA
#define R_ASM_GET_OFFSET(x,y,z) \
(x && x->binb.bin && x->binb.get_offset)? \
x->binb.get_offset (x->binb.bin, y, z): -1
2016-09-22 22:33:25 +00:00
#define R_ASM_GET_NAME(x,y,z) \
(x && x->binb.bin && x->binb.get_name)? \
x->binb.get_name (x->binb.bin, y, z): NULL
2016-09-22 22:33:25 +00:00
enum {
R_ASM_SYNTAX_NONE = 0,
R_ASM_SYNTAX_INTEL,
R_ASM_SYNTAX_ATT,
R_ASM_SYNTAX_MASM,
R_ASM_SYNTAX_REGNUM, // alias for capstone's NOREGNAME
R_ASM_SYNTAX_JZ, // hack to use jz instead of je on x86
};
enum {
R_ASM_MOD_RAWVALUE = 'r',
R_ASM_MOD_VALUE = 'v',
R_ASM_MOD_DSTREG = 'd',
R_ASM_MOD_SRCREG0 = '0',
R_ASM_MOD_SRCREG1 = '1',
R_ASM_MOD_SRCREG2 = '2'
};
typedef struct r_asm_op_t {
int size; // instruction size
2014-09-12 17:25:43 +00:00
int payload; // size of payload (opsize = (size-payload))
// But this is pretty slow..so maybe we should add some accessors
ut8 buf[R_ASM_BUFSIZE + 1];
char buf_asm[R_ASM_BUFSIZE + 1];
char buf_hex[R_ASM_BUFSIZE + 1];
} RAsmOp;
typedef struct r_asm_code_t {
int len;
ut8 *buf;
char *buf_hex;
char *buf_asm;
RList *equs; // TODO: must be a hash
ut64 code_offset;
ut64 data_offset;
2016-11-16 00:21:58 +00:00
int code_align;
} RAsmCode;
// TODO: Must use Hashtable instead of this hack
typedef struct {
char *key;
char *value;
} RAsmEqu;
#define _RAsmPlugin struct r_asm_plugin_t
typedef struct r_asm_t {
char *cpu;
int bits;
int big_endian;
int syntax;
ut64 pc;
void *user;
_RAsmPlugin *cur;
_RAsmPlugin *acur;
2010-05-26 00:55:50 +00:00
RList *plugins;
RBinBind binb;
RParse *ifilter;
RParse *ofilter;
Sdb *pair;
RSyscall *syscall;
RNum *num;
char *features;
int invhex; // invalid instructions displayed in hex
int pcalign;
int dataalign;
SdbHash *flags;
} RAsm;
typedef int (*RAsmModifyCallback)(RAsm *a, ut8 *buf, int field, ut64 val);
typedef struct r_asm_plugin_t {
const char *name;
const char *arch;
const char *author;
const char *version;
const char *cpus;
const char *desc;
const char *license;
void *user; // user data pointer
int bits;
int endian;
2015-12-01 11:39:12 +00:00
bool (*init)(void *user);
bool (*fini)(void *user);
int (*disassemble)(RAsm *a, RAsmOp *op, const ut8 *buf, int len);
2012-07-21 10:11:21 +00:00
int (*assemble)(RAsm *a, RAsmOp *op, const char *buf);
RAsmModifyCallback modify;
int (*set_subarch)(RAsm *a, const char *buf);
2016-10-30 11:39:49 +00:00
char *(*mnemonics)(RAsm *a, int id, bool json);
const char *features;
} RAsmPlugin;
#ifdef R_API
/* asm.c */
R_API RAsm *r_asm_new(void);
#define r_asm_op_free free
R_API RAsm *r_asm_free(RAsm *a);
R_API int r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val);
2016-10-30 11:39:49 +00:00
R_API char *r_asm_mnemonics(RAsm *a, int id, bool json);
2016-11-02 02:49:55 +00:00
R_API int r_asm_mnemonics_byname(RAsm *a, const char *name);
R_API void r_asm_set_user_ptr(RAsm *a, void *user);
2016-04-03 22:17:57 +00:00
R_API bool r_asm_add(RAsm *a, RAsmPlugin *foo);
R_API int r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian);
R_API int r_asm_is_valid(RAsm *a, const char *name);
R_API int r_asm_use(RAsm *a, const char *name);
R_API bool r_asm_use_assembler(RAsm *a, const char *name);
R_API bool r_asm_set_arch(RAsm *a, const char *name, int bits);
R_API int r_asm_set_bits(RAsm *a, int bits);
R_API void r_asm_set_cpu(RAsm *a, const char *cpu);
R_API bool r_asm_set_big_endian(RAsm *a, bool big_endian);
R_API int r_asm_set_syntax(RAsm *a, int syntax);
R_API int r_asm_syntax_from_string(const char *name);
R_API int r_asm_set_pc(RAsm *a, ut64 pc);
R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len);
2012-07-21 10:11:21 +00:00
R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf);
R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len);
R_API RAsmCode* r_asm_mdisassemble_hexstr(RAsm *a, const char *hexstr);
2012-07-21 10:11:21 +00:00
R_API RAsmCode* r_asm_massemble(RAsm *a, const char *buf);
R_API RAsmCode* r_asm_rasm_assemble(RAsm *a, const char *buf, bool use_spp);
2012-07-21 10:11:21 +00:00
R_API RAsmCode* r_asm_assemble_file(RAsm *a, const char *file);
R_API char *r_asm_to_string(RAsm *a, ut64 addr, const ut8 *b, int l);
2017-01-10 12:27:09 +00:00
/* to ease the use of the native bindings (not used in r2) */
R_API ut8 *r_asm_from_string(RAsm *a, ut64 addr, const char *b, int *l);
R_API int r_asm_filter_input(RAsm *a, const char *f);
R_API int r_asm_filter_output(RAsm *a, const char *f);
R_API char *r_asm_describe(RAsm *a, const char* str);
R_API RList* r_asm_get_plugins(RAsm *a);
R_API void r_asm_list_directives(void);
/* code.c */
R_API RAsmCode *r_asm_code_new(void);
2012-07-21 10:11:21 +00:00
R_API void* r_asm_code_free(RAsmCode *acode);
2016-01-30 02:02:06 +00:00
R_API bool r_asm_code_set_equ (RAsmCode *code, const char *key, const char *value);
R_API char *r_asm_code_equ_replace (RAsmCode *code, char *str);
// accessors, to make bindings happy
R_API char *r_asm_op_get_hex(RAsmOp *op);
R_API char *r_asm_op_get_asm(RAsmOp *op);
R_API int r_asm_op_get_size(RAsmOp *op);
/* plugin pointers */
extern RAsmPlugin r_asm_plugin_bf;
extern RAsmPlugin r_asm_plugin_java;
2014-06-25 10:22:08 +00:00
extern RAsmPlugin r_asm_plugin_mips_gnu;
extern RAsmPlugin r_asm_plugin_mips_cs;
2014-11-10 10:28:34 +00:00
extern RAsmPlugin r_asm_plugin_x86_udis;
extern RAsmPlugin r_asm_plugin_x86_as;
extern RAsmPlugin r_asm_plugin_x86_nz;
extern RAsmPlugin r_asm_plugin_x86_nasm;
extern RAsmPlugin r_asm_plugin_x86_cs;
2014-06-25 10:22:08 +00:00
extern RAsmPlugin r_asm_plugin_arm_gnu;
extern RAsmPlugin r_asm_plugin_arm_cs;
extern RAsmPlugin r_asm_plugin_arm_as;
extern RAsmPlugin r_asm_plugin_armthumb;
extern RAsmPlugin r_asm_plugin_arm_winedbg;
extern RAsmPlugin r_asm_plugin_xap;
extern RAsmPlugin r_asm_plugin_ppc_gnu;
extern RAsmPlugin r_asm_plugin_ppc_cs;
extern RAsmPlugin r_asm_plugin_sparc_gnu;
extern RAsmPlugin r_asm_plugin_avr;
extern RAsmPlugin r_asm_plugin_dalvik;
2011-03-29 12:55:41 +00:00
extern RAsmPlugin r_asm_plugin_sh;
extern RAsmPlugin r_asm_plugin_z80;
extern RAsmPlugin r_asm_plugin_i8080;
extern RAsmPlugin r_asm_plugin_m68k;
2015-10-07 12:37:25 +00:00
extern RAsmPlugin r_asm_plugin_m68k_cs;
extern RAsmPlugin r_asm_plugin_arc;
extern RAsmPlugin r_asm_plugin_dcpu16;
extern RAsmPlugin r_asm_plugin_8051;
extern RAsmPlugin r_asm_plugin_tms320;
2013-11-14 15:54:29 +00:00
extern RAsmPlugin r_asm_plugin_gb;
2013-11-29 16:30:58 +00:00
extern RAsmPlugin r_asm_plugin_snes;
extern RAsmPlugin r_asm_plugin_ebc;
extern RAsmPlugin r_asm_plugin_nios2;
extern RAsmPlugin r_asm_plugin_malbolge;
2014-02-02 01:42:21 +00:00
extern RAsmPlugin r_asm_plugin_ws;
extern RAsmPlugin r_asm_plugin_6502;
2014-02-21 17:00:28 +00:00
extern RAsmPlugin r_asm_plugin_h8300;
2014-03-12 07:42:36 +00:00
extern RAsmPlugin r_asm_plugin_cr16;
2014-05-16 11:54:36 +00:00
extern RAsmPlugin r_asm_plugin_v850;
extern RAsmPlugin r_asm_plugin_sysz;
extern RAsmPlugin r_asm_plugin_sparc_cs;
extern RAsmPlugin r_asm_plugin_xcore_cs;
extern RAsmPlugin r_asm_plugin_spc700;
2014-08-28 06:08:11 +00:00
extern RAsmPlugin r_asm_plugin_propeller;
2014-09-23 07:36:37 +00:00
extern RAsmPlugin r_asm_plugin_msp430;
2014-11-18 14:47:47 +00:00
extern RAsmPlugin r_asm_plugin_i4004;
extern RAsmPlugin r_asm_plugin_cris_gnu;
extern RAsmPlugin r_asm_plugin_lh5801;
extern RAsmPlugin r_asm_plugin_hppa_gnu;
2015-07-08 14:39:21 +00:00
extern RAsmPlugin r_asm_plugin_v810;
extern RAsmPlugin r_asm_plugin_mcs96;
2015-09-20 19:38:17 +00:00
extern RAsmPlugin r_asm_plugin_lm32;
2015-10-29 23:05:48 +00:00
extern RAsmPlugin r_asm_plugin_riscv;
extern RAsmPlugin r_asm_plugin_vax;
2016-02-15 12:27:23 +00:00
extern RAsmPlugin r_asm_plugin_lanai_gnu;
extern RAsmPlugin r_asm_plugin_xtensa;
extern RAsmPlugin r_asm_plugin_tricore;
extern RAsmPlugin r_asm_plugin_pic18c;
extern RAsmPlugin r_asm_plugin_rsp;
extern RAsmPlugin r_asm_plugin_hexagon_gnu;
extern RAsmPlugin r_asm_plugin_wasm;
extern RAsmPlugin r_asm_plugin_tms320c64x;
extern RAsmPlugin r_asm_plugin_evm;
#endif
#ifdef __cplusplus
}
#endif
#endif