Skip to content

Commit 3f08a3c

Browse files
committed
API for accessing platform-specific types and function definitions
1 parent 898ec98 commit 3f08a3c

4 files changed

Lines changed: 228 additions & 0 deletions

File tree

binaryninjaapi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,16 @@ namespace BinaryNinja
23422342
void AddRelatedPlatform(Architecture* arch, Platform* platform);
23432343
Ref<Platform> GetAssociatedPlatformByAddress(uint64_t& addr);
23442344

2345+
std::map<QualifiedName, Ref<Type>> GetTypes();
2346+
std::map<QualifiedName, Ref<Type>> GetVariables();
2347+
std::map<QualifiedName, Ref<Type>> GetFunctions();
2348+
std::map<uint32_t, QualifiedNameAndType> GetSystemCalls();
2349+
Ref<Type> GetTypeByName(const QualifiedName& name);
2350+
Ref<Type> GetVariableByName(const QualifiedName& name);
2351+
Ref<Type> GetFunctionByName(const QualifiedName& name);
2352+
std::string GetSystemCallName(uint32_t n);
2353+
Ref<Type> GetSystemCallType(uint32_t n);
2354+
23452355
std::string GenerateAutoPlatformTypeId(const QualifiedName& name);
23462356
Ref<NamedTypeReference> GenerateAutoPlatformTypeReference(BNNamedTypeReferenceClass cls,
23472357
const QualifiedName& name);

binaryninjacore.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,13 @@ extern "C"
12201220
uint64_t end;
12211221
};
12221222

1223+
struct BNSystemCallInfo
1224+
{
1225+
uint32_t number;
1226+
BNQualifiedName name;
1227+
BNType* type;
1228+
};
1229+
12231230
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
12241231
BINARYNINJACOREAPI void BNFreeString(char* str);
12251232
BINARYNINJACOREAPI void BNFreeStringList(char** strs, size_t count);
@@ -2202,6 +2209,17 @@ extern "C"
22022209
BINARYNINJACOREAPI void BNAddRelatedPlatform(BNPlatform* platform, BNArchitecture* arch, BNPlatform* related);
22032210
BINARYNINJACOREAPI BNPlatform* BNGetAssociatedPlatformByAddress(BNPlatform* platform, uint64_t* addr);
22042211

2212+
BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetPlatformTypes(BNPlatform* platform, size_t* count);
2213+
BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetPlatformVariables(BNPlatform* platform, size_t* count);
2214+
BINARYNINJACOREAPI BNQualifiedNameAndType* BNGetPlatformFunctions(BNPlatform* platform, size_t* count);
2215+
BINARYNINJACOREAPI BNSystemCallInfo* BNGetPlatformSystemCalls(BNPlatform* platform, size_t* count);
2216+
BINARYNINJACOREAPI void BNFreeSystemCallList(BNSystemCallInfo* syscalls, size_t count);
2217+
BINARYNINJACOREAPI BNType* BNGetPlatformTypeByName(BNPlatform* platform, BNQualifiedName* name);
2218+
BINARYNINJACOREAPI BNType* BNGetPlatformVariableByName(BNPlatform* platform, BNQualifiedName* name);
2219+
BINARYNINJACOREAPI BNType* BNGetPlatformFunctionByName(BNPlatform* platform, BNQualifiedName* name);
2220+
BINARYNINJACOREAPI char* BNGetPlatformSystemCallName(BNPlatform* platform, uint32_t number);
2221+
BINARYNINJACOREAPI BNType* BNGetPlatformSystemCallType(BNPlatform* platform, uint32_t number);
2222+
22052223
//Demangler
22062224
BINARYNINJACOREAPI bool BNDemangleMS(BNArchitecture* arch,
22072225
const char* mangledName,

platform.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,127 @@ Ref<Platform> Platform::GetAssociatedPlatformByAddress(uint64_t& addr)
255255
}
256256

257257

