From 93e62cfbf1d5df345020b8b28cc514301dd26064 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 21 Aug 2015 22:04:49 -0400 Subject: Adding type parser --- binaryninjaapi.h | 19 ++++++++++---- type.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 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& includeDirs = std::vector()); + bool ParseTypesFromSource(Architecture* arch, const std::string& source, const std::string& fileName, + std::map>& types, std::map>& variables, + std::map>& functions, std::string& errors, + const std::vector& includeDirs = std::vector()); + bool ParseTypesFromSourceFile(Architecture* arch, const std::string& fileName, + std::map>& types, std::map>& variables, + std::map>& functions, std::string& errors, + const std::vector& includeDirs = std::vector()); 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 GetChildType() const; std::vector GetParameters() const; bool HasVariableArguments() const; + bool CanReturn() const; Ref GetStructure() const; Ref GetEnumeration() const; uint64_t GetElementCount() const; @@ -985,7 +993,8 @@ namespace BinaryNinja static Ref EnumerationType(Architecture* arch, Enumeration* enm, size_t width = 0); static Ref PointerType(Architecture* arch, Type* type, bool cnst = false); static Ref ArrayType(Type* type, uint64_t elem); - static Ref FunctionType(Type* returnValue, const std::vector& params, bool varArg = false); + static Ref FunctionType(Type* returnValue, BNCallingConvention callingConvention, + const std::vector& 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 Type::GetStructure() const { BNStructure* s = BNGetTypeStructure(m_type); @@ -184,7 +190,8 @@ Ref Type::ArrayType(Type* type, uint64_t elem) } -Ref Type::FunctionType(Type* returnValue, const std::vector& params, bool varArg) +Ref Type::FunctionType(Type* returnValue, BNCallingConvention callingConvention, + const std::vector& params, bool varArg) { BNNameAndType* paramArray = new BNNameAndType[params.size()]; for (size_t i = 0; i < params.size(); i++) @@ -193,7 +200,8 @@ Ref Type::FunctionType(Type* returnValue, const std::vector& 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>& types, map>& variables, + map>& functions, string& errors, + const vector& 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>& types, map>& variables, + map>& functions, string& errors, + const vector& 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; +} -- cgit v1.3.1