forked from nologic/idaref
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Show Instruction Reference.py
145 lines (107 loc) · 3.83 KB
/
Show Instruction Reference.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
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
import sqlite3 as sq
import os
import inspect
import glob
doc = Document.getCurrentDocument()
seg = doc.getCurrentSegment()
adr = doc.getCurrentAddress()
doc.log("-----------")
doc.log("Documentation for instruction at " + hex(adr))
instr = seg.getInstructionAtAddress(adr)
# not sure why but stringForArchitecture returns <unknown> for arm/v7 (id 4)
if instr.getArchitecture() == 4:
arch = "arm/v7"
elif instr.getArchitecture() == 5:
arch = "arm/v8"
else:
arch = instr.stringForArchitecture(instr.getArchitecture())
doc.log("Architecture: %s" % arch)
doc.log("instruction: " + instr.getInstructionString())
doc.log("instruction length: %d" % instr.getInstructionLength())
class InstructionReference:
def __init__(self, mnem, arch):
self.arch = arch
self.mnem = mnem
self.ref_term = False
self.inst_map = {}
self.last_inst = None
self.is_loaded = False
self.do_auto = True
self.menu_update = None
self.menu_lookup = None
self.menu_autorefresh = None
self.change_arch = None
self.title = "Instruction Reference"
self.destroying = False
self.base_path = os.path.abspath(os.path.expanduser("~/Library/Application Support/Hopper/Scripts"))
self.archs = self.findManuals()
self.loadArchitecture(arch)
self.load_inst(mnem)
def findManuals(self):
doc_opts = glob.glob(self.base_path + os.sep + "*.sql")
if(len(doc_opts) == 0):
doc.log("Couldn't find any databases in %s" % self.base_path)
return
available = []
for c in doc_opts:
basefile = os.path.splitext(os.path.basename(c))[0]
available.append(basefile)
return available
def loadArchitecture(self, name):
# fix up name
name = name.lower()
if name == "x86_64" or name == "x86" or name == "i386":
name = "x86-64"
elif name.startswith("arm"):
name = "arm"
self.arch = name
path = self.base_path
dbpath = path + os.sep + name + ".sql"
if(not os.path.isfile(dbpath)):
doc.log("Manual not found for architecture: %s" % name)
return False
con = sq.connect(":memory:")
con.text_factory = str
con.executescript(open(dbpath).read())
cur = con.cursor()
cur.execute("SELECT mnem, description FROM instructions")
con.commit()
rows = cur.fetchall()
for row in rows:
inst = row[0]
lines = row[1].replace("\r\n", "\n").split("\n")
if not lines[0].startswith("-R:"):
lines[0] = inst + ": " + lines[0]
self.inst_map[inst] = lines
con.close()
for (inst, data) in self.inst_map.iteritems():
if (data[0].startswith("-R:")):
ref = data[0][3:]
if(ref in self.inst_map):
self.inst_map[inst] = self.inst_map[ref]
doc.log("Manual loaded for architecture: %s" % name)
return True
def cleanInstruction(self, inst):
inst = inst.upper()
# hacks for x86
if(inst[0:1] == 'J' and inst != 'JMP'):
inst = "Jcc"
elif(inst[0:4] == "LOOP"):
inst = "LOOP"
elif(inst[0:3] == "INT"):
inst = "INT n"
elif(inst[0:5] == "FCMOV"):
inst = "FCMOVcc"
elif(inst[0:4] == "CMOV"):
inst = "CMOVcc"
elif(inst[0:3] == "SET"):
inst = "SETcc"
return inst
def load_inst(self, inst):
inst = self.cleanInstruction(inst)
if(inst in self.inst_map):
text = self.inst_map[inst]
doc.log('\n'.join(text))
else:
doc.log(inst + " not documented.")
ref = InstructionReference(instr.getInstructionString(), arch)