-
Notifications
You must be signed in to change notification settings - Fork 12
/
demangler.py
137 lines (125 loc) · 3.54 KB
/
demangler.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Credit: https://gist.github.com/jackoalan/a4035651f6b870136da5
import sys
def demangleNode(node):
pointer = False
ref = False
const = False
unsigned = False
while True:
if node[0] == 'P':
pointer = True
elif node[0] == 'R':
ref = True
elif node[0] == 'C':
const = True
elif node[0] == 'U':
unsigned = True
else:
break
node = node[1:]
pre = str()
post = str()
if ref:
post = '&'
if pointer:
post = '*'
if const:
pre = 'const '
if unsigned:
pre += 'unsigned '
if node[0] == 'i':
return pre + 'int' + post, node[1:]
elif node[0] == 'b':
return pre + 'bool' + post, node[1:]
elif node[0] == 'c':
return pre + 'char' + post, node[1:]
elif node[0] == 's':
return pre + 'short' + post, node[1:]
elif node[0] == 'l':
return pre + 'long' + post, node[1:]
elif node[0] == 'f':
return pre + 'float' + post, node[1:]
elif node[0] == 'd':
return pre + 'double' + post, node[1:]
elif node[0] == 'w':
return pre + 'wchar_t' + post, node[1:]
elif node[0] == 'v':
return pre + 'void' + post, node[1:]
elif node[0] == 'F':
return demangleFuncNode(node[1:])
elif node[0] == 'Q':
compCount = int(node[1])
node = node[2:]
accum = str()
for idx in range(compCount):
demangle, node = demangleNode(node)
accum += demangle + "::"
return pre + accum[:len(accum)-2] + post, node
elif node[0].isdigit():
counter = 1
while node[counter].isdigit():
counter += 1
length = int(node[:counter])
if node[counter] == '>' or node[counter] == ',':
return '%d' % length, node[counter:]
return pre + node[counter:counter+length] + post, node[counter+length:]
return node, ''
def demangleFuncNode(node):
args = str()
while node[0] != '_':
if len(args):
args += ', '
arg, node = demangleNode(node)
args += arg
node = node[1:]
returnType, node = demangleNode(node)
return '%s(%s)' % (returnType, args), node
def findSepIdx(name, idx):
count = 0
retval = 0
for ch in name:
if ch == '<' or ch == ',':
if idx == count:
return retval
count += 1
retval += 1
return -1
def demangleTemplates(name):
token = 0
accum = name
while True:
tidx = findSepIdx(accum, token)
token += 1
if tidx < 0:
return accum
sidx = tidx + 1
toDem = accum[sidx:]
accum = accum[:sidx]
dem, rem = demangleNode(toDem)
accum += dem + rem
def demangle(sym):
typeSplit = sym[1:].find('__')
funcName = demangleTemplates(sym[:typeSplit+1])
rem = sym[typeSplit+3:]
funcType, rem = demangleNode(rem)
funcType = demangleTemplates(funcType)
objType = rem[0]
post = str()
if objType == 'C':
post = ' const'
objType = rem[1]
rem = rem[1:]
rem = rem[1:]
funcArgs = str()
while len(rem):
if len(funcArgs):
funcArgs += ', '
arg, rem = demangleNode(rem)
funcArgs += demangleTemplates(arg)
return '%s::%s(%s)%s' % (funcType, funcName, funcArgs, post)
if __name__ == "__main__":
if len(sys.argv) < 2:
raise RuntimeError("needs symbol argument")
print(demangle(sys.argv[1]))