scan.cpp
5.64 KB
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
#include <list>
#include <hash_map.h>
#include "romhack.h"
using namespace std;
list<u32> ambiguous;
typedef list<sig_t*> siglist_t;
static hash_map<u32, siglist_t> first_ops;
typedef enum {
NOT_FOUND,
FOUND,
AMBIGUOUS,
} sig_return_t;
void make_first_ops(sigmap_t &sigs)
{
sigmap_t::iterator i;
for (i = sigs.begin(); i != sigs.end(); ++i) {
first_ops[i->second->first_op & 0xffff0000].push_front(i->second);
}
}
sig_return_t check_signature(u32 *code, sig_t *sig)
{
u32 crc = 0;
u32 op;
int i;
list<rel_t*>::iterator p;
u32 *start = code;
u8 not_sure = 0;
p = sig->relocs.begin();
for (i = 0; i < sig->num_ops; ++i) {
op = *code++;
if (p != sig->relocs.end() && i == (*p)->index) {
sym_t *s = (*p)->sym;
switch ((*p)->type) {
case R_MIPS_HI16:
if (s) {
if (s->found) {
if ((op & 0x0000ffff) != (s->hi + (*p)->hi_offset)) {
return NOT_FOUND;
}
} else {
not_sure = 1;
}
}
op &= 0xffff0000;
break;
case R_MIPS_LO16:
if (s) {
if (s->found) {
if ((op & 0x0000ffff) != (s->lo + (*p)->lo_offset)) {
return NOT_FOUND;
}
} else {
not_sure = 1;
}
}
op &= 0xffff0000;
break;
case R_MIPS_26:
if (s) {
if (s->found) {
if ((op & 0x03ffffff) != ( (((s->hi<<16)|s->lo) & 0x0ffffffc)>>2)) {
return NOT_FOUND;
}
} else {
not_sure = 1;
}
}
op &= 0xfc000000;
break;
default:
fprintf(stderr, "Invalid relocation type: %d\n", (*p)->type);
break;
}
++p;
} else {
if (((op >> 26) & 0x3f) == 2) { /* Jump */
op &= 0xfc000000;
}
}
crc = crc32(crc, (u8*)&op, sizeof(op));
if (i == 3 && crc != sig->partial_crc)
return NOT_FOUND;
}
if (crc == sig->crc) {
if (sig->ambiguous && not_sure)
return AMBIGUOUS;
code = start;
p = sig->relocs.begin();
for (i = 0; i < sig->num_ops; ++i) {
op = *code++;
if (p != sig->relocs.end() && i == (*p)->index) {
sym_t *s = (*p)->sym;
if (s && !s->found) {
switch ((*p)->type) {
case R_MIPS_HI16:
s->hi = (op-(*p)->hi_offset) & 0xffff;
s->found = s->lo != 0;
break;
case R_MIPS_LO16:
s->lo = (op-(*p)->lo_offset) & 0xffff;
s->found = s->hi != 0;
break;
case R_MIPS_26:
s->lo = ((op - (*p)->hi_offset) << 2) & 0xffff;
s->hi = (((op - (*p)->hi_offset) & 0x03ffffff) >> 14) | 0x8000;
fprintf(stderr, "%s set to 0x%04x%04x\n", s->symbol, s->hi, s->lo);
s->found = 1;
break;
}
}
++p;
}
}
return FOUND;
} else {
return NOT_FOUND;
}
}
void delete_signature(sig_t *s)
{
first_ops[s->first_op & 0xffff0000].remove(s);
}
u32 check_address(u8* rom, u32 *rom_end, u32 *addr, u8 rescan)
{
siglist_t::iterator sl;
hash_map<u32, siglist_t>::iterator i=first_ops.find(*addr & 0xffff0000);
u32 ret = 1;
if (i != first_ops.end()) {
for (sl = (i->second).begin(); sl != (i->second).end(); sl++) {
if (((*addr) & (*sl)->first_mask) != (*sl)->first_op)
continue;
if ((rom_end - addr) >= (*sl)->num_ops) {
sig_return_t r = check_signature(addr, *sl);
if (r == FOUND) {
fprintf(stderr, "%s found @ 0x%08x\n", (*sl)->symbol, (u8*)addr - rom);
(*sl)->offsets.push_back((u8*)addr - rom);
//delete_signature(*sl);
ret = (*sl)->num_ops;
break;
} else if (r == AMBIGUOUS) {
if (rescan) {
fprintf(stderr, "Still ambiguous match @ 0x%08x [%s]\n", (u8*)addr - rom, (*sl)->symbol);
} else {
fprintf(stderr, "Found ambiguous match @ 0x%08x [%s]\n", (u8*)addr - rom, (*sl)->symbol);
ambiguous.push_back((u32)((u8*)addr-rom));
}
ret = (*sl)->num_ops;
break;
}
}
}
}
return ret;
}
void scan_rom(u8* rom, u32 rom_len, sigmap_t &sigs)
{
u32 *p = (u32 *) rom;
u32 *rom_end = (u32 *)(rom + rom_len);
make_first_ops(sigs);
fprintf(stderr, "Scanning ROM for functions\n");
while (p < rom_end) {
p += check_address(rom, rom_end, p, 0);
}
fprintf(stderr, "done.\n\n");
fprintf(stderr, "Re-scanning ambiguous functions\n");
for (list<u32>::const_iterator i=ambiguous.begin(); i != ambiguous.end(); ++i) {
check_address(rom, rom_end, (u32 *)(rom+*i), 1);
}
fprintf(stderr, "done.\n\n");
}