@wzwjustdoit/dsh-plugin-git-bash 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +154 -2
  3. package/client.js +736 -0
  4. package/cordis.patch.yml +27 -0
  5. package/lib/agents.d.ts +5 -0
  6. package/lib/agents.d.ts.map +1 -0
  7. package/lib/agents.js +56 -0
  8. package/lib/agents.js.map +1 -0
  9. package/lib/discovery.d.ts +21 -0
  10. package/lib/discovery.d.ts.map +1 -0
  11. package/lib/discovery.js +122 -0
  12. package/lib/discovery.js.map +1 -0
  13. package/lib/index.d.ts +55 -0
  14. package/lib/index.d.ts.map +1 -0
  15. package/lib/index.js +369 -0
  16. package/lib/index.js.map +1 -0
  17. package/native/CMakeLists.txt +58 -0
  18. package/native/bin/win32-x64/msys-token-guard-hook.dll +0 -0
  19. package/native/bin/win32-x64/msys-token-guard.exe +0 -0
  20. package/native/msys-token-guard-hook.cpp +470 -0
  21. package/native/msys-token-guard.cpp +671 -0
  22. package/native/vendor/detours/LICENSE.md +23 -0
  23. package/native/vendor/detours/README.dsh.md +13 -0
  24. package/native/vendor/detours/creatwth.cpp +1577 -0
  25. package/native/vendor/detours/detours.cpp +2364 -0
  26. package/native/vendor/detours/detours.h +1059 -0
  27. package/native/vendor/detours/detver.h +27 -0
  28. package/native/vendor/detours/disasm.cpp +4236 -0
  29. package/native/vendor/detours/image.cpp +2251 -0
  30. package/native/vendor/detours/modules.cpp +929 -0
  31. package/native/vendor/detours/uimports.cpp +269 -0
  32. package/package.json +120 -4
  33. package/scripts/build-native.mjs +35 -0
  34. package/scripts/dsh.desktop.startup/README.md +27 -0
  35. package/scripts/dsh.desktop.startup/dsh.desktop.startup.gitbash.bat +28 -0
  36. package/scripts/dsh.desktop.startup/dsh.desktop.startup.gitbash.sh +46 -0
  37. package/scripts/dsh.desktop.startup/dsh.desktop.startup.gitbash.vbs +0 -0
  38. package/scripts/dsh.web.startup/README.md +33 -0
  39. package/scripts/dsh.web.startup/dsh.web.startup.gitbash.bat +28 -0
  40. package/scripts/dsh.web.startup/dsh.web.startup.gitbash.sh +48 -0
  41. package/scripts/dsh.web.startup/dsh.web.startup.gitbash.vbs +0 -0
