00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef analy_h
00022 #define analy_h
00023
00024 #include "asm.h"
00025 #include "global.h"
00026 #include "common.h"
00027 #include "code_analy.h"
00028 #include "data_analy.h"
00029 #include "htdata.h"
00030 #include "stddata.h"
00031
00032 extern int num_ops_parsed;
00033
00034 class Analyser;
00035
00036 #define ADDRESS_STRING_FORMAT_COMPACT 0
00037 #define ADDRESS_STRING_FORMAT_LEADING_WHITESPACE 1
00038 #define ADDRESS_STRING_FORMAT_LEADING_ZEROS 2
00039 #define ADDRESS_STRING_FORMAT_RESERVED 3
00040
00041 #define ADDRESS_STRING_FORMAT_HEX_CAPS 4
00042 #define ADDRESS_STRING_FORMAT_ADD_0X 8
00043 #define ADDRESS_STRING_FORMAT_ADD_H 16
00044
00045 #define DUP_ADDR(a) ((Address*)(a)->duplicate())
00046 class Address: public Object {
00047 public:
00048 virtual bool add(int offset) = 0;
00049 virtual int byteSize() = 0;
00050 virtual int compareTo(const Object *obj) const = 0;
00051 virtual int compareDelinear(Address *to);
00052 virtual bool difference(int &result, Address *to) = 0;
00053 virtual void getFromArray(const byte *array) = 0;
00054 virtual void getFromCPUAddress(CPU_ADDR *ca) = 0;
00055 virtual bool isValid();
00056 virtual int parseString(const char *s, int length, Analyser *a) = 0;
00057 virtual void putIntoArray(byte *array) = 0;
00058 virtual void putIntoCPUAddress(CPU_ADDR *ca) = 0;
00059 virtual int stringify(char *s, int max_length, int format) = 0;
00060 virtual int stringSize() = 0;
00061 virtual int toString(char *s, int maxlen);
00062 };
00063
00064 class InvalidAddress: public Address {
00065 public:
00066 InvalidAddress();
00067 virtual bool add(int offset);
00068 virtual int byteSize();
00069 virtual int compareTo(const Object *obj) const;
00070 virtual bool difference(int &result, Address *to);
00071 virtual Object * duplicate();
00072 virtual void getFromArray(const byte *array);
00073 virtual void getFromCPUAddress(CPU_ADDR *ca);
00074 virtual bool isValid();
00075 virtual OBJECT_ID object_id() const;
00076 virtual int parseString(const char *s, int length, Analyser *a);
00077 virtual void putIntoArray(byte *array);
00078 virtual void putIntoCPUAddress(CPU_ADDR *ca);
00079 virtual int stringify(char *s, int max_length, int format);
00080 virtual int stringSize();
00081 };
00082
00083
00084
00085
00086 class AddressFlat32: public Address {
00087 public:
00088 dword addr;
00089 AddressFlat32();
00090 AddressFlat32(dword a);
00091 virtual bool add(int offset);
00092 virtual int byteSize();
00093 virtual int compareTo(const Object *obj) const;
00094 virtual int compareDelinear(Address *to);
00095 virtual void getFromArray(const byte *array);
00096 virtual void getFromCPUAddress(CPU_ADDR *ca);
00097 virtual bool difference(int &result, Address *to);
00098 virtual Object * duplicate();
00099 virtual int load(ht_object_stream *s);
00100 virtual OBJECT_ID object_id() const;
00101 virtual int parseString(const char *s, int length, Analyser *a);
00102 virtual void putIntoArray(byte *array);
00103 virtual void putIntoCPUAddress(CPU_ADDR *ca);
00104 virtual void store(ht_object_stream *s);
00105 virtual int stringify(char *s, int max_length, int format);
00106 virtual int stringSize();
00107 };
00108
00109 class AddressFlat64: public Address {
00110 public:
00111 qword addr;
00112 AddressFlat64();
00113 AddressFlat64(qword a);
00114 virtual bool add(int offset);
00115 virtual int byteSize();
00116 virtual int compareTo(const Object *obj) const;
00117 virtual int compareDelinear(Address *to);
00118 virtual void getFromArray(const byte *array);
00119 virtual void getFromCPUAddress(CPU_ADDR *ca);
00120 virtual bool difference(int &result, Address *to);
00121 virtual Object * duplicate();
00122 virtual int load(ht_object_stream *s);
00123 virtual OBJECT_ID object_id() const;
00124 virtual int parseString(const char *s, int length, Analyser *a);
00125 virtual void putIntoArray(byte *array);
00126 virtual void putIntoCPUAddress(CPU_ADDR *ca);
00127 virtual void store(ht_object_stream *s);
00128 virtual int stringify(char *s, int max_length, int format);
00129 virtual int stringSize();
00130 };
00131
00132 #define ANALY_SEGMENT_CAP_WRITE 1
00133 #define ANALY_SEGMENT_CAP_INITIALIZED 2
00134
00135
00136 class Segment: public Object {
00137 Address *start, *end;
00138 char *name;
00139 int caps;
00140
00141 Segment(const char *n, Address *s, Address *e, int c, int address_size);
00142 virtual bool containsAddress(Address *addr) = 0;
00143 virtual const char * getName();
00144 virtual int getAddressSize();
00145 virtual int getCapability(int cap);
00146 };
00147
00148
00149
00150
00151
00152 typedef enum {
00153 br_nobranch,
00154 br_jump,
00155 br_return,
00156 br_call,
00157 br_jXX
00158 } branch_enum_t;
00159
00160
00161
00162
00163 #define OPCODE dis_insn
00164
00165
00166
00167
00168 class AnalyDisassembler: public Object {
00169 public:
00170 Analyser *analy;
00171 Disassembler *disasm;
00172 AnalyDisassembler();
00173 void init(Analyser *A);
00174 int load(ht_object_stream *f);
00175 virtual void done();
00176
00177 virtual Address * branchAddr(OPCODE *opcode, branch_enum_t branchtype, bool examine) = 0;
00178 virtual void examineOpcode(OPCODE *opcode) = 0;
00179 virtual void initDisasm();
00180 virtual branch_enum_t isBranch(OPCODE *opcode) = 0;
00181 virtual void store(ht_object_stream *f);
00182 };
00183
00184
00185
00186 typedef enum {
00187 xrefread,
00188 xrefwrite,
00189 xrefoffset,
00190 xrefjump,
00191 xrefcall,
00192 xrefijump,
00193 xreficall
00194 } xref_enum_t;
00195
00196 class AddrXRef: public Object {
00197 public:
00198 xref_enum_t type;
00199 AddrXRef();
00200 AddrXRef(xref_enum_t Type);
00201 int load(ht_object_stream *f);
00202 virtual OBJECT_ID object_id() const;
00203 virtual void store(ht_object_stream *f);
00204 };
00205
00206 class CommentList: public ht_clist {
00207 public:
00208 void init();
00209 void appendPreComment(const char *s);
00210 void appendPreComment(int special);
00211 void appendPostComment(const char *s);
00212 void appendPostComment(int special);
00213 const char * getName(UINT i);
00214 };
00215
00216 struct Symbol;
00217
00218 struct Location {
00219
00220 Address *addr;
00221
00222 Location *left, *right;
00223
00224 Symbol *label;
00225
00226 ht_tree *xrefs;
00227
00228 CommentList *comments;
00229
00230 taddr_type type;
00231
00232 Location *thisfunc;
00233
00234 int flags;
00235 };
00236
00237
00238
00239
00240 #define AF_DELETED 1
00241 #define AF_FUNCTION_SET 2
00242 #define AF_FUNCTION_END 4
00243
00244 typedef enum {
00245 scvalid,
00246 scread,
00247 scwrite,
00248 screadwrite,
00249 sccode,
00250 scinitialized
00251 } tsectype;
00252
00253 typedef enum {
00254 acread,
00255 acwrite,
00256 acoffset
00257 } taccesstype;
00258
00259 struct taccess {
00260 bool indexed;
00261 int size;
00262 taccesstype type;
00263 };
00264
00265 typedef enum {
00266 label_unknown = 0,
00267 label_func,
00268 label_loc,
00269 label_data
00270 } labeltype;
00271
00272 struct Symbol {
00273 labeltype type;
00274 Location * location;
00275 char * name;
00276 Symbol *left, *right;
00277 };
00278
00279 class AddressQueueItem: public Object {
00280 public:
00281 Address *addr;
00282 Address *func;
00283 AddressQueueItem();
00284 AddressQueueItem(Address *Addr, Address *Func);
00285 ~AddressQueueItem();
00286 OBJECT_ID object_id() const;
00287 int load(ht_object_stream *f);
00288 virtual void store(ht_object_stream *f);
00289 };
00290
00291 class CodeAnalyser;
00292 class DataAnalyser;
00293
00294 class Analyser: public Object {
00295 public:
00296 Address * addr;
00297 Address * invalid_addr;
00298 ht_queue * addr_queue;
00299 int ops_parsed;
00300 bool active;
00301 Address *next_explored, *first_explored, *last_explored;
00302 bool next_address_is_invalid;
00303 Area * explored;
00304 Area * initialized;
00305 Location * locations;
00306 CodeAnalyser * code;
00307 DataAnalyser * data;
00308 AnalyDisassembler * analy_disasm;
00309 Disassembler * disasm;
00310 Symbol * symbols;
00311 int location_threshold, symbol_threshold;
00312 int cur_addr_ops, cur_label_ops;
00313 int max_opcode_length;
00314 Location *cur_func;
00315 bool dirty;
00316
00317 int symbol_count;
00318 int location_count;
00319
00320 void init();
00321 int load(ht_object_stream *f);
00322 virtual void done();
00323
00324 bool addAddressSymbol(Address *Addr, const char *Prefix, labeltype type, Location *infunc=NULL);
00325 void addComment(Address *Addr, int line, const char *c);
00326 bool addSymbol(Address *Addr, const char *label, labeltype type, Location *infunc=NULL);
00327 virtual FILEOFS addressToFileofs(Address *Addr) = 0;
00328 bool addXRef(Address *from, Address *to, xref_enum_t action);
00329 void assignComment(Address *Addr, int line, const char *c);
00330 bool assignSymbol(Address *Addr, const char *label, labeltype type, Location *infunc=NULL);
00331 void assignXRef(Address *from, Address *to, xref_enum_t action);
00332 virtual void beginAnalysis();
00333 virtual UINT bufPtr(Address *Addr, byte *buf, int size) = 0;
00334 bool continueAnalysis();
00335 void continueAnalysisAt(Address *Addr);
00336 virtual Address * createAddress() = 0;
00337 void dataAccess(Address *Addr, taccess access);
00338 void deleteLocation(Address *Addr);
00339 void deleteSymbol(Address *Addr);
00340 bool deleteXRef(Address *from, Address *to);
00341 void disableSymbol(Symbol *label);
00342 void doBranch(branch_enum_t branch, OPCODE *opcode, int len);
00343 void engageCodeanalyser();
00344 Location * enumLocations(Address *Addr);
00345 Location * enumLocationsReverse(Address *Addr);
00346 Symbol * enumSymbolsByName(const char *at);
00347 Symbol * enumSymbolsByNameReverse(const char *at);
00348 Symbol * enumSymbols(Symbol *sym);
00349 Symbol * enumSymbolsReverse(Symbol *sym);
00350 virtual taddr_typetype examineData(Address *Addr);
00351 void finish();
00352 void freeLocation(Location *loc);
00353 void freeLocations(Location *locs);
00354 void freeComments(Location *loc);
00355 void freeSymbol(Symbol *sym);
00356 void freeSymbols(Symbol *syms);
00357 Location * getLocationByAddress(Address *Addr);
00358 Location * getLocationContextByAddress(Address *Addr);
00359 int getLocationCount();
00360 Location * getFunctionByAddress(Address *Addr);
00361 Location * getPreviousSymbolByAddress(Address *Addr);
00362 virtual const char * getSegmentNameByAddress(Address *Addr);
00363 Symbol * getSymbolByAddress(Address *Addr);
00364 Symbol * getSymbolByName(const char *label);
00365 const char * getSymbolNameByLocation(Location *loc);
00366 int getSymbolCount();
00367 bool gotoAddress(Address *Addr, Address *func);
00368 virtual void initCodeAnalyser();
00369 virtual void initDataAnalyser();
00370 virtual void initUnasm() = 0;
00371 virtual void log(const char *s);
00372 virtual CPU_ADDR mapAddr(Address *Addr);
00373 Location * newLocation(Address *Addr);
00374 Location * newLocation(Location *&locs, Address *Addr);
00375 Symbol * newSymbol(const char *label, Location *loc, labeltype type, Location *infunc);
00376 Symbol * newSymbol(Symbol *&syms, const char *label, Location *loc, labeltype type);
00377 virtual Address * nextValid(Address *Addr) = 0;
00378 void optimizeLocationTree();
00379 void optimizeSymbolTree();
00380 bool popAddress(Address **Addr, Address **func);
00381 void pushAddress(Address *Addr, Address *func);
00382 virtual int queryConfig(int mode);
00383 void setActive(bool mode);
00384 void setLocationFunction(Location *a, Location *func);
00385 void setLocationTreeOptimizeThreshold(int threshold);
00386 void setDisasm(Disassembler *d);
00387 void setSymbolTreeOptimizeThreshold(int threshold);
00388 virtual void store(ht_object_stream *f);
00389 virtual bool validAddress(Address *addr, tsectype action) = 0;
00390 bool validCodeAddress(Address *addr);
00391 bool validReadAddress(Address *addr);
00392 bool validWriteAddress(Address *addr);
00393
00394
00395 int mode;
00396
00397 virtual Assembler * createAssembler();
00398 virtual Address * fileofsToAddress(FILEOFS fileofs);
00399 CommentList * getComments(Address *Addr);
00400 const char * getDisasmStr(Address *Addr, int &length);
00401 const char * getDisasmStrFormatted(Address *Addr);
00402 int getDisplayMode();
00403 virtual const char * getName();
00404 virtual const char * getType();
00405 ht_tree * getXRefs(Address *Addr);
00406 bool isDirty();
00407 void makeDirty();
00408 void setDisplayMode(int enable, int disable);
00409 void toggleDisplayMode(int toggle);
00410 };
00411
00412
00413 #define ANALY_SHOW_ADDRESS 1
00414 #define ANALY_SHOW_COMMENTS 2
00415 #define ANALY_SHOW_LABELS 4
00416 #define ANALY_SHOW_XREFS 8
00417 #define ANALY_SHOW_BYTES 16
00418 #define ANALY_EDIT_BYTES 32
00419 #define ANALY_TRANSLATE_SYMBOLS 64
00420 #define ANALY_COLLAPSE_XREFS 128
00421
00422
00423 #define Q_DO_ANALYSIS 1
00424 #define Q_ENGAGE_CODE_ANALYSER 2
00425 #define Q_ENGAGE_DATA_ANALYSER 3
00426
00427
00428 #define INVALID_FILE_OFS ((dword)-1)
00429
00430
00431 #define MAX_OPS_PER_CONTINUE 10
00432
00433 extern int global_analyser_address_string_format;
00434
00435
00436
00437
00438 extern Analyser *testanaly;
00439
00440 #endif