File: analy.h

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (448 lines) | stat: -rw-r--r-- 13,676 bytes parent folder | download | duplicates (4)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* 
 *	HT Editor
 *	analy.h
 *
 *	Copyright (C) 1999-2002 Sebastian Biallas (sb@biallas.net)
 *
 *	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 analy_h
#define analy_h

#include "asm.h"
#include "data.h"
#include "code_analy.h"
#include "data_analy.h"
#include "stddata.h"

extern int num_ops_parsed;

class Analyser;

#define ADDRESS_STRING_FORMAT_COMPACT            0
#define ADDRESS_STRING_FORMAT_LEADING_WHITESPACE 1
#define ADDRESS_STRING_FORMAT_LEADING_ZEROS      2
#define ADDRESS_STRING_FORMAT_RESERVED           3

#define ADDRESS_STRING_FORMAT_HEX_CAPS		    4
#define ADDRESS_STRING_FORMAT_ADD_0X		    8
#define ADDRESS_STRING_FORMAT_ADD_H		   16

class Address: public Object {
public:
				Address() {};
				Address(BuildCtorArg&a): Object(a) {};
	virtual	bool		add(int offset) = 0;
	virtual	int		byteSize() = 0;
	virtual Address	*	clone() const = 0;
	virtual	int		compareDelinear(Address *to);
	virtual	bool 		difference(int &result, Address *to) = 0;
	virtual	void 		getFromArray(const byte *array) = 0;
	virtual	void 		getFromCPUAddress(CPU_ADDR *ca) = 0;
	virtual	bool		getFromUInt64(uint64 u) = 0;
	virtual	bool 		isValid();
	virtual	int		parseString(const char *s, int length, Analyser *a) = 0;
	virtual	void		putIntoArray(byte *array) const = 0;
	virtual	void		putIntoCPUAddress(CPU_ADDR *ca) const = 0;
	virtual	bool		putIntoUInt64(uint64 &u) const = 0;
	virtual	int		stringify(char *s, int max_length, int format) const = 0;
	virtual	int		stringSize() const = 0;
	virtual	int		toString(char *buf, int buflen) const;
};

class InvalidAddress: public Address {
public:
				InvalidAddress() {};
				InvalidAddress(BuildCtorArg&a): Address(a) {};
	virtual	bool		add(int offset);
	virtual	int		byteSize();
	virtual	int		compareTo(const Object *obj) const;
	virtual	bool		difference(int &result, Address *to);
	virtual	InvalidAddress *clone() const;
	virtual	void 		getFromArray(const byte *array);
	virtual	void		getFromCPUAddress(CPU_ADDR *ca);
	virtual	bool		getFromUInt64(uint64 u);
	virtual	bool		isValid();
	virtual	ObjectID	getObjectID() const;
	virtual	int		parseString(const char *s, int length, Analyser *a);
	virtual	void 		putIntoArray(byte *array) const;
	virtual	void 		putIntoCPUAddress(CPU_ADDR *ca) const;
	virtual	bool		putIntoUInt64(uint64 &u) const;
	virtual	int		stringify(char *s, int max_length, int format) const;
	virtual	int		stringSize() const;
};

/*
 *	This address type will be used by most analysers, so we define it here.
 */
class AddressFlat32: public Address {
public:
	uint32 addr;
				AddressFlat32(BuildCtorArg&a): Address(a) {};
				AddressFlat32(uint32 a=0): addr(a) {};
	virtual	bool		add(int offset);
	virtual	int		byteSize();
	virtual	AddressFlat32 *	clone() const;
	virtual	int		compareTo(const Object *obj) const;
	virtual	int		compareDelinear(Address *to);
	virtual	void		getFromArray(const byte *array);
	virtual	void		getFromCPUAddress(CPU_ADDR *ca);
	virtual	bool		getFromUInt64(uint64 u);
	virtual	bool		difference(int &result, Address *to);
	virtual	void		load(ObjectStream &s);
	virtual	ObjectID	getObjectID() const;
	virtual	int		parseString(const char *s, int length, Analyser *a);
	virtual	void		putIntoArray(byte *array) const;
	virtual	void		putIntoCPUAddress(CPU_ADDR *ca) const;
	virtual	bool		putIntoUInt64(uint64 &u) const;
	virtual	void		store(ObjectStream &s) const;
	virtual	int		stringify(char *s, int max_length, int format) const;
	virtual	int		stringSize() const;
};

