Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions binaryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions objectivec/objc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (...)
{
Expand Down
27 changes: 27 additions & 0 deletions python/binaryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -5735,6 +5735,33 @@ def get_code_refs_for_type_fields_from(
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``.
Expand Down
14 changes: 7 additions & 7 deletions view/pe/coffview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand Down
Loading