summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Snyder <ryan@vector35.com>2023-07-18 12:09:21 -0400
committerRyan Snyder <ryan@vector35.com>2023-08-23 20:05:05 -0400
commitb116310afbb1940e2a13a3a2625a9069d7b489b9 (patch)
treece5db8e49d2fda5a783f5a766ed6a9cd13c61840
parent37df2e956d3c6b0008b72dfe915d36c63f694e30 (diff)
typelibrary: add methods for dependency info
-rw-r--r--binaryninjacore.h4
-rw-r--r--python/binaryview.py38
-rw-r--r--python/typelibrary.py11
-rw-r--r--typelibrary.cpp9
4 files changed, 41 insertions, 21 deletions
diff --git a/binaryninjacore.h b/binaryninjacore.h
index 2f053802..564fafdb 100644
--- a/binaryninjacore.h
+++ b/binaryninjacore.h
@@ -5237,7 +5237,6 @@ extern "C"
BINARYNINJACOREAPI BNTypeLibrary* BNDuplicateTypeLibrary(BNTypeLibrary* lib);
BINARYNINJACOREAPI BNTypeLibrary* BNLoadTypeLibraryFromFile(const char* path);
BINARYNINJACOREAPI bool BNTypeLibraryDecompressToFile(const char* file, const char* output);
- BINARYNINJACOREAPI char* BNTypeLibraryDecompressToString(const char* file);
BINARYNINJACOREAPI void BNFreeTypeLibrary(BNTypeLibrary* lib);
BINARYNINJACOREAPI BNTypeLibrary* BNLookupTypeLibraryByName(BNArchitecture* arch, const char* name);
@@ -5296,6 +5295,9 @@ extern "C"
BINARYNINJACOREAPI void BNBinaryViewExportObjectToTypeLibrary(
BNBinaryView* view, BNTypeLibrary* lib, BNQualifiedName* name, BNType* type);
+ BINARYNINJACOREAPI void BNBinaryViewSetManualDependencies(BNBinaryView* view,
+ BNQualifiedName* viewTypeNames, BNQualifiedName* libTypeNames, char** libNames, size_t count);
+
BINARYNINJACOREAPI void BNBinaryViewRecordImportedObjectLibrary(
BNBinaryView* view, BNPlatform* tgtPlatform, uint64_t tgtAddr, BNTypeLibrary* lib, BNQualifiedName* name);
BINARYNINJACOREAPI bool BNBinaryViewLookupImportedObjectLibrary(
diff --git a/python/binaryview.py b/python/binaryview.py
index 2de53d32..7d1f221b 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -29,7 +29,7 @@ import json
import inspect
import os
from typing import Callable, Generator, Optional, Union, Tuple, List, Mapping, Any, \
- Iterator, Iterable, KeysView, ItemsView, ValuesView
+ Iterator, Iterable, KeysView, ItemsView, ValuesView, Dict
from dataclasses import dataclass
from enum import IntFlag
@@ -7349,6 +7349,42 @@ class BinaryView:
raise ValueError("name can only be None if named type is derived from string passed to type_obj")
core.BNBinaryViewExportObjectToTypeLibrary(self.handle, lib.handle, _name._to_core_struct(), type_obj.handle)
+ def set_manual_type_source_override(self, entries: Mapping['_types.QualifiedName', Tuple['_types.QualifiedName', str]]):
+ """
+ This allows for fine-grained control over how types from this BinaryView are exported to a TypeLibrary
+ by `export_type_to_library` and `export_object_to_library`. Types identified by the keys of the dict
+ will NOT be exported to the destination TypeLibrary, but will instead be treated as a type that had
+ come from the string component of the value tuple. This results in the destination TypeLibrary gaining
+ a new dependency.
+
+ This is useful if a BinaryView was automatically marked up with a lot of debug information but you
+ want to export only a subset of that information into a new TypeLibrary. By creating a describing
+ which local types correspond to types in other already extant libraries, those types will be avoided
+ during the recursive export.
+
+ This data is not persisted and does not impact analysis.
+
+ BinaryView contains the following types:
+ struct RECT { ... }; // omitted
+ struct ContrivedExample { RECT rect; };
+
+ overrides = {"RECT": ("tagRECT", "winX64common")}
+ bv.set_manual_type_source_override(overrides)
+ bv.export_type_to_library(dest_new_typelib, "ContrivedExample", bv.get_type_by_name("ContrivedExample"))
+
+ Results in dest_new_typelib only having ContrivedExample added, and "RECT" being inserted as a dependency
+ to a the type "tagRECT" found in the typelibrary "winX64common"
+ """
+ count = len(entries)
+ src_names = (core.BNQualifiedName * count)()
+ dst_names = (core.BNQualifiedName * count)()
+ lib_names = (ctypes.c_char_p * count)()
+ for (i, src, (dst, lib)) in enumerate(entries.items()):
+ src_names[i] = src._to_core_struct()
+ dst_names[i] = dst._to_core_struct()
+ lib_names[i] = lib.encode("utf-8")
+ core.BNBinaryViewSetManualDependencies(self.handle, src_names, dst_names, lib_names, count)
+
def record_imported_object_library(
self, lib: typelibrary.TypeLibrary, name: str, addr: int, platform: Optional['_platform.Platform'] = None
) -> None:
diff --git a/python/typelibrary.py b/python/typelibrary.py
index eff60781..53f3e8ef 100644
--- a/python/typelibrary.py
+++ b/python/typelibrary.py
@@ -66,16 +66,6 @@ class TypeLibrary:
return core.BNTypeLibraryDecompressToFile(path, output)
@staticmethod
- def decompress(path: str) -> str:
- """
- Decompresses a type library file to a string.
-
- :param str path:
- :rtype: str
- """
- return core.BNTypeLibraryDecompressToString(path)
-
- @staticmethod
def load_from_file(path: str) -> Optional['TypeLibrary']:
"""
Loads a finalized type library instance from file
@@ -407,3 +397,4 @@ class TypeLibrary:
return result
finally:
core.BNFreeQualifiedNameAndTypeArray(named_types, count.value)
+
diff --git a/typelibrary.cpp b/typelibrary.cpp
index 5c81705f..73db3966 100644
--- a/typelibrary.cpp
+++ b/typelibrary.cpp
@@ -20,15 +20,6 @@ bool TypeLibrary::DecompressToFile(const std::string& path, const std::string& o
}
-std::string TypeLibrary::Decompress(const std::string& path)
-{
- auto str = BNTypeLibraryDecompressToString(path.c_str());
- std::string result = str;
- BNFreeString(str);
- return result;
-}
-
-
Ref<TypeLibrary> TypeLibrary::LoadFromFile(const std::string& path)
{
return new TypeLibrary(BNLoadTypeLibraryFromFile(path.c_str()));