class AddressFlat64: public Address {
public:
	uint64 addr;
				AddressFlat64(BuildCtorArg&a): Address(a) {};
				AddressFlat64(uint64 a=0): addr(a) {};
	virtual	bool		add(int offset);
	virtual	int		byteSize();
	virtual	int		compareTo(const Object *obj) const;
	virtual	int		compareDelinear(Address *to);
	virtual	void		getFromArray(const byte *array);
	virtual	void		getFromCPUAddress(CPU_ADDR *ca);
	virtual	bool		getFromUInt64(uint64 u);
	virtual	bool		difference(int &result, Address *to);
	virtual	AddressFlat64 *	clone() const;
	virtual	void		load(ObjectStream &s);
	virtual	ObjectID	getObjectID() const;
	virtual	int		parseString(const char *s, int length, Analyser *a);
	virtual	void		putIntoArray(byte *array) const;
	virtual	void		putIntoCPUAddress(CPU_ADDR *ca) const;
	virtual	bool		putIntoUInt64(uint64 &u) const;
	virtual	void		store(ObjectStream &s) const;
	virtual	int		stringify(char *s, int max_length, int format) const;
	virtual	int		stringSize() const;
};

#define ANALY_SEGMENT_CAP_WRITE 1
#define ANALY_SEGMENT_CAP_INITIALIZED 2
// other caps can be defined locally

class Segment: public Object {
	Address *start, *end;
	char *name;
	int caps;
	
					Segment(const char *n, Address *s, Address *e, int c, int address_size);
	virtual	bool			containsAddress(Address *addr) = 0;
	virtual	String &		getName(String &res);
	virtual	int			getAddressSize();
	virtual	int			getCapability(int cap);
};

/*
 *	these are the different possibilities of a branch
 *	to support further processors other types can be added
 */
enum branch_enum_t {
			br_nobranch,					// straight exec. flow
			br_jump,
			br_return,
			br_call,
			br_jXX
};

/*
 *   internal opcodes are interchanged in this format
 */
#define OPCODE dis_insn

/*
 *
 */
class AnalyDisassembler: public Object {
public:
	Analyser		*analy;
	Disassembler		*disasm;
					AnalyDisassembler();
					AnalyDisassembler(BuildCtorArg &a): Object(a) {};

		void			init(Analyser *A);

	virtual	Address *		branchAddr(OPCODE *opcode, branch_enum_t branchtype, bool examine) = 0;
	virtual	void			examineOpcode(OPCODE *opcode) = 0;
	virtual	void			initDisasm();
	virtual	branch_enum_t		isBranch(OPCODE *opcode) = 0;
};

/***************************************************************************/

enum xref_enum_t {
	xrefread,
	xrefwrite,
	xrefoffset,
	xrefjump,
	xrefcall,
	xrefijump,
	xreficall
};

class AddrXRef: public Object {
public:
	Address		*addr;
	xref_enum_t	type;
				AddrXRef(Address *a, xref_enum_t aType = xrefread);
				AddrXRef(BuildCtorArg&a): Object(a) {};
	virtual			~AddrXRef();
	virtual	void		load(ObjectStream &s);
	virtual	ObjectID	getObjectID() const;
	virtual	void		store(ObjectStream &s) const;
	virtual int		compareTo(const Object *) const;
};

class CommentList: public Array {
public:
				CommentList();
	void			appendPreComment(const char *s);
	void			appendPreComment(int special);
	void			appendPostComment(const char *s);
	void			appendPostComment(int special);
	const char *		getName(uint i);
};

struct Symbol;

