Skip to content

Commit

Permalink
MemoryMap update with new segment model.
Browse files Browse the repository at this point in the history
  • Loading branch information
bpotchik committed Jul 19, 2024
1 parent 4360736 commit 5fc0803
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 235 deletions.
105 changes: 76 additions & 29 deletions binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -4218,11 +4218,6 @@ namespace BinaryNinja {
uint32_t GetFlags() const;
bool IsAutoDefined() const;

std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const;
std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const;
std::vector<Ref<Relocation>> GetRelocationsInRange(uint64_t addr, uint64_t size) const;
uint64_t GetRelocationsCount() const;

void SetLength(uint64_t length);
void SetDataOffset(uint64_t dataOffset);
void SetDataLength(uint64_t dataLength);
Expand Down Expand Up @@ -4823,7 +4818,29 @@ namespace BinaryNinja {
*/
uint64_t GetNextValidOffset(uint64_t offset) const;

/*! GetImageBase queries for the image base in the BinaryView

\return the image base of the BinaryView
*/
uint64_t GetImageBase() const;

/*! GetOriginalImageBase queries for the original image base in the BinaryView, unaffected by any rebasing operations

\return the original image base of the BinaryView
*/
uint64_t GetOriginalImageBase() const;

/*! SetOriginalBase sets the original image base in the BinaryView, unaffected by any rebasing operations.
* This is only intended to be used by Binary View implementations to provide this value. Regular users should
* NOT change this value.

\param imageBase the original image base of the binary view
*/
void SetOriginalImageBase(uint64_t imageBase);


/*! GetOriginalBase queries for the original image base in the BinaryView, unaffected by any rebasing operations
\deprecated This API has been deprecated in favor of GetOriginalImageBase in 4.0.xxxx

\return the original image base of the BinaryView
*/
Expand All @@ -4832,6 +4849,7 @@ namespace BinaryNinja {
/*! SetOriginalBase sets the original image base in the BinaryView, unaffected by any rebasing operations.
* This is only intended to be used by Binary View implementations to provide this value. Regular users should
* NOT change this value.
\deprecated This API has been deprecated in favor of SetOriginalImageBase in 4.0.xxxx

\param base the original image base of the binary view
*/
Expand Down Expand Up @@ -4926,6 +4944,7 @@ namespace BinaryNinja {
void DefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> target, uint64_t reloc);
std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRanges() const;
std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesAtAddress(uint64_t addr) const;
std::vector<std::pair<uint64_t, uint64_t>> GetRelocationRangesInRange(uint64_t addr, size_t size) const;
bool RangeContainsRelocation(uint64_t addr, size_t size) const;
std::vector<Ref<Relocation>> GetRelocationsAt(uint64_t addr) const;

Expand Down Expand Up @@ -6499,6 +6518,25 @@ namespace BinaryNinja {
*/
std::vector<std::string> GetUniqueSectionNames(const std::vector<std::string>& names);

/*! Get the list of allocated ranges
\deprecated This API has been deprecated in favor of GetMappedAddressRanges in 4.0.xxxx

\return The list of allocated ranges
*/
std::vector<BNAddressRange> GetAllocatedRanges();

/*! Get the list of ranges mapped into the address space

\return The list of mapped ranges
*/
std::vector<BNAddressRange> GetMappedAddressRanges();

/*! Get the list of ranges that are mapped into the address space and are backed by a target object

\return The list of backed ranges
*/
std::vector<BNAddressRange> GetBackedAddressRanges();

/*! Get the comment placed at an address

\param addr Address at which to check for a comment
Expand All @@ -6519,12 +6557,6 @@ namespace BinaryNinja {
*/
void SetCommentForAddress(uint64_t addr, const std::string& comment);

/*! Get the list of allocated ranges

\return The list of allocated ranges
*/
std::vector<BNAddressRange> GetAllocatedRanges();

void StoreMetadata(const std::string& key, Ref<Metadata> value, bool isAuto = false);
Ref<Metadata> QueryMetadata(const std::string& key);
void RemoveMetadata(const std::string& key);
Expand Down Expand Up @@ -6704,39 +6736,57 @@ namespace BinaryNinja {
MemoryMap(BNBinaryView* view): m_object(view) {}
~MemoryMap() = default;

bool AddBinaryMemoryRegion(const std::string& name, uint64_t start, Ref<BinaryView> source)
bool AddBinaryMemoryRegion(const std::string& name, uint64_t start, Ref<BinaryView> source, uint32_t flags = 0)
{
return BNAddBinaryMemoryRegion(m_object, name.c_str(), start, source->GetObject());
return BNAddBinaryMemoryRegion(m_object, name.c_str(), start, source->GetObject(), flags);
}

bool AddDataMemoryRegion(const std::string& name, uint64_t start, const DataBuffer& source)
bool AddDataMemoryRegion(const std::string& name, uint64_t start, const DataBuffer& source, uint32_t flags = 0)
{
return BNAddDataMemoryRegion(m_object, name.c_str(), start, source.GetBufferObject());
return BNAddDataMemoryRegion(m_object, name.c_str(), start, source.GetBufferObject(), flags);
}

bool AddRemoteMemoryRegion(const std::string& name, uint64_t start, FileAccessor* source)
bool AddRemoteMemoryRegion(const std::string& name, uint64_t start, FileAccessor* source, uint32_t flags = 0)
{
return BNAddRemoteMemoryRegion(m_object, name.c_str(), start, source->GetCallbacks());
return BNAddRemoteMemoryRegion(m_object, name.c_str(), start, source->GetCallbacks(), flags);
}

bool RemoveMemoryRegion(const std::string& name)
{
return BNRemoveMemoryRegion(m_object, name.c_str());
}

bool IsMemoryRegionEnabled(const std::string& name, uint64_t start)
std::string GetActiveMemoryRegionAt(uint64_t addr)
{
char* name = BNGetActiveMemoryRegionAt(m_object, addr);
std::string result = name;
BNFreeString(name);
return result;
}

bool IsMemoryRegionEnabled(const std::string& name)
{
return BNIsMemoryRegionEnabled(m_object, name.c_str());
}

bool SetMemoryRegionEnabled(const std::string& name, bool enabled)
{
return BNSetMemoryRegionEnabled(m_object, name.c_str(), enabled);
}

bool IsMemoryRegionRebaseable(const std::string& name)
{
return BNIsMemoryRegionEnabled(m_object, name.c_str(), start);
return BNIsMemoryRegionRebaseable(m_object, name.c_str());
}

bool SetMemoryRegionEnabled(const std::string& name, uint64_t start, bool enabled)
bool SetMemoryRegionRebaseable(const std::string& name, bool rebaseable)
{
return BNSetMemoryRegionEnabled(m_object, name.c_str(), start, enabled);
return BNSetMemoryRegionRebaseable(m_object, name.c_str(), rebaseable);
}

bool SetMemoryRegionFill(const std::string& name, uint64_t start, uint8_t fill)
bool SetMemoryRegionFill(const std::string& name, uint8_t fill)
{
return BNSetMemoryRegionFill(m_object, name.c_str(), start, fill);
return BNSetMemoryRegionFill(m_object, name.c_str(), fill);
}

void Reset()
Expand Down Expand Up @@ -8111,16 +8161,13 @@ namespace BinaryNinja {

// These three binary view type constant APIs are deprecated and should no longer be used. Their implementations
// have been removed, and they now have no effects.
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no
longer has any effect
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no longer has any effect
*/
bool IsBinaryViewTypeConstantDefined(const std::string& type, const std::string& name);
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no
longer has any effect
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no longer has any effect
*/
uint64_t GetBinaryViewTypeConstant(const std::string& type, const std::string& name, uint64_t defaultValue = 0);
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no
longer has any effect
/*! \deprecated This API has been deprecated. The implementation has been removed, and this function no longer has any effect
*/
void SetBinaryViewTypeConstant(const std::string& type, const std::string& name, uint64_t value);

Expand Down
59 changes: 29 additions & 30 deletions binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
// 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 66
#define BN_CURRENT_CORE_ABI_VERSION 67

// 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
// will require rebuilding. The minimum version is increased when there are
// incompatible changes that break binary compatibility, such as changes to
// existing types or functions.
#define BN_MINIMUM_CORE_ABI_VERSION 65
#define BN_MINIMUM_CORE_ABI_VERSION 67

#ifdef __GNUC__
#ifdef BINARYNINJACORE_LIBRARY
Expand Down Expand Up @@ -3771,13 +3771,16 @@ extern "C"

// Memory Map
BINARYNINJACOREAPI char* BNGetMemoryMapDescription(BNBinaryView* view);
BINARYNINJACOREAPI bool BNAddBinaryMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNBinaryView* data);
BINARYNINJACOREAPI bool BNAddDataMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNDataBuffer* data);
BINARYNINJACOREAPI bool BNAddRemoteMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNFileAccessor* accessor);
BINARYNINJACOREAPI bool BNAddBinaryMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNBinaryView* data, uint32_t flags);
BINARYNINJACOREAPI bool BNAddDataMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNDataBuffer* data, uint32_t flags);
BINARYNINJACOREAPI bool BNAddRemoteMemoryRegion(BNBinaryView* view, const char* name, uint64_t start, BNFileAccessor* accessor, uint32_t flags);
BINARYNINJACOREAPI bool BNRemoveMemoryRegion(BNBinaryView* view, const char* name);
BINARYNINJACOREAPI bool BNIsMemoryRegionEnabled(BNBinaryView* view, const char* name, uint64_t start);
BINARYNINJACOREAPI bool BNSetMemoryRegionEnabled(BNBinaryView* view, const char* name, uint64_t start, bool enable);
BINARYNINJACOREAPI bool BNSetMemoryRegionFill(BNBinaryView* view, const char* name, uint64_t start, uint8_t fill);
BINARYNINJACOREAPI char* BNGetActiveMemoryRegionAt(BNBinaryView* view, uint64_t addr);
BINARYNINJACOREAPI bool BNIsMemoryRegionEnabled(BNBinaryView* view, const char* name);
BINARYNINJACOREAPI bool BNSetMemoryRegionEnabled(BNBinaryView* view, const char* name, bool enable);
BINARYNINJACOREAPI bool BNIsMemoryRegionRebaseable(BNBinaryView* view, const char* name);
BINARYNINJACOREAPI bool BNSetMemoryRegionRebaseable(BNBinaryView* view, const char* name, bool rebaseable);
BINARYNINJACOREAPI bool BNSetMemoryRegionFill(BNBinaryView* view, const char* name, uint8_t fill);
BINARYNINJACOREAPI void BNResetMemoryMap(BNBinaryView* view);

// Binary view access
Expand Down Expand Up @@ -3817,8 +3820,10 @@ extern "C"
BINARYNINJACOREAPI bool BNIsOffsetExternSemantics(BNBinaryView* view, uint64_t offset);
BINARYNINJACOREAPI bool BNIsOffsetWritableSemantics(BNBinaryView* view, uint64_t offset);
BINARYNINJACOREAPI uint64_t BNGetNextValidOffset(BNBinaryView* view, uint64_t offset);
BINARYNINJACOREAPI uint64_t BNGetOriginalBase(BNBinaryView* view);
BINARYNINJACOREAPI void BNSetOriginalBase(BNBinaryView* view, uint64_t base);

BINARYNINJACOREAPI uint64_t BNGetImageBase(BNBinaryView* view);
BINARYNINJACOREAPI uint64_t BNGetOriginalImageBase(BNBinaryView* view);
BINARYNINJACOREAPI void BNSetOriginalImageBase(BNBinaryView* view, uint64_t imageBase);
BINARYNINJACOREAPI uint64_t BNGetStartOffset(BNBinaryView* view);
BINARYNINJACOREAPI uint64_t BNGetEndOffset(BNBinaryView* view);
BINARYNINJACOREAPI uint64_t BNGetViewLength(BNBinaryView* view);
Expand All @@ -3837,15 +3842,16 @@ extern "C"

BINARYNINJACOREAPI bool BNSaveToFile(BNBinaryView* view, BNFileAccessor* file);
BINARYNINJACOREAPI bool BNSaveToFilename(BNBinaryView* view, const char* filename);
BINARYNINJACOREAPI void BNDefineRelocation(
BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* info, uint64_t target, uint64_t reloc);
BINARYNINJACOREAPI void BNDefineSymbolRelocation(
BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* info, BNSymbol* target, uint64_t reloc);

BINARYNINJACOREAPI void BNDefineRelocation(BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* info, uint64_t target, uint64_t reloc);
BINARYNINJACOREAPI void BNDefineSymbolRelocation(BNBinaryView* view, BNArchitecture* arch, BNRelocationInfo* info, BNSymbol* target, uint64_t reloc);
BINARYNINJACOREAPI BNRange* BNGetRelocationRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI BNRange* BNGetRelocationRangesAtAddress(BNBinaryView* view, uint64_t addr, size_t* count);
BINARYNINJACOREAPI BNRange* BNGetRelocationRangesInRange(BNBinaryView* view, uint64_t addr, uint64_t size, size_t* count);
BINARYNINJACOREAPI bool BNRangeContainsRelocation(BNBinaryView* view, uint64_t addr, size_t size);
BINARYNINJACOREAPI BNRelocation** BNGetRelocationsAt(BNBinaryView* view, uint64_t addr, size_t* count);
BINARYNINJACOREAPI void BNFreeRelocationList(BNRelocation** relocations, size_t count);
BINARYNINJACOREAPI void BNFreeRelocationRanges(BNRange* ranges);

BINARYNINJACOREAPI void BNRegisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify);
BINARYNINJACOREAPI void BNUnregisterDataNotification(BNBinaryView* view, BNBinaryDataNotification* notify);
Expand Down Expand Up @@ -3903,11 +3909,9 @@ extern "C"
BINARYNINJACOREAPI bool BNSearch(BNBinaryView* view, const char* query, void* context, bool (*callback)(void*, uint64_t, BNDataBuffer*));
BINARYNINJACOREAPI bool BNPerformSearch(const char* query, const uint8_t* buffer, size_t size, bool(*callback)(void*, size_t, size_t), void* context);

