1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* The HT Editor
* asm.h
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __ASM_H__
#define __ASM_H__
#include "data.h"
#define MAX_INSN_SIZE 16
struct CPU_ADDR {
union {
struct {
uint16 seg;
uint32 offset;
} addr32;
struct {
uint64 addr;
} flat64;
};
};
struct asm_code {
asm_code *next;
int size;
byte data[MAX_INSN_SIZE];
void *context;
};
typedef void dis_insn;
typedef void asm_insn;
/*
* CLASS assembler
*/
class Assembler: public Object {
protected:
int (*imm_eval_proc)(void *context, const char *s, uint64 &v);
void *imm_eval_context;
asm_code *codes;
asm_code code;
char error_msg[256];
bool error;
int options;
bool bigendian;
void emitbyte(byte b);
void emitword(uint16 w);
void emitdword(uint32 d);
void emitqword(uint64 q);
void free_asm_codes();
void deletecode(asm_code *c);
void clearcode();
void newcode();
void pushcode();
public:
Assembler(bool bigendian);
Assembler(BuildCtorArg&a): Object(a) {};
virtual ~Assembler();
/* new */
virtual asm_insn *alloc_insn();
virtual asm_code *encode(asm_insn *asm_insn, int options, CPU_ADDR cur_address);
const char *get_error_msg();
virtual const char *get_name();
virtual bool translate_str(asm_insn *asm_insn, const char *s) = 0;
void set_error_msg(const char *format, ...);
void set_imm_eval_proc(int (*imm_eval_proc)(void *context, const char *s, uint64 &v), void *imm_eval_context);
asm_code *shortest(asm_code *codes);
};
/*
* CLASS disassembler
*/
/* generic disassembler styles */
#define DIS_STYLE_HIGHLIGHT 0x80000000 /* create highlighting information in strf() */
#define DIS_STYLE_HEX_CSTYLE 0x40000000 /* IF SET: mov eax, 0x12345678 ELSE: mov eax, 12345678 */
#define DIS_STYLE_HEX_ASMSTYLE 0x20000000 /* IF SET: mov eax, 12345678h ELSE: mov eax, 12345678 */
#define DIS_STYLE_HEX_UPPERCASE 0x10000000 /* IF SET: mov eax, 5678ABCD ELSE: mov eax, 5678abcd */
#define DIS_STYLE_HEX_NOZEROPAD 0x08000000 /* IF SET: mov eax, 8002344 ELSE: mov eax, 008002344 */
#define DIS_STYLE_SIGNED 0x04000000 /* IF SET: mov eax, -1 ELSE: mov eax, 0ffffffffh */
#define DIS_STYLE_TABSIZE 12
extern char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context);
extern void* addr_sym_func_context;
enum AsmSyntaxHighlightEnum {
e_cs_default=0,
e_cs_comment,
e_cs_number,
e_cs_symbol,
e_cs_string
};
class Disassembler: public Object {
protected:
int options;
bool highlight;
const char *get_cs(AsmSyntaxHighlightEnum style);
void hexd(char **s, int size, int options, uint32 imm);
void hexq(char **s, int size, int options, uint64 imm);
void enable_highlighting();
void disable_highlighting();
public:
Disassembler();
Disassembler(BuildCtorArg&a): Object(a) {};
void load(ObjectStream &f);
/* new */
virtual dis_insn *createInvalidInsn();
virtual dis_insn *decode(byte *code, int maxlen, CPU_ADDR cur_address)=0;
virtual dis_insn *duplicateInsn(dis_insn *disasm_insn)=0;
virtual void getOpcodeMetrics(int &min_length, int &max_length, int &min_look_ahead, int &avg_look_ahead, int &addr_align)=0;
virtual byte getSize(dis_insn *disasm_insn)=0;
virtual const char *getName()=0;
virtual bool selectNext(dis_insn *disasm_insn);
virtual const char *str(dis_insn *disasm_insn, int style);
virtual const char *strf(dis_insn *disasm_insn, int style, const char *format)=0;
virtual bool validInsn(dis_insn *disasm_insn)=0;
};
/*****************************************************************************
* The strf() format *
*****************************************************************************
String Action
--------------------------------------------------
%x substitute expression with symbol "x"
?xy...y if symbol "x" is undefined leave out the whole expression,
otherwise subsitute expression with string between the two "y"s
Symbol Desc
--------------------------------------------------
p prefix
n name
1 first operand
2 second operand
3 third operand
4 forth operand
*/
#define DISASM_STRF_VAR '%'
#define DISASM_STRF_COND '?'
#define DISASM_STRF_PREFIX 'p'
#define DISASM_STRF_NAME 'n'
#define DISASM_STRF_FIRST '1'
#define DISASM_STRF_SECOND '2'
#define DISASM_STRF_THIRD '3'
#define DISASM_STRF_FORTH '4'
#define DISASM_STRF_FIFTH '5'
//#define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2?3/, %3/?4-, %4-#"
#define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2#?3#, %3#?4#, %4#?5#, %5#"
#define DISASM_STRF_SMALL_FORMAT "?p#%p #%n?1# %1#?2#,%2#?3#,%3#?4#,%4#?5#,%5#"
#define ATOM_DISASM_X86 MAGIC32("DIS\x01")
#define ATOM_DISASM_ALPHA MAGIC32("DIS\x02")
#define ATOM_DISASM_JAVA MAGIC32("DIS\x03")
// 0x04 was IA64
#define ATOM_DISASM_IL MAGIC32("DIS\x05")
#define ATOM_DISASM_X86_VXD MAGIC32("DIS\x06")
#define ATOM_DISASM_PPC MAGIC32("DIS\x07")
#define ATOM_DISASM_X86_64 MAGIC32("DIS\x08")
#define ATOM_DISASM_ARM MAGIC32("DIS\x09")
#define ATOM_DISASM_AVR MAGIC32("DIS\x0a")
#define ASM_SYNTAX_DEFAULT "\\@d"
#define ASM_SYNTAX_COMMENT "\\@#"
#define ASM_SYNTAX_NUMBER "\\@n"
#define ASM_SYNTAX_SYMBOL "\\@c"
#define ASM_SYNTAX_STRING "\\@s"
bool init_asm();
void done_asm();
#endif /* __ASM_H__ */
|