Skip to content

Commit fe925b0

Browse files
committed
Add support for new constant type EXTERN_PTR
1 parent 4fdd54f commit fe925b0

6 files changed

Lines changed: 57 additions & 17 deletions

File tree

binaryninjaapi.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,7 @@ namespace BinaryNinja
24172417
{
24182418
BNRegisterValueType state;
24192419
int64_t value;
2420+
int64_t offset;
24202421

24212422
RegisterValue();
24222423
static RegisterValue FromAPIObject(const BNRegisterValue& value);
@@ -2427,6 +2428,7 @@ namespace BinaryNinja
24272428
{
24282429
BNRegisterValueType state;
24292430
int64_t value;
2431+
int64_t offset;
24302432
std::vector<BNValueRange> ranges;
24312433
std::set<int64_t> valueSet;
24322434
std::vector<LookupTableEntry> table;
@@ -3421,27 +3423,30 @@ namespace BinaryNinja
34213423
BNRelocationInfo* result, size_t resultCount);
34223424
static bool ApplyRelocationCallback(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc,
34233425
uint8_t* dest, size_t len);
3424-
3426+
static size_t GetOperandForExternalRelocationCallback(void* ctxt, const uint8_t* data, uint64_t addr,
3427+
size_t length, BNLowLevelILFunction* il, BNRelocation* relocation);
34253428
protected:
34263429
RelocationHandler();
34273430
RelocationHandler(BNRelocationHandler* handler);
34283431
static void FreeCallback(void* ctxt);
34293432

34303433
public:
3431-
34323434
virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result);
34333435
virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest,
34343436
size_t len);
3437+
virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length,
3438+
Ref<LowLevelILFunction> il, Ref<Relocation> relocation);
34353439
};
34363440

34373441
class CoreRelocationHandler: public RelocationHandler
34383442
{
34393443
public:
34403444
CoreRelocationHandler(BNRelocationHandler* handler);
3441-
34423445
virtual bool GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result) override;
34433446
virtual bool ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest,
34443447
size_t len) override;
3448+
virtual size_t GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length,
3449+
Ref<LowLevelILFunction> il, Ref<Relocation> relocation) override;
34453450
};
34463451

34473452
class UpdateException: public std::exception

binaryninjacore.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#define LLIL_GET_TEMP_REG_INDEX(n) ((n) & 0x7fffffff)
6565
#define BN_INVALID_REGISTER 0xffffffff
6666

67+
#define BN_NOCOERCE_EXTERN_PTR 0xfffffffe
6768
#define BN_INVALID_OPERAND 0xffffffff
6869

6970
#define BN_INVALID_EXPR ((size_t)-1)
@@ -733,6 +734,7 @@ extern "C"
733734
EntryValue,
734735
ConstantValue,
735736
ConstantPointerValue,
737+
ExternalPointerValue,
736738
StackFrameOffset,
737739
ReturnAddressValue,
738740
ImportedAddressValue,
@@ -777,6 +779,7 @@ extern "C"
777779
{
778780
BNRegisterValueType state;
779781
int64_t value;
782+
int64_t offset;
780783
};
781784

782785
struct BNRegisterValueWithConfidence
@@ -794,6 +797,7 @@ extern "C"
794797
{
795798
BNRegisterValueType state;
796799
int64_t value;
800+
int64_t offset;
797801
BNValueRange* ranges;
798802
int64_t* valueSet;
799803
BNLookupTableEntry* table;
@@ -1124,12 +1128,13 @@ extern "C"
11241128
StandardRelocationType,
11251129
IgnoredRelocation
11261130
};
1127-
1131+
#define MAX_RELOCATION_SIZE 8
11281132
struct BNRelocationInfo
11291133
{
11301134
BNRelocationType type; // BinaryNinja Relocation Type
11311135
bool pcRelative; // PC Relative or Absolute (subtract address from relocation)
11321136
bool baseRelative; // Relative to start of module (Add module base to relocation)
1137+
uint64_t base; // Base address for this binary view
11331138
size_t size; // Size of the data to be written
11341139
size_t truncateSize; // After addition/subtraction truncate to
11351140
uint64_t nativeType; // Base type from relocation entry
@@ -1141,6 +1146,9 @@ extern "C"
11411146
size_t sectionIndex; // Index into the section table
11421147
uint64_t address; // Absolute address or segment offset
11431148
bool dataRelocation; // This relocation is effecting data not code
1149+
uint8_t relocationDataCache[MAX_RELOCATION_SIZE];
1150+
struct BNRelocationInfo* prev; // Link to relocation another related relocation
1151+
struct BNRelocationInfo* next; // Link to relocation another related relocation
11441152
};
11451153

