summaryrefslogtreecommitdiff
path: root/helpers/fixdump.awk
blob: b03f03f7feb265206154375a20390d3babbc3103 (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
#! /usr/bin/gawk -f

BEGIN {
	address_re = "0x[[:xdigit:]]+"
	bracketed_address =  "\\[(([[:space:]]*[[:digit:]]*:)?|[[:alpha:]_]+ = )0x[[:xdigit:]]+\\]"
}

{
	line[NR] = $0
	extract_addresses($0, NR)
}

END {
	for (i = 1; i <= NR; i++) {
		if (line[i] !~ address_re) {
			print line[i]
			continue
		}

		translate(line[i])
	}
}

# Global arrays
#
# Address[line]		--- Address of instruction - first hex number
# Target[address] = 1 	--- Address is target of a jump
# Newaddr[address]	--- Replacement address, counting from 1

function extract_addresses(line, num,	data, i, n, seps, addr)
{
	if (line !~ address_re)
		return

	split(line, data, bracketed_address, seps)
	n = length(seps)

	for (i = 1; i <= n; i++) {
		addr = gensub(".*(" address_re ").*", "\\1", 1, seps[i])
		if (i == 1)
			Address[num] = addr
		else {
			Target[addr]++
			if (! (addr in Newaddr))
				Newaddr[addr] = new_address()
		}
	}
}

function new_address()
{
	return sprintf("%8d", ++Address_seed)
}

function translate(line,	n, data, seps, i, newline)
{
	split(line, data, address_re, seps)
	n = length(seps)
	newline = line
	for (i = 1; i <= n; i++) {
		if (! (seps[i] in Target)) {
			gsub(seps[i], "        ", newline)
			continue
		}
		gsub(seps[i], Newaddr[seps[i]], newline)
	}

	print newline
}