summaryrefslogtreecommitdiff
path: root/other/burneye2/func_handling.c
blob: 18a60c5f0327fb1fc8f6c91cbbe51b33ff58e907 (plain)
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* func_handling.c - additional function processing for be2 engine
 *
 * by scut
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <elf.h>

#include <elf_base.h>
#include <elf_reloc.h>
#include <elf_section.h>
#include <ia32-function.h>
#include <ia32-decode.h>
#include <ia32-trace.h>
#include <ia32-dataflow.h>
#include <ia32-codeflow.h>

#include <func_handling.h>
#include <utility.h>
#include <common.h>


/*** static prototypes */

static void
func_bblock_remove_interfunc (ia32_bblock *cur, ia32_xref *xref);


/*** implementation */

unsigned int
func_bblock_deref_interfunc (ia32_function **flist, unsigned int flist_count)
{
	unsigned int		fn,
				changed = 0;
	ia32_bblock **		all = NULL;	/* all basic blocks of a function */
	unsigned int		all_count,
				all_walk;
	ia32_bblock *		cur;
	ia32_function *		target;
	ia32_xref **		xrarr;


	for (fn = 0 ; fn < flist_count ; ++fn) {

		/* do not operate on shallow-copied objects
		 */
		if (flist[fn]->is_copy) {
			ia32_function *	dupe_func;

			dupe_func = ia32_func_list_find_bystart (flist,
				flist_count, flist[fn]->start);
			assert (dupe_func != NULL);
			fnote ("function '%s' is a dupe of '%s'\n",
				flist[fn]->name, dupe_func->name);

			continue;
		}

		all = ia32_br_get_all (flist[fn]->br_root, &all_count);
		for (all_walk = 0 ; all_walk < all_count ; ++all_walk) {
			ia32_xref *	xref;

			cur = all[all_walk];

			/* check whether this basic block ends with a static
			 * inter-function reference. if not, skip it.
			 */
			if (cur->endtype != BR_END_CALL &&
				cur->endtype != BR_END_TRANSFER_INTER &&
				cur->endtype != BR_END_IF_INTER &&
				cur->endtype != BR_END_CALL_EXTERN)
			{
				continue;
			}

			xref = ia32_func_xref_findfrom (flist[fn],
				cur->end - cur->last_ilen - flist[fn]->start);

			assert (xref != NULL && (xref->to_type == IA32_XREF_FUNCTION ||
				xref->to_type == IA32_XREF_FUNCEXTERN));
			target = (ia32_function *) xref->to_data;
			assert (target != NULL || xref->to_type == IA32_XREF_FUNCEXTERN);

			fnote ("bblock'ing static inter-function reference "
				"from: %s -> %s\n", flist[fn]->name,
				target == NULL ? "(__extern__) ?" : target->name);

			/* sanity check: removal of this cross reference from
			 * the basic block itself.
			 */
			func_bblock_remove_interfunc (cur, xref);

			/* sort in all function references into their bblock
			 * other_xref.
			 */
			xref->from += flist[fn]->start - cur->start;
			/*xref->from -= cur->start;*/

			cur->other_xref_count += 1;
			cur->other_xref = xrealloc (cur->other_xref,
				cur->other_xref_count * sizeof (ia32_xref *));
			xrarr = (ia32_xref **) cur->other_xref;
			xrarr[cur->other_xref_count - 1] = xref;

			if (xref->to_type == IA32_XREF_FUNCEXTERN)
				continue;

			/* add the target function root block to the endbr
			 * array. note that this works for all three kinds of
			 * inter-function ends:
			 *  - _END_CALL/_END_TRANSFER_INTER
			 *    has no endbr, so this is the only one
			 *  - _END_IF_INTER
			 *    has one endbr (the false branch), so we add one
			 *    behind, the true basic block.
			 */
			cur->endbr_count += 1;
			cur->endbr = xrealloc (cur->endbr, cur->endbr_count *
				sizeof (ia32_bblock *));
			cur->endbr[cur->endbr_count - 1] = target->br_root;

			if (cur->endbr_external == NULL) {
				cur->endbr_external = xcalloc (cur->endbr_count,
					sizeof (unsigned int));
			} else {
				cur->endbr_external =
					xrealloc (cur->endbr_external,
					cur->endbr_count * sizeof (unsigned int));
			}
			cur->endbr_external[cur->endbr_count - 1] = 1;

			/* increase changed counter
			 */
			changed += 1;
		}

		if (all != NULL)
			free (all);
	}

	return (changed);
}


