-
Notifications
You must be signed in to change notification settings - Fork 29
/
mkstubs.pl
293 lines (234 loc) · 6.45 KB
/
mkstubs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# IPXWrapper - Generate assembly stub functions
# Copyright (C) 2008-2023 Daniel Collins <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
use strict;
use warnings;
if(@ARGV != 3) {
print STDERR "Usage: mkdll.pl <stub definitions file> <asm output file> <dll name>\n";
exit(1);
}
# Must be kept in sync with dll_names in common.c!
my %DLL_INDICES = (
"ipxwrapper.dll" => 0,
"wsock32.dll" => 1,
"mswsock.dll" => 2,
"dpwsockx.dll" => 3,
"ws2_32.dll" => 4,
"wpcap.dll" => 5,
"ipxconfig.exe" => 6,
);
my ($stub_file, $asm_file, $dll_name) = @ARGV;
my $dll_index = $DLL_INDICES{$dll_name}
// die "Unknown DLL name: $dll_name";
open(STUBS, "<$stub_file") or die("Cannot open $stub_file: $!");
open(CODE, ">$asm_file") or die("Cannot open $asm_file: $!");
my @stubs = ();
# Skip over header
(scalar <STUBS>);
(scalar <STUBS>);
# Read in stub definitions
foreach my $line(<STUBS>)
{
$line =~ s/[\r\n]//g;
if($line ne "") {
my ($name, $target_dll, $target_func, $params) = split(/\s+/, $line);
my $target_dll_index = $DLL_INDICES{$target_dll}
// die "Unknown DLL: $target_dll\n";
push(@stubs, {
name => $name,
target_dll => $target_dll,
target_dll_index => $target_dll_index,
target_func => $target_func,
params => $params,
});
}
}
print CODE <<"END";
extern _QueryPerformanceCounter\@4
extern _find_sym
extern _log_call
extern _fprof_record_timed
extern _fprof_record_untimed
struc FuncStats
.func_name: resd 1
.min_time: resd 1
.max_time: resd 1
.total_time: resd 1
.n_calls: resd 1
.cs: resb 24
endstruc
END
print CODE <<"END";
section .rdata
END
foreach my $func(@stubs)
{
print CODE <<"END";
$func->{name}_name: db '$func->{name}', 0
$func->{name}_target_func: db '$func->{target_func}', 0
END
}
my $num_funcs = (scalar @stubs);
print CODE <<"END";
global _NUM_STUBS
_NUM_STUBS: dd $num_funcs
DLL_NAME: db '$dll_name', 0
global _STUBS_DLL_NAME
_STUBS_DLL_NAME: dd DLL_NAME
END
print CODE <<"END";
section .data
global _stubs_enable_profile
_stubs_enable_profile: db 0
END
foreach my $func(@stubs)
{
print CODE <<"END";
$func->{name}_addr: dd 0
END
}
print CODE <<"END";
global _stub_fstats
_stub_fstats:
END
foreach my $func(@stubs)
{
print CODE <<"END";
$func->{name}_fstats:
istruc FuncStats
at FuncStats.func_name, dd $func->{name}_name
iend
END
}
print CODE <<"END";
section .text
END
foreach my $func(@stubs)
{
if(defined $func->{params})
{
my $to_copy = $func->{params};
print CODE <<"END";
global _$func->{name}
_$func->{name}:
; Log the call
push dword $func->{target_dll_index}
push $func->{name}_target_func
push dword $dll_index
call _log_call
; Check if we have address cached
cmp dword [$func->{name}_addr], 0
jne $func->{name}_go
; Fetch target function address
push $func->{name}_target_func
push dword $func->{target_dll_index}
call _find_sym
mov dword [$func->{name}_addr], eax
$func->{name}_go:
; Bypass the profiling code and jump straight into the taget
; function when not profiling.
cmp byte [_stubs_enable_profile], 0
je $func->{name}_skip
push ebp
mov ebp, esp
; Push tick count onto stack (ebp - 8)
sub esp, 8
push esp
call _QueryPerformanceCounter\@4
; Copy original arguments ($to_copy bytes)
END
for(; $to_copy >= 4;)
{
$to_copy -= 4;
print CODE <<"END";
push dword [ebp + 4 + 4 + $to_copy]
END
}
for(; $to_copy >= 2;)
{
$to_copy -= 2;
print CODE <<"END";
push word [ebp + 4 + 4 + $to_copy]
END
}
for(; $to_copy >= 1;)
{
$to_copy -= 1;
print CODE <<"END";
push byte [ebp + 4 + 4 + $to_copy]
END
}
print CODE <<"END";
; Call target function
call [$func->{name}_addr]
; Push target function return value onto stack (ebp - 12)
push eax
; Push tick count onto stack (ebp - 20)
sub esp, 8
push esp
call _QueryPerformanceCounter\@4
; End tick parameter to _fprof_record_timed
push dword ebp
sub dword [esp], 20
; Start tick parameter to _fprof_record_timed
push dword ebp
sub dword [esp], 8
; FuncStats parameter to _fprof_record_timed
push dword $func->{name}_fstats
; Record profiling data
call _fprof_record_timed
add esp, 8 ; Pop end tick count
pop eax ; Pop return value
add esp, 8 ; Pop start tick count
pop ebp ; Restore caller's ebp
ret $func->{params}
$func->{name}_skip:
jmp [$func->{name}_addr]
END
}
else{
print CODE <<"END";
global _$func->{name}
_$func->{name}:
; Log the call
push dword $func->{target_dll_index}
push $func->{name}_target_func
push dword $dll_index
call _log_call
; Check if we have address cached
cmp dword [$func->{name}_addr], 0
jne $func->{name}_go
; Fetch target function address
push $func->{name}_target_func
push dword $func->{target_dll_index}
call _find_sym
mov dword [$func->{name}_addr], eax
$func->{name}_go:
; Bypass the profiling code and jump straight into the taget
; function when not profiling.
cmp byte [_stubs_enable_profile], 0
je $func->{name}_skip
; Record that we were called
push dword $func->{name}_fstats
call _fprof_record_untimed
$func->{name}_skip:
; Jump into target function. We have left the stack as we found it
; so it can take over our frame.
jmp [$func->{name}_addr]
END
}
}
close(CODE);
close(STUBS);