BINARYNINJACOREAPI void BNAddAutoSegment(
BNBinaryView* view, uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags);
BINARYNINJACOREAPI void BNAddAutoSegment(BNBinaryView* view, uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags);
BINARYNINJACOREAPI void BNRemoveAutoSegment(BNBinaryView* view, uint64_t start, uint64_t length);
BINARYNINJACOREAPI void BNAddUserSegment(
BNBinaryView* view, uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags);
BINARYNINJACOREAPI void BNAddUserSegment(BNBinaryView* view, uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags);
BINARYNINJACOREAPI void BNRemoveUserSegment(BNBinaryView* view, uint64_t start, uint64_t length);
BINARYNINJACOREAPI BNSegment** BNGetSegments(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeSegmentList(BNSegment** segments, size_t count);
Expand Down Expand Up @@ -3936,18 +3940,19 @@ extern "C"
BINARYNINJACOREAPI BNSection** BNGetSectionsAt(BNBinaryView* view, uint64_t addr, size_t* count);
BINARYNINJACOREAPI void BNFreeSectionList(BNSection** sections, size_t count);
BINARYNINJACOREAPI BNSection* BNGetSectionByName(BNBinaryView* view, const char* name);

BINARYNINJACOREAPI char** BNGetUniqueSectionNames(BNBinaryView* view, const char** names, size_t count);

BINARYNINJACOREAPI BNAddressRange* BNGetAllocatedRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI BNAddressRange* BNGetMappedAddressRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI BNAddressRange* BNGetBackedAddressRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeAddressRanges(BNAddressRange* ranges);

BINARYNINJACOREAPI BNNameSpace* BNGetNameSpaces(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeNameSpaceList(BNNameSpace* nameSpace, size_t count);
BINARYNINJACOREAPI BNNameSpace BNGetExternalNameSpace(void);
BINARYNINJACOREAPI BNNameSpace BNGetInternalNameSpace(void);
BINARYNINJACOREAPI void BNFreeNameSpace(BNNameSpace* name);

BINARYNINJACOREAPI BNAddressRange* BNGetAllocatedRanges(BNBinaryView* view, size_t* count);
BINARYNINJACOREAPI void BNFreeAddressRanges(BNAddressRange* ranges);

BINARYNINJACOREAPI BNRegisterValueWithConfidence BNGetGlobalPointerValue(BNBinaryView* view);

// Raw binary data view
Expand Down Expand Up @@ -6985,17 +6990,11 @@ extern "C"
BINARYNINJACOREAPI uint64_t BNRelocationGetTarget(BNRelocation* reloc);
BINARYNINJACOREAPI uint64_t BNRelocationGetReloc(BNRelocation* reloc);
BINARYNINJACOREAPI BNSymbol* BNRelocationGetSymbol(BNRelocation* reloc);

// Segment object methods
BINARYNINJACOREAPI BNSegment* BNCreateSegment(
uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags, bool autoDefined);
BINARYNINJACOREAPI BNSegment* BNCreateSegment(uint64_t start, uint64_t length, uint64_t dataOffset, uint64_t dataLength, uint32_t flags, bool autoDefined);
BINARYNINJACOREAPI BNSegment* BNNewSegmentReference(BNSegment* seg);
BINARYNINJACOREAPI void BNFreeSegment(BNSegment* seg);

BINARYNINJACOREAPI BNRange* BNSegmentGetRelocationRanges(BNSegment* segment, size_t* count);
BINARYNINJACOREAPI uint64_t BNSegmentGetRelocationsCount(BNSegment* segment);
BINARYNINJACOREAPI BNRange* BNSegmentGetRelocationRangesAtAddress(BNSegment* segment, uint64_t addr, size_t* count);
BINARYNINJACOREAPI bool BNSegmentRangeContainsRelocation(BNSegment* segment, uint64_t addr, size_t size);
BINARYNINJACOREAPI void BNFreeRelocationRanges(BNRange* ranges);
BINARYNINJACOREAPI uint64_t BNSegmentGetStart(BNSegment* segment);
BINARYNINJACOREAPI uint64_t BNSegmentGetLength(BNSegment* segment);
BINARYNINJACOREAPI uint64_t BNSegmentGetEnd(BNSegment* segment);
Expand Down
Loading

0 comments on commit 5fc0803

Please sign in to comment.