11461154
struct BNInstructionTextToken
@@ -1403,6 +1411,8 @@ extern "C"
14031411
size_t resultCount);
14041412
bool (*applyRelocation)(void* ctxt, BNBinaryView* view, BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest,
14051413
size_t len);
1414+
size_t (*getOperandForExternalRelocation)(void* ctxt, const uint8_t* data, uint64_t addr, size_t length,
1415+
BNLowLevelILFunction* il, BNRelocation* relocation);
14061416
};
14071417

14081418
struct BNTypeParserResult
@@ -2346,6 +2356,8 @@ extern "C"
23462356
BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len);
23472357
BINARYNINJACOREAPI bool BNRelocationHandlerDefaultApplyRelocation(BNRelocationHandler* handler, BNBinaryView* view,
23482358
BNArchitecture* arch, BNRelocation* reloc, uint8_t* dest, size_t len);
2359+
BINARYNINJACOREAPI size_t BNRelocationHandlerGetOperandForExternalRelocation(BNRelocationHandler* handler,
2360+
const uint8_t* data, uint64_t addr, size_t length, const BNLowLevelILFunction* il, BNRelocation* relocation);
23492361
// Analysis
23502362
BINARYNINJACOREAPI void BNAddAnalysisOption(BNBinaryView* view, const char* name);
23512363
BINARYNINJACOREAPI void BNAddFunctionForAnalysis(BNBinaryView* view, BNPlatform* platform, uint64_t addr);

examples/mlil_parser/src/mlil_parser.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,10 @@ int main(int argc, char *argv[])
323323

324324
// Example of using visitors to find all constants in the instruction
325325
instr.VisitExprs([&](const MediumLevelILInstruction& expr) {
326-
switch (expr.operation)
327-
{
328-
case MLIL_CONST:
329-
case MLIL_CONST_PTR:
330-
case MLIL_EXTERN_PTR:
326+
bool status = MediumLevelILFunction::IsConstantType(expr.operation);
327+
if (status)
331328
printf(" Found constant 0x%" PRIx64 "\n", expr.GetConstant());
332-
return false; // Done parsing this
333-
default:
334-
break;
335-
}
336-
return true; // Parse any subexpressions
329+
return !status;
337330
});
338331

339332
// Example of using the templated accessors for efficiently parsing load instructions

function.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Variable Variable::FromIdentifier(uint64_t id)
9191
}
9292

9393

94-
RegisterValue::RegisterValue(): state(UndeterminedValue), value(0)
94+
RegisterValue::RegisterValue(): state(UndeterminedValue), value(0), offset(0)
9595
{
9696
}
9797

@@ -101,6 +101,7 @@ BNRegisterValue RegisterValue::ToAPIObject()
101101
BNRegisterValue result;
102102
result.state = state;
103103
result.value = value;
104+
result.offset = offset;
104105
return result;
105106
}
106107

