diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r-- | src/lib/libcrypto/perlasm/cbc.pl | 331 | ||||
-rwxr-xr-x | src/lib/libcrypto/perlasm/ppc-xlate.pl | 159 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
-rwxr-xr-x | src/lib/libcrypto/perlasm/x86_64-xlate.pl | 1105 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 257 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/x86gas.pl | 315 |
6 files changed, 0 insertions, 2291 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl deleted file mode 100644 index 392f23e145..0000000000 --- a/src/lib/libcrypto/perlasm/cbc.pl +++ /dev/null | |||
@@ -1,331 +0,0 @@ | |||
1 | #!/usr/local/bin/perl | ||
2 | |||
3 | # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc) | ||
4 | # des_cblock (*input); | ||
5 | # des_cblock (*output); | ||
6 | # long length; | ||
7 | # des_key_schedule schedule; | ||
8 | # des_cblock (*ivec); | ||
9 | # int enc; | ||
10 | # | ||
11 | # calls | ||
12 | # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT); | ||
13 | # | ||
14 | |||
15 | #&cbc("des_ncbc_encrypt","des_encrypt",0); | ||
16 | #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt", | ||
17 | # 1,4,5,3,5,-1); | ||
18 | #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt", | ||
19 | # 0,4,5,3,5,-1); | ||
20 | #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3", | ||
21 | # 0,6,7,3,4,5); | ||
22 | # | ||
23 | # When doing a cipher that needs bigendian order, | ||
24 | # for encrypt, the iv is kept in bigendian form, | ||
25 | # while for decrypt, it is kept in little endian. | ||
26 | sub cbc | ||
27 | { | ||
28 | local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_; | ||
29 | # name is the function name | ||
30 | # enc_func and dec_func and the functions to call for encrypt/decrypt | ||
31 | # swap is true if byte order needs to be reversed | ||
32 | # iv_off is parameter number for the iv | ||
33 | # enc_off is parameter number for the encrypt/decrypt flag | ||
34 | # p1,p2,p3 are the offsets for parameters to be passed to the | ||
35 | # underlying calls. | ||
36 | |||
37 | &static_label("cbc_enc_jmp_table_".$name); | ||
38 | &static_label("ej1_".$name); | ||
39 | &static_label("ej2_".$name); | ||
40 | &static_label("ej3_".$name); | ||
41 | &static_label("ej4_".$name); | ||
42 | &static_label("ej5_".$name); | ||
43 | &static_label("ej6_".$name); | ||
44 | &static_label("ej7_".$name); | ||
45 | |||
46 | &function_begin_B($name,""); | ||
47 | &comment(""); | ||
48 | |||
49 | $in="esi"; | ||
50 | $out="edi"; | ||
51 | $count="ebp"; | ||
52 | |||
53 | &push("ebp"); | ||
54 | &push("ebx"); | ||
55 | &push("esi"); | ||
56 | &push("edi"); | ||
57 | |||
58 | $data_off=4; | ||
59 | $data_off+=4 if ($p1 > 0); | ||
60 | $data_off+=4 if ($p2 > 0); | ||
61 | $data_off+=4 if ($p3 > 0); | ||
62 | |||
63 | &mov($count, &wparam(2)); # length | ||
64 | |||
65 | &comment("getting iv ptr from parameter $iv_off"); | ||
66 | &mov("ebx", &wparam($iv_off)); # Get iv ptr | ||
67 | |||
68 | &mov($in, &DWP(0,"ebx","",0));# iv[0] | ||
69 | &mov($out, &DWP(4,"ebx","",0));# iv[1] | ||
70 | |||
71 | &push($out); | ||
72 | &push($in); | ||
73 | &push($out); # used in decrypt for iv[1] | ||
74 | &push($in); # used in decrypt for iv[0] | ||
75 | |||
76 | &mov("ebx", "esp"); # This is the address of tin[2] | ||
77 | |||
78 | &mov($in, &wparam(0)); # in | ||
79 | &mov($out, &wparam(1)); # out | ||
80 | |||
81 | # We have loaded them all, how lets push things | ||
82 | &comment("getting encrypt flag from parameter $enc_off"); | ||
83 | &mov("ecx", &wparam($enc_off)); # Get enc flag | ||
84 | if ($p3 > 0) | ||
85 | { | ||
86 | &comment("get and push parameter $p3"); | ||
87 | if ($enc_off != $p3) | ||
88 | { &mov("eax", &wparam($p3)); &push("eax"); } | ||
89 | else { &push("ecx"); } | ||
90 | } | ||
91 | if ($p2 > 0) | ||
92 | { | ||
93 | &comment("get and push parameter $p2"); | ||
94 | if ($enc_off != $p2) | ||
95 | { &mov("eax", &wparam($p2)); &push("eax"); } | ||
96 | else { &push("ecx"); } | ||
97 | } | ||
98 | if ($p1 > 0) | ||
99 | { | ||
100 | &comment("get and push parameter $p1"); | ||
101 | if ($enc_off != $p1) | ||
102 | { &mov("eax", &wparam($p1)); &push("eax"); } | ||
103 | else { &push("ecx"); } | ||
104 | } | ||
105 | &push("ebx"); # push data/iv | ||
106 | |||
107 | &cmp("ecx",0); | ||
108 | &jz(&label("decrypt")); | ||
109 | |||
110 | &and($count,0xfffffff8); | ||
111 | &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0] | ||
112 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1] | ||
113 | |||
114 | &jz(&label("encrypt_finish")); | ||
115 | |||
116 | ############################################################# | ||
117 | |||
118 | &set_label("encrypt_loop"); | ||
119 | # encrypt start | ||
120 | # "eax" and "ebx" hold iv (or the last cipher text) | ||
121 | |||
122 | &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes | ||
123 | &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes | ||
124 | |||
125 | &xor("eax", "ecx"); | ||
126 | &xor("ebx", "edx"); | ||
127 | |||
128 | &bswap("eax") if $swap; | ||
129 | &bswap("ebx") if $swap; | ||
130 | |||
131 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
132 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
133 | |||
134 | &call($enc_func); | ||
135 | |||
136 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
137 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
138 | |||
139 | &bswap("eax") if $swap; | ||
140 | &bswap("ebx") if $swap; | ||
141 | |||
142 | &mov(&DWP(0,$out,"",0),"eax"); | ||
143 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
144 | |||
145 | # eax and ebx are the next iv. | ||
146 | |||
147 | &add($in, 8); | ||
148 | &add($out, 8); | ||
149 | |||
150 | &sub($count, 8); | ||
151 | &jnz(&label("encrypt_loop")); | ||
152 | |||
153 | ###################################################################3 | ||
154 | &set_label("encrypt_finish"); | ||
155 | &mov($count, &wparam(2)); # length | ||
156 | &and($count, 7); | ||
157 | &jz(&label("finish")); | ||
158 | |||
159 | &picsetup("edx"); | ||
160 | &picsymbol("ecx", &label("cbc_enc_jmp_table_".$name), "edx") | ||
161 | &mov($count,&DWP(0,"ecx",$count,4)); | ||
162 | &picadjust($count, "edx"); | ||
163 | |||
164 | &xor("ecx","ecx"); | ||
165 | &xor("edx","edx"); | ||
166 | &jmp_ptr($count); | ||
167 | |||
168 | &set_label("ej7_".$name); | ||
169 | &movb(&HB("edx"), &BP(6,$in,"",0)); | ||
170 | &shl("edx",8); | ||
171 | &set_label("ej6_".$name); | ||
172 | &movb(&HB("edx"), &BP(5,$in,"",0)); | ||
173 | &set_label("ej5_".$name); | ||
174 | &movb(&LB("edx"), &BP(4,$in,"",0)); | ||
175 | &set_label("ej4_".$name); | ||
176 | &mov("ecx", &DWP(0,$in,"",0)); | ||
177 | &jmp(&label("ejend")); | ||
178 | &set_label("ej3_".$name); | ||
179 | &movb(&HB("ecx"), &BP(2,$in,"",0)); | ||
180 | &shl("ecx",8); | ||
181 | &set_label("ej2_".$name); | ||
182 | &movb(&HB("ecx"), &BP(1,$in,"",0)); | ||
183 | &set_label("ej1_".$name); | ||
184 | &movb(&LB("ecx"), &BP(0,$in,"",0)); | ||
185 | &set_label("ejend"); | ||
186 | |||
187 | &xor("eax", "ecx"); | ||
188 | &xor("ebx", "edx"); | ||
189 | |||
190 | &bswap("eax") if $swap; | ||
191 | &bswap("ebx") if $swap; | ||
192 | |||
193 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
194 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
195 | |||
196 | &call($enc_func); | ||
197 | |||
198 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
199 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
200 | |||
201 | &bswap("eax") if $swap; | ||
202 | &bswap("ebx") if $swap; | ||
203 | |||
204 | &mov(&DWP(0,$out,"",0),"eax"); | ||
205 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
206 | |||
207 | &jmp(&label("finish")); | ||
208 | |||
209 | ############################################################# | ||
210 | ############################################################# | ||
211 | &set_label("decrypt",1); | ||
212 | # decrypt start | ||
213 | &and($count,0xfffffff8); | ||
214 | # The next 2 instructions are only for if the jz is taken | ||
215 | &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
216 | &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
217 | &jz(&label("decrypt_finish")); | ||
218 | |||
219 | &set_label("decrypt_loop"); | ||
220 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
221 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
222 | |||
223 | &bswap("eax") if $swap; | ||
224 | &bswap("ebx") if $swap; | ||
225 | |||
226 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
227 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
228 | |||
229 | &call($dec_func); | ||
230 | |||
231 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
232 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
233 | |||
234 | &bswap("eax") if $swap; | ||
235 | &bswap("ebx") if $swap; | ||
236 | |||
237 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
238 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
239 | |||
240 | &xor("ecx", "eax"); | ||
241 | &xor("edx", "ebx"); | ||
242 | |||
243 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
244 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
245 | |||
246 | &mov(&DWP(0,$out,"",0),"ecx"); | ||
247 | &mov(&DWP(4,$out,"",0),"edx"); | ||
248 | |||
249 | &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv | ||
250 | &mov(&DWP($data_off+12,"esp","",0), "ebx"); # | ||
251 | |||
252 | &add($in, 8); | ||
253 | &add($out, 8); | ||
254 | |||
255 | &sub($count, 8); | ||
256 | &jnz(&label("decrypt_loop")); | ||
257 | ############################ ENDIT #######################3 | ||
258 | &set_label("decrypt_finish"); | ||
259 | &mov($count, &wparam(2)); # length | ||
260 | &and($count, 7); | ||
261 | &jz(&label("finish")); | ||
262 | |||
263 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
264 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
265 | |||
266 | &bswap("eax") if $swap; | ||
267 | &bswap("ebx") if $swap; | ||
268 | |||
269 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
270 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
271 | |||
272 | &call($dec_func); | ||
273 | |||
274 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
275 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
276 | |||
277 | &bswap("eax") if $swap; | ||
278 | &bswap("ebx") if $swap; | ||
279 | |||
280 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
281 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
282 | |||
283 | &xor("ecx", "eax"); | ||
284 | &xor("edx", "ebx"); | ||
285 | |||
286 | # this is for when we exit | ||
287 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
288 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
289 | |||
290 | &rotr("edx", 16); | ||
291 | &movb(&BP(6,$out,"",0), &LB("edx")); | ||
292 | &shr("edx",16); | ||
293 | &movb(&BP(5,$out,"",0), &HB("edx")); | ||
294 | &movb(&BP(4,$out,"",0), &LB("edx")); | ||
295 | &mov(&DWP(0,$out,"",0), "ecx"); | ||
296 | |||
297 | # final iv is still in eax:ebx | ||
298 | |||
299 | ############################ FINISH #######################3 | ||
300 | &set_label("finish",1); | ||
301 | &mov("ecx", &wparam($iv_off)); # Get iv ptr | ||
302 | |||
303 | ################################################# | ||
304 | $total=16+4; | ||
305 | $total+=4 if ($p1 > 0); | ||
306 | $total+=4 if ($p2 > 0); | ||
307 | $total+=4 if ($p3 > 0); | ||
308 | &add("esp",$total); | ||
309 | |||
310 | &mov(&DWP(0,"ecx","",0), "eax"); # save iv | ||
311 | &mov(&DWP(4,"ecx","",0), "ebx"); # save iv | ||
312 | |||
313 | &function_end_A($name); | ||
314 | &function_end_B($name); | ||
315 | |||
316 | &rodataseg(); | ||
317 | &align(64); | ||
318 | &set_label("cbc_enc_jmp_table_".$name); | ||
319 | &data_word("0"); | ||
320 | &data_word(&code_sym(&label("ej1_".$name))); | ||
321 | &data_word(&code_sym(&label("ej2_".$name))); | ||
322 | &data_word(&code_sym(&label("ej3_".$name))); | ||
323 | &data_word(&code_sym(&label("ej4_".$name))); | ||
324 | &data_word(&code_sym(&label("ej5_".$name))); | ||
325 | &data_word(&code_sym(&label("ej6_".$name))); | ||
326 | &data_word(&code_sym(&label("ej7_".$name))); | ||
327 | &previous(); | ||
328 | |||
329 | } | ||
330 | |||
331 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/ppc-xlate.pl b/src/lib/libcrypto/perlasm/ppc-xlate.pl deleted file mode 100755 index a3edd982b6..0000000000 --- a/src/lib/libcrypto/perlasm/ppc-xlate.pl +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | # PowerPC assembler distiller by <appro>. | ||
4 | |||
5 | my $flavour = shift; | ||
6 | my $output = shift; | ||
7 | open STDOUT,">$output" || die "can't open $output: $!"; | ||
8 | |||
9 | my %GLOBALS; | ||
10 | my $dotinlocallabels=($flavour=~/linux/)?1:0; | ||
11 | |||
12 | ################################################################ | ||
13 | # directives which need special treatment on different platforms | ||
14 | ################################################################ | ||
15 | my $globl = sub { | ||
16 | my $junk = shift; | ||
17 | my $name = shift; | ||
18 | my $global = \$GLOBALS{$name}; | ||
19 | my $ret; | ||
20 | |||
21 | $name =~ s|^[\.\_]||; | ||
22 | |||
23 | SWITCH: for ($flavour) { | ||
24 | /aix/ && do { $name = ".$name"; | ||
25 | last; | ||
26 | }; | ||
27 | /osx/ && do { $name = "_$name"; | ||
28 | last; | ||
29 | }; | ||
30 | /linux.*32/ && do { $ret .= ".globl $name\n"; | ||
31 | $ret .= ".type $name,\@function"; | ||
32 | last; | ||
33 | }; | ||
34 | /linux.*64/ && do { $ret .= ".globl $name\n"; | ||
35 | $ret .= ".type $name,\@function\n"; | ||
36 | $ret .= ".section \".opd\",\"aw\"\n"; | ||
37 | $ret .= ".align 3\n"; | ||
38 | $ret .= "$name:\n"; | ||
39 | $ret .= ".quad .$name,.TOC.\@tocbase,0\n"; | ||
40 | $ret .= ".size $name,24\n"; | ||
41 | $ret .= ".previous\n"; | ||
42 | |||
43 | $name = ".$name"; | ||
44 | last; | ||
45 | }; | ||
46 | } | ||
47 | |||
48 | $ret = ".globl $name" if (!$ret); | ||
49 | $$global = $name; | ||
50 | $ret; | ||
51 | }; | ||
52 | my $text = sub { | ||
53 | ($flavour =~ /aix/) ? ".csect" : ".text"; | ||
54 | }; | ||
55 | my $machine = sub { | ||
56 | my $junk = shift; | ||
57 | my $arch = shift; | ||
58 | if ($flavour =~ /osx/) | ||
59 | { $arch =~ s/\"//g; | ||
60 | $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any"); | ||
61 | } | ||
62 | ".machine $arch"; | ||
63 | }; | ||
64 | my $size = sub { | ||
65 | if ($flavour =~ /linux.*32/) | ||
66 | { shift; | ||
67 | ".size " . join(",",@_); | ||
68 | } | ||
69 | else | ||
70 | { ""; } | ||
71 | }; | ||
72 | my $asciz = sub { | ||
73 | shift; | ||
74 | my $line = join(",",@_); | ||
75 | if ($line =~ /^"(.*)"$/) | ||
76 | { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; } | ||
77 | else | ||
78 | { ""; } | ||
79 | }; | ||
80 | |||
81 | ################################################################ | ||
82 | # simplified mnemonics not handled by at least one assembler | ||
83 | ################################################################ | ||
84 | my $cmplw = sub { | ||
85 | my $f = shift; | ||
86 | my $cr = 0; $cr = shift if ($#_>1); | ||
87 | # Some out-of-date 32-bit GNU assembler just can't handle cmplw... | ||
88 | ($flavour =~ /linux.*32/) ? | ||
89 | " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 : | ||
90 | " cmplw ".join(',',$cr,@_); | ||
91 | }; | ||
92 | my $bdnz = sub { | ||
93 | my $f = shift; | ||
94 | my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint | ||
95 | " bc $bo,0,".shift; | ||
96 | } if ($flavour!~/linux/); | ||
97 | my $bltlr = sub { | ||
98 | my $f = shift; | ||
99 | my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint | ||
100 | ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints | ||
101 | " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 : | ||
102 | " bclr $bo,0"; | ||
103 | }; | ||
104 | my $bnelr = sub { | ||
105 | my $f = shift; | ||
106 | my $bo = $f=~/\-/ ? 4+2 : 4; # optional "not to be taken" hint | ||
107 | ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints | ||
108 | " .long ".sprintf "0x%x",19<<26|$bo<<21|2<<16|16<<1 : | ||
109 | " bclr $bo,2"; | ||
110 | }; | ||
111 | my $beqlr = sub { | ||
112 | my $f = shift; | ||
113 | my $bo = $f=~/-/ ? 12+2 : 12; # optional "not to be taken" hint | ||
114 | ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints | ||
115 | " .long ".sprintf "0x%X",19<<26|$bo<<21|2<<16|16<<1 : | ||
116 | " bclr $bo,2"; | ||
117 | }; | ||
118 | # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two | ||
119 | # arguments is 64, with "operand out of range" error. | ||
120 | my $extrdi = sub { | ||
121 | my ($f,$ra,$rs,$n,$b) = @_; | ||
122 | $b = ($b+$n)&63; $n = 64-$n; | ||
123 | " rldicl $ra,$rs,$b,$n"; | ||
124 | }; | ||
125 | |||
126 | while($line=<>) { | ||
127 | |||
128 | $line =~ s|[#!;].*$||; # get rid of asm-style comments... | ||
129 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
130 | $line =~ s|^\s+||; # ... and skip white spaces in beginning... | ||
131 | $line =~ s|\s+$||; # ... and at the end | ||
132 | |||
133 | { | ||
134 | $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel | ||
135 | $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels); | ||
136 | } | ||
137 | |||
138 | { | ||
139 | $line =~ s|(^[\.\w]+)\:\s*||; | ||
140 | my $label = $1; | ||
141 | printf "%s:",($GLOBALS{$label} or $label) if ($label); | ||
142 | } | ||
143 | |||
144 | { | ||
145 | $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||; | ||
146 | my $c = $1; $c = "\t" if ($c eq ""); | ||
147 | my $mnemonic = $2; | ||
148 | my $f = $3; | ||
149 | my $opcode = eval("\$$mnemonic"); | ||
150 | $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/); | ||
151 | if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); } | ||
152 | elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; } | ||
153 | } | ||
154 | |||
155 | print $line if ($line); | ||
156 | print "\n"; | ||
157 | } | ||
158 | |||
159 | close STDOUT; | ||
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme deleted file mode 100644 index a7876bcc95..0000000000 --- a/src/lib/libcrypto/perlasm/readme +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
1 | The perl scripts in this directory are my 'hack' to generate | ||
2 | multiple different assembler formats via the one original script. | ||
3 | |||
4 | The way to use this library is to start with adding the path to this directory | ||
5 | and then include it. | ||
6 | |||
7 | push(@INC,"perlasm","../../perlasm"); | ||
8 | require "x86asm.pl"; | ||
9 | |||
10 | The first thing we do is setup the file and type of assembler | ||
11 | |||
12 | &asm_init($ARGV[0],$0); | ||
13 | |||
14 | The first argument is the 'type'. Currently | ||
15 | 'cpp', 'sol', 'a.out', 'elf' or 'win32'. | ||
16 | Argument 2 is the file name. | ||
17 | |||
18 | The reciprocal function is | ||
19 | &asm_finish() which should be called at the end. | ||
20 | |||
21 | There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler, | ||
22 | and x86unix.pl which is the unix (gas) version. | ||
23 | |||
24 | Functions of interest are: | ||
25 | &external_label("des_SPtrans"); declare and external variable | ||
26 | &LB(reg); Low byte for a register | ||
27 | &HB(reg); High byte for a register | ||
28 | &BP(off,base,index,scale) Byte pointer addressing | ||
29 | &DWP(off,base,index,scale) Word pointer addressing | ||
30 | &stack_push(num) Basically a 'sub esp, num*4' with extra | ||
31 | &stack_pop(num) inverse of stack_push | ||
32 | &function_begin(name,extra) Start a function with pushing of | ||
33 | edi, esi, ebx and ebp. extra is extra win32 | ||
34 | external info that may be required. | ||
35 | &function_begin_B(name,extra) Same as norma function_begin but no pushing. | ||
36 | &function_end(name) Call at end of function. | ||
37 | &function_end_A(name) Standard pop and ret, for use inside functions | ||
38 | &function_end_B(name) Call at end but with poping or 'ret'. | ||
39 | &swtmp(num) Address on stack temp word. | ||
40 | &wparam(num) Parameter number num, that was push | ||
41 | in C convention. This all works over pushes | ||
42 | and pops. | ||
43 | &comment("hello there") Put in a comment. | ||
44 | &label("loop") Refer to a label, normally a jmp target. | ||
45 | &set_label("loop") Set a label at this point. | ||
46 | &data_word(word) Put in a word of data. | ||
47 | |||
48 | So how does this all hold together? Given | ||
49 | |||
50 | int calc(int len, int *data) | ||
51 | { | ||
52 | int i,j=0; | ||
53 | |||
54 | for (i=0; i<len; i++) | ||
55 | { | ||
56 | j+=other(data[i]); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | So a very simple version of this function could be coded as | ||
61 | |||
62 | push(@INC,"perlasm","../../perlasm"); | ||
63 | require "x86asm.pl"; | ||
64 | |||
65 | &asm_init($ARGV[0],"cacl.pl"); | ||
66 | |||
67 | &external_label("other"); | ||
68 | |||
69 | $tmp1= "eax"; | ||
70 | $j= "edi"; | ||
71 | $data= "esi"; | ||
72 | $i= "ebp"; | ||
73 | |||
74 | &comment("a simple function"); | ||
75 | &function_begin("calc"); | ||
76 | &mov( $data, &wparam(1)); # data | ||
77 | &xor( $j, $j); | ||
78 | &xor( $i, $i); | ||
79 | |||
80 | &set_label("loop"); | ||
81 | &cmp( $i, &wparam(0)); | ||
82 | &jge( &label("end")); | ||
83 | |||
84 | &mov( $tmp1, &DWP(0,$data,$i,4)); | ||
85 | &push( $tmp1); | ||
86 | &call( "other"); | ||
87 | &add( $j, "eax"); | ||
88 | &pop( $tmp1); | ||
89 | &inc( $i); | ||
90 | &jmp( &label("loop")); | ||
91 | |||
92 | &set_label("end"); | ||
93 | &mov( "eax", $j); | ||
94 | |||
95 | &function_end("calc"); | ||
96 | |||
97 | &asm_finish(); | ||
98 | |||
99 | The above example is very very unoptimised but gives an idea of how | ||
100 | things work. | ||
101 | |||
102 | There is also a cbc mode function generator in cbc.pl | ||
103 | |||
104 | &cbc( $name, | ||
105 | $encrypt_function_name, | ||
106 | $decrypt_function_name, | ||
107 | $true_if_byte_swap_needed, | ||
108 | $parameter_number_for_iv, | ||
109 | $parameter_number_for_encrypt_flag, | ||
110 | $first_parameter_to_pass, | ||
111 | $second_parameter_to_pass, | ||
112 | $third_parameter_to_pass); | ||
113 | |||
114 | So for example, given | ||
115 | void BF_encrypt(BF_LONG *data,BF_KEY *key); | ||
116 | void BF_decrypt(BF_LONG *data,BF_KEY *key); | ||
117 | void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length, | ||
118 | BF_KEY *ks, unsigned char *iv, int enc); | ||
119 | |||
120 | &cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1); | ||
121 | |||
122 | &cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1); | ||
123 | &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5); | ||
124 | |||
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl deleted file mode 100755 index 1e54753123..0000000000 --- a/src/lib/libcrypto/perlasm/x86_64-xlate.pl +++ /dev/null | |||
@@ -1,1105 +0,0 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>. | ||
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; | ||
23 | # | ||
24 | # Dual-ABI styling rules. | ||
25 | # | ||
26 | # 1. Adhere to Unix register and stack layout [see cross-reference | ||
27 | # ABI "card" at the end for explanation]. | ||
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 | ||
32 | # unified Win64 prologue and epilogue automatically. If you want | ||
33 | # to take care of ABI differences yourself, tag functions as | ||
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:-) | ||
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.: | ||
50 | # | ||
51 | # .picmeup %rax | ||
52 | # lea .Label-.(%rax),%rax | ||
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. | ||
57 | # 9. .init segment is allowed to contain calls to functions only. | ||
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. | ||
60 | |||
61 | my $flavour = shift; | ||
62 | my $output = shift; | ||
63 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } | ||
64 | |||
65 | open STDOUT,">$output" || die "can't open $output: $!" | ||
66 | if (defined($output)); | ||
67 | |||
68 | my $gas=1; $gas=0 if ($output =~ /\.asm$/); | ||
69 | my $elf=1; $elf=0 if (!$gas); | ||
70 | my $win64=0; | ||
71 | my $prefix=""; | ||
72 | my $decor=".L"; | ||
73 | |||
74 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 | ||
75 | my $masm=0; | ||
76 | my $PTR=" PTR"; | ||
77 | |||
78 | my $nasmref=2.03; | ||
79 | my $nasm=0; | ||
80 | |||
81 | if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; | ||
82 | $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`; | ||
83 | chomp($prefix); | ||
84 | } | ||
85 | elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; } | ||
86 | elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; } | ||
87 | elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; } | ||
88 | elsif (!$gas) | ||
89 | { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i) | ||
90 | { $nasm = $1 + $2*0.01; $PTR=""; } | ||
91 | elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/) | ||
92 | { $masm = $1 + $2*2**-16 + $4*2**-32; } | ||
93 | die "no assembler found on %PATH" if (!($nasm || $masm)); | ||
94 | $win64=1; | ||
95 | $elf=0; | ||
96 | $decor="\$L\$"; | ||
97 | } | ||
98 | |||
99 | my $current_segment; | ||
100 | my $current_function; | ||
101 | my %globals; | ||
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 | |||
109 | if ($line =~ /^([a-z][a-z0-9]*)/i) { | ||
110 | $self->{op} = $1; | ||
111 | $ret = $self; | ||
112 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
113 | |||
114 | undef $self->{sz}; | ||
115 | if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain... | ||
116 | $self->{op} = $1; | ||
117 | $self->{sz} = $2; | ||
118 | } elsif ($self->{op} =~ /call|jmp/) { | ||
119 | $self->{sz} = ""; | ||
120 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn | ||
121 | $self->{sz} = ""; | ||
122 | } elsif ($self->{op} =~ /^v/) { # VEX | ||
123 | $self->{sz} = ""; | ||
124 | } elsif ($self->{op} =~ /mov[dq]/ && $line =~ /%xmm/) { | ||
125 | $self->{sz} = ""; | ||
126 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { | ||
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; | ||
141 | if ($gas) { | ||
142 | if ($self->{op} eq "movz") { # movz is pain... | ||
143 | sprintf "%s%s%s",$self->{op},$self->{sz},shift; | ||
144 | } elsif ($self->{op} =~ /^set/) { | ||
145 | "$self->{op}"; | ||
146 | } elsif ($self->{op} eq "ret") { | ||
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 . "retq"; | ||
153 | } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") { | ||
154 | ".p2align\t3\n\t.quad"; | ||
155 | } else { | ||
156 | "$self->{op}$self->{sz}"; | ||
157 | } | ||
158 | } else { | ||
159 | $self->{op} =~ s/^movz/movzx/; | ||
160 | if ($self->{op} eq "ret") { | ||
161 | $self->{op} = ""; | ||
162 | if ($win64 && $current_function->{abi} eq "svr4") { | ||
163 | $self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t". | ||
164 | "mov rsi,QWORD${PTR}[16+rsp]\n\t"; | ||
165 | } | ||
166 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; | ||
167 | } elsif ($self->{op} =~ /^(pop|push)f/) { | ||
168 | $self->{op} .= $self->{sz}; | ||
169 | } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") { | ||
170 | $self->{op} = "\tDQ"; | ||
171 | } | ||
172 | $self->{op}; | ||
173 | } | ||
174 | } | ||
175 | sub mnemonic { | ||
176 | my $self=shift; | ||
177 | my $op=shift; | ||
178 | $self->{op}=$op if (defined($op)); | ||
179 | $self->{op}; | ||
180 | } | ||
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; | ||
197 | |||
198 | if ($gas) { | ||
199 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
200 | # in $self->{value} | ||
201 | $self->{value} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; | ||
202 | $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
203 | sprintf "\$%s",$self->{value}; | ||
204 | } else { | ||
205 | $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig; | ||
206 | $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm); | ||
207 | sprintf "%s",$self->{value}; | ||
208 | } | ||
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 | |||
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); | ||
222 | $self->{scale} = 1 if (!defined($self->{scale})); | ||
223 | $ret = $self; | ||
224 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
225 | |||
226 | if ($win64 && $self->{label} =~ s/\@GOTPCREL//) { | ||
227 | die if (opcode->mnemonic() ne "mov"); | ||
228 | opcode->mnemonic("lea"); | ||
229 | } | ||
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 | |||
240 | $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; | ||
241 | $self->{label} =~ s/\.L/$decor/g; | ||
242 | |||
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 | |||
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 | |||
256 | if ($gas) { | ||
257 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64"); | ||
258 | |||
259 | if (defined($self->{index})) { | ||
260 | sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk}, | ||
261 | $self->{label}, | ||
262 | $self->{base}?"%$self->{base}":"", | ||
263 | $self->{index},$self->{scale}; | ||
264 | } else { | ||
265 | sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; | ||
266 | } | ||
267 | } else { | ||
268 | %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", | ||
269 | q=>"QWORD$PTR",o=>"OWORD$PTR",x=>"XMMWORD$PTR" ); | ||
270 | |||
271 | $self->{label} =~ s/\./\$/g; | ||
272 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; | ||
273 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); | ||
274 | $sz="q" if ($self->{asterisk} || opcode->mnemonic() eq "movq"); | ||
275 | $sz="l" if (opcode->mnemonic() eq "movd"); | ||
276 | |||
277 | if (defined($self->{index})) { | ||
278 | sprintf "%s[%s%s*%d%s]",$szmap{$sz}, | ||
279 | $self->{label}?"$self->{label}+":"", | ||
280 | $self->{index},$self->{scale}, | ||
281 | $self->{base}?"+$self->{base}":""; | ||
282 | } elsif ($self->{base} eq "rip") { | ||
283 | sprintf "%s[%s]",$szmap{$sz},$self->{label}; | ||
284 | } else { | ||
285 | sprintf "%s[%s%s]",$szmap{$sz}, | ||
286 | $self->{label}?"$self->{label}+":"", | ||
287 | $self->{base}; | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | { package register; # pick up registers, which start with %. | ||
293 | sub re { | ||
294 | my $class = shift; # multiple instances... | ||
295 | my $self = {}; | ||
296 | local *line = shift; | ||
297 | undef $ret; | ||
298 | |||
299 | # optional * ---vvv--- appears in indirect jmp/call | ||
300 | if ($line =~ /^(\*?)%(\w+)/) { | ||
301 | bless $self,$class; | ||
302 | $self->{asterisk} = $1; | ||
303 | $self->{value} = $2; | ||
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; | ||
326 | if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; } | ||
327 | else { $self->{value}; } | ||
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 | |||
336 | if ($line =~ /(^[\.\w]+)\:/) { | ||
337 | $self->{value} = $1; | ||
338 | $ret = $self; | ||
339 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
340 | |||
341 | $self->{value} =~ s/^\.L/$decor/; | ||
342 | } | ||
343 | $ret; | ||
344 | } | ||
345 | sub out { | ||
346 | my $self = shift; | ||
347 | |||
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"; | ||
357 | $func .= "${decor}SEH_begin_$current_function->{name}:\n"; | ||
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}") { | ||
369 | $self->{value} .= ":" if ($masm && $ret!~m/^\$/); | ||
370 | $self->{value} . ":"; | ||
371 | } elsif ($win64 && $current_function->{abi} eq "svr4") { | ||
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"; | ||
377 | $func .= " mov rax,rsp\n"; | ||
378 | $func .= "${decor}SEH_begin_$current_function->{name}:"; | ||
379 | $func .= ":" if ($masm); | ||
380 | $func .= "\n"; | ||
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); | ||
387 | $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4); | ||
388 | $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5); | ||
389 | $func .= "\n"; | ||
390 | } else { | ||
391 | "$current_function->{name}". | ||
392 | ($nasm ? ":" : "\tPROC $current_function->{scope}"); | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | { package expr; # pick up expressions | ||
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 | |||
407 | $self->{value} =~ s/\@PLT// if (!$elf); | ||
408 | $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; | ||
409 | $self->{value} =~ s/\.L/$decor/g; | ||
410 | } | ||
411 | $ret; | ||
412 | } | ||
413 | sub out { | ||
414 | my $self = shift; | ||
415 | if ($nasm && opcode->mnemonic()=~m/^j/) { | ||
416 | "NEAR ".$self->{value}; | ||
417 | } else { | ||
418 | $self->{value}; | ||
419 | } | ||
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; | ||
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 ); | ||
437 | |||
438 | if ($line =~ /^\s*(\.\w+)/) { | ||
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 | }; | ||
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 | }; | ||
483 | } | ||
484 | |||
485 | if ($gas) { | ||
486 | $self->{value} = $dir . "\t" . $line; | ||
487 | |||
488 | if ($dir =~ /\.extern/) { | ||
489 | $self->{value} = ""; # swallow extern | ||
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)) { | ||
499 | $self->{value} .= "${decor}SEH_end_$current_function->{name}:" | ||
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 ".rodata") { | ||
508 | if ($flavour eq "macosx") { $self->{value} = ".section\t__DATA,__const"; } | ||
509 | } | ||
510 | if (!$elf && $current_segment eq ".init") { | ||
511 | if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; } | ||
512 | elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; } | ||
513 | } | ||
514 | } elsif ($dir =~ /\.(text|data)/) { | ||
515 | $current_segment=".$1"; | ||
516 | } elsif ($dir =~ /\.hidden/) { | ||
517 | if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$line"; } | ||
518 | elsif ($flavour eq "mingw64") { $self->{value} = ""; } | ||
519 | } elsif ($dir =~ /\.comm/) { | ||
520 | $self->{value} = "$dir\t$prefix$line"; | ||
521 | $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx"); | ||
522 | } | ||
523 | $line = ""; | ||
524 | return $self; | ||
525 | } | ||
526 | |||
527 | # non-gas case or nasm/masm | ||
528 | SWITCH: for ($dir) { | ||
529 | /\.text/ && do { my $v=undef; | ||
530 | if ($nasm) { | ||
531 | $v="section .text code align=64\n"; | ||
532 | } else { | ||
533 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
534 | $current_segment = ".text\$"; | ||
535 | $v.="$current_segment\tSEGMENT "; | ||
536 | $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; | ||
537 | $v.=" 'CODE'"; | ||
538 | } | ||
539 | $self->{value} = $v; | ||
540 | last; | ||
541 | }; | ||
542 | /\.data/ && do { my $v=undef; | ||
543 | if ($nasm) { | ||
544 | $v="section .data data align=8\n"; | ||
545 | } else { | ||
546 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
547 | $current_segment = "_DATA"; | ||
548 | $v.="$current_segment\tSEGMENT"; | ||
549 | } | ||
550 | $self->{value} = $v; | ||
551 | last; | ||
552 | }; | ||
553 | /\.section/ && do { my $v=undef; | ||
554 | $line =~ s/([^,]*).*/$1/; | ||
555 | $line = ".CRT\$XCU" if ($line eq ".init"); | ||
556 | $line = ".rdata" if ($line eq ".rodata"); | ||
557 | if ($nasm) { | ||
558 | $v="section $line"; | ||
559 | if ($line=~/\.([prx])data/) { | ||
560 | $v.=" rdata align="; | ||
561 | $v.=$1 eq "p"? 4 : 8; | ||
562 | } elsif ($line=~/\.CRT\$/i) { | ||
563 | $v.=" rdata align=8"; | ||
564 | } | ||
565 | } else { | ||
566 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
567 | $v.="$line\tSEGMENT"; | ||
568 | if ($line=~/\.([prx])data/) { | ||
569 | $v.=" READONLY"; | ||
570 | if ($masm>=$masmref) { | ||
571 | if ($1 eq "r") { | ||
572 | $v.=" ALIGN(64)"; | ||
573 | } elsif ($1 eq "p") { | ||
574 | $v.=" ALIGN(4)"; | ||
575 | } else { | ||
576 | $v.=" ALIGN(8)"; | ||
577 | } | ||
578 | } | ||
579 | } elsif ($line=~/\.CRT\$/i) { | ||
580 | $v.=" READONLY "; | ||
581 | $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD"; | ||
582 | } | ||
583 | } | ||
584 | $current_segment = $line; | ||
585 | $self->{value} = $v; | ||
586 | last; | ||
587 | }; | ||
588 | /\.extern/ && do { $self->{value} = "EXTERN\t".$line; | ||
589 | $self->{value} .= ":NEAR" if ($masm); | ||
590 | last; | ||
591 | }; | ||
592 | /\.globl|.global/ | ||
593 | && do { $self->{value} = $masm?"PUBLIC":"global"; | ||
594 | $self->{value} .= "\t".$line; | ||
595 | last; | ||
596 | }; | ||
597 | /\.size/ && do { if (defined($current_function)) { | ||
598 | undef $self->{value}; | ||
599 | if ($current_function->{abi} eq "svr4") { | ||
600 | $self->{value}="${decor}SEH_end_$current_function->{name}:"; | ||
601 | $self->{value}.=":\n" if($masm); | ||
602 | } | ||
603 | $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name}); | ||
604 | undef $current_function; | ||
605 | } | ||
606 | last; | ||
607 | }; | ||
608 | /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; | ||
609 | /\.(value|long|rva|quad)/ | ||
610 | && do { my $sz = substr($1,0,1); | ||
611 | my @arr = split(/,\s*/,$line); | ||
612 | my $last = pop(@arr); | ||
613 | my $conv = sub { my $var=shift; | ||
614 | $var=~s/^(0b[0-1]+)/oct($1)/eig; | ||
615 | $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm); | ||
616 | if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva")) | ||
617 | { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; } | ||
618 | $var; | ||
619 | }; | ||
620 | |||
621 | $sz =~ tr/bvlrq/BWDDQ/; | ||
622 | $self->{value} = "\tD$sz\t"; | ||
623 | for (@arr) { $self->{value} .= &$conv($_).","; } | ||
624 | $self->{value} .= &$conv($last); | ||
625 | last; | ||
626 | }; | ||
627 | /\.byte/ && do { my @str=split(/,\s*/,$line); | ||
628 | map(s/(0b[0-1]+)/oct($1)/eig,@str); | ||
629 | map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm); | ||
630 | while ($#str>15) { | ||
631 | $self->{value}.="DB\t" | ||
632 | .join(",",@str[0..15])."\n"; | ||
633 | foreach (0..15) { shift @str; } | ||
634 | } | ||
635 | $self->{value}.="DB\t" | ||
636 | .join(",",@str) if (@str); | ||
637 | last; | ||
638 | }; | ||
639 | /\.comm/ && do { my @str=split(/,\s*/,$line); | ||
640 | my $v=undef; | ||
641 | if ($nasm) { | ||
642 | $v.="common $prefix@str[0] @str[1]"; | ||
643 | } else { | ||
644 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
645 | $current_segment = "_DATA"; | ||
646 | $v.="$current_segment\tSEGMENT\n"; | ||
647 | $v.="COMM @str[0]:DWORD:".@str[1]/4; | ||
648 | } | ||
649 | $self->{value} = $v; | ||
650 | last; | ||
651 | }; | ||
652 | } | ||
653 | $line = ""; | ||
654 | } | ||
655 | |||
656 | $ret; | ||
657 | } | ||
658 | sub out { | ||
659 | my $self = shift; | ||
660 | $self->{value}; | ||
661 | } | ||
662 | } | ||
663 | |||
664 | sub rex { | ||
665 | local *opcode=shift; | ||
666 | my ($dst,$src,$rex)=@_; | ||
667 | |||
668 | $rex|=0x04 if($dst>=8); | ||
669 | $rex|=0x01 if($src>=8); | ||
670 | push @opcode,($rex|0x40) if ($rex); | ||
671 | } | ||
672 | |||
673 | # older gas and ml64 don't handle SSE>2 instructions | ||
674 | my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3, | ||
675 | "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 ); | ||
676 | |||
677 | if ($flavour ne "openbsd") { | ||
678 | |||
679 | $movq = sub { # elderly gas can't handle inter-register movq | ||
680 | my $arg = shift; | ||
681 | my @opcode=(0x66); | ||
682 | if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) { | ||
683 | my ($src,$dst)=($1,$2); | ||
684 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } | ||
685 | rex(\@opcode,$src,$dst,0x8); | ||
686 | push @opcode,0x0f,0x7e; | ||
687 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M | ||
688 | @opcode; | ||
689 | } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) { | ||
690 | my ($src,$dst)=($2,$1); | ||
691 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } | ||
692 | rex(\@opcode,$src,$dst,0x8); | ||
693 | push @opcode,0x0f,0x6e; | ||
694 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M | ||
695 | @opcode; | ||
696 | } else { | ||
697 | (); | ||
698 | } | ||
699 | }; | ||
700 | |||
701 | } | ||
702 | |||
703 | my $pextrd = sub { | ||
704 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) { | ||
705 | my @opcode=(0x66); | ||
706 | $imm=$1; | ||
707 | $src=$2; | ||
708 | $dst=$3; | ||
709 | if ($dst =~ /%r([0-9]+)d/) { $dst = $1; } | ||
710 | elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; } | ||
711 | rex(\@opcode,$src,$dst); | ||
712 | push @opcode,0x0f,0x3a,0x16; | ||
713 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M | ||
714 | push @opcode,$imm; | ||
715 | @opcode; | ||
716 | } else { | ||
717 | (); | ||
718 | } | ||
719 | }; | ||
720 | |||
721 | my $pinsrd = sub { | ||
722 | if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) { | ||
723 | my @opcode=(0x66); | ||
724 | $imm=$1; | ||
725 | $src=$2; | ||
726 | $dst=$3; | ||
727 | if ($src =~ /%r([0-9]+)/) { $src = $1; } | ||
728 | elsif ($src =~ /%e/) { $src = $regrm{$src}; } | ||
729 | rex(\@opcode,$dst,$src); | ||
730 | push @opcode,0x0f,0x3a,0x22; | ||
731 | push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M | ||
732 | push @opcode,$imm; | ||
733 | @opcode; | ||
734 | } else { | ||
735 | (); | ||
736 | } | ||
737 | }; | ||
738 | |||
739 | if ($flavour ne "openbsd") { | ||
740 | |||
741 | $pshufb = sub { | ||
742 | if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) { | ||
743 | my @opcode=(0x66); | ||
744 | rex(\@opcode,$2,$1); | ||
745 | push @opcode,0x0f,0x38,0x00; | ||
746 | push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M | ||
747 | @opcode; | ||
748 | } else { | ||
749 | (); | ||
750 | } | ||
751 | }; | ||
752 | |||
753 | $palignr = sub { | ||
754 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { | ||
755 | my @opcode=(0x66); | ||
756 | rex(\@opcode,$3,$2); | ||
757 | push @opcode,0x0f,0x3a,0x0f; | ||
758 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M | ||
759 | push @opcode,$1; | ||
760 | @opcode; | ||
761 | } else { | ||
762 | (); | ||
763 | } | ||
764 | }; | ||
765 | |||
766 | $pclmulqdq = sub { | ||
767 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { | ||
768 | my @opcode=(0x66); | ||
769 | rex(\@opcode,$3,$2); | ||
770 | push @opcode,0x0f,0x3a,0x44; | ||
771 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M | ||
772 | my $c=$1; | ||
773 | push @opcode,$c=~/^0/?oct($c):$c; | ||
774 | @opcode; | ||
775 | } else { | ||
776 | (); | ||
777 | } | ||
778 | }; | ||
779 | |||
780 | } | ||
781 | |||
782 | if ($nasm) { | ||
783 | print <<___; | ||
784 | default rel | ||
785 | %define XMMWORD | ||
786 | ___ | ||
787 | } elsif ($masm) { | ||
788 | print <<___; | ||
789 | OPTION DOTNAME | ||
790 | ___ | ||
791 | } | ||
792 | |||
793 | if ($nasm) { | ||
794 | print <<___; | ||
795 | \%define _CET_ENDBR | ||
796 | ___ | ||
797 | } else { | ||
798 | print <<___; | ||
799 | #if defined(__CET__) | ||
800 | #include <cet.h> | ||
801 | #else | ||
802 | #define _CET_ENDBR | ||
803 | #endif | ||
804 | |||
805 | ___ | ||
806 | } | ||
807 | |||
808 | print "#include \"x86_arch.h\"\n"; | ||
809 | |||
810 | while($line=<>) { | ||
811 | |||
812 | chomp($line); | ||
813 | |||
814 | $line =~ s|[#!].*$||; # get rid of asm-style comments... | ||
815 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
816 | $line =~ s|^\s+||; # ... and skip white spaces in beginning | ||
817 | |||
818 | undef $label; | ||
819 | undef $opcode; | ||
820 | undef @args; | ||
821 | |||
822 | if ($label=label->re(\$line)) { print $label->out(); } | ||
823 | |||
824 | if (directive->re(\$line)) { | ||
825 | printf "%s",directive->out(); | ||
826 | } elsif ($opcode=opcode->re(\$line)) { | ||
827 | my $asm = eval("\$".$opcode->mnemonic()); | ||
828 | undef @bytes; | ||
829 | |||
830 | if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) { | ||
831 | print $gas?".byte\t":"DB\t",join(',',@bytes),"\n"; | ||
832 | next; | ||
833 | } | ||
834 | |||
835 | ARGUMENT: while (1) { | ||
836 | my $arg; | ||
837 | |||
838 | if ($arg=register->re(\$line)) { opcode->size($arg->size()); } | ||
839 | elsif ($arg=const->re(\$line)) { } | ||
840 | elsif ($arg=ea->re(\$line)) { } | ||
841 | elsif ($arg=expr->re(\$line)) { } | ||
842 | else { last ARGUMENT; } | ||
843 | |||
844 | push @args,$arg; | ||
845 | |||
846 | last ARGUMENT if ($line !~ /^,/); | ||
847 | |||
848 | $line =~ s/^,\s*//; | ||
849 | } # ARGUMENT: | ||
850 | |||
851 | if ($#args>=0) { | ||
852 | my $insn; | ||
853 | my $sz=opcode->size(); | ||
854 | |||
855 | if ($gas) { | ||
856 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); | ||
857 | @args = map($_->out($sz),@args); | ||
858 | printf "\t%s\t%s",$insn,join(",",@args); | ||
859 | } else { | ||
860 | $insn = $opcode->out(); | ||
861 | foreach (@args) { | ||
862 | my $arg = $_->out(); | ||
863 | # $insn.=$sz compensates for movq, pinsrw, ... | ||
864 | if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; } | ||
865 | if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; } | ||
866 | } | ||
867 | @args = reverse(@args); | ||
868 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); | ||
869 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); | ||
870 | } | ||
871 | } else { | ||
872 | printf "\t%s",$opcode->out(); | ||
873 | } | ||
874 | } | ||
875 | |||
876 | print $line,"\n"; | ||
877 | } | ||
878 | |||
879 | print "\n$current_segment\tENDS\n" if ($current_segment && $masm); | ||
880 | print "END\n" if ($masm); | ||
881 | |||
882 | close STDOUT; | ||
883 | |||
884 | ################################################# | ||
885 | # Cross-reference x86_64 ABI "card" | ||
886 | # | ||
887 | # Unix Win64 | ||
888 | # %rax * * | ||
889 | # %rbx - - | ||
890 | # %rcx #4 #1 | ||
891 | # %rdx #3 #2 | ||
892 | # %rsi #2 - | ||
893 | # %rdi #1 - | ||
894 | # %rbp - - | ||
895 | # %rsp - - | ||
896 | # %r8 #5 #3 | ||
897 | # %r9 #6 #4 | ||
898 | # %r10 * * | ||
899 | # %r11 * * | ||
900 | # %r12 - - | ||
901 | # %r13 - - | ||
902 | # %r14 - - | ||
903 | # %r15 - - | ||
904 | # | ||
905 | # (*) volatile register | ||
906 | # (-) preserved by callee | ||
907 | # (#) Nth argument, volatile | ||
908 | # | ||
909 | # In Unix terms top of stack is argument transfer area for arguments | ||
910 | # which could not be accommodated in registers. Or in other words 7th | ||
911 | # [integer] argument resides at 8(%rsp) upon function entry point. | ||
912 | # 128 bytes above %rsp constitute a "red zone" which is not touched | ||
913 | # by signal handlers and can be used as temporal storage without | ||
914 | # allocating a frame. | ||
915 | # | ||
916 | # In Win64 terms N*8 bytes on top of stack is argument transfer area, | ||
917 | # which belongs to/can be overwritten by callee. N is the number of | ||
918 | # arguments passed to callee, *but* not less than 4! This means that | ||
919 | # upon function entry point 5th argument resides at 40(%rsp), as well | ||
920 | # as that 32 bytes from 8(%rsp) can always be used as temporal | ||
921 | # storage [without allocating a frame]. One can actually argue that | ||
922 | # one can assume a "red zone" above stack pointer under Win64 as well. | ||
923 | # Point is that at apparently no occasion Windows kernel would alter | ||
924 | # the area above user stack pointer in true asynchronous manner... | ||
925 | # | ||
926 | # All the above means that if assembler programmer adheres to Unix | ||
927 | # register and stack layout, but disregards the "red zone" existence, | ||
928 | # it's possible to use following prologue and epilogue to "gear" from | ||
929 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments. | ||
930 | # | ||
931 | # omnipotent_function: | ||
932 | # ifdef WIN64 | ||
933 | # movq %rdi,8(%rsp) | ||
934 | # movq %rsi,16(%rsp) | ||
935 | # movq %rcx,%rdi ; if 1st argument is actually present | ||
936 | # movq %rdx,%rsi ; if 2nd argument is actually ... | ||
937 | # movq %r8,%rdx ; if 3rd argument is ... | ||
938 | # movq %r9,%rcx ; if 4th argument ... | ||
939 | # movq 40(%rsp),%r8 ; if 5th ... | ||
940 | # movq 48(%rsp),%r9 ; if 6th ... | ||
941 | # endif | ||
942 | # ... | ||
943 | # ifdef WIN64 | ||
944 | # movq 8(%rsp),%rdi | ||
945 | # movq 16(%rsp),%rsi | ||
946 | # endif | ||
947 | # ret | ||
948 | # | ||
949 | ################################################# | ||
950 | # Win64 SEH, Structured Exception Handling. | ||
951 | # | ||
952 | # Unlike on Unix systems(*) lack of Win64 stack unwinding information | ||
953 | # has undesired side-effect at run-time: if an exception is raised in | ||
954 | # assembler subroutine such as those in question (basically we're | ||
955 | # referring to segmentation violations caused by malformed input | ||
956 | # parameters), the application is briskly terminated without invoking | ||
957 | # any exception handlers, most notably without generating memory dump | ||
958 | # or any user notification whatsoever. This poses a problem. It's | ||
959 | # possible to address it by registering custom language-specific | ||
960 | # handler that would restore processor context to the state at | ||
961 | # subroutine entry point and return "exception is not handled, keep | ||
962 | # unwinding" code. Writing such handler can be a challenge... But it's | ||
963 | # doable, though requires certain coding convention. Consider following | ||
964 | # snippet: | ||
965 | # | ||
966 | # .type function,@function | ||
967 | # function: | ||
968 | # movq %rsp,%rax # copy rsp to volatile register | ||
969 | # pushq %r15 # save non-volatile registers | ||
970 | # pushq %rbx | ||
971 | # pushq %rbp | ||
972 | # movq %rsp,%r11 | ||
973 | # subq %rdi,%r11 # prepare [variable] stack frame | ||
974 | # andq $-64,%r11 | ||
975 | # movq %rax,0(%r11) # check for exceptions | ||
976 | # movq %r11,%rsp # allocate [variable] stack frame | ||
977 | # movq %rax,0(%rsp) # save original rsp value | ||
978 | # magic_point: | ||
979 | # ... | ||
980 | # movq 0(%rsp),%rcx # pull original rsp value | ||
981 | # movq -24(%rcx),%rbp # restore non-volatile registers | ||
982 | # movq -16(%rcx),%rbx | ||
983 | # movq -8(%rcx),%r15 | ||
984 | # movq %rcx,%rsp # restore original rsp | ||
985 | # ret | ||
986 | # .size function,.-function | ||
987 | # | ||
988 | # The key is that up to magic_point copy of original rsp value remains | ||
989 | # in chosen volatile register and no non-volatile register, except for | ||
990 | # rsp, is modified. While past magic_point rsp remains constant till | ||
991 | # the very end of the function. In this case custom language-specific | ||
992 | # exception handler would look like this: | ||
993 | # | ||
994 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, | ||
995 | # CONTEXT *context,DISPATCHER_CONTEXT *disp) | ||
996 | # { ULONG64 *rsp = (ULONG64 *)context->Rax; | ||
997 | # if (context->Rip >= magic_point) | ||
998 | # { rsp = ((ULONG64 **)context->Rsp)[0]; | ||
999 | # context->Rbp = rsp[-3]; | ||
1000 | # context->Rbx = rsp[-2]; | ||
1001 | # context->R15 = rsp[-1]; | ||
1002 | # } | ||
1003 | # context->Rsp = (ULONG64)rsp; | ||
1004 | # context->Rdi = rsp[1]; | ||
1005 | # context->Rsi = rsp[2]; | ||
1006 | # | ||
1007 | # memcpy (disp->ContextRecord,context,sizeof(CONTEXT)); | ||
1008 | # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase, | ||
1009 | # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord, | ||
1010 | # &disp->HandlerData,&disp->EstablisherFrame,NULL); | ||
1011 | # return ExceptionContinueSearch; | ||
1012 | # } | ||
1013 | # | ||
1014 | # It's appropriate to implement this handler in assembler, directly in | ||
1015 | # function's module. In order to do that one has to know members' | ||
1016 | # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant | ||
1017 | # values. Here they are: | ||
1018 | # | ||
1019 | # CONTEXT.Rax 120 | ||
1020 | # CONTEXT.Rcx 128 | ||
1021 | # CONTEXT.Rdx 136 | ||
1022 | # CONTEXT.Rbx 144 | ||
1023 | # CONTEXT.Rsp 152 | ||
1024 | # CONTEXT.Rbp 160 | ||
1025 | # CONTEXT.Rsi 168 | ||
1026 | # CONTEXT.Rdi 176 | ||
1027 | # CONTEXT.R8 184 | ||
1028 | # CONTEXT.R9 192 | ||
1029 | # CONTEXT.R10 200 | ||
1030 | # CONTEXT.R11 208 | ||
1031 | # CONTEXT.R12 216 | ||
1032 | # CONTEXT.R13 224 | ||
1033 | # CONTEXT.R14 232 | ||
1034 | # CONTEXT.R15 240 | ||
1035 | # CONTEXT.Rip 248 | ||
1036 | # CONTEXT.Xmm6 512 | ||
1037 | # sizeof(CONTEXT) 1232 | ||
1038 | # DISPATCHER_CONTEXT.ControlPc 0 | ||
1039 | # DISPATCHER_CONTEXT.ImageBase 8 | ||
1040 | # DISPATCHER_CONTEXT.FunctionEntry 16 | ||
1041 | # DISPATCHER_CONTEXT.EstablisherFrame 24 | ||
1042 | # DISPATCHER_CONTEXT.TargetIp 32 | ||
1043 | # DISPATCHER_CONTEXT.ContextRecord 40 | ||
1044 | # DISPATCHER_CONTEXT.LanguageHandler 48 | ||
1045 | # DISPATCHER_CONTEXT.HandlerData 56 | ||
1046 | # UNW_FLAG_NHANDLER 0 | ||
1047 | # ExceptionContinueSearch 1 | ||
1048 | # | ||
1049 | # In order to tie the handler to the function one has to compose | ||
1050 | # couple of structures: one for .xdata segment and one for .pdata. | ||
1051 | # | ||
1052 | # UNWIND_INFO structure for .xdata segment would be | ||
1053 | # | ||
1054 | # function_unwind_info: | ||
1055 | # .byte 9,0,0,0 | ||
1056 | # .rva handler | ||
1057 | # | ||
1058 | # This structure designates exception handler for a function with | ||
1059 | # zero-length prologue, no stack frame or frame register. | ||
1060 | # | ||
1061 | # To facilitate composing of .pdata structures, auto-generated "gear" | ||
1062 | # prologue copies rsp value to rax and denotes next instruction with | ||
1063 | # .LSEH_begin_{function_name} label. This essentially defines the SEH | ||
1064 | # styling rule mentioned in the beginning. Position of this label is | ||
1065 | # chosen in such manner that possible exceptions raised in the "gear" | ||
1066 | # prologue would be accounted to caller and unwound from latter's frame. | ||
1067 | # End of function is marked with respective .LSEH_end_{function_name} | ||
1068 | # label. To summarize, .pdata segment would contain | ||
1069 | # | ||
1070 | # .rva .LSEH_begin_function | ||
1071 | # .rva .LSEH_end_function | ||
1072 | # .rva function_unwind_info | ||
1073 | # | ||
1074 | # Reference to functon_unwind_info from .xdata segment is the anchor. | ||
1075 | # In case you wonder why references are 32-bit .rvas and not 64-bit | ||
1076 | # .quads. References put into these two segments are required to be | ||
1077 | # *relative* to the base address of the current binary module, a.k.a. | ||
1078 | # image base. No Win64 module, be it .exe or .dll, can be larger than | ||
1079 | # 2GB and thus such relative references can be and are accommodated in | ||
1080 | # 32 bits. | ||
1081 | # | ||
1082 | # Having reviewed the example function code, one can argue that "movq | ||
1083 | # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix | ||
1084 | # rax would contain an undefined value. If this "offends" you, use | ||
1085 | # another register and refrain from modifying rax till magic_point is | ||
1086 | # reached, i.e. as if it was a non-volatile register. If more registers | ||
1087 | # are required prior [variable] frame setup is completed, note that | ||
1088 | # nobody says that you can have only one "magic point." You can | ||
1089 | # "liberate" non-volatile registers by denoting last stack off-load | ||
1090 | # instruction and reflecting it in finer grade unwind logic in handler. | ||
1091 | # After all, isn't it why it's called *language-specific* handler... | ||
1092 | # | ||
1093 | # Attentive reader can notice that exceptions would be mishandled in | ||
1094 | # auto-generated "gear" epilogue. Well, exception effectively can't | ||
1095 | # occur there, because if memory area used by it was subject to | ||
1096 | # segmentation violation, then it would be raised upon call to the | ||
1097 | # function (and as already mentioned be accounted to caller, which is | ||
1098 | # not a problem). If you're still not comfortable, then define tail | ||
1099 | # "magic point" just prior ret instruction and have handler treat it... | ||
1100 | # | ||
1101 | # (*) Note that we're talking about run-time, not debug-time. Lack of | ||
1102 | # unwind information makes debugging hard on both Windows and | ||
1103 | # Unix. "Unlike" referes to the fact that on Unix signal handler | ||
1104 | # will always be invoked, core dumped and appropriate exit code | ||
1105 | # returned to parent (for user notification). | ||
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl deleted file mode 100644 index 7e72707684..0000000000 --- a/src/lib/libcrypto/perlasm/x86asm.pl +++ /dev/null | |||
@@ -1,257 +0,0 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | # require 'x86asm.pl'; | ||
4 | # &asm_init(<flavor>,"aes-586.pl"[,$x86only]); | ||
5 | # &function_begin("foo"); | ||
6 | # ... | ||
7 | # &function_end("foo"); | ||
8 | # &asm_finish | ||
9 | |||
10 | $out=(); | ||
11 | $i386=0; | ||
12 | |||
13 | # AUTOLOAD is this context has quite unpleasant side effect, namely | ||
14 | # that typos in function calls effectively go to assembler output, | ||
15 | # but on the pros side we don't have to implement one subroutine per | ||
16 | # each opcode... | ||
17 | sub ::AUTOLOAD | ||
18 | { my $opcode = $AUTOLOAD; | ||
19 | |||
20 | die "more than 4 arguments passed to $opcode" if ($#_>3); | ||
21 | |||
22 | $opcode =~ s/.*:://; | ||
23 | if ($opcode =~ /^push/) { $stack+=4; } | ||
24 | elsif ($opcode =~ /^pop/) { $stack-=4; } | ||
25 | |||
26 | &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD"; | ||
27 | } | ||
28 | |||
29 | sub ::emit | ||
30 | { my $opcode=shift; | ||
31 | |||
32 | if ($#_==-1) { push(@out,"\t$opcode\n"); } | ||
33 | else { push(@out,"\t$opcode\t".join(',',@_)."\n"); } | ||
34 | } | ||
35 | |||
36 | sub ::emitraw | ||
37 | { my $opcode=shift; | ||
38 | |||
39 | if ($#_==-1) { push(@out,"$opcode\n"); } | ||
40 | else { push(@out,"$opcode\t".join(',',@_)."\n"); } | ||
41 | } | ||
42 | |||
43 | sub ::LB | ||
44 | { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'"; | ||
45 | $1."l"; | ||
46 | } | ||
47 | sub ::HB | ||
48 | { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'"; | ||
49 | $1."h"; | ||
50 | } | ||
51 | sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); } | ||
52 | sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); } | ||
53 | sub ::blindpop { &pop($_[0]); $stack+=4; } | ||
54 | sub ::wparam { &DWP($stack+4*$_[0],"esp"); } | ||
55 | sub ::swtmp { &DWP(4*$_[0],"esp"); } | ||
56 | |||
57 | sub ::bswap | ||
58 | { if ($i386) # emulate bswap for i386 | ||
59 | { &comment("bswap @_"); | ||
60 | &xchg(&HB(@_),&LB(@_)); | ||
61 | &ror (@_,16); | ||
62 | &xchg(&HB(@_),&LB(@_)); | ||
63 | } | ||
64 | else | ||
65 | { &generic("bswap",@_); } | ||
66 | } | ||
67 | # These are made-up opcodes introduced over the years essentially | ||
68 | # by ignorance, just alias them to real ones... | ||
69 | sub ::movb { &mov(@_); } | ||
70 | sub ::xorb { &xor(@_); } | ||
71 | sub ::rotl { &rol(@_); } | ||
72 | sub ::rotr { &ror(@_); } | ||
73 | sub ::exch { &xchg(@_); } | ||
74 | sub ::halt { &hlt; } | ||
75 | sub ::movz { &movzx(@_); } | ||
76 | sub ::pushf { &pushfd; } | ||
77 | sub ::popf { &popfd; } | ||
78 | |||
79 | # 3 argument instructions | ||
80 | sub ::movq | ||
81 | { my($p1,$p2,$optimize)=@_; | ||
82 | |||
83 | if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/) | ||
84 | # movq between mmx registers can sink Intel CPUs | ||
85 | { &::pshufw($p1,$p2,0xe4); } | ||
86 | else | ||
87 | { &::generic("movq",@_); } | ||
88 | } | ||
89 | |||
90 | # SSE>2 instructions | ||
91 | my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3, | ||
92 | "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 ); | ||
93 | sub ::pextrd | ||
94 | { my($dst,$src,$imm)=@_; | ||
95 | if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/) | ||
96 | { &::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm); } | ||
97 | else | ||
98 | { &::generic("pextrd",@_); } | ||
99 | } | ||
100 | |||
101 | sub ::pinsrd | ||
102 | { my($dst,$src,$imm)=@_; | ||
103 | if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/) | ||
104 | { &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); } | ||
105 | else | ||
106 | { &::generic("pinsrd",@_); } | ||
107 | } | ||
108 | |||
109 | sub ::pshufb | ||
110 | { my($dst,$src)=@_; | ||
111 | if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/) | ||
112 | { &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); } | ||
113 | else | ||
114 | { &::generic("pshufb",@_); } | ||
115 | } | ||
116 | |||
117 | sub ::palignr | ||
118 | { my($dst,$src,$imm)=@_; | ||
119 | if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/) | ||
120 | { &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); } | ||
121 | else | ||
122 | { &::generic("palignr",@_); } | ||
123 | } | ||
124 | |||
125 | sub ::pclmulqdq | ||
126 | { my($dst,$src,$imm)=@_; | ||
127 | if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/) | ||
128 | { &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); } | ||
129 | else | ||
130 | { &::generic("pclmulqdq",@_); } | ||
131 | } | ||
132 | |||
133 | # label management | ||
134 | $lbdecor="L"; # local label decoration, set by package | ||
135 | $label="000"; | ||
136 | |||
137 | sub ::islabel # see is argument is a known label | ||
138 | { my $i; | ||
139 | foreach $i (values %label) { return $i if ($i eq $_[0]); } | ||
140 | $label{$_[0]}; # can be undef | ||
141 | } | ||
142 | |||
143 | sub ::label # instantiate a function-scope label | ||
144 | { if (!defined($label{$_[0]})) | ||
145 | { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; } | ||
146 | $label{$_[0]}; | ||
147 | } | ||
148 | |||
149 | sub ::LABEL # instantiate a file-scope label | ||
150 | { $label{$_[0]}=$_[1] if (!defined($label{$_[0]})); | ||
151 | $label{$_[0]}; | ||
152 | } | ||
153 | |||
154 | sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); } | ||
155 | |||
156 | sub ::set_label_B { push(@out,"@_:\n"); } | ||
157 | sub ::set_label | ||
158 | { my $label=&::label($_[0]); | ||
159 | &::align($_[1]) if ($_[1]>1); | ||
160 | &::set_label_B($label); | ||
161 | $label; | ||
162 | } | ||
163 | |||
164 | sub ::wipe_labels # wipes function-scope labels | ||
165 | { foreach $i (keys %label) | ||
166 | { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); } | ||
167 | } | ||
168 | |||
169 | # subroutine management | ||
170 | sub ::function_begin | ||
171 | { &function_begin_B(@_); | ||
172 | $stack=4; | ||
173 | &push("ebp"); | ||
174 | &push("ebx"); | ||
175 | &push("esi"); | ||
176 | &push("edi"); | ||
177 | } | ||
178 | |||
179 | sub ::function_end | ||
180 | { &pop("edi"); | ||
181 | &pop("esi"); | ||
182 | &pop("ebx"); | ||
183 | &pop("ebp"); | ||
184 | &ret(); | ||
185 | &function_end_B(@_); | ||
186 | $stack=0; | ||
187 | &wipe_labels(); | ||
188 | } | ||
189 | |||
190 | sub ::function_end_A | ||
191 | { &pop("edi"); | ||
192 | &pop("esi"); | ||
193 | &pop("ebx"); | ||
194 | &pop("ebp"); | ||
195 | &ret(); | ||
196 | $stack+=16; # readjust esp as if we didn't pop anything | ||
197 | } | ||
198 | |||
199 | sub ::asciz | ||
200 | { my @str=unpack("C*",shift); | ||
201 | push @str,0; | ||
202 | while ($#str>15) { | ||
203 | &data_byte(@str[0..15]); | ||
204 | foreach (0..15) { shift @str; } | ||
205 | } | ||
206 | &data_byte(@str) if (@str); | ||
207 | } | ||
208 | |||
209 | sub ::asm_finish | ||
210 | { &file_end(); | ||
211 | print @out; | ||
212 | } | ||
213 | |||
214 | sub ::asm_init | ||
215 | { my ($type,$fn,$cpu)=@_; | ||
216 | |||
217 | $filename=$fn; | ||
218 | $i386=$cpu; | ||
219 | |||
220 | $elf=$cpp=$coff=$aout=$macosx=$win32=$openbsd=$android=0; | ||
221 | if (($type eq "elf")) | ||
222 | { $elf=1; require "x86gas.pl"; } | ||
223 | elsif (($type eq "a\.out")) | ||
224 | { $aout=1; require "x86gas.pl"; } | ||
225 | elsif (($type eq "coff" or $type eq "gaswin")) | ||
226 | { $coff=1; require "x86gas.pl"; } | ||
227 | elsif (($type eq "macosx")) | ||
228 | { $aout=1; $macosx=1; require "x86gas.pl"; } | ||
229 | elsif (($type eq "openbsd-elf")) | ||
230 | { $openbsd=$elf=1; require "x86gas.pl"; } | ||
231 | elsif (($type eq "openbsd-a.out")) | ||
232 | { $openbsd=1; require "x86gas.pl"; } | ||
233 | elsif (($type eq "android")) | ||
234 | { $elf=1; $android=1; require "x86gas.pl"; } | ||
235 | else | ||
236 | { print STDERR <<"EOF"; | ||
237 | Pick one target type from | ||
238 | elf - Linux, FreeBSD, Solaris x86, etc. | ||
239 | a.out - DJGPP, elder OpenBSD, etc. | ||
240 | coff - GAS/COFF such as Win32 targets | ||
241 | openbsd-elf - OpenBSD elf | ||
242 | openbsd-a.out - OpenBSD a.out | ||
243 | macosx - Mac OS X | ||
244 | EOF | ||
245 | exit(1); | ||
246 | } | ||
247 | |||
248 | $pic=0; | ||
249 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
250 | |||
251 | ::emitraw("#include \"x86_arch.h\"\n"); | ||
252 | ::emitraw("#include <machine/asm.h>\n") if $openbsd; | ||
253 | $filename =~ s/\.pl$//; | ||
254 | &file($filename); | ||
255 | } | ||
256 | |||
257 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86gas.pl b/src/lib/libcrypto/perlasm/x86gas.pl deleted file mode 100644 index b84e28be97..0000000000 --- a/src/lib/libcrypto/perlasm/x86gas.pl +++ /dev/null | |||
@@ -1,315 +0,0 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | package x86gas; | ||
4 | |||
5 | *out=\@::out; | ||
6 | |||
7 | $::lbdecor=$::aout?"L":".L"; # local label decoration | ||
8 | $nmdecor=($::aout or $::coff)?"_":""; # external name decoration | ||
9 | |||
10 | $initseg=""; | ||
11 | |||
12 | $align=16; | ||
13 | $align=log($align)/log(2) if ($::aout); | ||
14 | $com_start="#" if ($::aout or $::coff); | ||
15 | |||
16 | sub opsize() | ||
17 | { my $reg=shift; | ||
18 | if ($reg =~ m/^%e/o) { "l"; } | ||
19 | elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; } | ||
20 | elsif ($reg =~ m/^%[xm]/o) { undef; } | ||
21 | else { "w"; } | ||
22 | } | ||
23 | |||
24 | # swap arguments; | ||
25 | # expand opcode with size suffix; | ||
26 | # prefix numeric constants with $; | ||
27 | sub ::generic | ||
28 | { my($opcode,@arg)=@_; | ||
29 | my($suffix,$dst,$src); | ||
30 | |||
31 | @arg=reverse(@arg); | ||
32 | |||
33 | for (@arg) | ||
34 | { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers | ||
35 | s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers | ||
36 | s/^(\-?[0-9]+)$/\$$1/o; # constants | ||
37 | s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants | ||
38 | } | ||
39 | |||
40 | $dst = $arg[$#arg] if ($#arg>=0); | ||
41 | $src = $arg[$#arg-1] if ($#arg>=1); | ||
42 | if ($dst =~ m/^%/o) { $suffix=&opsize($dst); } | ||
43 | elsif ($src =~ m/^%/o) { $suffix=&opsize($src); } | ||
44 | else { $suffix="l"; } | ||
45 | undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o); | ||
46 | |||
47 | if ($#_==0) { &::emit($opcode); } | ||
48 | elsif ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o) | ||
49 | { &::emit($opcode,@arg); } | ||
50 | else { &::emit($opcode.$suffix,@arg);} | ||
51 | |||
52 | 1; | ||
53 | } | ||
54 | # | ||
55 | # opcodes not covered by ::generic above, mostly inconsistent namings... | ||
56 | # | ||
57 | sub ::movzx { &::movzb(@_); } | ||
58 | sub ::pushfd { &::pushfl; } | ||
59 | sub ::popfd { &::popfl; } | ||
60 | sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); } | ||
61 | sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); } | ||
62 | |||
63 | sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } | ||
64 | sub ::call_ptr { &::generic("call","*$_[0]"); } | ||
65 | sub ::jmp_ptr { &::generic("jmp","*$_[0]"); } | ||
66 | |||
67 | *::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386); | ||
68 | |||
69 | sub ::DWP | ||
70 | { my($addr,$reg1,$reg2,$idx)=@_; | ||
71 | my $ret=""; | ||
72 | |||
73 | $addr =~ s/^\s+//; | ||
74 | # prepend global references with optional underscore | ||
75 | $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige; | ||
76 | |||
77 | $reg1 = "%$reg1" if ($reg1); | ||
78 | $reg2 = "%$reg2" if ($reg2); | ||
79 | |||
80 | $ret .= $addr if (($addr ne "") && ($addr ne 0)); | ||
81 | |||
82 | if ($reg2) | ||
83 | { $idx!= 0 or $idx=1; | ||
84 | $ret .= "($reg1,$reg2,$idx)"; | ||
85 | } | ||
86 | elsif ($reg1) | ||
87 | { $ret .= "($reg1)"; } | ||
88 | |||
89 | $ret; | ||
90 | } | ||
91 | sub ::QWP { &::DWP(@_); } | ||
92 | sub ::BP { &::DWP(@_); } | ||
93 | sub ::WP { &::DWP(@_); } | ||
94 | sub ::BC { @_; } | ||
95 | sub ::DWC { @_; } | ||
96 | |||
97 | sub ::file | ||
98 | { push(@out,".file\t\"$_[0].s\"\n.text\n"); } | ||
99 | |||
100 | sub ::function_begin_B | ||
101 | { my $func=shift; | ||
102 | my $global=($func !~ /^_/); | ||
103 | my $begin="${::lbdecor}_${func}_begin"; | ||
104 | |||
105 | &::LABEL($func,$global?"$begin":"$nmdecor$func"); | ||
106 | $func=$nmdecor.$func; | ||
107 | |||
108 | push(@out,".globl\t$func\n") if ($global); | ||
109 | if ($::coff) | ||
110 | { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); } | ||
111 | elsif (($::aout and !$::pic) or $::macosx) | ||
112 | { } | ||
113 | else | ||
114 | { push(@out,".type $func,\@function\n"); } | ||
115 | push(@out,".align\t$align\n"); | ||
116 | push(@out,"$func:\n"); | ||
117 | push(@out,"$begin:\n") if ($global); | ||
118 | $::stack=4; | ||
119 | } | ||
120 | |||
121 | sub ::function_end_B | ||
122 | { my $func=shift; | ||
123 | push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf); | ||
124 | $::stack=0; | ||
125 | &::wipe_labels(); | ||
126 | } | ||
127 | |||
128 | sub ::comment | ||
129 | { | ||
130 | if (!defined($com_start) or $::elf) | ||
131 | { # Regarding $::elf above... | ||
132 | # GNU and SVR4 as'es use different comment delimiters, | ||
133 | push(@out,"\n"); # so we just skip ELF comments... | ||
134 | return; | ||
135 | } | ||
136 | foreach (@_) | ||
137 | { | ||
138 | if (/^\s*$/) | ||
139 | { push(@out,"\n"); } | ||
140 | else | ||
141 | { push(@out,"\t$com_start $_ $com_end\n"); } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | sub ::external_label | ||
146 | { foreach(@_) { &::LABEL($_,$nmdecor.$_); } } | ||
147 | |||
148 | sub ::public_label | ||
149 | { push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); } | ||
150 | |||
151 | sub ::file_end | ||
152 | { if ($::macosx) | ||
153 | { if (%non_lazy_ptr) | ||
154 | { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n"); | ||
155 | foreach $i (keys %non_lazy_ptr) | ||
156 | { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); } | ||
157 | } | ||
158 | } | ||
159 | if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) { | ||
160 | push (@out, ".extern\t${nmdecor}OPENSSL_ia32cap_P\n"); | ||
161 | push (@out, ".hidden\t${nmdecor}OPENSSL_ia32cap_P\n"); | ||
162 | } | ||
163 | push(@out,$initseg) if ($initseg); | ||
164 | } | ||
165 | |||
166 | sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); } | ||
167 | sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); } | ||
168 | sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); } | ||
169 | |||
170 | sub ::align | ||
171 | { my $val=$_[0],$p2,$i; | ||
172 | if ($::aout) | ||
173 | { for ($p2=0;$val!=0;$val>>=1) { $p2++; } | ||
174 | $val=$p2-1; | ||
175 | $val.=",0x90"; | ||
176 | } | ||
177 | push(@out,".align\t$val\n"); | ||
178 | } | ||
179 | |||
180 | # | ||
181 | # PIC data access wrappers | ||
182 | # | ||
183 | # Usage: | ||
184 | # picsetup($base) | ||
185 | # - only allowed once per function (because of hardcoded label name), | ||
186 | # sets up pic access, uses $base register as temporary | ||
187 | # picsymbol($dst, $sym, $base) | ||
188 | # - loads the address of symbol $sym into $dst with the help of $base | ||
189 | # initialized by picsetup | ||
190 | # picadjust($sym, $base) | ||
191 | # - adjusts a code pointer read from a code_sym table with the help of | ||
192 | # $base initialized by picsetup | ||
193 | # code_sym($sym) | ||
194 | # - emits a pointer to the given code symbol, relative to the GOT if | ||
195 | # PIC. This pointer will need to be adjusted with picadjust above | ||
196 | # before use. | ||
197 | |||
198 | sub ::picsetup | ||
199 | { my($base)=@_; | ||
200 | |||
201 | if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) | ||
202 | { | ||
203 | &::call(&::label("PIC_setup")); | ||
204 | &::set_label("PIC_setup"); | ||
205 | &::blindpop($base); | ||
206 | if ($::macosx) | ||
207 | { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr"); | ||
208 | $non_lazy_ptr{"$nmdecor$sym"}=$indirect; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | |||
213 | sub ::picsymbol | ||
214 | { my($dst,$sym,$base)=@_; | ||
215 | |||
216 | if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) | ||
217 | { | ||
218 | my $reflabel=&::label("PIC_setup"); | ||
219 | if ($::macosx) | ||
220 | { my $indirect=$non_lazy_ptr{"$nmdecor$sym"}; | ||
221 | &::mov($dst,&::DWP("$indirect-$reflabel",$base)); | ||
222 | } | ||
223 | else | ||
224 | { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", | ||
225 | $base)); | ||
226 | &::mov($dst,&::DWP("$sym\@GOT",$dst)); | ||
227 | } | ||
228 | } | ||
229 | else | ||
230 | { &::lea($dst,&::DWP($sym)); } | ||
231 | } | ||
232 | |||
233 | sub ::picadjust | ||
234 | { my($sym,$base)=@_; | ||
235 | |||
236 | if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) | ||
237 | { | ||
238 | my $reflabel=&::label("PIC_setup"); | ||
239 | &::lea($sym,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", | ||
240 | $base,$sym)); | ||
241 | } | ||
242 | } | ||
243 | |||
244 | sub ::code_sym | ||
245 | { my($sym)=@_; | ||
246 | |||
247 | if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) | ||
248 | { | ||
249 | $sym."\@GOTOFF"; | ||
250 | } | ||
251 | else | ||
252 | { | ||
253 | $sym; | ||
254 | } | ||
255 | } | ||
256 | |||
257 | sub ::initseg | ||
258 | { my $f=$nmdecor.shift; | ||
259 | |||
260 | if ($::openbsd) | ||
261 | { $initseg.=<<___; | ||
262 | .section .init | ||
263 | PIC_PROLOGUE | ||
264 | call PIC_PLT($f) | ||
265 | PIC_EPILOGUE | ||
266 | ___ | ||
267 | } elsif ($::android) | ||
268 | { $initseg.=<<___; | ||
269 | .section .init_array | ||
270 | .align 4 | ||
271 | .long $f | ||
272 | ___ | ||
273 | } | ||
274 | elsif ($::elf) | ||
275 | { $initseg.=<<___; | ||
276 | .section .init | ||
277 | call $f | ||
278 | ___ | ||
279 | } | ||
280 | elsif ($::coff) | ||
281 | { $initseg.=<<___; # applies to both Cygwin and Mingw | ||
282 | .section .ctors | ||
283 | .long $f | ||
284 | ___ | ||
285 | } | ||
286 | elsif ($::macosx) | ||
287 | { $initseg.=<<___; | ||
288 | .mod_init_func | ||
289 | .align 2 | ||
290 | .long $f | ||
291 | ___ | ||
292 | } | ||
293 | elsif ($::aout) | ||
294 | { my $ctor="${nmdecor}_GLOBAL_\$I\$$f"; | ||
295 | $initseg.=".text\n"; | ||
296 | $initseg.=".type $ctor,\@function\n" if ($::pic); | ||
297 | $initseg.=<<___; # OpenBSD way... | ||
298 | .globl $ctor | ||
299 | .align 2 | ||
300 | $ctor: | ||
301 | jmp $f | ||
302 | ___ | ||
303 | } | ||
304 | } | ||
305 | |||
306 | sub ::dataseg | ||
307 | { push(@out,".data\n"); } | ||
308 | |||
309 | sub ::rodataseg | ||
310 | { push(@out,".section .rodata\n"); } | ||
311 | |||
312 | sub ::previous | ||
313 | { push(@out,".previous\n"); } | ||
314 | |||
315 | 1; | ||