]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/perlasm/x86_64-xlate.pl
aesni-x86_64.pl: minor CTR performance improvement.
[thirdparty/openssl.git] / crypto / perlasm / x86_64-xlate.pl
CommitLineData
1cfd258e
AP
1#!/usr/bin/env perl
2
80aa9cc9 3# Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
1cfd258e
AP
4#
5# Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
6# format is way easier to parse. Because it's simpler to "gear" from
7# Unix ABI to Windows one [see cross-reference "card" at the end of
8# file]. Because Linux targets were available first...
9#
10# In addition the script also "distills" code suitable for GNU
11# assembler, so that it can be compiled with more rigid assemblers,
12# such as Solaris /usr/ccs/bin/as.
13#
14# This translator is not designed to convert *arbitrary* assembler
15# code from AT&T format to MASM one. It's designed to convert just
16# enough to provide for dual-ABI OpenSSL modules development...
17# There *are* limitations and you might have to modify your assembler
18# code or this script to achieve the desired result...
19#
20# Currently recognized limitations:
21#
22# - can't use multiple ops per line;
1cfd258e
AP
23#
24# Dual-ABI styling rules.
25#
80aa9cc9
AP
26# 1. Adhere to Unix register and stack layout [see cross-reference
27# ABI "card" at the end for explanation].
1cfd258e
AP
28# 2. Forget about "red zone," stick to more traditional blended
29# stack frame allocation. If volatile storage is actually required
30# that is. If not, just leave the stack as is.
31# 3. Functions tagged with ".type name,@function" get crafted with
4b450519 32# unified Win64 prologue and epilogue automatically. If you want
1cfd258e 33# to take care of ABI differences yourself, tag functions as
4b450519
AP
34# ".type name,@abi-omnipotent" instead.
35# 4. To optimize the Win64 prologue you can specify number of input
36# arguments as ".type name,@function,N." Keep in mind that if N is
37# larger than 6, then you *have to* write "abi-omnipotent" code,
38# because >6 cases can't be addressed with unified prologue.
39# 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
40# (sorry about latter).
41# 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
42# required to identify the spots, where to inject Win64 epilogue!
43# But on the pros, it's then prefixed with rep automatically:-)
85253772
AP
44# 7. Stick to explicit ip-relative addressing. If you have to use
45# GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
46# Both are recognized and translated to proper Win64 addressing
47# modes. To support legacy code a synthetic directive, .picmeup,
48# is implemented. It puts address of the *next* instruction into
49# target register, e.g.:
5d0d60e2 50#
5d0d60e2
AP
51# .picmeup %rax
52# lea .Label-.(%rax),%rax
80aa9cc9
AP
53#
54# 8. In order to provide for structured exception handling unified
55# Win64 prologue copies %rsp value to %rax. For further details
56# see SEH paragraph at the end.
85253772 57# 9. .init segment is allowed to contain calls to functions only.
f9a152bd
AP
58# a. If function accepts more than 4 arguments *and* >4th argument
59# is declared as non 64-bit value, do clear its upper part.
80aa9cc9 60\f
85253772
AP
61my $flavour = shift;
62my $output = shift;
63if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
932cc129 64
b2ae61ec
AP
65open STDOUT,">$output" || die "can't open $output: $!"
66 if (defined($output));
1cfd258e 67
85253772
AP
68my $gas=1; $gas=0 if ($output =~ /\.asm$/);
69my $elf=1; $elf=0 if (!$gas);
70my $win64=0;
71my $prefix="";
93c4ba07 72my $decor=".L";
a23e3dbe 73
8ab9025e 74my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
9b634c9b 75my $masm=0;
a23e3dbe
AP
76my $PTR=" PTR";
77
9b634c9b 78my $nasmref=2.03;
a23e3dbe
AP
79my $nasm=0;
80
f9a152bd
AP
81if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
82 $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
83 chomp($prefix);
84 }
93c4ba07
AP
85elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
86elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
87elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
85253772 88elsif (!$gas)
9b634c9b
AP
89{ if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
90 { $nasm = $1 + $2*0.01; $PTR=""; }
a23e3dbe
AP
91 elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
92 { $masm = $1 + $2*2**-16 + $4*2**-32; }
9b634c9b 93 die "no assembler found on %PATH" if (!($nasm || $masm));
85253772
AP
94 $win64=1;
95 $elf=0;
93c4ba07 96 $decor="\$L\$";
a23e3dbe 97}
1cfd258e
AP
98
99my $current_segment;
100my $current_function;
9b634c9b 101my %globals;
1cfd258e
AP
102
103{ package opcode; # pick up opcodes
104 sub re {
105 my $self = shift; # single instance in enough...
106 local *line = shift;
107 undef $ret;
108
932cc129 109 if ($line =~ /^([a-z][a-z0-9]*)/i) {
1cfd258e
AP
110 $self->{op} = $1;
111 $ret = $self;
112 $line = substr($line,@+[0]); $line =~ s/^\s+//;
113
114 undef $self->{sz};
272ba870 115 if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
1cfd258e 116 $self->{op} = $1;
272ba870 117 $self->{sz} = $2;
9b634c9b 118 } elsif ($self->{op} =~ /call|jmp/) {
fead2539 119 $self->{sz} = "";
a9e790b9 120 } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
fead2539 121 $self->{sz} = "";
0a9a692e
AP
122 } elsif ($self->{op} =~ /^v/) { # VEX
123 $self->{sz} = "";
c7b903e0
AP
124 } elsif ($self->{op} =~ /movq/ && $line =~ /%xmm/) {
125 $self->{sz} = "";
a078befc 126 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
1cfd258e
AP
127 $self->{op} = $1;
128 $self->{sz} = $2;
129 }
130 }
131 $ret;
132 }
133 sub size {
134 my $self = shift;
135 my $sz = shift;
136 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
137 $self->{sz};
138 }
139 sub out {
140 my $self = shift;
85253772 141 if ($gas) {
932cc129 142 if ($self->{op} eq "movz") { # movz is pain...
1cfd258e 143 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
932cc129
AP
144 } elsif ($self->{op} =~ /^set/) {
145 "$self->{op}";
1cfd258e 146 } elsif ($self->{op} eq "ret") {
85253772
AP
147 my $epilogue = "";
148 if ($win64 && $current_function->{abi} eq "svr4") {
149 $epilogue = "movq 8(%rsp),%rdi\n\t" .
150 "movq 16(%rsp),%rsi\n\t";
151 }
152 $epilogue . ".byte 0xf3,0xc3";
153 } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
154 ".p2align\t3\n\t.quad";
1cfd258e
AP
155 } else {
156 "$self->{op}$self->{sz}";
157 }
158 } else {
a078befc 159 $self->{op} =~ s/^movz/movzx/;
1cfd258e
AP
160 if ($self->{op} eq "ret") {
161 $self->{op} = "";
85253772 162 if ($win64 && $current_function->{abi} eq "svr4") {
a23e3dbe
AP
163 $self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t".
164 "mov rsi,QWORD${PTR}[16+rsp]\n\t";
1cfd258e
AP
165 }
166 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
85253772 167 } elsif ($self->{op} =~ /^(pop|push)f/) {
80aa9cc9 168 $self->{op} .= $self->{sz};
85253772 169 } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
d6522548 170 $self->{op} = "\tDQ";
85253772 171 }
1cfd258e
AP
172 $self->{op};
173 }
174 }
85253772
AP
175 sub mnemonic {
176 my $self=shift;
177 my $op=shift;
178 $self->{op}=$op if (defined($op));
179 $self->{op};
180 }
1cfd258e
AP
181}
182{ package const; # pick up constants, which start with $
183 sub re {
184 my $self = shift; # single instance in enough...
185 local *line = shift;
186 undef $ret;
187
188 if ($line =~ /^\$([^,]+)/) {
189 $self->{value} = $1;
190 $ret = $self;
191 $line = substr($line,@+[0]); $line =~ s/^\s+//;
192 }
193 $ret;
194 }
195 sub out {
196 my $self = shift;
5d0d60e2 197
85253772 198 if ($gas) {
e84b663a
AP
199 # Solaris /usr/ccs/bin/as can't handle multiplications
200 # in $self->{value}
f9a152bd 201 $self->{value} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
e84b663a 202 $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
5d0d60e2
AP
203 sprintf "\$%s",$self->{value};
204 } else {
e8169520
AP
205 $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig;
206 $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
5d0d60e2
AP
207 sprintf "%s",$self->{value};
208 }
1cfd258e
AP
209 }
210}
211{ package ea; # pick up effective addresses: expr(%reg,%reg,scale)
212 sub re {
213 my $self = shift; # single instance in enough...
214 local *line = shift;
215 undef $ret;
216
9b634c9b
AP
217 # optional * ---vvv--- appears in indirect jmp/call
218 if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) {
219 $self->{asterisk} = $1;
220 $self->{label} = $2;
221 ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
1cfd258e
AP
222 $self->{scale} = 1 if (!defined($self->{scale}));
223 $ret = $self;
224 $line = substr($line,@+[0]); $line =~ s/^\s+//;
225
85253772
AP
226 if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
227 die if (opcode->mnemonic() ne "mov");
228 opcode->mnemonic("lea");
229 }
1cfd258e
AP
230 $self->{base} =~ s/^%//;
231 $self->{index} =~ s/^%// if (defined($self->{index}));
232 }
233 $ret;
234 }
235 sub size {}
236 sub out {
237 my $self = shift;
238 my $sz = shift;
239
85253772 240 $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
93c4ba07 241 $self->{label} =~ s/\.L/$decor/g;
85253772 242
afbe674e
AP
243 # Silently convert all EAs to 64-bit. This is required for
244 # elder GNU assembler and results in more compact code,
245 # *but* most importantly AES module depends on this feature!
246 $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
247 $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
248
6fa4c7c4
AP
249 # Solaris /usr/ccs/bin/as can't handle multiplications
250 # in $self->{label}, new gas requires sign extension...
251 use integer;
252 $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
253 $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
254 $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg;
255
85253772 256 if ($gas) {
93c4ba07 257 $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
4b450519 258
1cfd258e 259 if (defined($self->{index})) {
b5c6aab5
AP
260 sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk},
261 $self->{label},
262 $self->{base}?"%$self->{base}":"",
1cfd258e 263 $self->{index},$self->{scale};
5d0d60e2 264 } else {
9b634c9b 265 sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base};
1cfd258e
AP
266 }
267 } else {
046ea308 268 %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR",
a9e790b9 269 q=>"QWORD$PTR",o=>"OWORD$PTR",x=>"XMMWORD$PTR" );
1cfd258e 270
70cf3095 271 $self->{label} =~ s/\./\$/g;
f9a152bd 272 $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
70cf3095 273 $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
94c64f9a 274 $sz="q" if ($self->{asterisk} || opcode->mnemonic() eq "movq");
4010b341 275 $sz="l" if (opcode->mnemonic() eq "movd");
70cf3095 276
1cfd258e 277 if (defined($self->{index})) {
b5c6aab5 278 sprintf "%s[%s%s*%d%s]",$szmap{$sz},
a23e3dbe 279 $self->{label}?"$self->{label}+":"",
1cfd258e 280 $self->{index},$self->{scale},
b5c6aab5 281 $self->{base}?"+$self->{base}":"";
932cc129 282 } elsif ($self->{base} eq "rip") {
a23e3dbe 283 sprintf "%s[%s]",$szmap{$sz},$self->{label};
5d0d60e2 284 } else {
a23e3dbe
AP
285 sprintf "%s[%s%s]",$szmap{$sz},
286 $self->{label}?"$self->{label}+":"",
287 $self->{base};
1cfd258e
AP
288 }
289 }
290 }
291}
292{ package register; # pick up registers, which start with %.
293 sub re {
294 my $class = shift; # muliple instances...
295 my $self = {};
296 local *line = shift;
297 undef $ret;
298
9b634c9b
AP
299 # optional * ---vvv--- appears in indirect jmp/call
300 if ($line =~ /^(\*?)%(\w+)/) {
1cfd258e 301 bless $self,$class;
9b634c9b
AP
302 $self->{asterisk} = $1;
303 $self->{value} = $2;
1cfd258e
AP
304 $ret = $self;
305 $line = substr($line,@+[0]); $line =~ s/^\s+//;
306 }
307 $ret;
308 }
309 sub size {
310 my $self = shift;
311 undef $ret;
312
313 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
314 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
315 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
316 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
317 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
318 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
319 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
320 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
321
322 $ret;
323 }
324 sub out {
325 my $self = shift;
85253772 326 if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; }
9b634c9b 327 else { $self->{value}; }
1cfd258e
AP
328 }
329}
330{ package label; # pick up labels, which end with :
331 sub re {
332 my $self = shift; # single instance is enough...
333 local *line = shift;
334 undef $ret;
335
85253772 336 if ($line =~ /(^[\.\w]+)\:/) {
1cfd258e
AP
337 $self->{value} = $1;
338 $ret = $self;
339 $line = substr($line,@+[0]); $line =~ s/^\s+//;
340
93c4ba07 341 $self->{value} =~ s/^\.L/$decor/;
1cfd258e
AP
342 }
343 $ret;
344 }
345 sub out {
346 my $self = shift;
347
85253772
AP
348 if ($gas) {
349 my $func = ($globals{$self->{value}} or $self->{value}) . ":";
350 if ($win64 &&
351 $current_function->{name} eq $self->{value} &&
352 $current_function->{abi} eq "svr4") {
353 $func .= "\n";
354 $func .= " movq %rdi,8(%rsp)\n";
355 $func .= " movq %rsi,16(%rsp)\n";
356 $func .= " movq %rsp,%rax\n";
93c4ba07 357 $func .= "${decor}SEH_begin_$current_function->{name}:\n";
85253772
AP
358 my $narg = $current_function->{narg};
359 $narg=6 if (!defined($narg));
360 $func .= " movq %rcx,%rdi\n" if ($narg>0);
361 $func .= " movq %rdx,%rsi\n" if ($narg>1);
362 $func .= " movq %r8,%rdx\n" if ($narg>2);
363 $func .= " movq %r9,%rcx\n" if ($narg>3);
364 $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
365 $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
366 }
367 $func;
368 } elsif ($self->{value} ne "$current_function->{name}") {
9b634c9b 369 $self->{value} .= ":" if ($masm && $ret!~m/^\$/);
85253772
AP
370 $self->{value} . ":";
371 } elsif ($win64 && $current_function->{abi} eq "svr4") {
9b634c9b
AP
372 my $func = "$current_function->{name}" .
373 ($nasm ? ":" : "\tPROC $current_function->{scope}") .
374 "\n";
375 $func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n";
376 $func .= " mov QWORD${PTR}[16+rsp],rsi\n";
80aa9cc9 377 $func .= " mov rax,rsp\n";
93c4ba07 378 $func .= "${decor}SEH_begin_$current_function->{name}:";
80aa9cc9
AP
379 $func .= ":" if ($masm);
380 $func .= "\n";
1cfd258e
AP
381 my $narg = $current_function->{narg};
382 $narg=6 if (!defined($narg));
383 $func .= " mov rdi,rcx\n" if ($narg>0);
384 $func .= " mov rsi,rdx\n" if ($narg>1);
385 $func .= " mov rdx,r8\n" if ($narg>2);
386 $func .= " mov rcx,r9\n" if ($narg>3);
a23e3dbe
AP
387 $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4);
388 $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5);
1cfd258e
AP
389 $func .= "\n";
390 } else {
9b634c9b
AP
391 "$current_function->{name}".
392 ($nasm ? ":" : "\tPROC $current_function->{scope}");
1cfd258e
AP
393 }
394 }
395}
396{ package expr; # pick up expressioins
397 sub re {
398 my $self = shift; # single instance is enough...
399 local *line = shift;
400 undef $ret;
401
402 if ($line =~ /(^[^,]+)/) {
403 $self->{value} = $1;
404 $ret = $self;
405 $line = substr($line,@+[0]); $line =~ s/^\s+//;
406
85253772
AP
407 $self->{value} =~ s/\@PLT// if (!$elf);
408 $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
93c4ba07 409 $self->{value} =~ s/\.L/$decor/g;
1cfd258e
AP
410 }
411 $ret;
412 }
413 sub out {
414 my $self = shift;
9b634c9b
AP
415 if ($nasm && opcode->mnemonic()=~m/^j/) {
416 "NEAR ".$self->{value};
417 } else {
418 $self->{value};
419 }
1cfd258e
AP
420 }
421}
422{ package directive; # pick up directives, which start with .
423 sub re {
424 my $self = shift; # single instance is enough...
425 local *line = shift;
426 undef $ret;
427 my $dir;
5d0d60e2
AP
428 my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
429 ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
430 "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
431 "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
432 "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
433 "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
434 "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
435 "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
436 "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
1cfd258e
AP
437
438 if ($line =~ /^\s*(\.\w+)/) {
85253772
AP
439 $dir = $1;
440 $ret = $self;
441 undef $self->{value};
442 $line = substr($line,@+[0]); $line =~ s/^\s+//;
443
444 SWITCH: for ($dir) {
445 /\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) {
446 $dir="\t.long";
447 $line=sprintf "0x%x,0x90000000",$opcode{$1};
448 }
449 last;
450 };
451 /\.global|\.globl|\.extern/
452 && do { $globals{$line} = $prefix . $line;
453 $line = $globals{$line} if ($prefix);
454 last;
455 };
456 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
457 if ($type eq "\@function") {
458 undef $current_function;
459 $current_function->{name} = $sym;
460 $current_function->{abi} = "svr4";
461 $current_function->{narg} = $narg;
462 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
463 } elsif ($type eq "\@abi-omnipotent") {
464 undef $current_function;
465 $current_function->{name} = $sym;
466 $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
467 }
468 $line =~ s/\@abi\-omnipotent/\@function/;
469 $line =~ s/\@function.*/\@function/;
470 last;
471 };
472 /\.asciz/ && do { if ($line =~ /^"(.*)"$/) {
473 $dir = ".byte";
474 $line = join(",",unpack("C*",$1),0);
475 }
476 last;
477 };
93c4ba07
AP
478 /\.rva|\.long|\.quad/
479 && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
480 $line =~ s/\.L/$decor/g;
481 last;
482 };
85253772
AP
483 }
484
485 if ($gas) {
486 $self->{value} = $dir . "\t" . $line;
487
488 if ($dir =~ /\.extern/) {
932cc129 489 $self->{value} = ""; # swallow extern
85253772
AP
490 } elsif (!$elf && $dir =~ /\.type/) {
491 $self->{value} = "";
492 $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
493 (defined($globals{$1})?".scl 2;":".scl 3;") .
494 "\t.type 32;\t.endef"
495 if ($win64 && $line =~ /([^,]+),\@function/);
496 } elsif (!$elf && $dir =~ /\.size/) {
497 $self->{value} = "";
498 if (defined($current_function)) {
93c4ba07 499 $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
85253772
AP
500 if ($win64 && $current_function->{abi} eq "svr4");
501 undef $current_function;
502 }
503 } elsif (!$elf && $dir =~ /\.align/) {
504 $self->{value} = ".p2align\t" . (log($line)/log(2));
505 } elsif ($dir eq ".section") {
506 $current_segment=$line;
507 if (!$elf && $current_segment eq ".init") {
508 if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
509 elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
510 }
511 } elsif ($dir =~ /\.(text|data)/) {
512 $current_segment=".$1";
ddc20d4d
AP
513 } elsif ($dir =~ /\.hidden/) {
514 if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$line"; }
515 elsif ($flavour eq "mingw64") { $self->{value} = ""; }
516 } elsif ($dir =~ /\.comm/) {
517 $self->{value} = "$dir\t$prefix$line";
ff6f9f96 518 $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
5d0d60e2 519 }
1cfd258e
AP
520 $line = "";
521 return $self;
522 }
523
85253772 524 # non-gas case or nasm/masm
1cfd258e 525 SWITCH: for ($dir) {
9b634c9b 526 /\.text/ && do { my $v=undef;
a23e3dbe 527 if ($nasm) {
9b634c9b 528 $v="section .text code align=64\n";
a23e3dbe
AP
529 } else {
530 $v="$current_segment\tENDS\n" if ($current_segment);
9b634c9b 531 $current_segment = ".text\$";
a23e3dbe
AP
532 $v.="$current_segment\tSEGMENT ";
533 $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE";
534 $v.=" 'CODE'";
535 }
1cfd258e
AP
536 $self->{value} = $v;
537 last;
538 };
9b634c9b
AP
539 /\.data/ && do { my $v=undef;
540 if ($nasm) {
541 $v="section .data data align=8\n";
542 } else {
543 $v="$current_segment\tENDS\n" if ($current_segment);
544 $current_segment = "_DATA";
545 $v.="$current_segment\tSEGMENT";
546 }
547 $self->{value} = $v;
548 last;
549 };
550 /\.section/ && do { my $v=undef;
85253772
AP
551 $line =~ s/([^,]*).*/$1/;
552 $line = ".CRT\$XCU" if ($line eq ".init");
9b634c9b
AP
553 if ($nasm) {
554 $v="section $line";
555 if ($line=~/\.([px])data/) {
556 $v.=" rdata align=";
557 $v.=$1 eq "p"? 4 : 8;
d6522548
AP
558 } elsif ($line=~/\.CRT\$/i) {
559 $v.=" rdata align=8";
9b634c9b
AP
560 }
561 } else {
562 $v="$current_segment\tENDS\n" if ($current_segment);
563 $v.="$line\tSEGMENT";
564 if ($line=~/\.([px])data/) {
565 $v.=" READONLY";
566 $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
d6522548 567 } elsif ($line=~/\.CRT\$/i) {
e6903980
AP
568 $v.=" READONLY ";
569 $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
9b634c9b
AP
570 }
571 }
572 $current_segment = $line;
573 $self->{value} = $v;
574 last;
575 };
a23e3dbe 576 /\.extern/ && do { $self->{value} = "EXTERN\t".$line;
9b634c9b
AP
577 $self->{value} .= ":NEAR" if ($masm);
578 last;
579 };
80aa9cc9
AP
580 /\.globl|.global/
581 && do { $self->{value} = $masm?"PUBLIC":"global";
582 $self->{value} .= "\t".$line;
1cfd258e
AP
583 last;
584 };
5d0d60e2 585 /\.size/ && do { if (defined($current_function)) {
8fe8bae1
AP
586 undef $self->{value};
587 if ($current_function->{abi} eq "svr4") {
93c4ba07 588 $self->{value}="${decor}SEH_end_$current_function->{name}:";
8fe8bae1
AP
589 $self->{value}.=":\n" if($masm);
590 }
4010b341 591 $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
1cfd258e
AP
592 undef $current_function;
593 }
594 last;
595 };
596 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
85253772 597 /\.(value|long|rva|quad)/
80aa9cc9 598 && do { my $sz = substr($1,0,1);
fead2539 599 my @arr = split(/,\s*/,$line);
1cfd258e 600 my $last = pop(@arr);
a23e3dbe 601 my $conv = sub { my $var=shift;
065c5d63 602 $var=~s/^(0b[0-1]+)/oct($1)/eig;
fead2539 603 $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
85253772 604 if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
80aa9cc9 605 { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
9b634c9b 606 $var;
a23e3dbe 607 };
1cfd258e 608
85253772 609 $sz =~ tr/bvlrq/BWDDQ/;
1cfd258e 610 $self->{value} = "\tD$sz\t";
a23e3dbe
AP
611 for (@arr) { $self->{value} .= &$conv($_).","; }
612 $self->{value} .= &$conv($last);
1cfd258e
AP
613 last;
614 };
fead2539 615 /\.byte/ && do { my @str=split(/,\s*/,$line);
e8169520 616 map(s/(0b[0-1]+)/oct($1)/eig,@str);
bf785c98 617 map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
85253772 618 while ($#str>15) {
55eab3b7 619 $self->{value}.="DB\t"
85253772
AP
620 .join(",",@str[0..15])."\n";
621 foreach (0..15) { shift @str; }
d68ff710 622 }
85253772
AP
623 $self->{value}.="DB\t"
624 .join(",",@str) if (@str);
d68ff710
AP
625 last;
626 };
ddc20d4d
AP
627 /\.comm/ && do { my @str=split(/,\s*/,$line);
628 my $v=undef;
629 if ($nasm) {
301799b8 630 $v.="common $prefix@str[0] @str[1]";
ddc20d4d
AP
631 } else {
632 $v="$current_segment\tENDS\n" if ($current_segment);
94c64f9a 633 $current_segment = "_DATA";
ddc20d4d
AP
634 $v.="$current_segment\tSEGMENT\n";
635 $v.="COMM @str[0]:DWORD:".@str[1]/4;
636 }
637 $self->{value} = $v;
638 last;
639 };
1cfd258e
AP
640 }
641 $line = "";
642 }
643
644 $ret;
645 }
646 sub out {
647 my $self = shift;
648 $self->{value};
649 }
650}
651
fead2539
AP
652sub rex {
653 local *opcode=shift;
ddc20d4d
AP
654 my ($dst,$src,$rex)=@_;
655
656 $rex|=0x04 if($dst>=8);
657 $rex|=0x01 if($src>=8);
658 push @opcode,($rex|0x40) if ($rex);
fead2539
AP
659}
660
a9e790b9 661# older gas and ml64 don't handle SSE>2 instructions
fead2539
AP
662my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
663 "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
664
c7b903e0
AP
665my $movq = sub { # elderly gas can't handle inter-register movq
666 my $arg = shift;
667 my @opcode=(0x66);
a87ff751 668 if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
c7b903e0
AP
669 my ($src,$dst)=($1,$2);
670 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
671 rex(\@opcode,$src,$dst,0x8);
672 push @opcode,0x0f,0x7e;
673 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
674 @opcode;
a87ff751 675 } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
c7b903e0
AP
676 my ($src,$dst)=($2,$1);
677 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
678 rex(\@opcode,$src,$dst,0x8);
679 push @opcode,0x0f,0x6e;
680 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
681 @opcode;
682 } else {
683 ();
684 }
685};
686
fead2539 687my $pextrd = sub {
a87ff751 688 if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
fead2539
AP
689 my @opcode=(0x66);
690 $imm=$1;
691 $src=$2;
a9e790b9 692 $dst=$3;
fead2539
AP
693 if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
694 elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
695 rex(\@opcode,$src,$dst);
696 push @opcode,0x0f,0x3a,0x16;
697 push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
698 push @opcode,$imm;
a9e790b9 699 @opcode;
fead2539 700 } else {
a9e790b9 701 ();
fead2539 702 }
a9e790b9 703};
fead2539
AP
704
705my $pinsrd = sub {
a87ff751 706 if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
fead2539
AP
707 my @opcode=(0x66);
708 $imm=$1;
a9e790b9
AP
709 $src=$2;
710 $dst=$3;
b5c6aab5 711 if ($src =~ /%r([0-9]+)/) { $src = $1; }
fead2539
AP
712 elsif ($src =~ /%e/) { $src = $regrm{$src}; }
713 rex(\@opcode,$dst,$src);
714 push @opcode,0x0f,0x3a,0x22;
715 push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
716 push @opcode,$imm;
a9e790b9 717 @opcode;
fead2539 718 } else {
a9e790b9 719 ();
fead2539 720 }
a9e790b9 721};
fead2539
AP
722
723my $pshufb = sub {
a87ff751 724 if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
fead2539 725 my @opcode=(0x66);
a9e790b9 726 rex(\@opcode,$2,$1);
fead2539 727 push @opcode,0x0f,0x38,0x00;
a9e790b9
AP
728 push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
729 @opcode;
fead2539 730 } else {
a9e790b9 731 ();
fead2539 732 }
a9e790b9 733};
fead2539 734
b5c6aab5 735my $palignr = sub {
a87ff751 736 if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
b5c6aab5
AP
737 my @opcode=(0x66);
738 rex(\@opcode,$3,$2);
739 push @opcode,0x0f,0x3a,0x0f;
740 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
741 push @opcode,$1;
742 @opcode;
743 } else {
744 ();
745 }
746};
747
748my $pclmulqdq = sub {
749 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
750 my @opcode=(0x66);
751 rex(\@opcode,$3,$2);
752 push @opcode,0x0f,0x3a,0x44;
753 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
754 my $c=$1;
755 push @opcode,$c=~/^0/?oct($c):$c;
756 @opcode;
757 } else {
758 ();
759 }
760};
761
301799b8
AP
762my $rdrand = sub {
763 if (shift =~ /%[er](\w+)/) {
764 my @opcode=();
765 my $dst=$1;
766 if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
767 rex(\@opcode,0,$1,8);
768 push @opcode,0x0f,0xc7,0xf0|($dst&7);
769 @opcode;
770 } else {
771 ();
772 }
773};
774
f6ff1aa8
AP
775sub rxb {
776 local *opcode=shift;
777 my ($dst,$src1,$src2,$rxb)=@_;
778
779 $rxb|=0x7<<5;
780 $rxb&=~(0x04<<5) if($dst>=8);
781 $rxb&=~(0x01<<5) if($src1>=8);
782 $rxb&=~(0x02<<5) if($src2>=8);
783 push @opcode,$rxb;
784}
785
786my $vprotd = sub {
787 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
788 my @opcode=(0x8f);
789 rxb(\@opcode,$3,$2,-1,0x08);
790 push @opcode,0x78,0xc2;
791 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
792 my $c=$1;
793 push @opcode,$c=~/^0/?oct($c):$c;
794 @opcode;
795 } else {
796 ();
797 }
798};
799
800my $vprotq = sub {
801 if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
802 my @opcode=(0x8f);
803 rxb(\@opcode,$3,$2,-1,0x08);
804 push @opcode,0x78,0xc3;
805 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
806 my $c=$1;
807 push @opcode,$c=~/^0/?oct($c):$c;
808 @opcode;
809 } else {
810 ();
811 }
812};
813
9b634c9b
AP
814if ($nasm) {
815 print <<___;
816default rel
a9e790b9 817%define XMMWORD
9b634c9b
AP
818___
819} elsif ($masm) {
820 print <<___;
821OPTION DOTNAME
822___
823}
1cfd258e
AP
824while($line=<>) {
825
826 chomp($line);
827
4b450519
AP
828 $line =~ s|[#!].*$||; # get rid of asm-style comments...
829 $line =~ s|/\*.*\*/||; # ... and C-style comments...
830 $line =~ s|^\s+||; # ... and skip white spaces in beginning
1cfd258e
AP
831
832 undef $label;
833 undef $opcode;
4db48824 834 undef @args;
1cfd258e
AP
835
836 if ($label=label->re(\$line)) { print $label->out(); }
837
838 if (directive->re(\$line)) {
839 printf "%s",directive->out();
a9e790b9
AP
840 } elsif ($opcode=opcode->re(\$line)) {
841 my $asm = eval("\$".$opcode->mnemonic());
842 undef @bytes;
843
844 if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) {
845 print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
846 next;
847 }
848
849 ARGUMENT: while (1) {
4db48824 850 my $arg;
1cfd258e 851
4db48824
AP
852 if ($arg=register->re(\$line)) { opcode->size($arg->size()); }
853 elsif ($arg=const->re(\$line)) { }
854 elsif ($arg=ea->re(\$line)) { }
855 elsif ($arg=expr->re(\$line)) { }
856 else { last ARGUMENT; }
1cfd258e 857
4db48824 858 push @args,$arg;
1cfd258e 859
4db48824 860 last ARGUMENT if ($line !~ /^,/);
1cfd258e 861
4db48824 862 $line =~ s/^,\s*//;
1cfd258e
AP
863 } # ARGUMENT:
864
4db48824
AP
865 if ($#args>=0) {
866 my $insn;
a9e790b9
AP
867 my $sz=opcode->size();
868
85253772 869 if ($gas) {
4db48824 870 $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
fead2539 871 @args = map($_->out($sz),@args);
a9e790b9 872 printf "\t%s\t%s",$insn,join(",",@args);
5d0d60e2 873 } else {
4db48824 874 $insn = $opcode->out();
046ea308
AP
875 foreach (@args) {
876 my $arg = $_->out();
a9e790b9
AP
877 # $insn.=$sz compensates for movq, pinsrw, ...
878 if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
879 if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
046ea308 880 }
4db48824 881 @args = reverse(@args);
a23e3dbe 882 undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
fead2539 883 printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
1cfd258e 884 }
1cfd258e
AP
885 } else {
886 printf "\t%s",$opcode->out();
887 }
888 }
889
890 print $line,"\n";
891}
892
85253772
AP
893print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
894print "END\n" if ($masm);
1cfd258e
AP
895
896close STDOUT;
897
80aa9cc9 898\f#################################################
1cfd258e
AP
899# Cross-reference x86_64 ABI "card"
900#
901# Unix Win64
902# %rax * *
903# %rbx - -
904# %rcx #4 #1
905# %rdx #3 #2
906# %rsi #2 -
907# %rdi #1 -
908# %rbp - -
909# %rsp - -
910# %r8 #5 #3
911# %r9 #6 #4
912# %r10 * *
913# %r11 * *
914# %r12 - -
915# %r13 - -
916# %r14 - -
917# %r15 - -
918#
919# (*) volatile register
920# (-) preserved by callee
921# (#) Nth argument, volatile
922#
923# In Unix terms top of stack is argument transfer area for arguments
924# which could not be accomodated in registers. Or in other words 7th
925# [integer] argument resides at 8(%rsp) upon function entry point.
926# 128 bytes above %rsp constitute a "red zone" which is not touched
927# by signal handlers and can be used as temporal storage without
928# allocating a frame.
929#
930# In Win64 terms N*8 bytes on top of stack is argument transfer area,
931# which belongs to/can be overwritten by callee. N is the number of
932# arguments passed to callee, *but* not less than 4! This means that
933# upon function entry point 5th argument resides at 40(%rsp), as well
934# as that 32 bytes from 8(%rsp) can always be used as temporal
d256b957
AP
935# storage [without allocating a frame]. One can actually argue that
936# one can assume a "red zone" above stack pointer under Win64 as well.
6a3a7f30
AP
937# Point is that at apparently no occasion Windows kernel would alter
938# the area above user stack pointer in true asynchronous manner...
1cfd258e
AP
939#
940# All the above means that if assembler programmer adheres to Unix
941# register and stack layout, but disregards the "red zone" existense,
942# it's possible to use following prologue and epilogue to "gear" from
943# Unix to Win64 ABI in leaf functions with not more than 6 arguments.
944#
945# omnipotent_function:
946# ifdef WIN64
947# movq %rdi,8(%rsp)
948# movq %rsi,16(%rsp)
949# movq %rcx,%rdi ; if 1st argument is actually present
950# movq %rdx,%rsi ; if 2nd argument is actually ...
951# movq %r8,%rdx ; if 3rd argument is ...
952# movq %r9,%rcx ; if 4th argument ...
953# movq 40(%rsp),%r8 ; if 5th ...
954# movq 48(%rsp),%r9 ; if 6th ...
955# endif
956# ...
957# ifdef WIN64
958# movq 8(%rsp),%rdi
959# movq 16(%rsp),%rsi
960# endif
961# ret
4f469342 962#
80aa9cc9
AP
963\f#################################################
964# Win64 SEH, Structured Exception Handling.
965#
4f469342
AP
966# Unlike on Unix systems(*) lack of Win64 stack unwinding information
967# has undesired side-effect at run-time: if an exception is raised in
968# assembler subroutine such as those in question (basically we're
969# referring to segmentation violations caused by malformed input
970# parameters), the application is briskly terminated without invoking
971# any exception handlers, most notably without generating memory dump
972# or any user notification whatsoever. This poses a problem. It's
973# possible to address it by registering custom language-specific
974# handler that would restore processor context to the state at
975# subroutine entry point and return "exception is not handled, keep
976# unwinding" code. Writing such handler can be a challenge... But it's
977# doable, though requires certain coding convention. Consider following
978# snippet:
979#
80aa9cc9 980# .type function,@function
4f469342
AP
981# function:
982# movq %rsp,%rax # copy rsp to volatile register
983# pushq %r15 # save non-volatile registers
984# pushq %rbx
985# pushq %rbp
986# movq %rsp,%r11
987# subq %rdi,%r11 # prepare [variable] stack frame
988# andq $-64,%r11
989# movq %rax,0(%r11) # check for exceptions
990# movq %r11,%rsp # allocate [variable] stack frame
991# movq %rax,0(%rsp) # save original rsp value
992# magic_point:
993# ...
994# movq 0(%rsp),%rcx # pull original rsp value
995# movq -24(%rcx),%rbp # restore non-volatile registers
996# movq -16(%rcx),%rbx
997# movq -8(%rcx),%r15
998# movq %rcx,%rsp # restore original rsp
999# ret
80aa9cc9 1000# .size function,.-function
4f469342
AP
1001#
1002# The key is that up to magic_point copy of original rsp value remains
1003# in chosen volatile register and no non-volatile register, except for
1004# rsp, is modified. While past magic_point rsp remains constant till
1005# the very end of the function. In this case custom language-specific
1006# exception handler would look like this:
1007#
1008# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1009# CONTEXT *context,DISPATCHER_CONTEXT *disp)
80aa9cc9
AP
1010# { ULONG64 *rsp = (ULONG64 *)context->Rax;
1011# if (context->Rip >= magic_point)
4f469342
AP
1012# { rsp = ((ULONG64 **)context->Rsp)[0];
1013# context->Rbp = rsp[-3];
1014# context->Rbx = rsp[-2];
1015# context->R15 = rsp[-1];
1016# }
1017# context->Rsp = (ULONG64)rsp;
1018# context->Rdi = rsp[1];
1019# context->Rsi = rsp[2];
1020#
1021# memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
1022# RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
1023# dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
1024# &disp->HandlerData,&disp->EstablisherFrame,NULL);
1025# return ExceptionContinueSearch;
1026# }
1027#
1028# It's appropriate to implement this handler in assembler, directly in
1029# function's module. In order to do that one has to know members'
1030# offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
1031# values. Here they are:
1032#
1033# CONTEXT.Rax 120
1034# CONTEXT.Rcx 128
1035# CONTEXT.Rdx 136
1036# CONTEXT.Rbx 144
1037# CONTEXT.Rsp 152
1038# CONTEXT.Rbp 160
1039# CONTEXT.Rsi 168
1040# CONTEXT.Rdi 176
1041# CONTEXT.R8 184
1042# CONTEXT.R9 192
1043# CONTEXT.R10 200
1044# CONTEXT.R11 208
1045# CONTEXT.R12 216
1046# CONTEXT.R13 224
1047# CONTEXT.R14 232
1048# CONTEXT.R15 240
1049# CONTEXT.Rip 248
6dd9066e 1050# CONTEXT.Xmm6 512
4f469342
AP
1051# sizeof(CONTEXT) 1232
1052# DISPATCHER_CONTEXT.ControlPc 0
1053# DISPATCHER_CONTEXT.ImageBase 8
1054# DISPATCHER_CONTEXT.FunctionEntry 16
1055# DISPATCHER_CONTEXT.EstablisherFrame 24
1056# DISPATCHER_CONTEXT.TargetIp 32
1057# DISPATCHER_CONTEXT.ContextRecord 40
1058# DISPATCHER_CONTEXT.LanguageHandler 48
1059# DISPATCHER_CONTEXT.HandlerData 56
1060# UNW_FLAG_NHANDLER 0
1061# ExceptionContinueSearch 1
1062#
80aa9cc9
AP
1063# In order to tie the handler to the function one has to compose
1064# couple of structures: one for .xdata segment and one for .pdata.
1065#
4f469342 1066# UNWIND_INFO structure for .xdata segment would be
80aa9cc9
AP
1067#
1068# function_unwind_info:
1069# .byte 9,0,0,0
85253772 1070# .rva handler
80aa9cc9
AP
1071#
1072# This structure designates exception handler for a function with
1073# zero-length prologue, no stack frame or frame register.
1074#
1075# To facilitate composing of .pdata structures, auto-generated "gear"
1076# prologue copies rsp value to rax and denotes next instruction with
85253772 1077# .LSEH_begin_{function_name} label. This essentially defines the SEH
80aa9cc9
AP
1078# styling rule mentioned in the beginning. Position of this label is
1079# chosen in such manner that possible exceptions raised in the "gear"
1080# prologue would be accounted to caller and unwound from latter's frame.
85253772 1081# End of function is marked with respective .LSEH_end_{function_name}
80aa9cc9
AP
1082# label. To summarize, .pdata segment would contain
1083#
85253772
AP
1084# .rva .LSEH_begin_function
1085# .rva .LSEH_end_function
1086# .rva function_unwind_info
80aa9cc9
AP
1087#
1088# Reference to functon_unwind_info from .xdata segment is the anchor.
85253772 1089# In case you wonder why references are 32-bit .rvas and not 64-bit
80aa9cc9
AP
1090# .quads. References put into these two segments are required to be
1091# *relative* to the base address of the current binary module, a.k.a.
1092# image base. No Win64 module, be it .exe or .dll, can be larger than
1093# 2GB and thus such relative references can be and are accommodated in
1094# 32 bits.
1095#
1096# Having reviewed the example function code, one can argue that "movq
1097# %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
1098# rax would contain an undefined value. If this "offends" you, use
1099# another register and refrain from modifying rax till magic_point is
1100# reached, i.e. as if it was a non-volatile register. If more registers
1101# are required prior [variable] frame setup is completed, note that
1102# nobody says that you can have only one "magic point." You can
1103# "liberate" non-volatile registers by denoting last stack off-load
1104# instruction and reflecting it in finer grade unwind logic in handler.
1105# After all, isn't it why it's called *language-specific* handler...
1106#
1107# Attentive reader can notice that exceptions would be mishandled in
1108# auto-generated "gear" epilogue. Well, exception effectively can't
1109# occur there, because if memory area used by it was subject to
1110# segmentation violation, then it would be raised upon call to the
1111# function (and as already mentioned be accounted to caller, which is
1112# not a problem). If you're still not comfortable, then define tail
1113# "magic point" just prior ret instruction and have handler treat it...
4f469342
AP
1114#
1115# (*) Note that we're talking about run-time, not debug-time. Lack of
1116# unwind information makes debugging hard on both Windows and
1117# Unix. "Unlike" referes to the fact that on Unix signal handler
1118# will always be invoked, core dumped and appropriate exit code
1119# returned to parent (for user notification).