-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBNRelocationHandler.cs
More file actions
262 lines (226 loc) · 5.89 KB
/
Copy pathBNRelocationHandler.cs
File metadata and controls
262 lines (226 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
namespace BinaryNinja
{
/// <summary>
/// Applies architecture-specific relocations and describes their native metadata.
/// </summary>
public partial class RelocationHandler : AbstractSafeHandle<RelocationHandler>
{
/// <summary>Requests automatic coercion for an external relocation pointer.</summary>
public const ulong AutoCoerceExternPointer = 0xfffffffd;
private static readonly object registrationLock = new object();
private static readonly List<RelocationHandler> registeredHandlers =
new List<RelocationHandler>();
private readonly bool custom;
private bool registered;
internal RelocationHandler(IntPtr handle, bool owner)
: base(handle, owner)
{
}
/// <summary>Creates a custom relocation handler backed by managed callbacks.</summary>
protected RelocationHandler()
: base(true)
{
this.custom = true;
this.InitializeCustomCallbacks();
}
internal static RelocationHandler? NewFromHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
return null;
}
return new CoreRelocationHandler(
NativeMethods.BNNewRelocationHandlerReference(handle), true
);
}
internal static RelocationHandler MustNewFromHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
throw new ArgumentNullException(nameof(handle));
}
return new CoreRelocationHandler(
NativeMethods.BNNewRelocationHandlerReference(handle), true
);
}
internal static RelocationHandler? TakeHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
return null;
}
return new CoreRelocationHandler(handle, true);
}
internal static RelocationHandler MustTakeHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
throw new ArgumentNullException(nameof(handle));
}
return new CoreRelocationHandler(handle, true);
}
internal static RelocationHandler? BorrowHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
return null;
}
return new CoreRelocationHandler(handle, false);
}
internal static RelocationHandler MustBorrowHandle(IntPtr handle)
{
if (IntPtr.Zero == handle)
{
throw new ArgumentNullException(nameof(handle));
}
return new CoreRelocationHandler(handle, false);
}
internal static void RootForRegistration(RelocationHandler handler)
{
if (null == handler)
{
throw new ArgumentNullException(nameof(handler));
}
if (!handler.custom)
{
return;
}
lock (RelocationHandler.registrationLock)
{
if (!handler.registered)
{
handler.registered = true;
RelocationHandler.registeredHandlers.Add(handler);
}
}
}
protected override bool ReleaseHandle()
{
if (!this.IsInvalid && !this.registered)
{
NativeMethods.BNFreeRelocationHandler(this.handle);
this.SetHandleAsInvalid();
}
return true;
}
/// <summary>Updates native relocation metadata before relocations are defined.</summary>
/// <param name="view">The binary view containing the relocations.</param>
/// <param name="architecture">The architecture interpreting the relocations.</param>
/// <param name="result">The metadata records to inspect and update in place.</param>
/// <returns>True when the metadata was recognized and updated.</returns>
public virtual bool GetRelocationInfo(
BinaryView view,
Architecture architecture,
RelocationInfo[] result
)
{
return false;
}
/// <summary>Applies one relocation to a destination buffer.</summary>
public virtual bool ApplyRelocation(
BinaryView view,
Architecture architecture,
Relocation relocation,
IntPtr destination,
ulong length
)
{
return this.DefaultApplyRelocation(
view, architecture, relocation, destination, length
);
}
/// <summary>Applies one relocation using the core default implementation.</summary>
public bool DefaultApplyRelocation(
BinaryView view,
Architecture architecture,
Relocation relocation,
IntPtr destination,
ulong length
)
{
RelocationHandler.ValidateRelocationArguments(
view, architecture, relocation
);
return NativeMethods.BNRelocationHandlerDefaultApplyRelocation(
this.handle,
view.DangerousGetHandle(),
architecture.DangerousGetHandle(),
relocation.DangerousGetHandle(),
destination,
length
);
}
/// <summary>Gets the LLIL operand for an external relocation.</summary>
public virtual ulong GetOperandForExternalRelocation(
IntPtr data,
ulong address,
ulong length,
LowLevelILFunction lowLevelIl,
Relocation relocation
)
{
return RelocationHandler.AutoCoerceExternPointer;
}
private static void ValidateRelocationArguments(
BinaryView view,
Architecture architecture,
Relocation relocation
)
{
if (null == view)
{
throw new ArgumentNullException(nameof(view));
}
if (null == architecture)
{
throw new ArgumentNullException(nameof(architecture));
}
if (null == relocation)
{
throw new ArgumentNullException(nameof(relocation));
}
}
private static void ValidateRelocationInfoArguments(
BinaryView view,
Architecture architecture,
RelocationInfo[] result
)
{
if (null == view)
{
throw new ArgumentNullException(nameof(view));
}
if (null == architecture)
{
throw new ArgumentNullException(nameof(architecture));
}
if (null == result)
{
throw new ArgumentNullException(nameof(result));
}
foreach (RelocationInfo info in result)
{
if (null == info)
{
throw new ArgumentException(
"Relocation metadata cannot contain null values.",
nameof(result)
);
}
}
}
private static BNRelocationInfo[] CreateNativeRelocationInfo(
RelocationInfo[] result
)
{
BNRelocationInfo[] nativeResult = new BNRelocationInfo[result.Length];
for (int i = 0; i < result.Length; i++)
{
nativeResult[i] = result[i].ToNative(default(BNRelocationInfo));
}
return nativeResult;
}
}
}