-
Notifications
You must be signed in to change notification settings - Fork 0
/
trampoline_x86_64.S
152 lines (140 loc) · 3.85 KB
/
trampoline_x86_64.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#if !defined(__APPLE__)
# define ASM_HIDDEN(symbol) .hidden symbol
# define ASM_TYPE_FUNCTION(symbol) .type symbol, @function
# define ASM_SIZE(symbol) .size symbol, .-symbol
# define ASM_SYMBOL(symbol) symbol
# define ASM_SYMBOL_INTERCEPTOR(symbol) symbol
#else
# define ASM_HIDDEN(symbol)
# define ASM_TYPE_FUNCTION(symbol)
# define ASM_SIZE(symbol)
# define ASM_SYMBOL(symbol) _##symbol
# define ASM_SYMBOL_INTERCEPTOR(symbol) _wrap_##symbol
#endif
.macro SAVE_REGISTERS
subq $240, %rsp
// CFI_DEF_CFA_OFFSET(248)
movq %rbp, 232(%rsp)
movupd %xmm0, 216(%rsp)
movupd %xmm1, 200(%rsp)
movupd %xmm2, 184(%rsp)
movupd %xmm3, 168(%rsp)
movupd %xmm4, 152(%rsp)
movupd %xmm5, 136(%rsp)
movupd %xmm6, 120(%rsp)
movupd %xmm7, 104(%rsp)
movq %rdi, 96(%rsp)
movq %rax, 88(%rsp)
movq %rdx, 80(%rsp)
movq %rsi, 72(%rsp)
movq %rcx, 64(%rsp)
movq %r8, 56(%rsp)
movq %r9, 48(%rsp)
movq %r10, 40(%rsp)
movq %r11, 32(%rsp)
movq %r12, 24(%rsp)
movq %r13, 16(%rsp)
movq %r14, 8(%rsp)
movq %r15, 0(%rsp)
.endm
.macro RESTORE_REGISTERS
movq 232(%rsp), %rbp
movupd 216(%rsp), %xmm0
movupd 200(%rsp), %xmm1
movupd 184(%rsp), %xmm2
movupd 168(%rsp), %xmm3
movupd 152(%rsp), %xmm4
movupd 136(%rsp), %xmm5
movupd 120(%rsp) , %xmm6
movupd 104(%rsp) , %xmm7
movq 96(%rsp), %rdi
movq 88(%rsp), %rax
movq 80(%rsp), %rdx
movq 72(%rsp), %rsi
movq 64(%rsp), %rcx
movq 56(%rsp), %r8
movq 48(%rsp), %r9
movq 40(%rsp), %r10
movq 32(%rsp), %r11
movq 24(%rsp), %r12
movq 16(%rsp), %r13
movq 8(%rsp), %r14
movq 0(%rsp), %r15
addq $240, %rsp
// CFI_DEF_CFA_OFFSET(8)
.endm
.text
#if !defined(__APPLE__)
.section .text
#else
.section __TEXT,__text
#endif
.file "trampoline_x86.S"
.macro ALIGNED_CALL_RAX
// Call the logging handler, after aligning the stack to a 16-byte boundary.
// The approach we're taking here uses additional stack space to stash the
// stack pointer twice before aligning the pointer to 16-bytes. If the stack
// was 8-byte aligned, it will become 16-byte aligned -- when restoring the
// pointer, we can always look -8 bytes from the current position to get
// either of the values we've stashed in the first place.
pushq %rsp
pushq (%rsp)
andq $-0x10, %rsp
callq *%rax
movq 8(%rsp), %rsp
.endm
//===----------------------------------------------------------------------===//
// .globl ASM_SYMBOL(__ret_getCurrentRA)
// .globl ASM_SYMBOL(__ret_printCurrentRA)
.globl ASM_SYMBOL(__ret_FunctionExit)
.align 16, 0x90
ASM_TYPE_FUNCTION(__ret_FunctionExit)
ASM_SYMBOL(__ret_FunctionExit):
// CFI_STARTPROC
// Save the important registers first. Since we're assuming that this
// function is only jumped into, we only preserve the registers for
// returning.
subq $64, %rsp
// CFI_DEF_CFA_OFFSET(64)
movq %rbp, 48(%rsp)
movupd %xmm0, 32(%rsp)
movupd %xmm1, 16(%rsp)
movq %rax, 8(%rsp)
movq %rdx, 0(%rsp)
callq ASM_SYMBOL(__ret_getCurrentRA@PLT)
movq (%rax), %rax
movq %rax, 56(%rsp)
// xor %rsi,%rsi
// callq __ret_printCurrentRA@PLT
// Fix up the next frame
// Save next return address in the TL
movq 48(%rsp), %rbp
movq 8(%rbp), %rdi
callq ASM_SYMBOL(__ret_setCurrentRA@PLT)
testq %rax, %rax
jne .Ltmp
// Overwrite trampoline as the return address of the frame above
movq ASM_SYMBOL(trampoline_fn@GOTPCREL)(%rip), %rax
movq (%rax), %rax
mov %rax, 8(%rbp)
// Call instrumentation function
movq ASM_SYMBOL(instrumentation_fn@GOTPCREL)(%rip), %rax
movq (%rax), %rax
testq %rax, %rax
je .Ltmp
// Return address of this frame is saved at 56(%rsp)
// Set it as an argument to the instrumentation function
movq 56(%rsp), %rdi
ALIGNED_CALL_RAX
.Ltmp:
// Restore the important registers.
movq 48(%rsp), %rbp
movupd 32(%rsp), %xmm0
movupd 16(%rsp), %xmm1
movq 8(%rsp), %rax
movq 0(%rsp), %rdx
addq $56, %rsp
// CFI_DEF_CFA_OFFSET(8)
retq
ASM_SIZE(__ret_FunctionExit)
// CFI_ENDPROC