-
Notifications
You must be signed in to change notification settings - Fork 0
/
porter.py
87 lines (65 loc) · 2.47 KB
/
porter.py
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
from lst import LST
import os
class Match:
def __init__(self, source_start, source_end, target_start, target_end) -> None:
self.source_start = source_start
self.source_end = source_end
self.target_start = target_start
self.target_end = target_end
assert self.source_end - self.source_start == self.target_end - self.target_start, "Invalid match"
self.size = self.source_end - self.source_start
def source_contains(self, address):
return self.source_start <= address < self.source_end
def target_contains(self, address):
return self.target_start <= address < self.target_end
def fwdport(address):
# OSGlobals will never change
if address < 0x80004000:
return address
for match in matches:
if not match.contains(address):
continue
return address - match.source_start + match.target_start
return 0x0
def backport(address):
# OSGlobals will never change
if address < 0x80004000:
return address
for match in matches:
if not match.target_contains(address):
continue
return address - match.target_start + match.source_start
return 0x0
def init_matches(csvfilename):
global matches
matches = []
with open(csvfilename) as f:
data = f.read().splitlines()
for line in data[1:]:
attrs = [int(attr, base=16) for attr in line.split(',')]
matches.append(Match(*attrs))
def batch_port(source_lst, portfunc):
ported_syms = []
for symbol in source_lst.symbols:
if symbol.address > 0x8031beff:
print(f"Skipping {symbol.name} (outside of .text)")
continue # after .text
ported = portfunc(symbol.address)
if ported > 0x0:
ported_syms.append(f"{hex(ported)[2:]}:{symbol.name}")
else:
print(f"Skipping {symbol.name} (failed to port)")
return ported_syms
matches = []
def main():
csv = input("Matches csv file: ")
init_matches(csv)
should_backport = input("Port backwards? (from target to source) (y/N) ") == 'y'
lst_file = input("LST entries file: ")
source_lst = LST(lst_file)
print()
ported_syms = batch_port(source_lst, backport if should_backport else fwdport)
print()
print('\n'.join(ported_syms))
if __name__ == '__main__':
main()