From 7c9ada78241e08bf36bd04d989f742a6de721575 Mon Sep 17 00:00:00 2001 From: Xusheng Date: Wed, 5 May 2021 17:23:49 +0800 Subject: Add the ability to automatically create struct members --- binaryview.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 118 insertions(+), 7 deletions(-) (limited to 'binaryview.cpp') diff --git a/binaryview.cpp b/binaryview.cpp index f37b60ff..e57b6bf4 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1917,25 +1917,26 @@ vector BinaryView::GetTypeReferencesForType(const Qualified } -vector BinaryView::GetCodeReferencesForTypeField(const QualifiedName& type, uint64_t offset) +vector BinaryView::GetCodeReferencesForTypeField(const QualifiedName& type, uint64_t offset) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); - BNReferenceSource* refs = BNGetCodeReferencesForTypeField(m_object, &nameObj, offset, &count); + BNTypeFieldReference* refs = BNGetCodeReferencesForTypeField(m_object, &nameObj, offset, &count); QualifiedName::FreeAPIObject(&nameObj); - vector result; + vector result; result.reserve(count); for (size_t i = 0; i < count; i++) { - ReferenceSource src; + TypeFieldReference src; src.func = new Function(BNNewFunctionReference(refs[i].func)); src.arch = new CoreArchitecture(refs[i].arch); src.addr = refs[i].addr; + src.size = refs[i].size; result.push_back(src); } - BNFreeCodeReferences(refs, count); + BNFreeTypeFieldReferences(refs, count); return result; } @@ -2063,11 +2064,11 @@ vector BinaryView::GetCodeReferencesForTypeFieldFrom(Refere } -vector BinaryView::GetAllFieldsReferencedByCode(const QualifiedName& type) +vector BinaryView::GetAllFieldsReferenced(const QualifiedName& type) { size_t count; BNQualifiedName nameObj = type.GetAPIObject(); - uint64_t* fields = BNGetAllFieldsReferencedByCode(m_object, &nameObj, &count); + uint64_t* fields = BNGetAllFieldsReferenced(m_object, &nameObj, &count); vector result(fields, &fields[count]); // Data refs and the fields above are both an array of uint64_t, so they can be freed in @@ -2077,6 +2078,93 @@ vector BinaryView::GetAllFieldsReferencedByCode(const QualifiedName& t } +std::map> BinaryView::GetAllSizesReferenced( + const QualifiedName& type) +{ + size_t count; + BNQualifiedName nameObj = type.GetAPIObject(); + BNTypeFieldReferenceSizeInfo* fields = BNGetAllSizesReferenced(m_object, + &nameObj, &count); + + std::map> result; + for (size_t i = 0; i < count; i++) + { + auto& sizes = result[fields[i].offset]; + for (size_t j = 0; j < fields[i].count; j++) + { + sizes.push_back(fields[i].sizes[j]); + } + } + + BNFreeTypeFieldReferenceSizeInfo(fields, count); + return result; +} + + +std::map>>> + BinaryView::GetAllTypesReferenced(const QualifiedName& type) +{ + size_t count; + BNQualifiedName nameObj = type.GetAPIObject(); + BNTypeFieldReferenceTypeInfo* fields = BNGetAllTypesReferenced(m_object, + &nameObj, &count); + + std::map>>> result; + for (size_t i = 0; i < count; i++) + { + auto& types = result[fields[i].offset]; + for (size_t j = 0; j < fields[i].count; j++) + { + BNTypeWithConfidence tc = fields[i].types[j]; + Ref type = tc.type ? new Type(tc.type) : nullptr; + types.push_back(Confidence>(type, tc.confidence)); + } + } + + BNFreeTypeFieldReferenceTypeInfo(fields, count); + return result; +} + + +std::vector BinaryView::GetSizesReferenced(const QualifiedName& type, + uint64_t offset) +{ + size_t count; + BNQualifiedName nameObj = type.GetAPIObject(); + size_t* refs = BNGetSizesReferenced(m_object, &nameObj, offset, &count); + + std::vector result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result[i] = refs[i]; + + BNFreeTypeFieldReferenceSizes(refs, count); + return result; +} + + +std::vector>> BinaryView::GetTypesReferenced( + const QualifiedName& type, uint64_t offset) +{ + size_t count; + BNQualifiedName nameObj = type.GetAPIObject(); + BNTypeWithConfidence* types = BNGetTypesReferenced(m_object, &nameObj, offset, + &count); + + std::vector>> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + BNTypeWithConfidence tc = types[i]; + Ref type = tc.type ? new Type(tc.type) : nullptr; + result.push_back(Confidence>(type, tc.confidence)); + } + + BNFreeTypeFieldReferenceTypes(types, count); + return result; +} + + vector BinaryView::GetCallees(ReferenceSource callSite) { size_t count; @@ -3465,6 +3553,29 @@ bool BinaryView::ParseExpression(Ref view, const string& expression, } +Ref BinaryView::CreateStructureFromOffsetAccess(const QualifiedName& type, + bool* newMemberAdded) const +{ + BNQualifiedName typeObj = type.GetAPIObject(); + BNStructure* result = BNCreateStructureFromOffsetAccess(m_object, &typeObj, + newMemberAdded); + return new Structure(result); +} + + +Confidence> BinaryView::CreateStructureMemberFromAccess( + const QualifiedName& name, uint64_t offset) const +{ + BNQualifiedName typeObj = name.GetAPIObject(); + BNTypeWithConfidence type = BNCreateStructureMemberFromAccess(m_object, &typeObj, + offset); + + if (type.type) + return Confidence>(new Type(type.type), type.confidence); + return nullptr; +} + + Relocation::Relocation(BNRelocation* reloc) { m_object = reloc; -- cgit v1.3.1