1 | #! /usr/bin/env perl
|
---|
2 | # Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | #
|
---|
4 | # Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | # this file except in compliance with the License. You can obtain a copy
|
---|
6 | # in the file LICENSE in the source distribution or at
|
---|
7 | # https://www.openssl.org/source/license.html
|
---|
8 |
|
---|
9 |
|
---|
10 | # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
|
---|
11 | #
|
---|
12 | # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
|
---|
13 | # format is way easier to parse. Because it's simpler to "gear" from
|
---|
14 | # Unix ABI to Windows one [see cross-reference "card" at the end of
|
---|
15 | # file]. Because Linux targets were available first...
|
---|
16 | #
|
---|
17 | # In addition the script also "distills" code suitable for GNU
|
---|
18 | # assembler, so that it can be compiled with more rigid assemblers,
|
---|
19 | # such as Solaris /usr/ccs/bin/as.
|
---|
20 | #
|
---|
21 | # This translator is not designed to convert *arbitrary* assembler
|
---|
22 | # code from AT&T format to MASM one. It's designed to convert just
|
---|
23 | # enough to provide for dual-ABI OpenSSL modules development...
|
---|
24 | # There *are* limitations and you might have to modify your assembler
|
---|
25 | # code or this script to achieve the desired result...
|
---|
26 | #
|
---|
27 | # Currently recognized limitations:
|
---|
28 | #
|
---|
29 | # - can't use multiple ops per line;
|
---|
30 | #
|
---|
31 | # Dual-ABI styling rules.
|
---|
32 | #
|
---|
33 | # 1. Adhere to Unix register and stack layout [see cross-reference
|
---|
34 | # ABI "card" at the end for explanation].
|
---|
35 | # 2. Forget about "red zone," stick to more traditional blended
|
---|
36 | # stack frame allocation. If volatile storage is actually required
|
---|
37 | # that is. If not, just leave the stack as is.
|
---|
38 | # 3. Functions tagged with ".type name,@function" get crafted with
|
---|
39 | # unified Win64 prologue and epilogue automatically. If you want
|
---|
40 | # to take care of ABI differences yourself, tag functions as
|
---|
41 | # ".type name,@abi-omnipotent" instead.
|
---|
42 | # 4. To optimize the Win64 prologue you can specify number of input
|
---|
43 | # arguments as ".type name,@function,N." Keep in mind that if N is
|
---|
44 | # larger than 6, then you *have to* write "abi-omnipotent" code,
|
---|
45 | # because >6 cases can't be addressed with unified prologue.
|
---|
46 | # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
|
---|
47 | # (sorry about latter).
|
---|
48 | # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
|
---|
49 | # required to identify the spots, where to inject Win64 epilogue!
|
---|
50 | # But on the pros, it's then prefixed with rep automatically:-)
|
---|
51 | # 7. Stick to explicit ip-relative addressing. If you have to use
|
---|
52 | # GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
|
---|
53 | # Both are recognized and translated to proper Win64 addressing
|
---|
54 | # modes.
|
---|
55 | #
|
---|
56 | # 8. In order to provide for structured exception handling unified
|
---|
57 | # Win64 prologue copies %rsp value to %rax. For further details
|
---|
58 | # see SEH paragraph at the end.
|
---|
59 | # 9. .init segment is allowed to contain calls to functions only.
|
---|
60 | # a. If function accepts more than 4 arguments *and* >4th argument
|
---|
61 | # is declared as non 64-bit value, do clear its upper part.
|
---|
62 | |
---|
63 |
|
---|
64 |
|
---|
65 | use strict;
|
---|
66 |
|
---|
67 | my $flavour = shift;
|
---|
68 | my $output = shift;
|
---|
69 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
|
---|
70 |
|
---|
71 | open STDOUT,">$output" || die "can't open $output: $!"
|
---|
72 | if (defined($output));
|
---|
73 |
|
---|
74 | my $gas=1; $gas=0 if ($output =~ /\.asm$/);
|
---|
75 | my $elf=1; $elf=0 if (!$gas);
|
---|
76 | my $win64=0;
|
---|
77 | my $prefix="";
|
---|
78 | my $decor=".L";
|
---|
79 |
|
---|
80 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
|
---|
81 | my $masm=0;
|
---|
82 | my $PTR=" PTR";
|
---|
83 |
|
---|
84 | my $nasmref=2.03;
|
---|
85 | my $nasm=0;
|
---|
86 |
|
---|
87 | if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
|
---|
88 | $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
|
---|
89 | $prefix =~ s|\R$||; # Better chomp
|
---|
90 | }
|
---|
91 | elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
|
---|
92 | elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
|
---|
93 | elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
|
---|
94 | elsif (!$gas)
|
---|
95 | { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
|
---|
96 | { $nasm = $1 + $2*0.01; $PTR=""; }
|
---|
97 | elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
|
---|
98 | { $masm = $1 + $2*2**-16 + $4*2**-32; }
|
---|
99 | die "no assembler found on %PATH%" if (!($nasm || $masm));
|
---|
100 | $win64=1;
|
---|
101 | $elf=0;
|
---|
102 | $decor="\$L\$";
|
---|
103 | }
|
---|
104 |
|
---|
105 | my $current_segment;
|
---|
106 | my $current_function;
|
---|
107 | my %globals;
|
---|
108 |
|
---|
109 | { package opcode; # pick up opcodes
|
---|
110 | sub re {
|
---|
111 | my ($class, $line) = @_;
|
---|
112 | my $self = {};
|
---|
113 | my $ret;
|
---|
114 |
|
---|
115 | if ($$line =~ /^([a-z][a-z0-9]*)/i) {
|
---|
116 | bless $self,$class;
|
---|
117 | $self->{op} = $1;
|
---|
118 | $ret = $self;
|
---|
119 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
120 |
|
---|
121 | undef $self->{sz};
|
---|
122 | if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
|
---|
123 | $self->{op} = $1;
|
---|
124 | $self->{sz} = $2;
|
---|
125 | } elsif ($self->{op} =~ /call|jmp/) {
|
---|
126 | $self->{sz} = "";
|
---|
127 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
|
---|
128 | $self->{sz} = "";
|
---|
129 | } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
|
---|
130 | $self->{sz} = "";
|
---|
131 | } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
|
---|
132 | $self->{sz} = "";
|
---|
133 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
|
---|
134 | $self->{op} = $1;
|
---|
135 | $self->{sz} = $2;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | $ret;
|
---|
139 | }
|
---|
140 | sub size {
|
---|
141 | my ($self, $sz) = @_;
|
---|
142 | $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
|
---|
143 | $self->{sz};
|
---|
144 | }
|
---|
145 | sub out {
|
---|
146 | my $self = shift;
|
---|
147 | if ($gas) {
|
---|
148 | if ($self->{op} eq "movz") { # movz is pain...
|
---|
149 | sprintf "%s%s%s",$self->{op},$self->{sz},shift;
|
---|
150 | } elsif ($self->{op} =~ /^set/) {
|
---|
151 | "$self->{op}";
|
---|
152 | } elsif ($self->{op} eq "ret") {
|
---|
153 | my $epilogue = "";
|
---|
154 | if ($win64 && $current_function->{abi} eq "svr4") {
|
---|
155 | $epilogue = "movq 8(%rsp),%rdi\n\t" .
|
---|
156 | "movq 16(%rsp),%rsi\n\t";
|
---|
157 | }
|
---|
158 | $epilogue . ".byte 0xf3,0xc3";
|
---|
159 | } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
|
---|
160 | ".p2align\t3\n\t.quad";
|
---|
161 | } else {
|
---|
162 | "$self->{op}$self->{sz}";
|
---|
163 | }
|
---|
164 | } else {
|
---|
165 | $self->{op} =~ s/^movz/movzx/;
|
---|
166 | if ($self->{op} eq "ret") {
|
---|
167 | $self->{op} = "";
|
---|
168 | if ($win64 && $current_function->{abi} eq "svr4") {
|
---|
169 | $self->{op} = "mov rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
|
---|
170 | "mov rsi,QWORD$PTR\[16+rsp\]\n\t";
|
---|
171 | }
|
---|
172 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
|
---|
173 | } elsif ($self->{op} =~ /^(pop|push)f/) {
|
---|
174 | $self->{op} .= $self->{sz};
|
---|
175 | } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
|
---|
176 | $self->{op} = "\tDQ";
|
---|
177 | }
|
---|
178 | $self->{op};
|
---|
179 | }
|
---|
180 | }
|
---|
181 | sub mnemonic {
|
---|
182 | my ($self, $op) = @_;
|
---|
183 | $self->{op}=$op if (defined($op));
|
---|
184 | $self->{op};
|
---|
185 | }
|
---|
186 | }
|
---|
187 | { package const; # pick up constants, which start with $
|
---|
188 | sub re {
|
---|
189 | my ($class, $line) = @_;
|
---|
190 | my $self = {};
|
---|
191 | my $ret;
|
---|
192 |
|
---|
193 | if ($$line =~ /^\$([^,]+)/) {
|
---|
194 | bless $self, $class;
|
---|
195 | $self->{value} = $1;
|
---|
196 | $ret = $self;
|
---|
197 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
198 | }
|
---|
199 | $ret;
|
---|
200 | }
|
---|
201 | sub out {
|
---|
202 | my $self = shift;
|
---|
203 |
|
---|
204 | $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
|
---|
205 | if ($gas) {
|
---|
206 | # Solaris /usr/ccs/bin/as can't handle multiplications
|
---|
207 | # in $self->{value}
|
---|
208 | my $value = $self->{value};
|
---|
209 | no warnings; # oct might complain about overflow, ignore here...
|
---|
210 | $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
|
---|
211 | if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
|
---|
212 | $self->{value} = $value;
|
---|
213 | }
|
---|
214 | sprintf "\$%s",$self->{value};
|
---|
215 | } else {
|
---|
216 | my $value = $self->{value};
|
---|
217 | $value =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
|
---|
218 | sprintf "%s",$value;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 | { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
|
---|
223 |
|
---|
224 | my %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR",
|
---|
225 | l=>"DWORD$PTR", d=>"DWORD$PTR",
|
---|
226 | q=>"QWORD$PTR", o=>"OWORD$PTR",
|
---|
227 | x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
|
---|
228 | z=>"ZMMWORD$PTR" ) if (!$gas);
|
---|
229 |
|
---|
230 | sub re {
|
---|
231 | my ($class, $line, $opcode) = @_;
|
---|
232 | my $self = {};
|
---|
233 | my $ret;
|
---|
234 |
|
---|
235 | # optional * ----vvv--- appears in indirect jmp/call
|
---|
236 | if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
|
---|
237 | bless $self, $class;
|
---|
238 | $self->{asterisk} = $1;
|
---|
239 | $self->{label} = $2;
|
---|
240 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
|
---|
241 | $self->{scale} = 1 if (!defined($self->{scale}));
|
---|
242 | $self->{opmask} = $4;
|
---|
243 | $ret = $self;
|
---|
244 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
245 |
|
---|
246 | if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
|
---|
247 | die if ($opcode->mnemonic() ne "mov");
|
---|
248 | $opcode->mnemonic("lea");
|
---|
249 | }
|
---|
250 | $self->{base} =~ s/^%//;
|
---|
251 | $self->{index} =~ s/^%// if (defined($self->{index}));
|
---|
252 | $self->{opcode} = $opcode;
|
---|
253 | }
|
---|
254 | $ret;
|
---|
255 | }
|
---|
256 | sub size {}
|
---|
257 | sub out {
|
---|
258 | my ($self, $sz) = @_;
|
---|
259 |
|
---|
260 | $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
261 | $self->{label} =~ s/\.L/$decor/g;
|
---|
262 |
|
---|
263 | # Silently convert all EAs to 64-bit. This is required for
|
---|
264 | # elder GNU assembler and results in more compact code,
|
---|
265 | # *but* most importantly AES module depends on this feature!
|
---|
266 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
|
---|
267 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
|
---|
268 |
|
---|
269 | # Solaris /usr/ccs/bin/as can't handle multiplications
|
---|
270 | # in $self->{label}...
|
---|
271 | use integer;
|
---|
272 | $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
|
---|
273 | $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
|
---|
274 |
|
---|
275 | # Some assemblers insist on signed presentation of 32-bit
|
---|
276 | # offsets, but sign extension is a tricky business in perl...
|
---|
277 | if ((1<<31)<<1) {
|
---|
278 | $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
|
---|
279 | } else {
|
---|
280 | $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
|
---|
281 | }
|
---|
282 |
|
---|
283 | # if base register is %rbp or %r13, see if it's possible to
|
---|
284 | # flip base and index registers [for better performance]
|
---|
285 | if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
|
---|
286 | $self->{base} =~ /(rbp|r13)/) {
|
---|
287 | $self->{base} = $self->{index}; $self->{index} = $1;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if ($gas) {
|
---|
291 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
|
---|
292 |
|
---|
293 | if (defined($self->{index})) {
|
---|
294 | sprintf "%s%s(%s,%%%s,%d)%s",
|
---|
295 | $self->{asterisk},$self->{label},
|
---|
296 | $self->{base}?"%$self->{base}":"",
|
---|
297 | $self->{index},$self->{scale},
|
---|
298 | $self->{opmask};
|
---|
299 | } else {
|
---|
300 | sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
|
---|
301 | $self->{base},$self->{opmask};
|
---|
302 | }
|
---|
303 | } else {
|
---|
304 | $self->{label} =~ s/\./\$/g;
|
---|
305 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
|
---|
306 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
|
---|
307 |
|
---|
308 | my $mnemonic = $self->{opcode}->mnemonic();
|
---|
309 | ($self->{asterisk}) && ($sz="q") ||
|
---|
310 | ($mnemonic =~ /^v?mov([qd])$/) && ($sz=$1) ||
|
---|
311 | ($mnemonic =~ /^v?pinsr([qdwb])$/) && ($sz=$1) ||
|
---|
312 | ($mnemonic =~ /^vpbroadcast([qdwb])$/) && ($sz=$1) ||
|
---|
313 | ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/) && ($sz="x");
|
---|
314 |
|
---|
315 | $self->{opmask} =~ s/%(k[0-7])/$1/;
|
---|
316 |
|
---|
317 | if (defined($self->{index})) {
|
---|
318 | sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
|
---|
319 | $self->{label}?"$self->{label}+":"",
|
---|
320 | $self->{index},$self->{scale},
|
---|
321 | $self->{base}?"+$self->{base}":"",
|
---|
322 | $self->{opmask};
|
---|
323 | } elsif ($self->{base} eq "rip") {
|
---|
324 | sprintf "%s[%s]",$szmap{$sz},$self->{label};
|
---|
325 | } else {
|
---|
326 | sprintf "%s[%s%s]%s", $szmap{$sz},
|
---|
327 | $self->{label}?"$self->{label}+":"",
|
---|
328 | $self->{base},$self->{opmask};
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 | { package register; # pick up registers, which start with %.
|
---|
334 | sub re {
|
---|
335 | my ($class, $line, $opcode) = @_;
|
---|
336 | my $self = {};
|
---|
337 | my $ret;
|
---|
338 |
|
---|
339 | # optional * ----vvv--- appears in indirect jmp/call
|
---|
340 | if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
|
---|
341 | bless $self,$class;
|
---|
342 | $self->{asterisk} = $1;
|
---|
343 | $self->{value} = $2;
|
---|
344 | $self->{opmask} = $3;
|
---|
345 | $opcode->size($self->size());
|
---|
346 | $ret = $self;
|
---|
347 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
348 | }
|
---|
349 | $ret;
|
---|
350 | }
|
---|
351 | sub size {
|
---|
352 | my $self = shift;
|
---|
353 | my $ret;
|
---|
354 |
|
---|
355 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
|
---|
356 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
|
---|
357 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
|
---|
358 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
|
---|
359 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
|
---|
360 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
|
---|
361 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
|
---|
362 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
|
---|
363 |
|
---|
364 | $ret;
|
---|
365 | }
|
---|
366 | sub out {
|
---|
367 | my $self = shift;
|
---|
368 | if ($gas) { sprintf "%s%%%s%s", $self->{asterisk},
|
---|
369 | $self->{value},
|
---|
370 | $self->{opmask}; }
|
---|
371 | else { $self->{opmask} =~ s/%(k[0-7])/$1/;
|
---|
372 | $self->{value}.$self->{opmask}; }
|
---|
373 | }
|
---|
374 | }
|
---|
375 | { package label; # pick up labels, which end with :
|
---|
376 | sub re {
|
---|
377 | my ($class, $line) = @_;
|
---|
378 | my $self = {};
|
---|
379 | my $ret;
|
---|
380 |
|
---|
381 | if ($$line =~ /(^[\.\w]+)\:/) {
|
---|
382 | bless $self,$class;
|
---|
383 | $self->{value} = $1;
|
---|
384 | $ret = $self;
|
---|
385 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
386 |
|
---|
387 | $self->{value} =~ s/^\.L/$decor/;
|
---|
388 | }
|
---|
389 | $ret;
|
---|
390 | }
|
---|
391 | sub out {
|
---|
392 | my $self = shift;
|
---|
393 |
|
---|
394 | if ($gas) {
|
---|
395 | my $func = ($globals{$self->{value}} or $self->{value}) . ":";
|
---|
396 | if ($win64 && $current_function->{name} eq $self->{value}
|
---|
397 | && $current_function->{abi} eq "svr4") {
|
---|
398 | $func .= "\n";
|
---|
399 | $func .= " movq %rdi,8(%rsp)\n";
|
---|
400 | $func .= " movq %rsi,16(%rsp)\n";
|
---|
401 | $func .= " movq %rsp,%rax\n";
|
---|
402 | $func .= "${decor}SEH_begin_$current_function->{name}:\n";
|
---|
403 | my $narg = $current_function->{narg};
|
---|
404 | $narg=6 if (!defined($narg));
|
---|
405 | $func .= " movq %rcx,%rdi\n" if ($narg>0);
|
---|
406 | $func .= " movq %rdx,%rsi\n" if ($narg>1);
|
---|
407 | $func .= " movq %r8,%rdx\n" if ($narg>2);
|
---|
408 | $func .= " movq %r9,%rcx\n" if ($narg>3);
|
---|
409 | $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
|
---|
410 | $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
|
---|
411 | }
|
---|
412 | $func;
|
---|
413 | } elsif ($self->{value} ne "$current_function->{name}") {
|
---|
414 | # Make all labels in masm global.
|
---|
415 | $self->{value} .= ":" if ($masm);
|
---|
416 | $self->{value} . ":";
|
---|
417 | } elsif ($win64 && $current_function->{abi} eq "svr4") {
|
---|
418 | my $func = "$current_function->{name}" .
|
---|
419 | ($nasm ? ":" : "\tPROC $current_function->{scope}") .
|
---|
420 | "\n";
|
---|
421 | $func .= " mov QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
|
---|
422 | $func .= " mov QWORD$PTR\[16+rsp\],rsi\n";
|
---|
423 | $func .= " mov rax,rsp\n";
|
---|
424 | $func .= "${decor}SEH_begin_$current_function->{name}:";
|
---|
425 | $func .= ":" if ($masm);
|
---|
426 | $func .= "\n";
|
---|
427 | my $narg = $current_function->{narg};
|
---|
428 | $narg=6 if (!defined($narg));
|
---|
429 | $func .= " mov rdi,rcx\n" if ($narg>0);
|
---|
430 | $func .= " mov rsi,rdx\n" if ($narg>1);
|
---|
431 | $func .= " mov rdx,r8\n" if ($narg>2);
|
---|
432 | $func .= " mov rcx,r9\n" if ($narg>3);
|
---|
433 | $func .= " mov r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
|
---|
434 | $func .= " mov r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
|
---|
435 | $func .= "\n";
|
---|
436 | } else {
|
---|
437 | "$current_function->{name}".
|
---|
438 | ($nasm ? ":" : "\tPROC $current_function->{scope}");
|
---|
439 | }
|
---|
440 | }
|
---|
441 | }
|
---|
442 | { package expr; # pick up expressions
|
---|
443 | sub re {
|
---|
444 | my ($class, $line, $opcode) = @_;
|
---|
445 | my $self = {};
|
---|
446 | my $ret;
|
---|
447 |
|
---|
448 | if ($$line =~ /(^[^,]+)/) {
|
---|
449 | bless $self,$class;
|
---|
450 | $self->{value} = $1;
|
---|
451 | $ret = $self;
|
---|
452 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
453 |
|
---|
454 | $self->{value} =~ s/\@PLT// if (!$elf);
|
---|
455 | $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
456 | $self->{value} =~ s/\.L/$decor/g;
|
---|
457 | $self->{opcode} = $opcode;
|
---|
458 | }
|
---|
459 | $ret;
|
---|
460 | }
|
---|
461 | sub out {
|
---|
462 | my $self = shift;
|
---|
463 | if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
|
---|
464 | "NEAR ".$self->{value};
|
---|
465 | } else {
|
---|
466 | $self->{value};
|
---|
467 | }
|
---|
468 | }
|
---|
469 | }
|
---|
470 | { package cfi_directive;
|
---|
471 | # CFI directives annotate instructions that are significant for
|
---|
472 | # stack unwinding procedure compliant with DWARF specification,
|
---|
473 | # see http://dwarfstd.org/. Besides naturally expected for this
|
---|
474 | # script platform-specific filtering function, this module adds
|
---|
475 | # three auxiliary synthetic directives not recognized by [GNU]
|
---|
476 | # assembler:
|
---|
477 | #
|
---|
478 | # - .cfi_push to annotate push instructions in prologue, which
|
---|
479 | # translates to .cfi_adjust_cfa_offset (if needed) and
|
---|
480 | # .cfi_offset;
|
---|
481 | # - .cfi_pop to annotate pop instructions in epilogue, which
|
---|
482 | # translates to .cfi_adjust_cfa_offset (if needed) and
|
---|
483 | # .cfi_restore;
|
---|
484 | # - [and most notably] .cfi_cfa_expression which encodes
|
---|
485 | # DW_CFA_def_cfa_expression and passes it to .cfi_escape as
|
---|
486 | # byte vector;
|
---|
487 | #
|
---|
488 | # CFA expressions were introduced in DWARF specification version
|
---|
489 | # 3 and describe how to deduce CFA, Canonical Frame Address. This
|
---|
490 | # becomes handy if your stack frame is variable and you can't
|
---|
491 | # spare register for [previous] frame pointer. Suggested directive
|
---|
492 | # syntax is made-up mix of DWARF operator suffixes [subset of]
|
---|
493 | # and references to registers with optional bias. Following example
|
---|
494 | # describes offloaded *original* stack pointer at specific offset
|
---|
495 | # from *current* stack pointer:
|
---|
496 | #
|
---|
497 | # .cfi_cfa_expression %rsp+40,deref,+8
|
---|
498 | #
|
---|
499 | # Final +8 has everything to do with the fact that CFA is defined
|
---|
500 | # as reference to top of caller's stack, and on x86_64 call to
|
---|
501 | # subroutine pushes 8-byte return address. In other words original
|
---|
502 | # stack pointer upon entry to a subroutine is 8 bytes off from CFA.
|
---|
503 |
|
---|
504 | # Below constants are taken from "DWARF Expressions" section of the
|
---|
505 | # DWARF specification, section is numbered 7.7 in versions 3 and 4.
|
---|
506 | my %DW_OP_simple = ( # no-arg operators, mapped directly
|
---|
507 | deref => 0x06, dup => 0x12,
|
---|
508 | drop => 0x13, over => 0x14,
|
---|
509 | pick => 0x15, swap => 0x16,
|
---|
510 | rot => 0x17, xderef => 0x18,
|
---|
511 |
|
---|
512 | abs => 0x19, and => 0x1a,
|
---|
513 | div => 0x1b, minus => 0x1c,
|
---|
514 | mod => 0x1d, mul => 0x1e,
|
---|
515 | neg => 0x1f, not => 0x20,
|
---|
516 | or => 0x21, plus => 0x22,
|
---|
517 | shl => 0x24, shr => 0x25,
|
---|
518 | shra => 0x26, xor => 0x27,
|
---|
519 | );
|
---|
520 |
|
---|
521 | my %DW_OP_complex = ( # used in specific subroutines
|
---|
522 | constu => 0x10, # uleb128
|
---|
523 | consts => 0x11, # sleb128
|
---|
524 | plus_uconst => 0x23, # uleb128
|
---|
525 | lit0 => 0x30, # add 0-31 to opcode
|
---|
526 | reg0 => 0x50, # add 0-31 to opcode
|
---|
527 | breg0 => 0x70, # add 0-31 to opcole, sleb128
|
---|
528 | regx => 0x90, # uleb28
|
---|
529 | fbreg => 0x91, # sleb128
|
---|
530 | bregx => 0x92, # uleb128, sleb128
|
---|
531 | piece => 0x93, # uleb128
|
---|
532 | );
|
---|
533 |
|
---|
534 | # Following constants are defined in x86_64 ABI supplement, for
|
---|
535 | # example available at https://www.uclibc.org/docs/psABI-x86_64.pdf,
|
---|
536 | # see section 3.7 "Stack Unwind Algorithm".
|
---|
537 | my %DW_reg_idx = (
|
---|
538 | "%rax"=>0, "%rdx"=>1, "%rcx"=>2, "%rbx"=>3,
|
---|
539 | "%rsi"=>4, "%rdi"=>5, "%rbp"=>6, "%rsp"=>7,
|
---|
540 | "%r8" =>8, "%r9" =>9, "%r10"=>10, "%r11"=>11,
|
---|
541 | "%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
|
---|
542 | );
|
---|
543 |
|
---|
544 | my ($cfa_reg, $cfa_rsp);
|
---|
545 | my @cfa_stack;
|
---|
546 |
|
---|
547 | # [us]leb128 format is variable-length integer representation base
|
---|
548 | # 2^128, with most significant bit of each byte being 0 denoting
|
---|
549 | # *last* most significant digit. See "Variable Length Data" in the
|
---|
550 | # DWARF specification, numbered 7.6 at least in versions 3 and 4.
|
---|
551 | sub sleb128 {
|
---|
552 | use integer; # get right shift extend sign
|
---|
553 |
|
---|
554 | my $val = shift;
|
---|
555 | my $sign = ($val < 0) ? -1 : 0;
|
---|
556 | my @ret = ();
|
---|
557 |
|
---|
558 | while(1) {
|
---|
559 | push @ret, $val&0x7f;
|
---|
560 |
|
---|
561 | # see if remaining bits are same and equal to most
|
---|
562 | # significant bit of the current digit, if so, it's
|
---|
563 | # last digit...
|
---|
564 | last if (($val>>6) == $sign);
|
---|
565 |
|
---|
566 | @ret[-1] |= 0x80;
|
---|
567 | $val >>= 7;
|
---|
568 | }
|
---|
569 |
|
---|
570 | return @ret;
|
---|
571 | }
|
---|
572 | sub uleb128 {
|
---|
573 | my $val = shift;
|
---|
574 | my @ret = ();
|
---|
575 |
|
---|
576 | while(1) {
|
---|
577 | push @ret, $val&0x7f;
|
---|
578 |
|
---|
579 | # see if it's last significant digit...
|
---|
580 | last if (($val >>= 7) == 0);
|
---|
581 |
|
---|
582 | @ret[-1] |= 0x80;
|
---|
583 | }
|
---|
584 |
|
---|
585 | return @ret;
|
---|
586 | }
|
---|
587 | sub const {
|
---|
588 | my $val = shift;
|
---|
589 |
|
---|
590 | if ($val >= 0 && $val < 32) {
|
---|
591 | return ($DW_OP_complex{lit0}+$val);
|
---|
592 | }
|
---|
593 | return ($DW_OP_complex{consts}, sleb128($val));
|
---|
594 | }
|
---|
595 | sub reg {
|
---|
596 | my $val = shift;
|
---|
597 |
|
---|
598 | return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
|
---|
599 |
|
---|
600 | my $reg = $DW_reg_idx{$1};
|
---|
601 | my $off = eval ("0 $2 $3");
|
---|
602 |
|
---|
603 | return (($DW_OP_complex{breg0} + $reg), sleb128($off));
|
---|
604 | # Yes, we use DW_OP_bregX+0 to push register value and not
|
---|
605 | # DW_OP_regX, because latter would require even DW_OP_piece,
|
---|
606 | # which would be a waste under the circumstances. If you have
|
---|
607 | # to use DWP_OP_reg, use "regx:N"...
|
---|
608 | }
|
---|
609 | sub cfa_expression {
|
---|
610 | my $line = shift;
|
---|
611 | my @ret;
|
---|
612 |
|
---|
613 | foreach my $token (split(/,\s*/,$line)) {
|
---|
614 | if ($token =~ /^%r/) {
|
---|
615 | push @ret,reg($token);
|
---|
616 | } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
|
---|
617 | push @ret,reg("$2+$1");
|
---|
618 | } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
|
---|
619 | my $i = 1*eval($2);
|
---|
620 | push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
|
---|
621 | } elsif (my $i = 1*eval($token) or $token eq "0") {
|
---|
622 | if ($token =~ /^\+/) {
|
---|
623 | push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
|
---|
624 | } else {
|
---|
625 | push @ret,const($i);
|
---|
626 | }
|
---|
627 | } else {
|
---|
628 | push @ret,$DW_OP_simple{$token};
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | # Finally we return DW_CFA_def_cfa_expression, 15, followed by
|
---|
633 | # length of the expression and of course the expression itself.
|
---|
634 | return (15,scalar(@ret),@ret);
|
---|
635 | }
|
---|
636 | sub re {
|
---|
637 | my ($class, $line) = @_;
|
---|
638 | my $self = {};
|
---|
639 | my $ret;
|
---|
640 |
|
---|
641 | if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
|
---|
642 | bless $self,$class;
|
---|
643 | $ret = $self;
|
---|
644 | undef $self->{value};
|
---|
645 | my $dir = $1;
|
---|
646 |
|
---|
647 | SWITCH: for ($dir) {
|
---|
648 | # What is $cfa_rsp? Effectively it's difference between %rsp
|
---|
649 | # value and current CFA, Canonical Frame Address, which is
|
---|
650 | # why it starts with -8. Recall that CFA is top of caller's
|
---|
651 | # stack...
|
---|
652 | /startproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
|
---|
653 | /endproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", 0);
|
---|
654 | # .cfi_remember_state directives that are not
|
---|
655 | # matched with .cfi_restore_state are
|
---|
656 | # unnecessary.
|
---|
657 | die "unpaired .cfi_remember_state" if (@cfa_stack);
|
---|
658 | last;
|
---|
659 | };
|
---|
660 | /def_cfa_register/
|
---|
661 | && do { $cfa_reg = $$line; last; };
|
---|
662 | /def_cfa_offset/
|
---|
663 | && do { $cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
|
---|
664 | last;
|
---|
665 | };
|
---|
666 | /adjust_cfa_offset/
|
---|
667 | && do { $cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
|
---|
668 | last;
|
---|
669 | };
|
---|
670 | /def_cfa/ && do { if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
|
---|
671 | $cfa_reg = $1;
|
---|
672 | $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
|
---|
673 | }
|
---|
674 | last;
|
---|
675 | };
|
---|
676 | /push/ && do { $dir = undef;
|
---|
677 | $cfa_rsp -= 8;
|
---|
678 | if ($cfa_reg eq "%rsp") {
|
---|
679 | $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
|
---|
680 | }
|
---|
681 | $self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
|
---|
682 | last;
|
---|
683 | };
|
---|
684 | /pop/ && do { $dir = undef;
|
---|
685 | $cfa_rsp += 8;
|
---|
686 | if ($cfa_reg eq "%rsp") {
|
---|
687 | $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
|
---|
688 | }
|
---|
689 | $self->{value} .= ".cfi_restore\t$$line";
|
---|
690 | last;
|
---|
691 | };
|
---|
692 | /cfa_expression/
|
---|
693 | && do { $dir = undef;
|
---|
694 | $self->{value} = ".cfi_escape\t" .
|
---|
695 | join(",", map(sprintf("0x%02x", $_),
|
---|
696 | cfa_expression($$line)));
|
---|
697 | last;
|
---|
698 | };
|
---|
699 | /remember_state/
|
---|
700 | && do { push @cfa_stack, [$cfa_reg, $cfa_rsp];
|
---|
701 | last;
|
---|
702 | };
|
---|
703 | /restore_state/
|
---|
704 | && do { ($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
|
---|
705 | last;
|
---|
706 | };
|
---|
707 | }
|
---|
708 |
|
---|
709 | $self->{value} = ".cfi_$dir\t$$line" if ($dir);
|
---|
710 |
|
---|
711 | $$line = "";
|
---|
712 | }
|
---|
713 |
|
---|
714 | return $ret;
|
---|
715 | }
|
---|
716 | sub out {
|
---|
717 | my $self = shift;
|
---|
718 | return ($elf ? $self->{value} : undef);
|
---|
719 | }
|
---|
720 | }
|
---|
721 | { package directive; # pick up directives, which start with .
|
---|
722 | sub re {
|
---|
723 | my ($class, $line) = @_;
|
---|
724 | my $self = {};
|
---|
725 | my $ret;
|
---|
726 | my $dir;
|
---|
727 |
|
---|
728 | # chain-call to cfi_directive
|
---|
729 | $ret = cfi_directive->re($line) and return $ret;
|
---|
730 |
|
---|
731 | if ($$line =~ /^\s*(\.\w+)/) {
|
---|
732 | bless $self,$class;
|
---|
733 | $dir = $1;
|
---|
734 | $ret = $self;
|
---|
735 | undef $self->{value};
|
---|
736 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
737 |
|
---|
738 | SWITCH: for ($dir) {
|
---|
739 | /\.global|\.globl|\.extern/
|
---|
740 | && do { $globals{$$line} = $prefix . $$line;
|
---|
741 | $$line = $globals{$$line} if ($prefix);
|
---|
742 | last;
|
---|
743 | };
|
---|
744 | /\.type/ && do { my ($sym,$type,$narg) = split(',',$$line);
|
---|
745 | if ($type eq "\@function") {
|
---|
746 | undef $current_function;
|
---|
747 | $current_function->{name} = $sym;
|
---|
748 | $current_function->{abi} = "svr4";
|
---|
749 | $current_function->{narg} = $narg;
|
---|
750 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
|
---|
751 | } elsif ($type eq "\@abi-omnipotent") {
|
---|
752 | undef $current_function;
|
---|
753 | $current_function->{name} = $sym;
|
---|
754 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
|
---|
755 | }
|
---|
756 | $$line =~ s/\@abi\-omnipotent/\@function/;
|
---|
757 | $$line =~ s/\@function.*/\@function/;
|
---|
758 | last;
|
---|
759 | };
|
---|
760 | /\.asciz/ && do { if ($$line =~ /^"(.*)"$/) {
|
---|
761 | $dir = ".byte";
|
---|
762 | $$line = join(",",unpack("C*",$1),0);
|
---|
763 | }
|
---|
764 | last;
|
---|
765 | };
|
---|
766 | /\.rva|\.long|\.quad/
|
---|
767 | && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
768 | $$line =~ s/\.L/$decor/g;
|
---|
769 | last;
|
---|
770 | };
|
---|
771 | }
|
---|
772 |
|
---|
773 | if ($gas) {
|
---|
774 | $self->{value} = $dir . "\t" . $$line;
|
---|
775 |
|
---|
776 | if ($dir =~ /\.extern/) {
|
---|
777 | $self->{value} = ""; # swallow extern
|
---|
778 | } elsif (!$elf && $dir =~ /\.type/) {
|
---|
779 | $self->{value} = "";
|
---|
780 | $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
|
---|
781 | (defined($globals{$1})?".scl 2;":".scl 3;") .
|
---|
782 | "\t.type 32;\t.endef"
|
---|
783 | if ($win64 && $$line =~ /([^,]+),\@function/);
|
---|
784 | } elsif (!$elf && $dir =~ /\.size/) {
|
---|
785 | $self->{value} = "";
|
---|
786 | if (defined($current_function)) {
|
---|
787 | $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
|
---|
788 | if ($win64 && $current_function->{abi} eq "svr4");
|
---|
789 | undef $current_function;
|
---|
790 | }
|
---|
791 | } elsif (!$elf && $dir =~ /\.align/) {
|
---|
792 | $self->{value} = ".p2align\t" . (log($$line)/log(2));
|
---|
793 | } elsif ($dir eq ".section") {
|
---|
794 | $current_segment=$$line;
|
---|
795 | if (!$elf && $current_segment eq ".init") {
|
---|
796 | if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
|
---|
797 | elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
|
---|
798 | }
|
---|
799 | } elsif ($dir =~ /\.(text|data)/) {
|
---|
800 | $current_segment=".$1";
|
---|
801 | } elsif ($dir =~ /\.hidden/) {
|
---|
802 | if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$$line"; }
|
---|
803 | elsif ($flavour eq "mingw64") { $self->{value} = ""; }
|
---|
804 | } elsif ($dir =~ /\.comm/) {
|
---|
805 | $self->{value} = "$dir\t$prefix$$line";
|
---|
806 | $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
|
---|
807 | }
|
---|
808 | $$line = "";
|
---|
809 | return $self;
|
---|
810 | }
|
---|
811 |
|
---|
812 | # non-gas case or nasm/masm
|
---|
813 | SWITCH: for ($dir) {
|
---|
814 | /\.text/ && do { my $v=undef;
|
---|
815 | if ($nasm) {
|
---|
816 | $v="section .text code align=64\n";
|
---|
817 | } else {
|
---|
818 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
819 | $current_segment = ".text\$";
|
---|
820 | $v.="$current_segment\tSEGMENT ";
|
---|
821 | $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
|
---|
822 | $v.=" 'CODE'";
|
---|
823 | }
|
---|
824 | $self->{value} = $v;
|
---|
825 | last;
|
---|
826 | };
|
---|
827 | /\.data/ && do { my $v=undef;
|
---|
828 | if ($nasm) {
|
---|
829 | $v="section .data data align=8\n";
|
---|
830 | } else {
|
---|
831 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
832 | $current_segment = "_DATA";
|
---|
833 | $v.="$current_segment\tSEGMENT";
|
---|
834 | }
|
---|
835 | $self->{value} = $v;
|
---|
836 | last;
|
---|
837 | };
|
---|
838 | /\.section/ && do { my $v=undef;
|
---|
839 | $$line =~ s/([^,]*).*/$1/;
|
---|
840 | $$line = ".CRT\$XCU" if ($$line eq ".init");
|
---|
841 | if ($nasm) {
|
---|
842 | $v="section $$line";
|
---|
843 | if ($$line=~/\.([px])data/) {
|
---|
844 | $v.=" rdata align=";
|
---|
845 | $v.=$1 eq "p"? 4 : 8;
|
---|
846 | } elsif ($$line=~/\.CRT\$/i) {
|
---|
847 | $v.=" rdata align=8";
|
---|
848 | }
|
---|
849 | } else {
|
---|
850 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
851 | $v.="$$line\tSEGMENT";
|
---|
852 | if ($$line=~/\.([px])data/) {
|
---|
853 | $v.=" READONLY";
|
---|
854 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
|
---|
855 | } elsif ($$line=~/\.CRT\$/i) {
|
---|
856 | $v.=" READONLY ";
|
---|
857 | $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
|
---|
858 | }
|
---|
859 | }
|
---|
860 | $current_segment = $$line;
|
---|
861 | $self->{value} = $v;
|
---|
862 | last;
|
---|
863 | };
|
---|
864 | /\.extern/ && do { $self->{value} = "EXTERN\t".$$line;
|
---|
865 | $self->{value} .= ":NEAR" if ($masm);
|
---|
866 | last;
|
---|
867 | };
|
---|
868 | /\.globl|.global/
|
---|
869 | && do { $self->{value} = $masm?"PUBLIC":"global";
|
---|
870 | $self->{value} .= "\t".$$line;
|
---|
871 | last;
|
---|
872 | };
|
---|
873 | /\.size/ && do { if (defined($current_function)) {
|
---|
874 | undef $self->{value};
|
---|
875 | if ($current_function->{abi} eq "svr4") {
|
---|
876 | $self->{value}="${decor}SEH_end_$current_function->{name}:";
|
---|
877 | $self->{value}.=":\n" if($masm);
|
---|
878 | }
|
---|
879 | $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
|
---|
880 | undef $current_function;
|
---|
881 | }
|
---|
882 | last;
|
---|
883 | };
|
---|
884 | /\.align/ && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
|
---|
885 | $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
|
---|
886 | last;
|
---|
887 | };
|
---|
888 | /\.(value|long|rva|quad)/
|
---|
889 | && do { my $sz = substr($1,0,1);
|
---|
890 | my @arr = split(/,\s*/,$$line);
|
---|
891 | my $last = pop(@arr);
|
---|
892 | my $conv = sub { my $var=shift;
|
---|
893 | $var=~s/^(0b[0-1]+)/oct($1)/eig;
|
---|
894 | $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
|
---|
895 | if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
|
---|
896 | { $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
|
---|
897 | $var;
|
---|
898 | };
|
---|
899 |
|
---|
900 | $sz =~ tr/bvlrq/BWDDQ/;
|
---|
901 | $self->{value} = "\tD$sz\t";
|
---|
902 | for (@arr) { $self->{value} .= &$conv($_).","; }
|
---|
903 | $self->{value} .= &$conv($last);
|
---|
904 | last;
|
---|
905 | };
|
---|
906 | /\.byte/ && do { my @str=split(/,\s*/,$$line);
|
---|
907 | map(s/(0b[0-1]+)/oct($1)/eig,@str);
|
---|
908 | map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
|
---|
909 | while ($#str>15) {
|
---|
910 | $self->{value}.="DB\t"
|
---|
911 | .join(",",@str[0..15])."\n";
|
---|
912 | foreach (0..15) { shift @str; }
|
---|
913 | }
|
---|
914 | $self->{value}.="DB\t"
|
---|
915 | .join(",",@str) if (@str);
|
---|
916 | last;
|
---|
917 | };
|
---|
918 | /\.comm/ && do { my @str=split(/,\s*/,$$line);
|
---|
919 | my $v=undef;
|
---|
920 | if ($nasm) {
|
---|
921 | $v.="common $prefix@str[0] @str[1]";
|
---|
922 | } else {
|
---|
923 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
924 | $current_segment = "_DATA";
|
---|
925 | $v.="$current_segment\tSEGMENT\n";
|
---|
926 | $v.="COMM @str[0]:DWORD:".@str[1]/4;
|
---|
927 | }
|
---|
928 | $self->{value} = $v;
|
---|
929 | last;
|
---|
930 | };
|
---|
931 | }
|
---|
932 | $$line = "";
|
---|
933 | }
|
---|
934 |
|
---|
935 | $ret;
|
---|
936 | }
|
---|
937 | sub out {
|
---|
938 | my $self = shift;
|
---|
939 | $self->{value};
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 | # Upon initial x86_64 introduction SSE>2 extensions were not introduced
|
---|
944 | # yet. In order not to be bothered by tracing exact assembler versions,
|
---|
945 | # but at the same time to provide a bare security minimum of AES-NI, we
|
---|
946 | # hard-code some instructions. Extensions past AES-NI on the other hand
|
---|
947 | # are traced by examining assembler version in individual perlasm
|
---|
948 | # modules...
|
---|
949 |
|
---|
950 | my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
|
---|
951 | "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
|
---|
952 |
|
---|
953 | sub rex {
|
---|
954 | my $opcode=shift;
|
---|
955 | my ($dst,$src,$rex)=@_;
|
---|
956 |
|
---|
957 | $rex|=0x04 if($dst>=8);
|
---|
958 | $rex|=0x01 if($src>=8);
|
---|
959 | push @$opcode,($rex|0x40) if ($rex);
|
---|
960 | }
|
---|
961 |
|
---|
962 | my $movq = sub { # elderly gas can't handle inter-register movq
|
---|
963 | my $arg = shift;
|
---|
964 | my @opcode=(0x66);
|
---|
965 | if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
|
---|
966 | my ($src,$dst)=($1,$2);
|
---|
967 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
968 | rex(\@opcode,$src,$dst,0x8);
|
---|
969 | push @opcode,0x0f,0x7e;
|
---|
970 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
971 | @opcode;
|
---|
972 | } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
|
---|
973 | my ($src,$dst)=($2,$1);
|
---|
974 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
975 | rex(\@opcode,$src,$dst,0x8);
|
---|
976 | push @opcode,0x0f,0x6e;
|
---|
977 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
978 | @opcode;
|
---|
979 | } else {
|
---|
980 | ();
|
---|
981 | }
|
---|
982 | };
|
---|
983 |
|
---|
984 | my $pextrd = sub {
|
---|
985 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
|
---|
986 | my @opcode=(0x66);
|
---|
987 | my $imm=$1;
|
---|
988 | my $src=$2;
|
---|
989 | my $dst=$3;
|
---|
990 | if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
|
---|
991 | elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
|
---|
992 | rex(\@opcode,$src,$dst);
|
---|
993 | push @opcode,0x0f,0x3a,0x16;
|
---|
994 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
995 | push @opcode,$imm;
|
---|
996 | @opcode;
|
---|
997 | } else {
|
---|
998 | ();
|
---|
999 | }
|
---|
1000 | };
|
---|
1001 |
|
---|
1002 | my $pinsrd = sub {
|
---|
1003 | if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
|
---|
1004 | my @opcode=(0x66);
|
---|
1005 | my $imm=$1;
|
---|
1006 | my $src=$2;
|
---|
1007 | my $dst=$3;
|
---|
1008 | if ($src =~ /%r([0-9]+)/) { $src = $1; }
|
---|
1009 | elsif ($src =~ /%e/) { $src = $regrm{$src}; }
|
---|
1010 | rex(\@opcode,$dst,$src);
|
---|
1011 | push @opcode,0x0f,0x3a,0x22;
|
---|
1012 | push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
|
---|
1013 | push @opcode,$imm;
|
---|
1014 | @opcode;
|
---|
1015 | } else {
|
---|
1016 | ();
|
---|
1017 | }
|
---|
1018 | };
|
---|
1019 |
|
---|
1020 | my $pshufb = sub {
|
---|
1021 | if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1022 | my @opcode=(0x66);
|
---|
1023 | rex(\@opcode,$2,$1);
|
---|
1024 | push @opcode,0x0f,0x38,0x00;
|
---|
1025 | push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
|
---|
1026 | @opcode;
|
---|
1027 | } else {
|
---|
1028 | ();
|
---|
1029 | }
|
---|
1030 | };
|
---|
1031 |
|
---|
1032 | my $palignr = sub {
|
---|
1033 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1034 | my @opcode=(0x66);
|
---|
1035 | rex(\@opcode,$3,$2);
|
---|
1036 | push @opcode,0x0f,0x3a,0x0f;
|
---|
1037 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1038 | push @opcode,$1;
|
---|
1039 | @opcode;
|
---|
1040 | } else {
|
---|
1041 | ();
|
---|
1042 | }
|
---|
1043 | };
|
---|
1044 |
|
---|
1045 | my $pclmulqdq = sub {
|
---|
1046 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1047 | my @opcode=(0x66);
|
---|
1048 | rex(\@opcode,$3,$2);
|
---|
1049 | push @opcode,0x0f,0x3a,0x44;
|
---|
1050 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1051 | my $c=$1;
|
---|
1052 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1053 | @opcode;
|
---|
1054 | } else {
|
---|
1055 | ();
|
---|
1056 | }
|
---|
1057 | };
|
---|
1058 |
|
---|
1059 | my $rdrand = sub {
|
---|
1060 | if (shift =~ /%[er](\w+)/) {
|
---|
1061 | my @opcode=();
|
---|
1062 | my $dst=$1;
|
---|
1063 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1064 | rex(\@opcode,0,$dst,8);
|
---|
1065 | push @opcode,0x0f,0xc7,0xf0|($dst&7);
|
---|
1066 | @opcode;
|
---|
1067 | } else {
|
---|
1068 | ();
|
---|
1069 | }
|
---|
1070 | };
|
---|
1071 |
|
---|
1072 | my $rdseed = sub {
|
---|
1073 | if (shift =~ /%[er](\w+)/) {
|
---|
1074 | my @opcode=();
|
---|
1075 | my $dst=$1;
|
---|
1076 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1077 | rex(\@opcode,0,$dst,8);
|
---|
1078 | push @opcode,0x0f,0xc7,0xf8|($dst&7);
|
---|
1079 | @opcode;
|
---|
1080 | } else {
|
---|
1081 | ();
|
---|
1082 | }
|
---|
1083 | };
|
---|
1084 |
|
---|
1085 | # Not all AVX-capable assemblers recognize AMD XOP extension. Since we
|
---|
1086 | # are using only two instructions hand-code them in order to be excused
|
---|
1087 | # from chasing assembler versions...
|
---|
1088 |
|
---|
1089 | sub rxb {
|
---|
1090 | my $opcode=shift;
|
---|
1091 | my ($dst,$src1,$src2,$rxb)=@_;
|
---|
1092 |
|
---|
1093 | $rxb|=0x7<<5;
|
---|
1094 | $rxb&=~(0x04<<5) if($dst>=8);
|
---|
1095 | $rxb&=~(0x01<<5) if($src1>=8);
|
---|
1096 | $rxb&=~(0x02<<5) if($src2>=8);
|
---|
1097 | push @$opcode,$rxb;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | my $vprotd = sub {
|
---|
1101 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1102 | my @opcode=(0x8f);
|
---|
1103 | rxb(\@opcode,$3,$2,-1,0x08);
|
---|
1104 | push @opcode,0x78,0xc2;
|
---|
1105 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1106 | my $c=$1;
|
---|
1107 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1108 | @opcode;
|
---|
1109 | } else {
|
---|
1110 | ();
|
---|
1111 | }
|
---|
1112 | };
|
---|
1113 |
|
---|
1114 | my $vprotq = sub {
|
---|
1115 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1116 | my @opcode=(0x8f);
|
---|
1117 | rxb(\@opcode,$3,$2,-1,0x08);
|
---|
1118 | push @opcode,0x78,0xc3;
|
---|
1119 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1120 | my $c=$1;
|
---|
1121 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1122 | @opcode;
|
---|
1123 | } else {
|
---|
1124 | ();
|
---|
1125 | }
|
---|
1126 | };
|
---|
1127 |
|
---|
1128 | # Intel Control-flow Enforcement Technology extension. All functions and
|
---|
1129 | # indirect branch targets will have to start with this instruction...
|
---|
1130 |
|
---|
1131 | my $endbranch = sub {
|
---|
1132 | (0xf3,0x0f,0x1e,0xfa);
|
---|
1133 | };
|
---|
1134 |
|
---|
1135 | ########################################################################
|
---|
1136 |
|
---|
1137 | if ($nasm) {
|
---|
1138 | print <<___;
|
---|
1139 | default rel
|
---|
1140 | %define XMMWORD
|
---|
1141 | %define YMMWORD
|
---|
1142 | %define ZMMWORD
|
---|
1143 | ___
|
---|
1144 | } elsif ($masm) {
|
---|
1145 | print <<___;
|
---|
1146 | OPTION DOTNAME
|
---|
1147 | ___
|
---|
1148 | }
|
---|
1149 | while(defined(my $line=<>)) {
|
---|
1150 |
|
---|
1151 | $line =~ s|\R$||; # Better chomp
|
---|
1152 |
|
---|
1153 | $line =~ s|[#!].*$||; # get rid of asm-style comments...
|
---|
1154 | $line =~ s|/\*.*\*/||; # ... and C-style comments...
|
---|
1155 | $line =~ s|^\s+||; # ... and skip white spaces in beginning
|
---|
1156 | $line =~ s|\s+$||; # ... and at the end
|
---|
1157 |
|
---|
1158 | if (my $label=label->re(\$line)) { print $label->out(); }
|
---|
1159 |
|
---|
1160 | if (my $directive=directive->re(\$line)) {
|
---|
1161 | printf "%s",$directive->out();
|
---|
1162 | } elsif (my $opcode=opcode->re(\$line)) {
|
---|
1163 | my $asm = eval("\$".$opcode->mnemonic());
|
---|
1164 |
|
---|
1165 | if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
|
---|
1166 | print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
|
---|
1167 | next;
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | my @args;
|
---|
1171 | ARGUMENT: while (1) {
|
---|
1172 | my $arg;
|
---|
1173 |
|
---|
1174 | ($arg=register->re(\$line, $opcode))||
|
---|
1175 | ($arg=const->re(\$line)) ||
|
---|
1176 | ($arg=ea->re(\$line, $opcode)) ||
|
---|
1177 | ($arg=expr->re(\$line, $opcode)) ||
|
---|
1178 | last ARGUMENT;
|
---|
1179 |
|
---|
1180 | push @args,$arg;
|
---|
1181 |
|
---|
1182 | last ARGUMENT if ($line !~ /^,/);
|
---|
1183 |
|
---|
1184 | $line =~ s/^,\s*//;
|
---|
1185 | } # ARGUMENT:
|
---|
1186 |
|
---|
1187 | if ($#args>=0) {
|
---|
1188 | my $insn;
|
---|
1189 | my $sz=$opcode->size();
|
---|
1190 |
|
---|
1191 | if ($gas) {
|
---|
1192 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
|
---|
1193 | @args = map($_->out($sz),@args);
|
---|
1194 | printf "\t%s\t%s",$insn,join(",",@args);
|
---|
1195 | } else {
|
---|
1196 | $insn = $opcode->out();
|
---|
1197 | foreach (@args) {
|
---|
1198 | my $arg = $_->out();
|
---|
1199 | # $insn.=$sz compensates for movq, pinsrw, ...
|
---|
1200 | if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
|
---|
1201 | if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
|
---|
1202 | if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
|
---|
1203 | if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
|
---|
1204 | }
|
---|
1205 | @args = reverse(@args);
|
---|
1206 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
|
---|
1207 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
|
---|
1208 | }
|
---|
1209 | } else {
|
---|
1210 | printf "\t%s",$opcode->out();
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | print $line,"\n";
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
|
---|
1218 | print "END\n" if ($masm);
|
---|
1219 |
|
---|
1220 | close STDOUT or die "error closing STDOUT: $!";
|
---|
1221 |
|
---|
1222 | |
---|
1223 | #################################################
|
---|
1224 | # Cross-reference x86_64 ABI "card"
|
---|
1225 | #
|
---|
1226 | # Unix Win64
|
---|
1227 | # %rax * *
|
---|
1228 | # %rbx - -
|
---|
1229 | # %rcx #4 #1
|
---|
1230 | # %rdx #3 #2
|
---|
1231 | # %rsi #2 -
|
---|
1232 | # %rdi #1 -
|
---|
1233 | # %rbp - -
|
---|
1234 | # %rsp - -
|
---|
1235 | # %r8 #5 #3
|
---|
1236 | # %r9 #6 #4
|
---|
1237 | # %r10 * *
|
---|
1238 | # %r11 * *
|
---|
1239 | # %r12 - -
|
---|
1240 | # %r13 - -
|
---|
1241 | # %r14 - -
|
---|
1242 | # %r15 - -
|
---|
1243 | #
|
---|
1244 | # (*) volatile register
|
---|
1245 | # (-) preserved by callee
|
---|
1246 | # (#) Nth argument, volatile
|
---|
1247 | #
|
---|
1248 | # In Unix terms top of stack is argument transfer area for arguments
|
---|
1249 | # which could not be accommodated in registers. Or in other words 7th
|
---|
1250 | # [integer] argument resides at 8(%rsp) upon function entry point.
|
---|
1251 | # 128 bytes above %rsp constitute a "red zone" which is not touched
|
---|
1252 | # by signal handlers and can be used as temporal storage without
|
---|
1253 | # allocating a frame.
|
---|
1254 | #
|
---|
1255 | # In Win64 terms N*8 bytes on top of stack is argument transfer area,
|
---|
1256 | # which belongs to/can be overwritten by callee. N is the number of
|
---|
1257 | # arguments passed to callee, *but* not less than 4! This means that
|
---|
1258 | # upon function entry point 5th argument resides at 40(%rsp), as well
|
---|
1259 | # as that 32 bytes from 8(%rsp) can always be used as temporal
|
---|
1260 | # storage [without allocating a frame]. One can actually argue that
|
---|
1261 | # one can assume a "red zone" above stack pointer under Win64 as well.
|
---|
1262 | # Point is that at apparently no occasion Windows kernel would alter
|
---|
1263 | # the area above user stack pointer in true asynchronous manner...
|
---|
1264 | #
|
---|
1265 | # All the above means that if assembler programmer adheres to Unix
|
---|
1266 | # register and stack layout, but disregards the "red zone" existence,
|
---|
1267 | # it's possible to use following prologue and epilogue to "gear" from
|
---|
1268 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
|
---|
1269 | #
|
---|
1270 | # omnipotent_function:
|
---|
1271 | # ifdef WIN64
|
---|
1272 | # movq %rdi,8(%rsp)
|
---|
1273 | # movq %rsi,16(%rsp)
|
---|
1274 | # movq %rcx,%rdi ; if 1st argument is actually present
|
---|
1275 | # movq %rdx,%rsi ; if 2nd argument is actually ...
|
---|
1276 | # movq %r8,%rdx ; if 3rd argument is ...
|
---|
1277 | # movq %r9,%rcx ; if 4th argument ...
|
---|
1278 | # movq 40(%rsp),%r8 ; if 5th ...
|
---|
1279 | # movq 48(%rsp),%r9 ; if 6th ...
|
---|
1280 | # endif
|
---|
1281 | # ...
|
---|
1282 | # ifdef WIN64
|
---|
1283 | # movq 8(%rsp),%rdi
|
---|
1284 | # movq 16(%rsp),%rsi
|
---|
1285 | # endif
|
---|
1286 | # ret
|
---|
1287 | #
|
---|
1288 | |
---|
1289 | #################################################
|
---|
1290 | # Win64 SEH, Structured Exception Handling.
|
---|
1291 | #
|
---|
1292 | # Unlike on Unix systems(*) lack of Win64 stack unwinding information
|
---|
1293 | # has undesired side-effect at run-time: if an exception is raised in
|
---|
1294 | # assembler subroutine such as those in question (basically we're
|
---|
1295 | # referring to segmentation violations caused by malformed input
|
---|
1296 | # parameters), the application is briskly terminated without invoking
|
---|
1297 | # any exception handlers, most notably without generating memory dump
|
---|
1298 | # or any user notification whatsoever. This poses a problem. It's
|
---|
1299 | # possible to address it by registering custom language-specific
|
---|
1300 | # handler that would restore processor context to the state at
|
---|
1301 | # subroutine entry point and return "exception is not handled, keep
|
---|
1302 | # unwinding" code. Writing such handler can be a challenge... But it's
|
---|
1303 | # doable, though requires certain coding convention. Consider following
|
---|
1304 | # snippet:
|
---|
1305 | #
|
---|
1306 | # .type function,@function
|
---|
1307 | # function:
|
---|
1308 | # movq %rsp,%rax # copy rsp to volatile register
|
---|
1309 | # pushq %r15 # save non-volatile registers
|
---|
1310 | # pushq %rbx
|
---|
1311 | # pushq %rbp
|
---|
1312 | # movq %rsp,%r11
|
---|
1313 | # subq %rdi,%r11 # prepare [variable] stack frame
|
---|
1314 | # andq $-64,%r11
|
---|
1315 | # movq %rax,0(%r11) # check for exceptions
|
---|
1316 | # movq %r11,%rsp # allocate [variable] stack frame
|
---|
1317 | # movq %rax,0(%rsp) # save original rsp value
|
---|
1318 | # magic_point:
|
---|
1319 | # ...
|
---|
1320 | # movq 0(%rsp),%rcx # pull original rsp value
|
---|
1321 | # movq -24(%rcx),%rbp # restore non-volatile registers
|
---|
1322 | # movq -16(%rcx),%rbx
|
---|
1323 | # movq -8(%rcx),%r15
|
---|
1324 | # movq %rcx,%rsp # restore original rsp
|
---|
1325 | # magic_epilogue:
|
---|
1326 | # ret
|
---|
1327 | # .size function,.-function
|
---|
1328 | #
|
---|
1329 | # The key is that up to magic_point copy of original rsp value remains
|
---|
1330 | # in chosen volatile register and no non-volatile register, except for
|
---|
1331 | # rsp, is modified. While past magic_point rsp remains constant till
|
---|
1332 | # the very end of the function. In this case custom language-specific
|
---|
1333 | # exception handler would look like this:
|
---|
1334 | #
|
---|
1335 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
|
---|
1336 | # CONTEXT *context,DISPATCHER_CONTEXT *disp)
|
---|
1337 | # { ULONG64 *rsp = (ULONG64 *)context->Rax;
|
---|
1338 | # ULONG64 rip = context->Rip;
|
---|
1339 | #
|
---|
1340 | # if (rip >= magic_point)
|
---|
1341 | # { rsp = (ULONG64 *)context->Rsp;
|
---|
1342 | # if (rip < magic_epilogue)
|
---|
1343 | # { rsp = (ULONG64 *)rsp[0];
|
---|
1344 | # context->Rbp = rsp[-3];
|
---|
1345 | # context->Rbx = rsp[-2];
|
---|
1346 | # context->R15 = rsp[-1];
|
---|
1347 | # }
|
---|
1348 | # }
|
---|
1349 | # context->Rsp = (ULONG64)rsp;
|
---|
1350 | # context->Rdi = rsp[1];
|
---|
1351 | # context->Rsi = rsp[2];
|
---|
1352 | #
|
---|
1353 | # memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
|
---|
1354 | # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
|
---|
1355 | # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
|
---|
1356 | # &disp->HandlerData,&disp->EstablisherFrame,NULL);
|
---|
1357 | # return ExceptionContinueSearch;
|
---|
1358 | # }
|
---|
1359 | #
|
---|
1360 | # It's appropriate to implement this handler in assembler, directly in
|
---|
1361 | # function's module. In order to do that one has to know members'
|
---|
1362 | # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
|
---|
1363 | # values. Here they are:
|
---|
1364 | #
|
---|
1365 | # CONTEXT.Rax 120
|
---|
1366 | # CONTEXT.Rcx 128
|
---|
1367 | # CONTEXT.Rdx 136
|
---|
1368 | # CONTEXT.Rbx 144
|
---|
1369 | # CONTEXT.Rsp 152
|
---|
1370 | # CONTEXT.Rbp 160
|
---|
1371 | # CONTEXT.Rsi 168
|
---|
1372 | # CONTEXT.Rdi 176
|
---|
1373 | # CONTEXT.R8 184
|
---|
1374 | # CONTEXT.R9 192
|
---|
1375 | # CONTEXT.R10 200
|
---|
1376 | # CONTEXT.R11 208
|
---|
1377 | # CONTEXT.R12 216
|
---|
1378 | # CONTEXT.R13 224
|
---|
1379 | # CONTEXT.R14 232
|
---|
1380 | # CONTEXT.R15 240
|
---|
1381 | # CONTEXT.Rip 248
|
---|
1382 | # CONTEXT.Xmm6 512
|
---|
1383 | # sizeof(CONTEXT) 1232
|
---|
1384 | # DISPATCHER_CONTEXT.ControlPc 0
|
---|
1385 | # DISPATCHER_CONTEXT.ImageBase 8
|
---|
1386 | # DISPATCHER_CONTEXT.FunctionEntry 16
|
---|
1387 | # DISPATCHER_CONTEXT.EstablisherFrame 24
|
---|
1388 | # DISPATCHER_CONTEXT.TargetIp 32
|
---|
1389 | # DISPATCHER_CONTEXT.ContextRecord 40
|
---|
1390 | # DISPATCHER_CONTEXT.LanguageHandler 48
|
---|
1391 | # DISPATCHER_CONTEXT.HandlerData 56
|
---|
1392 | # UNW_FLAG_NHANDLER 0
|
---|
1393 | # ExceptionContinueSearch 1
|
---|
1394 | #
|
---|
1395 | # In order to tie the handler to the function one has to compose
|
---|
1396 | # couple of structures: one for .xdata segment and one for .pdata.
|
---|
1397 | #
|
---|
1398 | # UNWIND_INFO structure for .xdata segment would be
|
---|
1399 | #
|
---|
1400 | # function_unwind_info:
|
---|
1401 | # .byte 9,0,0,0
|
---|
1402 | # .rva handler
|
---|
1403 | #
|
---|
1404 | # This structure designates exception handler for a function with
|
---|
1405 | # zero-length prologue, no stack frame or frame register.
|
---|
1406 | #
|
---|
1407 | # To facilitate composing of .pdata structures, auto-generated "gear"
|
---|
1408 | # prologue copies rsp value to rax and denotes next instruction with
|
---|
1409 | # .LSEH_begin_{function_name} label. This essentially defines the SEH
|
---|
1410 | # styling rule mentioned in the beginning. Position of this label is
|
---|
1411 | # chosen in such manner that possible exceptions raised in the "gear"
|
---|
1412 | # prologue would be accounted to caller and unwound from latter's frame.
|
---|
1413 | # End of function is marked with respective .LSEH_end_{function_name}
|
---|
1414 | # label. To summarize, .pdata segment would contain
|
---|
1415 | #
|
---|
1416 | # .rva .LSEH_begin_function
|
---|
1417 | # .rva .LSEH_end_function
|
---|
1418 | # .rva function_unwind_info
|
---|
1419 | #
|
---|
1420 | # Reference to function_unwind_info from .xdata segment is the anchor.
|
---|
1421 | # In case you wonder why references are 32-bit .rvas and not 64-bit
|
---|
1422 | # .quads. References put into these two segments are required to be
|
---|
1423 | # *relative* to the base address of the current binary module, a.k.a.
|
---|
1424 | # image base. No Win64 module, be it .exe or .dll, can be larger than
|
---|
1425 | # 2GB and thus such relative references can be and are accommodated in
|
---|
1426 | # 32 bits.
|
---|
1427 | #
|
---|
1428 | # Having reviewed the example function code, one can argue that "movq
|
---|
1429 | # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
|
---|
1430 | # rax would contain an undefined value. If this "offends" you, use
|
---|
1431 | # another register and refrain from modifying rax till magic_point is
|
---|
1432 | # reached, i.e. as if it was a non-volatile register. If more registers
|
---|
1433 | # are required prior [variable] frame setup is completed, note that
|
---|
1434 | # nobody says that you can have only one "magic point." You can
|
---|
1435 | # "liberate" non-volatile registers by denoting last stack off-load
|
---|
1436 | # instruction and reflecting it in finer grade unwind logic in handler.
|
---|
1437 | # After all, isn't it why it's called *language-specific* handler...
|
---|
1438 | #
|
---|
1439 | # SE handlers are also involved in unwinding stack when executable is
|
---|
1440 | # profiled or debugged. Profiling implies additional limitations that
|
---|
1441 | # are too subtle to discuss here. For now it's sufficient to say that
|
---|
1442 | # in order to simplify handlers one should either a) offload original
|
---|
1443 | # %rsp to stack (like discussed above); or b) if you have a register to
|
---|
1444 | # spare for frame pointer, choose volatile one.
|
---|
1445 | #
|
---|
1446 | # (*) Note that we're talking about run-time, not debug-time. Lack of
|
---|
1447 | # unwind information makes debugging hard on both Windows and
|
---|
1448 | # Unix. "Unlike" refers to the fact that on Unix signal handler
|
---|
1449 | # will always be invoked, core dumped and appropriate exit code
|
---|
1450 | # returned to parent (for user notification).
|
---|