static void
func_bblock_remove_interfunc (ia32_bblock *cur, ia32_xref *xref)
{
	ia32_xref **	xrarr;
	ia32_xref *	xrw;	/* cross reference walker */
	unsigned int	xn;
	elf_reloc *	rel;


	xrarr = (ia32_xref **) cur->other_xref;
	for (xn = 0 ; xn < cur->other_xref_count ; ++xn) {
		xrw = xrarr[xn];
		assert (xrw != NULL);

		if (xrw->from != xref->from)
			continue;

		rel = (elf_reloc *) xrw->to_data;

		/* TODO: if this ever happens, implement removal of this cross
		 * reference from the basic block. should not happen, as we
		 * would have made an error in the analysis before (which we
		 * hopefully didn't ;).
		 */
		assert (0);
	}
}


void
fix_ustart (elf_base *obj)
{
	unsigned int	n;
	elf_section *	symtab_str;
	elf_section *	symtab;
	char *		fname;
	Elf32_Sym	sent;


	symtab = elf_section_list_find_type (obj->seclist, SHT_SYMTAB, NULL);
	assert (symtab != NULL);
	symtab_str = elf_section_list_find_index (obj->seclist,
		symtab->Shdr.sh_link);
	assert (symtab_str != NULL);


	for (n = 0 ; n < symtab->Shdr.sh_size ; n += sizeof (sent)) {
		memcpy (&sent, &symtab->data[n], sizeof (sent));
		if (ELF32_ST_TYPE (sent.st_info) == STT_FUNC)
			continue;

		fname = elf_string (symtab_str, sent.st_name);
		if (strcmp (fname, "_start") != 0)
			continue;

		fnote ("fixing _start to STT_FUNC");
		sent.st_info = STT_FUNC;
		memcpy (&symtab->data[n], &sent, sizeof (sent));

		break;
	}

	return;
}


/*** HEURISTICS */

void
find_position_curious_functions (ia32_function **flist, unsigned int flist_count)
{
	unsigned int	fn;


	printf ("listing position curious functions\n");

	for (fn = 0 ; fn < flist_count ; ++fn) {
		if (flist[fn]->is_pos_curious == 0)
			continue;

		printf ("0x%08x | %s\n", flist[fn]->start, flist[fn]->name);
	}
	printf ("\n");
}


void
find_abnormal_end_functions (ia32_function **flist, unsigned int flist_count)
{
	unsigned int	fn;


	printf ("listing functions which end abnormally\n");

	for (fn = 0 ; fn < flist_count ; ++fn) {
		if (flist[fn]->is_abnormal_end == 0)
			continue;

		printf ("0x%08x | %s\n", flist[fn]->start, flist[fn]->name);
	}
	printf ("\n");
}


/*** TESTING AND OUTPUT functions */

void
func_output (const char *outputfile, ia32_function **flist, unsigned int flist_count,
	char *fname, int loop_detect)
{
	FILE *		fp;
	ia32_function *	func;


	fp = fopen (outputfile == NULL ? "output.vcg" : outputfile, "w");
	if (fp == NULL) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	func = ia32_func_list_find_byname (flist, flist_count, fname);
	if (func == NULL) {
		fprintf (stderr, "no function named \"%s\" to output, sorry.\n",
			fname);

		exit (EXIT_FAILURE);
	}

	if (loop_detect) {
		ia32_domtree_build (func->br_root);
		ia32_loop_find (func->br_root, loop_detect == 2 ?
			IA32_LOOP_DRAGON : IA32_LOOP_NEST);
	}

	/*ia32_graphviz_br_output (fp, func->br_root, func);*/
	ia32_vcg_br_output (fp, func->br_root, func);
	fclose (fp);

	/*system ("dot -Tps -o output.ps output.dot");*/
	system ("rm -f output.ps ; xvcg -color -psoutput output.ps output.vcg");
	system ("gv output.ps");
/*	system ("rm output.ps output.dot"); */
}


