summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2025-06-30 10:31:08 -0400
committerMark Rowe <mark@vector35.com>2025-07-03 09:10:13 -0700
commitb26a85dda211a0f1f00eecc37441e1725c5ef898 (patch)
tree0f4157d209c00b93c8ba1d9a2e93f342209b924a
parente96a7a078a9909fc1212861c9669e6af0b034e46 (diff)
Expose Add/RemoveDataReference and ensure BinaryViews use this API instead of the _user_ variant
-rw-r--r--binaryninjaapi.h15
-rw-r--r--binaryninjacore.h4
-rw-r--r--binaryview.cpp12
-rw-r--r--objectivec/objc.cpp4
-rw-r--r--python/binaryview.py27
-rw-r--r--view/pe/coffview.cpp14
6 files changed, 66 insertions, 10 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h
index 2457bf6e..edac64b8 100644
--- a/binaryninjaapi.h
+++ b/binaryninjaapi.h
@@ -5548,6 +5548,21 @@ namespace BinaryNinja {
*/
std::vector<uint64_t> GetDataReferencesFrom(uint64_t addr, uint64_t len);
+
+ /*! Add an auto Data Reference from a virtual address to another virtual address
+
+ \param fromAddr Address referencing the toAddr value
+ \param toAddr virtual address being referenced
+ */
+ void AddDataReference(uint64_t fromAddr, uint64_t toAddr);
+
+ /*! Remove an auto Data Reference from a virtual address to another virtual address
+
+ \param fromAddr Address referencing the toAddr value
+ \param toAddr virtual address being referenced
+ */
+ void RemoveDataReference(uint64_t fromAddr, uint64_t toAddr);
+
/*! Add a user Data Reference from a virtual address to another virtual address
\param fromAddr Address referencing the toAddr value
diff --git a/binaryninjacore.h b/binaryninjacore.h
index 253330c0..b65dac73 100644
--- a/binaryninjacore.h
+++ b/binaryninjacore.h
@@ -37,7 +37,7 @@
// Current ABI version for linking to the core. This is incremented any time
// there are changes to the API that affect linking, including new functions,
// new types, or modifications to existing functions or types.
-#define BN_CURRENT_CORE_ABI_VERSION 117
+#define BN_CURRENT_CORE_ABI_VERSION 118
// Minimum ABI version that is supported for loading of plugins. Plugins that
// are linked to an ABI version less than this will not be able to load and
@@ -5014,6 +5014,8 @@ extern "C"
BINARYNINJACOREAPI uint64_t* BNGetDataReferencesFrom(BNBinaryView* view, uint64_t addr, size_t* count);
BINARYNINJACOREAPI uint64_t* BNGetDataReferencesFromInRange(
BNBinaryView* view, uint64_t addr, uint64_t len, size_t* count);
+ BINARYNINJACOREAPI void BNAddDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
+ BINARYNINJACOREAPI void BNRemoveDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
BINARYNINJACOREAPI void BNAddUserDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
BINARYNINJACOREAPI void BNRemoveUserDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
BINARYNINJACOREAPI void BNFreeDataReferences(uint64_t* refs);
diff --git a/binaryview.cpp b/binaryview.cpp
index bad12407..598f666c 100644
--- a/binaryview.cpp
+++ b/binaryview.cpp
@@ -2524,6 +2524,18 @@ vector<uint64_t> BinaryView::GetDataReferencesFrom(uint64_t addr, uint64_t len)
}
+void BinaryView::AddDataReference(uint64_t fromAddr, uint64_t toAddr)
+{
+ BNAddDataReference(m_object, fromAddr, toAddr);
+}
+
+
+void BinaryView::RemoveDataReference(uint64_t fromAddr, uint64_t toAddr)
+{
+ BNRemoveDataReference(m_object, fromAddr, toAddr);
+}
+
+
void BinaryView::AddUserDataReference(uint64_t fromAddr, uint64_t toAddr)
{
BNAddUserDataReference(m_object, fromAddr, toAddr);
diff --git a/objectivec/objc.cpp b/objectivec/objc.cpp
index e37be388..4c574600 100644
--- a/objectivec/objc.cpp
+++ b/objectivec/objc.cpp
@@ -970,9 +970,9 @@ void ObjCProcessor::ReadMethodList(ObjCReader* reader, ClassBase& cls, std::stri
m_localMethods[cursor] = method;
if (selAddr)
- m_data->AddUserDataReference(selAddr, meth.imp);
+ m_data->AddDataReference(selAddr, meth.imp);
if (selRefAddr)
- m_data->AddUserDataReference(selRefAddr, meth.imp);
+ m_data->AddDataReference(selRefAddr, meth.imp);
}
catch (...)
{
diff --git a/python/binaryview.py b/python/binaryview.py
index 16c2dbfb..da2423ac 100644
--- a/python/binaryview.py
+++ b/python/binaryview.py
@@ -5735,6 +5735,33 @@ class BinaryView:
core.BNFreeTypeReferences(refs, count.value)
return result
+ def add_data_ref(self, from_addr: int, to_addr: int) -> None:
+ """
+ ``add_data_ref`` adds an auto data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.
+
+ :param int from_addr: the reference's source virtual address.
+ :param int to_addr: the reference's destination virtual address.
+ :rtype: None
+
+ .. note:: It is intended to be used from within workflows or binary view initialization.
+ """
+ core.BNAddUserDataReference(self.handle, from_addr, to_addr)
+
+ def remove_data_ref(self, from_addr: int, to_addr: int) -> None:
+ """
+ ``remove_data_ref`` removes an auto data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.
+ This function will only remove ones generated during autoanalysis.
+ If the reference does not exist, no action is performed.
+
+ :param int from_addr: the reference's source virtual address.
+ :param int to_addr: the reference's destination virtual address.
+ :rtype: None
+
+ .. note:: It is intended to be used from within workflows or other reoccurring analysis tasks. Removed \
+ references will be re-created whenever auto analysis is re-run for the
+ """
+ core.BNRemoveDataReference(self.handle, from_addr, to_addr)
+
def add_user_data_ref(self, from_addr: int, to_addr: int) -> None:
"""
``add_user_data_ref`` adds a user-specified data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.
diff --git a/view/pe/coffview.cpp b/view/pe/coffview.cpp
index 6fb216e1..e009b91d 100644
--- a/view/pe/coffview.cpp
+++ b/view/pe/coffview.cpp
@@ -1023,14 +1023,14 @@ bool COFFView::Init()
DefineDataVariable(m_imageBase + stringTableBase + e_offset, Type::ArrayType(Type::IntegerType(1, true, "char"), symbolName.length() + 1));
string symbolStringName = "__symbol_name(" + symbolName + ")";
DefineAutoSymbol(new Symbol(DataSymbol, symbolStringName, m_imageBase + stringTableBase + e_offset, NoBinding));
- DEBUG_COFF(AddUserDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + stringTableBase + e_offset));
+ DEBUG_COFF(AddDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + stringTableBase + e_offset));
}
if (e_sclass == IMAGE_SYM_CLASS_STATIC && e_value == 0)
{
size_t sectionHeaderOffset = sectionHeadersOffset + (e_scnum - 1) * sizeof(COFFSectionHeader);
(void)sectionHeaderOffset;
- DEBUG_COFF(AddUserDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + sectionHeaderOffset));
+ DEBUG_COFF(AddDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + sectionHeaderOffset));
}
else if (e_sclass == IMAGE_SYM_CLASS_EXTERNAL && e_value == 0 && e_scnum == IMAGE_SYM_UNDEFINED)
{
@@ -1251,11 +1251,11 @@ bool COFFView::Init()
DEBUG_COFF(m_logger->LogDebug("COFF: section %d reloc %d at: 0x%" PRIx32 " va: 0x%x, index: %d, type: 0x%hx, item at: 0x%x",
i, j, relocationOffset, virtualAddress, symbolTableIndex, relocType, itemAddress));
- DEBUG_COFF(AddUserDataReference(m_imageBase + relocationOffset, m_imageBase + itemAddress));
+ DEBUG_COFF(AddDataReference(m_imageBase + relocationOffset, m_imageBase + itemAddress));
uint64_t symbolOffset = symbolTableAdjustedOffset + symbolTableIndex * sizeofCOFFSymbol;
- DEBUG_COFF(AddUserDataReference(m_imageBase + relocationOffset, m_imageBase + symbolOffset));
+ DEBUG_COFF(AddDataReference(m_imageBase + relocationOffset, m_imageBase + symbolOffset));
const auto symbol = GetSymbolByAddress(m_imageBase + symbolOffset);
if (!symbol)
@@ -1280,7 +1280,7 @@ bool COFFView::Init()
coffSymbol.type = reader.Read16();
coffSymbol.storageClass = reader.Read8();
- DEBUG_COFF(AddUserDataReference(m_imageBase + itemAddress, m_imageBase + symbolOffset));
+ DEBUG_COFF(AddDataReference(m_imageBase + itemAddress, m_imageBase + symbolOffset));
DEBUG_COFF(m_logger->LogDebug("COFF: CREATING RELOC SYMBOL REF from 0x%" PRIx64 " to 0x%" PRIx64 " for \"%s\"", m_imageBase + itemAddress, m_imageBase + symbolOffset, symbolName.c_str()));
DefineAutoSymbol(new Symbol(DataSymbol, "__reloc(" + symbolName + ")", m_imageBase + relocationOffset));
@@ -1313,11 +1313,11 @@ bool COFFView::Init()
uint64_t relocTargetOffset = m_sections[reloc.sectionIndex].virtualAddress + coffSymbol.value;
DEBUG_COFF(m_logger->LogError("COFF: CREATING RELOC (%d) REF from 0x%" PRIx64 " to 0x%" PRIx64 " for %s", relocType, m_imageBase + itemAddress, m_imageBase + relocTargetOffset, symbolName.c_str()));
- DEBUG_COFF(AddUserDataReference(m_imageBase + itemAddress, m_imageBase + relocTargetOffset));
+ DEBUG_COFF(AddDataReference(m_imageBase + itemAddress, m_imageBase + relocTargetOffset));
DefineRelocation(m_arch, reloc, m_imageBase + relocTargetOffset, m_imageBase + reloc.address);
- DEBUG_COFF(AddUserDataReference(m_imageBase + relocTargetOffset, m_imageBase + itemAddress));
+ DEBUG_COFF(AddDataReference(m_imageBase + relocTargetOffset, m_imageBase + itemAddress));
DEBUG_COFF(m_logger->LogError("COFF: DEFINED RELOCATION for 0x%" PRIx64 ":0x%" PRIx64 " to 0x%" PRIx64 " reloc type %#04x", reloc.base, reloc.address, m_imageBase + relocTargetOffset, reloc.nativeType));
}
else if (coffSymbol.storageClass == IMAGE_SYM_CLASS_EXTERNAL)