Skip to content

Commit 49e79c5

Browse files
committed
Add MemoryMap API refinements for region and range enumeration.
1 parent 82b1b5c commit 49e79c5

6 files changed

Lines changed: 447 additions & 38 deletions

File tree

binaryninjaapi.h

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8211,6 +8211,35 @@ namespace BinaryNinja {
82118211
segmentation is automatically managed. If multiple regions overlap, the most recently added region takes
82128212
precedence by default.
82138213
*/
8214+
8215+
struct MemoryRegionInfo
8216+
{
8217+
std::string name;
8218+
std::string displayName;
8219+
uint64_t start;
8220+
uint64_t length;
8221+
uint32_t flags;
8222+
bool enabled;
8223+
bool rebaseable;
8224+
uint8_t fill;
8225+
bool hasTarget;
8226+
bool absoluteAddressMode;
8227+
bool local;
8228+
};
8229+
8230+
struct ResolvedMemoryRange
8231+
{
8232+
uint64_t start;
8233+
uint64_t length;
8234+
std::vector<MemoryRegionInfo> regions;
8235+
8236+
uint64_t End() const { return start + length; }
8237+
const MemoryRegionInfo* ActiveRegion() const { return regions.empty() ? nullptr : &regions.front(); }
8238+
const MemoryRegionInfo* Region() const { return ActiveRegion(); }
8239+
std::string Name() const { auto* r = ActiveRegion(); return r ? r->name : std::string(); }
8240+
uint32_t Flags() const { auto* r = ActiveRegion(); return r ? r->flags : 0; }
8241+
};
8242+
82148243
class MemoryMap
82158244
{
82168245
BNBinaryView* m_object;
@@ -8336,6 +8365,59 @@ namespace BinaryNinja {
83368365
return BNIsMemoryRegionLocal(m_object, name.c_str());
83378366
}
83388367

8368+
std::vector<MemoryRegionInfo> GetMemoryRegions()
8369+
{
8370+
size_t count = 0;
8371+
BNMemoryRegionInfo* regions = BNGetMemoryRegions(m_object, &count);
8372+
std::vector<MemoryRegionInfo> result;
8373+
result.reserve(count);
8374+
for (size_t i = 0; i < count; i++)
8375+
{
8376+
result.push_back({
8377+
regions[i].name,
8378+
regions[i].displayName,
8379+
regions[i].start,
8380+
regions[i].length,
8381+
regions[i].flags,
8382+
regions[i].enabled,
8383+
regions[i].rebaseable,
8384+
regions[i].fill,
8385+
regions[i].hasTarget,
8386+
regions[i].absoluteAddressMode,
8387+
regions[i].local,
8388+
});
8389+
}
8390+
BNFreeMemoryRegions(regions, count);
8391+
return result;
8392+
}
8393+
8394+
std::vector<ResolvedMemoryRange> GetResolvedRanges()
8395+
{
8396+
size_t count = 0;
8397+
BNResolvedMemoryRange* ranges = BNGetResolvedMemoryRanges(m_object, &count);
8398+
std::vector<ResolvedMemoryRange> result;
8399+
result.reserve(count);
8400+
for (size_t i = 0; i < count; i++)
8401+
{
8402+
ResolvedMemoryRange range;
8403+
range.start = ranges[i].start;
8404+
range.length = ranges[i].length;
8405+
range.regions.reserve(ranges[i].regionCount);
8406+
for (size_t j = 0; j < ranges[i].regionCount; j++)
8407+
{
8408+
auto& r = ranges[i].regions[j];
8409+
range.regions.push_back({
8410+
r.name, r.displayName, r.start, r.length,
8411+
r.flags, r.enabled, r.rebaseable, r.fill,
8412+
r.hasTarget, r.absoluteAddressMode, r.local,
8413+
});
8414+
}
8415+
result.push_back(std::move(range));
8416+
}
8417+
BNFreeResolvedMemoryRanges(ranges, count);
8418+
return result;
8419+
}
8420+
83398421
void Reset()
83408422
{
83418423
BNResetMemoryMap(m_object);
@@ -20044,7 +20126,7 @@ namespace BinaryNinja {
2004420126
\return True if the type library was successfully decompressed
2004520127
*/
2004620128
bool DecompressToFile(const std::string& path);
20047-
20129+
2004820130
/*! The Architecture this type library is associated with
2004920131

2005020132
\return

binaryninjacore.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
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 158
40+
#define BN_CURRENT_CORE_ABI_VERSION 159
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
4444
// will require rebuilding. The minimum version is increased when there are
4545
// incompatible changes that break binary compatibility, such as changes to
4646
// existing types or functions.
47-
#define BN_MINIMUM_CORE_ABI_VERSION 158
47+
#define BN_MINIMUM_CORE_ABI_VERSION 159
4848

4949
#ifdef __GNUC__
5050
#ifdef BINARYNINJACORE_LIBRARY
@@ -3803,6 +3803,27 @@ extern "C"
38033803
uint64_t infoData;
38043804
} BNSectionInfo;
38053805

3806+
typedef struct BNMemoryRegionInfo {
3807+
char* name;
3808+
char* displayName;
3809+
uint64_t start;
3810+
uint64_t length;
3811+
uint32_t flags;
3812+
bool enabled;
3813+
bool rebaseable;
3814+
uint8_t fill;
3815+
bool hasTarget;
3816+
bool absoluteAddressMode;
3817+
bool local;
3818+
} BNMemoryRegionInfo;
3819+
3820+
typedef struct BNResolvedMemoryRange {
3821+
uint64_t start;
3822+
uint64_t length;
3823+
BNMemoryRegionInfo* regions;
3824+
size_t regionCount;
3825+
} BNResolvedMemoryRange;
3826+
38063827
typedef bool(*BNCollaborationAnalysisConflictHandler)(void*, const char** keys, BNAnalysisMergeConflict** conflicts, size_t conflictCount);
38073828
typedef bool(*BNCollaborationNameChangesetFunction)(void*, BNCollaborationChangeset*);
38083829

@@ -4531,6 +4552,10 @@ extern "C"
45314552
BINARYNINJACOREAPI char* BNGetMemoryRegionDisplayName(BNBinaryView* view, const char* name);
45324553
BINARYNINJACOREAPI bool BNSetMemoryRegionDisplayName(BNBinaryView* view, const char* name, const char* displayName);
45334554
BINARYNINJACOREAPI bool BNIsMemoryRegionLocal(BNBinaryView* view, const char* name);
4555+
BINARYNINJACOREAPI BNMemoryRegionInfo* BNGetMemoryRegions(BNBinaryView* view, size_t* count);
4556+
BINARYNINJACOREAPI void BNFreeMemoryRegions(BNMemoryRegionInfo* regions, size_t count);
4557+
BINARYNINJACOREAPI BNResolvedMemoryRange* BNGetResolvedMemoryRanges(BNBinaryView* view, size_t* count);
4558+
BINARYNINJACOREAPI void BNFreeResolvedMemoryRanges(BNResolvedMemoryRange* ranges, size_t count);
45344559
BINARYNINJACOREAPI void BNResetMemoryMap(BNBinaryView* view);
45354560

45364561
// Binary view access

0 commit comments

Comments
 (0)