00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __ASM_H__
00022 #define __ASM_H__
00023
00024 #include "common.h"
00025
00026 #define CPU_X86 1
00027
00028 #define MAX_INSN_SIZE 16
00029
00030 struct CPU_ADDR {
00031 union {
00032 struct {
00033 word seg;
00034 dword offset;
00035 } addr32;
00036 struct {
00037 qword addr;
00038 } flat64;
00039 };
00040 };
00041
00042 struct asm_code {
00043 asm_code *next;
00044 dword size;
00045 byte data[MAX_INSN_SIZE];
00046 void *context;
00047 };
00048
00049 typedef void dis_insn;
00050 typedef void asm_insn;
00051
00052
00053
00054
00055
00056 class Assembler: public Object {
00057 protected:
00058 int (*imm_eval_proc)(void *context, char **s, dword *v);
00059 void *imm_eval_context;
00060
00061 asm_code *codes;
00062 asm_code code;
00063 char error_msg[256];
00064 bool error;
00065 int options;
00066 bool bigendian;
00067
00068 void emitbyte(byte b);
00069 void emitword(word w);
00070 void emitdword(dword d);
00071 void free_asm_codes();
00072 void deletecode(asm_code *c);
00073 void clearcode();
00074 void newcode();
00075 void pushcode();
00076 public:
00077 Assembler(bool bigendian);
00078 ~Assembler();
00079
00080 virtual asm_insn *alloc_insn();
00081 virtual asm_code *encode(asm_insn *asm_insn, int options, CPU_ADDR cur_address);
00082 char *get_error_msg();
00083 virtual char *get_name();
00084 virtual int translate_str(asm_insn *asm_insn, const char *s);
00085 void set_error_msg(char *format, ...);
00086 void set_imm_eval_proc(int (*imm_eval_proc)(void *context, char **s, dword *v), void *imm_eval_context);
00087 asm_code *shortest(asm_code *codes);
00088 };
00089
00090
00091
00092
00093
00094
00095 #define DIS_STYLE_HIGHLIGHT 0x80000000
00096 #define DIS_STYLE_HEX_CSTYLE 0x40000000
00097 #define DIS_STYLE_HEX_ASMSTYLE 0x20000000
00098 #define DIS_STYLE_HEX_UPPERCASE 0x10000000
00099 #define DIS_STYLE_HEX_NOZEROPAD 0x08000000
00100 #define DIS_STYLE_SIGNED 0x04000000
00101
00102 #define DIS_STYLE_TABSIZE 8
00103
00104 extern char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context);
00105 extern void* addr_sym_func_context;
00106
00107 enum AsmSyntaxHighlightEnum {
00108 e_cs_default=0,
00109 e_cs_comment,
00110 e_cs_number,
00111 e_cs_symbol,
00112 e_cs_string
00113 };
00114
00115 class Disassembler: public Object {
00116 protected:
00117 int options;
00118 bool highlight;
00119
00120 const char *get_cs(AsmSyntaxHighlightEnum style);
00121 void hexd(char **s, int size, int options, int imm);
00122 void enable_highlighting();
00123 void disable_highlighting();
00124 public:
00125 Disassembler();
00126 ~Disassembler();
00127
00128 virtual dis_insn *createInvalidInsn();
00129 virtual dis_insn *decode(byte *code, int maxlen, CPU_ADDR cur_address)=0;
00130 virtual dis_insn *duplicateInsn(dis_insn *disasm_insn)=0;
00131 virtual void getOpcodeMetrics(int &min_length, int &max_length, int &min_look_ahead, int &avg_look_ahead, int &addr_align)=0;
00132 virtual byte getSize(dis_insn *disasm_insn)=0;
00133 virtual char *getName()=0;
00134 virtual bool selectNext(dis_insn *disasm_insn);
00135 virtual char *str(dis_insn *disasm_insn, int style);
00136 virtual char *strf(dis_insn *disasm_insn, int style, char *format)=0;
00137 virtual bool validInsn(dis_insn *disasm_insn)=0;
00138 };
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 #define DISASM_STRF_VAR '%'
00159 #define DISASM_STRF_COND '?'
00160
00161 #define DISASM_STRF_PREFIX 'p'
00162 #define DISASM_STRF_NAME 'n'
00163 #define DISASM_STRF_FIRST '1'
00164 #define DISASM_STRF_SECOND '2'
00165 #define DISASM_STRF_THIRD '3'
00166
00167 #define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2?3/, %3/#"
00168 #define DISASM_STRF_SMALL_FORMAT "?p#%p #%n?1- %1?2#,%2?3/,%3/#-"
00169
00170 #define ATOM_DISASM_X86 MAGICD("DIS\x01")
00171 #define ATOM_DISASM_ALPHA MAGICD("DIS\x02")
00172 #define ATOM_DISASM_JAVA MAGICD("DIS\x03")
00173 #define ATOM_DISASM_IA64 MAGICD("DIS\x04")
00174 #define ATOM_DISASM_IL MAGICD("DIS\x05")
00175 #define ATOM_DISASM_X86_VXD MAGICD("DIS\x06")
00176 #define ATOM_DISASM_PPC MAGICD("DIS\x07")
00177
00178 #define ASM_SYNTAX_DEFAULT "\\@d"
00179 #define ASM_SYNTAX_COMMENT "\\@#"
00180 #define ASM_SYNTAX_NUMBER "\\@n"
00181 #define ASM_SYNTAX_SYMBOL "\\@c"
00182 #define ASM_SYNTAX_STRING "\\@s"
00183
00184 bool init_asm();
00185 void done_asm();
00186
00187 #endif