2016-04-20 02:10:43 +00:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
# Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
package x86masm;
|
|
|
|
|
|
|
|
*out=\@::out;
|
|
|
|
|
|
|
|
$::lbdecor="\$L"; # local label decoration
|
|
|
|
$nmdecor="_"; # external name decoration
|
|
|
|
|
|
|
|
$initseg="";
|
2008-07-22 08:44:31 +00:00
|
|
|
$segment="";
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
sub ::generic
|
|
|
|
{ my ($opcode,@arg)=@_;
|
|
|
|
|
|
|
|
# fix hexadecimal constants
|
2010-06-04 13:13:18 +00:00
|
|
|
for (@arg) { s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/oi; }
|
2007-12-18 09:18:49 +00:00
|
|
|
|
2012-03-29 18:09:36 +00:00
|
|
|
if ($opcode =~ /lea/ && @arg[1] =~ s/.*PTR\s+(\(.*\))$/OFFSET $1/) # no []
|
2011-12-15 22:20:05 +00:00
|
|
|
{ $opcode="mov"; }
|
2015-02-22 18:23:25 +00:00
|
|
|
elsif ($opcode !~ /mov[dq]$/)
|
2009-04-26 17:58:58 +00:00
|
|
|
{ # fix xmm references
|
2015-02-22 18:23:25 +00:00
|
|
|
$arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[-1]=~/\bxmm[0-7]\b/i);
|
|
|
|
$arg[-1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i);
|
2009-04-26 17:58:58 +00:00
|
|
|
}
|
2008-01-03 16:21:06 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
&::emit($opcode,@arg);
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
#
|
|
|
|
# opcodes not covered by ::generic above, mostly inconsistent namings...
|
|
|
|
#
|
|
|
|
sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
|
|
|
|
sub ::call_ptr { &::emit("call",@_); }
|
|
|
|
sub ::jmp_ptr { &::emit("jmp",@_); }
|
2011-04-18 20:18:03 +00:00
|
|
|
sub ::lock { &::data_byte(0xf0); }
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
sub get_mem
|
|
|
|
{ my($size,$addr,$reg1,$reg2,$idx)=@_;
|
|
|
|
my($post,$ret);
|
|
|
|
|
2012-11-17 19:04:15 +00:00
|
|
|
if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
$ret .= "$size PTR " if ($size ne "");
|
|
|
|
|
|
|
|
$addr =~ s/^\s+//;
|
|
|
|
# prepend global references with optional underscore
|
|
|
|
$addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
|
|
|
|
# put address arithmetic expression in parenthesis
|
|
|
|
$addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
|
|
|
|
|
|
|
|
if (($addr ne "") && ($addr ne 0))
|
|
|
|
{ if ($addr !~ /^-/) { $ret .= "$addr"; }
|
|
|
|
else { $post=$addr; }
|
|
|
|
}
|
|
|
|
$ret .= "[";
|
|
|
|
|
|
|
|
if ($reg2 ne "")
|
|
|
|
{ $idx!=0 or $idx=1;
|
|
|
|
$ret .= "$reg2*$idx";
|
|
|
|
$ret .= "+$reg1" if ($reg1 ne "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ $ret .= "$reg1"; }
|
|
|
|
|
|
|
|
$ret .= "$post]";
|
|
|
|
$ret =~ s/\+\]/]/; # in case $addr was the only argument
|
|
|
|
$ret =~ s/\[\s*\]//;
|
|
|
|
|
|
|
|
$ret;
|
|
|
|
}
|
|
|
|
sub ::BP { &get_mem("BYTE",@_); }
|
2010-06-04 13:13:18 +00:00
|
|
|
sub ::WP { &get_mem("WORD",@_); }
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::DWP { &get_mem("DWORD",@_); }
|
|
|
|
sub ::QWP { &get_mem("QWORD",@_); }
|
|
|
|
sub ::BC { "@_"; }
|
|
|
|
sub ::DWC { "@_"; }
|
|
|
|
|
|
|
|
sub ::file
|
|
|
|
{ my $tmp=<<___;
|
|
|
|
TITLE $_[0].asm
|
2008-07-15 13:16:42 +00:00
|
|
|
IF \@Version LT 800
|
|
|
|
ECHO MASM version 8.00 or later is strongly recommended.
|
|
|
|
ENDIF
|
2016-08-16 12:46:13 +00:00
|
|
|
.686
|
2007-12-18 09:18:49 +00:00
|
|
|
.MODEL FLAT
|
|
|
|
OPTION DOTNAME
|
2008-07-15 13:16:42 +00:00
|
|
|
IF \@Version LT 800
|
2008-07-17 09:46:09 +00:00
|
|
|
.text\$ SEGMENT PAGE 'CODE'
|
2008-07-15 13:16:42 +00:00
|
|
|
ELSE
|
|
|
|
.text\$ SEGMENT ALIGN(64) 'CODE'
|
|
|
|
ENDIF
|
2007-12-18 09:18:49 +00:00
|
|
|
___
|
|
|
|
push(@out,$tmp);
|
2008-07-22 08:44:31 +00:00
|
|
|
$segment = ".text\$";
|
2007-12-18 09:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ::function_begin_B
|
|
|
|
{ my $func=shift;
|
|
|
|
my $global=($func !~ /^_/);
|
|
|
|
my $begin="${::lbdecor}_${func}_begin";
|
|
|
|
|
|
|
|
&::LABEL($func,$global?"$begin":"$nmdecor$func");
|
2008-07-17 09:46:09 +00:00
|
|
|
$func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
|
|
|
|
else { $func.=" PRIVATE\n"; }
|
|
|
|
push(@out,$func);
|
|
|
|
$::stack=4;
|
|
|
|
}
|
|
|
|
sub ::function_end_B
|
|
|
|
{ my $func=shift;
|
|
|
|
|
|
|
|
push(@out,"$nmdecor$func ENDP\n");
|
|
|
|
$::stack=0;
|
|
|
|
&::wipe_labels();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::file_end
|
|
|
|
{ my $xmmheader=<<___;
|
|
|
|
.686
|
|
|
|
.XMM
|
|
|
|
IF \@Version LT 800
|
|
|
|
XMMWORD STRUCT 16
|
|
|
|
DQ 2 dup (?)
|
|
|
|
XMMWORD ENDS
|
|
|
|
ENDIF
|
|
|
|
___
|
|
|
|
if (grep {/\b[x]?mm[0-7]\b/i} @out) {
|
|
|
|
grep {s/\.[3-7]86/$xmmheader/} @out;
|
|
|
|
}
|
|
|
|
|
2008-07-22 08:44:31 +00:00
|
|
|
push(@out,"$segment ENDS\n");
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
|
|
|
|
{ my $comm=<<___;
|
2009-11-15 17:06:44 +00:00
|
|
|
.bss SEGMENT 'BSS'
|
2012-11-17 19:04:15 +00:00
|
|
|
COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD:4
|
2008-07-15 13:16:42 +00:00
|
|
|
.bss ENDS
|
2007-12-18 09:18:49 +00:00
|
|
|
___
|
|
|
|
# comment out OPENSSL_ia32cap_P declarations
|
|
|
|
grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
|
|
|
|
push (@out,$comm);
|
|
|
|
}
|
|
|
|
push (@out,$initseg) if ($initseg);
|
|
|
|
push (@out,"END\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
|
|
|
|
|
|
|
|
*::set_label_B = sub
|
|
|
|
{ my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
|
|
|
|
|
|
|
|
sub ::external_label
|
2008-01-03 16:21:06 +00:00
|
|
|
{ foreach(@_)
|
|
|
|
{ push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
|
|
|
|
}
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
sub ::public_label
|
|
|
|
{ push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
|
|
|
|
|
|
|
|
sub ::data_byte
|
2015-02-22 18:23:25 +00:00
|
|
|
{ push(@out,("DB\t").join(',',splice(@_,0,16))."\n") while(@_); }
|
2007-12-18 09:18:49 +00:00
|
|
|
|
2010-06-04 13:13:18 +00:00
|
|
|
sub ::data_short
|
2015-02-22 18:23:25 +00:00
|
|
|
{ push(@out,("DW\t").join(',',splice(@_,0,8))."\n") while(@_); }
|
2010-06-04 13:13:18 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::data_word
|
2015-02-22 18:23:25 +00:00
|
|
|
{ push(@out,("DD\t").join(',',splice(@_,0,4))."\n") while(@_); }
|
2007-12-18 09:18:49 +00:00
|
|
|
|
|
|
|
sub ::align
|
|
|
|
{ push(@out,"ALIGN\t$_[0]\n"); }
|
|
|
|
|
|
|
|
sub ::picmeup
|
|
|
|
{ my($dst,$sym)=@_;
|
|
|
|
&::lea($dst,&::DWP($sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::initseg
|
|
|
|
{ my $f=$nmdecor.shift;
|
|
|
|
|
|
|
|
$initseg.=<<___;
|
2008-01-03 16:21:06 +00:00
|
|
|
.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
|
2007-12-18 09:18:49 +00:00
|
|
|
EXTERN $f:NEAR
|
|
|
|
DD $f
|
|
|
|
.CRT\$XCU ENDS
|
|
|
|
___
|
|
|
|
}
|
|
|
|
|
2008-07-22 08:44:31 +00:00
|
|
|
sub ::dataseg
|
|
|
|
{ push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
|
|
|
|
|
2011-09-06 20:45:36 +00:00
|
|
|
sub ::safeseh
|
|
|
|
{ my $nm=shift;
|
|
|
|
push(@out,"IF \@Version GE 710\n");
|
|
|
|
push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n");
|
|
|
|
push(@out,"ENDIF\n");
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
1;
|