void
func_livereg (const char *outputfile, ia32_function **flist, unsigned int flist_count,
	char *livereg_func, int loop_detect)
{
	FILE *		fp;
	ia32_function *	func =
		ia32_func_list_find_byname (flist, flist_count, livereg_func);

	if (func == NULL) {
		fprintf (stderr, "no function named \"%s\" to do d/f "
			"analysis on.\n", livereg_func);

		exit (EXIT_FAILURE);
	}

	ia32_df_bbtree_live (func, func->br_root);
	if (loop_detect) {
		ia32_domtree_build (func->br_root);
		ia32_loop_find (func->br_root, loop_detect == 2 ?
			IA32_LOOP_DRAGON : IA32_LOOP_NEST);
	}

	func->livereg_available = 1;

	fp = fopen (outputfile == NULL ? "output.vcg" : outputfile, "w");
	if (fp == NULL) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	/*ia32_graphviz_br_output (fp, func->br_root, func);*/
	ia32_vcg_br_output (fp, func->br_root, func);
	fclose (fp);

	/*system ("dot -Tps -o output.ps output.dot");*/
	system ("rm -f output.ps ; xvcg -color -psoutput output.ps output.vcg");
	system ("gv output.ps");
}


void
func_domtree (const char *outputfile, ia32_function **flist,
	unsigned int flist_count, char *domtree_func)
{
	FILE *		fp;
	ia32_function *	func =
		ia32_func_list_find_byname (flist, flist_count, domtree_func);

	if (func == NULL) {
		fprintf (stderr, "no function named \"%s\" to do dominator "
			"tree analysis on.\n", domtree_func);

		exit (EXIT_FAILURE);
	}

	ia32_domtree_build (func->br_root);

	fp = fopen (outputfile == NULL ? "output.vcg" : outputfile, "w");
	if (fp == NULL) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	ia32_vcg_domtree_output (fp, func->br_root);
	fclose (fp);

	system ("rm -f output.ps ; xvcg -color -psoutput output.ps output.vcg");
	system ("gv output.ps");
}


/* restore_section_data
 *
 * restore the backup'ed sections of `base'.
 *
 * return in any case
 */

void
restore_section_data (elf_base *base)
{
	elf_section *	cur;
	unsigned int	sn;

	for (sn = 0 ; sn < base->seclist->elem_count ; ++sn) {
		cur = base->seclist->list[sn];
		memcpy (cur->data, cur->data_backup, cur->data_len);
	}
}


void
backup_section_data (elf_base *base)
{
	elf_section *	cur;
	unsigned int	sn;

	for (sn = 0 ; sn < base->seclist->elem_count ; ++sn) {
		cur = base->seclist->list[sn];
		cur->data_backup = xcalloc (1, cur->data_len);
		memcpy (cur->data_backup, cur->data, cur->data_len);
	}
}


elf_reloc_list *
get_rodata_relocation (elf_base *base, elf_rel_list *rel_list)
{
	elf_section *		sh_walk = NULL;
	elf_reloc_list *	reloc_sec;

	do {
		sh_walk = elf_section_list_find_type (base->seclist,
			SHT_REL, sh_walk);

		if (sh_walk == NULL)
			break;

		reloc_sec = elf_reloc_list_create (base,
			elf_rel_list_find_byrelsection (rel_list, sh_walk),
			NULL, 0);

		if (strcmp (reloc_sec->reloc_modify->name, ".rodata") != 0) {
			elf_reloc_list_destroy (reloc_sec);
			continue;
		}

		elf_reloc_list_hashgen (reloc_sec, 0);

		return (reloc_sec);
	} while (1);

	return (NULL);
}


