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