-
Notifications
You must be signed in to change notification settings - Fork 217
/
demangle.cpp
247 lines (209 loc) · 6.87 KB
/
demangle.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "binaryninjaapi.h"
#include <string>
using namespace std;
using namespace BinaryNinja;
namespace BinaryNinja {
bool DemangleGeneric(Ref<Architecture> arch, const std::string& name, Ref<Type>& outType,
QualifiedName& outVarName, Ref<BinaryView> view, bool simplify)
{
BNType* apiType;
BNQualifiedName apiVarName;
bool success = BNDemangleGeneric(
arch->m_object, name.c_str(), &apiType, &apiVarName, view ? view->m_object : nullptr, simplify);
if (!success)
return false;
if (apiType)
outType = new Type(apiType);
outVarName = QualifiedName::FromAPIObject(&apiVarName);
BNFreeQualifiedName(&apiVarName);
return true;
}
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName,
BinaryView* view)
{
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.templateSimplifier", view);
return DemangleLLVM(mangledName, outVarName, simplify);
}
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName,
const bool simplify)
{
char** localVarName = nullptr;
size_t localSize = 0;
if (!BNDemangleLLVM(mangledName.c_str(), &localVarName, &localSize, simplify))
return false;
for (size_t i = 0; i < localSize; i++)
{
outVarName.push_back(localVarName[i]);
}
BNFreeDemangledName(&localVarName, localSize);
return true;
}
bool DemangleMS(Architecture* arch, const std::string& mangledName, Ref<Type>& outType, QualifiedName& outVarName,
BinaryView* view)
{
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.templateSimplifier", view);
return DemangleMS(arch, mangledName, outType, outVarName, simplify);
}
bool DemangleMS(Architecture* arch, const std::string& mangledName, Ref<Type>& outType, QualifiedName& outVarName,
const bool simplify)
{
BNType* localType = nullptr;
char** localVarName = nullptr;
size_t localSize = 0;
if (!BNDemangleMS(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify))
return false;
outType = localType ? new Type(localType) : nullptr;
for (size_t i = 0; i < localSize; i++)
{
outVarName.push_back(localVarName[i]);
}
BNFreeDemangledName(&localVarName, localSize);
return true;
}
bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Ref<Type>& outType, QualifiedName& outVarName,
BinaryView* view)
{
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.templateSimplifier", view);
return DemangleGNU3(arch, mangledName, outType, outVarName, simplify);
}
bool DemangleGNU3(Ref<Architecture> arch, const std::string& mangledName, Ref<Type>& outType, QualifiedName& outVarName,
const bool simplify)
{
BNType* localType;
char** localVarName = nullptr;
size_t localSize = 0;
if (!BNDemangleGNU3(arch->GetObject(), mangledName.c_str(), &localType, &localVarName, &localSize, simplify))
return false;
outType = localType ? new Type(localType) : nullptr;
outVarName.clear();
for (size_t i = 0; i < localSize; i++)
{
outVarName.push_back(localVarName[i]);
}
BNFreeDemangledName(&localVarName, localSize);
return true;
}
bool IsGNU3MangledString(const std::string& mangledName)
{
return BNIsGNU3MangledString(mangledName.c_str());
}
string SimplifyToString(const string& input)
{
return BNRustSimplifyStrToStr(input.c_str());
}
string SimplifyToString(const QualifiedName& input)
{
return BNRustSimplifyStrToStr(input.GetString().c_str());
}
QualifiedName SimplifyToQualifiedName(const string& input, bool simplify)
{
BNQualifiedName name = BNRustSimplifyStrToFQN(input.c_str(), simplify);
QualifiedName result = QualifiedName::FromAPIObject(&name);
BNFreeQualifiedName(&name);
return result;
}
QualifiedName SimplifyToQualifiedName(const QualifiedName& input)
{
BNQualifiedName name = BNRustSimplifyStrToFQN(input.GetString().c_str(), true);
QualifiedName result = QualifiedName::FromAPIObject(&name);
BNFreeQualifiedName(&name);
return result;
}
Demangler::Demangler(const std::string& name): m_nameForRegister(name)
{
}
Demangler::Demangler(BNDemangler* demangler)
{
m_object = demangler;
}
bool Demangler::IsMangledStringCallback(void* ctxt, const char* name)
{
Demangler* demangler = (Demangler*)ctxt;
return demangler->IsMangledString(name);
}
bool Demangler::DemangleCallback(void* ctxt, BNArchitecture* arch, const char* name, BNType** outType,
BNQualifiedName* outVarName, BNBinaryView* view)
{
Demangler* demangler = (Demangler*)ctxt;
Ref<Architecture> apiArch = new CoreArchitecture(arch);
Ref<BinaryView> apiView = view ? new BinaryView(BNNewViewReference(view)) : nullptr;
Ref<Type> apiType;
QualifiedName apiVarName;
bool success = demangler->Demangle(apiArch, name, apiType, apiVarName, apiView);
if (!success)
return false;
if (apiType)
{
*outType = BNNewTypeReference(apiType->m_object);
}
else
{
*outType = nullptr;
}
*outVarName = apiVarName.GetAPIObject();
return true;
}
void Demangler::FreeVarNameCallback(void* ctxt, BNQualifiedName* name)
{
QualifiedName::FreeAPIObject(name);
}
void Demangler::Register(Demangler* demangler)
{
BNDemanglerCallbacks cb;
cb.context = (void*)demangler;
cb.isMangledString = IsMangledStringCallback;
cb.demangle = DemangleCallback;
cb.freeVarName = FreeVarNameCallback;
demangler->m_object = BNRegisterDemangler(demangler->m_nameForRegister.c_str(), &cb);
}
std::vector<Ref<Demangler>> Demangler::GetList()
{
size_t count;
BNDemangler** list = BNGetDemanglerList(&count);
vector<Ref<Demangler>> result;
for (size_t i = 0; i < count; i++)
result.push_back(new CoreDemangler(list[i]));
BNFreeDemanglerList(list);
return result;
}
Ref<Demangler> Demangler::GetByName(const std::string& name)
{
BNDemangler* result = BNGetDemanglerByName(name.c_str());
if (!result)
return nullptr;
return new CoreDemangler(result);
}
void Demangler::Promote(Ref<Demangler> demangler)
{
BNPromoteDemangler(demangler->m_object);
}
std::string Demangler::GetName() const
{
char* name = BNGetDemanglerName(m_object);
std::string value = name;
BNFreeString(name);
return value;
}
CoreDemangler::CoreDemangler(BNDemangler* demangler): Demangler(demangler)
{
}
bool CoreDemangler::IsMangledString(const std::string& name)
{
return BNIsDemanglerMangledName(m_object, name.c_str());
}
bool CoreDemangler::Demangle(Ref<Architecture> arch, const std::string& name, Ref<Type>& outType,
QualifiedName& outVarName, Ref<BinaryView> view)
{
BNType* apiType;
BNQualifiedName apiVarName;
bool success = BNDemanglerDemangle(
m_object, arch->m_object, name.c_str(), &apiType, &apiVarName, view ? view->m_object : nullptr);
if (!success)
return false;
if (apiType)
outType = new Type(apiType);
outVarName = QualifiedName::FromAPIObject(&apiVarName);
BNFreeQualifiedName(&apiVarName);
return true;
}
} // namespace BinaryNinja