summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Wagner <rusty@vector35.com>2015-08-21 22:04:49 -0400
committerRusty Wagner <rusty@vector35.com>2015-08-21 22:04:49 -0400
commit93e62cfbf1d5df345020b8b28cc514301dd26064 (patch)
tree81cef7e326a354a0246c49c7ac4b514df8a340b8
parent00369dcd62ea48a9174a6923498f4375968a94c3 (diff)
Adding type parser
-rw-r--r--binaryninjaapi.h19
-rw-r--r--type.cpp80
2 files changed, 92 insertions, 7 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h
index a2779e70..1e5fc879 100644
--- a/binaryninjaapi.h
+++ b/binaryninjaapi.h
@@ -143,6 +143,9 @@ namespace BinaryNinja
virtual BNLogLevel GetLogLevel() { return WarningLog; }
};
+ class Architecture;
+ class Type;
+
void Log(BNLogLevel level, const char* fmt, ...);
void LogDebug(const char* fmt, ...);
void LogInfo(const char* fmt, ...);
@@ -161,6 +164,14 @@ namespace BinaryNinja
bool PreprocessSource(const std::string& source, const std::string& fileName,
std::string& output, std::string& errors,
const std::vector<std::string>& includeDirs = std::vector<std::string>());
+ bool ParseTypesFromSource(Architecture* arch, const std::string& source, const std::string& fileName,
+ std::map<std::string, Ref<Type>>& types, std::map<std::string, Ref<Type>>& variables,
+ std::map<std::string, Ref<Type>>& functions, std::string& errors,
+ const std::vector<std::string>& includeDirs = std::vector<std::string>());
+ bool ParseTypesFromSourceFile(Architecture* arch, const std::string& fileName,
+ std::map<std::string, Ref<Type>>& types, std::map<std::string, Ref<Type>>& variables,
+ std::map<std::string, Ref<Type>>& functions, std::string& errors,
+ const std::vector<std::string>& includeDirs = std::vector<std::string>());
class DataBuffer
{
@@ -388,7 +399,6 @@ namespace BinaryNinja
virtual size_t Write(uint64_t offset, const void* src, size_t len) override;
};
- class Architecture;
class Function;
class BasicBlock;
@@ -594,8 +604,6 @@ namespace BinaryNinja
BinaryData(FileMetadata* file, FileAccessor* accessor);
};
- class Architecture;
-
class BinaryViewType: public RefCountObject
{
protected:
@@ -940,7 +948,6 @@ namespace BinaryNinja
virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value) override;
};
- class Type;
class Structure;
class Enumeration;
@@ -969,6 +976,7 @@ namespace BinaryNinja
Ref<Type> GetChildType() const;
std::vector<NameAndType> GetParameters() const;
bool HasVariableArguments() const;
+ bool CanReturn() const;
Ref<Structure> GetStructure() const;
Ref<Enumeration> GetEnumeration() const;
uint64_t GetElementCount() const;
@@ -985,7 +993,8 @@ namespace BinaryNinja
static Ref<Type> EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0);
static Ref<Type> PointerType(Architecture* arch, Type* type, bool cnst = false);
static Ref<Type> ArrayType(Type* type, uint64_t elem);
- static Ref<Type> FunctionType(Type* returnValue, const std::vector<NameAndType>& params, bool varArg = false);
+ static Ref<Type> FunctionType(Type* returnValue, BNCallingConvention callingConvention,
+ const std::vector<NameAndType>& params, bool varArg = false);
};
struct StructureMember
diff --git a/type.cpp b/type.cpp
index 1a0bec58..d1322c1b 100644
--- a/type.cpp
+++ b/type.cpp
@@ -85,6 +85,12 @@ bool Type::HasVariableArguments() const
}
+bool Type::CanReturn() const
+{
+ return BNFunctionTypeCanReturn(m_type);
+}
+
+
Ref<Structure> Type::GetStructure() const
{
BNStructure* s = BNGetTypeStructure(m_type);
@@ -184,7 +190,8 @@ Ref<Type> Type::ArrayType(Type* type, uint64_t elem)
}
-Ref<Type> Type::FunctionType(Type* returnValue, const std::vector<NameAndType>& params, bool varArg)
+Ref<Type> Type::FunctionType(Type* returnValue, BNCallingConvention callingConvention,
+ const std::vector<NameAndType>& params, bool varArg)
{
BNNameAndType* paramArray = new BNNameAndType[params.size()];
for (size_t i = 0; i < params.size(); i++)
@@ -193,7 +200,8 @@ Ref<Type> Type::FunctionType(Type* returnValue, const std::vector<NameAndType>&
paramArray[i].type = params[i].type->GetTypeObject();
}
- Type* type = new Type(BNCreateFunctionType(returnValue->GetTypeObject(), paramArray, params.size(), varArg));
+ Type* type = new Type(BNCreateFunctionType(returnValue->GetTypeObject(), callingConvention,
+ paramArray, params.size(), varArg));
delete[] paramArray;
return type;
}
@@ -378,3 +386,71 @@ bool BinaryNinja::PreprocessSource(const string& source, const string& fileName,
delete[] includeDirList;
return result;
}
+
+
+bool BinaryNinja::ParseTypesFromSource(Architecture* arch, const string& source, const string& fileName,
+ map<string, Ref<Type>>& types, map<string, Ref<Type>>& variables,
+ map<string, Ref<Type>>& functions, string& errors,
+ const vector<string>& includeDirs)
+{
+ BNTypeParserResult result;
+ char* errorStr;
+ const char** includeDirList = new const char*[includeDirs.size()];
+
+ for (size_t i = 0; i < includeDirs.size(); i++)
+ includeDirList[i] = includeDirs[i].c_str();
+
+ types.clear();
+ variables.clear();
+ functions.clear();
+
+ bool ok = BNParseTypesFromSource(arch->GetArchitectureObject(), source.c_str(), fileName.c_str(), &result,
+ &errorStr, includeDirList, includeDirs.size());
+ errors = errorStr;
+ BNFreeString(errorStr);
+ if (!ok)
+ return false;
+
+ for (size_t i = 0; i < result.typeCount; i++)
+ types[result.types[i].name] = new Type(BNNewTypeReference(result.types[i].type));
+ for (size_t i = 0; i < result.variableCount; i++)
+ types[result.variables[i].name] = new Type(BNNewTypeReference(result.variables[i].type));
+ for (size_t i = 0; i < result.functionCount; i++)
+ types[result.functions[i].name] = new Type(BNNewTypeReference(result.functions[i].type));
+ BNFreeTypeParserResult(&result);
+ return true;
+}
+
+
+bool BinaryNinja::ParseTypesFromSourceFile(Architecture* arch, const string& fileName,
+ map<string, Ref<Type>>& types, map<string, Ref<Type>>& variables,
+ map<string, Ref<Type>>& functions, string& errors,
+ const vector<string>& includeDirs)
+{
+ BNTypeParserResult result;
+ char* errorStr;
+ const char** includeDirList = new const char*[includeDirs.size()];
+
+ for (size_t i = 0; i < includeDirs.size(); i++)
+ includeDirList[i] = includeDirs[i].c_str();
+
+ types.clear();
+ variables.clear();
+ functions.clear();
+
+ bool ok = BNParseTypesFromSourceFile(arch->GetArchitectureObject(), fileName.c_str(), &result,
+ &errorStr, includeDirList, includeDirs.size());
+ errors = errorStr;
+ BNFreeString(errorStr);
+ if (!ok)
+ return false;
+
+ for (size_t i = 0; i < result.typeCount; i++)
+ types[result.types[i].name] = new Type(BNNewTypeReference(result.types[i].type));
+ for (size_t i = 0; i < result.variableCount; i++)
+ variables[result.variables[i].name] = new Type(BNNewTypeReference(result.variables[i].type));
+ for (size_t i = 0; i < result.functionCount; i++)
+ functions[result.functions[i].name] = new Type(BNNewTypeReference(result.functions[i].type));
+ BNFreeTypeParserResult(&result);
+ return true;
+}