258+
map<QualifiedName, Ref<Type>> Platform::GetTypes()
259+
{
260+
size_t count;
261+
BNQualifiedNameAndType* types = BNGetPlatformTypes(m_object, &count);
262+
263+
map<QualifiedName, Ref<Type>> result;
264+
for (size_t i = 0; i < count; i++)
265+
{
266+
QualifiedName name = QualifiedName::FromAPIObject(&types[i].name);
267+
result[name] = new Type(BNNewTypeReference(types[i].type));
268+
}
269+
270+
BNFreeTypeList(types, count);
271+
return result;
272+
}
273+
274+
275+
map<QualifiedName, Ref<Type>> Platform::GetVariables()
276+
{
277+
size_t count;
278+
BNQualifiedNameAndType* types = BNGetPlatformVariables(m_object, &count);
279+
280+
map<QualifiedName, Ref<Type>> result;
281+
for (size_t i = 0; i < count; i++)
282+
{
283+
QualifiedName name = QualifiedName::FromAPIObject(&types[i].name);
284+
result[name] = new Type(BNNewTypeReference(types[i].type));
285+
}
286+
287+
BNFreeTypeList(types, count);
288+
return result;
289+
}
290+
291+
292+
map<QualifiedName, Ref<Type>> Platform::GetFunctions()
293+
{
294+
size_t count;
295+
BNQualifiedNameAndType* types = BNGetPlatformFunctions(m_object, &count);
296+
297+
map<QualifiedName, Ref<Type>> result;
298+
for (size_t i = 0; i < count; i++)
299+
{
300+
QualifiedName name = QualifiedName::FromAPIObject(&types[i].name);
301+
result[name] = new Type(BNNewTypeReference(types[i].type));
302+
}
303+
304+
BNFreeTypeList(types, count);
305+
return result;
306+
}
307+
308+
309+
map<uint32_t, QualifiedNameAndType> Platform::GetSystemCalls()
310+
{
311+
size_t count;
312+
BNSystemCallInfo* calls = BNGetPlatformSystemCalls(m_object, &count);
313+
314+
map<uint32_t, QualifiedNameAndType> result;
315+
for (size_t i = 0; i < count; i++)
316+
{
317+
QualifiedNameAndType nt;
318+
nt.name = QualifiedName::FromAPIObject(&calls[i].name);
319+
nt.type = new Type(BNNewTypeReference(calls[i].type));
320+
result[calls[i].number] = nt;
321+
}
322+
323+
BNFreeSystemCallList(calls, count);
324+
return result;
325+
}
326+
327+
328+
Ref<Type> Platform::GetTypeByName(const QualifiedName& name)
329+
{
330+
BNQualifiedName nameObj = name.GetAPIObject();
331+
BNType* type = BNGetPlatformTypeByName(m_object, &nameObj);
332+
QualifiedName::FreeAPIObject(&nameObj);
333+
if (!type)
334+
return nullptr;
335+
return new Type(type);
336+
}
337+
338+
339+
Ref<Type> Platform::GetVariableByName(const QualifiedName& name)
340+
{
341+
BNQualifiedName nameObj = name.GetAPIObject();
342+
BNType* type = BNGetPlatformVariableByName(m_object, &nameObj);
343+
QualifiedName::FreeAPIObject(&nameObj);
344+
if (!type)
345+
return nullptr;
346+
return new Type(type);
347+
}
348+
349+
350+
Ref<Type> Platform::GetFunctionByName(const QualifiedName& name)
351+
{
352+
BNQualifiedName nameObj = name.GetAPIObject();
353+
BNType* type = BNGetPlatformFunctionByName(m_object, &nameObj);
354+
QualifiedName::FreeAPIObject(&nameObj);
355+
if (!type)
356+
return nullptr;
357+
return new Type(type);
358+
}
359+
360+
361+
string Platform::GetSystemCallName(uint32_t n)
362+
{
363+
char* str = BNGetPlatformSystemCallName(m_object, n);
364+
string result = str;
365+
BNFreeString(str);
366+
return result;
367+
}
368+
369+
370+
Ref<Type> Platform::GetSystemCallType(uint32_t n)
371+
{
372+
BNType* type = BNGetPlatformSystemCallType(m_object, n);
373+
if (!type)
374+
return nullptr;
375+
return new Type(type);
376+
}
377+
378+
258379
string Platform::GenerateAutoPlatformTypeId(const QualifiedName& name)
259380
{
260381
BNQualifiedName nameObj = name.GetAPIObject();

python/platform.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,55 @@ def calling_conventions(self):
216216
core.BNFreeCallingConventionList(cc, count.value)
217217
return result
218218

219+
@property
220+
def types(self):
221+
"""List of platform-specific types (read-only)"""
222+
count = ctypes.c_ulonglong(0)
223+
type_list = core.BNGetPlatformTypes(self.handle, count)
224+
result = {}
225+
for i in xrange(0, count.value):
226+
name = types.QualifiedName._from_core_struct(type_list[i].name)
227+
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type))
228+
core.BNFreeTypeList(type_list, count.value)
229+
return result
230+
231+
@property
232+
def variables(self):
233+
"""List of platform-specific variable definitions (read-only)"""
234+
count = ctypes.c_ulonglong(0)
235+
type_list = core.BNGetPlatformVariables(self.handle, count)
236+
result = {}
237+
for i in xrange(0, count.value):
238+
name = types.QualifiedName._from_core_struct(type_list[i].name)
239+
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type))
240+
core.BNFreeTypeList(type_list, count.value)
241+
return result
242+
243+
@property
244+
def functions(self):
245+
"""List of platform-specific function definitions (read-only)"""
246+
count = ctypes.c_ulonglong(0)
247+
type_list = core.BNGetPlatformFunctions(self.handle, count)
248+
result = {}
249+
for i in xrange(0, count.value):
250+
name = types.QualifiedName._from_core_struct(type_list[i].name)
251+
result[name] = types.Type(core.BNNewTypeReference(type_list[i].type))
252+
core.BNFreeTypeList(type_list, count.value)
253+
return result
254+
255+
@property
256+
def system_calls(self):
257+
"""List of system calls for this platform (read-only)"""
258+
count = ctypes.c_ulonglong(0)
259+
call_list = core.BNGetPlatformSystemCalls(self.handle, count)
260+
result = {}
261+
for i in xrange(0, count.value):
262+
name = types.QualifiedName._from_core_struct(call_list[i].name)
263+
t = types.Type(core.BNNewTypeReference(call_list[i].type))
264+
result[call_list[i].number] = (name, t)
265+
core.BNFreeSystemCallList(call_list, count.value)
266+
return result
267+
219268
def __setattr__(self, name, value):
220269
try:
221270
object.__setattr__(self, name, value)
@@ -261,6 +310,36 @@ def get_associated_platform_by_address(self, addr):
261310
result = core.BNGetAssociatedPlatformByAddress(self.handle, new_addr)
262311
return Platform(None, handle = result), new_addr.value
263312