struct Location {
	// the address
	Address		*addr;
	// this is a tree structure (key is addr)
	Location	*left, *right;
	// attached label
	Symbol		*label;
	// attached xrefs
	Container	*xrefs;
	// attached comments
	CommentList	*comments;
	// for data types
	taddr_type	type;
	// the function the address belongs to (if applicable)
	Location	*thisfunc;
	// some flags
	int		flags;
};

/*
 * taddr.flags:
 */
#define AF_DELETED 1
#define AF_FUNCTION_SET 2
#define AF_FUNCTION_END 4

enum tsectype {
	scvalid,
	scread,
	scwrite,
	screadwrite,
	sccode,
	scinitialized
};

enum taccesstype {
	acread,
	acwrite,
	acoffset
};

struct taccess	{
	bool		indexed;
	int		size;
	taccesstype 	type;
};

enum labeltype {
	label_unknown = 0,
	label_func,
	label_loc,
	label_data
};

struct Symbol {
	labeltype	type;
	Location *	location;
	char *		name;
	Symbol		*left, *right;
};

class AddressQueueItem: public Object {
public:
	Address	*addr;
	Address	*func;
				AddressQueueItem(BuildCtorArg&a): Object(a) {};
				AddressQueueItem(Address *Addr, Address *Func);
				~AddressQueueItem();
	virtual	void		load(ObjectStream &s);
	virtual	ObjectID	getObjectID() const;
	virtual	void		store(ObjectStream &s) const;
};

class CodeAnalyser;
class DataAnalyser;

class Analyser: public Object	{
public:
	Address *		addr;
	Address *		invalid_addr;
	Queue *			addr_queue;
	int			ops_parsed;							// for continuing
	bool			active;
	Address			*next_explored, *first_explored, *last_explored;
	bool			next_address_is_invalid;
	Area *			explored;
	Area *			initialized;
	Location *		locations;
	CodeAnalyser *		code;
	DataAnalyser *		data;
	AnalyDisassembler *	analy_disasm;
	Disassembler *		disasm;
	Symbol *		symbols;
	int			location_threshold, symbol_threshold;
	int			cur_addr_ops, cur_label_ops;                 // for threshold
	int			max_opcode_length;
	Location		*cur_func;
	mutable bool		dirty;

	int			symbol_count;
	int			location_count;

				Analyser() {};
				Analyser(BuildCtorArg&a): Object(a) {};

		void		init();
	virtual	void		load(ObjectStream &s);
	virtual	void		done();

