-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrap_dynamic_calls_reverse.pl
More file actions
executable file
·218 lines (180 loc) · 5.62 KB
/
wrap_dynamic_calls_reverse.pl
File metadata and controls
executable file
·218 lines (180 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl -w
$#ARGV==1 or die "usage: wrap_dynamic_calls_reverse.pl funclist exec\n";
$lf=shift;
$stem=shift;
$preorig="__fpvm_orig_";
$entry = "*__fpvm_foreign_entry\@GOTPCREL(%rip)";
$exit = "*__fpvm_foreign_exit\@GOTPCREL(%rip)";
open(L,"$lf") or die "cannot open $lf\n";
while (<L>) {
chomp();
next if (/^\s*#/ || /^\s*$/); # kill comments and empty lines
($f) = split(/\s+/); $fs{$f}=1; }
close(L);
@funcs = sort keys %fs;
open(I,">$stem.inc") or die "cannot open $stem.inc\n";
print I "// This file is auto-generated and\n";
print I "// conforms with $stem.S\n";
print I "// conforms with $stem.h\n\n";
# the I file in intended to be #included only in fpvm
foreach $func (@funcs) {
print I "void (*$preorig$func)() = 0;\n";
}
print I "\n";
print I "int fpvm_setup_additional_wrappers(void) { int rc=0;\n";
foreach $func (@funcs) {
print I <<ENDI
if (!($preorig$func = dlsym(RTLD_NEXT,"$func"))) {
DEBUG("failed to setup SHIM for additional wrapper $func - ignoring\\n");
rc=-1;
} else {
DEBUG("additional wrapper for $func (%p) set up\\n",$preorig$func);
}
ENDI
;
}
print I " if (rc) { ERROR(\"some additional wrappers not set up\\n\");\n }\n";
print I " return rc; \n}\n\n";
close(I);
open(H,">$stem.h") or die "cannot open $stem.h\n";
print H "// This file is auto-generated and\n";
print H "// conforms with $stem.inc\n";
print H "// conforms with $stem.S\n\n";
print H "// This is intentionally blank since reverse wrappers are in use\n";
print H "\n";
close(H);
open(S,">$stem.S") or die "cannot open $stem.S\n";
print S "# This file is auto-generated and\n";
print S "# conforms with include/fpvm/additional_wrappers.h\n\n";
#
# rdi, rsi, rdx, rcx, r8, r9, xmm0..xmm7 => rax or rdx::rax or xmm0::xmm1
# for varargs, rax is INPUT as well, passing number
# of vector registers used
# scratch: rax, r10, r11 (rax, r11 safest, r10
# mxcsr partially preserved across boundary
# control => callee-save
# status => caller-save (not preserved)
#
#
#
#
# https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
#
# The wrapper function does the following steps:
#
# 1. save integer argument registers (including rax)
# 2. allocate space for mxcsr restore
# 3. stack align for call
# call site rsp must be 16 (or 32) aligned
# so that at entry to function, we are off by 8 (ret addr)
# 4. invoke runtime for demotion of machine xmms
# RT code must not use SSE
# RT returns mxcsr to use on exit
# 5. stash mxcsr for return
# 6. restore integer argument registers, including rax
# 7. invoke original function (must have alignment right)
# 8. load mxcsr from stash
# 9. unwind and return
#
#
# Planned stack frame:
#
# ret (-0)
# rbp stash <= rbp (-8) ** OK
# mxcsr_stash (8) (-16)
# rax (-24)
# rdi (-32)
# rsi (-40)
# rdx (-48)
# rcx (-56)
# r8 (-64)
# r9 (-72)
# <blank*1> (-80) ** OK, caller at -80 (16*5)
# <blank*10> (-160) ** OK, caller at -160 (32*5) [maybe]
#
#
#
foreach $func (@funcs) {
print S <<ENDS
.weak $func
.globl $func\$fpvm
$func\$fpvm:
pushq %rbp # temporary new stack frame
mov %rsp, %rbp # with rbp so we can easily reference
pushq %rax # number of vector registers used in call
pushq %rdi # 1st arg
pushq %rsi # 2nd arg
pushq %rdx # 3rd arg
pushq %rcx # 4th arg
pushq %r8 # 5th arg
pushq %r9 # 6th arg
pushq %r11 # Enforce alignment of stack
# r11 is a temporary reg not saved
# note that rbx, r12,r13,r14,r15 are callee save, but we will not use them
# r15 is GOT base pointer (optionally)
# xmm0 is 1st float arg and return
# xmm1 is 2nd float arg and return
# xmm2..7 are 3rd through 8th float args
# invoke foreign_entry(addr_of_ret,addr_of_tramp,addr_of_func)
# this will
# - demote argument registers
# - configure mxcsr and other FPVM state appropriately
# - stash state as needed
# - switch return address to tramp address
# - update return addressupdate the return address to the tramp address
leaq 8(%rbp), %rdi
movq .tramp$func\@GOTPCREL(%rip), %rsi
movq $func\@GOTPCREL(%rip), %rdx # for debugging
call $entry
popq %r11 # undo alignment
popq %r9
popq %r8
popq %rcx
popq %rdx
popq %rsi
popq %rdi
popq %rax
# Tear down the frame
popq %rbp
# Simply jump (tail-call) to the 'real' func
jmp $func
# for testing
# jmp __fpvm_f_debug;
# the original function will return here...
.tramp$func:
pushq \$0 # The return address (alignment)
movq %rsp, %rdi # Point to the ret addr slot
pushq %rbp # make a frame
mov %rsp, %rbp
pushq %rax
pushq %rdi
pushq %rsi
pushq %rdx
pushq %rcx
pushq %r8
pushq %r9
pushq %r11 # Enforce alignment
#
# invoke foreign_exit(addr_of_ret)
#
# This will update the FP state (e.g., mxcsr)
# and modify the return address back to the original
# which was captured earlier
#
leaq 8(%rbp), %rdi
call $exit
popq %r11 # undo alignment
popq %r9
popq %r8
popq %rcx
popq %rdx
popq %rsi
popq %rdi
popq %rax
# Calgon, take us away (back to the original caller)
popq %rbp # tear down frame
ret
ENDS
;
}
close(S);