Skip to content

Commit b26a85d

Browse files
plafossebdash
authored andcommitted
Expose Add/RemoveDataReference and ensure BinaryViews use this API instead of the _user_ variant
1 parent e96a7a0 commit b26a85d

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

binaryninjaapi.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,6 +5548,21 @@ namespace BinaryNinja {
55485548
*/
55495549
std::vector<uint64_t> GetDataReferencesFrom(uint64_t addr, uint64_t len);
55505550

5551+
5552+
/*! Add an auto Data Reference from a virtual address to another virtual address
5553+
5554+
\param fromAddr Address referencing the toAddr value
5555+
\param toAddr virtual address being referenced
5556+
*/
5557+
void AddDataReference(uint64_t fromAddr, uint64_t toAddr);
5558+
5559+
/*! Remove an auto Data Reference from a virtual address to another virtual address
5560+
5561+
\param fromAddr Address referencing the toAddr value
5562+
\param toAddr virtual address being referenced
5563+
*/
5564+
void RemoveDataReference(uint64_t fromAddr, uint64_t toAddr);
5565+
55515566
/*! Add a user Data Reference from a virtual address to another virtual address
55525567

55535568
\param fromAddr Address referencing the toAddr value

binaryninjacore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 117
40+
#define BN_CURRENT_CORE_ABI_VERSION 118
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -5014,6 +5014,8 @@ extern "C"
50145014
BINARYNINJACOREAPI uint64_t* BNGetDataReferencesFrom(BNBinaryView* view, uint64_t addr, size_t* count);
50155015
BINARYNINJACOREAPI uint64_t* BNGetDataReferencesFromInRange(
50165016
BNBinaryView* view, uint64_t addr, uint64_t len, size_t* count);
5017+
BINARYNINJACOREAPI void BNAddDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
5018+
BINARYNINJACOREAPI void BNRemoveDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
50175019
BINARYNINJACOREAPI void BNAddUserDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
50185020
BINARYNINJACOREAPI void BNRemoveUserDataReference(BNBinaryView* view, uint64_t fromAddr, uint64_t toAddr);
50195021
BINARYNINJACOREAPI void BNFreeDataReferences(uint64_t* refs);

binaryview.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,18 @@ vector<uint64_t> BinaryView::GetDataReferencesFrom(uint64_t addr, uint64_t len)
25242524
}
25252525

25262526

2527+
void BinaryView::AddDataReference(uint64_t fromAddr, uint64_t toAddr)
2528+
{
2529+
BNAddDataReference(m_object, fromAddr, toAddr);
2530+
}
2531+
2532+
2533+
void BinaryView::RemoveDataReference(uint64_t fromAddr, uint64_t toAddr)
2534+
{
2535+
BNRemoveDataReference(m_object, fromAddr, toAddr);
2536+
}
2537+
2538+
25272539
void BinaryView::AddUserDataReference(uint64_t fromAddr, uint64_t toAddr)
25282540
{
25292541
BNAddUserDataReference(m_object, fromAddr, toAddr);

objectivec/objc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,9 +970,9 @@ void ObjCProcessor::ReadMethodList(ObjCReader* reader, ClassBase& cls, std::stri
970970
m_localMethods[cursor] = method;
971971

972972
if (selAddr)
973-
m_data->AddUserDataReference(selAddr, meth.imp);
973+
m_data->AddDataReference(selAddr, meth.imp);
974974
if (selRefAddr)
975-
m_data->AddUserDataReference(selRefAddr, meth.imp);
975+
m_data->AddDataReference(selRefAddr, meth.imp);
976976
}
977977
catch (...)
978978
{

python/binaryview.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,6 +5735,33 @@ def get_code_refs_for_type_fields_from(
57355735
core.BNFreeTypeReferences(refs, count.value)
57365736
return result
57375737

5738+
def add_data_ref(self, from_addr: int, to_addr: int) -> None:
5739+
"""
5740+
``add_data_ref`` adds an auto data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.
5741+
5742+
:param int from_addr: the reference's source virtual address.
5743+
:param int to_addr: the reference's destination virtual address.
5744+
:rtype: None
5745+
5746+
.. note:: It is intended to be used from within workflows or binary view initialization.
5747+
"""
5748+
core.BNAddUserDataReference(self.handle, from_addr, to_addr)
5749+
5750+
def remove_data_ref(self, from_addr: int, to_addr: int) -> None:
5751+
"""
5752+
``remove_data_ref`` removes an auto data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.
5753+
This function will only remove ones generated during autoanalysis.
5754+
If the reference does not exist, no action is performed.
5755+
5756+
:param int from_addr: the reference's source virtual address.
5757+
:param int to_addr: the reference's destination virtual address.
5758+
:rtype: None
5759+
5760+
.. note:: It is intended to be used from within workflows or other reoccurring analysis tasks. Removed \
5761+
references will be re-created whenever auto analysis is re-run for the
5762+
"""
5763+
core.BNRemoveDataReference(self.handle, from_addr, to_addr)
5764+
57385765
def add_user_data_ref(self, from_addr: int, to_addr: int) -> None:
57395766
"""
57405767
``add_user_data_ref`` adds a user-specified data cross-reference (xref) from the address ``from_addr`` to the address ``to_addr``.

view/pe/coffview.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,14 +1023,14 @@ bool COFFView::Init()
10231023
DefineDataVariable(m_imageBase + stringTableBase + e_offset, Type::ArrayType(Type::IntegerType(1, true, "char"), symbolName.length() + 1));
10241024
string symbolStringName = "__symbol_name(" + symbolName + ")";
10251025
DefineAutoSymbol(new Symbol(DataSymbol, symbolStringName, m_imageBase + stringTableBase + e_offset, NoBinding));
1026-
DEBUG_COFF(AddUserDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + stringTableBase + e_offset));
1026+
DEBUG_COFF(AddDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + stringTableBase + e_offset));
10271027
}
10281028

10291029
if (e_sclass == IMAGE_SYM_CLASS_STATIC && e_value == 0)
10301030
{
10311031
size_t sectionHeaderOffset = sectionHeadersOffset + (e_scnum - 1) * sizeof(COFFSectionHeader);
10321032
(void)sectionHeaderOffset;
1033-
DEBUG_COFF(AddUserDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + sectionHeaderOffset));
1033+
DEBUG_COFF(AddDataReference(m_imageBase + symbolVirtualAddress, m_imageBase + sectionHeaderOffset));
10341034
}
10351035
else if (e_sclass == IMAGE_SYM_CLASS_EXTERNAL && e_value == 0 && e_scnum == IMAGE_SYM_UNDEFINED)
10361036
{
@@ -1251,11 +1251,11 @@ bool COFFView::Init()
12511251
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",
12521252
i, j, relocationOffset, virtualAddress, symbolTableIndex, relocType, itemAddress));
12531253

1254-
DEBUG_COFF(AddUserDataReference(m_imageBase + relocationOffset, m_imageBase + itemAddress));
1254+
DEBUG_COFF(AddDataReference(m_imageBase + relocationOffset, m_imageBase + itemAddress));
12551255

12561256
uint64_t symbolOffset = symbolTableAdjustedOffset + symbolTableIndex * sizeofCOFFSymbol;
12571257

1258-
DEBUG_COFF(AddUserDataReference(m_imageBase + relocationOffset, m_imageBase + symbolOffset));
1258+
DEBUG_COFF(AddDataReference(m_imageBase + relocationOffset, m_imageBase + symbolOffset));
12591259

12601260
const auto symbol = GetSymbolByAddress(m_imageBase + symbolOffset);
12611261
if (!symbol)
@@ -1280,7 +1280,7 @@ bool COFFView::Init()
12801280
coffSymbol.type = reader.Read16();
12811281
coffSymbol.storageClass = reader.Read8();
12821282

1283-
DEBUG_COFF(AddUserDataReference(m_imageBase + itemAddress, m_imageBase + symbolOffset));
1283+
DEBUG_COFF(AddDataReference(m_imageBase + itemAddress, m_imageBase + symbolOffset));
12841284
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()));
12851285

12861286
DefineAutoSymbol(new Symbol(DataSymbol, "__reloc(" + symbolName + ")", m_imageBase + relocationOffset));
@@ -1313,11 +1313,11 @@ bool COFFView::Init()
13131313
uint64_t relocTargetOffset = m_sections[reloc.sectionIndex].virtualAddress + coffSymbol.value;
13141314

13151315
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()));
1316-
DEBUG_COFF(AddUserDataReference(m_imageBase + itemAddress, m_imageBase + relocTargetOffset));
1316+
DEBUG_COFF(AddDataReference(m_imageBase + itemAddress, m_imageBase + relocTargetOffset));
13171317

13181318
DefineRelocation(m_arch, reloc, m_imageBase + relocTargetOffset, m_imageBase + reloc.address);
13191319

1320-
DEBUG_COFF(AddUserDataReference(m_imageBase + relocTargetOffset, m_imageBase + itemAddress));
1320+
DEBUG_COFF(AddDataReference(m_imageBase + relocTargetOffset, m_imageBase + itemAddress));
13211321
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));
13221322
}
13231323
else if (coffSymbol.storageClass == IMAGE_SYM_CLASS_EXTERNAL)

0 commit comments

Comments
 (0)