		bool		addAddressSymbol(Address *Addr, const char *Prefix, labeltype type, Location *infunc=NULL);
		void	 	addComment(Address *Addr, int line, const char *c);
		bool		addSymbol(Address *Addr, const char *label, labeltype type, Location *infunc=NULL);
	virtual	FileOfs		addressToFileofs(Address *Addr) = 0;
		bool		addXRef(Address *from, Address *to, xref_enum_t action);
		void	 	assignComment(Address *Addr, int line, const char *c);
		bool		assignSymbol(Address *Addr, const char *label, labeltype type, Location *infunc=NULL);
		void		assignXRef(Address *from, Address *to, xref_enum_t action);
	virtual	void		beginAnalysis();
	virtual	uint		bufPtr(Address *Addr, byte *buf, int size) = 0;
		bool	  	continueAnalysis();
		void		continueAnalysisAt(Address *Addr);
	virtual	Address *	createAddress() = 0;
		void		dataAccess(Address *Addr, taccess access);
		void		deleteLocation(Address *Addr);
		void		deleteSymbol(Address *Addr);
		bool		deleteXRef(Address *from, Address *to);
		void		disableSymbol(Symbol *label);
		void		doBranch(branch_enum_t branch, OPCODE *opcode, int len);
		void		engageCodeanalyser();
		Location *	enumLocations(Address *Addr);
		Location *	enumLocationsReverse(Address *Addr);
		Symbol *	enumSymbolsByName(const char *at);
		Symbol *	enumSymbolsByNameReverse(const char *at);
		Symbol *	enumSymbols(Symbol *sym);
		Symbol *	enumSymbolsReverse(Symbol *sym);
	virtual	taddr_typetype	examineData(Address *Addr);
		void		finish();
		void		freeLocation(Location *loc);
		void		freeLocations(Location *locs);
		void		freeComments(Location *loc);
		void		freeSymbol(Symbol *sym);
		void		freeSymbols(Symbol *syms);
		Location *	getLocationByAddress(Address *Addr);
		Location *	getLocationContextByAddress(Address *Addr);
		int		getLocationCount() const;
		Location *	getFunctionByAddress(Address *Addr);
		Location *	getPreviousSymbolByAddress(Address *Addr);
	virtual	const char *	getSegmentNameByAddress(Address *Addr);
		Symbol *	getSymbolByAddress(Address *Addr);
		Symbol *	getSymbolByName(const char *label);
		const char *	getSymbolNameByLocation(Location *loc);
		int		getSymbolCount() const;
		bool		gotoAddress(Address *Addr, Address *func);
	virtual	void 		initCodeAnalyser();
	virtual	void		initDataAnalyser();
	virtual	void		initUnasm() = 0;
	virtual	void		log(const char *s);                // stub
	virtual	CPU_ADDR 	mapAddr(Address *Addr);      // stub
		Location *	newLocation(Address *Addr);
		Location *	newLocation(Location *&locs, Address *Addr);
		Symbol *	newSymbol(const char *label, Location *loc, labeltype type, Location *infunc);
		Symbol *	newSymbol(Symbol *&syms, const char *label, Location *loc, labeltype type);
	virtual	Address *	nextValid(Address *Addr) = 0;
		void		optimizeLocationTree();
		void		optimizeSymbolTree();
		bool		popAddress(Address **Addr, Address **func);
		void		pushAddress(Address *Addr, Address *func);
	virtual	int		queryConfig(int mode);				// stub
		void		setActive(bool mode);
		void		setLocationFunction(Location *a, Location *func);
		void		setLocationTreeOptimizeThreshold(int threshold);
		void		setDisasm(Disassembler *d);
		void		setSymbolTreeOptimizeThreshold(int threshold);
	virtual	void		store(ObjectStream &s) const;
	virtual	bool		validAddress(Address *addr, tsectype action) = 0;
		bool		validCodeAddress(Address *addr);
		bool		validReadAddress(Address *addr);
		bool		validWriteAddress(Address *addr);

//  interface only (there's no internal use)
		int	mode;

	virtual	Assembler *	createAssembler();
	virtual	Address *	fileofsToAddress(FileOfs fileofs);
		CommentList *	getComments(Address *Addr);
		const char *	getDisasmStr(Address *Addr, int &length);
		const char *	getDisasmStrFormatted(Address *Addr);
		int		getDisplayMode();
	virtual	String &	getName(String &res);
	virtual	const char *	getType();
		Container *	getXRefs(Address *Addr);
		bool		isDirty();
		void		makeDirty();
		void		setDisplayMode(int enable, int disable);
		void		toggleDisplayMode(int toggle);
};

/* display modes */
#define ANALY_SHOW_ADDRESS 1
#define ANALY_SHOW_COMMENTS 2
#define ANALY_SHOW_LABELS 4
#define ANALY_SHOW_XREFS 8
#define ANALY_SHOW_BYTES 16
#define ANALY_EDIT_BYTES 32
#define ANALY_TRANSLATE_SYMBOLS 64
#define ANALY_COLLAPSE_XREFS 128

/* queryConfig() constants */
#define Q_DO_ANALYSIS 1
#define Q_ENGAGE_CODE_ANALYSER 2
#define Q_ENGAGE_DATA_ANALYSER 3

/* interesting constants */
#define INVALID_FILE_OFS ((FileOfs)-1)

/* analyser system constants */
#define MAX_OPS_PER_CONTINUE 10

extern int global_analyser_address_string_format;
 
#endif