313+
def get_type_by_name(self, name):
314+
name = types.QualifiedName(name)._get_core_struct()
315+
obj = core.BNGetPlatformTypeByName(self.handle, name)
316+
if not obj:
317+
return None
318+
return types.Type(obj)
319+
320+
def get_variable_by_name(self, name):
321+
name = types.QualifiedName(name)._get_core_struct()
322+
obj = core.BNGetPlatformVariableByName(self.handle, name)
323+
if not obj:
324+
return None
325+
return types.Type(obj)
326+
327+
def get_function_by_name(self, name):
328+
name = types.QualifiedName(name)._get_core_struct()
329+
obj = core.BNGetPlatformFunctionByName(self.handle, name)
330+
if not obj:
331+
return None
332+
return types.Type(obj)
333+
334+
def get_system_call_name(self, number):
335+
return core.BNGetPlatformSystemCallName(self.handle, number)
336+
337+
def get_system_call_type(self, number):
338+
obj = core.BNGetPlatformSystemCallType(self.handle, number)
339+
if not obj:
340+
return None
341+
return types.Type(obj)
342+
264343
def generate_auto_platform_type_id(self, name):
265344
name = types.QualifiedName(name)._get_core_struct()
266345
return core.BNGenerateAutoPlatformTypeId(self.handle, name)

0 commit comments

Comments
 (0)