@@ -278,6 +279,7 @@ PossibleValueSet PossibleValueSet::FromAPIObject(BNPossibleValueSet& value)
278279
PossibleValueSet result;
279280
result.state = value.state;
280281
result.value = value.value;
282+
result.offset = value.offset;
281283
if (value.state == LookupTableValue)
282284
{
283285
for (size_t i = 0; i < value.count; i++)

mediumlevelilinstruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ unordered_map<BNMediumLevelILOperation, vector<MediumLevelILOperandUsage>>
168168
{MLIL_MEM_PHI, {DestMemoryVersionMediumLevelOperandUsage, SourceMemoryVersionsMediumLevelOperandUsage}},
169169
{MLIL_CONST, {ConstantMediumLevelOperandUsage}},
170170
{MLIL_CONST_PTR, {ConstantMediumLevelOperandUsage}},
171-
{MLIL_EXTERN_PTR, {ConstantMediumLevelOperandUsage}},
171+
{MLIL_EXTERN_PTR, {ConstantMediumLevelOperandUsage, OffsetMediumLevelOperandUsage}},
172172
{MLIL_FLOAT_CONST, {ConstantMediumLevelOperandUsage}},
173173
{MLIL_IMPORT, {ConstantMediumLevelOperandUsage}},
174174
{MLIL_ADD, {LeftExprMediumLevelOperandUsage, RightExprMediumLevelOperandUsage}},

relocationhandler.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ RelocationHandler::RelocationHandler()
3737
handler.freeObject = FreeCallback;
3838
handler.getRelocationInfo = GetRelocationInfoCallback;
3939
handler.applyRelocation = ApplyRelocationCallback;
40+
handler.getOperandForExternalRelocation = GetOperandForExternalRelocationCallback;
4041

4142
AddRefForRegistration();
4243
m_object = BNCreateRelocationHandler(&handler);
@@ -77,6 +78,16 @@ bool RelocationHandler::ApplyRelocationCallback(void* ctxt, BNBinaryView* view,
7778
}
7879

7980

81+
size_t RelocationHandler::GetOperandForExternalRelocationCallback(void* ctxt, const uint8_t* data, uint64_t addr,
82+
size_t length, BNLowLevelILFunction* il, BNRelocation* reloc)
83+
{
84+
RelocationHandler* handler = (RelocationHandler*)ctxt;
85+
Ref<LowLevelILFunction> func(new LowLevelILFunction(BNNewLowLevelILFunctionReference(il)));
86+
Ref<Relocation> relocObj = new Relocation(BNNewRelocationReference(reloc));
87+
return handler->GetOperandForExternalRelocation(data, addr, length, func, relocObj);
88+
}
89+
90+
8091
bool RelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture> arch, std::vector<BNRelocationInfo>& result)
8192
{
8293
(void)view;
@@ -89,9 +100,18 @@ bool RelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architecture
89100
bool RelocationHandler::ApplyRelocation(Ref<BinaryView> view, Ref<Architecture> arch, Ref<Relocation> reloc, uint8_t* dest,
90101
size_t len)
91102
{
92-
return BNRelocationHandlerDefaultApplyRelocation(m_object, view->GetObject(), arch->GetObject(), BNNewRelocationReference(reloc->GetObject()), dest, len);
103+
return BNRelocationHandlerDefaultApplyRelocation(m_object, view->GetObject(), arch->GetObject(),
104+
BNNewRelocationReference(reloc->GetObject()), dest, len);
93105
}
94106

107+
108+
size_t RelocationHandler::GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length,
109+
Ref<LowLevelILFunction> il, Ref<Relocation> relocation)
110+
{
111+
return BN_INVALID_OPERAND;
112+
}
113+
114+
95115
CoreRelocationHandler::CoreRelocationHandler(BNRelocationHandler* handler) : RelocationHandler(handler)
96116
{
97117
}
@@ -114,4 +134,12 @@ bool CoreRelocationHandler::GetRelocationInfo(Ref<BinaryView> view, Ref<Architec
114134
for (size_t i = 0; i < result.size(); i++)
115135
result[i] = results[i];
116136
return status;
137+
}
138+
139+
140+
size_t CoreRelocationHandler::GetOperandForExternalRelocation(const uint8_t* data, uint64_t addr, size_t length,
141+
Ref<LowLevelILFunction> il, Ref<Relocation> relocation)
142+
{
143+
return BNRelocationHandlerGetOperandForExternalRelocation(m_object, data, addr, length, il->GetObject(),
144+
relocation->GetObject());
117145
}

0 commit comments

Comments
 (0)