Debian Codegraph @

vendor/Heap-Layers/wrappers/x86jump.h

raw
1 #ifndef X86JUMP_H
2 #define X86JUMP_H
3
4 /**
5
6 Jump code to permit detouring of functions.
7 Use by instantiating into the original start of the function
8 as follows:
9
10 new (originalFunctionStart) x86Jump (newFunctionStart);
11
12 Based on code contributed by Charlie Curtsinger <[email protected]>
13
14 @author Emery Berger <[email protected]>
15
16 **/
17
18 #include <new>
19 #include <stdint.h>
20
21 #ifdef _MSC_VER
22 #define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
23 #else
24 #define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
25 #endif
26
27
28 PACK(
29 struct X86Jump32 {
30 volatile uint8_t jmp_opcode;
31 volatile uint32_t jmp_offset;
32
33 // Install a direct 32-bit jump.
34 X86Jump32 (void *target)
35 {
36 jmp_opcode = 0xE9;
37 jmp_offset = (uint32_t)((intptr_t)target - (intptr_t)this) - sizeof(struct X86Jump32);
38 }
39 });
40
41 PACK(
42 struct X86Jump64 {
43
44 volatile uint16_t farjmp;
45 volatile uint32_t offset;
46 volatile uint64_t addr;
47
48 // Install a 64-bit jump (a relative jump to the target with a 0 offset).
49 X86Jump64 (void *target)
50 {
51 farjmp = 0x25ff;
52 offset = 0x00000000;
53 addr = (uint64_t) target;
54 }
55 });
56
57
58 PACK(
59 struct X86_64Jump {
60 union {
61 uint8_t jmp32[sizeof(X86Jump32)];
62 uint8_t jmp64[sizeof(X86Jump64)];
63 };
64
65 X86_64Jump (void *target) {
66 // When the target is close enough, use a 32-bit jump;
67 // otherwise, use the full 64-bit jump.
68 if ((uintptr_t)target - (uintptr_t)this <= 0x00000000FFFFFFFFu ||
69 (uintptr_t)this - (uintptr_t)target <= 0x00000000FFFFFFFFu)
70 {
71 new (this) X86Jump32(target);
72 } else {
73 new (this) X86Jump64(target);
74 }
75 }
76 });
77
78 #if defined(__LP64__) || defined(_LP64) || defined(__APPLE__) || defined(_WIN64) || defined(__x86_64__)
79 typedef X86_64Jump X86Jump;
80 #else
81 typedef X86Jump32 X86Jump;
82 #endif
83
84 #endif