@@ -0,0 +1,4236 @@
1
+ //////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Detours Disassembler (disasm.cpp of detours.lib)
4
+ //
5
+ // Microsoft Research Detours Package, Version 4.0.1
6
+ //
7
+ // Copyright (c) Microsoft Corporation. All rights reserved.
8
+ //
9
+
10
+ #if _MSC_VER >= 1900
11
+ #pragma warning(push)
12
+ #pragma warning(disable:4091) // empty typedef
13
+ #endif
14
+
15
+ #define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1
16
+ #include <windows.h>
17
+ #include <limits.h>
18
+
19
+ // #define DETOUR_DEBUG 1
20
+ #define DETOURS_INTERNAL
21
+
22
+ #include "detours.h"
23
+
24
+ #if DETOURS_VERSION != 0x4c0c1 // 0xMAJORcMINORcPATCH
25
+ #error detours.h version mismatch
26
+ #endif
27
+
28
+ #if _MSC_VER >= 1900
29
+ #pragma warning(pop)
30
+ #endif
31
+
32
+ #undef ASSERT
33
+ #define ASSERT(x)
34
+
35
+ //////////////////////////////////////////////////////////////////////////////
36
+ //
37
+ // Special macros to handle the case when we are building disassembler for
38
+ // offline processing.
39
+ //
40
+
41
+
42
+ #if defined(DETOURS_X86_OFFLINE_LIBRARY) \
43
+ || defined(DETOURS_X64_OFFLINE_LIBRARY) \
44
+ || defined(DETOURS_ARM_OFFLINE_LIBRARY) \
45
+ || defined(DETOURS_ARM64_OFFLINE_LIBRARY) \
46
+ || defined(DETOURS_IA64_OFFLINE_LIBRARY)
47
+
48
+ #undef DETOURS_X64
49
+ #undef DETOURS_X86
50
+ #undef DETOURS_IA64
51
+ #undef DETOURS_ARM
52
+ #undef DETOURS_ARM64
53
+
54
+ #if defined(DETOURS_X86_OFFLINE_LIBRARY)
55
+
56
+ #define DetourCopyInstruction DetourCopyInstructionX86
57
+ #define DetourSetCodeModule DetourSetCodeModuleX86
58
+ #define CDetourDis CDetourDisX86
59
+ #define DETOURS_X86
60
+
61
+ #elif defined(DETOURS_X64_OFFLINE_LIBRARY)
62
+
63
+ #if !defined(DETOURS_64BIT)
64
+ // Fix this as/if bugs are discovered.
65
+ //#error X64 disassembler can only build for 64-bit.
66
+ #endif
67
+
68
+ #define DetourCopyInstruction DetourCopyInstructionX64
69
+ #define DetourSetCodeModule DetourSetCodeModuleX64
70
+ #define CDetourDis CDetourDisX64
71
+ #define DETOURS_X64
72
+
73
+ #elif defined(DETOURS_ARM_OFFLINE_LIBRARY)
74
+
75
+ #define DetourCopyInstruction DetourCopyInstructionARM
76
+ #define DetourSetCodeModule DetourSetCodeModuleARM
77
+ #define CDetourDis CDetourDisARM
78
+ #define DETOURS_ARM
79
+
80
+ #elif defined(DETOURS_ARM64_OFFLINE_LIBRARY)
81
+
82
+ #define DetourCopyInstruction DetourCopyInstructionARM64
83
+ #define DetourSetCodeModule DetourSetCodeModuleARM64
84
+ #define CDetourDis CDetourDisARM64
85
+ #define DETOURS_ARM64
86
+
87
+ #elif defined(DETOURS_IA64_OFFLINE_LIBRARY)
88
+
89
+ #define DetourCopyInstruction DetourCopyInstructionIA64
90
+ #define DetourSetCodeModule DetourSetCodeModuleIA64
91
+ #define DETOURS_IA64
92
+
93
+ #else
94
+
95
+ #error
96
+
97
+ #endif
98
+ #endif
99
+
100
+ //////////////////////////////////////////////////////////////////////////////
101
+ //
102
+ // Function:
103
+ // DetourCopyInstruction(PVOID pDst,
104
+ // PVOID *ppDstPool
105
+ // PVOID pSrc,
106
+ // PVOID *ppTarget,
107
+ // LONG *plExtra)
108
+ // Purpose:
109
+ // Copy a single instruction from pSrc to pDst.
110
+ //
111
+ // Arguments:
112
+ // pDst:
113
+ // Destination address for the instruction. May be NULL in which
114
+ // case DetourCopyInstruction is used to measure an instruction.
115
+ // If not NULL then the source instruction is copied to the
116
+ // destination instruction and any relative arguments are adjusted.
117
+ // ppDstPool:
118
+ // Destination address for the end of the constant pool. The
119
+ // constant pool works backwards toward pDst. All memory between
120
+ // pDst and *ppDstPool must be available for use by this function.
121
+ // ppDstPool may be NULL if pDst is NULL.
122
+ // pSrc:
123
+ // Source address of the instruction.
124
+ // ppTarget:
125
+ // Out parameter for any target instruction address pointed to by
126
+ // the instruction. For example, a branch or a jump insruction has
127
+ // a target, but a load or store instruction doesn't. A target is
128
+ // another instruction that may be executed as a result of this
129
+ // instruction. ppTarget may be NULL.
130
+ // plExtra:
131
+ // Out parameter for the number of extra bytes needed by the
132
+ // instruction to reach the target. For example, lExtra = 3 if the
133
+ // instruction had an 8-bit relative offset, but needs a 32-bit
134
+ // relative offset.
135
+ //
136
+ // Returns:
137
+ // Returns the address of the next instruction (following in the source)
138
+ // instruction. By subtracting pSrc from the return value, the caller
139
+ // can determinte the size of the instruction copied.
140
+ //
141
+ // Comments:
142
+ // By following the pTarget, the caller can follow alternate
143
+ // instruction streams. However, it is not always possible to determine
144
+ // the target based on static analysis. For example, the destination of
145
+ // a jump relative to a register cannot be determined from just the
146
+ // instruction stream. The output value, pTarget, can have any of the
147
+ // following outputs:
148
+ // DETOUR_INSTRUCTION_TARGET_NONE:
149
+ // The instruction has no targets.
150
+ // DETOUR_INSTRUCTION_TARGET_DYNAMIC:
151
+ // The instruction has a non-deterministic (dynamic) target.
152
+ // (i.e. the jump is to an address held in a register.)
153
+ // Address: The instruction has the specified target.
154
+ //
155
+ // When copying instructions, DetourCopyInstruction insures that any
156
+ // targets remain constant. It does so by adjusting any IP relative
157
+ // offsets.
158
+ //
159
+
160
+ #pragma data_seg(".detourd")
161
+ #pragma const_seg(".detourc")
162
+
163
+ //////////////////////////////////////////////////// X86 and X64 Disassembler.
164
+ //
165
+ // Includes full support for all x86 chips prior to the Pentium III, and some newer stuff.
166
+ //
167
+ #if defined(DETOURS_X64) || defined(DETOURS_X86)
168
+
169
+ class CDetourDis
170
+ {
171
+ public:
172
+ CDetourDis(_Out_opt_ PBYTE *ppbTarget,
173
+ _Out_opt_ LONG *plExtra);
174
+
175
+ PBYTE CopyInstruction(PBYTE pbDst, PBYTE pbSrc);
176
+ static BOOL SanityCheckSystem();
177
+ static BOOL SetCodeModule(PBYTE pbBeg, PBYTE pbEnd, BOOL fLimitReferencesToModule);
178
+
179
+ public:
180
+ struct COPYENTRY;
181
+ typedef const COPYENTRY * REFCOPYENTRY;
182
+
183
+ typedef PBYTE (CDetourDis::* COPYFUNC)(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
184
+
185
+ // nFlagBits flags.
186
+ enum {
187
+ DYNAMIC = 0x1u,
188
+ ADDRESS = 0x2u,
189
+ NOENLARGE = 0x4u,
190
+ RAX = 0x8u,
191
+ };
192
+
193
+ // ModR/M Flags
194
+ enum {
195
+ SIB = 0x10u,
196
+ RIP = 0x20u,
197
+ NOTSIB = 0x0fu,
198
+ };
199
+
200
+ struct COPYENTRY
201
+ {
202
+ // Many of these fields are often ignored. See ENTRY_DataIgnored.
203
+ ULONG nOpcode : 8; // Opcode (ignored)
204
+ ULONG nFixedSize : 4; // Fixed size of opcode
205
+ ULONG nFixedSize16 : 4; // Fixed size when 16 bit operand
206
+ ULONG nModOffset : 4; // Offset to mod/rm byte (0=none)
207
+ ULONG nRelOffset : 4; // Offset to relative target.
208
+ ULONG nTargetBack : 4; // Offset back to absolute or rip target
209
+ ULONG nFlagBits : 4; // Flags for DYNAMIC, etc.
210
+ COPYFUNC pfCopy; // Function pointer.
211
+ };
212
+
213
+ protected:
214
+ // These macros define common uses of nFixedSize..pfCopy.
215
+ #define ENTRY_DataIgnored 0, 0, 0, 0, 0, 0,
216
+ #define ENTRY_CopyBytes1 1, 1, 0, 0, 0, 0, &CDetourDis::CopyBytes
217
+ #ifdef DETOURS_X64
218
+ #define ENTRY_CopyBytes1Address 9, 5, 0, 0, 0, ADDRESS, &CDetourDis::CopyBytes
219
+ #else
220
+ #define ENTRY_CopyBytes1Address 5, 3, 0, 0, 0, ADDRESS, &CDetourDis::CopyBytes
221
+ #endif
222
+ #define ENTRY_CopyBytes1Dynamic 1, 1, 0, 0, 0, DYNAMIC, &CDetourDis::CopyBytes
223
+ #define ENTRY_CopyBytes2 2, 2, 0, 0, 0, 0, &CDetourDis::CopyBytes
224
+ #define ENTRY_CopyBytes2Jump ENTRY_DataIgnored &CDetourDis::CopyBytesJump
225
+ #define ENTRY_CopyBytes2CantJump 2, 2, 0, 1, 0, NOENLARGE, &CDetourDis::CopyBytes
226
+ #define ENTRY_CopyBytes2Dynamic 2, 2, 0, 0, 0, DYNAMIC, &CDetourDis::CopyBytes
227
+ #define ENTRY_CopyBytes3 3, 3, 0, 0, 0, 0, &CDetourDis::CopyBytes
228
+ #define ENTRY_CopyBytes3Dynamic 3, 3, 0, 0, 0, DYNAMIC, &CDetourDis::CopyBytes
229
+ #define ENTRY_CopyBytes3Or5 5, 3, 0, 0, 0, 0, &CDetourDis::CopyBytes
230
+ #define ENTRY_CopyBytes3Or5Dynamic 5, 3, 0, 0, 0, DYNAMIC, &CDetourDis::CopyBytes // x86 only
231
+ #ifdef DETOURS_X64
232
+ #define ENTRY_CopyBytes3Or5Rax 5, 3, 0, 0, 0, RAX, &CDetourDis::CopyBytes
233
+ #define ENTRY_CopyBytes3Or5Target 5, 5, 0, 1, 0, 0, &CDetourDis::CopyBytes
234
+ #else
235
+ #define ENTRY_CopyBytes3Or5Rax 5, 3, 0, 0, 0, 0, &CDetourDis::CopyBytes
236
+ #define ENTRY_CopyBytes3Or5Target 5, 3, 0, 1, 0, 0, &CDetourDis::CopyBytes
237
+ #endif
238
+ #define ENTRY_CopyBytes4 4, 4, 0, 0, 0, 0, &CDetourDis::CopyBytes
239
+ #define ENTRY_CopyBytes5 5, 5, 0, 0, 0, 0, &CDetourDis::CopyBytes
240
+ #define ENTRY_CopyBytes5Or7Dynamic 7, 5, 0, 0, 0, DYNAMIC, &CDetourDis::CopyBytes
241
+ #define ENTRY_CopyBytes7 7, 7, 0, 0, 0, 0, &CDetourDis::CopyBytes
242
+ #define ENTRY_CopyBytes2Mod 2, 2, 1, 0, 0, 0, &CDetourDis::CopyBytes
243
+ #define ENTRY_CopyBytes2ModDynamic 2, 2, 1, 0, 0, DYNAMIC, &CDetourDis::CopyBytes
244
+ #define ENTRY_CopyBytes2Mod1 3, 3, 1, 0, 1, 0, &CDetourDis::CopyBytes
245
+ #define ENTRY_CopyBytes2ModOperand 6, 4, 1, 0, 4, 0, &CDetourDis::CopyBytes
246
+ #define ENTRY_CopyBytes3Mod 3, 3, 2, 0, 0, 0, &CDetourDis::CopyBytes // SSE3 0F 38 opcode modrm
247
+ #define ENTRY_CopyBytes3Mod1 4, 4, 2, 0, 1, 0, &CDetourDis::CopyBytes // SSE3 0F 3A opcode modrm .. imm8
248
+ #define ENTRY_CopyBytesPrefix ENTRY_DataIgnored &CDetourDis::CopyBytesPrefix
249
+ #define ENTRY_CopyBytesSegment ENTRY_DataIgnored &CDetourDis::CopyBytesSegment
250
+ #define ENTRY_CopyBytesRax ENTRY_DataIgnored &CDetourDis::CopyBytesRax
251
+ #define ENTRY_CopyF2 ENTRY_DataIgnored &CDetourDis::CopyF2
252
+ #define ENTRY_CopyF3 ENTRY_DataIgnored &CDetourDis::CopyF3 // 32bit x86 only
253
+ #define ENTRY_Copy0F ENTRY_DataIgnored &CDetourDis::Copy0F
254
+ #define ENTRY_Copy0F78 ENTRY_DataIgnored &CDetourDis::Copy0F78
255
+ #define ENTRY_Copy0F00 ENTRY_DataIgnored &CDetourDis::Copy0F00 // 32bit x86 only
256
+ #define ENTRY_Copy0FB8 ENTRY_DataIgnored &CDetourDis::Copy0FB8 // 32bit x86 only
257
+ #define ENTRY_Copy66 ENTRY_DataIgnored &CDetourDis::Copy66
258
+ #define ENTRY_Copy67 ENTRY_DataIgnored &CDetourDis::Copy67
259
+ #define ENTRY_CopyF6 ENTRY_DataIgnored &CDetourDis::CopyF6
260
+ #define ENTRY_CopyF7 ENTRY_DataIgnored &CDetourDis::CopyF7
261
+ #define ENTRY_CopyFF ENTRY_DataIgnored &CDetourDis::CopyFF
262
+ #define ENTRY_CopyVex2 ENTRY_DataIgnored &CDetourDis::CopyVex2
263
+ #define ENTRY_CopyVex3 ENTRY_DataIgnored &CDetourDis::CopyVex3
264
+ #define ENTRY_Invalid ENTRY_DataIgnored &CDetourDis::Invalid
265
+ #define ENTRY_End ENTRY_DataIgnored NULL
266
+
267
+ PBYTE CopyBytes(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
268
+ PBYTE CopyBytesPrefix(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
269
+ PBYTE CopyBytesSegment(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
270
+ PBYTE CopyBytesRax(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
271
+ PBYTE CopyBytesJump(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
272
+
273
+ PBYTE Invalid(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
274
+
275
+ PBYTE AdjustTarget(PBYTE pbDst, PBYTE pbSrc, UINT cbOp,
276
+ UINT cbTargetOffset, UINT cbTargetSize);
277
+
278
+ protected:
279
+ PBYTE Copy0F(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
280
+ PBYTE Copy0F00(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc); // x86 only sldt/0 str/1 lldt/2 ltr/3 err/4 verw/5 jmpe/6/dynamic invalid/7
281
+ PBYTE Copy0F78(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc); // vmread, 66/extrq/ib/ib, F2/insertq/ib/ib
282
+ PBYTE Copy0FB8(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc); // jmpe or F3/popcnt
283
+ PBYTE Copy66(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
284
+ PBYTE Copy67(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
285
+ PBYTE CopyF2(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
286
+ PBYTE CopyF3(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc); // x86 only
287
+ PBYTE CopyF6(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
288
+ PBYTE CopyF7(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
289
+ PBYTE CopyFF(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
290
+ PBYTE CopyVex2(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
291
+ PBYTE CopyVex3(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
292
+ PBYTE CopyVexCommon(BYTE m, PBYTE pbDst, PBYTE pbSrc);
293
+
294
+ protected:
295
+ static const COPYENTRY s_rceCopyTable[257];
296
+ static const COPYENTRY s_rceCopyTable0F[257];
297
+ static const BYTE s_rbModRm[256];
298
+ static PBYTE s_pbModuleBeg;
299
+ static PBYTE s_pbModuleEnd;
300
+ static BOOL s_fLimitReferencesToModule;
301
+
302
+ protected:
303
+ BOOL m_bOperandOverride;
304
+ BOOL m_bAddressOverride;
305
+ BOOL m_bRaxOverride; // AMD64 only
306
+ BOOL m_bVex;
307
+ BOOL m_bF2;
308
+ BOOL m_bF3; // x86 only
309
+ BYTE m_nSegmentOverride;
310
+
311
+ PBYTE * m_ppbTarget;
312
+ LONG * m_plExtra;
313
+
314
+ LONG m_lScratchExtra;
315
+ PBYTE m_pbScratchTarget;
316
+ BYTE m_rbScratchDst[64];
317
+ };
318
+
319
+ PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst,
320
+ _Inout_opt_ PVOID *ppDstPool,
321
+ _In_ PVOID pSrc,
322
+ _Out_opt_ PVOID *ppTarget,
323
+ _Out_opt_ LONG *plExtra)
324
+ {
325
+ UNREFERENCED_PARAMETER(ppDstPool); // x86 & x64 don't use a constant pool.
326
+
327
+ CDetourDis oDetourDisasm((PBYTE*)ppTarget, plExtra);
328
+ return oDetourDisasm.CopyInstruction((PBYTE)pDst, (PBYTE)pSrc);
329
+ }
330
+
331
+ /////////////////////////////////////////////////////////// Disassembler Code.
332
+ //
333
+ CDetourDis::CDetourDis(_Out_opt_ PBYTE *ppbTarget, _Out_opt_ LONG *plExtra)
334
+ {
335
+ m_bOperandOverride = FALSE;
336
+ m_bAddressOverride = FALSE;
337
+ m_bRaxOverride = FALSE;
338
+ m_bF2 = FALSE;
339
+ m_bF3 = FALSE;
340
+ m_bVex = FALSE;
341
+
342
+ m_ppbTarget = ppbTarget ? ppbTarget : &m_pbScratchTarget;
343
+ m_plExtra = plExtra ? plExtra : &m_lScratchExtra;
344
+
345
+ *m_ppbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_NONE;
346
+ *m_plExtra = 0;
347
+ }
348
+
349
+ PBYTE CDetourDis::CopyInstruction(PBYTE pbDst, PBYTE pbSrc)
350
+ {
351
+ // Configure scratch areas if real areas are not available.
352
+ if (NULL == pbDst) {
353
+ pbDst = m_rbScratchDst;
354
+ }
355
+ if (NULL == pbSrc) {
356
+ // We can't copy a non-existent instruction.
357
+ SetLastError(ERROR_INVALID_DATA);
358
+ return NULL;
359
+ }
360
+
361
+ // Figure out how big the instruction is, do the appropriate copy,
362
+ // and figure out what the target of the instruction is if any.
363
+ //
364
+ REFCOPYENTRY pEntry = &s_rceCopyTable[pbSrc[0]];
365
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
366
+ }
367
+
368
+ PBYTE CDetourDis::CopyBytes(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
369
+ {
370
+ UINT nBytesFixed;
371
+
372
+ ASSERT(!m_bVex || pEntry->nFlagBits == 0);
373
+ ASSERT(!m_bVex || pEntry->nFixedSize == pEntry->nFixedSize16);
374
+
375
+ UINT const nModOffset = pEntry->nModOffset;
376
+ UINT const nFlagBits = pEntry->nFlagBits;
377
+ UINT const nFixedSize = pEntry->nFixedSize;
378
+ UINT const nFixedSize16 = pEntry->nFixedSize16;
379
+
380
+ if (nFlagBits & ADDRESS) {
381
+ nBytesFixed = m_bAddressOverride ? nFixedSize16 : nFixedSize;
382
+ }
383
+ #ifdef DETOURS_X64
384
+ // REX.W trumps 66
385
+ else if (m_bRaxOverride) {
386
+ nBytesFixed = nFixedSize + ((nFlagBits & RAX) ? 4 : 0);
387
+ }
388
+ #endif
389
+ else {
390
+ nBytesFixed = m_bOperandOverride ? nFixedSize16 : nFixedSize;
391
+ }
392
+
393
+ UINT nBytes = nBytesFixed;
394
+ UINT nRelOffset = pEntry->nRelOffset;
395
+ UINT cbTarget = nBytes - nRelOffset;
396
+ if (nModOffset > 0) {
397
+ ASSERT(nRelOffset == 0);
398
+ BYTE const bModRm = pbSrc[nModOffset];
399
+ BYTE const bFlags = s_rbModRm[bModRm];
400
+
401
+ nBytes += bFlags & NOTSIB;
402
+
403
+ if (bFlags & SIB) {
404
+ BYTE const bSib = pbSrc[nModOffset + 1];
405
+
406
+ if ((bSib & 0x07) == 0x05) {
407
+ if ((bModRm & 0xc0) == 0x00) {
408
+ nBytes += 4;
409
+ }
410
+ else if ((bModRm & 0xc0) == 0x40) {
411
+ nBytes += 1;
412
+ }
413
+ else if ((bModRm & 0xc0) == 0x80) {
414
+ nBytes += 4;
415
+ }
416
+ }
417
+ cbTarget = nBytes - nRelOffset;
418
+ }
419
+ #ifdef DETOURS_X64
420
+ else if (bFlags & RIP) {
421
+ UINT nTargetBack = pEntry->nTargetBack;
422
+ // nTargetBack describes immediate bytes at the end: 1, 2, or 4.
423
+ // 2 vs. 4 is selected via 66 operand size override.
424
+ ASSERT(nTargetBack == 0 || nTargetBack == 1 || nTargetBack == 4);
425
+ if (nTargetBack == 4 && m_bOperandOverride && !m_bRaxOverride) {
426
+ nTargetBack = 2;
427
+ }
428
+
429
+ nRelOffset = nBytes - (4 + nTargetBack);
430
+ cbTarget = 4;
431
+ }
432
+ #endif
433
+ }
434
+ CopyMemory(pbDst, pbSrc, nBytes);
435
+
436
+ if (nRelOffset) {
437
+ *m_ppbTarget = AdjustTarget(pbDst, pbSrc, nBytes, nRelOffset, cbTarget);
438
+ #ifdef DETOURS_X64
439
+ if (pEntry->nRelOffset == 0) {
440
+ // This is a data target, not a code target, so we shouldn't return it.
441
+ *m_ppbTarget = NULL;
442
+ }
443
+ #endif
444
+ }
445
+ if (nFlagBits & NOENLARGE) {
446
+ *m_plExtra = -*m_plExtra;
447
+ }
448
+ if (nFlagBits & DYNAMIC) {
449
+ *m_ppbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
450
+ }
451
+ return pbSrc + nBytes;
452
+ }
453
+
454
+ PBYTE CDetourDis::CopyBytesPrefix(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
455
+ {
456
+ pbDst[0] = pbSrc[0];
457
+ pEntry = &s_rceCopyTable[pbSrc[1]];
458
+ return (this->*pEntry->pfCopy)(pEntry, pbDst + 1, pbSrc + 1);
459
+ }
460
+
461
+ PBYTE CDetourDis::CopyBytesSegment(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
462
+ {
463
+ m_nSegmentOverride = pbSrc[0];
464
+ return CopyBytesPrefix(0, pbDst, pbSrc);
465
+ }
466
+
467
+ PBYTE CDetourDis::CopyBytesRax(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
468
+ { // AMD64 only
469
+ if (pbSrc[0] & 0x8) {
470
+ m_bRaxOverride = TRUE;
471
+ }
472
+ return CopyBytesPrefix(0, pbDst, pbSrc);
473
+ }
474
+
475
+ PBYTE CDetourDis::CopyBytesJump(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
476
+ {
477
+ (void)pEntry;
478
+
479
+ PVOID pvSrcAddr = &pbSrc[1];
480
+ PVOID pvDstAddr = NULL;
481
+ LONG_PTR nOldOffset = (LONG_PTR)*(signed char*&)pvSrcAddr;
482
+ LONG_PTR nNewOffset = 0;
483
+
484
+ *m_ppbTarget = pbSrc + 2 + nOldOffset;
485
+
486
+ if (pbSrc[0] == 0xeb) {
487
+ pbDst[0] = 0xe9;
488
+ pvDstAddr = &pbDst[1];
489
+ nNewOffset = nOldOffset - ((pbDst - pbSrc) + 3);
490
+ *(UNALIGNED LONG*&)pvDstAddr = (LONG)nNewOffset;
491
+
492
+ *m_plExtra = 3;
493
+ return pbSrc + 2;
494
+ }
495
+
496
+ ASSERT(pbSrc[0] >= 0x70 && pbSrc[0] <= 0x7f);
497
+
498
+ pbDst[0] = 0x0f;
499
+ pbDst[1] = 0x80 | (pbSrc[0] & 0xf);
500
+ pvDstAddr = &pbDst[2];
501
+ nNewOffset = nOldOffset - ((pbDst - pbSrc) + 4);
502
+ *(UNALIGNED LONG*&)pvDstAddr = (LONG)nNewOffset;
503
+
504
+ *m_plExtra = 4;
505
+ return pbSrc + 2;
506
+ }
507
+
508
+ PBYTE CDetourDis::AdjustTarget(PBYTE pbDst, PBYTE pbSrc, UINT cbOp,
509
+ UINT cbTargetOffset, UINT cbTargetSize)
510
+ {
511
+ PBYTE pbTarget = NULL;
512
+ #if 1 // fault injection to test test code
513
+ #if defined(DETOURS_X64)
514
+ typedef LONGLONG T;
515
+ #else
516
+ typedef LONG T;
517
+ #endif
518
+ T nOldOffset;
519
+ T nNewOffset;
520
+ PVOID pvTargetAddr = &pbDst[cbTargetOffset];
521
+
522
+ switch (cbTargetSize) {
523
+ case 1:
524
+ nOldOffset = *(signed char*&)pvTargetAddr;
525
+ break;
526
+ case 2:
527
+ nOldOffset = *(UNALIGNED SHORT*&)pvTargetAddr;
528
+ break;
529
+ case 4:
530
+ nOldOffset = *(UNALIGNED LONG*&)pvTargetAddr;
531
+ break;
532
+ #if defined(DETOURS_X64)
533
+ case 8:
534
+ nOldOffset = *(UNALIGNED LONGLONG*&)pvTargetAddr;
535
+ break;
536
+ #endif
537
+ default:
538
+ ASSERT(!"cbTargetSize is invalid.");
539
+ nOldOffset = 0;
540
+ break;
541
+ }
542
+
543
+ pbTarget = pbSrc + cbOp + nOldOffset;
544
+ nNewOffset = nOldOffset - (T)(pbDst - pbSrc);
545
+
546
+ switch (cbTargetSize) {
547
+ case 1:
548
+ *(CHAR*&)pvTargetAddr = (CHAR)nNewOffset;
549
+ if (nNewOffset < SCHAR_MIN || nNewOffset > SCHAR_MAX) {
550
+ *m_plExtra = sizeof(ULONG) - 1;
551
+ }
552
+ break;
553
+ case 2:
554
+ *(UNALIGNED SHORT*&)pvTargetAddr = (SHORT)nNewOffset;
555
+ if (nNewOffset < SHRT_MIN || nNewOffset > SHRT_MAX) {
556
+ *m_plExtra = sizeof(ULONG) - 2;
557
+ }
558
+ break;
559
+ case 4:
560
+ *(UNALIGNED LONG*&)pvTargetAddr = (LONG)nNewOffset;
561
+ if (nNewOffset < LONG_MIN || nNewOffset > LONG_MAX) {
562
+ *m_plExtra = sizeof(ULONG) - 4;
563
+ }
564
+ break;
565
+ #if defined(DETOURS_X64)
566
+ case 8:
567
+ *(UNALIGNED LONGLONG*&)pvTargetAddr = nNewOffset;
568
+ break;
569
+ #endif
570
+ }
571
+ #ifdef DETOURS_X64
572
+ // When we are only computing size, source and dest can be
573
+ // far apart, distance not encodable in 32bits. Ok.
574
+ // At least still check the lower 32bits.
575
+
576
+ if (pbDst >= m_rbScratchDst && pbDst < (sizeof(m_rbScratchDst) + m_rbScratchDst)) {
577
+ ASSERT((((size_t)pbDst + cbOp + nNewOffset) & 0xFFFFFFFF) == (((size_t)pbTarget) & 0xFFFFFFFF));
578
+ }
579
+ else
580
+ #endif
581
+ {
582
+ ASSERT(pbDst + cbOp + nNewOffset == pbTarget);
583
+ }
584
+ #endif
585
+ return pbTarget;
586
+ }
587
+
588
+ PBYTE CDetourDis::Invalid(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
589
+ {
590
+ (void)pbDst;
591
+ (void)pEntry;
592
+ ASSERT(!"Invalid Instruction");
593
+ return pbSrc + 1;
594
+ }
595
+
596
+ ////////////////////////////////////////////////////// Individual Bytes Codes.
597
+ //
598
+ PBYTE CDetourDis::Copy0F(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
599
+ {
600
+ pbDst[0] = pbSrc[0];
601
+ pEntry = &s_rceCopyTable0F[pbSrc[1]];
602
+ return (this->*pEntry->pfCopy)(pEntry, pbDst + 1, pbSrc + 1);
603
+ }
604
+
605
+ PBYTE CDetourDis::Copy0F78(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
606
+ {
607
+ // vmread, 66/extrq, F2/insertq
608
+
609
+ static const COPYENTRY vmread = { 0x78, ENTRY_CopyBytes2Mod };
610
+ static const COPYENTRY extrq_insertq = { 0x78, ENTRY_CopyBytes4 };
611
+
612
+ ASSERT(!(m_bF2 && m_bOperandOverride));
613
+
614
+ // For insertq and presumably despite documentation extrq, mode must be 11, not checked.
615
+ // insertq/extrq/78 are followed by two immediate bytes, and given mode == 11, mod/rm byte is always one byte,
616
+ // and the 0x78 makes 4 bytes (not counting the 66/F2/F which are accounted for elsewhere)
617
+
618
+ REFCOPYENTRY const pEntry = ((m_bF2 || m_bOperandOverride) ? &extrq_insertq : &vmread);
619
+
620
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
621
+ }
622
+
623
+ PBYTE CDetourDis::Copy0F00(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
624
+ {
625
+ // jmpe is 32bit x86 only
626
+ // Notice that the sizes are the same either way, but jmpe is marked as "dynamic".
627
+
628
+ static const COPYENTRY other = { 0xB8, ENTRY_CopyBytes2Mod }; // sldt/0 str/1 lldt/2 ltr/3 err/4 verw/5 jmpe/6 invalid/7
629
+ static const COPYENTRY jmpe = { 0xB8, ENTRY_CopyBytes2ModDynamic }; // jmpe/6 x86-on-IA64 syscalls
630
+
631
+ REFCOPYENTRY const pEntry = (((6 << 3) == ((7 << 3) & pbSrc[1])) ? &jmpe : &other);
632
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
633
+ }
634
+
635
+ PBYTE CDetourDis::Copy0FB8(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
636
+ {
637
+ // jmpe is 32bit x86 only
638
+
639
+ static const COPYENTRY popcnt = { 0xB8, ENTRY_CopyBytes2Mod };
640
+ static const COPYENTRY jmpe = { 0xB8, ENTRY_CopyBytes3Or5Dynamic }; // jmpe x86-on-IA64 syscalls
641
+ REFCOPYENTRY const pEntry = m_bF3 ? &popcnt : &jmpe;
642
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
643
+ }
644
+
645
+ PBYTE CDetourDis::Copy66(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
646
+ { // Operand-size override prefix
647
+ m_bOperandOverride = TRUE;
648
+ return CopyBytesPrefix(pEntry, pbDst, pbSrc);
649
+ }
650
+
651
+ PBYTE CDetourDis::Copy67(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
652
+ { // Address size override prefix
653
+ m_bAddressOverride = TRUE;
654
+ return CopyBytesPrefix(pEntry, pbDst, pbSrc);
655
+ }
656
+
657
+ PBYTE CDetourDis::CopyF2(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
658
+ {
659
+ m_bF2 = TRUE;
660
+ return CopyBytesPrefix(pEntry, pbDst, pbSrc);
661
+ }
662
+
663
+ PBYTE CDetourDis::CopyF3(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
664
+ { // x86 only
665
+ m_bF3 = TRUE;
666
+ return CopyBytesPrefix(pEntry, pbDst, pbSrc);
667
+ }
668
+
669
+ PBYTE CDetourDis::CopyF6(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
670
+ {
671
+ (void)pEntry;
672
+
673
+ // TEST BYTE /0
674
+ if (0x00 == (0x38 & pbSrc[1])) { // reg(bits 543) of ModR/M == 0
675
+ static const COPYENTRY ce = { 0xf6, ENTRY_CopyBytes2Mod1 };
676
+ return (this->*ce.pfCopy)(&ce, pbDst, pbSrc);
677
+ }
678
+ // DIV /6
679
+ // IDIV /7
680
+ // IMUL /5
681
+ // MUL /4
682
+ // NEG /3
683
+ // NOT /2
684
+
685
+ static const COPYENTRY ce = { 0xf6, ENTRY_CopyBytes2Mod };
686
+ return (this->*ce.pfCopy)(&ce, pbDst, pbSrc);
687
+ }
688
+
689
+ PBYTE CDetourDis::CopyF7(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
690
+ {
691
+ (void)pEntry;
692
+
693
+ // TEST WORD /0
694
+ if (0x00 == (0x38 & pbSrc[1])) { // reg(bits 543) of ModR/M == 0
695
+ static const COPYENTRY ce = { 0xf7, ENTRY_CopyBytes2ModOperand };
696
+ return (this->*ce.pfCopy)(&ce, pbDst, pbSrc);
697
+ }
698
+
699
+ // DIV /6
700
+ // IDIV /7
701
+ // IMUL /5
702
+ // MUL /4
703
+ // NEG /3
704
+ // NOT /2
705
+ static const COPYENTRY ce = { 0xf7, ENTRY_CopyBytes2Mod };
706
+ return (this->*ce.pfCopy)(&ce, pbDst, pbSrc);
707
+ }
708
+
709
+ PBYTE CDetourDis::CopyFF(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc)
710
+ { // INC /0
711
+ // DEC /1
712
+ // CALL /2
713
+ // CALL /3
714
+ // JMP /4
715
+ // JMP /5
716
+ // PUSH /6
717
+ // invalid/7
718
+ (void)pEntry;
719
+
720
+ static const COPYENTRY ce = { 0xff, ENTRY_CopyBytes2Mod };
721
+ PBYTE pbOut = (this->*ce.pfCopy)(&ce, pbDst, pbSrc);
722
+
723
+ BYTE const b1 = pbSrc[1];
724
+
725
+ if (0x15 == b1 || 0x25 == b1) { // CALL [], JMP []
726
+ #ifdef DETOURS_X64
727
+ // All segments but FS and GS are equivalent.
728
+ if (m_nSegmentOverride != 0x64 && m_nSegmentOverride != 0x65)
729
+ #else
730
+ if (m_nSegmentOverride == 0 || m_nSegmentOverride == 0x2E)
731
+ #endif
732
+ {
733
+ #ifdef DETOURS_X64
734
+ INT32 offset = *(UNALIGNED INT32*)&pbSrc[2];
735
+ PBYTE *ppbTarget = (PBYTE *)(pbSrc + 6 + offset);
736
+ #else
737
+ PBYTE *ppbTarget = (PBYTE *)(SIZE_T)*(UNALIGNED ULONG*)&pbSrc[2];
738
+ #endif
739
+ if (s_fLimitReferencesToModule &&
740
+ (ppbTarget < (PVOID)s_pbModuleBeg || ppbTarget >= (PVOID)s_pbModuleEnd)) {
741
+
742
+ *m_ppbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
743
+ }
744
+ else {
745
+ // This can access violate on random bytes. Use DetourSetCodeModule.
746
+ *m_ppbTarget = *ppbTarget;
747
+ }
748
+ }
749
+ else {
750
+ *m_ppbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
751
+ }
752
+ }
753
+ else if (0x10 == (0x30 & b1) || // CALL /2 or /3 --> reg(bits 543) of ModR/M == 010 or 011
754
+ 0x20 == (0x30 & b1)) { // JMP /4 or /5 --> reg(bits 543) of ModR/M == 100 or 101
755
+ *m_ppbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
756
+ }
757
+ return pbOut;
758
+ }
759
+
760
+ PBYTE CDetourDis::CopyVexCommon(BYTE m, PBYTE pbDst, PBYTE pbSrc)
761
+ // m is first instead of last in the hopes of pbDst/pbSrc being
762
+ // passed along efficiently in the registers they were already in.
763
+ {
764
+ static const COPYENTRY ceF38 = { 0x38, ENTRY_CopyBytes2Mod };
765
+ static const COPYENTRY ceF3A = { 0x3A, ENTRY_CopyBytes2Mod1 };
766
+ static const COPYENTRY Invalid = { 0xC4, ENTRY_Invalid };
767
+
768
+ m_bVex = TRUE;
769
+ REFCOPYENTRY pEntry;
770
+ switch (m) {
771
+ default: pEntry = &Invalid; break;
772
+ case 1: pEntry = &s_rceCopyTable0F[pbSrc[0]]; break;
773
+ case 2: pEntry = &ceF38; break;
774
+ case 3: pEntry = &ceF3A; break;
775
+ }
776
+
777
+ switch (pbSrc[-1] & 3) { // p in last byte
778
+ case 0: break;
779
+ case 1: m_bOperandOverride = TRUE; break;
780
+ case 2: m_bF3 = TRUE; break;
781
+ case 3: m_bF2 = TRUE; break;
782
+ }
783
+
784
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
785
+ }
786
+
787
+ PBYTE CDetourDis::CopyVex3(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
788
+ // 3 byte VEX prefix 0xC4
789
+ {
790
+ #ifdef DETOURS_X86
791
+ const static COPYENTRY ceLES = { 0xC4, ENTRY_CopyBytes2Mod };
792
+ if ((pbSrc[1] & 0xC0) != 0xC0) {
793
+ REFCOPYENTRY pEntry = &ceLES;
794
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
795
+ }
796
+ #endif
797
+ pbDst[0] = pbSrc[0];
798
+ pbDst[1] = pbSrc[1];
799
+ pbDst[2] = pbSrc[2];
800
+ #ifdef DETOURS_X64
801
+ m_bRaxOverride |= !!(pbSrc[2] & 0x80); // w in last byte, see CopyBytesRax
802
+ #else
803
+ //
804
+ // TODO
805
+ //
806
+ // Usually the VEX.W bit changes the size of a general purpose register and is ignored for 32bit.
807
+ // Sometimes it is an opcode extension.
808
+ // Look in the Intel manual, in the instruction-by-instruction reference, for ".W1",
809
+ // without nearby wording saying it is ignored for 32bit.
810
+ // For example: "VFMADD132PD/VFMADD213PD/VFMADD231PD Fused Multiply-Add of Packed Double-Precision Floating-Point Values".
811
+ //
812
+ // Then, go through each such case and determine if W0 vs. W1 affect the size of the instruction. Probably not.
813
+ // Look for the same encoding but with "W1" changed to "W0".
814
+ // Here is one such pairing:
815
+ // VFMADD132PD/VFMADD213PD/VFMADD231PD Fused Multiply-Add of Packed Double-Precision Floating-Point Values
816
+ //
817
+ // VEX.DDS.128.66.0F38.W1 98 /r A V/V FMA Multiply packed double-precision floating-point values
818
+ // from xmm0 and xmm2/mem, add to xmm1 and
819
+ // put result in xmm0.
820
+ // VFMADD132PD xmm0, xmm1, xmm2/m128
821
+ //
822
+ // VFMADD132PS/VFMADD213PS/VFMADD231PS Fused Multiply-Add of Packed Single-Precision Floating-Point Values
823
+ // VEX.DDS.128.66.0F38.W0 98 /r A V/V FMA Multiply packed single-precision floating-point values
824
+ // from xmm0 and xmm2/mem, add to xmm1 and put
825
+ // result in xmm0.
826
+ // VFMADD132PS xmm0, xmm1, xmm2/m128
827
+ //
828
+ #endif
829
+ return CopyVexCommon(pbSrc[1] & 0x1F, pbDst + 3, pbSrc + 3);
830
+ }
831
+
832
+ PBYTE CDetourDis::CopyVex2(REFCOPYENTRY, PBYTE pbDst, PBYTE pbSrc)
833
+ // 2 byte VEX prefix 0xC5
834
+ {
835
+ #ifdef DETOURS_X86
836
+ const static COPYENTRY ceLDS = { 0xC5, ENTRY_CopyBytes2Mod };
837
+ if ((pbSrc[1] & 0xC0) != 0xC0) {
838
+ REFCOPYENTRY pEntry = &ceLDS;
839
+ return (this->*pEntry->pfCopy)(pEntry, pbDst, pbSrc);
840
+ }
841
+ #endif
842
+ pbDst[0] = pbSrc[0];
843
+ pbDst[1] = pbSrc[1];
844
+ return CopyVexCommon(1, pbDst + 2, pbSrc + 2);
845
+ }
846
+
847
+ //////////////////////////////////////////////////////////////////////////////
848
+ //
849
+ PBYTE CDetourDis::s_pbModuleBeg = NULL;
850
+ PBYTE CDetourDis::s_pbModuleEnd = (PBYTE)~(ULONG_PTR)0;
851
+ BOOL CDetourDis::s_fLimitReferencesToModule = FALSE;
852
+
853
+ BOOL CDetourDis::SetCodeModule(PBYTE pbBeg, PBYTE pbEnd, BOOL fLimitReferencesToModule)
854
+ {
855
+ if (pbEnd < pbBeg) {
856
+ return FALSE;
857
+ }
858
+
859
+ s_pbModuleBeg = pbBeg;
860
+ s_pbModuleEnd = pbEnd;
861
+ s_fLimitReferencesToModule = fLimitReferencesToModule;
862
+
863
+ return TRUE;
864
+ }
865
+
866
+ ///////////////////////////////////////////////////////// Disassembler Tables.
867
+ //
868
+ const BYTE CDetourDis::s_rbModRm[256] = {
869
+ 0,0,0,0, SIB|1,RIP|4,0,0, 0,0,0,0, SIB|1,RIP|4,0,0, // 0x
870
+ 0,0,0,0, SIB|1,RIP|4,0,0, 0,0,0,0, SIB|1,RIP|4,0,0, // 1x
871
+ 0,0,0,0, SIB|1,RIP|4,0,0, 0,0,0,0, SIB|1,RIP|4,0,0, // 2x
872
+ 0,0,0,0, SIB|1,RIP|4,0,0, 0,0,0,0, SIB|1,RIP|4,0,0, // 3x
873
+ 1,1,1,1, 2,1,1,1, 1,1,1,1, 2,1,1,1, // 4x
874
+ 1,1,1,1, 2,1,1,1, 1,1,1,1, 2,1,1,1, // 5x
875
+ 1,1,1,1, 2,1,1,1, 1,1,1,1, 2,1,1,1, // 6x
876
+ 1,1,1,1, 2,1,1,1, 1,1,1,1, 2,1,1,1, // 7x
877
+ 4,4,4,4, 5,4,4,4, 4,4,4,4, 5,4,4,4, // 8x
878
+ 4,4,4,4, 5,4,4,4, 4,4,4,4, 5,4,4,4, // 9x
879
+ 4,4,4,4, 5,4,4,4, 4,4,4,4, 5,4,4,4, // Ax
880
+ 4,4,4,4, 5,4,4,4, 4,4,4,4, 5,4,4,4, // Bx
881
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // Cx
882
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // Dx
883
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // Ex
884
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 // Fx
885
+ };
886
+
887
+ const CDetourDis::COPYENTRY CDetourDis::s_rceCopyTable[257] =
888
+ {
889
+ { 0x00, ENTRY_CopyBytes2Mod }, // ADD /r
890
+ { 0x01, ENTRY_CopyBytes2Mod }, // ADD /r
891
+ { 0x02, ENTRY_CopyBytes2Mod }, // ADD /r
892
+ { 0x03, ENTRY_CopyBytes2Mod }, // ADD /r
893
+ { 0x04, ENTRY_CopyBytes2 }, // ADD ib
894
+ { 0x05, ENTRY_CopyBytes3Or5 }, // ADD iw
895
+ #ifdef DETOURS_X64
896
+ { 0x06, ENTRY_Invalid }, // Invalid
897
+ { 0x07, ENTRY_Invalid }, // Invalid
898
+ #else
899
+ { 0x06, ENTRY_CopyBytes1 }, // PUSH
900
+ { 0x07, ENTRY_CopyBytes1 }, // POP
901
+ #endif
902
+ { 0x08, ENTRY_CopyBytes2Mod }, // OR /r
903
+ { 0x09, ENTRY_CopyBytes2Mod }, // OR /r
904
+ { 0x0A, ENTRY_CopyBytes2Mod }, // OR /r
905
+ { 0x0B, ENTRY_CopyBytes2Mod }, // OR /r
906
+ { 0x0C, ENTRY_CopyBytes2 }, // OR ib
907
+ { 0x0D, ENTRY_CopyBytes3Or5 }, // OR iw
908
+ #ifdef DETOURS_X64
909
+ { 0x0E, ENTRY_Invalid }, // Invalid
910
+ #else
911
+ { 0x0E, ENTRY_CopyBytes1 }, // PUSH
912
+ #endif
913
+ { 0x0F, ENTRY_Copy0F }, // Extension Ops
914
+ { 0x10, ENTRY_CopyBytes2Mod }, // ADC /r
915
+ { 0x11, ENTRY_CopyBytes2Mod }, // ADC /r
916
+ { 0x12, ENTRY_CopyBytes2Mod }, // ADC /r
917
+ { 0x13, ENTRY_CopyBytes2Mod }, // ADC /r
918
+ { 0x14, ENTRY_CopyBytes2 }, // ADC ib
919
+ { 0x15, ENTRY_CopyBytes3Or5 }, // ADC id
920
+ #ifdef DETOURS_X64
921
+ { 0x16, ENTRY_Invalid }, // Invalid
922
+ { 0x17, ENTRY_Invalid }, // Invalid
923
+ #else
924
+ { 0x16, ENTRY_CopyBytes1 }, // PUSH
925
+ { 0x17, ENTRY_CopyBytes1 }, // POP
926
+ #endif
927
+ { 0x18, ENTRY_CopyBytes2Mod }, // SBB /r
928
+ { 0x19, ENTRY_CopyBytes2Mod }, // SBB /r
929
+ { 0x1A, ENTRY_CopyBytes2Mod }, // SBB /r
930
+ { 0x1B, ENTRY_CopyBytes2Mod }, // SBB /r
931
+ { 0x1C, ENTRY_CopyBytes2 }, // SBB ib
932
+ { 0x1D, ENTRY_CopyBytes3Or5 }, // SBB id
933
+ #ifdef DETOURS_X64
934
+ { 0x1E, ENTRY_Invalid }, // Invalid
935
+ { 0x1F, ENTRY_Invalid }, // Invalid
936
+ #else
937
+ { 0x1E, ENTRY_CopyBytes1 }, // PUSH
938
+ { 0x1F, ENTRY_CopyBytes1 }, // POP
939
+ #endif
940
+ { 0x20, ENTRY_CopyBytes2Mod }, // AND /r
941
+ { 0x21, ENTRY_CopyBytes2Mod }, // AND /r
942
+ { 0x22, ENTRY_CopyBytes2Mod }, // AND /r
943
+ { 0x23, ENTRY_CopyBytes2Mod }, // AND /r
944
+ { 0x24, ENTRY_CopyBytes2 }, // AND ib
945
+ { 0x25, ENTRY_CopyBytes3Or5 }, // AND id
946
+ { 0x26, ENTRY_CopyBytesSegment }, // ES prefix
947
+ #ifdef DETOURS_X64
948
+ { 0x27, ENTRY_Invalid }, // Invalid
949
+ #else
950
+ { 0x27, ENTRY_CopyBytes1 }, // DAA
951
+ #endif
952
+ { 0x28, ENTRY_CopyBytes2Mod }, // SUB /r
953
+ { 0x29, ENTRY_CopyBytes2Mod }, // SUB /r
954
+ { 0x2A, ENTRY_CopyBytes2Mod }, // SUB /r
955
+ { 0x2B, ENTRY_CopyBytes2Mod }, // SUB /r
956
+ { 0x2C, ENTRY_CopyBytes2 }, // SUB ib
957
+ { 0x2D, ENTRY_CopyBytes3Or5 }, // SUB id
958
+ { 0x2E, ENTRY_CopyBytesSegment }, // CS prefix
959
+ #ifdef DETOURS_X64
960
+ { 0x2F, ENTRY_Invalid }, // Invalid
961
+ #else
962
+ { 0x2F, ENTRY_CopyBytes1 }, // DAS
963
+ #endif
964
+ { 0x30, ENTRY_CopyBytes2Mod }, // XOR /r
965
+ { 0x31, ENTRY_CopyBytes2Mod }, // XOR /r
966
+ { 0x32, ENTRY_CopyBytes2Mod }, // XOR /r
967
+ { 0x33, ENTRY_CopyBytes2Mod }, // XOR /r
968
+ { 0x34, ENTRY_CopyBytes2 }, // XOR ib
969
+ { 0x35, ENTRY_CopyBytes3Or5 }, // XOR id
970
+ { 0x36, ENTRY_CopyBytesSegment }, // SS prefix
971
+ #ifdef DETOURS_X64
972
+ { 0x37, ENTRY_Invalid }, // Invalid
973
+ #else
974
+ { 0x37, ENTRY_CopyBytes1 }, // AAA
975
+ #endif
976
+ { 0x38, ENTRY_CopyBytes2Mod }, // CMP /r
977
+ { 0x39, ENTRY_CopyBytes2Mod }, // CMP /r
978
+ { 0x3A, ENTRY_CopyBytes2Mod }, // CMP /r
979
+ { 0x3B, ENTRY_CopyBytes2Mod }, // CMP /r
980
+ { 0x3C, ENTRY_CopyBytes2 }, // CMP ib
981
+ { 0x3D, ENTRY_CopyBytes3Or5 }, // CMP id
982
+ { 0x3E, ENTRY_CopyBytesSegment }, // DS prefix
983
+ #ifdef DETOURS_X64
984
+ { 0x3F, ENTRY_Invalid }, // Invalid
985
+ #else
986
+ { 0x3F, ENTRY_CopyBytes1 }, // AAS
987
+ #endif
988
+ #ifdef DETOURS_X64 // For Rax Prefix
989
+ { 0x40, ENTRY_CopyBytesRax }, // Rax
990
+ { 0x41, ENTRY_CopyBytesRax }, // Rax
991
+ { 0x42, ENTRY_CopyBytesRax }, // Rax
992
+ { 0x43, ENTRY_CopyBytesRax }, // Rax
993
+ { 0x44, ENTRY_CopyBytesRax }, // Rax
994
+ { 0x45, ENTRY_CopyBytesRax }, // Rax
995
+ { 0x46, ENTRY_CopyBytesRax }, // Rax
996
+ { 0x47, ENTRY_CopyBytesRax }, // Rax
997
+ { 0x48, ENTRY_CopyBytesRax }, // Rax
998
+ { 0x49, ENTRY_CopyBytesRax }, // Rax
999
+ { 0x4A, ENTRY_CopyBytesRax }, // Rax
1000
+ { 0x4B, ENTRY_CopyBytesRax }, // Rax
1001
+ { 0x4C, ENTRY_CopyBytesRax }, // Rax
1002
+ { 0x4D, ENTRY_CopyBytesRax }, // Rax
1003
+ { 0x4E, ENTRY_CopyBytesRax }, // Rax
1004
+ { 0x4F, ENTRY_CopyBytesRax }, // Rax
1005
+ #else
1006
+ { 0x40, ENTRY_CopyBytes1 }, // INC
1007
+ { 0x41, ENTRY_CopyBytes1 }, // INC
1008
+ { 0x42, ENTRY_CopyBytes1 }, // INC
1009
+ { 0x43, ENTRY_CopyBytes1 }, // INC
1010
+ { 0x44, ENTRY_CopyBytes1 }, // INC
1011
+ { 0x45, ENTRY_CopyBytes1 }, // INC
1012
+ { 0x46, ENTRY_CopyBytes1 }, // INC
1013
+ { 0x47, ENTRY_CopyBytes1 }, // INC
1014
+ { 0x48, ENTRY_CopyBytes1 }, // DEC
1015
+ { 0x49, ENTRY_CopyBytes1 }, // DEC
1016
+ { 0x4A, ENTRY_CopyBytes1 }, // DEC
1017
+ { 0x4B, ENTRY_CopyBytes1 }, // DEC
1018
+ { 0x4C, ENTRY_CopyBytes1 }, // DEC
1019
+ { 0x4D, ENTRY_CopyBytes1 }, // DEC
1020
+ { 0x4E, ENTRY_CopyBytes1 }, // DEC
1021
+ { 0x4F, ENTRY_CopyBytes1 }, // DEC
1022
+ #endif
1023
+ { 0x50, ENTRY_CopyBytes1 }, // PUSH
1024
+ { 0x51, ENTRY_CopyBytes1 }, // PUSH
1025
+ { 0x52, ENTRY_CopyBytes1 }, // PUSH
1026
+ { 0x53, ENTRY_CopyBytes1 }, // PUSH
1027
+ { 0x54, ENTRY_CopyBytes1 }, // PUSH
1028
+ { 0x55, ENTRY_CopyBytes1 }, // PUSH
1029
+ { 0x56, ENTRY_CopyBytes1 }, // PUSH
1030
+ { 0x57, ENTRY_CopyBytes1 }, // PUSH
1031
+ { 0x58, ENTRY_CopyBytes1 }, // POP
1032
+ { 0x59, ENTRY_CopyBytes1 }, // POP
1033
+ { 0x5A, ENTRY_CopyBytes1 }, // POP
1034
+ { 0x5B, ENTRY_CopyBytes1 }, // POP
1035
+ { 0x5C, ENTRY_CopyBytes1 }, // POP
1036
+ { 0x5D, ENTRY_CopyBytes1 }, // POP
1037
+ { 0x5E, ENTRY_CopyBytes1 }, // POP
1038
+ { 0x5F, ENTRY_CopyBytes1 }, // POP
1039
+ #ifdef DETOURS_X64
1040
+ { 0x60, ENTRY_Invalid }, // Invalid
1041
+ { 0x61, ENTRY_Invalid }, // Invalid
1042
+ { 0x62, ENTRY_Invalid }, // Invalid (not yet implemented Intel EVEX support)
1043
+ #else
1044
+ { 0x60, ENTRY_CopyBytes1 }, // PUSHAD
1045
+ { 0x61, ENTRY_CopyBytes1 }, // POPAD
1046
+ { 0x62, ENTRY_CopyBytes2Mod }, // BOUND /r
1047
+ #endif
1048
+ { 0x63, ENTRY_CopyBytes2Mod }, // 32bit ARPL /r, 64bit MOVSXD
1049
+ { 0x64, ENTRY_CopyBytesSegment }, // FS prefix
1050
+ { 0x65, ENTRY_CopyBytesSegment }, // GS prefix
1051
+ { 0x66, ENTRY_Copy66 }, // Operand Prefix
1052
+ { 0x67, ENTRY_Copy67 }, // Address Prefix
1053
+ { 0x68, ENTRY_CopyBytes3Or5 }, // PUSH
1054
+ { 0x69, ENTRY_CopyBytes2ModOperand }, // IMUL /r iz
1055
+ { 0x6A, ENTRY_CopyBytes2 }, // PUSH
1056
+ { 0x6B, ENTRY_CopyBytes2Mod1 }, // IMUL /r ib
1057
+ { 0x6C, ENTRY_CopyBytes1 }, // INS
1058
+ { 0x6D, ENTRY_CopyBytes1 }, // INS
1059
+ { 0x6E, ENTRY_CopyBytes1 }, // OUTS/OUTSB
1060
+ { 0x6F, ENTRY_CopyBytes1 }, // OUTS/OUTSW
1061
+ { 0x70, ENTRY_CopyBytes2Jump }, // JO // 0f80
1062
+ { 0x71, ENTRY_CopyBytes2Jump }, // JNO // 0f81
1063
+ { 0x72, ENTRY_CopyBytes2Jump }, // JB/JC/JNAE // 0f82
1064
+ { 0x73, ENTRY_CopyBytes2Jump }, // JAE/JNB/JNC // 0f83
1065
+ { 0x74, ENTRY_CopyBytes2Jump }, // JE/JZ // 0f84
1066
+ { 0x75, ENTRY_CopyBytes2Jump }, // JNE/JNZ // 0f85
1067
+ { 0x76, ENTRY_CopyBytes2Jump }, // JBE/JNA // 0f86
1068
+ { 0x77, ENTRY_CopyBytes2Jump }, // JA/JNBE // 0f87
1069
+ { 0x78, ENTRY_CopyBytes2Jump }, // JS // 0f88
1070
+ { 0x79, ENTRY_CopyBytes2Jump }, // JNS // 0f89
1071
+ { 0x7A, ENTRY_CopyBytes2Jump }, // JP/JPE // 0f8a
1072
+ { 0x7B, ENTRY_CopyBytes2Jump }, // JNP/JPO // 0f8b
1073
+ { 0x7C, ENTRY_CopyBytes2Jump }, // JL/JNGE // 0f8c
1074
+ { 0x7D, ENTRY_CopyBytes2Jump }, // JGE/JNL // 0f8d
1075
+ { 0x7E, ENTRY_CopyBytes2Jump }, // JLE/JNG // 0f8e
1076
+ { 0x7F, ENTRY_CopyBytes2Jump }, // JG/JNLE // 0f8f
1077
+ { 0x80, ENTRY_CopyBytes2Mod1 }, // ADD/0 OR/1 ADC/2 SBB/3 AND/4 SUB/5 XOR/6 CMP/7 byte reg, immediate byte
1078
+ { 0x81, ENTRY_CopyBytes2ModOperand }, // ADD/0 OR/1 ADC/2 SBB/3 AND/4 SUB/5 XOR/6 CMP/7 byte reg, immediate word or dword
1079
+ #ifdef DETOURS_X64
1080
+ { 0x82, ENTRY_Invalid }, // Invalid
1081
+ #else
1082
+ { 0x82, ENTRY_CopyBytes2Mod1 }, // MOV al,x
1083
+ #endif
1084
+ { 0x83, ENTRY_CopyBytes2Mod1 }, // ADD/0 OR/1 ADC/2 SBB/3 AND/4 SUB/5 XOR/6 CMP/7 reg, immediate byte
1085
+ { 0x84, ENTRY_CopyBytes2Mod }, // TEST /r
1086
+ { 0x85, ENTRY_CopyBytes2Mod }, // TEST /r
1087
+ { 0x86, ENTRY_CopyBytes2Mod }, // XCHG /r @todo
1088
+ { 0x87, ENTRY_CopyBytes2Mod }, // XCHG /r @todo
1089
+ { 0x88, ENTRY_CopyBytes2Mod }, // MOV /r
1090
+ { 0x89, ENTRY_CopyBytes2Mod }, // MOV /r
1091
+ { 0x8A, ENTRY_CopyBytes2Mod }, // MOV /r
1092
+ { 0x8B, ENTRY_CopyBytes2Mod }, // MOV /r
1093
+ { 0x8C, ENTRY_CopyBytes2Mod }, // MOV /r
1094
+ { 0x8D, ENTRY_CopyBytes2Mod }, // LEA /r
1095
+ { 0x8E, ENTRY_CopyBytes2Mod }, // MOV /r
1096
+ { 0x8F, ENTRY_CopyBytes2Mod }, // POP /0
1097
+ { 0x90, ENTRY_CopyBytes1 }, // NOP
1098
+ { 0x91, ENTRY_CopyBytes1 }, // XCHG
1099
+ { 0x92, ENTRY_CopyBytes1 }, // XCHG
1100
+ { 0x93, ENTRY_CopyBytes1 }, // XCHG
1101
+ { 0x94, ENTRY_CopyBytes1 }, // XCHG
1102
+ { 0x95, ENTRY_CopyBytes1 }, // XCHG
1103
+ { 0x96, ENTRY_CopyBytes1 }, // XCHG
1104
+ { 0x97, ENTRY_CopyBytes1 }, // XCHG
1105
+ { 0x98, ENTRY_CopyBytes1 }, // CWDE
1106
+ { 0x99, ENTRY_CopyBytes1 }, // CDQ
1107
+ #ifdef DETOURS_X64
1108
+ { 0x9A, ENTRY_Invalid }, // Invalid
1109
+ #else
1110
+ { 0x9A, ENTRY_CopyBytes5Or7Dynamic }, // CALL cp
1111
+ #endif
1112
+ { 0x9B, ENTRY_CopyBytes1 }, // WAIT/FWAIT
1113
+ { 0x9C, ENTRY_CopyBytes1 }, // PUSHFD
1114
+ { 0x9D, ENTRY_CopyBytes1 }, // POPFD
1115
+ { 0x9E, ENTRY_CopyBytes1 }, // SAHF
1116
+ { 0x9F, ENTRY_CopyBytes1 }, // LAHF
1117
+ { 0xA0, ENTRY_CopyBytes1Address }, // MOV
1118
+ { 0xA1, ENTRY_CopyBytes1Address }, // MOV
1119
+ { 0xA2, ENTRY_CopyBytes1Address }, // MOV
1120
+ { 0xA3, ENTRY_CopyBytes1Address }, // MOV
1121
+ { 0xA4, ENTRY_CopyBytes1 }, // MOVS
1122
+ { 0xA5, ENTRY_CopyBytes1 }, // MOVS/MOVSD
1123
+ { 0xA6, ENTRY_CopyBytes1 }, // CMPS/CMPSB
1124
+ { 0xA7, ENTRY_CopyBytes1 }, // CMPS/CMPSW
1125
+ { 0xA8, ENTRY_CopyBytes2 }, // TEST
1126
+ { 0xA9, ENTRY_CopyBytes3Or5 }, // TEST
1127
+ { 0xAA, ENTRY_CopyBytes1 }, // STOS/STOSB
1128
+ { 0xAB, ENTRY_CopyBytes1 }, // STOS/STOSW
1129
+ { 0xAC, ENTRY_CopyBytes1 }, // LODS/LODSB
1130
+ { 0xAD, ENTRY_CopyBytes1 }, // LODS/LODSW
1131
+ { 0xAE, ENTRY_CopyBytes1 }, // SCAS/SCASB
1132
+ { 0xAF, ENTRY_CopyBytes1 }, // SCAS/SCASD
1133
+ { 0xB0, ENTRY_CopyBytes2 }, // MOV B0+rb
1134
+ { 0xB1, ENTRY_CopyBytes2 }, // MOV B0+rb
1135
+ { 0xB2, ENTRY_CopyBytes2 }, // MOV B0+rb
1136
+ { 0xB3, ENTRY_CopyBytes2 }, // MOV B0+rb
1137
+ { 0xB4, ENTRY_CopyBytes2 }, // MOV B0+rb
1138
+ { 0xB5, ENTRY_CopyBytes2 }, // MOV B0+rb
1139
+ { 0xB6, ENTRY_CopyBytes2 }, // MOV B0+rb
1140
+ { 0xB7, ENTRY_CopyBytes2 }, // MOV B0+rb
1141
+ { 0xB8, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1142
+ { 0xB9, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1143
+ { 0xBA, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1144
+ { 0xBB, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1145
+ { 0xBC, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1146
+ { 0xBD, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1147
+ { 0xBE, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1148
+ { 0xBF, ENTRY_CopyBytes3Or5Rax }, // MOV B8+rb
1149
+ { 0xC0, ENTRY_CopyBytes2Mod1 }, // RCL/2 ib, etc.
1150
+ { 0xC1, ENTRY_CopyBytes2Mod1 }, // RCL/2 ib, etc.
1151
+ { 0xC2, ENTRY_CopyBytes3 }, // RET
1152
+ { 0xC3, ENTRY_CopyBytes1 }, // RET
1153
+ { 0xC4, ENTRY_CopyVex3 }, // LES, VEX 3-byte opcodes.
1154
+ { 0xC5, ENTRY_CopyVex2 }, // LDS, VEX 2-byte opcodes.
1155
+ { 0xC6, ENTRY_CopyBytes2Mod1 }, // MOV
1156
+ { 0xC7, ENTRY_CopyBytes2ModOperand }, // MOV/0 XBEGIN/7
1157
+ { 0xC8, ENTRY_CopyBytes4 }, // ENTER
1158
+ { 0xC9, ENTRY_CopyBytes1 }, // LEAVE
1159
+ { 0xCA, ENTRY_CopyBytes3Dynamic }, // RET
1160
+ { 0xCB, ENTRY_CopyBytes1Dynamic }, // RET
1161
+ { 0xCC, ENTRY_CopyBytes1Dynamic }, // INT 3
1162
+ { 0xCD, ENTRY_CopyBytes2Dynamic }, // INT ib
1163
+ #ifdef DETOURS_X64
1164
+ { 0xCE, ENTRY_Invalid }, // Invalid
1165
+ #else
1166
+ { 0xCE, ENTRY_CopyBytes1Dynamic }, // INTO
1167
+ #endif
1168
+ { 0xCF, ENTRY_CopyBytes1Dynamic }, // IRET
1169
+ { 0xD0, ENTRY_CopyBytes2Mod }, // RCL/2, etc.
1170
+ { 0xD1, ENTRY_CopyBytes2Mod }, // RCL/2, etc.
1171
+ { 0xD2, ENTRY_CopyBytes2Mod }, // RCL/2, etc.
1172
+ { 0xD3, ENTRY_CopyBytes2Mod }, // RCL/2, etc.
1173
+ #ifdef DETOURS_X64
1174
+ { 0xD4, ENTRY_Invalid }, // Invalid
1175
+ { 0xD5, ENTRY_Invalid }, // Invalid
1176
+ #else
1177
+ { 0xD4, ENTRY_CopyBytes2 }, // AAM
1178
+ { 0xD5, ENTRY_CopyBytes2 }, // AAD
1179
+ #endif
1180
+ { 0xD6, ENTRY_Invalid }, // Invalid
1181
+ { 0xD7, ENTRY_CopyBytes1 }, // XLAT/XLATB
1182
+ { 0xD8, ENTRY_CopyBytes2Mod }, // FADD, etc.
1183
+ { 0xD9, ENTRY_CopyBytes2Mod }, // F2XM1, etc.
1184
+ { 0xDA, ENTRY_CopyBytes2Mod }, // FLADD, etc.
1185
+ { 0xDB, ENTRY_CopyBytes2Mod }, // FCLEX, etc.
1186
+ { 0xDC, ENTRY_CopyBytes2Mod }, // FADD/0, etc.
1187
+ { 0xDD, ENTRY_CopyBytes2Mod }, // FFREE, etc.
1188
+ { 0xDE, ENTRY_CopyBytes2Mod }, // FADDP, etc.
1189
+ { 0xDF, ENTRY_CopyBytes2Mod }, // FBLD/4, etc.
1190
+ { 0xE0, ENTRY_CopyBytes2CantJump }, // LOOPNE cb
1191
+ { 0xE1, ENTRY_CopyBytes2CantJump }, // LOOPE cb
1192
+ { 0xE2, ENTRY_CopyBytes2CantJump }, // LOOP cb
1193
+ { 0xE3, ENTRY_CopyBytes2CantJump }, // JCXZ/JECXZ
1194
+ { 0xE4, ENTRY_CopyBytes2 }, // IN ib
1195
+ { 0xE5, ENTRY_CopyBytes2 }, // IN id
1196
+ { 0xE6, ENTRY_CopyBytes2 }, // OUT ib
1197
+ { 0xE7, ENTRY_CopyBytes2 }, // OUT ib
1198
+ { 0xE8, ENTRY_CopyBytes3Or5Target }, // CALL cd
1199
+ { 0xE9, ENTRY_CopyBytes3Or5Target }, // JMP cd
1200
+ #ifdef DETOURS_X64
1201
+ { 0xEA, ENTRY_Invalid }, // Invalid
1202
+ #else
1203
+ { 0xEA, ENTRY_CopyBytes5Or7Dynamic }, // JMP cp
1204
+ #endif
1205
+ { 0xEB, ENTRY_CopyBytes2Jump }, // JMP cb
1206
+ { 0xEC, ENTRY_CopyBytes1 }, // IN ib
1207
+ { 0xED, ENTRY_CopyBytes1 }, // IN id
1208
+ { 0xEE, ENTRY_CopyBytes1 }, // OUT
1209
+ { 0xEF, ENTRY_CopyBytes1 }, // OUT
1210
+ { 0xF0, ENTRY_CopyBytesPrefix }, // LOCK prefix
1211
+ { 0xF1, ENTRY_CopyBytes1Dynamic }, // INT1 / ICEBP somewhat documented by AMD, not by Intel
1212
+ { 0xF2, ENTRY_CopyF2 }, // REPNE prefix
1213
+ //#ifdef DETOURS_X86
1214
+ { 0xF3, ENTRY_CopyF3 }, // REPE prefix
1215
+ //#else
1216
+ // This does presently suffice for AMD64 but it requires tracing
1217
+ // through a bunch of code to verify and seems not worth maintaining.
1218
+ // { 0xF3, ENTRY_CopyBytesPrefix }, // REPE prefix
1219
+ //#endif
1220
+ { 0xF4, ENTRY_CopyBytes1 }, // HLT
1221
+ { 0xF5, ENTRY_CopyBytes1 }, // CMC
1222
+ { 0xF6, ENTRY_CopyF6 }, // TEST/0, DIV/6
1223
+ { 0xF7, ENTRY_CopyF7 }, // TEST/0, DIV/6
1224
+ { 0xF8, ENTRY_CopyBytes1 }, // CLC
1225
+ { 0xF9, ENTRY_CopyBytes1 }, // STC
1226
+ { 0xFA, ENTRY_CopyBytes1 }, // CLI
1227
+ { 0xFB, ENTRY_CopyBytes1 }, // STI
1228
+ { 0xFC, ENTRY_CopyBytes1 }, // CLD
1229
+ { 0xFD, ENTRY_CopyBytes1 }, // STD
1230
+ { 0xFE, ENTRY_CopyBytes2Mod }, // DEC/1,INC/0
1231
+ { 0xFF, ENTRY_CopyFF }, // CALL/2
1232
+ { 0, ENTRY_End },
1233
+ };
1234
+
1235
+ const CDetourDis::COPYENTRY CDetourDis::s_rceCopyTable0F[257] =
1236
+ {
1237
+ #ifdef DETOURS_X86
1238
+ { 0x00, ENTRY_Copy0F00 }, // sldt/0 str/1 lldt/2 ltr/3 err/4 verw/5 jmpe/6/dynamic invalid/7
1239
+ #else
1240
+ { 0x00, ENTRY_CopyBytes2Mod }, // sldt/0 str/1 lldt/2 ltr/3 err/4 verw/5 jmpe/6/dynamic invalid/7
1241
+ #endif
1242
+ { 0x01, ENTRY_CopyBytes2Mod }, // INVLPG/7, etc.
1243
+ { 0x02, ENTRY_CopyBytes2Mod }, // LAR/r
1244
+ { 0x03, ENTRY_CopyBytes2Mod }, // LSL/r
1245
+ { 0x04, ENTRY_Invalid }, // _04
1246
+ { 0x05, ENTRY_CopyBytes1 }, // SYSCALL
1247
+ { 0x06, ENTRY_CopyBytes1 }, // CLTS
1248
+ { 0x07, ENTRY_CopyBytes1 }, // SYSRET
1249
+ { 0x08, ENTRY_CopyBytes1 }, // INVD
1250
+ { 0x09, ENTRY_CopyBytes1 }, // WBINVD
1251
+ { 0x0A, ENTRY_Invalid }, // _0A
1252
+ { 0x0B, ENTRY_CopyBytes1 }, // UD2
1253
+ { 0x0C, ENTRY_Invalid }, // _0C
1254
+ { 0x0D, ENTRY_CopyBytes2Mod }, // PREFETCH
1255
+ { 0x0E, ENTRY_CopyBytes1 }, // FEMMS (3DNow -- not in Intel documentation)
1256
+ { 0x0F, ENTRY_CopyBytes2Mod1 }, // 3DNow Opcodes
1257
+ { 0x10, ENTRY_CopyBytes2Mod }, // MOVSS MOVUPD MOVSD
1258
+ { 0x11, ENTRY_CopyBytes2Mod }, // MOVSS MOVUPD MOVSD
1259
+ { 0x12, ENTRY_CopyBytes2Mod }, // MOVLPD
1260
+ { 0x13, ENTRY_CopyBytes2Mod }, // MOVLPD
1261
+ { 0x14, ENTRY_CopyBytes2Mod }, // UNPCKLPD
1262
+ { 0x15, ENTRY_CopyBytes2Mod }, // UNPCKHPD
1263
+ { 0x16, ENTRY_CopyBytes2Mod }, // MOVHPD
1264
+ { 0x17, ENTRY_CopyBytes2Mod }, // MOVHPD
1265
+ { 0x18, ENTRY_CopyBytes2Mod }, // PREFETCHINTA...
1266
+ { 0x19, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1267
+ { 0x1A, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1268
+ { 0x1B, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1269
+ { 0x1C, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1270
+ { 0x1D, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1271
+ { 0x1E, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop, not documented by Intel, documented by AMD
1272
+ { 0x1F, ENTRY_CopyBytes2Mod }, // NOP/r multi byte nop
1273
+ { 0x20, ENTRY_CopyBytes2Mod }, // MOV/r
1274
+ { 0x21, ENTRY_CopyBytes2Mod }, // MOV/r
1275
+ { 0x22, ENTRY_CopyBytes2Mod }, // MOV/r
1276
+ { 0x23, ENTRY_CopyBytes2Mod }, // MOV/r
1277
+ #ifdef DETOURS_X64
1278
+ { 0x24, ENTRY_Invalid }, // _24
1279
+ #else
1280
+ { 0x24, ENTRY_CopyBytes2Mod }, // MOV/r,TR TR is test register on 80386 and 80486, removed in Pentium
1281
+ #endif
1282
+ { 0x25, ENTRY_Invalid }, // _25
1283
+ #ifdef DETOURS_X64
1284
+ { 0x26, ENTRY_Invalid }, // _26
1285
+ #else
1286
+ { 0x26, ENTRY_CopyBytes2Mod }, // MOV TR/r TR is test register on 80386 and 80486, removed in Pentium
1287
+ #endif
1288
+ { 0x27, ENTRY_Invalid }, // _27
1289
+ { 0x28, ENTRY_CopyBytes2Mod }, // MOVAPS MOVAPD
1290
+ { 0x29, ENTRY_CopyBytes2Mod }, // MOVAPS MOVAPD
1291
+ { 0x2A, ENTRY_CopyBytes2Mod }, // CVPI2PS &
1292
+ { 0x2B, ENTRY_CopyBytes2Mod }, // MOVNTPS MOVNTPD
1293
+ { 0x2C, ENTRY_CopyBytes2Mod }, // CVTTPS2PI &
1294
+ { 0x2D, ENTRY_CopyBytes2Mod }, // CVTPS2PI &
1295
+ { 0x2E, ENTRY_CopyBytes2Mod }, // UCOMISS UCOMISD
1296
+ { 0x2F, ENTRY_CopyBytes2Mod }, // COMISS COMISD
1297
+ { 0x30, ENTRY_CopyBytes1 }, // WRMSR
1298
+ { 0x31, ENTRY_CopyBytes1 }, // RDTSC
1299
+ { 0x32, ENTRY_CopyBytes1 }, // RDMSR
1300
+ { 0x33, ENTRY_CopyBytes1 }, // RDPMC
1301
+ { 0x34, ENTRY_CopyBytes1 }, // SYSENTER
1302
+ { 0x35, ENTRY_CopyBytes1 }, // SYSEXIT
1303
+ { 0x36, ENTRY_Invalid }, // _36
1304
+ { 0x37, ENTRY_CopyBytes1 }, // GETSEC
1305
+ { 0x38, ENTRY_CopyBytes3Mod }, // SSE3 Opcodes
1306
+ { 0x39, ENTRY_Invalid }, // _39
1307
+ { 0x3A, ENTRY_CopyBytes3Mod1 }, // SSE3 Opcodes
1308
+ { 0x3B, ENTRY_Invalid }, // _3B
1309
+ { 0x3C, ENTRY_Invalid }, // _3C
1310
+ { 0x3D, ENTRY_Invalid }, // _3D
1311
+ { 0x3E, ENTRY_Invalid }, // _3E
1312
+ { 0x3F, ENTRY_Invalid }, // _3F
1313
+ { 0x40, ENTRY_CopyBytes2Mod }, // CMOVO (0F 40)
1314
+ { 0x41, ENTRY_CopyBytes2Mod }, // CMOVNO (0F 41)
1315
+ { 0x42, ENTRY_CopyBytes2Mod }, // CMOVB & CMOVNE (0F 42)
1316
+ { 0x43, ENTRY_CopyBytes2Mod }, // CMOVAE & CMOVNB (0F 43)
1317
+ { 0x44, ENTRY_CopyBytes2Mod }, // CMOVE & CMOVZ (0F 44)
1318
+ { 0x45, ENTRY_CopyBytes2Mod }, // CMOVNE & CMOVNZ (0F 45)
1319
+ { 0x46, ENTRY_CopyBytes2Mod }, // CMOVBE & CMOVNA (0F 46)
1320
+ { 0x47, ENTRY_CopyBytes2Mod }, // CMOVA & CMOVNBE (0F 47)
1321
+ { 0x48, ENTRY_CopyBytes2Mod }, // CMOVS (0F 48)
1322
+ { 0x49, ENTRY_CopyBytes2Mod }, // CMOVNS (0F 49)
1323
+ { 0x4A, ENTRY_CopyBytes2Mod }, // CMOVP & CMOVPE (0F 4A)
1324
+ { 0x4B, ENTRY_CopyBytes2Mod }, // CMOVNP & CMOVPO (0F 4B)
1325
+ { 0x4C, ENTRY_CopyBytes2Mod }, // CMOVL & CMOVNGE (0F 4C)
1326
+ { 0x4D, ENTRY_CopyBytes2Mod }, // CMOVGE & CMOVNL (0F 4D)
1327
+ { 0x4E, ENTRY_CopyBytes2Mod }, // CMOVLE & CMOVNG (0F 4E)
1328
+ { 0x4F, ENTRY_CopyBytes2Mod }, // CMOVG & CMOVNLE (0F 4F)
1329
+ { 0x50, ENTRY_CopyBytes2Mod }, // MOVMSKPD MOVMSKPD
1330
+ { 0x51, ENTRY_CopyBytes2Mod }, // SQRTPS &
1331
+ { 0x52, ENTRY_CopyBytes2Mod }, // RSQRTTS RSQRTPS
1332
+ { 0x53, ENTRY_CopyBytes2Mod }, // RCPPS RCPSS
1333
+ { 0x54, ENTRY_CopyBytes2Mod }, // ANDPS ANDPD
1334
+ { 0x55, ENTRY_CopyBytes2Mod }, // ANDNPS ANDNPD
1335
+ { 0x56, ENTRY_CopyBytes2Mod }, // ORPS ORPD
1336
+ { 0x57, ENTRY_CopyBytes2Mod }, // XORPS XORPD
1337
+ { 0x58, ENTRY_CopyBytes2Mod }, // ADDPS &
1338
+ { 0x59, ENTRY_CopyBytes2Mod }, // MULPS &
1339
+ { 0x5A, ENTRY_CopyBytes2Mod }, // CVTPS2PD &
1340
+ { 0x5B, ENTRY_CopyBytes2Mod }, // CVTDQ2PS &
1341
+ { 0x5C, ENTRY_CopyBytes2Mod }, // SUBPS &
1342
+ { 0x5D, ENTRY_CopyBytes2Mod }, // MINPS &
1343
+ { 0x5E, ENTRY_CopyBytes2Mod }, // DIVPS &
1344
+ { 0x5F, ENTRY_CopyBytes2Mod }, // MASPS &
1345
+ { 0x60, ENTRY_CopyBytes2Mod }, // PUNPCKLBW/r
1346
+ { 0x61, ENTRY_CopyBytes2Mod }, // PUNPCKLWD/r
1347
+ { 0x62, ENTRY_CopyBytes2Mod }, // PUNPCKLWD/r
1348
+ { 0x63, ENTRY_CopyBytes2Mod }, // PACKSSWB/r
1349
+ { 0x64, ENTRY_CopyBytes2Mod }, // PCMPGTB/r
1350
+ { 0x65, ENTRY_CopyBytes2Mod }, // PCMPGTW/r
1351
+ { 0x66, ENTRY_CopyBytes2Mod }, // PCMPGTD/r
1352
+ { 0x67, ENTRY_CopyBytes2Mod }, // PACKUSWB/r
1353
+ { 0x68, ENTRY_CopyBytes2Mod }, // PUNPCKHBW/r
1354
+ { 0x69, ENTRY_CopyBytes2Mod }, // PUNPCKHWD/r
1355
+ { 0x6A, ENTRY_CopyBytes2Mod }, // PUNPCKHDQ/r
1356
+ { 0x6B, ENTRY_CopyBytes2Mod }, // PACKSSDW/r
1357
+ { 0x6C, ENTRY_CopyBytes2Mod }, // PUNPCKLQDQ
1358
+ { 0x6D, ENTRY_CopyBytes2Mod }, // PUNPCKHQDQ
1359
+ { 0x6E, ENTRY_CopyBytes2Mod }, // MOVD/r
1360
+ { 0x6F, ENTRY_CopyBytes2Mod }, // MOV/r
1361
+ { 0x70, ENTRY_CopyBytes2Mod1 }, // PSHUFW/r ib
1362
+ { 0x71, ENTRY_CopyBytes2Mod1 }, // PSLLW/6 ib,PSRAW/4 ib,PSRLW/2 ib
1363
+ { 0x72, ENTRY_CopyBytes2Mod1 }, // PSLLD/6 ib,PSRAD/4 ib,PSRLD/2 ib
1364
+ { 0x73, ENTRY_CopyBytes2Mod1 }, // PSLLQ/6 ib,PSRLQ/2 ib
1365
+ { 0x74, ENTRY_CopyBytes2Mod }, // PCMPEQB/r
1366
+ { 0x75, ENTRY_CopyBytes2Mod }, // PCMPEQW/r
1367
+ { 0x76, ENTRY_CopyBytes2Mod }, // PCMPEQD/r
1368
+ { 0x77, ENTRY_CopyBytes1 }, // EMMS
1369
+ // extrq/insertq require mode=3 and are followed by two immediate bytes
1370
+ { 0x78, ENTRY_Copy0F78 }, // VMREAD/r, 66/EXTRQ/r/ib/ib, F2/INSERTQ/r/ib/ib
1371
+ // extrq/insertq require mod=3, therefore ENTRY_CopyBytes2, but it ends up the same
1372
+ { 0x79, ENTRY_CopyBytes2Mod }, // VMWRITE/r, 66/EXTRQ/r, F2/INSERTQ/r
1373
+ { 0x7A, ENTRY_Invalid }, // _7A
1374
+ { 0x7B, ENTRY_Invalid }, // _7B
1375
+ { 0x7C, ENTRY_CopyBytes2Mod }, // HADDPS
1376
+ { 0x7D, ENTRY_CopyBytes2Mod }, // HSUBPS
1377
+ { 0x7E, ENTRY_CopyBytes2Mod }, // MOVD/r
1378
+ { 0x7F, ENTRY_CopyBytes2Mod }, // MOV/r
1379
+ { 0x80, ENTRY_CopyBytes3Or5Target }, // JO
1380
+ { 0x81, ENTRY_CopyBytes3Or5Target }, // JNO
1381
+ { 0x82, ENTRY_CopyBytes3Or5Target }, // JB,JC,JNAE
1382
+ { 0x83, ENTRY_CopyBytes3Or5Target }, // JAE,JNB,JNC
1383
+ { 0x84, ENTRY_CopyBytes3Or5Target }, // JE,JZ,JZ
1384
+ { 0x85, ENTRY_CopyBytes3Or5Target }, // JNE,JNZ
1385
+ { 0x86, ENTRY_CopyBytes3Or5Target }, // JBE,JNA
1386
+ { 0x87, ENTRY_CopyBytes3Or5Target }, // JA,JNBE
1387
+ { 0x88, ENTRY_CopyBytes3Or5Target }, // JS
1388
+ { 0x89, ENTRY_CopyBytes3Or5Target }, // JNS
1389
+ { 0x8A, ENTRY_CopyBytes3Or5Target }, // JP,JPE
1390
+ { 0x8B, ENTRY_CopyBytes3Or5Target }, // JNP,JPO
1391
+ { 0x8C, ENTRY_CopyBytes3Or5Target }, // JL,NGE
1392
+ { 0x8D, ENTRY_CopyBytes3Or5Target }, // JGE,JNL
1393
+ { 0x8E, ENTRY_CopyBytes3Or5Target }, // JLE,JNG
1394
+ { 0x8F, ENTRY_CopyBytes3Or5Target }, // JG,JNLE
1395
+ { 0x90, ENTRY_CopyBytes2Mod }, // CMOVO (0F 40)
1396
+ { 0x91, ENTRY_CopyBytes2Mod }, // CMOVNO (0F 41)
1397
+ { 0x92, ENTRY_CopyBytes2Mod }, // CMOVB & CMOVC & CMOVNAE (0F 42)
1398
+ { 0x93, ENTRY_CopyBytes2Mod }, // CMOVAE & CMOVNB & CMOVNC (0F 43)
1399
+ { 0x94, ENTRY_CopyBytes2Mod }, // CMOVE & CMOVZ (0F 44)
1400
+ { 0x95, ENTRY_CopyBytes2Mod }, // CMOVNE & CMOVNZ (0F 45)
1401
+ { 0x96, ENTRY_CopyBytes2Mod }, // CMOVBE & CMOVNA (0F 46)
1402
+ { 0x97, ENTRY_CopyBytes2Mod }, // CMOVA & CMOVNBE (0F 47)
1403
+ { 0x98, ENTRY_CopyBytes2Mod }, // CMOVS (0F 48)
1404
+ { 0x99, ENTRY_CopyBytes2Mod }, // CMOVNS (0F 49)
1405
+ { 0x9A, ENTRY_CopyBytes2Mod }, // CMOVP & CMOVPE (0F 4A)
1406
+ { 0x9B, ENTRY_CopyBytes2Mod }, // CMOVNP & CMOVPO (0F 4B)
1407
+ { 0x9C, ENTRY_CopyBytes2Mod }, // CMOVL & CMOVNGE (0F 4C)
1408
+ { 0x9D, ENTRY_CopyBytes2Mod }, // CMOVGE & CMOVNL (0F 4D)
1409
+ { 0x9E, ENTRY_CopyBytes2Mod }, // CMOVLE & CMOVNG (0F 4E)
1410
+ { 0x9F, ENTRY_CopyBytes2Mod }, // CMOVG & CMOVNLE (0F 4F)
1411
+ { 0xA0, ENTRY_CopyBytes1 }, // PUSH
1412
+ { 0xA1, ENTRY_CopyBytes1 }, // POP
1413
+ { 0xA2, ENTRY_CopyBytes1 }, // CPUID
1414
+ { 0xA3, ENTRY_CopyBytes2Mod }, // BT (0F A3)
1415
+ { 0xA4, ENTRY_CopyBytes2Mod1 }, // SHLD
1416
+ { 0xA5, ENTRY_CopyBytes2Mod }, // SHLD
1417
+ { 0xA6, ENTRY_CopyBytes2Mod }, // XBTS
1418
+ { 0xA7, ENTRY_CopyBytes2Mod }, // IBTS
1419
+ { 0xA8, ENTRY_CopyBytes1 }, // PUSH
1420
+ { 0xA9, ENTRY_CopyBytes1 }, // POP
1421
+ { 0xAA, ENTRY_CopyBytes1 }, // RSM
1422
+ { 0xAB, ENTRY_CopyBytes2Mod }, // BTS (0F AB)
1423
+ { 0xAC, ENTRY_CopyBytes2Mod1 }, // SHRD
1424
+ { 0xAD, ENTRY_CopyBytes2Mod }, // SHRD
1425
+
1426
+ // 0F AE mod76=mem mod543=0 fxsave
1427
+ // 0F AE mod76=mem mod543=1 fxrstor
1428
+ // 0F AE mod76=mem mod543=2 ldmxcsr
1429
+ // 0F AE mod76=mem mod543=3 stmxcsr
1430
+ // 0F AE mod76=mem mod543=4 xsave
1431
+ // 0F AE mod76=mem mod543=5 xrstor
1432
+ // 0F AE mod76=mem mod543=6 saveopt
1433
+ // 0F AE mod76=mem mod543=7 clflush
1434
+ // 0F AE mod76=11b mod543=5 lfence
1435
+ // 0F AE mod76=11b mod543=6 mfence
1436
+ // 0F AE mod76=11b mod543=7 sfence
1437
+ // F3 0F AE mod76=11b mod543=0 rdfsbase
1438
+ // F3 0F AE mod76=11b mod543=1 rdgsbase
1439
+ // F3 0F AE mod76=11b mod543=2 wrfsbase
1440
+ // F3 0F AE mod76=11b mod543=3 wrgsbase
1441
+ { 0xAE, ENTRY_CopyBytes2Mod }, // fxsave fxrstor ldmxcsr stmxcsr xsave xrstor saveopt clflush lfence mfence sfence rdfsbase rdgsbase wrfsbase wrgsbase
1442
+ { 0xAF, ENTRY_CopyBytes2Mod }, // IMUL (0F AF)
1443
+ { 0xB0, ENTRY_CopyBytes2Mod }, // CMPXCHG (0F B0)
1444
+ { 0xB1, ENTRY_CopyBytes2Mod }, // CMPXCHG (0F B1)
1445
+ { 0xB2, ENTRY_CopyBytes2Mod }, // LSS/r
1446
+ { 0xB3, ENTRY_CopyBytes2Mod }, // BTR (0F B3)
1447
+ { 0xB4, ENTRY_CopyBytes2Mod }, // LFS/r
1448
+ { 0xB5, ENTRY_CopyBytes2Mod }, // LGS/r
1449
+ { 0xB6, ENTRY_CopyBytes2Mod }, // MOVZX/r
1450
+ { 0xB7, ENTRY_CopyBytes2Mod }, // MOVZX/r
1451
+ #ifdef DETOURS_X86
1452
+ { 0xB8, ENTRY_Copy0FB8 }, // jmpe f3/popcnt
1453
+ #else
1454
+ { 0xB8, ENTRY_CopyBytes2Mod }, // f3/popcnt
1455
+ #endif
1456
+ { 0xB9, ENTRY_Invalid }, // _B9
1457
+ { 0xBA, ENTRY_CopyBytes2Mod1 }, // BT & BTC & BTR & BTS (0F BA)
1458
+ { 0xBB, ENTRY_CopyBytes2Mod }, // BTC (0F BB)
1459
+ { 0xBC, ENTRY_CopyBytes2Mod }, // BSF (0F BC)
1460
+ { 0xBD, ENTRY_CopyBytes2Mod }, // BSR (0F BD)
1461
+ { 0xBE, ENTRY_CopyBytes2Mod }, // MOVSX/r
1462
+ { 0xBF, ENTRY_CopyBytes2Mod }, // MOVSX/r
1463
+ { 0xC0, ENTRY_CopyBytes2Mod }, // XADD/r
1464
+ { 0xC1, ENTRY_CopyBytes2Mod }, // XADD/r
1465
+ { 0xC2, ENTRY_CopyBytes2Mod1 }, // CMPPS &
1466
+ { 0xC3, ENTRY_CopyBytes2Mod }, // MOVNTI
1467
+ { 0xC4, ENTRY_CopyBytes2Mod1 }, // PINSRW /r ib
1468
+ { 0xC5, ENTRY_CopyBytes2Mod1 }, // PEXTRW /r ib
1469
+ { 0xC6, ENTRY_CopyBytes2Mod1 }, // SHUFPS & SHUFPD
1470
+ { 0xC7, ENTRY_CopyBytes2Mod }, // CMPXCHG8B (0F C7)
1471
+ { 0xC8, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1472
+ { 0xC9, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1473
+ { 0xCA, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1474
+ { 0xCB, ENTRY_CopyBytes1 }, // CVTPD2PI BSWAP 0F C8 + rd
1475
+ { 0xCC, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1476
+ { 0xCD, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1477
+ { 0xCE, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1478
+ { 0xCF, ENTRY_CopyBytes1 }, // BSWAP 0F C8 + rd
1479
+ { 0xD0, ENTRY_CopyBytes2Mod }, // ADDSUBPS (untestd)
1480
+ { 0xD1, ENTRY_CopyBytes2Mod }, // PSRLW/r
1481
+ { 0xD2, ENTRY_CopyBytes2Mod }, // PSRLD/r
1482
+ { 0xD3, ENTRY_CopyBytes2Mod }, // PSRLQ/r
1483
+ { 0xD4, ENTRY_CopyBytes2Mod }, // PADDQ
1484
+ { 0xD5, ENTRY_CopyBytes2Mod }, // PMULLW/r
1485
+ { 0xD6, ENTRY_CopyBytes2Mod }, // MOVDQ2Q / MOVQ2DQ
1486
+ { 0xD7, ENTRY_CopyBytes2Mod }, // PMOVMSKB/r
1487
+ { 0xD8, ENTRY_CopyBytes2Mod }, // PSUBUSB/r
1488
+ { 0xD9, ENTRY_CopyBytes2Mod }, // PSUBUSW/r
1489
+ { 0xDA, ENTRY_CopyBytes2Mod }, // PMINUB/r
1490
+ { 0xDB, ENTRY_CopyBytes2Mod }, // PAND/r
1491
+ { 0xDC, ENTRY_CopyBytes2Mod }, // PADDUSB/r
1492
+ { 0xDD, ENTRY_CopyBytes2Mod }, // PADDUSW/r
1493
+ { 0xDE, ENTRY_CopyBytes2Mod }, // PMAXUB/r
1494
+ { 0xDF, ENTRY_CopyBytes2Mod }, // PANDN/r
1495
+ { 0xE0, ENTRY_CopyBytes2Mod }, // PAVGB
1496
+ { 0xE1, ENTRY_CopyBytes2Mod }, // PSRAW/r
1497
+ { 0xE2, ENTRY_CopyBytes2Mod }, // PSRAD/r
1498
+ { 0xE3, ENTRY_CopyBytes2Mod }, // PAVGW
1499
+ { 0xE4, ENTRY_CopyBytes2Mod }, // PMULHUW/r
1500
+ { 0xE5, ENTRY_CopyBytes2Mod }, // PMULHW/r
1501
+ { 0xE6, ENTRY_CopyBytes2Mod }, // CTDQ2PD &
1502
+ { 0xE7, ENTRY_CopyBytes2Mod }, // MOVNTQ
1503
+ { 0xE8, ENTRY_CopyBytes2Mod }, // PSUBB/r
1504
+ { 0xE9, ENTRY_CopyBytes2Mod }, // PSUBW/r
1505
+ { 0xEA, ENTRY_CopyBytes2Mod }, // PMINSW/r
1506
+ { 0xEB, ENTRY_CopyBytes2Mod }, // POR/r
1507
+ { 0xEC, ENTRY_CopyBytes2Mod }, // PADDSB/r
1508
+ { 0xED, ENTRY_CopyBytes2Mod }, // PADDSW/r
1509
+ { 0xEE, ENTRY_CopyBytes2Mod }, // PMAXSW /r
1510
+ { 0xEF, ENTRY_CopyBytes2Mod }, // PXOR/r
1511
+ { 0xF0, ENTRY_CopyBytes2Mod }, // LDDQU
1512
+ { 0xF1, ENTRY_CopyBytes2Mod }, // PSLLW/r
1513
+ { 0xF2, ENTRY_CopyBytes2Mod }, // PSLLD/r
1514
+ { 0xF3, ENTRY_CopyBytes2Mod }, // PSLLQ/r
1515
+ { 0xF4, ENTRY_CopyBytes2Mod }, // PMULUDQ/r
1516
+ { 0xF5, ENTRY_CopyBytes2Mod }, // PMADDWD/r
1517
+ { 0xF6, ENTRY_CopyBytes2Mod }, // PSADBW/r
1518
+ { 0xF7, ENTRY_CopyBytes2Mod }, // MASKMOVQ
1519
+ { 0xF8, ENTRY_CopyBytes2Mod }, // PSUBB/r
1520
+ { 0xF9, ENTRY_CopyBytes2Mod }, // PSUBW/r
1521
+ { 0xFA, ENTRY_CopyBytes2Mod }, // PSUBD/r
1522
+ { 0xFB, ENTRY_CopyBytes2Mod }, // FSUBQ/r
1523
+ { 0xFC, ENTRY_CopyBytes2Mod }, // PADDB/r
1524
+ { 0xFD, ENTRY_CopyBytes2Mod }, // PADDW/r
1525
+ { 0xFE, ENTRY_CopyBytes2Mod }, // PADDD/r
1526
+ { 0xFF, ENTRY_Invalid }, // _FF
1527
+ { 0, ENTRY_End },
1528
+ };
1529
+
1530
+ BOOL CDetourDis::SanityCheckSystem()
1531
+ {
1532
+ ULONG n = 0;
1533
+ for (; n < 256; n++) {
1534
+ REFCOPYENTRY pEntry = &s_rceCopyTable[n];
1535
+
1536
+ if (n != pEntry->nOpcode) {
1537
+ ASSERT(n == pEntry->nOpcode);
1538
+ return FALSE;
1539
+ }
1540
+ }
1541
+ if (s_rceCopyTable[256].pfCopy != NULL) {
1542
+ ASSERT(!"Missing end marker.");
1543
+ return FALSE;
1544
+ }
1545
+
1546
+ for (n = 0; n < 256; n++) {
1547
+ REFCOPYENTRY pEntry = &s_rceCopyTable0F[n];
1548
+
1549
+ if (n != pEntry->nOpcode) {
1550
+ ASSERT(n == pEntry->nOpcode);
1551
+ return FALSE;
1552
+ }
1553
+ }
1554
+ if (s_rceCopyTable0F[256].pfCopy != NULL) {
1555
+ ASSERT(!"Missing end marker.");
1556
+ return FALSE;
1557
+ }
1558
+
1559
+ return TRUE;
1560
+ }
1561
+ #endif // defined(DETOURS_X64) || defined(DETOURS_X86)
1562
+
1563
+ /////////////////////////////////////////////////////////// IA64 Disassembler.
1564
+ //
1565
+ #ifdef DETOURS_IA64
1566
+
1567
+ #if defined(_IA64_) != defined(DETOURS_IA64_OFFLINE_LIBRARY)
1568
+ // Compile DETOUR_IA64_BUNDLE for native IA64 or cross, but not both -- we get duplicates otherwise.
1569
+ const DETOUR_IA64_BUNDLE::DETOUR_IA64_METADATA DETOUR_IA64_BUNDLE::s_rceCopyTable[33] =
1570
+ {
1571
+ { 0x00, M_UNIT, I_UNIT, I_UNIT, },
1572
+ { 0x01, M_UNIT, I_UNIT, I_UNIT, },
1573
+ { 0x02, M_UNIT, I_UNIT, I_UNIT, },
1574
+ { 0x03, M_UNIT, I_UNIT, I_UNIT, },
1575
+ { 0x04, M_UNIT, L_UNIT, X_UNIT, },
1576
+ { 0x05, M_UNIT, L_UNIT, X_UNIT, },
1577
+ { 0x06, 0, 0, 0, },
1578
+ { 0x07, 0, 0, 0, },
1579
+ { 0x08, M_UNIT, M_UNIT, I_UNIT, },
1580
+ { 0x09, M_UNIT, M_UNIT, I_UNIT, },
1581
+ { 0x0a, M_UNIT, M_UNIT, I_UNIT, },
1582
+ { 0x0b, M_UNIT, M_UNIT, I_UNIT, },
1583
+ { 0x0c, M_UNIT, F_UNIT, I_UNIT, },
1584
+ { 0x0d, M_UNIT, F_UNIT, I_UNIT, },
1585
+ { 0x0e, M_UNIT, M_UNIT, F_UNIT, },
1586
+ { 0x0f, M_UNIT, M_UNIT, F_UNIT, },
1587
+ { 0x10, M_UNIT, I_UNIT, B_UNIT, },
1588
+ { 0x11, M_UNIT, I_UNIT, B_UNIT, },
1589
+ { 0x12, M_UNIT, B_UNIT, B_UNIT, },
1590
+ { 0x13, M_UNIT, B_UNIT, B_UNIT, },
1591
+ { 0x14, 0, 0, 0, },
1592
+ { 0x15, 0, 0, 0, },
1593
+ { 0x16, B_UNIT, B_UNIT, B_UNIT, },
1594
+ { 0x17, B_UNIT, B_UNIT, B_UNIT, },
1595
+ { 0x18, M_UNIT, M_UNIT, B_UNIT, },
1596
+ { 0x19, M_UNIT, M_UNIT, B_UNIT, },
1597
+ { 0x1a, 0, 0, 0, },
1598
+ { 0x1b, 0, 0, 0, },
1599
+ { 0x1c, M_UNIT, F_UNIT, B_UNIT, },
1600
+ { 0x1d, M_UNIT, F_UNIT, B_UNIT, },
1601
+ { 0x1e, 0, 0, 0, },
1602
+ { 0x1f, 0, 0, 0, },
1603
+ { 0x00, 0, 0, 0, },
1604
+ };
1605
+
1606
+ // 120 112 104 96 88 80 72 64 56 48 40 32 24 16 8 0
1607
+ // f. e. d. c. b. a. 9. 8. 7. 6. 5. 4. 3. 2. 1. 0.
1608
+
1609
+ // 00
1610
+ // f.e. d.c. b.a. 9.8. 7.6. 5.4. 3.2. 1.0.
1611
+ // 0000 0000 0000 0000 0000 0000 0000 001f : Template [4..0]
1612
+ // 0000 0000 0000 0000 0000 03ff ffff ffe0 : Zero [ 41.. 5]
1613
+ // 0000 0000 0000 0000 0000 3c00 0000 0000 : Zero [ 45.. 42]
1614
+ // 0000 0000 0007 ffff ffff c000 0000 0000 : One [ 82.. 46]
1615
+ // 0000 0000 0078 0000 0000 0000 0000 0000 : One [ 86.. 83]
1616
+ // 0fff ffff ff80 0000 0000 0000 0000 0000 : Two [123.. 87]
1617
+ // f000 0000 0000 0000 0000 0000 0000 0000 : Two [127..124]
1618
+ BYTE DETOUR_IA64_BUNDLE::GetTemplate() const
1619
+ {
1620
+ return (data[0] & 0x1f);
1621
+ }
1622
+
1623
+ BYTE DETOUR_IA64_BUNDLE::GetInst0() const
1624
+ {
1625
+ return ((data[5] & 0x3c) >> 2);
1626
+ }
1627
+
1628
+ BYTE DETOUR_IA64_BUNDLE::GetInst1() const
1629
+ {
1630
+ return ((data[10] & 0x78) >> 3);
1631
+ }
1632
+
1633
+ BYTE DETOUR_IA64_BUNDLE::GetInst2() const
1634
+ {
1635
+ return ((data[15] & 0xf0) >> 4);
1636
+ }
1637
+
1638
+ BYTE DETOUR_IA64_BUNDLE::GetUnit(BYTE slot) const
1639
+ {
1640
+ switch (slot) {
1641
+ case 0: return GetUnit0();
1642
+ case 1: return GetUnit1();
1643
+ case 2: return GetUnit2();
1644
+ }
1645
+ __debugbreak();
1646
+ return 0;
1647
+ }
1648
+
1649
+ BYTE DETOUR_IA64_BUNDLE::GetUnit0() const
1650
+ {
1651
+ return s_rceCopyTable[data[0] & 0x1f].nUnit0;
1652
+ }
1653
+
1654
+ BYTE DETOUR_IA64_BUNDLE::GetUnit1() const
1655
+ {
1656
+ return s_rceCopyTable[data[0] & 0x1f].nUnit1;
1657
+ }
1658
+
1659
+ BYTE DETOUR_IA64_BUNDLE::GetUnit2() const
1660
+ {
1661
+ return s_rceCopyTable[data[0] & 0x1f].nUnit2;
1662
+ }
1663
+
1664
+ UINT64 DETOUR_IA64_BUNDLE::GetData0() const
1665
+ {
1666
+ return (((wide[0] & 0x000003ffffffffe0) >> 5));
1667
+ }
1668
+
1669
+ UINT64 DETOUR_IA64_BUNDLE::GetData1() const
1670
+ {
1671
+ return (((wide[0] & 0xffffc00000000000) >> 46) |
1672
+ ((wide[1] & 0x000000000007ffff) << 18));
1673
+ }
1674
+
1675
+ UINT64 DETOUR_IA64_BUNDLE::GetData2() const
1676
+ {
1677
+ return (((wide[1] & 0x0fffffffff800000) >> 23));
1678
+ }
1679
+
1680
+ VOID DETOUR_IA64_BUNDLE::SetInst(BYTE slot, BYTE nInst)
1681
+ {
1682
+ switch (slot)
1683
+ {
1684
+ case 0: SetInst0(nInst); return;
1685
+ case 1: SetInst1(nInst); return;
1686
+ case 2: SetInst2(nInst); return;
1687
+ }
1688
+ __debugbreak();
1689
+ }
1690
+
1691
+ VOID DETOUR_IA64_BUNDLE::SetInst0(BYTE nInst)
1692
+ {
1693
+ data[5] = (data[5] & ~0x3c) | ((nInst << 2) & 0x3c);
1694
+ }
1695
+
1696
+ VOID DETOUR_IA64_BUNDLE::SetInst1(BYTE nInst)
1697
+ {
1698
+ data[10] = (data[10] & ~0x78) | ((nInst << 3) & 0x78);
1699
+ }
1700
+
1701
+ VOID DETOUR_IA64_BUNDLE::SetInst2(BYTE nInst)
1702
+ {
1703
+ data[15] = (data[15] & ~0xf0) | ((nInst << 4) & 0xf0);
1704
+ }
1705
+
1706
+ VOID DETOUR_IA64_BUNDLE::SetData(BYTE slot, UINT64 nData)
1707
+ {
1708
+ switch (slot)
1709
+ {
1710
+ case 0: SetData0(nData); return;
1711
+ case 1: SetData1(nData); return;
1712
+ case 2: SetData2(nData); return;
1713
+ }
1714
+ __debugbreak();
1715
+ }
1716
+
1717
+ VOID DETOUR_IA64_BUNDLE::SetData0(UINT64 nData)
1718
+ {
1719
+ wide[0] = (wide[0] & ~0x000003ffffffffe0) | (( nData << 5) & 0x000003ffffffffe0);
1720
+ }
1721
+
1722
+ VOID DETOUR_IA64_BUNDLE::SetData1(UINT64 nData)
1723
+ {
1724
+ wide[0] = (wide[0] & ~0xffffc00000000000) | ((nData << 46) & 0xffffc00000000000);
1725
+ wide[1] = (wide[1] & ~0x000000000007ffff) | ((nData >> 18) & 0x000000000007ffff);
1726
+ }
1727
+
1728
+ VOID DETOUR_IA64_BUNDLE::SetData2(UINT64 nData)
1729
+ {
1730
+ wide[1] = (wide[1] & ~0x0fffffffff800000) | ((nData << 23) & 0x0fffffffff800000);
1731
+ }
1732
+
1733
+ UINT64 DETOUR_IA64_BUNDLE::GetInstruction(BYTE slot) const
1734
+ {
1735
+ switch (slot) {
1736
+ case 0: return GetInstruction0();
1737
+ case 1: return GetInstruction1();
1738
+ case 2: return GetInstruction2();
1739
+ }
1740
+ __debugbreak();
1741
+ return 0;
1742
+ }
1743
+
1744
+ UINT64 DETOUR_IA64_BUNDLE::GetInstruction0() const
1745
+ {
1746
+ // 41 bits from wide[0], skipping the 5 bit template.
1747
+ return GetBits(wide[0], DETOUR_IA64_INSTRUCTION0_OFFSET, DETOUR_IA64_INSTRUCTION_SIZE);
1748
+ }
1749
+
1750
+ UINT64 DETOUR_IA64_BUNDLE::GetInstruction1() const
1751
+ {
1752
+ // 64-46 bits from wide[0] and the rest from wide[1].
1753
+ const UINT count0 = 64 - DETOUR_IA64_INSTRUCTION1_OFFSET;
1754
+ const UINT count1 = DETOUR_IA64_INSTRUCTION_SIZE - count0;
1755
+ return GetBits(wide[0], DETOUR_IA64_INSTRUCTION1_OFFSET, count0) | (GetBits(wide[1], 0, count1) << count0);
1756
+ }
1757
+
1758
+ UINT64 DETOUR_IA64_BUNDLE::GetInstruction2() const
1759
+ {
1760
+ // Upper 41 bits of wide[1].
1761
+ return wide[1] >> (64 - DETOUR_IA64_INSTRUCTION_SIZE);
1762
+ }
1763
+
1764
+ void DETOUR_IA64_BUNDLE::SetInstruction(BYTE slot, UINT64 instruction)
1765
+ {
1766
+ switch (slot) {
1767
+ case 0: SetInstruction0(instruction); return;
1768
+ case 1: SetInstruction1(instruction); return;
1769
+ case 2: SetInstruction2(instruction); return;
1770
+ }
1771
+ __debugbreak();
1772
+ }
1773
+
1774
+ void DETOUR_IA64_BUNDLE::SetInstruction0(UINT64 instruction)
1775
+ {
1776
+ wide[0] = SetBits(wide[0], DETOUR_IA64_INSTRUCTION0_OFFSET, DETOUR_IA64_INSTRUCTION_SIZE, instruction);
1777
+ }
1778
+
1779
+ void DETOUR_IA64_BUNDLE::SetInstruction1(UINT64 instruction)
1780
+ {
1781
+ UINT const count0 = 64 - DETOUR_IA64_INSTRUCTION1_OFFSET;
1782
+ UINT const count1 = DETOUR_IA64_INSTRUCTION_SIZE - count0;
1783
+ UINT64 const wide0 = SetBits(wide[0], DETOUR_IA64_INSTRUCTION1_OFFSET, count0, instruction);
1784
+ UINT64 const wide1 = SetBits(wide[1], 0, count1, instruction >> count0);
1785
+ wide[0] = wide0;
1786
+ wide[1] = wide1;
1787
+ }
1788
+
1789
+ void DETOUR_IA64_BUNDLE::SetInstruction2(UINT64 instruction)
1790
+ {
1791
+ // Set upper 41 bits of wide[1].
1792
+ wide[1] = SetBits(wide[1], 64 - DETOUR_IA64_INSTRUCTION_SIZE, DETOUR_IA64_INSTRUCTION_SIZE, instruction);
1793
+ }
1794
+
1795
+ UINT64 DETOUR_IA64_BUNDLE::SignExtend(UINT64 Value, UINT64 Offset)
1796
+ // This definition is from the IA64 manual.
1797
+ {
1798
+ if ((Value & (((UINT64)1) << (Offset - 1))) == 0)
1799
+ return Value;
1800
+ UINT64 const new_value = Value | ((~(UINT64)0) << Offset);
1801
+ return new_value;
1802
+ }
1803
+
1804
+ UINT64 DETOUR_IA64_BUNDLE::GetBits(UINT64 Value, UINT64 Offset, UINT64 Count)
1805
+ {
1806
+ UINT64 const new_value = (Value >> Offset) & ~(~((UINT64)0) << Count);
1807
+ return new_value;
1808
+ }
1809
+
1810
+ UINT64 DETOUR_IA64_BUNDLE::SetBits(UINT64 Value, UINT64 Offset, UINT64 Count, UINT64 Field)
1811
+ {
1812
+ UINT64 const mask = (~((~(UINT64)0) << Count)) << Offset;
1813
+ UINT64 const new_value = (Value & ~mask) | ((Field << Offset) & mask);
1814
+ return new_value;
1815
+ }
1816
+
1817
+ UINT64 DETOUR_IA64_BUNDLE::GetOpcode(UINT64 instruction)
1818
+ // Get 4bit primary opcode.
1819
+ {
1820
+ UINT64 const opcode = GetBits(instruction, DETOUR_IA64_INSTRUCTION_SIZE - 4, 4);
1821
+ return opcode;
1822
+ }
1823
+
1824
+ UINT64 DETOUR_IA64_BUNDLE::GetX(UINT64 instruction)
1825
+ // Get 1bit opcode extension.
1826
+ {
1827
+ UINT64 const x = GetBits(instruction, 33, 1);
1828
+ return x;
1829
+ }
1830
+
1831
+ UINT64 DETOUR_IA64_BUNDLE::GetX3(UINT64 instruction)
1832
+ // Get 3bit opcode extension.
1833
+ {
1834
+ UINT64 const x3 = GetBits(instruction, 33, 3);
1835
+ return x3;
1836
+ }
1837
+
1838
+ UINT64 DETOUR_IA64_BUNDLE::GetX6(UINT64 instruction)
1839
+ // Get 6bit opcode extension.
1840
+ {
1841
+ UINT64 const x6 = GetBits(instruction, 27, 6);
1842
+ return x6;
1843
+ }
1844
+
1845
+ UINT64 DETOUR_IA64_BUNDLE::GetImm7a(UINT64 instruction)
1846
+ {
1847
+ UINT64 const imm7a = GetBits(instruction, 6, 7);
1848
+ return imm7a;
1849
+ }
1850
+
1851
+ UINT64 DETOUR_IA64_BUNDLE::SetImm7a(UINT64 instruction, UINT64 imm7a)
1852
+ {
1853
+ UINT64 const new_instruction = SetBits(instruction, 6, 7, imm7a);
1854
+ return new_instruction;
1855
+ }
1856
+
1857
+ UINT64 DETOUR_IA64_BUNDLE::GetImm13c(UINT64 instruction)
1858
+ {
1859
+ UINT64 const imm13c = GetBits(instruction, 20, 13);
1860
+ return imm13c;
1861
+ }
1862
+
1863
+ UINT64 DETOUR_IA64_BUNDLE::SetImm13c(UINT64 instruction, UINT64 imm13c)
1864
+ {
1865
+ UINT64 const new_instruction = SetBits(instruction, 20, 13, imm13c);
1866
+ return new_instruction;
1867
+ }
1868
+
1869
+ UINT64 DETOUR_IA64_BUNDLE::GetSignBit(UINT64 instruction)
1870
+ {
1871
+ UINT64 const signBit = GetBits(instruction, 36, 1);
1872
+ return signBit;
1873
+ }
1874
+
1875
+ UINT64 DETOUR_IA64_BUNDLE::SetSignBit(UINT64 instruction, UINT64 signBit)
1876
+ {
1877
+ UINT64 const new_instruction = SetBits(instruction, 36, 1, signBit);
1878
+ return new_instruction;
1879
+ }
1880
+
1881
+ UINT64 DETOUR_IA64_BUNDLE::GetImm20a(UINT64 instruction)
1882
+ {
1883
+ UINT64 const imm20a = GetBits(instruction, 6, 20);
1884
+ return imm20a;
1885
+ }
1886
+
1887
+ UINT64 DETOUR_IA64_BUNDLE::SetImm20a(UINT64 instruction, UINT64 imm20a)
1888
+ {
1889
+ UINT64 const new_instruction = SetBits(instruction, 6, 20, imm20a);
1890
+ return new_instruction;
1891
+ }
1892
+
1893
+ UINT64 DETOUR_IA64_BUNDLE::GetImm20b(UINT64 instruction)
1894
+ {
1895
+ UINT64 const imm20b = GetBits(instruction, 13, 20);
1896
+ return imm20b;
1897
+ }
1898
+
1899
+ UINT64 DETOUR_IA64_BUNDLE::SetImm20b(UINT64 instruction, UINT64 imm20b)
1900
+ {
1901
+ UINT64 const new_instruction = SetBits(instruction, 13, 20, imm20b);
1902
+ return new_instruction;
1903
+ }
1904
+
1905
+ bool DETOUR_IA64_BUNDLE::RelocateInstruction(_Inout_ DETOUR_IA64_BUNDLE* pDst,
1906
+ _In_ BYTE slot,
1907
+ _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const
1908
+ /*
1909
+ If pBundleExtra is provided and instruction is IP-relative,
1910
+ this function relocates instruction to target pBundleExtra,
1911
+ pBundleExtra is set to brl the original target, and return true.
1912
+
1913
+ [Not used] If pBundleExtra is not provided and instruction is IP-relative, return true.
1914
+
1915
+ Else return false.
1916
+
1917
+ The following IP-relative forms are recognized:
1918
+ br and br.call
1919
+ chk.s.m integer and float
1920
+ chk.a.nc integer and float
1921
+ chk.a.clr integer and float
1922
+ chk.s.i
1923
+ fchkf
1924
+
1925
+ Brl is handled elsewhere, because the code was previously written.
1926
+
1927
+ Branch prediction hints are not relocated.
1928
+ */
1929
+ {
1930
+ UINT64 const instruction = GetInstruction(slot);
1931
+ UINT64 const opcode = GetOpcode(instruction);
1932
+ size_t const dest = (size_t)pDst;
1933
+ size_t const extra = (size_t)pBundleExtra;
1934
+
1935
+ switch (GetUnit(slot)) {
1936
+ case F_UNIT:
1937
+ // F14 fchkf
1938
+ if (opcode == 0 && GetX(instruction) == 0 && GetX6(instruction) == 8) {
1939
+ goto imm20a;
1940
+ }
1941
+ return false;
1942
+
1943
+ case M_UNIT:
1944
+ // M20 x3 == 1 integer chk.s.m
1945
+ // M21 x3 == 3 floating point chk.s
1946
+ if (opcode == 1) {
1947
+ UINT64 const x3 = GetX3(instruction);
1948
+ if (x3 == 1 || x3 == 3) {
1949
+ goto imm13_7;
1950
+ }
1951
+ }
1952
+
1953
+ // M22 x3 == 4 integer chk.a.nc
1954
+ // M22 x3 == 5 integer chk.a.clr
1955
+ // M23 x3 == 6 floating point chk.a.nc
1956
+ // M23 x3 == 7 floating point chk.a.clr
1957
+ if (opcode == 0) {
1958
+ UINT64 const x3 = GetX3(instruction);
1959
+ if (x3 == 4 || x3 == 5 || x3 == 6 || x3 == 7) {
1960
+ goto imm20b;
1961
+ }
1962
+ }
1963
+ return false;
1964
+ case I_UNIT:
1965
+ // I20
1966
+ if (opcode == 0 && GetX3(instruction) == 1) { // chk.s.i
1967
+ goto imm13_7;
1968
+ }
1969
+ return false;
1970
+ case B_UNIT:
1971
+ // B1 B2 B3
1972
+ // 4 br
1973
+ // 5 br.call
1974
+ if (opcode == 4 || opcode == 5) {
1975
+ goto imm20b;
1976
+ }
1977
+ return false;
1978
+ }
1979
+ return false;
1980
+
1981
+ UINT64 imm;
1982
+ UINT64 new_instruction;
1983
+
1984
+ imm13_7:
1985
+ imm = SignExtend((GetSignBit(instruction) << 20) | (GetImm13c(instruction) << 7) | GetImm7a(instruction), 21) << 4;
1986
+ new_instruction = SetSignBit(SetImm13c(SetImm7a(instruction, (extra - dest) >> 4), (extra - dest) >> 11), extra < dest);
1987
+ goto set_brl;
1988
+
1989
+ imm20a:
1990
+ imm = SignExtend((GetSignBit(instruction) << 20) | GetImm20a(instruction), 21) << 4;
1991
+ new_instruction = SetSignBit(SetImm20a(instruction, (extra - dest) >> 4), extra < dest);
1992
+ goto set_brl;
1993
+
1994
+ imm20b:
1995
+ imm = SignExtend((GetSignBit(instruction) << 20) | GetImm20b(instruction), 21) << 4;
1996
+ new_instruction = SetSignBit(SetImm20b(instruction, (extra - dest) >> 4), extra < dest);
1997
+ goto set_brl;
1998
+
1999
+ set_brl:
2000
+ if (pBundleExtra != NULL) {
2001
+ pDst->SetInstruction(slot, new_instruction);
2002
+ pBundleExtra->SetBrl((size_t)this + imm);
2003
+ }
2004
+ return true;
2005
+ }
2006
+
2007
+ UINT DETOUR_IA64_BUNDLE::RelocateBundle(_Inout_ DETOUR_IA64_BUNDLE* pDst,
2008
+ _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const
2009
+ /*
2010
+ Having already copied the bundle unchanged, then relocate its instructions one at a time.
2011
+ Return how many extra bytes are required to relocate the bundle.
2012
+ */
2013
+ {
2014
+ UINT nExtraBytes = 0;
2015
+ for (BYTE slot = 0; slot < DETOUR_IA64_INSTRUCTIONS_PER_BUNDLE; ++slot) {
2016
+ if (!RelocateInstruction(pDst, slot, pBundleExtra)) {
2017
+ continue;
2018
+ }
2019
+ pBundleExtra -= !!pBundleExtra;
2020
+ nExtraBytes += sizeof(DETOUR_IA64_BUNDLE);
2021
+ }
2022
+ return nExtraBytes;
2023
+ }
2024
+
2025
+ BOOL DETOUR_IA64_BUNDLE::IsBrl() const
2026
+ {
2027
+ // f.e. d.c. b.a. 9.8. 7.6. 5. 4. 3. 2. 1. 0.
2028
+ // c000 0070 0000 0000 0000 00 01 00 00 00 05 : brl.sptk.few
2029
+ // c8ff fff0 007f fff0 ffff 00 01 00 00 00 05 : brl.sptk.few
2030
+ // c000 0048 0000 0000 0001 00 00 00 00 00 05 : brl.sptk.many
2031
+ return ((wide[0] & 0x000000000000001e) == 0x0000000000000004 && // 4 or 5.
2032
+ (wide[1] & 0xe000000000000000) == 0xc000000000000000); // c or d.
2033
+ }
2034
+
2035
+ VOID DETOUR_IA64_BUNDLE::SetBrl()
2036
+ {
2037
+ wide[0] = 0x0000000100000005; // few
2038
+ //wide[0] = 0x0000000180000005; // many
2039
+ wide[1] = 0xc000000800000000;
2040
+ }
2041
+
2042
+ UINT64 DETOUR_IA64_BUNDLE::GetBrlImm() const
2043
+ {
2044
+ return (
2045
+ // 0x0000000000fffff0
2046
+ ((wide[1] & 0x00fffff000000000) >> 32) | // all 20 bits of imm20b.
2047
+ // 0x000000ffff000000
2048
+ ((wide[0] & 0xffff000000000000) >> 24) | // bottom 16 bits of imm39.
2049
+ // 0x7fffff0000000000
2050
+ ((wide[1] & 0x00000000007fffff) << 40) | // top 23 bits of imm39.
2051
+ // 0x8000000000000000
2052
+ ((wide[1] & 0x0800000000000000) << 4) // single bit of i.
2053
+ );
2054
+ }
2055
+
2056
+ VOID DETOUR_IA64_BUNDLE::SetBrlImm(UINT64 imm)
2057
+ {
2058
+ wide[0] = ((wide[0] & ~0xffff000000000000) |
2059
+ // 0xffff000000000000
2060
+ ((imm & 0x000000ffff000000) << 24) // bottom 16 bits of imm39.
2061
+ );
2062
+ wide[1] = ((wide[1] & ~0x08fffff0007fffff) |
2063
+ // 0x00fffff000000000
2064
+ ((imm & 0x0000000000fffff0) << 32) | // all 20 bits of imm20b.
2065
+ // 0x00000000007fffff
2066
+ ((imm & 0x7fffff0000000000) >> 40) | // top 23 bits of imm39.
2067
+ // 0x0800000000000000
2068
+ ((imm & 0x8000000000000000) >> 4) // single bit of i.
2069
+ );
2070
+ }
2071
+
2072
+ UINT64 DETOUR_IA64_BUNDLE::GetBrlTarget() const
2073
+ {
2074
+ return (UINT64)this + GetBrlImm();
2075
+ }
2076
+
2077
+ VOID DETOUR_IA64_BUNDLE::SetBrl(UINT64 target)
2078
+ {
2079
+ UINT64 imm = target - (UINT64)this;
2080
+ SetBrl();
2081
+ SetBrlImm(imm);
2082
+ }
2083
+
2084
+ VOID DETOUR_IA64_BUNDLE::SetBrlTarget(UINT64 target)
2085
+ {
2086
+ UINT64 imm = target - (UINT64)this;
2087
+ SetBrlImm(imm);
2088
+ }
2089
+
2090
+ BOOL DETOUR_IA64_BUNDLE::IsMovlGp() const
2091
+ {
2092
+ // f.e. d.c. b.a. 9.8. 7.6. 5.4. 3.2. 1.0.
2093
+ // 6fff f7f0 207f ffff ffff c001 0000 0004
2094
+ // 6000 0000 2000 0000 0000 0001 0000 0004
2095
+ return ((wide[0] & 0x00003ffffffffffe) == 0x0000000100000004 &&
2096
+ (wide[1] & 0xf000080fff800000) == 0x6000000020000000);
2097
+ }
2098
+
2099
+ UINT64 DETOUR_IA64_BUNDLE::GetMovlGp() const
2100
+ {
2101
+ UINT64 raw = (
2102
+ // 0x0000000000000070
2103
+ ((wide[1] & 0x000007f000000000) >> 36) |
2104
+ // 0x000000000000ff80
2105
+ ((wide[1] & 0x07fc000000000000) >> 43) |
2106
+ // 0x00000000001f0000
2107
+ ((wide[1] & 0x0003e00000000000) >> 29) |
2108
+ // 0x0000000000200000
2109
+ ((wide[1] & 0x0000100000000000) >> 23) |
2110
+ // 0x000000ffffc00000
2111
+ ((wide[0] & 0xffffc00000000000) >> 24) |
2112
+ // 0x7fffff0000000000
2113
+ ((wide[1] & 0x00000000007fffff) << 40) |
2114
+ // 0x8000000000000000
2115
+ ((wide[1] & 0x0800000000000000) << 4)
2116
+ );
2117
+
2118
+ return (INT64)raw;
2119
+ }
2120
+
2121
+ VOID DETOUR_IA64_BUNDLE::SetMovlGp(UINT64 gp)
2122
+ {
2123
+ UINT64 raw = (UINT64)gp;
2124
+
2125
+ wide[0] = (0x0000000100000005 |
2126
+ // 0xffffc00000000000
2127
+ ((raw & 0x000000ffffc00000) << 24)
2128
+ );
2129
+ wide[1] = (
2130
+ 0x6000000020000000 |
2131
+ // 0x0000070000000000
2132
+ ((raw & 0x0000000000000070) << 36) |
2133
+ // 0x07fc000000000000
2134
+ ((raw & 0x000000000000ff80) << 43) |
2135
+ // 0x0003e00000000000
2136
+ ((raw & 0x00000000001f0000) << 29) |
2137
+ // 0x0000100000000000
2138
+ ((raw & 0x0000000000200000) << 23) |
2139
+ // 0x00000000007fffff
2140
+ ((raw & 0x7fffff0000000000) >> 40) |
2141
+ // 0x0800000000000000
2142
+ ((raw & 0x8000000000000000) >> 4)
2143
+ );
2144
+ }
2145
+
2146
+ UINT DETOUR_IA64_BUNDLE::Copy(_Out_ DETOUR_IA64_BUNDLE *pDst,
2147
+ _Inout_opt_ DETOUR_IA64_BUNDLE* pBundleExtra) const
2148
+ {
2149
+ // Copy the bytes unchanged.
2150
+
2151
+ #pragma warning(suppress:6001) // using uninitialized *pDst
2152
+ pDst->wide[0] = wide[0];
2153
+ pDst->wide[1] = wide[1];
2154
+
2155
+ // Relocate if necessary.
2156
+
2157
+ UINT nExtraBytes = RelocateBundle(pDst, pBundleExtra);
2158
+
2159
+ if (GetUnit1() == L_UNIT && IsBrl()) {
2160
+ pDst->SetBrlTarget(GetBrlTarget());
2161
+ }
2162
+
2163
+ return nExtraBytes;
2164
+ }
2165
+
2166
+ BOOL DETOUR_IA64_BUNDLE::SetNop(BYTE slot)
2167
+ {
2168
+ switch (GetUnit(slot)) {
2169
+ case I_UNIT:
2170
+ case M_UNIT:
2171
+ case F_UNIT:
2172
+ SetInst(slot, 0);
2173
+ SetData(slot, 0x8000000);
2174
+ return true;
2175
+ case B_UNIT:
2176
+ SetInst(slot, 2);
2177
+ SetData(slot, 0);
2178
+ return true;
2179
+ }
2180
+ DebugBreak();
2181
+ return false;
2182
+ }
2183
+
2184
+ BOOL DETOUR_IA64_BUNDLE::SetNop0()
2185
+ {
2186
+ return SetNop(0);
2187
+ }
2188
+
2189
+ BOOL DETOUR_IA64_BUNDLE::SetNop1()
2190
+ {
2191
+ return SetNop(1);
2192
+ }
2193
+
2194
+ BOOL DETOUR_IA64_BUNDLE::SetNop2()
2195
+ {
2196
+ return SetNop(2);
2197
+ }
2198
+
2199
+ VOID DETOUR_IA64_BUNDLE::SetStop()
2200
+ {
2201
+ data[0] |= 0x01;
2202
+ }
2203
+
2204
+ #endif // DETOURS_IA64
2205
+
2206
+ PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst,
2207
+ _Inout_opt_ PVOID *ppDstPool,
2208
+ _In_ PVOID pSrc,
2209
+ _Out_opt_ PVOID *ppTarget,
2210
+ _Out_opt_ LONG *plExtra)
2211
+ {
2212
+ LONG nExtra;
2213
+ DETOUR_IA64_BUNDLE bExtra;
2214
+ DETOUR_IA64_BUNDLE *pbSrc = (DETOUR_IA64_BUNDLE *)pSrc;
2215
+ DETOUR_IA64_BUNDLE *pbDst = pDst ? (DETOUR_IA64_BUNDLE *)pDst : &bExtra;
2216
+
2217
+ plExtra = plExtra ? plExtra : &nExtra;
2218
+ *plExtra = 0;
2219
+
2220
+ if (ppTarget != NULL) {
2221
+ if (pbSrc->IsBrl()) {
2222
+ *ppTarget = (PVOID)pbSrc->GetBrlTarget();
2223
+ }
2224
+ else {
2225
+ *ppTarget = DETOUR_INSTRUCTION_TARGET_NONE;
2226
+ }
2227
+ }
2228
+ *plExtra = (LONG)pbSrc->Copy(pbDst, ppDstPool ? ((DETOUR_IA64_BUNDLE*)*ppDstPool) - 1 : (DETOUR_IA64_BUNDLE*)NULL);
2229
+ return pbSrc + 1;
2230
+ }
2231
+
2232
+ #endif // DETOURS_IA64
2233
+
2234
+ #ifdef DETOURS_ARM
2235
+
2236
+ #define DETOURS_PFUNC_TO_PBYTE(p) ((PBYTE)(((ULONG_PTR)(p)) & ~(ULONG_PTR)1))
2237
+ #define DETOURS_PBYTE_TO_PFUNC(p) ((PBYTE)(((ULONG_PTR)(p)) | (ULONG_PTR)1))
2238
+
2239
+ #define c_PCAdjust 4 // The PC value of an instruction is the PC address plus 4.
2240
+ #define c_PC 15 // The register number for the Program Counter
2241
+ #define c_LR 14 // The register number for the Link Register
2242
+ #define c_SP 13 // The register number for the Stack Pointer
2243
+ #define c_NOP 0xbf00 // A nop instruction
2244
+ #define c_BREAK 0xdefe // A nop instruction
2245
+
2246
+ class CDetourDis
2247
+ {
2248
+ public:
2249
+ CDetourDis();
2250
+
2251
+ PBYTE CopyInstruction(PBYTE pDst,
2252
+ PBYTE *ppDstPool,
2253
+ PBYTE pSrc,
2254
+ PBYTE *ppTarget,
2255
+ LONG *plExtra);
2256
+
2257
+ public:
2258
+ typedef BYTE (CDetourDis::* COPYFUNC)(PBYTE pbDst, PBYTE pbSrc);
2259
+
2260
+ struct COPYENTRY {
2261
+ USHORT nOpcode;
2262
+ COPYFUNC pfCopy;
2263
+ };
2264
+
2265
+ typedef const COPYENTRY * REFCOPYENTRY;
2266
+
2267
+ struct Branch5
2268
+ {
2269
+ DWORD Register : 3;
2270
+ DWORD Imm5 : 5;
2271
+ DWORD Padding : 1;
2272
+ DWORD I : 1;
2273
+ DWORD OpCode : 6;
2274
+ };
2275
+
2276
+ struct Branch5Target
2277
+ {
2278
+ DWORD Padding : 1;
2279
+ DWORD Imm5 : 5;
2280
+ DWORD I : 1;
2281
+ DWORD Padding2 : 25;
2282
+ };
2283
+
2284
+ struct Branch8
2285
+ {
2286
+ DWORD Imm8 : 8;
2287
+ DWORD Condition : 4;
2288
+ DWORD OpCode : 4;
2289
+ };
2290
+
2291
+ struct Branch8Target
2292
+ {
2293
+ DWORD Padding : 1;
2294
+ DWORD Imm8 : 8;
2295
+ DWORD Padding2 : 23;
2296
+ };
2297
+
2298
+ struct Branch11
2299
+ {
2300
+ DWORD Imm11 : 11;
2301
+ DWORD OpCode : 5;
2302
+ };
2303
+
2304
+ struct Branch11Target
2305
+ {
2306
+ DWORD Padding : 1;
2307
+ DWORD Imm11 : 11;
2308
+ DWORD Padding2 : 20;
2309
+ };
2310
+
2311
+ struct Branch20
2312
+ {
2313
+ DWORD Imm11 : 11;
2314
+ DWORD J2 : 1;
2315
+ DWORD IT : 1;
2316
+ DWORD J1 : 1;
2317
+ DWORD Other : 2;
2318
+ DWORD Imm6 : 6;
2319
+ DWORD Condition : 4;
2320
+ DWORD Sign : 1;
2321
+ DWORD OpCode : 5;
2322
+ };
2323
+
2324
+ struct Branch20Target
2325
+ {
2326
+ DWORD Padding : 1;
2327
+ DWORD Imm11 : 11;
2328
+ DWORD Imm6 : 6;
2329
+ DWORD J1 : 1;
2330
+ DWORD J2 : 1;
2331
+ DWORD Sign : 1;
2332
+ INT32 Padding2 : 11;
2333
+ };
2334
+
2335
+ struct Branch24
2336
+ {
2337
+ DWORD Imm11 : 11;
2338
+ DWORD J2 : 1;
2339
+ DWORD InstructionSet : 1;
2340
+ DWORD J1 : 1;
2341
+ DWORD Link : 1;
2342
+ DWORD Branch : 1;
2343
+ DWORD Imm10 : 10;
2344
+ DWORD Sign : 1;
2345
+ DWORD OpCode : 5;
2346
+ };
2347
+
2348
+ struct Branch24Target
2349
+ {
2350
+ DWORD Padding : 1;
2351
+ DWORD Imm11 : 11;
2352
+ DWORD Imm10 : 10;
2353
+ DWORD I2 : 1;
2354
+ DWORD I1 : 1;
2355
+ DWORD Sign : 1;
2356
+ INT32 Padding2 : 7;
2357
+ };
2358
+
2359
+ struct LiteralLoad8
2360
+ {
2361
+ DWORD Imm8 : 8;
2362
+ DWORD Register : 3;
2363
+ DWORD OpCode : 5;
2364
+ };
2365
+
2366
+ struct LiteralLoad8Target
2367
+ {
2368
+ DWORD Padding : 2;
2369
+ DWORD Imm8 : 8;
2370
+ DWORD Padding2 : 22;
2371
+ };
2372
+
2373
+ struct LiteralLoad12
2374
+ {
2375
+ DWORD Imm12 : 12;
2376
+ DWORD Register : 4;
2377
+ DWORD OpCodeSuffix : 7;
2378
+ DWORD Add : 1;
2379
+ DWORD OpCodePrefix : 8;
2380
+ };
2381
+
2382
+ struct LiteralLoad12Target
2383
+ {
2384
+ DWORD Imm12 : 12;
2385
+ DWORD Padding : 20;
2386
+ };
2387
+
2388
+ struct ImmediateRegisterLoad32
2389
+ {
2390
+ DWORD Imm12 : 12;
2391
+ DWORD DestinationRegister : 4;
2392
+ DWORD SourceRegister: 4;
2393
+ DWORD OpCode : 12;
2394
+ };
2395
+
2396
+ struct ImmediateRegisterLoad16
2397
+ {
2398
+ DWORD DestinationRegister : 3;
2399
+ DWORD SourceRegister: 3;
2400
+ DWORD OpCode : 10;
2401
+ };
2402
+
2403
+ struct TableBranch
2404
+ {
2405
+ DWORD IndexRegister : 4;
2406
+ DWORD HalfWord : 1;
2407
+ DWORD OpCodeSuffix : 11;
2408
+ DWORD BaseRegister : 4;
2409
+ DWORD OpCodePrefix : 12;
2410
+ };
2411
+
2412
+ struct Shift
2413
+ {
2414
+ DWORD Imm2 : 2;
2415
+ DWORD Imm3 : 3;
2416
+ };
2417
+
2418
+ struct Add32
2419
+ {
2420
+ DWORD SecondOperandRegister : 4;
2421
+ DWORD Type : 2;
2422
+ DWORD Imm2 : 2;
2423
+ DWORD DestinationRegister : 4;
2424
+ DWORD Imm3 : 3;
2425
+ DWORD Padding : 1;
2426
+ DWORD FirstOperandRegister : 4;
2427
+ DWORD SetFlags : 1;
2428
+ DWORD OpCode : 11;
2429
+ };
2430
+
2431
+ struct LogicalShiftLeft32
2432
+ {
2433
+ DWORD SourceRegister : 4;
2434
+ DWORD Padding : 2;
2435
+ DWORD Imm2 : 2;
2436
+ DWORD DestinationRegister : 4;
2437
+ DWORD Imm3 : 3;
2438
+ DWORD Padding2 : 5;
2439
+ DWORD SetFlags : 1;
2440
+ DWORD OpCode : 11;
2441
+ };
2442
+
2443
+ struct StoreImmediate12
2444
+ {
2445
+ DWORD Imm12 : 12;
2446
+ DWORD SourceRegister : 4;
2447
+ DWORD BaseRegister : 4;
2448
+ DWORD OpCode : 12;
2449
+ };
2450
+
2451
+ protected:
2452
+ BYTE PureCopy16(BYTE* pSource, BYTE* pDest);
2453
+ BYTE PureCopy32(BYTE* pSource, BYTE* pDest);
2454
+ BYTE CopyMiscellaneous16(BYTE* pSource, BYTE* pDest);
2455
+ BYTE CopyConditionalBranchOrOther16(BYTE* pSource, BYTE* pDest);
2456
+ BYTE CopyUnConditionalBranch16(BYTE* pSource, BYTE* pDest);
2457
+ BYTE CopyLiteralLoad16(BYTE* pSource, BYTE* pDest);
2458
+ BYTE CopyBranchExchangeOrDataProcessing16(BYTE* pSource, BYTE* pDest);
2459
+ BYTE CopyBranch24(BYTE* pSource, BYTE* pDest);
2460
+ BYTE CopyBranchOrMiscellaneous32(BYTE* pSource, BYTE* pDest);
2461
+ BYTE CopyLiteralLoad32(BYTE* pSource, BYTE* pDest);
2462
+ BYTE CopyLoadAndStoreSingle(BYTE* pSource, BYTE* pDest);
2463
+ BYTE CopyLoadAndStoreMultipleAndSRS(BYTE* pSource, BYTE* pDest);
2464
+ BYTE CopyTableBranch(BYTE* pSource, BYTE* pDest);
2465
+ BYTE BeginCopy32(BYTE* pSource, BYTE* pDest);
2466
+
2467
+ LONG DecodeBranch5(ULONG opcode);
2468
+ USHORT EncodeBranch5(ULONG originalOpCode, LONG delta);
2469
+ LONG DecodeBranch8(ULONG opcode);
2470
+ USHORT EncodeBranch8(ULONG originalOpCode, LONG delta);
2471
+ LONG DecodeBranch11(ULONG opcode);
2472
+ USHORT EncodeBranch11(ULONG originalOpCode, LONG delta);
2473
+ BYTE EmitBranch11(PUSHORT& pDest, LONG relativeAddress);
2474
+ LONG DecodeBranch20(ULONG opcode);
2475
+ ULONG EncodeBranch20(ULONG originalOpCode, LONG delta);
2476
+ LONG DecodeBranch24(ULONG opcode, BOOL& fLink);
2477
+ ULONG EncodeBranch24(ULONG originalOpCode, LONG delta, BOOL fLink);
2478
+ LONG DecodeLiteralLoad8(ULONG instruction);
2479
+ LONG DecodeLiteralLoad12(ULONG instruction);
2480
+ BYTE EmitLiteralLoad8(PUSHORT& pDest, BYTE targetRegister, PBYTE pLiteral);
2481
+ BYTE EmitLiteralLoad12(PUSHORT& pDest, BYTE targetRegister, PBYTE pLiteral);
2482
+ BYTE EmitImmediateRegisterLoad32(PUSHORT& pDest, BYTE reg);
2483
+ BYTE EmitImmediateRegisterLoad16(PUSHORT& pDest, BYTE reg);
2484
+ BYTE EmitLongLiteralLoad(PUSHORT& pDest, BYTE reg, PVOID pTarget);
2485
+ BYTE EmitLongBranch(PUSHORT& pDest, PVOID pTarget);
2486
+ USHORT CalculateExtra(BYTE sourceLength, BYTE* pDestStart, BYTE* pDestEnd);
2487
+
2488
+ protected:
2489
+ ULONG GetLongInstruction(BYTE* pSource)
2490
+ {
2491
+ return (((PUSHORT)pSource)[0] << 16) | (((PUSHORT)pSource)[1]);
2492
+ }
2493
+
2494
+ BYTE EmitLongInstruction(PUSHORT& pDstInst, ULONG instruction)
2495
+ {
2496
+ *pDstInst++ = (USHORT)(instruction >> 16);
2497
+ *pDstInst++ = (USHORT)instruction;
2498
+ return sizeof(ULONG);
2499
+ }
2500
+
2501
+ BYTE EmitShortInstruction(PUSHORT& pDstInst, USHORT instruction)
2502
+ {
2503
+ *pDstInst++ = instruction;
2504
+ return sizeof(USHORT);
2505
+ }
2506
+
2507
+ PBYTE Align4(PBYTE pValue)
2508
+ {
2509
+ return (PBYTE)(((size_t)pValue) & ~(ULONG)3u);
2510
+ }
2511
+
2512
+ PBYTE CalculateTarget(PBYTE pSource, LONG delta)
2513
+ {
2514
+ return (pSource + delta + c_PCAdjust);
2515
+ }
2516
+
2517
+ LONG CalculateNewDelta(PBYTE pTarget, BYTE* pDest)
2518
+ {
2519
+ return (LONG)(pTarget - (pDest + c_PCAdjust));
2520
+ }
2521
+
2522
+ BYTE EmitAdd32(PUSHORT& pDstInst, BYTE op1Reg, BYTE op2Reg, BYTE dstReg, BYTE shiftAmount)
2523
+ {
2524
+ Shift& shift = (Shift&)(shiftAmount);
2525
+ const BYTE shiftType = 0x00; // LSL
2526
+ Add32 add = { op2Reg, shiftType, shift.Imm2, dstReg, shift.Imm3,
2527
+ 0x0, op1Reg, 0x0, 0x758 };
2528
+ return EmitLongInstruction(pDstInst, (ULONG&)add);
2529
+ }
2530
+
2531
+ BYTE EmitLogicalShiftLeft32(PUSHORT& pDstInst, BYTE srcReg, BYTE dstReg, BYTE shiftAmount)
2532
+ {
2533
+ Shift& shift = (Shift&)(shiftAmount);
2534
+ LogicalShiftLeft32 shiftLeft = { srcReg, 0x00, shift.Imm2, dstReg, shift.Imm3, 0x1E,
2535
+ 0x00, 0x752 };
2536
+ return EmitLongInstruction(pDstInst, (ULONG&)shiftLeft);
2537
+ }
2538
+
2539
+ BYTE EmitStoreImmediate12(PUSHORT& pDstInst, BYTE srcReg, BYTE baseReg, USHORT offset)
2540
+ {
2541
+ StoreImmediate12 store = { offset, srcReg, baseReg, 0xF8C };
2542
+ return EmitLongInstruction(pDstInst, (ULONG&)store);
2543
+ }
2544
+
2545
+ protected:
2546
+ PBYTE m_pbTarget;
2547
+ PBYTE m_pbPool;
2548
+ LONG m_lExtra;
2549
+
2550
+ BYTE m_rbScratchDst[64];
2551
+
2552
+ static const COPYENTRY s_rceCopyTable[33];
2553
+ };
2554
+
2555
+ LONG CDetourDis::DecodeBranch5(ULONG opcode)
2556
+ {
2557
+ Branch5& branch = (Branch5&)(opcode);
2558
+
2559
+ Branch5Target target;
2560
+ ZeroMemory(&target, sizeof(target));
2561
+ target.Imm5 = branch.Imm5;
2562
+ target.I = branch.I;
2563
+
2564
+ // Return zero-extended value
2565
+ return (LONG&)target;
2566
+ }
2567
+
2568
+ USHORT CDetourDis::EncodeBranch5(ULONG originalOpCode, LONG delta)
2569
+ {
2570
+ // Too large for a 5 bit branch (5 bit branches can be up to 7 bits due to I and the trailing 0)
2571
+ if (delta < 0 || delta > 0x7F) {
2572
+ return 0;
2573
+ }
2574
+
2575
+ Branch5& branch = (Branch5&)(originalOpCode);
2576
+ Branch5Target& target = (Branch5Target&)(delta);
2577
+
2578
+ branch.Imm5 = target.Imm5;
2579
+ branch.I = target.I;
2580
+
2581
+ return (USHORT&)branch;
2582
+ }
2583
+
2584
+ LONG CDetourDis::DecodeBranch8(ULONG opcode)
2585
+ {
2586
+ Branch8& branch = (Branch8&)(opcode);
2587
+
2588
+ Branch8Target target;
2589
+ ZeroMemory(&target, sizeof(target));
2590
+ target.Imm8 = branch.Imm8;
2591
+
2592
+ // Return sign extended value
2593
+ return (((LONG&)target) << 23) >> 23;
2594
+ }
2595
+
2596
+ USHORT CDetourDis::EncodeBranch8(ULONG originalOpCode, LONG delta)
2597
+ {
2598
+ // Too large for 8 bit branch (8 bit branches can be up to 9 bits due to the trailing 0)
2599
+ if (delta < (-(int)0x100) || delta > 0xFF) {
2600
+ return 0;
2601
+ }
2602
+
2603
+ Branch8& branch = (Branch8&)(originalOpCode);
2604
+ Branch8Target& target = (Branch8Target&)(delta);
2605
+
2606
+ branch.Imm8 = target.Imm8;
2607
+
2608
+ return (USHORT&)branch;
2609
+ }
2610
+
2611
+ LONG CDetourDis::DecodeBranch11(ULONG opcode)
2612
+ {
2613
+ Branch11& branch = (Branch11&)(opcode);
2614
+
2615
+ Branch11Target target;
2616
+ ZeroMemory(&target, sizeof(target));
2617
+ target.Imm11 = branch.Imm11;
2618
+
2619
+ // Return sign extended value
2620
+ return (((LONG&)target) << 20) >> 20;
2621
+ }
2622
+
2623
+ USHORT CDetourDis::EncodeBranch11(ULONG originalOpCode, LONG delta)
2624
+ {
2625
+ // Too large for an 11 bit branch (11 bit branches can be up to 12 bits due to the trailing 0)
2626
+ if (delta < (-(int)0x800) || delta > 0x7FF) {
2627
+ return 0;
2628
+ }
2629
+
2630
+ Branch11& branch = (Branch11&)(originalOpCode);
2631
+ Branch11Target& target = (Branch11Target&)(delta);
2632
+
2633
+ branch.Imm11 = target.Imm11;
2634
+
2635
+ return (USHORT&)branch;
2636
+ }
2637
+
2638
+ BYTE CDetourDis::EmitBranch11(PUSHORT& pDest, LONG relativeAddress)
2639
+ {
2640
+ Branch11Target& target = (Branch11Target&)(relativeAddress);
2641
+ Branch11 branch11 = { target.Imm11, 0x1C };
2642
+
2643
+ *pDest++ = (USHORT&)branch11;
2644
+ return sizeof(USHORT);
2645
+ }
2646
+
2647
+ LONG CDetourDis::DecodeBranch20(ULONG opcode)
2648
+ {
2649
+ Branch20& branch = (Branch20&)(opcode);
2650
+
2651
+ Branch20Target target;
2652
+ ZeroMemory(&target, sizeof(target));
2653
+ target.Imm11 = branch.Imm11;
2654
+ target.Imm6 = branch.Imm6;
2655
+ target.Sign = branch.Sign;
2656
+ target.J1 = branch.J1;
2657
+ target.J2 = branch.J2;
2658
+
2659
+ // Sign extend
2660
+ if (target.Sign) {
2661
+ target.Padding2 = -1;
2662
+ }
2663
+
2664
+ return (LONG&)target;
2665
+ }
2666
+
2667
+ ULONG CDetourDis::EncodeBranch20(ULONG originalOpCode, LONG delta)
2668
+ {
2669
+ // Too large for 20 bit branch (20 bit branches can be up to 21 bits due to the trailing 0)
2670
+ if (delta < (-(int)0x100000) || delta > 0xFFFFF) {
2671
+ return 0;
2672
+ }
2673
+
2674
+ Branch20& branch = (Branch20&)(originalOpCode);
2675
+ Branch20Target& target = (Branch20Target&)(delta);
2676
+
2677
+ branch.Imm11 = target.Imm11;
2678
+ branch.Imm6 = target.Imm6;
2679
+ branch.Sign = target.Sign;
2680
+ branch.J1 = target.J1;
2681
+ branch.J2 = target.J2;
2682
+
2683
+ return (ULONG&)branch;
2684
+ }
2685
+
2686
+ LONG CDetourDis::DecodeBranch24(ULONG opcode, BOOL& fLink)
2687
+ {
2688
+ Branch24& branch = (Branch24&)(opcode);
2689
+
2690
+ Branch24Target target;
2691
+ ZeroMemory(&target, sizeof(target));
2692
+ target.Imm11 = branch.Imm11;
2693
+ target.Imm10 = branch.Imm10;
2694
+ target.Sign = branch.Sign;
2695
+ target.I1 = ~(branch.J1 ^ target.Sign);
2696
+ target.I2 = ~(branch.J2 ^ target.Sign);
2697
+ fLink = branch.Link;
2698
+
2699
+ // Sign extend
2700
+ if (target.Sign) {
2701
+ target.Padding2 = -1;
2702
+ }
2703
+
2704
+ return (LONG&)target;
2705
+ }
2706
+
2707
+ ULONG CDetourDis::EncodeBranch24(ULONG originalOpCode, LONG delta, BOOL fLink)
2708
+ {
2709
+ // Too large for 24 bit branch (24 bit branches can be up to 25 bits due to the trailing 0)
2710
+ if (delta < static_cast<int>(0xFF000000) || delta > static_cast<int>(0xFFFFFF)) {
2711
+ return 0;
2712
+ }
2713
+
2714
+ Branch24& branch = (Branch24&)(originalOpCode);
2715
+ Branch24Target& target = (Branch24Target&)(delta);
2716
+
2717
+ branch.Imm11 = target.Imm11;
2718
+ branch.Imm10 = target.Imm10;
2719
+ branch.Link = fLink;
2720
+ branch.Sign = target.Sign;
2721
+ branch.J1 = ~(target.I1 ^ branch.Sign);
2722
+ branch.J2 = ~(target.I2 ^ branch.Sign);
2723
+
2724
+ return (ULONG&)branch;
2725
+ }
2726
+
2727
+ LONG CDetourDis::DecodeLiteralLoad8(ULONG instruction)
2728
+ {
2729
+ LiteralLoad8& load = (LiteralLoad8&)(instruction);
2730
+
2731
+ LiteralLoad8Target target;
2732
+ ZeroMemory(&target, sizeof(target));
2733
+ target.Imm8 = load.Imm8;
2734
+
2735
+ return (LONG&)target;
2736
+ }
2737
+
2738
+ BYTE CDetourDis::EmitLiteralLoad8(PUSHORT& pDest, BYTE targetRegister, PBYTE pLiteral)
2739
+ {
2740
+ // Note: We add 2 (which gets rounded down) because literals must be 32-bit
2741
+ // aligned, but the ldr can be 16-bit aligned.
2742
+ LONG newDelta = CalculateNewDelta((PBYTE)pLiteral + 2, (PBYTE)pDest);
2743
+ LONG relative = ((newDelta > 0 ? newDelta : -newDelta) & 0x3FF);
2744
+
2745
+ LiteralLoad8Target& target = (LiteralLoad8Target&)(relative);
2746
+ LiteralLoad8 load = { target.Imm8, targetRegister, 0x9 };
2747
+
2748
+ return EmitShortInstruction(pDest, (USHORT&)load);
2749
+ }
2750
+
2751
+ LONG CDetourDis::DecodeLiteralLoad12(ULONG instruction)
2752
+ {
2753
+ LiteralLoad12& load = (LiteralLoad12&)(instruction);
2754
+
2755
+ LiteralLoad12Target target;
2756
+ ZeroMemory(&target, sizeof(target));
2757
+ target.Imm12 = load.Imm12;
2758
+
2759
+ return (LONG&)target;
2760
+ }
2761
+
2762
+ BYTE CDetourDis::EmitLiteralLoad12(PUSHORT& pDest, BYTE targetRegister, PBYTE pLiteral)
2763
+ {
2764
+ // Note: We add 2 (which gets rounded down) because literals must be 32-bit
2765
+ // aligned, but the ldr can be 16-bit aligned.
2766
+ LONG newDelta = CalculateNewDelta((PBYTE)pLiteral + 2, (PBYTE)pDest);
2767
+ LONG relative = ((newDelta > 0 ? newDelta : -newDelta) & 0xFFF);
2768
+
2769
+ LiteralLoad12Target& target = (LiteralLoad12Target&)(relative);
2770
+ target.Imm12 -= target.Imm12 & 3;
2771
+ LiteralLoad12 load = { target.Imm12, targetRegister, 0x5F, (DWORD)(newDelta > 0), 0xF8 };
2772
+
2773
+ return EmitLongInstruction(pDest, (ULONG&)load);
2774
+ }
2775
+
2776
+ BYTE CDetourDis::EmitImmediateRegisterLoad32(PUSHORT& pDest, BYTE reg)
2777
+ {
2778
+ ImmediateRegisterLoad32 load = { 0, reg, reg, 0xF8D };
2779
+ return EmitLongInstruction(pDest, (ULONG&)load);
2780
+ }
2781
+
2782
+ BYTE CDetourDis::EmitImmediateRegisterLoad16(PUSHORT& pDest, BYTE reg)
2783
+ {
2784
+ ImmediateRegisterLoad16 load = { reg, reg, 0x680 >> 2 };
2785
+ return EmitShortInstruction(pDest, (USHORT&)load);
2786
+ }
2787
+
2788
+ BYTE CDetourDis::EmitLongLiteralLoad(PUSHORT& pDest, BYTE targetRegister, PVOID pTarget)
2789
+ {
2790
+ *--((PULONG&)m_pbPool) = (ULONG)(size_t)pTarget;
2791
+
2792
+ // ldr rn, target.
2793
+ BYTE size = EmitLiteralLoad12(pDest, targetRegister, m_pbPool);
2794
+
2795
+ // This only makes sense if targetRegister != PC;
2796
+ // otherwise, we would have branched with the previous instruction anyway
2797
+ if (targetRegister != c_PC) {
2798
+ // ldr rn, [rn]
2799
+ if (targetRegister <= 7) {
2800
+ size = (BYTE)(size + EmitImmediateRegisterLoad16(pDest, targetRegister));
2801
+ }
2802
+ else {
2803
+ size = (BYTE)(size + EmitImmediateRegisterLoad32(pDest, targetRegister));
2804
+ }
2805
+ }
2806
+
2807
+ return size;
2808
+ }
2809
+
2810
+ BYTE CDetourDis::EmitLongBranch(PUSHORT& pDest, PVOID pTarget)
2811
+ {
2812
+ // Emit a long literal load into PC
2813
+ BYTE size = EmitLongLiteralLoad(pDest, c_PC, DETOURS_PBYTE_TO_PFUNC(pTarget));
2814
+ return size;
2815
+ }
2816
+
2817
+ BYTE CDetourDis::PureCopy16(BYTE* pSource, BYTE* pDest)
2818
+ {
2819
+ *(USHORT *)pDest = *(USHORT *)pSource;
2820
+ return sizeof(USHORT);
2821
+ }
2822
+
2823
+ BYTE CDetourDis::PureCopy32(BYTE* pSource, BYTE* pDest)
2824
+ {
2825
+ *(UNALIGNED ULONG *)pDest = *(UNALIGNED ULONG*)pSource;
2826
+ return sizeof(DWORD);
2827
+ }
2828
+
2829
+ USHORT CDetourDis::CalculateExtra(BYTE sourceLength, BYTE* pDestStart, BYTE* pDestEnd)
2830
+ {
2831
+ ULONG destinationLength = (ULONG)(pDestEnd - pDestStart);
2832
+ return static_cast<USHORT>((destinationLength > sourceLength) ? (destinationLength - sourceLength) : 0);
2833
+ }
2834
+
2835
+ BYTE CDetourDis::CopyMiscellaneous16(BYTE* pSource, BYTE* pDest)
2836
+ {
2837
+ USHORT instruction = *(PUSHORT)(pSource);
2838
+
2839
+ // Compare and branch imm5 (CBZ, CBNZ)
2840
+ if ((instruction & 0x100) && !(instruction & 0x400)) { // (1011x0x1xxxxxxxx)
2841
+ LONG oldDelta = DecodeBranch5(instruction);
2842
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
2843
+ m_pbTarget = pTarget;
2844
+
2845
+ LONG newDelta = CalculateNewDelta(pTarget, pDest);
2846
+ instruction = EncodeBranch5(instruction, newDelta);
2847
+
2848
+ if (instruction) {
2849
+ // Copy the 16 bit instruction over
2850
+ *(PUSHORT)(pDest) = instruction;
2851
+ return sizeof(USHORT); // The source instruction was 16 bits
2852
+ }
2853
+
2854
+ // If that fails, re-encode with 'conditional branch' logic, without using the condition flags
2855
+ // For example, cbz r2,+0x56 (0x90432) becomes:
2856
+ //
2857
+ // 001df73a b92a cbnz r2,001df748
2858
+ // 001df73c e002 b 001df744
2859
+ // 001df73e bf00 nop
2860
+ // 001df740 0432 dc.h 0432
2861
+ // 001df742 0009 dc.h 0009
2862
+ // 001df744 f85ff008 ldr pc,=0x90432
2863
+ //
2864
+
2865
+ // Store where we will be writing our conditional branch, and move past it so we can emit a long branch
2866
+ PUSHORT pDstInst = (PUSHORT)(pDest);
2867
+ PUSHORT pConditionalBranchInstruction = pDstInst++;
2868
+
2869
+ // Emit the long branch instruction
2870
+ BYTE longBranchSize = EmitLongBranch(pDstInst, pTarget);
2871
+
2872
+ // Invert the CBZ/CBNZ instruction to move past our 'long branch' if the inverse comparison succeeds
2873
+ // Write the CBZ/CBNZ instruction *before* the long branch we emitted above
2874
+ // This had to be done out of order, since the size of a long branch can vary due to alignment restrictions
2875
+ instruction = EncodeBranch5(*(PUSHORT)(pSource), longBranchSize - c_PCAdjust + sizeof(USHORT));
2876
+ Branch5& branch = (Branch5&)(instruction);
2877
+ branch.OpCode = (branch.OpCode & 0x02) ? 0x2C : 0x2E; // Invert the CBZ/CBNZ comparison
2878
+ *pConditionalBranchInstruction = instruction;
2879
+
2880
+ // Compute the extra space needed for the branch sequence
2881
+ m_lExtra = CalculateExtra(sizeof(USHORT), pDest, (BYTE*)(pDstInst));
2882
+ return sizeof(USHORT); // The source instruction was 16 bits
2883
+ }
2884
+
2885
+ // If-Then Instruction (IT)
2886
+ if ((instruction >> 8 == 0xBF) && (instruction & 0xF)) { //(10111111xxxx(mask != 0b0000))
2887
+ // ToDo: Implement IT handler
2888
+ ASSERT(false);
2889
+ return sizeof(USHORT);
2890
+ }
2891
+
2892
+ // ADD/SUB, SXTH, SXTB, UXTH, UXTB, CBZ, CBNZ, PUSH, POP, REV, REV15, REVSH, NOP, YIELD, WFE, WFI, SEV, etc.
2893
+ return PureCopy16(pSource, pDest);
2894
+ }
2895
+
2896
+ BYTE CDetourDis::CopyConditionalBranchOrOther16(BYTE* pSource, BYTE* pDest)
2897
+ {
2898
+ USHORT instruction = *(PUSHORT)(pSource);
2899
+
2900
+ // Could be a conditional branch, an Undefined instruction or a Service System Call
2901
+ // Only the former needs special logic
2902
+ if ((instruction & 0xE00) != 0xE00) { // 1101(!=111x)xxxxxxxx
2903
+ LONG oldDelta = DecodeBranch8(instruction);
2904
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
2905
+ m_pbTarget = pTarget;
2906
+
2907
+ LONG newDelta = CalculateNewDelta(pTarget, pDest);
2908
+ instruction = EncodeBranch8(instruction, newDelta);
2909
+ if (instruction) {
2910
+ // Copy the 16 bit instruction over
2911
+ *(PUSHORT)(pDest) = instruction;
2912
+ return sizeof(USHORT); // The source instruction was 16 bits
2913
+ }
2914
+
2915
+ // If that fails, re-encode as a sequence of branches
2916
+ // For example, bne +0x6E (0x90452) becomes:
2917
+ //
2918
+ // 001df758 d100 bne 001df75c
2919
+ // 001df75a e005 b 001df768
2920
+ // 001df75c e002 b 001df764
2921
+ // 001df75e bf00 nop
2922
+ // 001df760 0452 dc.h 0452
2923
+ // 001df762 0009 dc.h 0009
2924
+ // 001df764 f85ff008 ldr pc,=0x90452
2925
+ //
2926
+
2927
+ // First, reuse the existing conditional branch to, if successful, branch down to a 'long branch' that we will emit below
2928
+ USHORT newInstruction = EncodeBranch8(*(PUSHORT)(pSource), 0); // Due to the size of c_PCAdjust a zero-length branch moves 4 bytes forward, past the following unconditional branch
2929
+ ASSERT(newInstruction);
2930
+ PUSHORT pDstInst = (PUSHORT)(pDest);
2931
+ *pDstInst++ = newInstruction;
2932
+
2933
+ // Next, prepare to insert an unconditional branch that will be hit if the condition above is not met. This branch will branch over the following 'long branch'
2934
+ // We can't actually encode this branch yet though, because 'long branches' can vary in size
2935
+ PUSHORT pUnconditionalBranchInstruction = pDstInst++;
2936
+
2937
+ // Then, emit a 'long branch' that will be hit if the original condition is met
2938
+ BYTE longBranchSize = EmitLongBranch(pDstInst, pTarget);
2939
+
2940
+ // Finally, encode and emit the unconditional branch that will be used to branch past the 'long branch' if the initial condition was not met
2941
+ Branch11 branch11 = { 0x00, 0x1C };
2942
+ newInstruction = EncodeBranch11(*(DWORD*)(&branch11), longBranchSize - c_PCAdjust + sizeof(USHORT));
2943
+ ASSERT(newInstruction);
2944
+ *pUnconditionalBranchInstruction = newInstruction;
2945
+
2946
+ // Compute the extra space needed for the branch sequence
2947
+ m_lExtra = CalculateExtra(sizeof(USHORT), pDest, (BYTE*)(pDstInst));
2948
+ return sizeof(USHORT); // The source instruction was 16 bits
2949
+ }
2950
+
2951
+ return PureCopy16(pSource, pDest);
2952
+ }
2953
+
2954
+ BYTE CDetourDis::CopyUnConditionalBranch16(BYTE* pSource, BYTE* pDest)
2955
+ {
2956
+ ULONG instruction = *(PUSHORT)(pSource);
2957
+
2958
+ LONG oldDelta = DecodeBranch11(instruction);
2959
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
2960
+ m_pbTarget = pTarget;
2961
+
2962
+ LONG newDelta = CalculateNewDelta(pTarget, pDest);
2963
+ instruction = EncodeBranch11(instruction, newDelta);
2964
+ if (instruction) {
2965
+ // Copy the 16 bit instruction over
2966
+ *(PUSHORT)(pDest) = (USHORT)instruction;
2967
+ return sizeof(USHORT); // The source instruction was 16 bits
2968
+ }
2969
+
2970
+ // If that fails, re-encode as 32-bit
2971
+ PUSHORT pDstInst = (PUSHORT)(pDest);
2972
+ instruction = EncodeBranch24(0xf0009000, newDelta, FALSE);
2973
+ if (instruction) {
2974
+ // Copy both bytes of the instruction
2975
+ EmitLongInstruction(pDstInst, instruction);
2976
+
2977
+ m_lExtra = sizeof(DWORD) - sizeof(USHORT); // The destination instruction was 32 bits
2978
+ return sizeof(USHORT); // The source instruction was 16 bits
2979
+ }
2980
+
2981
+ // If that fails, emit as a 'long branch'
2982
+ if (!instruction) {
2983
+ // For example, b +0x7FE (00090be6) becomes:
2984
+ // 003f6d02 e001 b 003f6d08
2985
+ // 003f6d04 0be6 dc.h 0be6
2986
+ // 003f6d06 0009 dc.h 0009
2987
+ // 003f6d08 f85ff008 ldr pc,=0x90BE6
2988
+ EmitLongBranch(pDstInst, pTarget);
2989
+
2990
+ // Compute the extra space needed for the branch sequence
2991
+ m_lExtra = CalculateExtra(sizeof(USHORT), pDest, (BYTE*)(pDstInst));
2992
+ return sizeof(USHORT); // The source instruction was 16 bits
2993
+ }
2994
+
2995
+ return sizeof(USHORT); // The source instruction was 16 bits
2996
+ }
2997
+
2998
+ BYTE CDetourDis::CopyLiteralLoad16(BYTE* pSource, BYTE* pDest)
2999
+ {
3000
+ PBYTE pStart = pDest;
3001
+ USHORT instruction = *(PUSHORT)(pSource);
3002
+
3003
+ LONG oldDelta = DecodeLiteralLoad8(instruction);
3004
+ PBYTE pTarget = CalculateTarget(Align4(pSource), oldDelta);
3005
+
3006
+ // Re-encode as a 'long literal load'
3007
+ // For example, ldr r0, [PC + 1E0] (0x905B4) becomes:
3008
+ //
3009
+ // 001df72c f85f0008 ldr r0,=0x905B4
3010
+ // 001df730 f8d00000 ldr.w r0,[r0]
3011
+ LiteralLoad8& load8 = (LiteralLoad8&)(instruction);
3012
+ EmitLongLiteralLoad((PUSHORT&)pDest, load8.Register, pTarget);
3013
+
3014
+ m_lExtra = (LONG)(pDest - pStart - sizeof(USHORT));
3015
+ return sizeof(USHORT); // The source instruction was 16 bits
3016
+ }
3017
+
3018
+ BYTE CDetourDis::CopyBranchExchangeOrDataProcessing16(BYTE* pSource, BYTE* pDest)
3019
+ {
3020
+ ULONG instruction = *(PUSHORT)(pSource);
3021
+
3022
+ // BX
3023
+ if ((instruction & 0xff80) == 0x4700) {
3024
+ // The target is stored in a register
3025
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3026
+ }
3027
+
3028
+ // AND, LSR, TST, ADD, CMP, MOV
3029
+ return PureCopy16(pSource, pDest);
3030
+ }
3031
+
3032
+ const CDetourDis::COPYENTRY CDetourDis::s_rceCopyTable[33] =
3033
+ {
3034
+ // Shift by immediate, move register
3035
+ // ToDo: Not handling moves from PC
3036
+ /* 0b00000 */ { 0x00, &CDetourDis::PureCopy16 },
3037
+ /* 0b00001 */ { 0x01, &CDetourDis::PureCopy16 },
3038
+ /* 0b00010 */ { 0x02, &CDetourDis::PureCopy16 },
3039
+
3040
+ // Add/subtract register
3041
+ // Add/subtract immediate
3042
+ /* 0b00011 */ { 0x03, &CDetourDis::PureCopy16},
3043
+
3044
+ // Add/subtract/compare/move immediate
3045
+ /* 0b00100 */ { 0x04, &CDetourDis::PureCopy16 },
3046
+ /* 0b00101 */ { 0x05, &CDetourDis::PureCopy16 },
3047
+ /* 0b00110 */ { 0x06, &CDetourDis::PureCopy16 },
3048
+ /* 0b00111 */ { 0x07, &CDetourDis::PureCopy16 },
3049
+
3050
+ // Data-processing register
3051
+ // Special data processing
3052
+ // Branch/exchange instruction set
3053
+ /* 0b01000 */ { 0x08, &CDetourDis::CopyBranchExchangeOrDataProcessing16 },
3054
+
3055
+ // Load from literal pool
3056
+ /* 0b01001 */ { 0x09, &CDetourDis::CopyLiteralLoad16 },
3057
+
3058
+ // Load/store register offset
3059
+ /* 0b01010 */ { 0x0a, &CDetourDis::PureCopy16 },
3060
+ /* 0b01011 */ { 0x0b, &CDetourDis::PureCopy16 },
3061
+
3062
+ // Load/store word/byte immediate offset.
3063
+ /* 0b01100 */ { 0x0c, &CDetourDis::PureCopy16 },
3064
+ /* 0b01101 */ { 0x0d, &CDetourDis::PureCopy16 },
3065
+ /* 0b01110 */ { 0x0e, &CDetourDis::PureCopy16 },
3066
+ /* 0b01111 */ { 0x0f, &CDetourDis::PureCopy16 },
3067
+
3068
+ // Load/store halfword immediate offset.
3069
+ /* 0b10000 */ { 0x10, &CDetourDis::PureCopy16 },
3070
+ /* 0b10001 */ { 0x11, &CDetourDis::PureCopy16 },
3071
+
3072
+ // Load from or store to stack
3073
+ /* 0b10010 */ { 0x12, &CDetourDis::PureCopy16 },
3074
+ /* 0b10011 */ { 0x13, &CDetourDis::PureCopy16 },
3075
+
3076
+ // Add to SP or PC
3077
+ /* 0b10100 */ { 0x14, &CDetourDis::PureCopy16 },
3078
+ // ToDo: Is ADR (T1) blitt-able?
3079
+ // It adds a value to PC and stores the result in a register.
3080
+ // Does this count as a 'target' for detours?
3081
+ /* 0b10101 */ { 0x15, &CDetourDis::PureCopy16 },
3082
+
3083
+ // Miscellaneous
3084
+ /* 0b10110 */ { 0x16, &CDetourDis::CopyMiscellaneous16 },
3085
+ /* 0b10111 */ { 0x17, &CDetourDis::CopyMiscellaneous16 },
3086
+
3087
+ // Load/store multiple
3088
+ /* 0b11000 */ { 0x18, &CDetourDis::PureCopy16 },
3089
+ /* 0b11001 */ { 0x19, &CDetourDis::PureCopy16 },
3090
+ // ToDo: Are we sure these are all safe?
3091
+ // LDMIA, for example, can include an 'embedded' branch.
3092
+ // Does this count as a 'target' for detours?
3093
+
3094
+ // Conditional branch
3095
+ /* 0b11010 */ { 0x1a, &CDetourDis::CopyConditionalBranchOrOther16 },
3096
+
3097
+ // Conditional branch
3098
+ // Undefined instruction
3099
+ // Service (system) call
3100
+ /* 0b11011 */ { 0x1b, &CDetourDis::CopyConditionalBranchOrOther16 },
3101
+
3102
+ // Unconditional branch
3103
+ /* 0b11100 */ { 0x1c, &CDetourDis::CopyUnConditionalBranch16 },
3104
+
3105
+ // 32-bit instruction
3106
+ /* 0b11101 */ { 0x1d, &CDetourDis::BeginCopy32 },
3107
+ /* 0b11110 */ { 0x1e, &CDetourDis::BeginCopy32 },
3108
+ /* 0b11111 */ { 0x1f, &CDetourDis::BeginCopy32 },
3109
+ { 0, NULL }
3110
+ };
3111
+
3112
+ BYTE CDetourDis::CopyBranch24(BYTE* pSource, BYTE* pDest)
3113
+ {
3114
+ ULONG instruction = GetLongInstruction(pSource);
3115
+ BOOL fLink;
3116
+ LONG oldDelta = DecodeBranch24(instruction, fLink);
3117
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
3118
+ m_pbTarget = pTarget;
3119
+
3120
+ // Re-encode as 32-bit
3121
+ PUSHORT pDstInst = (PUSHORT)(pDest);
3122
+ LONG newDelta = CalculateNewDelta(pTarget, pDest);
3123
+ instruction = EncodeBranch24(instruction, newDelta, fLink);
3124
+ if (instruction) {
3125
+ // Copy both bytes of the instruction
3126
+ EmitLongInstruction(pDstInst, instruction);
3127
+ return sizeof(DWORD);
3128
+ }
3129
+
3130
+ // If that fails, re-encode as a 'long branch'
3131
+ EmitLongBranch(pDstInst, pTarget);
3132
+
3133
+ // Compute the extra space needed for the instruction
3134
+ m_lExtra = CalculateExtra(sizeof(DWORD), pDest, (BYTE*)(pDstInst));
3135
+ return sizeof(DWORD); // The source instruction was 32 bits
3136
+ }
3137
+
3138
+ BYTE CDetourDis::CopyBranchOrMiscellaneous32(BYTE* pSource, BYTE* pDest)
3139
+ {
3140
+ ULONG instruction = GetLongInstruction(pSource);
3141
+ if ((instruction & 0xf800d000) == 0xf0008000) { // B<c>.W <label>
3142
+ LONG oldDelta = DecodeBranch20(instruction);
3143
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
3144
+ m_pbTarget = pTarget;
3145
+
3146
+ // Re-encode as 32-bit
3147
+ PUSHORT pDstInst = (PUSHORT)(pDest);
3148
+ LONG newDelta = CalculateNewDelta(pTarget, pDest);
3149
+ instruction = EncodeBranch20(instruction, newDelta);
3150
+ if (instruction) {
3151
+ // Copy both bytes of the instruction
3152
+ EmitLongInstruction(pDstInst, instruction);
3153
+ return sizeof(DWORD);
3154
+ }
3155
+
3156
+ // If that fails, re-encode as a sequence of branches
3157
+ // For example, bls.w +0x86 (00090480)| becomes:
3158
+ //
3159
+ // 001df788 f2408001 bls.w 001df78e
3160
+ // 001df78c e004 b 001df798
3161
+ // 001df78e e001 b 001df794
3162
+ // 001df790 0480 dc.h 0480
3163
+ // 001df792 0009 dc.h 0009
3164
+ // 001df794 f85ff008 ldr pc,=0x90480
3165
+ //
3166
+
3167
+ // First, reuse the existing conditional branch to, if successful,
3168
+ // branch down to a 'long branch' that we will emit below
3169
+ instruction = EncodeBranch20(GetLongInstruction(pSource), 2);
3170
+ // Due to the size of c_PCAdjust a two-length branch moves 6 bytes forward,
3171
+ // past the following unconditional branch
3172
+ ASSERT(instruction);
3173
+ EmitLongInstruction(pDstInst, instruction);
3174
+
3175
+ // Next, prepare to insert an unconditional branch that will be hit
3176
+ // if the condition above is not met. This branch will branch over
3177
+ // the following 'long branch'
3178
+ // We can't actually encode this branch yet though, because
3179
+ // 'long branches' can vary in size
3180
+ PUSHORT pUnconditionalBranchInstruction = pDstInst++;
3181
+
3182
+ // Then, emit a 'long branch' that will be hit if the original condition is met
3183
+ BYTE longBranchSize = EmitLongBranch(pDstInst, pTarget);
3184
+
3185
+ // Finally, encode and emit the unconditional branch that will be used
3186
+ // to branch past the 'long branch' if the initial condition was not met
3187
+ Branch11 branch11 = { 0x00, 0x1C };
3188
+ instruction = EncodeBranch11(*(DWORD*)(&branch11), longBranchSize - c_PCAdjust + sizeof(USHORT));
3189
+ ASSERT(instruction);
3190
+ *pUnconditionalBranchInstruction = static_cast<USHORT>(instruction);
3191
+
3192
+ // Compute the extra space needed for the instruction
3193
+ m_lExtra = CalculateExtra(sizeof(DWORD), pDest, (BYTE*)(pDstInst));
3194
+ return sizeof(DWORD); // The source instruction was 32 bits
3195
+ }
3196
+
3197
+ if ((instruction & 0xf800d000) == 0xf0009000) { // B.W <label>
3198
+ // B <label> 11110xxxxxxxxxxx10xxxxxxxxxxxxxx
3199
+ return CopyBranch24(pSource, pDest);
3200
+ }
3201
+
3202
+ if ((instruction & 0xf800d000) == 0xf000d000) { // BL.W <label>
3203
+ // B <label> 11110xxxxxxxxxxx10xxxxxxxxxxxxxx
3204
+
3205
+ PUSHORT pDstInst = (PUSHORT)(pDest);
3206
+ BOOL fLink;
3207
+ LONG oldDelta = DecodeBranch24(instruction, fLink);
3208
+ PBYTE pTarget = CalculateTarget(pSource, oldDelta);
3209
+ m_pbTarget = pTarget;
3210
+
3211
+ *--((PULONG&)m_pbPool) = (ULONG)(size_t)DETOURS_PBYTE_TO_PFUNC(pTarget);
3212
+
3213
+ // ldr lr, target.
3214
+ EmitLiteralLoad12(pDstInst, c_LR, m_pbPool);
3215
+ // blx lr
3216
+ EmitShortInstruction(pDstInst, 0x47f0);
3217
+
3218
+ // Compute the extra space needed for the instruction
3219
+ m_lExtra = CalculateExtra(sizeof(DWORD), pDest, (BYTE*)(pDstInst));
3220
+ return sizeof(DWORD); // The source instruction was 32 bits
3221
+ }
3222
+
3223
+ if ((instruction & 0xFFF0FFFF) == 0xF3C08F00) {
3224
+ // BXJ 111100111100xxxx1000111100000000
3225
+ // BXJ switches to Jazelle mode, which is not supported
3226
+ ASSERT(false);
3227
+ }
3228
+
3229
+ if ((instruction & 0xFFFFFF00) == 0xF3DE8F00) {
3230
+ // SUBS PC, LR 111100111101111010001111xxxxxxxx
3231
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3232
+ }
3233
+
3234
+ // Everything else should be blitt-able
3235
+ return PureCopy32(pSource, pDest);
3236
+ }
3237
+
3238
+ BYTE CDetourDis::CopyLiteralLoad32(BYTE* pSource, BYTE* pDest)
3239
+ {
3240
+ BYTE* pStart = pDest;
3241
+ ULONG instruction = GetLongInstruction(pSource);
3242
+
3243
+ LONG oldDelta = DecodeLiteralLoad12(instruction);
3244
+ PBYTE pTarget = CalculateTarget(Align4(pSource), oldDelta);
3245
+
3246
+ LiteralLoad12& load = (LiteralLoad12&)(instruction);
3247
+
3248
+ EmitLongLiteralLoad((PUSHORT&)pDest, load.Register, pTarget);
3249
+
3250
+ m_lExtra = (LONG)(pDest - pStart - sizeof(DWORD));
3251
+
3252
+ return sizeof(DWORD); // The source instruction was 32 bits
3253
+ }
3254
+
3255
+ BYTE CDetourDis::CopyLoadAndStoreSingle(BYTE* pSource, BYTE* pDest)
3256
+ {
3257
+ ULONG instruction = GetLongInstruction(pSource);
3258
+
3259
+ // Note: The following masks only look at the interesting bits
3260
+ // (not the opCode prefix, since that check was performed in
3261
+ // order to get to this function)
3262
+ if (!(instruction & 0x100000)) {
3263
+ // 1111 100x xxx0 xxxxxxxxxxxxxxxxxxxx : STR, STRB, STRH, etc.
3264
+ return PureCopy32(pSource, pDest);
3265
+ }
3266
+
3267
+ if ((instruction & 0xF81F0000) == 0xF81F0000) {
3268
+ // 1111100xxxx11111xxxxxxxxxxxxxxxx : PC +/- Imm12
3269
+ return CopyLiteralLoad32(pSource, pDest);
3270
+ }
3271
+
3272
+ if ((instruction & 0xFE70F000) == 0xF81FF000) {
3273
+ // 1111100xx001xxxx1111xxxxxxxxxxxx : PLD, PLI
3274
+ // Convert PC-Relative PLD/PLI instructions to noops (1111100Xx00111111111xxxxxxxxxxxx)
3275
+ if ((instruction & 0xFE7FF000) == 0xF81FF000) {
3276
+ PUSHORT pDstInst = (PUSHORT)(pDest);
3277
+ *pDstInst++ = c_NOP;
3278
+ *pDstInst++ = c_NOP;
3279
+ return sizeof(DWORD); // The source instruction was 32 bits
3280
+ }
3281
+
3282
+ // All other PLD/PLI instructions are blitt-able
3283
+ return PureCopy32(pSource, pDest);
3284
+ }
3285
+
3286
+ // If the load is writing to PC
3287
+ if ((instruction & 0xF950F000) == 0xF850F000) {
3288
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3289
+ }
3290
+
3291
+ // All other loads LDR (immediate), etc.
3292
+ return PureCopy32(pSource, pDest);
3293
+ }
3294
+
3295
+ BYTE CDetourDis::CopyLoadAndStoreMultipleAndSRS(BYTE* pSource, BYTE* pDest)
3296
+ {
3297
+ // Probably all blitt-able, although not positive since some of these can result in a branch (LDMIA, POP, etc.)
3298
+ return PureCopy32(pSource, pDest);
3299
+ }
3300
+
3301
+ BYTE CDetourDis::CopyTableBranch(BYTE* pSource, BYTE* pDest)
3302
+ {
3303
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3304
+ ULONG instruction = GetLongInstruction(pSource);
3305
+ TableBranch& tableBranch = (TableBranch&)(instruction);
3306
+
3307
+ // If the base register is anything other than PC, we can simply copy the instruction
3308
+ if (tableBranch.BaseRegister != c_PC) {
3309
+ return PureCopy32(pSource, pDest);
3310
+ }
3311
+
3312
+ __debugbreak();
3313
+
3314
+ // If the base register is PC, we need to manually perform the table lookup
3315
+ // For example, this:
3316
+ //
3317
+ // 7ef40000 e8dff002 tbb [pc,r2]
3318
+ //
3319
+ // becomes this:
3320
+ //
3321
+ // 7ef40404 b401 push {r0} ; pushed as a placeholder for the target address
3322
+ // 7ef40406 e92d0005 push.w {r0,r2} ; scratch register and another register are pushed; there's a minimum of two registers in the list for push.w
3323
+ // 7ef40410 4820 ldr r0,=0x7EF40004 ; load the table address from the literal pool
3324
+ // 7ef40414 eb000042 add r0,r0,r2,lsl #1 ; add the index value to the address of the table to get the table entry; lsl only used if it's a TBH instruction
3325
+ // 7ef40418 f8d00000 ldr.w r0,[r0] ; dereference the table entry to get the value of the target
3326
+ // 7ef4041c ea4f0040 lsl r0,r0,#1 ; multiply the offset by 2 (per the spec)
3327
+ // 7ef40420 eb00000f add.w r0,r0,pc ; Add the offset to pc to get the target address
3328
+ // 7ef40424 f8cd000c str.w r0,[sp,#0xC] ; store the target address on the stack (into the first push)
3329
+ // 7ef40428 e8bd0005 pop.w {r0,r2} ; scratch register and another register are popped; there's a minimum of two registers in the list for pop.w
3330
+ // 7ef4042c bd00 pop {pc} ; pop the address into pc
3331
+ //
3332
+
3333
+ // Push r0 to make room for our jump address on the stack
3334
+ PUSHORT pDstInst = (PUSHORT)(pDest);
3335
+ *pDstInst++ = 0xb401;
3336
+
3337
+ // Locate a scratch register
3338
+ BYTE scrReg = 0;
3339
+ while (scrReg == tableBranch.IndexRegister) {
3340
+ ++scrReg;
3341
+ }
3342
+
3343
+ // Push scrReg and tableBranch.IndexRegister (push.w doesn't support pushing just 1 register)
3344
+ DWORD pushInstruction = 0xe92d0000;
3345
+ pushInstruction |= 1 << scrReg;
3346
+ pushInstruction |= 1 << tableBranch.IndexRegister;
3347
+ EmitLongInstruction(pDstInst, pushInstruction);
3348
+
3349
+ // Write the target address out to the 'literal pool';
3350
+ // when the base register of a TBB/TBH is PC,
3351
+ // the branch table immediately follows the instruction
3352
+ BYTE* pTarget = CalculateTarget(pSource, 0);
3353
+ *--((PUSHORT&)m_pbPool) = (USHORT)((size_t)pTarget & 0xffff);
3354
+ *--((PUSHORT&)m_pbPool) = (USHORT)((size_t)pTarget >> 16);
3355
+
3356
+ // Load the literal pool value into our scratch register (this contains the address of the branch table)
3357
+ // ldr rn, target
3358
+ EmitLiteralLoad8(pDstInst, scrReg, m_pbPool);
3359
+
3360
+ // Add the index offset to the address of the branch table; the result will be the value within the table that contains the branch offset
3361
+ // We need to multiply the index by two if we are using halfword indexing
3362
+ // Will shift tableBranch.IndexRegister by 1 (multiply by 2) if using a TBH
3363
+ EmitAdd32(pDstInst, scrReg, tableBranch.IndexRegister, scrReg, tableBranch.HalfWord);
3364
+
3365
+ // Dereference rn into rn, to load the value within the table
3366
+ // ldr rn, [rn]
3367
+ if (scrReg < 0x7) {
3368
+ EmitImmediateRegisterLoad16(pDstInst, scrReg);
3369
+ }
3370
+ else {
3371
+ EmitImmediateRegisterLoad32(pDstInst, scrReg);
3372
+ }
3373
+
3374
+ // Multiply the offset by two to get the true offset value (as per the spec)
3375
+ EmitLogicalShiftLeft32(pDstInst, scrReg, scrReg, 1);
3376
+
3377
+ // Add the offset to PC to get the target
3378
+ EmitAdd32(pDstInst, scrReg, c_PC, scrReg, 0);
3379
+
3380
+ // Now write the contents of scrReg to the stack, so we can pop it into PC
3381
+ // Write the address of the branch table entry to the stack, so we can pop it into PC
3382
+ EmitStoreImmediate12(pDstInst, scrReg, c_SP, sizeof(DWORD) * 3);
3383
+
3384
+ // Pop scrReg and tableBranch.IndexRegister (pop.w doesn't support popping just 1 register)
3385
+ DWORD popInstruction = 0xe8bd0000;
3386
+ popInstruction |= 1 << scrReg;
3387
+ popInstruction |= 1 << tableBranch.IndexRegister;
3388
+ EmitLongInstruction(pDstInst, popInstruction);
3389
+
3390
+ // Pop PC
3391
+ *pDstInst++ = 0xbd00;
3392
+
3393
+ // Compute the extra space needed for the branch sequence
3394
+ m_lExtra = CalculateExtra(sizeof(USHORT), pDest, (BYTE*)(pDstInst));
3395
+ return sizeof(DWORD);
3396
+ }
3397
+
3398
+ BYTE CDetourDis::BeginCopy32(BYTE* pSource, BYTE* pDest)
3399
+ {
3400
+ ULONG instruction = GetLongInstruction(pSource);
3401
+
3402
+ // Immediate data processing instructions; ADD, SUB, MOV, MOVN, ADR, MOVT, BFC, SSAT16, etc.
3403
+ if ((instruction & 0xF8008000) == 0xF0000000) { // 11110xxxxxxxxxxx0xxxxxxxxxxxxxxx
3404
+ // Should all be blitt-able
3405
+ // ToDo: What about ADR? Is it safe to do a straight-copy?
3406
+ // ToDo: Not handling moves to or from PC
3407
+ return PureCopy32(pSource, pDest);
3408
+ }
3409
+
3410
+ // Non-Immediate data processing instructions; ADD, EOR, TST, etc.
3411
+ if ((instruction & 0xEE000000) == 0xEA000000) { // 111x101xxxxxxxxxxxxxxxxxxxxxxx
3412
+ // Should all be blitt-able
3413
+ return PureCopy32(pSource, pDest);
3414
+ }
3415
+
3416
+ // Load and store single data item, memory hints
3417
+ if ((instruction & 0xFE000000) == 0xF8000000) { // 1111100xxxxxxxxxxxxxxxxxxxxxxxxx
3418
+ return CopyLoadAndStoreSingle(pSource, pDest);
3419
+ }
3420
+
3421
+ // Load and store, double and exclusive, and table branch
3422
+ if ((instruction & 0xFE400000) == 0xE8400000) { // 1110100xx1xxxxxxxxxxxxxxxxxxxxxx
3423
+ // Load and store double
3424
+ if (instruction & 0x1200000) {
3425
+ // LDRD, STRD (immediate) : xxxxxxxPxxWxxxxxxxxxxxxxxxxxxxxx where PW != 0b00
3426
+ // The source register is PC
3427
+ if ((instruction & 0xF0000) == 0xF0000) {
3428
+ // ToDo: If the source register is PC, what should we do?
3429
+ ASSERT(false);
3430
+ }
3431
+
3432
+ // If either target registers are PC
3433
+ if (((instruction & 0xF000) == 0xF000) ||
3434
+ ((instruction & 0xF00) == 0xF00)) {
3435
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3436
+ }
3437
+
3438
+ return PureCopy32(pSource, pDest);
3439
+ }
3440
+
3441
+ // Load and store exclusive
3442
+ if (!(instruction & 0x800000)) { // LDREX, STREX : xxxxxxxx0xxxxxxxxxxxxxxxxxxxxxxx
3443
+ if ((instruction & 0xF000) == 0xF000) { // xxxxxxxxxxxx1111xxxxxxxxxxxx
3444
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_DYNAMIC;
3445
+ }
3446
+ return PureCopy32(pSource, pDest);
3447
+ }
3448
+
3449
+ // Table branch
3450
+ if ((instruction & 0x1000F0) == 0x100000 || // TBB : xxxxxxxxxxx1xxxxxxxxxxxx0000xxxx
3451
+ (instruction & 0x1000F0) == 0x100010) { // TBH : xxxxxxxxxxx1xxxxxxxxxxxx0001xxxx
3452
+ return CopyTableBranch(pSource, pDest);
3453
+ }
3454
+
3455
+ // Load and store exclusive byte, halfword, doubleword (LDREXB, LDREXH, LDREXD, STREXB, STREXH, STREXD, etc.)
3456
+ return PureCopy32(pSource, pDest);
3457
+ }
3458
+
3459
+ // Load and store multiple, RFE and SRS
3460
+ if ((instruction & 0xFE400000) == 0xE8000000) { // 1110100xx0xxxxxxxxxxxxxxxxxxxxxx
3461
+ // Return from exception (RFE)
3462
+ if ((instruction & 0xE9900000) == 0xE9900000 || // 1110100110x1xxxxxxxxxxxxxxxxxxxx
3463
+ (instruction & 0xE8100000) == 0xE8100000) { // 1110100000x1xxxxxxxxxxxxxxxxxxxx
3464
+ return PureCopy32(pSource, pDest);
3465
+ }
3466
+
3467
+ return CopyLoadAndStoreMultipleAndSRS(pSource, pDest);
3468
+ }
3469
+
3470
+ // Branches, miscellaneous control
3471
+ if ((instruction & 0xF8008000) == 0xF0008000) { // 11110xxxxxxxxxxx0xxxxxxxxxxxxxxx
3472
+ // Branches, miscellaneous control
3473
+ return CopyBranchOrMiscellaneous32(pSource, pDest);
3474
+ }
3475
+
3476
+ // Coprocessor instructions
3477
+ if ((instruction & 0xEC000000) == 0xEC000000) { // 111x11xxxxxxxxxxxxxxxxxxxxxxxxxx
3478
+ return PureCopy32(pSource, pDest);
3479
+ }
3480
+
3481
+ // Unhandled instruction; should never make it this far
3482
+ ASSERT(false);
3483
+ return PureCopy32(pSource, pDest);
3484
+ }
3485
+
3486
+ /////////////////////////////////////////////////////////// Disassembler Code.
3487
+ //
3488
+ CDetourDis::CDetourDis()
3489
+ {
3490
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_NONE;
3491
+ m_pbPool = NULL;
3492
+ m_lExtra = 0;
3493
+ }
3494
+
3495
+ PBYTE CDetourDis::CopyInstruction(PBYTE pDst,
3496
+ PBYTE *ppDstPool,
3497
+ PBYTE pSrc,
3498
+ PBYTE *ppTarget,
3499
+ LONG *plExtra)
3500
+ {
3501
+ if (pDst && ppDstPool && ppDstPool != NULL) {
3502
+ m_pbPool = (PBYTE)*ppDstPool;
3503
+ }
3504
+ else {
3505
+ pDst = m_rbScratchDst;
3506
+ m_pbPool = m_rbScratchDst + sizeof(m_rbScratchDst);
3507
+ }
3508
+ // Make sure the constant pool is 32-bit aligned.
3509
+ m_pbPool -= ((ULONG_PTR)m_pbPool) & 3;
3510
+
3511
+ REFCOPYENTRY pEntry = &s_rceCopyTable[pSrc[1] >> 3];
3512
+ ULONG size = (this->*pEntry->pfCopy)(pSrc, pDst);
3513
+
3514
+ pSrc += size;
3515
+
3516
+ // If the target is needed, store our target
3517
+ if (ppTarget) {
3518
+ *ppTarget = m_pbTarget;
3519
+ }
3520
+ if (plExtra) {
3521
+ *plExtra = m_lExtra;
3522
+ }
3523
+ if (ppDstPool) {
3524
+ *ppDstPool = m_pbPool;
3525
+ }
3526
+
3527
+ return pSrc;
3528
+ }
3529
+
3530
+
3531
+ PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst,
3532
+ _Inout_opt_ PVOID *ppDstPool,
3533
+ _In_ PVOID pSrc,
3534
+ _Out_opt_ PVOID *ppTarget,
3535
+ _Out_opt_ LONG *plExtra)
3536
+ {
3537
+ CDetourDis state;
3538
+ return (PVOID)state.CopyInstruction((PBYTE)pDst,
3539
+ (PBYTE*)ppDstPool,
3540
+ (PBYTE)pSrc,
3541
+ (PBYTE*)ppTarget,
3542
+ plExtra);
3543
+ }
3544
+
3545
+ #endif // DETOURS_ARM
3546
+
3547
+ #ifdef DETOURS_ARM64
3548
+
3549
+ #define c_LR 30 // The register number for the Link Register
3550
+ #define c_SP 31 // The register number for the Stack Pointer
3551
+ #define c_NOP 0xd503201f // A nop instruction
3552
+ #define c_BREAK (0xd4200000 | (0xf000 << 5)) // A break instruction
3553
+
3554
+ //
3555
+ // Problematic instructions:
3556
+ //
3557
+ // ADR 0ll10000 hhhhhhhh hhhhhhhh hhhddddd & 0x9f000000 == 0x10000000 (l = low, h = high, d = Rd)
3558
+ // ADRP 1ll10000 hhhhhhhh hhhhhhhh hhhddddd & 0x9f000000 == 0x90000000 (l = low, h = high, d = Rd)
3559
+ //
3560
+ // B.cond 01010100 iiiiiiii iiiiiiii iii0cccc & 0xff000010 == 0x54000000 (i = delta = SignExtend(imm19:00, 64), c = cond)
3561
+ //
3562
+ // B 000101ii iiiiiiii iiiiiiii iiiiiiii & 0xfc000000 == 0x14000000 (i = delta = SignExtend(imm26:00, 64))
3563
+ // BL 100101ii iiiiiiii iiiiiiii iiiiiiii & 0xfc000000 == 0x94000000 (i = delta = SignExtend(imm26:00, 64))
3564
+ //
3565
+ // CBNZ z0110101 iiiiiiii iiiiiiii iiittttt & 0x7f000000 == 0x35000000 (z = size, i = delta = SignExtend(imm19:00, 64), t = Rt)
3566
+ // CBZ z0110100 iiiiiiii iiiiiiii iiittttt & 0x7f000000 == 0x34000000 (z = size, i = delta = SignExtend(imm19:00, 64), t = Rt)
3567
+ //
3568
+ // LDR Wt 00011000 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x18000000 (i = SignExtend(imm19:00, 64), t = Rt)
3569
+ // LDR Xt 01011000 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x58000000 (i = SignExtend(imm19:00, 64), t = Rt)
3570
+ // LDRSW 10011000 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x98000000 (i = SignExtend(imm19:00, 64), t = Rt)
3571
+ // PRFM 11011000 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0xd8000000 (i = SignExtend(imm19:00, 64), t = Rt)
3572
+ // LDR St 00011100 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x1c000000 (i = SignExtend(imm19:00, 64), t = Rt)
3573
+ // LDR Dt 01011100 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x5c000000 (i = SignExtend(imm19:00, 64), t = Rt)
3574
+ // LDR Qt 10011100 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0x9c000000 (i = SignExtend(imm19:00, 64), t = Rt)
3575
+ // LDR inv 11011100 iiiiiiii iiiiiiii iiittttt & 0xff000000 == 0xdc000000 (i = SignExtend(imm19:00, 64), t = Rt)
3576
+ //
3577
+ // TBNZ z0110111 bbbbbiii iiiiiiii iiittttt & 0x7f000000 == 0x37000000 (z = size, b = bitnum, i = SignExtend(imm14:00, 64), t = Rt)
3578
+ // TBZ z0110110 bbbbbiii iiiiiiii iiittttt & 0x7f000000 == 0x36000000 (z = size, b = bitnum, i = SignExtend(imm14:00, 64), t = Rt)
3579
+ //
3580
+
3581
+ class CDetourDis
3582
+ {
3583
+ public:
3584
+ CDetourDis();
3585
+
3586
+ PBYTE CopyInstruction(PBYTE pDst,
3587
+ PBYTE pSrc,
3588
+ PBYTE *ppTarget,
3589
+ LONG *plExtra);
3590
+
3591
+ public:
3592
+ typedef BYTE (CDetourDis::* COPYFUNC)(PBYTE pbDst, PBYTE pbSrc);
3593
+
3594
+ union AddImm12
3595
+ {
3596
+ DWORD Assembled;
3597
+ struct
3598
+ {
3599
+ DWORD Rd : 5; // Destination register
3600
+ DWORD Rn : 5; // Source register
3601
+ DWORD Imm12 : 12; // 12-bit immediate
3602
+ DWORD Shift : 2; // shift (must be 0 or 1)
3603
+ DWORD Opcode1 : 7; // Must be 0010001 == 0x11
3604
+ DWORD Size : 1; // 0 = 32-bit, 1 = 64-bit
3605
+ } s;
3606
+ static DWORD Assemble(DWORD size, DWORD rd, DWORD rn, ULONG imm, DWORD shift)
3607
+ {
3608
+ AddImm12 temp;
3609
+ temp.s.Rd = rd;
3610
+ temp.s.Rn = rn;
3611
+ temp.s.Imm12 = imm & 0xfff;
3612
+ temp.s.Shift = shift;
3613
+ temp.s.Opcode1 = 0x11;
3614
+ temp.s.Size = size;
3615
+ return temp.Assembled;
3616
+ }
3617
+ static DWORD AssembleAdd32(DWORD rd, DWORD rn, ULONG imm, DWORD shift) { return Assemble(0, rd, rn, imm, shift); }
3618
+ static DWORD AssembleAdd64(DWORD rd, DWORD rn, ULONG imm, DWORD shift) { return Assemble(1, rd, rn, imm, shift); }
3619
+ };
3620
+
3621
+ union Adr19
3622
+ {
3623
+ DWORD Assembled;
3624
+ struct
3625
+ {
3626
+ DWORD Rd : 5; // Destination register
3627
+ DWORD Imm19 : 19; // 19-bit upper immediate
3628
+ DWORD Opcode1 : 5; // Must be 10000 == 0x10
3629
+ DWORD Imm2 : 2; // 2-bit lower immediate
3630
+ DWORD Type : 1; // 0 = ADR, 1 = ADRP
3631
+ } s;
3632
+ inline LONG Imm() const { DWORD Imm = (s.Imm19 << 2) | s.Imm2; return (LONG)(Imm << 11) >> 11; }
3633
+ static DWORD Assemble(DWORD type, DWORD rd, LONG delta)
3634
+ {
3635
+ Adr19 temp;
3636
+ temp.s.Rd = rd;
3637
+ temp.s.Imm19 = (delta >> 2) & 0x7ffff;
3638
+ temp.s.Opcode1 = 0x10;
3639
+ temp.s.Imm2 = delta & 3;
3640
+ temp.s.Type = type;
3641
+ return temp.Assembled;
3642
+ }
3643
+ static DWORD AssembleAdr(DWORD rd, LONG delta) { return Assemble(0, rd, delta); }
3644
+ static DWORD AssembleAdrp(DWORD rd, LONG delta) { return Assemble(1, rd, delta); }
3645
+ };
3646
+
3647
+ union Bcc19
3648
+ {
3649
+ DWORD Assembled;
3650
+ struct
3651
+ {
3652
+ DWORD Condition : 4; // Condition
3653
+ DWORD Opcode1 : 1; // Must be 0
3654
+ DWORD Imm19 : 19; // 19-bit immediate
3655
+ DWORD Opcode2 : 8; // Must be 01010100 == 0x54
3656
+ } s;
3657
+ inline LONG Imm() const { return (LONG)(s.Imm19 << 13) >> 11; }
3658
+ static DWORD AssembleBcc(DWORD condition, LONG delta)
3659
+ {
3660
+ Bcc19 temp;
3661
+ temp.s.Condition = condition;
3662
+ temp.s.Opcode1 = 0;
3663
+ temp.s.Imm19 = delta >> 2;
3664
+ temp.s.Opcode2 = 0x54;
3665
+ return temp.Assembled;
3666
+ }
3667
+ };
3668
+
3669
+ union Branch26
3670
+ {
3671
+ DWORD Assembled;
3672
+ struct
3673
+ {
3674
+ DWORD Imm26 : 26; // 26-bit immediate
3675
+ DWORD Opcode1 : 5; // Must be 00101 == 0x5
3676
+ DWORD Link : 1; // 0 = B, 1 = BL
3677
+ } s;
3678
+ inline LONG Imm() const { return (LONG)(s.Imm26 << 6) >> 4; }
3679
+ static DWORD Assemble(DWORD link, LONG delta)
3680
+ {
3681
+ Branch26 temp;
3682
+ temp.s.Imm26 = delta >> 2;
3683
+ temp.s.Opcode1 = 0x5;
3684
+ temp.s.Link = link;
3685
+ return temp.Assembled;
3686
+ }
3687
+ static DWORD AssembleB(LONG delta) { return Assemble(0, delta); }
3688
+ static DWORD AssembleBl(LONG delta) { return Assemble(1, delta); }
3689
+ };
3690
+
3691
+ union Br
3692
+ {
3693
+ DWORD Assembled;
3694
+ struct
3695
+ {
3696
+ DWORD Opcode1 : 5; // Must be 00000 == 0
3697
+ DWORD Rn : 5; // Register number
3698
+ DWORD Opcode2 : 22; // Must be 1101011000011111000000 == 0x3587c0
3699
+ } s;
3700
+ static DWORD AssembleBr(DWORD rn)
3701
+ {
3702
+ Br temp;
3703
+ temp.s.Opcode1 = 0;
3704
+ temp.s.Rn = rn;
3705
+ temp.s.Opcode2 = 0x3587c0;
3706
+ return temp.Assembled;
3707
+ }
3708
+ };
3709
+
3710
+ union Cbz19
3711
+ {
3712
+ DWORD Assembled;
3713
+ struct
3714
+ {
3715
+ DWORD Rt : 5; // Register to test
3716
+ DWORD Imm19 : 19; // 19-bit immediate
3717
+ DWORD Nz : 1; // 0 = CBZ, 1 = CBNZ
3718
+ DWORD Opcode1 : 6; // Must be 011010 == 0x1a
3719
+ DWORD Size : 1; // 0 = 32-bit, 1 = 64-bit
3720
+ } s;
3721
+ inline LONG Imm() const { return (LONG)(s.Imm19 << 13) >> 11; }
3722
+ static DWORD Assemble(DWORD size, DWORD nz, DWORD rt, LONG delta)
3723
+ {
3724
+ Cbz19 temp;
3725
+ temp.s.Rt = rt;
3726
+ temp.s.Imm19 = delta >> 2;
3727
+ temp.s.Nz = nz;
3728
+ temp.s.Opcode1 = 0x1a;
3729
+ temp.s.Size = size;
3730
+ return temp.Assembled;
3731
+ }
3732
+ };
3733
+
3734
+ union LdrLit19
3735
+ {
3736
+ DWORD Assembled;
3737
+ struct
3738
+ {
3739
+ DWORD Rt : 5; // Destination register
3740
+ DWORD Imm19 : 19; // 19-bit immediate
3741
+ DWORD Opcode1 : 2; // Must be 0
3742
+ DWORD FpNeon : 1; // 0 = LDR Wt/LDR Xt/LDRSW/PRFM, 1 = LDR St/LDR Dt/LDR Qt
3743
+ DWORD Opcode2 : 3; // Must be 011 = 3
3744
+ DWORD Size : 2; // 00 = LDR Wt/LDR St, 01 = LDR Xt/LDR Dt, 10 = LDRSW/LDR Qt, 11 = PRFM/invalid
3745
+ } s;
3746
+ inline LONG Imm() const { return (LONG)(s.Imm19 << 13) >> 11; }
3747
+ static DWORD Assemble(DWORD size, DWORD fpneon, DWORD rt, LONG delta)
3748
+ {
3749
+ LdrLit19 temp;
3750
+ temp.s.Rt = rt;
3751
+ temp.s.Imm19 = delta >> 2;
3752
+ temp.s.Opcode1 = 0;
3753
+ temp.s.FpNeon = fpneon;
3754
+ temp.s.Opcode2 = 3;
3755
+ temp.s.Size = size;
3756
+ return temp.Assembled;
3757
+ }
3758
+ };
3759
+
3760
+ union LdrFpNeonImm9
3761
+ {
3762
+ DWORD Assembled;
3763
+ struct
3764
+ {
3765
+ DWORD Rt : 5; // Destination register
3766
+ DWORD Rn : 5; // Base register
3767
+ DWORD Imm12 : 12; // 12-bit immediate
3768
+ DWORD Opcode1 : 1; // Must be 1 == 1
3769
+ DWORD Opc : 1; // Part of size
3770
+ DWORD Opcode2 : 6; // Must be 111101 == 0x3d
3771
+ DWORD Size : 2; // Size (0=8-bit, 1=16-bit, 2=32-bit, 3=64-bit, 4=128-bit)
3772
+ } s;
3773
+ static DWORD Assemble(DWORD size, DWORD rt, DWORD rn, ULONG imm)
3774
+ {
3775
+ LdrFpNeonImm9 temp;
3776
+ temp.s.Rt = rt;
3777
+ temp.s.Rn = rn;
3778
+ temp.s.Imm12 = imm;
3779
+ temp.s.Opcode1 = 1;
3780
+ temp.s.Opc = size >> 2;
3781
+ temp.s.Opcode2 = 0x3d;
3782
+ temp.s.Size = size & 3;
3783
+ return temp.Assembled;
3784
+ }
3785
+ };
3786
+
3787
+ union Mov16
3788
+ {
3789
+ DWORD Assembled;
3790
+ struct
3791
+ {
3792
+ DWORD Rd : 5; // Destination register
3793
+ DWORD Imm16 : 16; // Immediate
3794
+ DWORD Shift : 2; // Shift amount (0=0, 1=16, 2=32, 3=48)
3795
+ DWORD Opcode : 6; // Must be 100101 == 0x25
3796
+ DWORD Type : 2; // 0 = MOVN, 1 = reserved, 2 = MOVZ, 3 = MOVK
3797
+ DWORD Size : 1; // 0 = 32-bit, 1 = 64-bit
3798
+ } s;
3799
+ static DWORD Assemble(DWORD size, DWORD type, DWORD rd, DWORD imm, DWORD shift)
3800
+ {
3801
+ Mov16 temp;
3802
+ temp.s.Rd = rd;
3803
+ temp.s.Imm16 = imm;
3804
+ temp.s.Shift = shift;
3805
+ temp.s.Opcode = 0x25;
3806
+ temp.s.Type = type;
3807
+ temp.s.Size = size;
3808
+ return temp.Assembled;
3809
+ }
3810
+ static DWORD AssembleMovn32(DWORD rd, DWORD imm, DWORD shift) { return Assemble(0, 0, rd, imm, shift); }
3811
+ static DWORD AssembleMovn64(DWORD rd, DWORD imm, DWORD shift) { return Assemble(1, 0, rd, imm, shift); }
3812
+ static DWORD AssembleMovz32(DWORD rd, DWORD imm, DWORD shift) { return Assemble(0, 2, rd, imm, shift); }
3813
+ static DWORD AssembleMovz64(DWORD rd, DWORD imm, DWORD shift) { return Assemble(1, 2, rd, imm, shift); }
3814
+ static DWORD AssembleMovk32(DWORD rd, DWORD imm, DWORD shift) { return Assemble(0, 3, rd, imm, shift); }
3815
+ static DWORD AssembleMovk64(DWORD rd, DWORD imm, DWORD shift) { return Assemble(1, 3, rd, imm, shift); }
3816
+ };
3817
+
3818
+ union Tbz14
3819
+ {
3820
+ DWORD Assembled;
3821
+ struct
3822
+ {
3823
+ DWORD Rt : 5; // Register to test
3824
+ DWORD Imm14 : 14; // 14-bit immediate
3825
+ DWORD Bit : 5; // 5-bit index
3826
+ DWORD Nz : 1; // 0 = TBZ, 1 = TBNZ
3827
+ DWORD Opcode1 : 6; // Must be 011011 == 0x1b
3828
+ DWORD Size : 1; // 0 = 32-bit, 1 = 64-bit
3829
+ } s;
3830
+ inline LONG Imm() const { return (LONG)(s.Imm14 << 18) >> 16; }
3831
+ static DWORD Assemble(DWORD size, DWORD nz, DWORD rt, DWORD bit, LONG delta)
3832
+ {
3833
+ Tbz14 temp;
3834
+ temp.s.Rt = rt;
3835
+ temp.s.Imm14 = delta >> 2;
3836
+ temp.s.Bit = bit;
3837
+ temp.s.Nz = nz;
3838
+ temp.s.Opcode1 = 0x1b;
3839
+ temp.s.Size = size;
3840
+ return temp.Assembled;
3841
+ }
3842
+ };
3843
+
3844
+
3845
+ protected:
3846
+ BYTE PureCopy32(BYTE* pSource, BYTE* pDest);
3847
+ BYTE EmitMovImmediate(PULONG& pDstInst, BYTE rd, UINT64 immediate);
3848
+ BYTE CopyAdr(BYTE* pSource, BYTE* pDest, ULONG instruction);
3849
+ BYTE CopyBcc(BYTE* pSource, BYTE* pDest, ULONG instruction);
3850
+ BYTE CopyB(BYTE* pSource, BYTE* pDest, ULONG instruction);
3851
+ BYTE CopyCbz(BYTE* pSource, BYTE* pDest, ULONG instruction);
3852
+ BYTE CopyTbz(BYTE* pSource, BYTE* pDest, ULONG instruction);
3853
+ BYTE CopyLdrLiteral(BYTE* pSource, BYTE* pDest, ULONG instruction);
3854
+
3855
+ protected:
3856
+ ULONG GetInstruction(BYTE* pSource)
3857
+ {
3858
+ return ((PULONG)pSource)[0];
3859
+ }
3860
+
3861
+ BYTE EmitInstruction(PULONG& pDstInst, ULONG instruction)
3862
+ {
3863
+ *pDstInst++ = instruction;
3864
+ return sizeof(ULONG);
3865
+ }
3866
+
3867
+ protected:
3868
+ PBYTE m_pbTarget;
3869
+ BYTE m_rbScratchDst[64];
3870
+ };
3871
+
3872
+ BYTE CDetourDis::PureCopy32(BYTE* pSource, BYTE* pDest)
3873
+ {
3874
+ *(ULONG *)pDest = *(ULONG*)pSource;
3875
+ return sizeof(DWORD);
3876
+ }
3877
+
3878
+ /////////////////////////////////////////////////////////// Disassembler Code.
3879
+ //
3880
+ CDetourDis::CDetourDis()
3881
+ {
3882
+ m_pbTarget = (PBYTE)DETOUR_INSTRUCTION_TARGET_NONE;
3883
+ }
3884
+
3885
+ PBYTE CDetourDis::CopyInstruction(PBYTE pDst,
3886
+ PBYTE pSrc,
3887
+ PBYTE *ppTarget,
3888
+ LONG *plExtra)
3889
+ {
3890
+ if (pDst == NULL) {
3891
+ pDst = m_rbScratchDst;
3892
+ }
3893
+
3894
+ DWORD Instruction = GetInstruction(pSrc);
3895
+
3896
+ ULONG CopiedSize;
3897
+ if ((Instruction & 0x1f000000) == 0x10000000) {
3898
+ CopiedSize = CopyAdr(pSrc, pDst, Instruction);
3899
+ } else if ((Instruction & 0xff000010) == 0x54000000) {
3900
+ CopiedSize = CopyBcc(pSrc, pDst, Instruction);
3901
+ } else if ((Instruction & 0x7c000000) == 0x14000000) {
3902
+ CopiedSize = CopyB(pSrc, pDst, Instruction);
3903
+ } else if ((Instruction & 0x7e000000) == 0x34000000) {
3904
+ CopiedSize = CopyCbz(pSrc, pDst, Instruction);
3905
+ } else if ((Instruction & 0x7e000000) == 0x36000000) {
3906
+ CopiedSize = CopyTbz(pSrc, pDst, Instruction);
3907
+ } else if ((Instruction & 0x3b000000) == 0x18000000) {
3908
+ CopiedSize = CopyLdrLiteral(pSrc, pDst, Instruction);
3909
+ } else {
3910
+ CopiedSize = PureCopy32(pSrc, pDst);
3911
+ }
3912
+
3913
+ // If the target is needed, store our target
3914
+ if (ppTarget) {
3915
+ *ppTarget = m_pbTarget;
3916
+ }
3917
+ if (plExtra) {
3918
+ *plExtra = CopiedSize - sizeof(DWORD);
3919
+ }
3920
+
3921
+ return pSrc + 4;
3922
+ }
3923
+
3924
+ BYTE CDetourDis::EmitMovImmediate(PULONG& pDstInst, BYTE rd, UINT64 immediate)
3925
+ {
3926
+ DWORD piece[4];
3927
+ piece[3] = (DWORD)((immediate >> 48) & 0xffff);
3928
+ piece[2] = (DWORD)((immediate >> 32) & 0xffff);
3929
+ piece[1] = (DWORD)((immediate >> 16) & 0xffff);
3930
+ piece[0] = (DWORD)((immediate >> 0) & 0xffff);
3931
+ int count = 0;
3932
+
3933
+ // special case: MOVN with 32-bit dest
3934
+ if (piece[3] == 0 && piece[2] == 0 && piece[1] == 0xffff)
3935
+ {
3936
+ EmitInstruction(pDstInst, Mov16::AssembleMovn32(rd, piece[0] ^ 0xffff, 0));
3937
+ count++;
3938
+ }
3939
+
3940
+ // MOVN/MOVZ with 64-bit dest
3941
+ else
3942
+ {
3943
+ int zero_pieces = (piece[3] == 0x0000) + (piece[2] == 0x0000) + (piece[1] == 0x0000) + (piece[0] == 0x0000);
3944
+ int ffff_pieces = (piece[3] == 0xffff) + (piece[2] == 0xffff) + (piece[1] == 0xffff) + (piece[0] == 0xffff);
3945
+ DWORD defaultPiece = (ffff_pieces > zero_pieces) ? 0xffff : 0x0000;
3946
+ bool first = true;
3947
+ for (int pieceNum = 3; pieceNum >= 0; pieceNum--)
3948
+ {
3949
+ DWORD curPiece = piece[pieceNum];
3950
+ if (curPiece != defaultPiece || (pieceNum == 0 && first))
3951
+ {
3952
+ count++;
3953
+ if (first)
3954
+ {
3955
+ if (defaultPiece == 0xffff)
3956
+ {
3957
+ EmitInstruction(pDstInst, Mov16::AssembleMovn64(rd, curPiece ^ 0xffff, pieceNum));
3958
+ }
3959
+ else
3960
+ {
3961
+ EmitInstruction(pDstInst, Mov16::AssembleMovz64(rd, curPiece, pieceNum));
3962
+ }
3963
+ first = false;
3964
+ }
3965
+ else
3966
+ {
3967
+ EmitInstruction(pDstInst, Mov16::AssembleMovk64(rd, curPiece, pieceNum));
3968
+ }
3969
+ }
3970
+ }
3971
+ }
3972
+ return (BYTE)(count * sizeof(DWORD));
3973
+ }
3974
+
3975
+ BYTE CDetourDis::CopyAdr(BYTE* pSource, BYTE* pDest, ULONG instruction)
3976
+ {
3977
+ Adr19& decoded = (Adr19&)(instruction);
3978
+ PULONG pDstInst = (PULONG)(pDest);
3979
+
3980
+ // ADR case
3981
+ if (decoded.s.Type == 0)
3982
+ {
3983
+ BYTE* pTarget = pSource + decoded.Imm();
3984
+ LONG64 delta = pTarget - pDest;
3985
+ LONG64 deltaPage = ((ULONG_PTR)pTarget >> 12) - ((ULONG_PTR)pDest >> 12);
3986
+
3987
+ // output as ADR
3988
+ if (delta >= -(1 << 20) && delta < (1 << 20))
3989
+ {
3990
+ EmitInstruction(pDstInst, Adr19::AssembleAdr(decoded.s.Rd, (LONG)delta));
3991
+ }
3992
+
3993
+ // output as ADRP; ADD
3994
+ else if (deltaPage >= -(1 << 20) && (deltaPage < (1 << 20)))
3995
+ {
3996
+ EmitInstruction(pDstInst, Adr19::AssembleAdrp(decoded.s.Rd, (LONG)deltaPage));
3997
+ EmitInstruction(pDstInst, AddImm12::AssembleAdd32(decoded.s.Rd, decoded.s.Rd, ((ULONG)(ULONG_PTR)pTarget) & 0xfff, 0));
3998
+ }
3999
+
4000
+ // output as immediate move
4001
+ else
4002
+ {
4003
+ EmitMovImmediate(pDstInst, decoded.s.Rd, (ULONG_PTR)pTarget);
4004
+ }
4005
+ }
4006
+
4007
+ // ADRP case
4008
+ else
4009
+ {
4010
+ BYTE* pTarget = (BYTE*)((((ULONG_PTR)pSource >> 12) + decoded.Imm()) << 12);
4011
+ LONG64 deltaPage = ((ULONG_PTR)pTarget >> 12) - ((ULONG_PTR)pDest >> 12);
4012
+
4013
+ // output as ADRP
4014
+ if (deltaPage >= -(1 << 20) && (deltaPage < (1 << 20)))
4015
+ {
4016
+ EmitInstruction(pDstInst, Adr19::AssembleAdrp(decoded.s.Rd, (LONG)deltaPage));
4017
+ }
4018
+
4019
+ // output as immediate move
4020
+ else
4021
+ {
4022
+ EmitMovImmediate(pDstInst, decoded.s.Rd, (ULONG_PTR)pTarget);
4023
+ }
4024
+ }
4025
+
4026
+ return (BYTE)((BYTE*)pDstInst - pDest);
4027
+ }
4028
+
4029
+ BYTE CDetourDis::CopyBcc(BYTE* pSource, BYTE* pDest, ULONG instruction)
4030
+ {
4031
+ Bcc19& decoded = (Bcc19&)(instruction);
4032
+ PULONG pDstInst = (PULONG)(pDest);
4033
+
4034
+ BYTE* pTarget = pSource + decoded.Imm();
4035
+ m_pbTarget = pTarget;
4036
+ LONG64 delta = pTarget - pDest;
4037
+ LONG64 delta4 = pTarget - (pDest + 4);
4038
+
4039
+ // output as BCC
4040
+ if (delta >= -(1 << 20) && delta < (1 << 20))
4041
+ {
4042
+ EmitInstruction(pDstInst, Bcc19::AssembleBcc(decoded.s.Condition, (LONG)delta));
4043
+ }
4044
+
4045
+ // output as BCC <skip>; B
4046
+ else if (delta4 >= -(1 << 27) && (delta4 < (1 << 27)))
4047
+ {
4048
+ EmitInstruction(pDstInst, Bcc19::AssembleBcc(decoded.s.Condition ^ 1, 8));
4049
+ EmitInstruction(pDstInst, Branch26::AssembleB((LONG)delta4));
4050
+ }
4051
+
4052
+ // output as MOV x17, Target; BCC <skip>; BR x17 (BIG assumption that x17 isn't being used for anything!!)
4053
+ else
4054
+ {
4055
+ EmitMovImmediate(pDstInst, 17, (ULONG_PTR)pTarget);
4056
+ EmitInstruction(pDstInst, Bcc19::AssembleBcc(decoded.s.Condition ^ 1, 8));
4057
+ EmitInstruction(pDstInst, Br::AssembleBr(17));
4058
+ }
4059
+
4060
+ return (BYTE)((BYTE*)pDstInst - pDest);
4061
+ }
4062
+
4063
+ BYTE CDetourDis::CopyB(BYTE* pSource, BYTE* pDest, ULONG instruction)
4064
+ {
4065
+ Branch26& decoded = (Branch26&)(instruction);
4066
+ PULONG pDstInst = (PULONG)(pDest);
4067
+
4068
+ BYTE* pTarget = pSource + decoded.Imm();
4069
+ m_pbTarget = pTarget;
4070
+ LONG64 delta = pTarget - pDest;
4071
+
4072
+ // output as B
4073
+ if (delta >= -(1 << 27) && (delta < (1 << 27)))
4074
+ {
4075
+ EmitInstruction(pDstInst, Branch26::AssembleB((LONG)delta));
4076
+ }
4077
+
4078
+ // output as MOV x17, Target; BR x17 (BIG assumption that x17 isn't being used for anything!!)
4079
+ else
4080
+ {
4081
+ EmitMovImmediate(pDstInst, 17, (ULONG_PTR)pTarget);
4082
+ EmitInstruction(pDstInst, Br::AssembleBr(17));
4083
+ }
4084
+
4085
+ return (BYTE)((BYTE*)pDstInst - pDest);
4086
+ }
4087
+
4088
+ BYTE CDetourDis::CopyCbz(BYTE* pSource, BYTE* pDest, ULONG instruction)
4089
+ {
4090
+ Cbz19& decoded = (Cbz19&)(instruction);
4091
+ PULONG pDstInst = (PULONG)(pDest);
4092
+
4093
+ BYTE* pTarget = pSource + decoded.Imm();
4094
+ m_pbTarget = pTarget;
4095
+ LONG64 delta = pTarget - pDest;
4096
+ LONG64 delta4 = pTarget - (pDest + 4);
4097
+
4098
+ // output as CBZ/NZ
4099
+ if (delta >= -(1 << 20) && delta < (1 << 20))
4100
+ {
4101
+ EmitInstruction(pDstInst, Cbz19::Assemble(decoded.s.Size, decoded.s.Nz, decoded.s.Rt, (LONG)delta));
4102
+ }
4103
+
4104
+ // output as CBNZ/Z <skip>; B
4105
+ else if (delta4 >= -(1 << 27) && (delta4 < (1 << 27)))
4106
+ {
4107
+ EmitInstruction(pDstInst, Cbz19::Assemble(decoded.s.Size, decoded.s.Nz ^ 1, decoded.s.Rt, 8));
4108
+ EmitInstruction(pDstInst, Branch26::AssembleB((LONG)delta4));
4109
+ }
4110
+
4111
+ // output as MOV x17, Target; CBNZ/Z <skip>; BR x17 (BIG assumption that x17 isn't being used for anything!!)
4112
+ else
4113
+ {
4114
+ EmitMovImmediate(pDstInst, 17, (ULONG_PTR)pTarget);
4115
+ EmitInstruction(pDstInst, Cbz19::Assemble(decoded.s.Size, decoded.s.Nz ^ 1, decoded.s.Rt, 8));
4116
+ EmitInstruction(pDstInst, Br::AssembleBr(17));
4117
+ }
4118
+
4119
+ return (BYTE)((BYTE*)pDstInst - pDest);
4120
+ }
4121
+
4122
+ BYTE CDetourDis::CopyTbz(BYTE* pSource, BYTE* pDest, ULONG instruction)
4123
+ {
4124
+ Tbz14& decoded = (Tbz14&)(instruction);
4125
+ PULONG pDstInst = (PULONG)(pDest);
4126
+
4127
+ BYTE* pTarget = pSource + decoded.Imm();
4128
+ m_pbTarget = pTarget;
4129
+ LONG64 delta = pTarget - pDest;
4130
+ LONG64 delta4 = pTarget - (pDest + 4);
4131
+
4132
+ // output as TBZ/NZ
4133
+ if (delta >= -(1 << 13) && delta < (1 << 13))
4134
+ {
4135
+ EmitInstruction(pDstInst, Tbz14::Assemble(decoded.s.Size, decoded.s.Nz, decoded.s.Rt, decoded.s.Bit, (LONG)delta));
4136
+ }
4137
+
4138
+ // output as TBNZ/Z <skip>; B
4139
+ else if (delta4 >= -(1 << 27) && (delta4 < (1 << 27)))
4140
+ {
4141
+ EmitInstruction(pDstInst, Tbz14::Assemble(decoded.s.Size, decoded.s.Nz ^ 1, decoded.s.Rt, decoded.s.Bit, 8));
4142
+ EmitInstruction(pDstInst, Branch26::AssembleB((LONG)delta4));
4143
+ }
4144
+
4145
+ // output as MOV x17, Target; TBNZ/Z <skip>; BR x17 (BIG assumption that x17 isn't being used for anything!!)
4146
+ else
4147
+ {
4148
+ EmitMovImmediate(pDstInst, 17, (ULONG_PTR)pTarget);
4149
+ EmitInstruction(pDstInst, Tbz14::Assemble(decoded.s.Size, decoded.s.Nz ^ 1, decoded.s.Rt, decoded.s.Bit, 8));
4150
+ EmitInstruction(pDstInst, Br::AssembleBr(17));
4151
+ }
4152
+
4153
+ return (BYTE)((BYTE*)pDstInst - pDest);
4154
+ }
4155
+
4156
+ BYTE CDetourDis::CopyLdrLiteral(BYTE* pSource, BYTE* pDest, ULONG instruction)
4157
+ {
4158
+ LdrLit19& decoded = (LdrLit19&)(instruction);
4159
+ PULONG pDstInst = (PULONG)(pDest);
4160
+
4161
+ BYTE* pTarget = pSource + decoded.Imm();
4162
+ LONG64 delta = pTarget - pDest;
4163
+
4164
+ // output as LDR
4165
+ if (delta >= -(1 << 21) && delta < (1 << 21))
4166
+ {
4167
+ EmitInstruction(pDstInst, LdrLit19::Assemble(decoded.s.Size, decoded.s.FpNeon, decoded.s.Rt, (LONG)delta));
4168
+ }
4169
+
4170
+ // output as move immediate
4171
+ else if (decoded.s.FpNeon == 0)
4172
+ {
4173
+ UINT64 value = 0;
4174
+ switch (decoded.s.Size)
4175
+ {
4176
+ case 0: value = *(ULONG*)pTarget; break;
4177
+ case 1: value = *(UINT64*)pTarget; break;
4178
+ case 2: value = *(LONG*)pTarget; break;
4179
+ }
4180
+ EmitMovImmediate(pDstInst, decoded.s.Rt, value);
4181
+ }
4182
+
4183
+ // FP/NEON register: compute address in x17 and load from there (BIG assumption that x17 isn't being used for anything!!)
4184
+ else
4185
+ {
4186
+ EmitMovImmediate(pDstInst, 17, (ULONG_PTR)pTarget);
4187
+ EmitInstruction(pDstInst, LdrFpNeonImm9::Assemble(2 + decoded.s.Size, decoded.s.Rt, 17, 0));
4188
+ }
4189
+
4190
+ return (BYTE)((BYTE*)pDstInst - pDest);
4191
+ }
4192
+
4193
+
4194
+ PVOID WINAPI DetourCopyInstruction(_In_opt_ PVOID pDst,
4195
+ _Inout_opt_ PVOID *ppDstPool,
4196
+ _In_ PVOID pSrc,
4197
+ _Out_opt_ PVOID *ppTarget,
4198
+ _Out_opt_ LONG *plExtra)
4199
+ {
4200
+ UNREFERENCED_PARAMETER(ppDstPool);
4201
+
4202
+ CDetourDis state;
4203
+ return (PVOID)state.CopyInstruction((PBYTE)pDst,
4204
+ (PBYTE)pSrc,
4205
+ (PBYTE*)ppTarget,
4206
+ plExtra);
4207
+ }
4208
+
4209
+ #endif // DETOURS_ARM64
4210
+
4211
+ BOOL WINAPI DetourSetCodeModule(_In_ HMODULE hModule,
4212
+ _In_ BOOL fLimitReferencesToModule)
4213
+ {
4214
+ #if defined(DETOURS_X64) || defined(DETOURS_X86)
4215
+ PBYTE pbBeg = NULL;
4216
+ PBYTE pbEnd = (PBYTE)~(ULONG_PTR)0;
4217
+
4218
+ if (hModule != NULL) {
4219
+ ULONG cbModule = DetourGetModuleSize(hModule);
4220
+
4221
+ pbBeg = (PBYTE)hModule;
4222
+ pbEnd = (PBYTE)hModule + cbModule;
4223
+ }
4224
+
4225
+ return CDetourDis::SetCodeModule(pbBeg, pbEnd, fLimitReferencesToModule);
4226
+ #elif defined(DETOURS_ARM) || defined(DETOURS_ARM64) || defined(DETOURS_IA64)
4227
+ (void)hModule;
4228
+ (void)fLimitReferencesToModule;
4229
+ return TRUE;
4230
+ #else
4231
+ #error unknown architecture (x86, x64, arm, arm64, ia64)
4232
+ #endif
4233
+ }
4234
+
4235
+ //
4236
+ ///////////////////////////////////////////////////////////////// End of File.