forked from phracker/HopperScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComment String XREFs.py
executable file
·47 lines (40 loc) · 1.61 KB
/
Comment String XREFs.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
#
# Add a comment at every XREF to a string (from the __cstring segment).
# This only seems to be necessary on ARM.
#
# Samuel Groß <[email protected]> - github.com/saelo
#
def readString(addr):
"""Read and return a string at the given address"""
seg = Document.getCurrentDocument().getSegmentAtAddress(addr)
string = ""
while not seg.readByte(addr) == 0:
string += chr(seg.readByte(addr))
addr += 1
return string
doc = Document.getCurrentDocument()
# find __cstring segment
stringSegment = None
for i in range(doc.getSegmentCount()):
cur = doc.getSegment(i)
if cur.getName() == '__cstring':
stringSegment = cur
break
if not stringSegment:
doc.log("No cstring section found!")
raise Exception("No cstring segment found!")
# find all strings
for addr in range(stringSegment.getStartingAddress(), stringSegment.getStartingAddress() + stringSegment.getLength()):
if stringSegment.getTypeAtAddress(addr) == Segment.TYPE_ASCII:
string = readString(addr)
xrefs = stringSegment.getReferencesOfAddress(addr)
# if there are no XREFs the above method apparently
# returns "None" instead of an empty list...
if xrefs:
for xref in xrefs:
xrefSegment = doc.getSegmentAtAddress(xref)
comment = xrefSegment.getInlineCommentAtAddress(xref)
if comment is None or comment.startswith('0x'):
xrefSegment.setInlineCommentAtAddress(xref,
'"%s"%s @0x%x' % (string[:100], '..' if len(string) > 100 else '', addr))
addr += len(string)