-
Notifications
You must be signed in to change notification settings - Fork 586
/
Copy pathCpuArch.cs
427 lines (382 loc) · 12.6 KB
/
CpuArch.cs
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// dnlib: See LICENSE.txt for more info
using System;
using dnlib.DotNet.Writer;
using dnlib.IO;
using dnlib.PE;
namespace dnlib.DotNet {
enum StubType {
Export,
EntryPoint,
}
abstract class CpuArch {
// To support a new CPU arch, the easiest way is to check coreclr/src/ilasm/writer.cpp or
// coreclr/src/dlls/mscorpe/stubs.h, eg. ExportStubAMD64Template, ExportStubX86Template,
// ExportStubARMTemplate, ExportStubIA64Template, or use ilasm to generate a file with
// exports and check the stub
static readonly X86CpuArch x86CpuArch = new X86CpuArch();
static readonly X64CpuArch x64CpuArch = new X64CpuArch();
static readonly ItaniumCpuArch itaniumCpuArch = new ItaniumCpuArch();
static readonly ArmCpuArch armCpuArch = new ArmCpuArch();
/// <summary>
/// Gets the required alignment for the stubs, must be a power of 2
/// </summary>
/// <param name="stubType">Stub type</param>
/// <returns></returns>
public abstract uint GetStubAlignment(StubType stubType);
/// <summary>
/// Gets the size of a stub, it doesn't have to be a multiple of <see cref="GetStubAlignment(StubType)"/>
/// </summary>
/// <param name="stubType">Stub type</param>
/// <returns></returns>
public abstract uint GetStubSize(StubType stubType);
/// <summary>
/// Gets the offset of the code (entry point) relative to the start of the stub
/// </summary>
/// <param name="stubType">Stub type</param>
/// <returns></returns>
public abstract uint GetStubCodeOffset(StubType stubType);
public static bool TryGetCpuArch(Machine machine, out CpuArch cpuArch) {
switch (machine) {
case Machine.I386:
case Machine.I386_Native_Apple:
case Machine.I386_Native_FreeBSD:
case Machine.I386_Native_Linux:
case Machine.I386_Native_NetBSD:
case Machine.I386_Native_Sun:
cpuArch = x86CpuArch;
return true;
case Machine.AMD64:
case Machine.AMD64_Native_Apple:
case Machine.AMD64_Native_FreeBSD:
case Machine.AMD64_Native_Linux:
case Machine.AMD64_Native_NetBSD:
case Machine.AMD64_Native_Sun:
cpuArch = x64CpuArch;
return true;
case Machine.IA64:
cpuArch = itaniumCpuArch;
return true;
case Machine.ARMNT:
case Machine.ARMNT_Native_Apple:
case Machine.ARMNT_Native_FreeBSD:
case Machine.ARMNT_Native_Linux:
case Machine.ARMNT_Native_NetBSD:
case Machine.ARMNT_Native_Sun:
cpuArch = armCpuArch;
return true;
case Machine.ARM64:
case Machine.ARM64_Native_Apple:
case Machine.ARM64_Native_FreeBSD:
case Machine.ARM64_Native_Linux:
case Machine.ARM64_Native_NetBSD:
case Machine.ARM64_Native_Sun:
//TODO: Support ARM64
goto default;
default:
cpuArch = null;
return false;
}
}
/// <summary>
/// Gets the RVA of the func field that the stub jumps to
/// </summary>
/// <param name="reader">Reader, positioned at the stub func</param>
/// <param name="peImage">PE image</param>
/// <param name="funcRva">Updated with RVA of func field</param>
/// <returns></returns>
public bool TryGetExportedRvaFromStub(ref DataReader reader, IPEImage peImage, out uint funcRva) =>
TryGetExportedRvaFromStubCore(ref reader, peImage, out funcRva);
protected abstract bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva);
/// <summary>
/// Writes stub relocs, if needed
/// </summary>
/// <param name="stubType">Stub type</param>
/// <param name="relocDirectory">Reloc directory</param>
/// <param name="chunk">The chunk where this stub will be written to</param>
/// <param name="stubOffset">Offset of this stub in <paramref name="chunk"/></param>
public abstract void WriteStubRelocs(StubType stubType, RelocDirectory relocDirectory, IChunk chunk, uint stubOffset);
/// <summary>
/// Writes the stub that jumps to the managed function
/// </summary>
/// <param name="stubType">Stub type</param>
/// <param name="writer">Writer</param>
/// <param name="imageBase">Image base</param>
/// <param name="stubRva">RVA of this stub</param>
/// <param name="managedFuncRva">RVA of a pointer-sized field that contains the absolute address of the managed function</param>
public abstract void WriteStub(StubType stubType, DataWriter writer, ulong imageBase, uint stubRva, uint managedFuncRva);
}
sealed class X86CpuArch : CpuArch {
public override uint GetStubAlignment(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 4;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubSize(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 2/*padding*/ + 6;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubCodeOffset(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 2/*padding*/;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva) {
funcRva = 0;
// FF25xxxxxxxx jmp DWORD PTR [xxxxxxxx]
if (reader.ReadUInt16() != 0x25FF)
return false;
funcRva = reader.ReadUInt32() - (uint)peImage.ImageNTHeaders.OptionalHeader.ImageBase;
return true;
}
public override void WriteStubRelocs(StubType stubType, RelocDirectory relocDirectory, IChunk chunk, uint stubOffset) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
relocDirectory.Add(chunk, stubOffset + 4);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void WriteStub(StubType stubType, DataWriter writer, ulong imageBase, uint stubRva, uint managedFuncRva) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
writer.WriteUInt16(0);// padding
writer.WriteUInt16(0x25FF);
writer.WriteUInt32((uint)imageBase + managedFuncRva);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
sealed class X64CpuArch : CpuArch {
public override uint GetStubAlignment(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 4;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubSize(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 2/*padding*/ + 12;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubCodeOffset(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 2/*padding*/;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva) {
funcRva = 0;
// 48A1xxxxxxxxxxxxxxxx movabs rax,[xxxxxxxxxxxxxxxx]
// FFE0 jmp rax
if (reader.ReadUInt16() != 0xA148)
return false;
ulong absAddr = reader.ReadUInt64();
if (reader.ReadUInt16() != 0xE0FF)
return false;
ulong rva = absAddr - peImage.ImageNTHeaders.OptionalHeader.ImageBase;
if (rva > uint.MaxValue)
return false;
funcRva = (uint)rva;
return true;
}
public override void WriteStubRelocs(StubType stubType, RelocDirectory relocDirectory, IChunk chunk, uint stubOffset) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
relocDirectory.Add(chunk, stubOffset + 4);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void WriteStub(StubType stubType, DataWriter writer, ulong imageBase, uint stubRva, uint managedFuncRva) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
writer.WriteUInt16(0);// padding
writer.WriteUInt16(0xA148);
writer.WriteUInt64(imageBase + managedFuncRva);
writer.WriteUInt16(0xE0FF);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
sealed class ItaniumCpuArch : CpuArch {
public override uint GetStubAlignment(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 16;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubSize(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 0x30;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubCodeOffset(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 0x20;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva) {
funcRva = 0;
// From ExportStubIA64Template in coreclr/src/ilasm/writer.cpp
//
// ld8 r9 = [gp] ;;
// ld8 r10 = [r9],8
// nop.i ;;
// ld8 gp = [r9]
// mov b6 = r10
// br.cond.sptk.few b6
//
// 0x0B, 0x48, 0x00, 0x02, 0x18, 0x10, 0xA0, 0x40,
// 0x24, 0x30, 0x28, 0x00, 0x00, 0x00, 0x04, 0x00,
// 0x10, 0x08, 0x00, 0x12, 0x18, 0x10, 0x60, 0x50,
// 0x04, 0x80, 0x03, 0x00, 0x60, 0x00, 0x80, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//address of the template
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //address of VTFixup slot
ulong addrTemplate = reader.ReadUInt64();
ulong absAddr = reader.ReadUInt64();
reader.Position = (uint)peImage.ToFileOffset((RVA)(addrTemplate - peImage.ImageNTHeaders.OptionalHeader.ImageBase));
if (reader.ReadUInt64() != 0x40A010180200480BUL)
return false;
if (reader.ReadUInt64() != 0x0004000000283024UL)
return false;
if (reader.ReadUInt64() != 0x5060101812000810UL)
return false;
if (reader.ReadUInt64() != 0x0080006000038004UL)
return false;
ulong rva = absAddr - peImage.ImageNTHeaders.OptionalHeader.ImageBase;
if (rva > uint.MaxValue)
return false;
funcRva = (uint)rva;
return true;
}
public override void WriteStubRelocs(StubType stubType, RelocDirectory relocDirectory, IChunk chunk, uint stubOffset) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
relocDirectory.Add(chunk, stubOffset + 0x20);
relocDirectory.Add(chunk, stubOffset + 0x28);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void WriteStub(StubType stubType, DataWriter writer, ulong imageBase, uint stubRva, uint managedFuncRva) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
writer.WriteUInt64(0x40A010180200480BUL);
writer.WriteUInt64(0x0004000000283024UL);
writer.WriteUInt64(0x5060101812000810UL);
writer.WriteUInt64(0x0080006000038004UL);
writer.WriteUInt64(imageBase + stubRva);
writer.WriteUInt64(imageBase + managedFuncRva);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
sealed class ArmCpuArch : CpuArch {
public override uint GetStubAlignment(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 4;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubSize(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 8;
default:
throw new ArgumentOutOfRangeException();
}
}
public override uint GetStubCodeOffset(StubType stubType) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
return 0;
default:
throw new ArgumentOutOfRangeException();
}
}
protected override bool TryGetExportedRvaFromStubCore(ref DataReader reader, IPEImage peImage, out uint funcRva) {
funcRva = 0;
// DFF800F0 ldr.w pc,[pc]
// xxxxxxxx
if (reader.ReadUInt32() != 0xF000F8DF)
return false;
funcRva = reader.ReadUInt32() - (uint)peImage.ImageNTHeaders.OptionalHeader.ImageBase;
return true;
}
public override void WriteStubRelocs(StubType stubType, RelocDirectory relocDirectory, IChunk chunk, uint stubOffset) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
relocDirectory.Add(chunk, stubOffset + 4);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void WriteStub(StubType stubType, DataWriter writer, ulong imageBase, uint stubRva, uint managedFuncRva) {
switch (stubType) {
case StubType.Export:
case StubType.EntryPoint:
writer.WriteUInt32(0xF000F8DF);
writer.WriteUInt32((uint)imageBase + managedFuncRva);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}