summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2018-09-28 22:54:05 -0400
committerPeter LaFosse <peter@vector35.com>2018-09-28 22:54:05 -0400
commit515381255191f35845e8c57059842741fcc652cd (patch)
treee62b554e4f25f9da7d3388941a9aeffd22b6d290
parent328281d02cc9ed281b47915c4b02e36328ad2bb5 (diff)
Namespace API cleanup
-rw-r--r--binaryninjaapi.h29
-rw-r--r--binaryninjacore.h35
-rw-r--r--binaryview.cpp101
-rw-r--r--python/binaryview.py127
-rw-r--r--python/types.py22
-rw-r--r--settings.cpp5
-rw-r--r--type.cpp17
7 files changed, 249 insertions, 87 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h
index 91a5061a..5ebfd92e 100644
--- a/binaryninjaapi.h
+++ b/binaryninjaapi.h
@@ -919,6 +919,7 @@ namespace BinaryNinja
NameList(const std::string& name, const std::string& join);
NameList(const std::vector<std::string>& name, const std::string& join);
NameList(const NameList& name, const std::string& join);
+ virtual ~NameList();
virtual NameList& operator=(const std::string& name);
virtual NameList& operator=(const std::vector<std::string>& name);
@@ -965,6 +966,7 @@ namespace BinaryNinja
QualifiedName(const std::string& name);
QualifiedName(const std::vector<std::string>& name);
QualifiedName(const QualifiedName& name);
+ virtual ~QualifiedName();
virtual QualifiedName& operator=(const std::string& name);
virtual QualifiedName& operator=(const std::vector<std::string>& name);
@@ -983,6 +985,7 @@ namespace BinaryNinja
NameSpace(const std::string& name);
NameSpace(const std::vector<std::string>& name);
NameSpace(const NameSpace& name);
+ virtual ~NameSpace();
virtual NameSpace& operator=(const std::string& name);
virtual NameSpace& operator=(const std::vector<std::string>& name);
@@ -1354,20 +1357,20 @@ namespace BinaryNinja
std::vector<ReferenceSource> GetCodeReferences(uint64_t addr);
std::vector<ReferenceSource> GetCodeReferences(uint64_t addr, uint64_t len);
- Ref<Symbol> GetSymbolByAddress(uint64_t addr);
- Ref<Symbol> GetSymbolByRawName(const std::string& name);
- std::vector<Ref<Symbol>> GetSymbolsByName(const std::string& name);
- std::vector<Ref<Symbol>> GetSymbols();
- std::vector<Ref<Symbol>> GetSymbols(uint64_t start, uint64_t len);
- std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type);
- std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len);
+ Ref<Symbol> GetSymbolByAddress(uint64_t addr, const NameSpace& nameSpace=NameSpace());
+ Ref<Symbol> GetSymbolByRawName(const std::string& name, const NameSpace& nameSpace=NameSpace());
+ std::vector<Ref<Symbol>> GetSymbolsByName(const std::string& name, const NameSpace& nameSpace=NameSpace());
+ std::vector<Ref<Symbol>> GetSymbols(const NameSpace& nameSpace=NameSpace());
+ std::vector<Ref<Symbol>> GetSymbols(uint64_t start, uint64_t len, const NameSpace& nameSpace=NameSpace());
+ std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, const NameSpace& nameSpace=NameSpace());
+ std::vector<Ref<Symbol>> GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len, const NameSpace& nameSpace=NameSpace());
void DefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace());
- void DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type);
- void UndefineAutoSymbol(Ref<Symbol> sym);
+ void DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type, const NameSpace& nameSpace=NameSpace());
+ void UndefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace());
- void DefineUserSymbol(Ref<Symbol> sym);
- void UndefineUserSymbol(Ref<Symbol> sym);
+ void DefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace());
+ void UndefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace=NameSpace());
void DefineImportedFunction(Ref<Symbol> importAddressSym, Ref<Function> func);
@@ -1474,6 +1477,10 @@ namespace BinaryNinja
void SetMaxFunctionSizeForAnalysis(uint64_t size);
bool GetNewAutoFunctionAnalysisSuppressed();
void SetNewAutoFunctionAnalysisSuppressed(bool suppress);
+
+ std::set<NameSpace> GetNameSpaces() const;
+ NameSpace GetInternalNameSpace() const;
+ NameSpace GetExternalNameSpace() const;
};
diff --git a/binaryninjacore.h b/binaryninjacore.h
index c2e6943f..ae4b27a3 100644
--- a/binaryninjacore.h
+++ b/binaryninjacore.h
@@ -98,8 +98,8 @@
#define BN_MINIMUM_CONFIDENCE 1
#define BN_HEURISTIC_CONFIDENCE 192
-#define DEFAULT_INTERNAL_NAMESPACE "INTERNAL"
-#define DEFAULT_EXTERNAL_NAMESPACE "EXTERNAL"
+#define DEFAULT_INTERNAL_NAMESPACE "BNINTERNALNAMESPACE"
+#define DEFAULT_EXTERNAL_NAMESPACE "BNEXTERNALNAMESPACE"
#ifdef __cplusplus
extern "C"
@@ -281,8 +281,7 @@ extern "C"
ImportedFunctionSymbol = 2,
DataSymbol = 3,
ImportedDataSymbol = 4,
- ExternalSymbol = 5,
- LabelSymbol = 6
+ ExternalSymbol = 5
};
enum BNSymbolBinding
@@ -2179,6 +2178,12 @@ extern "C"
BINARYNINJACOREAPI char** BNGetUniqueSectionNames(BNBinaryView* view, const char** names, size_t count);
+ BINARYNINJACOREAPI BNNameSpace* BNGetNameSpaces(BNBinaryView* view, size_t* count);
+ BINARYNINJACOREAPI void BNFreeNameSpaceList(BNNameSpace* nameSpace, size_t count);
+ BINARYNINJACOREAPI BNNameSpace BNGetExternalNameSpace(BNBinaryView* view);
+ BINARYNINJACOREAPI BNNameSpace BNGetInternalNameSpace(BNBinaryView* view);
+ BINARYNINJACOREAPI void BNFreeNameSpace(BNNameSpace* name);
+
BINARYNINJACOREAPI BNAddressRange* BNGetAllocatedRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeAddressRanges(BNAddressRange* ranges);
@@ -2833,23 +2838,23 @@ extern "C"
BINARYNINJACOREAPI uint64_t BNGetSymbolAddress(BNSymbol* sym);
BINARYNINJACOREAPI bool BNIsSymbolAutoDefined(BNSymbol* sym);
- BINARYNINJACOREAPI BNSymbol* BNGetSymbolByAddress(BNBinaryView* view, uint64_t addr);
- BINARYNINJACOREAPI BNSymbol* BNGetSymbolByRawName(BNBinaryView* view, const char* name);
- BINARYNINJACOREAPI BNSymbol** BNGetSymbolsByName(BNBinaryView* view, const char* name, size_t* count);
- BINARYNINJACOREAPI BNSymbol** BNGetSymbols(BNBinaryView* view, size_t* count);
- BINARYNINJACOREAPI BNSymbol** BNGetSymbolsInRange(BNBinaryView* view, uint64_t start, uint64_t len, size_t* count);
- BINARYNINJACOREAPI BNSymbol** BNGetSymbolsOfType(BNBinaryView* view, BNSymbolType type, size_t* count);
+ BINARYNINJACOREAPI BNSymbol* BNGetSymbolByAddress(BNBinaryView* view, uint64_t addr, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI BNSymbol* BNGetSymbolByRawName(BNBinaryView* view, const char* name, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI BNSymbol** BNGetSymbolsByName(BNBinaryView* view, const char* name, size_t* count, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI BNSymbol** BNGetSymbols(BNBinaryView* view, size_t* count, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI BNSymbol** BNGetSymbolsInRange(BNBinaryView* view, uint64_t start, uint64_t len, size_t* count, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI BNSymbol** BNGetSymbolsOfType(BNBinaryView* view, BNSymbolType type, size_t* count, const BNNameSpace* nameSpace);
BINARYNINJACOREAPI BNSymbol** BNGetSymbolsOfTypeInRange(BNBinaryView* view, BNSymbolType type,
- uint64_t start, uint64_t len, size_t* count);
+ uint64_t start, uint64_t len, size_t* count, const BNNameSpace* nameSpace);
BINARYNINJACOREAPI void BNFreeSymbolList(BNSymbol** syms, size_t count);
BINARYNINJACOREAPI void BNDefineAutoSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace);
- BINARYNINJACOREAPI void BNUndefineAutoSymbol(BNBinaryView* view, BNSymbol* sym);
- BINARYNINJACOREAPI void BNDefineUserSymbol(BNBinaryView* view, BNSymbol* sym);
- BINARYNINJACOREAPI void BNUndefineUserSymbol(BNBinaryView* view, BNSymbol* sym);
+ BINARYNINJACOREAPI void BNUndefineAutoSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI void BNDefineUserSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace);
+ BINARYNINJACOREAPI void BNUndefineUserSymbol(BNBinaryView* view, BNSymbol* sym, const BNNameSpace* nameSpace);
BINARYNINJACOREAPI void BNDefineImportedFunction(BNBinaryView* view, BNSymbol* importAddressSym, BNFunction* func);
BINARYNINJACOREAPI void BNDefineAutoSymbolAndVariableOrFunction(BNBinaryView* view, BNPlatform* platform,
- BNSymbol* sym, BNType* type);
+ BNSymbol* sym, BNType* type, const BNNameSpace* nameSpace);
BINARYNINJACOREAPI BNSymbol* BNImportedFunctionFromImportAddressSymbol(BNSymbol* sym, uint64_t addr);
diff --git a/binaryview.cpp b/binaryview.cpp
index 1540e049..7ddcf672 100644
--- a/binaryview.cpp
+++ b/binaryview.cpp
@@ -195,7 +195,8 @@ BNSymbolType Symbol::GetType() const
NameSpace Symbol::GetNameSpace() const
{
BNNameSpace name = BNGetSymbolNameSpace(m_object);
- return NameSpace::FromAPIObject(&name);
+ NameSpace result = NameSpace::FromAPIObject(&name);
+ BNFreeNameSpace(&name);
}
@@ -1430,28 +1431,31 @@ vector<ReferenceSource> BinaryView::GetCodeReferences(uint64_t addr, uint64_t le
}
-Ref<Symbol> BinaryView::GetSymbolByAddress(uint64_t addr)
+Ref<Symbol> BinaryView::GetSymbolByAddress(uint64_t addr, const NameSpace& nameSpace)
{
- BNSymbol* sym = BNGetSymbolByAddress(m_object, addr);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol* sym = BNGetSymbolByAddress(m_object, addr, &ns);
if (!sym)
return nullptr;
return new Symbol(sym);
}
-Ref<Symbol> BinaryView::GetSymbolByRawName(const string& name)
+Ref<Symbol> BinaryView::GetSymbolByRawName(const string& name, const NameSpace& nameSpace)
{
- BNSymbol* sym = BNGetSymbolByRawName(m_object, name.c_str());
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol* sym = BNGetSymbolByRawName(m_object, name.c_str(), &ns);
if (!sym)
return nullptr;
return new Symbol(sym);
}
-vector<Ref<Symbol>> BinaryView::GetSymbolsByName(const string& name)
+vector<Ref<Symbol>> BinaryView::GetSymbolsByName(const string& name, const NameSpace& nameSpace)
{
size_t count;
- BNSymbol** syms = BNGetSymbolsByName(m_object, name.c_str(), &count);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol** syms = BNGetSymbolsByName(m_object, name.c_str(), &count, &ns);
vector<Ref<Symbol>> result;
result.reserve(count);
@@ -1463,10 +1467,11 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsByName(const string& name)
}
-vector<Ref<Symbol>> BinaryView::GetSymbols()
+vector<Ref<Symbol>> BinaryView::GetSymbols(const NameSpace& nameSpace)
{
size_t count;
- BNSymbol** syms = BNGetSymbols(m_object, &count);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol** syms = BNGetSymbols(m_object, &count, &ns);
vector<Ref<Symbol>> result;
result.reserve(count);
@@ -1478,10 +1483,11 @@ vector<Ref<Symbol>> BinaryView::GetSymbols()
}
-vector<Ref<Symbol>> BinaryView::GetSymbols(uint64_t start, uint64_t len)
+vector<Ref<Symbol>> BinaryView::GetSymbols(uint64_t start, uint64_t len, const NameSpace& nameSpace)
{
size_t count;
- BNSymbol** syms = BNGetSymbolsInRange(m_object, start, len, &count);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol** syms = BNGetSymbolsInRange(m_object, start, len, &count, &ns);
vector<Ref<Symbol>> result;
result.reserve(count);
@@ -1493,10 +1499,11 @@ vector<Ref<Symbol>> BinaryView::GetSymbols(uint64_t start, uint64_t len)
}
-vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type)
+vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, const NameSpace& nameSpace)
{
size_t count;
- BNSymbol** syms = BNGetSymbolsOfType(m_object, type, &count);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol** syms = BNGetSymbolsOfType(m_object, type, &count, &ns);
vector<Ref<Symbol>> result;
result.reserve(count);
@@ -1508,10 +1515,11 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type)
}
-vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len)
+vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t start, uint64_t len, const NameSpace& nameSpace)
{
size_t count;
- BNSymbol** syms = BNGetSymbolsOfTypeInRange(m_object, type, start, len, &count);
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNSymbol** syms = BNGetSymbolsOfTypeInRange(m_object, type, start, len, &count, &ns);
vector<Ref<Symbol>> result;
result.reserve(count);
@@ -1525,42 +1533,37 @@ vector<Ref<Symbol>> BinaryView::GetSymbolsOfType(BNSymbolType type, uint64_t sta
void BinaryView::DefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace)
{
- BNNameSpace ns;
- ns.join = BNAllocString(nameSpace.GetJoinString().c_str());
- ns.name = new char*[nameSpace.size()];
- for (size_t i = 0; i < nameSpace.size(); i++)
- ns.name[i] = BNAllocString(nameSpace[i].c_str());
- ns.nameCount = nameSpace.size();
+ BNNameSpace ns = nameSpace.GetAPIObject();
BNDefineAutoSymbol(m_object, sym->GetObject(), &ns);
- BNFreeString(ns.join);
- for (size_t i = 0; i < nameSpace.size(); i++)
- BNFreeString(ns.name[i]);
- delete[] ns.name;
}
-void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type)
+void BinaryView::DefineAutoSymbolAndVariableOrFunction(Ref<Platform> platform, Ref<Symbol> sym, Ref<Type> type, const NameSpace& nameSpace)
{
+ BNNameSpace ns = nameSpace.GetAPIObject();
BNDefineAutoSymbolAndVariableOrFunction(m_object, platform ? platform->GetObject() : nullptr, sym->GetObject(),
- type ? type->GetObject() : nullptr);
+ type ? type->GetObject() : nullptr, &ns);
}
-void BinaryView::UndefineAutoSymbol(Ref<Symbol> sym)
+void BinaryView::UndefineAutoSymbol(Ref<Symbol> sym, const NameSpace& nameSpace)
{
- BNUndefineAutoSymbol(m_object, sym->GetObject());
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNUndefineAutoSymbol(m_object, sym->GetObject(), &ns);
}
-void BinaryView::DefineUserSymbol(Ref<Symbol> sym)
+void BinaryView::DefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace)
{
- BNDefineUserSymbol(m_object, sym->GetObject());
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNDefineUserSymbol(m_object, sym->GetObject(), &ns);
}
-void BinaryView::UndefineUserSymbol(Ref<Symbol> sym)
+void BinaryView::UndefineUserSymbol(Ref<Symbol> sym, const NameSpace& nameSpace)
{
- BNUndefineUserSymbol(m_object, sym->GetObject());
+ BNNameSpace ns = nameSpace.GetAPIObject();
+ BNUndefineUserSymbol(m_object, sym->GetObject(), &ns);
}
@@ -2259,9 +2262,39 @@ void BinaryView::SetNewAutoFunctionAnalysisSuppressed(bool suppress)
}
+set<NameSpace> BinaryView::GetNameSpaces() const
+{
+ set<NameSpace> nameSpaces;
+ size_t count = 0;
+ BNNameSpace* nameSpaceList = BNGetNameSpaces(m_object, &count);
+ for (size_t i = 0; i < count; i++)
+ nameSpaces.insert(NameSpace::FromAPIObject(&nameSpaceList[i]));
+ BNFreeNameSpaceList(nameSpaceList, count);
+ return nameSpaces;
+}
+
+
+NameSpace BinaryView::GetInternalNameSpace() const
+{
+ BNNameSpace ns = BNGetInternalNameSpace(m_object);
+ NameSpace nameSpace = NameSpace::FromAPIObject(&ns);
+ BNFreeNameSpace(&ns);
+ return nameSpace;
+}
+
+
+NameSpace BinaryView::GetExternalNameSpace() const
+{
+ BNNameSpace ns = BNGetExternalNameSpace(m_object);
+ NameSpace nameSpace = NameSpace::FromAPIObject(&ns);
+ BNFreeNameSpace(&ns);
+ return nameSpace;
+}
+
+
Relocation::Relocation(BNRelocation* reloc)
{
- m_object = reloc;
+ m_object = reloc;
}
diff --git a/python/binaryview.py b/python/binaryview.py
index f7ead7a6..5802c3ba 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -1011,15 +1011,44 @@ class BinaryView(object):
def symbols(self):
"""Dict of symbols (read-only)"""
count = ctypes.c_ulonglong(0)
- syms = core.BNGetSymbols(self.handle, count)
+ syms = core.BNGetSymbols(self.handle, count, None)
result = {}
for i in range(0, count.value):
sym = types.Symbol(None, None, None, handle=core.BNNewSymbolReference(syms[i]))
- result[sym.raw_name] = sym
+ if sym.raw_name in result:
+ result[sym.raw_name] = [result[sym.raw_name], sym]
+ else:
+ result[sym.raw_name] = sym
core.BNFreeSymbolList(syms, count.value)
return result
@property
+ def internal_namespace(self):
+ """Internal namespace for the current BinaryView"""
+ ns = core.BNGetInternalNameSpace(self.handle)
+ result = types.NameSpace._from_core_struct(ns)
+ core.BNFreeNameSpace(ns)
+ return result
+
+ @property
+ def external_namespace(self):
+ """External namespace for the current BinaryView"""
+ ns = core.BNGetExternalNameSpace(self.handle)
+ result = types.NameSpace._from_core_struct(ns)
+ core.BNFreeNameSpace(ns)
+ return result
+
+ @property
+ def namespaces(self):
+ count = ctypes.c_ulonglong(0)
+ nameSpaceList = core.BNGetNameSpaces(self.handle, count)
+ result = []
+ for i in range(count.value):
+ result.append(types.NameSpace._from_core_struct(nameSpaceList[i]))
+ core.BNFreeNameSpaceList(nameSpaceList, count.value);
+ return result
+
+ @property
def view_type(self):
"""View type (read-only)"""
return core.BNGetViewType(self.handle)
@@ -2402,12 +2431,13 @@ class BinaryView(object):
core.BNFreeCodeReferences(refs, count.value)
return result
- def get_symbol_at(self, addr):
+ def get_symbol_at(self, addr, namespace=None):
"""
``get_symbol_at`` returns the Symbol at the provided virtual address.
:param int addr: virtual address to query for symbol
:return: Symbol for the given virtual address
+ :param NameSpace namespace: the namespace of the symbols to retrieve
:rtype: Symbol
:Example:
@@ -2415,17 +2445,23 @@ class BinaryView(object):
<FunctionSymbol: "_start" @ 0x100001174>
>>>
"""
- sym = core.BNGetSymbolByAddress(self.handle, addr)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+
+ sym = core.BNGetSymbolByAddress(self.handle, addr, namespace)
if sym is None:
return None
return types.Symbol(None, None, None, handle = sym)
- def get_symbol_by_raw_name(self, name):
+ def get_symbol_by_raw_name(self, name, namespace=None):
"""
``get_symbol_by_raw_name`` retrieves a Symbol object for the given a raw (mangled) name.
:param str name: raw (mangled) name of Symbol to be retrieved
:return: Symbol object corresponding to the provided raw name
+ :param NameSpace namespace: the namespace to search for the given symbol
:rtype: Symbol
:Example:
@@ -2433,17 +2469,22 @@ class BinaryView(object):
<FunctionSymbol: "public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)" @ 0x10001100>
>>>
"""
- sym = core.BNGetSymbolByRawName(self.handle, name)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ sym = core.BNGetSymbolByRawName(self.handle, name, namespace)
if sym is None:
return None
return types.Symbol(None, None, None, handle = sym)
- def get_symbols_by_name(self, name):
+ def get_symbols_by_name(self, name, namespace=None):
"""
``get_symbols_by_name`` retrieves a list of Symbol objects for the given symbol name.
:param str name: name of Symbol object to be retrieved
:return: Symbol object corresponding to the provided name
+ :param NameSpace namespace: the namespace of the symbol
:rtype: Symbol
:Example:
@@ -2451,15 +2492,19 @@ class BinaryView(object):
[<FunctionSymbol: "public: static enum Foobar::foo __cdecl Foobar::testf(enum Foobar::foo)" @ 0x10001100>]
>>>
"""
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
count = ctypes.c_ulonglong(0)
- syms = core.BNGetSymbolsByName(self.handle, name, count)
+ syms = core.BNGetSymbolsByName(self.handle, name, count, namespace)
result = []
for i in range(0, count.value):
result.append(types.Symbol(None, None, None, handle = core.BNNewSymbolReference(syms[i])))
core.BNFreeSymbolList(syms, count.value)
return result
- def get_symbols(self, start = None, length = None):
+ def get_symbols(self, start=None, length=None, namespace=None):
"""
``get_symbols`` retrieves the list of all Symbol objects in the optionally provided range.
@@ -2474,17 +2519,21 @@ class BinaryView(object):
>>>
"""
count = ctypes.c_ulonglong(0)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
if start is None:
- syms = core.BNGetSymbols(self.handle, count)
+ syms = core.BNGetSymbols(self.handle, count, namespace)
else:
- syms = core.BNGetSymbolsInRange(self.handle, start, length, count)
+ syms = core.BNGetSymbolsInRange(self.handle, start, length, count, namespace)
result = []
for i in range(0, count.value):
result.append(types.Symbol(None, None, None, handle = core.BNNewSymbolReference(syms[i])))
core.BNFreeSymbolList(syms, count.value)
return result
- def get_symbols_of_type(self, sym_type, start = None, length = None):
+ def get_symbols_of_type(self, sym_type, start=None, length=None, namespace=None):
"""
``get_symbols_of_type`` retrieves a list of all Symbol objects of the provided symbol type in the optionally
provided range.
@@ -2502,9 +2551,13 @@ class BinaryView(object):
"""
if isinstance(sym_type, str):
sym_type = SymbolType[sym_type]
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
count = ctypes.c_ulonglong(0)
if start is None:
- syms = core.BNGetSymbolsOfType(self.handle, sym_type, count)
+ syms = core.BNGetSymbolsOfType(self.handle, sym_type, count, namespace)
else:
syms = core.BNGetSymbolsOfTypeInRange(self.handle, sym_type, start, length, count)
result = []
@@ -2513,18 +2566,24 @@ class BinaryView(object):
core.BNFreeSymbolList(syms, count.value)
return result
- def define_auto_symbol(self, sym):
+ def define_auto_symbol(self, sym, namespace=None):
"""
- ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects.
+ ``define_auto_symbol`` adds a symbol to the internal list of automatically discovered Symbol objects in a given
+ namespace.
.. warning:: If multiple symbols for the same address are defined, only the most recent symbol will ever be used.
:param Symbol sym: the symbol to define
+ :param NameSpace namespace: the namespace of the symbol
:rtype: None
"""
- core.BNDefineAutoSymbol(self.handle, sym.handle)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ core.BNDefineAutoSymbol(self.handle, sym.handle, namespace)
- def define_auto_symbol_and_var_or_function(self, sym, sym_type, plat=None):
+ def define_auto_symbol_and_var_or_function(self, sym, sym_type, plat=None, namespace=None):
"""
``define_auto_symbol_and_var_or_function``
@@ -2533,6 +2592,7 @@ class BinaryView(object):
:param Symbol sym: the symbol to define
:param SymbolType sym_type: Type of symbol being defined
:param Platform plat: (optional) platform
+ :param NameSpace namespace: the namespace of the symbol
:rtype: None
"""
if plat is None:
@@ -2541,36 +2601,55 @@ class BinaryView(object):
plat = plat.handle
if sym_type is not None:
sym_type = sym_type.handle
- core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, plat, sym.handle, sym_type)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ core.BNDefineAutoSymbolAndVariableOrFunction(self.handle, plat, sym.handle, sym_type, namespace)
- def undefine_auto_symbol(self, sym):
+ def undefine_auto_symbol(self, sym, namespace=None):
"""
``undefine_auto_symbol`` removes a symbol from the internal list of automatically discovered Symbol objects.
:param Symbol sym: the symbol to undefine
+ :param NameSpace namespace: the namespace of the symbol
:rtype: None
"""
- core.BNUndefineAutoSymbol(self.handle, sym.handle)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ core.BNUndefineAutoSymbol(self.handle, sym.handle, namespace)
- def define_user_symbol(self, sym):
+ def define_user_symbol(self, sym, namespace=None):
"""
``define_user_symbol`` adds a symbol to the internal list of user added Symbol objects.
.. warning:: If multiple symbols for the same address are defined, only the most recent symbol will ever be used.
:param Symbol sym: the symbol to define
+ :param NameSpace namespace: the namespace of the symbol
:rtype: None
"""
- core.BNDefineUserSymbol(self.handle, sym.handle)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ core.BNDefineUserSymbol(self.handle, sym.handle, namespace)
- def undefine_user_symbol(self, sym):
+ def undefine_user_symbol(self, sym, namespace=None):
"""
``undefine_user_symbol`` removes a symbol from the internal list of user added Symbol objects.
:param Symbol sym: the symbol to undefine
+ :param NameSpace namespace: the namespace of the symbol
:rtype: None
"""
- core.BNUndefineUserSymbol(self.handle, sym.handle)
+ if isinstance(namespace, str):
+ namespace = types.NameSpace(namespace)
+ if isinstance(namespace, types.NameSpace):
+ namespace = namespace._get_core_struct()
+ core.BNUndefineUserSymbol(self.handle, sym.handle, namespace)
def define_imported_function(self, import_addr_sym, func):
"""
diff --git a/python/types.py b/python/types.py
index f8ff279a..a7973fd7 100644
--- a/python/types.py
+++ b/python/types.py
@@ -123,6 +123,22 @@ class NameSpace(QualifiedName):
def __str__(self):
return ":".join(self.name)
+ def _get_core_struct(self):
+ result = core.BNNameSpace()
+ name_list = (ctypes.c_char_p * len(self.name))()
+ for i in range(0, len(self.name)):
+ name_list[i] = self.name[i].encode('charmap')
+ result.name = name_list
+ result.nameCount = len(self.name)
+ return result
+
+ @classmethod
+ def _from_core_struct(cls, name):
+ result = []
+ for i in range(0, name.nameCount):
+ result.append(name.name[i])
+ return NameSpace(result)
+
class Symbol(object):
"""
Symbols are defined as one of the following types:
@@ -135,6 +151,7 @@ class Symbol(object):
ImportedFunctionSymbol Symbol for Function that is not defined in the current binary
DataSymbol Symbol for Data in the current binary
ImportedDataSymbol Symbol for Data that is not defined in the current binary
+ ExternalSymbol Symbols for data and code that reside outside the BinaryView
=========================== ==============================================================
"""
def __init__(self, sym_type, addr, short_name, full_name = None, raw_name = None, handle = None):
@@ -170,7 +187,10 @@ class Symbol(object):
@property
def namespace(self):
"""Symbol namespace (read-only)"""
- return SymbolNameSpace(core.BNGetSymbolNameSpace(self.handle))
+ ns = core.BNGetSymbolNameSpace(self.handle)
+ result = NameSpace._from_core_struct(ns)
+ core.BNFreeNameSpace(ns)
+ return result
@property
def name(self):
diff --git a/settings.cpp b/settings.cpp
index b5899e38..ca1e42a3 100644
--- a/settings.cpp
+++ b/settings.cpp
@@ -17,7 +17,10 @@ int64_t Setting::GetInteger(const std::string& pluginName, const std::string& na
std::string Setting::GetString(const std::string& pluginName, const std::string& name, const std::string& defaultValue)
{
- return BNSettingGetString(pluginName.c_str(), name.c_str(), defaultValue.c_str());
+ char* str = BNSettingGetString(pluginName.c_str(), name.c_str(), defaultValue.c_str());
+ string result(str);
+ BNFreeString(str);
+ return result;
}
double Setting::GetDouble(const std::string& pluginName, const std::string& name, double defaultValue)
diff --git a/type.cpp b/type.cpp
index 8ac6ded4..e3988401 100644
--- a/type.cpp
+++ b/type.cpp
@@ -31,7 +31,8 @@ NameList::NameList(const string& join): m_join(join)
NameList::NameList(const string& name, const string& join): m_join(join)
{
- m_name.push_back(name);
+ if (!name.empty())
+ m_name.push_back(name);
}
@@ -44,6 +45,8 @@ NameList::NameList(const NameList& name, const string& join): m_join(join), m_na
{
}
+NameList::~NameList()
+{}
NameList& NameList::operator=(const string& name)
{
@@ -269,6 +272,10 @@ QualifiedName::QualifiedName(const QualifiedName& name): NameList(name.m_name, "
}
+QualifiedName::~QualifiedName()
+{}
+
+
QualifiedName& QualifiedName::operator=(const string& name)
{
m_name = vector<string>{name};
@@ -352,6 +359,10 @@ NameSpace::NameSpace(const NameSpace& name): NameList(name.m_name, "::")
}
+NameSpace::~NameSpace()
+{}
+
+
NameSpace& NameSpace::operator=(const string& name)
{
m_name = vector<string>{name};
@@ -405,6 +416,8 @@ BNNameSpace NameSpace::GetAPIObject() const
void NameSpace::FreeAPIObject(BNNameSpace* name)
{
+ if (!name)
+ return;
for (size_t i = 0; i < name->nameCount; i++)
BNFreeString(name->name[i]);
BNFreeString(name->join);
@@ -415,6 +428,8 @@ void NameSpace::FreeAPIObject(BNNameSpace* name)
NameSpace NameSpace::FromAPIObject(const BNNameSpace* name)
{
NameSpace result;
+ if (!name)
+ return result;
for (size_t i = 0; i < name->nameCount; i++)
result.push_back(name->name[i]);
return result;