summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/perlasm
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r--src/lib/libcrypto/perlasm/cbc.pl2
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl16
-rw-r--r--src/lib/libcrypto/perlasm/x86gas.pl21
-rw-r--r--src/lib/libcrypto/perlasm/x86masm.pl198
-rw-r--r--src/lib/libcrypto/perlasm/x86nasm.pl177
5 files changed, 411 insertions, 3 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl
index 24561e759a..6fc2510905 100644
--- a/src/lib/libcrypto/perlasm/cbc.pl
+++ b/src/lib/libcrypto/perlasm/cbc.pl
@@ -150,7 +150,7 @@ sub cbc
150&set_label("PIC_point"); 150&set_label("PIC_point");
151 &blindpop("edx"); 151 &blindpop("edx");
152 &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx")); 152 &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx"));
153 &mov($count,&DWP(0,"ecx",$count,4)); 153 &mov($count,&DWP(0,"ecx",$count,4))
154 &add($count,"edx"); 154 &add($count,"edx");
155 &xor("ecx","ecx"); 155 &xor("ecx","ecx");
156 &xor("edx","edx"); 156 &xor("edx","edx");
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
index eb543db2f6..bf783cff26 100644
--- a/src/lib/libcrypto/perlasm/x86asm.pl
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -33,6 +33,13 @@ sub ::emit
33 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); } 33 else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
34} 34}
35 35
36sub ::emitraw
37{ my $opcode=shift;
38
39 if ($#_==-1) { push(@out,"$opcode\n"); }
40 else { push(@out,"$opcode\t".join(',',@_)."\n"); }
41}
42
36sub ::LB 43sub ::LB
37{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'"; 44{ $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
38 $1."l"; 45 $1."l";
@@ -218,7 +225,7 @@ sub ::asm_init
218 $filename=$fn; 225 $filename=$fn;
219 $i386=$cpu; 226 $i386=$cpu;
220 227
221 $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0; 228 $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$openbsd=$android=0;
222 if (($type eq "elf")) 229 if (($type eq "elf"))
223 { $elf=1; require "x86gas.pl"; } 230 { $elf=1; require "x86gas.pl"; }
224 elsif (($type eq "a\.out")) 231 elsif (($type eq "a\.out"))
@@ -235,6 +242,10 @@ sub ::asm_init
235 { $win32=1; require "x86masm.pl"; } 242 { $win32=1; require "x86masm.pl"; }
236 elsif (($type eq "macosx")) 243 elsif (($type eq "macosx"))
237 { $aout=1; $macosx=1; require "x86gas.pl"; } 244 { $aout=1; $macosx=1; require "x86gas.pl"; }
245 elsif (($type eq "openbsd-elf"))
246 { $openbsd=$elf=1; require "x86gas.pl"; }
247 elsif (($type eq "openbsd-a.out"))
248 { $openbsd=1; require "x86gas.pl"; }
238 elsif (($type eq "android")) 249 elsif (($type eq "android"))
239 { $elf=1; $android=1; require "x86gas.pl"; } 250 { $elf=1; $android=1; require "x86gas.pl"; }
240 else 251 else
@@ -244,6 +255,8 @@ Pick one target type from
244 a.out - DJGPP, elder OpenBSD, etc. 255 a.out - DJGPP, elder OpenBSD, etc.
245 coff - GAS/COFF such as Win32 targets 256 coff - GAS/COFF such as Win32 targets
246 win32n - Windows 95/Windows NT NASM format 257 win32n - Windows 95/Windows NT NASM format
258 openbsd-elf - OpenBSD elf
259 openbsd-a.out - OpenBSD a.out
247 nw-nasm - NetWare NASM format 260 nw-nasm - NetWare NASM format
248 macosx - Mac OS X 261 macosx - Mac OS X
249EOF 262EOF
@@ -253,6 +266,7 @@ EOF
253 $pic=0; 266 $pic=0;
254 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } 267 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
255 268
269 ::emitraw("#include <machine/asm.h>\n") if $openbsd;
256 $filename =~ s/\.pl$//; 270 $filename =~ s/\.pl$//;
257 &file($filename); 271 &file($filename);
258} 272}
diff --git a/src/lib/libcrypto/perlasm/x86gas.pl b/src/lib/libcrypto/perlasm/x86gas.pl
index 682a3a3163..d4baea514b 100644
--- a/src/lib/libcrypto/perlasm/x86gas.pl
+++ b/src/lib/libcrypto/perlasm/x86gas.pl
@@ -182,6 +182,15 @@ sub ::align
182sub ::picmeup 182sub ::picmeup
183{ my($dst,$sym,$base,$reflabel)=@_; 183{ my($dst,$sym,$base,$reflabel)=@_;
184 184
185 if ($::openbsd)
186 { &::emitraw("#if defined(PIC) || defined(__PIC__)");
187 &::emitraw("PIC_PROLOGUE");
188 &::mov($dst, &::DWP("PIC_GOT($sym)"));
189 &::emitraw("PIC_EPILOGUE");
190 &::emitraw("#else /* PIC */");
191 &::lea($dst,&::DWP($sym));
192 &::emitraw("#endif /* PIC */");
193 }
185 if (($::pic && ($::elf || $::aout)) || $::macosx) 194 if (($::pic && ($::elf || $::aout)) || $::macosx)
186 { if (!defined($base)) 195 { if (!defined($base))
187 { &::call(&::label("PIC_me_up")); 196 { &::call(&::label("PIC_me_up"));
@@ -208,7 +217,17 @@ sub ::picmeup
208sub ::initseg 217sub ::initseg
209{ my $f=$nmdecor.shift; 218{ my $f=$nmdecor.shift;
210 219
211 if ($::android) 220 if ($::openbsd)
221 { $initseg.=<<___;
222.section .init
223PIC_PROLOGUE
224 call PIC_PLT($f)
225PIC_EPILOGUE
226 jmp .Linitalign
227.align $align
228.Linitalign:
229___
230 } elsif ($::android)
212 { $initseg.=<<___; 231 { $initseg.=<<___;
213.section .init_array 232.section .init_array
214.align 4 233.align 4
diff --git a/src/lib/libcrypto/perlasm/x86masm.pl b/src/lib/libcrypto/perlasm/x86masm.pl
new file mode 100644
index 0000000000..f937d07c87
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86masm.pl
@@ -0,0 +1,198 @@
1#!/usr/bin/env perl
2
3package x86masm;
4
5*out=\@::out;
6
7$::lbdecor="\$L"; # local label decoration
8$nmdecor="_"; # external name decoration
9
10$initseg="";
11$segment="";
12
13sub ::generic
14{ my ($opcode,@arg)=@_;
15
16 # fix hexadecimal constants
17 for (@arg) { s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/oi; }
18
19 if ($opcode =~ /lea/ && @arg[1] =~ s/.*PTR\s+(\(.*\))$/OFFSET $1/) # no []
20 { $opcode="mov"; }
21 elsif ($opcode !~ /movq/)
22 { # fix xmm references
23 $arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[1]=~/\bxmm[0-7]\b/i);
24 $arg[1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i);
25 }
26
27 &::emit($opcode,@arg);
28 1;
29}
30#
31# opcodes not covered by ::generic above, mostly inconsistent namings...
32#
33sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
34sub ::call_ptr { &::emit("call",@_); }
35sub ::jmp_ptr { &::emit("jmp",@_); }
36sub ::lock { &::data_byte(0xf0); }
37
38sub get_mem
39{ my($size,$addr,$reg1,$reg2,$idx)=@_;
40 my($post,$ret);
41
42 $ret .= "$size PTR " if ($size ne "");
43
44 $addr =~ s/^\s+//;
45 # prepend global references with optional underscore
46 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
47 # put address arithmetic expression in parenthesis
48 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
49
50 if (($addr ne "") && ($addr ne 0))
51 { if ($addr !~ /^-/) { $ret .= "$addr"; }
52 else { $post=$addr; }
53 }
54 $ret .= "[";
55
56 if ($reg2 ne "")
57 { $idx!=0 or $idx=1;
58 $ret .= "$reg2*$idx";
59 $ret .= "+$reg1" if ($reg1 ne "");
60 }
61 else
62 { $ret .= "$reg1"; }
63
64 $ret .= "$post]";
65 $ret =~ s/\+\]/]/; # in case $addr was the only argument
66 $ret =~ s/\[\s*\]//;
67
68 $ret;
69}
70sub ::BP { &get_mem("BYTE",@_); }
71sub ::WP { &get_mem("WORD",@_); }
72sub ::DWP { &get_mem("DWORD",@_); }
73sub ::QWP { &get_mem("QWORD",@_); }
74sub ::BC { "@_"; }
75sub ::DWC { "@_"; }
76
77sub ::file
78{ my $tmp=<<___;
79TITLE $_[0].asm
80IF \@Version LT 800
81ECHO MASM version 8.00 or later is strongly recommended.
82ENDIF
83.486
84.MODEL FLAT
85OPTION DOTNAME
86IF \@Version LT 800
87.text\$ SEGMENT PAGE 'CODE'
88ELSE
89.text\$ SEGMENT ALIGN(64) 'CODE'
90ENDIF
91___
92 push(@out,$tmp);
93 $segment = ".text\$";
94}
95
96sub ::function_begin_B
97{ my $func=shift;
98 my $global=($func !~ /^_/);
99 my $begin="${::lbdecor}_${func}_begin";
100
101 &::LABEL($func,$global?"$begin":"$nmdecor$func");
102 $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
103
104 if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
105 else { $func.=" PRIVATE\n"; }
106 push(@out,$func);
107 $::stack=4;
108}
109sub ::function_end_B
110{ my $func=shift;
111
112 push(@out,"$nmdecor$func ENDP\n");
113 $::stack=0;
114 &::wipe_labels();
115}
116
117sub ::file_end
118{ my $xmmheader=<<___;
119.686
120.XMM
121IF \@Version LT 800
122XMMWORD STRUCT 16
123DQ 2 dup (?)
124XMMWORD ENDS
125ENDIF
126___
127 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
128 grep {s/\.[3-7]86/$xmmheader/} @out;
129 }
130
131 push(@out,"$segment ENDS\n");
132
133 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
134 { my $comm=<<___;
135.bss SEGMENT 'BSS'
136COMM ${nmdecor}OPENSSL_ia32cap_P:QWORD
137.bss ENDS
138___
139 # comment out OPENSSL_ia32cap_P declarations
140 grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
141 push (@out,$comm);
142 }
143 push (@out,$initseg) if ($initseg);
144 push (@out,"END\n");
145}
146
147sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
148
149*::set_label_B = sub
150{ my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
151
152sub ::external_label
153{ foreach(@_)
154 { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
155}
156
157sub ::public_label
158{ push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
159
160sub ::data_byte
161{ push(@out,("DB\t").join(',',@_)."\n"); }
162
163sub ::data_short
164{ push(@out,("DW\t").join(',',@_)."\n"); }
165
166sub ::data_word
167{ push(@out,("DD\t").join(',',@_)."\n"); }
168
169sub ::align
170{ push(@out,"ALIGN\t$_[0]\n"); }
171
172sub ::picmeup
173{ my($dst,$sym)=@_;
174 &::lea($dst,&::DWP($sym));
175}
176
177sub ::initseg
178{ my $f=$nmdecor.shift;
179
180 $initseg.=<<___;
181.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
182EXTERN $f:NEAR
183DD $f
184.CRT\$XCU ENDS
185___
186}
187
188sub ::dataseg
189{ push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
190
191sub ::safeseh
192{ my $nm=shift;
193 push(@out,"IF \@Version GE 710\n");
194 push(@out,".SAFESEH ".&::LABEL($nm,$nmdecor.$nm)."\n");
195 push(@out,"ENDIF\n");
196}
197
1981;
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl
new file mode 100644
index 0000000000..ca2511c9eb
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86nasm.pl
@@ -0,0 +1,177 @@
1#!/usr/bin/env perl
2
3package x86nasm;
4
5*out=\@::out;
6
7$::lbdecor="L\$"; # local label decoration
8$nmdecor=$::netware?"":"_"; # external name decoration
9$drdecor=$::mwerks?".":""; # directive decoration
10
11$initseg="";
12
13sub ::generic
14{ my $opcode=shift;
15 my $tmp;
16
17 if (!$::mwerks)
18 { if ($opcode =~ m/^j/o && $#_==0) # optimize jumps
19 { $_[0] = "NEAR $_[0]"; }
20 elsif ($opcode eq "lea" && $#_==1) # wipe storage qualifier from lea
21 { $_[1] =~ s/^[^\[]*\[/\[/o; }
22 elsif ($opcode eq "clflush" && $#_==0)
23 { $_[0] =~ s/^[^\[]*\[/\[/o; }
24 }
25 &::emit($opcode,@_);
26 1;
27}
28#
29# opcodes not covered by ::generic above, mostly inconsistent namings...
30#
31sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
32sub ::call_ptr { &::emit("call",@_); }
33sub ::jmp_ptr { &::emit("jmp",@_); }
34
35sub get_mem
36{ my($size,$addr,$reg1,$reg2,$idx)=@_;
37 my($post,$ret);
38
39 if ($size ne "")
40 { $ret .= "$size";
41 $ret .= " PTR" if ($::mwerks);
42 $ret .= " ";
43 }
44 $ret .= "[";
45
46 $addr =~ s/^\s+//;
47 # prepend global references with optional underscore
48 $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
49 # put address arithmetic expression in parenthesis
50 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
51
52 if (($addr ne "") && ($addr ne 0))
53 { if ($addr !~ /^-/) { $ret .= "$addr+"; }
54 else { $post=$addr; }
55 }
56
57 if ($reg2 ne "")
58 { $idx!=0 or $idx=1;
59 $ret .= "$reg2*$idx";
60 $ret .= "+$reg1" if ($reg1 ne "");
61 }
62 else
63 { $ret .= "$reg1"; }
64
65 $ret .= "$post]";
66 $ret =~ s/\+\]/]/; # in case $addr was the only argument
67
68 $ret;
69}
70sub ::BP { &get_mem("BYTE",@_); }
71sub ::DWP { &get_mem("DWORD",@_); }
72sub ::WP { &get_mem("WORD",@_); }
73sub ::QWP { &get_mem("",@_); }
74sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; }
75sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; }
76
77sub ::file
78{ if ($::mwerks) { push(@out,".section\t.text,64\n"); }
79 else
80 { my $tmp=<<___;
81%ifidn __OUTPUT_FORMAT__,obj
82section code use32 class=code align=64
83%elifidn __OUTPUT_FORMAT__,win32
84\$\@feat.00 equ 1
85section .text code align=64
86%else
87section .text code
88%endif
89___
90 push(@out,$tmp);
91 }
92}
93
94sub ::function_begin_B
95{ my $func=shift;
96 my $global=($func !~ /^_/);
97 my $begin="${::lbdecor}_${func}_begin";
98
99 $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
100
101 &::LABEL($func,$global?"$begin":"$nmdecor$func");
102 $func=$nmdecor.$func;
103
104 push(@out,"${drdecor}global $func\n") if ($global);
105 push(@out,"${drdecor}align 16\n");
106 push(@out,"$func:\n");
107 push(@out,"$begin:\n") if ($global);
108 $::stack=4;
109}
110
111sub ::function_end_B
112{ $::stack=0;
113 &::wipe_labels();
114}
115
116sub ::file_end
117{ if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
118 { my $comm=<<___;
119${drdecor}segment .bss
120${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 8
121___
122 # comment out OPENSSL_ia32cap_P declarations
123 grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
124 push (@out,$comm)
125 }
126 push (@out,$initseg) if ($initseg);
127}
128
129sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
130
131sub ::external_label
132{ foreach(@_)
133 { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
134}
135
136sub ::public_label
137{ push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
138
139sub ::data_byte
140{ push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
141sub ::data_short
142{ push(@out,(($::mwerks)?".word\t":"dw\t").join(',',@_)."\n"); }
143sub ::data_word
144{ push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
145
146sub ::align
147{ push(@out,"${drdecor}align\t$_[0]\n"); }
148
149sub ::picmeup
150{ my($dst,$sym)=@_;
151 &::lea($dst,&::DWP($sym));
152}
153
154sub ::initseg
155{ my $f=$nmdecor.shift;
156 if ($::win32)
157 { $initseg=<<___;
158segment .CRT\$XCU data align=4
159extern $f
160dd $f
161___
162 }
163}
164
165sub ::dataseg
166{ if ($mwerks) { push(@out,".section\t.data,4\n"); }
167 else { push(@out,"section\t.data align=4\n"); }
168}
169
170sub ::safeseh
171{ my $nm=shift;
172 push(@out,"%if __NASM_VERSION_ID__ >= 0x02030000\n");
173 push(@out,"safeseh ".&::LABEL($nm,$nmdecor.$nm)."\n");
174 push(@out,"%endif\n");
175}
176
1771;