void
relocate_sections (elf_base *base, elf_rel_list *rel_list)
{
	elf_section *		sh_walk = NULL;
	elf_reloc_list *	reloc_sec;

	do {
		sh_walk = elf_section_list_find_type (base->seclist,
			SHT_REL, sh_walk);

		if (sh_walk == NULL)
			break;

		reloc_sec = elf_reloc_list_create (base,
			elf_rel_list_find_byrelsection (rel_list, sh_walk),
			NULL, 0);

#if 0
		/* do not modify code sections yet
		 */
		if (reloc_sec->reloc_modify->Shdr.sh_flags & SHF_EXECINSTR)
			continue;
#endif

		/* some speed optimization
		 */
		elf_reloc_list_hashgen (reloc_sec, 0);
		relocate_data (base, reloc_sec);

		/* FIXME: memleak */
	} while (1);

	return;
}


code_pair *
code_pair_extract (elf_base *base, elf_section_list *seclist,
	elf_rel_list *rel_list)
{
	code_pair	current;
	code_pair *	cp;		/* temporary object */
	code_pair *	root = NULL;
	code_pair *	listel = NULL;
	elf_section *	sh_walk = NULL;


	do {
		sh_walk = elf_section_list_find_type (base->seclist,
			SHT_PROGBITS, sh_walk);

		if (sh_walk == NULL)
			break;

		if ((sh_walk->Shdr.sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) !=
			(SHF_ALLOC | SHF_EXECINSTR))
			continue;

		memset (&current, 0x00, sizeof (current));

		current.next = NULL;
		current.code_section = sh_walk;
		current.reloc = elf_rel_list_find_bymodsection (rel_list, sh_walk);
		assert (current.reloc != NULL);

		cp = xcalloc (1, sizeof (code_pair));
		memcpy (cp, &current, sizeof (code_pair));

		if (listel == NULL) {
			root = listel = cp;
		} else {
			listel->next = cp;
			listel = cp;
		}
	} while (1);

	return (root);
}


void
relocate_data (elf_base *eb, elf_reloc_list *rl)
{
	unsigned int	rn;		/* relocation entry index walker */
	elf_reloc *	rel;		/* one relocation entry at a time */

	unsigned int	value,		/* absolute base value for reloc */
			symval,		/* the encoded symbol value */
			addend;		/* value found in-place in memory */
	unsigned int *	place;		/* where to commit the relocation */


	for (rn = 0 ; rn < rl->reloc_count ; ++rn) {
		rel = rl->reloc[rn];

		assert (rel->sym != NULL);
		if (rel->sym->sec == NULL) {
			fprintf (stderr, "FATAL: relocation refers NULL "
				"section. most likely this is the result\n"
				"       of dangling *common references, see "
				"manual how to fix this.\n");

			exit (EXIT_FAILURE);
			assert (rel->sym->sec != NULL);
		}

		/* rl->reloc_modify is the only section we modify.
		 * rel->sym->sec is the section we use to compute the address
		 *   we store in the reloc_modify section.
		 */
		place = (unsigned int *)
			&rl->reloc_modify->data[rel->orig.r_offset];

		addend = *place;

		value = (unsigned int) rel->sym->sec->data;
		symval = rel->sym->sent.st_value;

#if 0
		fnote (" [A: 0x%08x, S: 0x%08x, SEC: %08x]\n",
			addend, symval, value);
#endif

		switch (ELF32_R_TYPE (rel->orig.r_info)) {
		/* R_386_32 = S + A */
		case (R_386_32):
			value += symval + addend;
			break;
		default:
			assert (0);
			break;
		}

#if 0
		fnote ("DATA RELOCATION => 0x%08x (%s) into 0x%08x ###\n",
			value, rel->sym->name == NULL ? "?" : rel->sym->name,
			(unsigned int) place);
#endif

		*place = value;
	}
}