diff options
author | cvs2svn <admin@example.com> | 2012-07-13 17:49:55 +0000 |
---|---|---|
committer | cvs2svn <admin@example.com> | 2012-07-13 17:49:55 +0000 |
commit | 6fdb436ab2cd5b35066babb3a03be7ad0daf1ae2 (patch) | |
tree | a760cf389e7ea59961bb306a1f50bf5443205176 /src/lib/libcrypto/perlasm | |
parent | 9204e59073bcf27e1487ec4ac46e981902ddd904 (diff) | |
download | openbsd-OPENBSD_5_2_BASE.tar.gz openbsd-OPENBSD_5_2_BASE.tar.bz2 openbsd-OPENBSD_5_2_BASE.zip |
This commit was manufactured by cvs2git to create tag 'OPENBSD_5_2_BASE'.OPENBSD_5_2_BASE
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r-- | src/lib/libcrypto/perlasm/cbc.pl | 349 | ||||
-rwxr-xr-x | src/lib/libcrypto/perlasm/ppc-xlate.pl | 152 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
-rwxr-xr-x | src/lib/libcrypto/perlasm/x86_64-xlate.pl | 920 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 221 | ||||
-rw-r--r-- | src/lib/libcrypto/perlasm/x86gas.pl | 267 |
6 files changed, 0 insertions, 2033 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl deleted file mode 100644 index 6fc2510905..0000000000 --- a/src/lib/libcrypto/perlasm/cbc.pl +++ /dev/null | |||
@@ -1,349 +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 | &function_begin_B($name,""); | ||
38 | &comment(""); | ||
39 | |||
40 | $in="esi"; | ||
41 | $out="edi"; | ||
42 | $count="ebp"; | ||
43 | |||
44 | &push("ebp"); | ||
45 | &push("ebx"); | ||
46 | &push("esi"); | ||
47 | &push("edi"); | ||
48 | |||
49 | $data_off=4; | ||
50 | $data_off+=4 if ($p1 > 0); | ||
51 | $data_off+=4 if ($p2 > 0); | ||
52 | $data_off+=4 if ($p3 > 0); | ||
53 | |||
54 | &mov($count, &wparam(2)); # length | ||
55 | |||
56 | &comment("getting iv ptr from parameter $iv_off"); | ||
57 | &mov("ebx", &wparam($iv_off)); # Get iv ptr | ||
58 | |||
59 | &mov($in, &DWP(0,"ebx","",0));# iv[0] | ||
60 | &mov($out, &DWP(4,"ebx","",0));# iv[1] | ||
61 | |||
62 | &push($out); | ||
63 | &push($in); | ||
64 | &push($out); # used in decrypt for iv[1] | ||
65 | &push($in); # used in decrypt for iv[0] | ||
66 | |||
67 | &mov("ebx", "esp"); # This is the address of tin[2] | ||
68 | |||
69 | &mov($in, &wparam(0)); # in | ||
70 | &mov($out, &wparam(1)); # out | ||
71 | |||
72 | # We have loaded them all, how lets push things | ||
73 | &comment("getting encrypt flag from parameter $enc_off"); | ||
74 | &mov("ecx", &wparam($enc_off)); # Get enc flag | ||
75 | if ($p3 > 0) | ||
76 | { | ||
77 | &comment("get and push parameter $p3"); | ||
78 | if ($enc_off != $p3) | ||
79 | { &mov("eax", &wparam($p3)); &push("eax"); } | ||
80 | else { &push("ecx"); } | ||
81 | } | ||
82 | if ($p2 > 0) | ||
83 | { | ||
84 | &comment("get and push parameter $p2"); | ||
85 | if ($enc_off != $p2) | ||
86 | { &mov("eax", &wparam($p2)); &push("eax"); } | ||
87 | else { &push("ecx"); } | ||
88 | } | ||
89 | if ($p1 > 0) | ||
90 | { | ||
91 | &comment("get and push parameter $p1"); | ||
92 | if ($enc_off != $p1) | ||
93 | { &mov("eax", &wparam($p1)); &push("eax"); } | ||
94 | else { &push("ecx"); } | ||
95 | } | ||
96 | &push("ebx"); # push data/iv | ||
97 | |||
98 | &cmp("ecx",0); | ||
99 | &jz(&label("decrypt")); | ||
100 | |||
101 | &and($count,0xfffffff8); | ||
102 | &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0] | ||
103 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1] | ||
104 | |||
105 | &jz(&label("encrypt_finish")); | ||
106 | |||
107 | ############################################################# | ||
108 | |||
109 | &set_label("encrypt_loop"); | ||
110 | # encrypt start | ||
111 | # "eax" and "ebx" hold iv (or the last cipher text) | ||
112 | |||
113 | &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes | ||
114 | &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes | ||
115 | |||
116 | &xor("eax", "ecx"); | ||
117 | &xor("ebx", "edx"); | ||
118 | |||
119 | &bswap("eax") if $swap; | ||
120 | &bswap("ebx") if $swap; | ||
121 | |||
122 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
123 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
124 | |||
125 | &call($enc_func); | ||
126 | |||
127 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
128 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
129 | |||
130 | &bswap("eax") if $swap; | ||
131 | &bswap("ebx") if $swap; | ||
132 | |||
133 | &mov(&DWP(0,$out,"",0),"eax"); | ||
134 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
135 | |||
136 | # eax and ebx are the next iv. | ||
137 | |||
138 | &add($in, 8); | ||
139 | &add($out, 8); | ||
140 | |||
141 | &sub($count, 8); | ||
142 | &jnz(&label("encrypt_loop")); | ||
143 | |||
144 | ###################################################################3 | ||
145 | &set_label("encrypt_finish"); | ||
146 | &mov($count, &wparam(2)); # length | ||
147 | &and($count, 7); | ||
148 | &jz(&label("finish")); | ||
149 | &call(&label("PIC_point")); | ||
150 | &set_label("PIC_point"); | ||
151 | &blindpop("edx"); | ||
152 | &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx")); | ||
153 | &mov($count,&DWP(0,"ecx",$count,4)) | ||
154 | &add($count,"edx"); | ||
155 | &xor("ecx","ecx"); | ||
156 | &xor("edx","edx"); | ||
157 | #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4)); | ||
158 | &jmp_ptr($count); | ||
159 | |||
160 | &set_label("ej7"); | ||
161 | &movb(&HB("edx"), &BP(6,$in,"",0)); | ||
162 | &shl("edx",8); | ||
163 | &set_label("ej6"); | ||
164 | &movb(&HB("edx"), &BP(5,$in,"",0)); | ||
165 | &set_label("ej5"); | ||
166 | &movb(&LB("edx"), &BP(4,$in,"",0)); | ||
167 | &set_label("ej4"); | ||
168 | &mov("ecx", &DWP(0,$in,"",0)); | ||
169 | &jmp(&label("ejend")); | ||
170 | &set_label("ej3"); | ||
171 | &movb(&HB("ecx"), &BP(2,$in,"",0)); | ||
172 | &shl("ecx",8); | ||
173 | &set_label("ej2"); | ||
174 | &movb(&HB("ecx"), &BP(1,$in,"",0)); | ||
175 | &set_label("ej1"); | ||
176 | &movb(&LB("ecx"), &BP(0,$in,"",0)); | ||
177 | &set_label("ejend"); | ||
178 | |||
179 | &xor("eax", "ecx"); | ||
180 | &xor("ebx", "edx"); | ||
181 | |||
182 | &bswap("eax") if $swap; | ||
183 | &bswap("ebx") if $swap; | ||
184 | |||
185 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
186 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
187 | |||
188 | &call($enc_func); | ||
189 | |||
190 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
191 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
192 | |||
193 | &bswap("eax") if $swap; | ||
194 | &bswap("ebx") if $swap; | ||
195 | |||
196 | &mov(&DWP(0,$out,"",0),"eax"); | ||
197 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
198 | |||
199 | &jmp(&label("finish")); | ||
200 | |||
201 | ############################################################# | ||
202 | ############################################################# | ||
203 | &set_label("decrypt",1); | ||
204 | # decrypt start | ||
205 | &and($count,0xfffffff8); | ||
206 | # The next 2 instructions are only for if the jz is taken | ||
207 | &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
208 | &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
209 | &jz(&label("decrypt_finish")); | ||
210 | |||
211 | &set_label("decrypt_loop"); | ||
212 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
213 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
214 | |||
215 | &bswap("eax") if $swap; | ||
216 | &bswap("ebx") if $swap; | ||
217 | |||
218 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
219 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
220 | |||
221 | &call($dec_func); | ||
222 | |||
223 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
224 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
225 | |||
226 | &bswap("eax") if $swap; | ||
227 | &bswap("ebx") if $swap; | ||
228 | |||
229 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
230 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
231 | |||
232 | &xor("ecx", "eax"); | ||
233 | &xor("edx", "ebx"); | ||
234 | |||
235 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
236 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
237 | |||
238 | &mov(&DWP(0,$out,"",0),"ecx"); | ||
239 | &mov(&DWP(4,$out,"",0),"edx"); | ||
240 | |||
241 | &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv | ||
242 | &mov(&DWP($data_off+12,"esp","",0), "ebx"); # | ||
243 | |||
244 | &add($in, 8); | ||
245 | &add($out, 8); | ||
246 | |||
247 | &sub($count, 8); | ||
248 | &jnz(&label("decrypt_loop")); | ||
249 | ############################ ENDIT #######################3 | ||
250 | &set_label("decrypt_finish"); | ||
251 | &mov($count, &wparam(2)); # length | ||
252 | &and($count, 7); | ||
253 | &jz(&label("finish")); | ||
254 | |||
255 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
256 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
257 | |||
258 | &bswap("eax") if $swap; | ||
259 | &bswap("ebx") if $swap; | ||
260 | |||
261 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
262 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
263 | |||
264 | &call($dec_func); | ||
265 | |||
266 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
267 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
268 | |||
269 | &bswap("eax") if $swap; | ||
270 | &bswap("ebx") if $swap; | ||
271 | |||
272 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
273 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
274 | |||
275 | &xor("ecx", "eax"); | ||
276 | &xor("edx", "ebx"); | ||
277 | |||
278 | # this is for when we exit | ||
279 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
280 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
281 | |||
282 | &set_label("dj7"); | ||
283 | &rotr("edx", 16); | ||
284 | &movb(&BP(6,$out,"",0), &LB("edx")); | ||
285 | &shr("edx",16); | ||
286 | &set_label("dj6"); | ||
287 | &movb(&BP(5,$out,"",0), &HB("edx")); | ||
288 | &set_label("dj5"); | ||
289 | &movb(&BP(4,$out,"",0), &LB("edx")); | ||
290 | &set_label("dj4"); | ||
291 | &mov(&DWP(0,$out,"",0), "ecx"); | ||
292 | &jmp(&label("djend")); | ||
293 | &set_label("dj3"); | ||
294 | &rotr("ecx", 16); | ||
295 | &movb(&BP(2,$out,"",0), &LB("ecx")); | ||
296 | &shl("ecx",16); | ||
297 | &set_label("dj2"); | ||
298 | &movb(&BP(1,$in,"",0), &HB("ecx")); | ||
299 | &set_label("dj1"); | ||
300 | &movb(&BP(0,$in,"",0), &LB("ecx")); | ||
301 | &set_label("djend"); | ||
302 | |||
303 | # final iv is still in eax:ebx | ||
304 | &jmp(&label("finish")); | ||
305 | |||
306 | |||
307 | ############################ FINISH #######################3 | ||
308 | &set_label("finish",1); | ||
309 | &mov("ecx", &wparam($iv_off)); # Get iv ptr | ||
310 | |||
311 | ################################################# | ||
312 | $total=16+4; | ||
313 | $total+=4 if ($p1 > 0); | ||
314 | $total+=4 if ($p2 > 0); | ||
315 | $total+=4 if ($p3 > 0); | ||
316 | &add("esp",$total); | ||
317 | |||
318 | &mov(&DWP(0,"ecx","",0), "eax"); # save iv | ||
319 | &mov(&DWP(4,"ecx","",0), "ebx"); # save iv | ||
320 | |||
321 | &function_end_A($name); | ||
322 | |||
323 | &align(64); | ||
324 | &set_label("cbc_enc_jmp_table"); | ||
325 | &data_word("0"); | ||
326 | &data_word(&label("ej1")."-".&label("PIC_point")); | ||
327 | &data_word(&label("ej2")."-".&label("PIC_point")); | ||
328 | &data_word(&label("ej3")."-".&label("PIC_point")); | ||
329 | &data_word(&label("ej4")."-".&label("PIC_point")); | ||
330 | &data_word(&label("ej5")."-".&label("PIC_point")); | ||
331 | &data_word(&label("ej6")."-".&label("PIC_point")); | ||
332 | &data_word(&label("ej7")."-".&label("PIC_point")); | ||
333 | # not used | ||
334 | #&set_label("cbc_dec_jmp_table",1); | ||
335 | #&data_word("0"); | ||
336 | #&data_word(&label("dj1")."-".&label("PIC_point")); | ||
337 | #&data_word(&label("dj2")."-".&label("PIC_point")); | ||
338 | #&data_word(&label("dj3")."-".&label("PIC_point")); | ||
339 | #&data_word(&label("dj4")."-".&label("PIC_point")); | ||
340 | #&data_word(&label("dj5")."-".&label("PIC_point")); | ||
341 | #&data_word(&label("dj6")."-".&label("PIC_point")); | ||
342 | #&data_word(&label("dj7")."-".&label("PIC_point")); | ||
343 | &align(64); | ||
344 | |||
345 | &function_end_B($name); | ||
346 | |||
347 | } | ||
348 | |||
349 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/ppc-xlate.pl b/src/lib/libcrypto/perlasm/ppc-xlate.pl deleted file mode 100755 index 4579671c97..0000000000 --- a/src/lib/libcrypto/perlasm/ppc-xlate.pl +++ /dev/null | |||
@@ -1,152 +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 .= ".globl $name\n"; | ||
38 | $ret .= ".align 3\n"; | ||
39 | $ret .= "$name:\n"; | ||
40 | $ret .= ".quad .$name,.TOC.\@tocbase,0\n"; | ||
41 | $ret .= ".size $name,24\n"; | ||
42 | $ret .= ".previous\n"; | ||
43 | |||
44 | $name = ".$name"; | ||
45 | last; | ||
46 | }; | ||
47 | } | ||
48 | |||
49 | $ret = ".globl $name" if (!$ret); | ||
50 | $$global = $name; | ||
51 | $ret; | ||
52 | }; | ||
53 | my $text = sub { | ||
54 | ($flavour =~ /aix/) ? ".csect" : ".text"; | ||
55 | }; | ||
56 | my $machine = sub { | ||
57 | my $junk = shift; | ||
58 | my $arch = shift; | ||
59 | if ($flavour =~ /osx/) | ||
60 | { $arch =~ s/\"//g; | ||
61 | $arch = ($flavour=~/64/) ? "ppc970-64" : "ppc970" if ($arch eq "any"); | ||
62 | } | ||
63 | ".machine $arch"; | ||
64 | }; | ||
65 | my $asciz = sub { | ||
66 | shift; | ||
67 | my $line = join(",",@_); | ||
68 | if ($line =~ /^"(.*)"$/) | ||
69 | { ".byte " . join(",",unpack("C*",$1),0) . "\n.align 2"; } | ||
70 | else | ||
71 | { ""; } | ||
72 | }; | ||
73 | |||
74 | ################################################################ | ||
75 | # simplified mnemonics not handled by at least one assembler | ||
76 | ################################################################ | ||
77 | my $cmplw = sub { | ||
78 | my $f = shift; | ||
79 | my $cr = 0; $cr = shift if ($#_>1); | ||
80 | # Some out-of-date 32-bit GNU assembler just can't handle cmplw... | ||
81 | ($flavour =~ /linux.*32/) ? | ||
82 | " .long ".sprintf "0x%x",31<<26|$cr<<23|$_[0]<<16|$_[1]<<11|64 : | ||
83 | " cmplw ".join(',',$cr,@_); | ||
84 | }; | ||
85 | my $bdnz = sub { | ||
86 | my $f = shift; | ||
87 | my $bo = $f=~/[\+\-]/ ? 16+9 : 16; # optional "to be taken" hint | ||
88 | " bc $bo,0,".shift; | ||
89 | } if ($flavour!~/linux/); | ||
90 | my $bltlr = sub { | ||
91 | my $f = shift; | ||
92 | my $bo = $f=~/\-/ ? 12+2 : 12; # optional "not to be taken" hint | ||
93 | ($flavour =~ /linux/) ? # GNU as doesn't allow most recent hints | ||
94 | " .long ".sprintf "0x%x",19<<26|$bo<<21|16<<1 : | ||
95 | " bclr $bo,0"; | ||
96 | }; | ||
97 | my $bnelr = sub { | ||
98 | my $f = shift; | ||
99 | my $bo = $f=~/\-/ ? 4+2 : 4; # 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|2<<16|16<<1 : | ||
102 | " bclr $bo,2"; | ||
103 | }; | ||
104 | my $beqlr = sub { | ||
105 | my $f = shift; | ||
106 | my $bo = $f=~/-/ ? 12+2 : 12; # 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 | # GNU assembler can't handle extrdi rA,rS,16,48, or when sum of last two | ||
112 | # arguments is 64, with "operand out of range" error. | ||
113 | my $extrdi = sub { | ||
114 | my ($f,$ra,$rs,$n,$b) = @_; | ||
115 | $b = ($b+$n)&63; $n = 64-$n; | ||
116 | " rldicl $ra,$rs,$b,$n"; | ||
117 | }; | ||
118 | |||
119 | while($line=<>) { | ||
120 | |||
121 | $line =~ s|[#!;].*$||; # get rid of asm-style comments... | ||
122 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
123 | $line =~ s|^\s+||; # ... and skip white spaces in beginning... | ||
124 | $line =~ s|\s+$||; # ... and at the end | ||
125 | |||
126 | { | ||
127 | $line =~ s|\b\.L(\w+)|L$1|g; # common denominator for Locallabel | ||
128 | $line =~ s|\bL(\w+)|\.L$1|g if ($dotinlocallabels); | ||
129 | } | ||
130 | |||
131 | { | ||
132 | $line =~ s|(^[\.\w]+)\:\s*||; | ||
133 | my $label = $1; | ||
134 | printf "%s:",($GLOBALS{$label} or $label) if ($label); | ||
135 | } | ||
136 | |||
137 | { | ||
138 | $line =~ s|^\s*(\.?)(\w+)([\.\+\-]?)\s*||; | ||
139 | my $c = $1; $c = "\t" if ($c eq ""); | ||
140 | my $mnemonic = $2; | ||
141 | my $f = $3; | ||
142 | my $opcode = eval("\$$mnemonic"); | ||
143 | $line =~ s|\bc?[rf]([0-9]+)\b|$1|g if ($c ne "." and $flavour !~ /osx/); | ||
144 | if (ref($opcode) eq 'CODE') { $line = &$opcode($f,split(',',$line)); } | ||
145 | elsif ($mnemonic) { $line = $c.$mnemonic.$f."\t".$line; } | ||
146 | } | ||
147 | |||
148 | print $line if ($line); | ||
149 | print "\n"; | ||
150 | } | ||
151 | |||
152 | close STDOUT; | ||
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme deleted file mode 100644 index f02bbee75a..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 origional 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 assember | ||
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 68b4c1ca80..0000000000 --- a/src/lib/libcrypto/perlasm/x86_64-xlate.pl +++ /dev/null | |||
@@ -1,920 +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 | { my ($stddev,$stdino,@junk)=stat(STDOUT); | ||
66 | my ($outdev,$outino,@junk)=stat($output); | ||
67 | |||
68 | open STDOUT,">$output" || die "can't open $output: $!" | ||
69 | if (1 || $stddev!=$outdev || $stdino!=$outino); | ||
70 | } | ||
71 | |||
72 | my $gas=1; $gas=0 if ($output =~ /\.asm$/); | ||
73 | my $elf=1; $elf=0 if (!$gas); | ||
74 | my $win64=0; | ||
75 | my $prefix=""; | ||
76 | my $decor=".L"; | ||
77 | |||
78 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 | ||
79 | my $masm=0; | ||
80 | my $PTR=" PTR"; | ||
81 | |||
82 | my $nasmref=2.03; | ||
83 | my $nasm=0; | ||
84 | |||
85 | if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; | ||
86 | $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`; | ||
87 | chomp($prefix); | ||
88 | } | ||
89 | elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; } | ||
90 | elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; } | ||
91 | elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; } | ||
92 | elsif (!$gas) | ||
93 | { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i) | ||
94 | { $nasm = $1 + $2*0.01; $PTR=""; } | ||
95 | elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/) | ||
96 | { $masm = $1 + $2*2**-16 + $4*2**-32; } | ||
97 | die "no assembler found on %PATH" if (!($nasm || $masm)); | ||
98 | $win64=1; | ||
99 | $elf=0; | ||
100 | $decor="\$L\$"; | ||
101 | } | ||
102 | |||
103 | my $current_segment; | ||
104 | my $current_function; | ||
105 | my %globals; | ||
106 | |||
107 | { package opcode; # pick up opcodes | ||
108 | sub re { | ||
109 | my $self = shift; # single instance in enough... | ||
110 | local *line = shift; | ||
111 | undef $ret; | ||
112 | |||
113 | if ($line =~ /^([a-z][a-z0-9]*)/i) { | ||
114 | $self->{op} = $1; | ||
115 | $ret = $self; | ||
116 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
117 | |||
118 | undef $self->{sz}; | ||
119 | if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... | ||
120 | $self->{op} = $1; | ||
121 | $self->{sz} = "b"; | ||
122 | } elsif ($self->{op} =~ /call|jmp/) { | ||
123 | $self->{sz} = ""; | ||
124 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op)/) { # SSEn | ||
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 . ".byte 0xf3,0xc3"; | ||
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 | !($line =~ /^PIC_(GOT|PLT)/)) { | ||
220 | $self->{asterisk} = $1; | ||
221 | $self->{label} = $2; | ||
222 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$3); | ||
223 | $self->{scale} = 1 if (!defined($self->{scale})); | ||
224 | $ret = $self; | ||
225 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
226 | |||
227 | if ($win64 && $self->{label} =~ s/\@GOTPCREL//) { | ||
228 | die if (opcode->mnemonic() ne "mov"); | ||
229 | opcode->mnemonic("lea"); | ||
230 | } | ||
231 | $self->{base} =~ s/^%//; | ||
232 | $self->{index} =~ s/^%// if (defined($self->{index})); | ||
233 | } | ||
234 | $ret; | ||
235 | } | ||
236 | sub size {} | ||
237 | sub out { | ||
238 | my $self = shift; | ||
239 | my $sz = shift; | ||
240 | |||
241 | $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; | ||
242 | $self->{label} =~ s/\.L/$decor/g; | ||
243 | |||
244 | # Silently convert all EAs to 64-bit. This is required for | ||
245 | # elder GNU assembler and results in more compact code, | ||
246 | # *but* most importantly AES module depends on this feature! | ||
247 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
248 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
249 | |||
250 | if ($gas) { | ||
251 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
252 | # in $self->{label}, new gas requires sign extension... | ||
253 | use integer; | ||
254 | $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; | ||
255 | $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
256 | $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg; | ||
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},$self->{base}, | ||
262 | $self->{index},$self->{scale}; | ||
263 | } else { | ||
264 | sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; | ||
265 | } | ||
266 | } else { | ||
267 | %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", q=>"QWORD$PTR" ); | ||
268 | |||
269 | $self->{label} =~ s/\./\$/g; | ||
270 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; | ||
271 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); | ||
272 | $sz="q" if ($self->{asterisk}); | ||
273 | |||
274 | if (defined($self->{index})) { | ||
275 | sprintf "%s[%s%s*%d+%s]",$szmap{$sz}, | ||
276 | $self->{label}?"$self->{label}+":"", | ||
277 | $self->{index},$self->{scale}, | ||
278 | $self->{base}; | ||
279 | } elsif ($self->{base} eq "rip") { | ||
280 | sprintf "%s[%s]",$szmap{$sz},$self->{label}; | ||
281 | } else { | ||
282 | sprintf "%s[%s%s]",$szmap{$sz}, | ||
283 | $self->{label}?"$self->{label}+":"", | ||
284 | $self->{base}; | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | { package register; # pick up registers, which start with %. | ||
290 | sub re { | ||
291 | my $class = shift; # muliple instances... | ||
292 | my $self = {}; | ||
293 | local *line = shift; | ||
294 | undef $ret; | ||
295 | |||
296 | # optional * ---vvv--- appears in indirect jmp/call | ||
297 | if ($line =~ /^(\*?)%(\w+)/) { | ||
298 | bless $self,$class; | ||
299 | $self->{asterisk} = $1; | ||
300 | $self->{value} = $2; | ||
301 | $ret = $self; | ||
302 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
303 | } | ||
304 | $ret; | ||
305 | } | ||
306 | sub size { | ||
307 | my $self = shift; | ||
308 | undef $ret; | ||
309 | |||
310 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } | ||
311 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } | ||
312 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } | ||
313 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } | ||
314 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } | ||
315 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } | ||
316 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } | ||
317 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } | ||
318 | |||
319 | $ret; | ||
320 | } | ||
321 | sub out { | ||
322 | my $self = shift; | ||
323 | if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; } | ||
324 | else { $self->{value}; } | ||
325 | } | ||
326 | } | ||
327 | { package label; # pick up labels, which end with : | ||
328 | sub re { | ||
329 | my $self = shift; # single instance is enough... | ||
330 | local *line = shift; | ||
331 | undef $ret; | ||
332 | |||
333 | if ($line =~ /(^[\.\w]+)\:/) { | ||
334 | $self->{value} = $1; | ||
335 | $ret = $self; | ||
336 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
337 | |||
338 | $self->{value} =~ s/^\.L/$decor/; | ||
339 | } | ||
340 | $ret; | ||
341 | } | ||
342 | sub out { | ||
343 | my $self = shift; | ||
344 | |||
345 | if ($gas) { | ||
346 | my $func = ($globals{$self->{value}} or $self->{value}) . ":"; | ||
347 | if ($win64 && | ||
348 | $current_function->{name} eq $self->{value} && | ||
349 | $current_function->{abi} eq "svr4") { | ||
350 | $func .= "\n"; | ||
351 | $func .= " movq %rdi,8(%rsp)\n"; | ||
352 | $func .= " movq %rsi,16(%rsp)\n"; | ||
353 | $func .= " movq %rsp,%rax\n"; | ||
354 | $func .= "${decor}SEH_begin_$current_function->{name}:\n"; | ||
355 | my $narg = $current_function->{narg}; | ||
356 | $narg=6 if (!defined($narg)); | ||
357 | $func .= " movq %rcx,%rdi\n" if ($narg>0); | ||
358 | $func .= " movq %rdx,%rsi\n" if ($narg>1); | ||
359 | $func .= " movq %r8,%rdx\n" if ($narg>2); | ||
360 | $func .= " movq %r9,%rcx\n" if ($narg>3); | ||
361 | $func .= " movq 40(%rsp),%r8\n" if ($narg>4); | ||
362 | $func .= " movq 48(%rsp),%r9\n" if ($narg>5); | ||
363 | } | ||
364 | $func; | ||
365 | } elsif ($self->{value} ne "$current_function->{name}") { | ||
366 | $self->{value} .= ":" if ($masm && $ret!~m/^\$/); | ||
367 | $self->{value} . ":"; | ||
368 | } elsif ($win64 && $current_function->{abi} eq "svr4") { | ||
369 | my $func = "$current_function->{name}" . | ||
370 | ($nasm ? ":" : "\tPROC $current_function->{scope}") . | ||
371 | "\n"; | ||
372 | $func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n"; | ||
373 | $func .= " mov QWORD${PTR}[16+rsp],rsi\n"; | ||
374 | $func .= " mov rax,rsp\n"; | ||
375 | $func .= "${decor}SEH_begin_$current_function->{name}:"; | ||
376 | $func .= ":" if ($masm); | ||
377 | $func .= "\n"; | ||
378 | my $narg = $current_function->{narg}; | ||
379 | $narg=6 if (!defined($narg)); | ||
380 | $func .= " mov rdi,rcx\n" if ($narg>0); | ||
381 | $func .= " mov rsi,rdx\n" if ($narg>1); | ||
382 | $func .= " mov rdx,r8\n" if ($narg>2); | ||
383 | $func .= " mov rcx,r9\n" if ($narg>3); | ||
384 | $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4); | ||
385 | $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5); | ||
386 | $func .= "\n"; | ||
387 | } else { | ||
388 | "$current_function->{name}". | ||
389 | ($nasm ? ":" : "\tPROC $current_function->{scope}"); | ||
390 | } | ||
391 | } | ||
392 | } | ||
393 | { package expr; # pick up expressioins | ||
394 | sub re { | ||
395 | my $self = shift; # single instance is enough... | ||
396 | local *line = shift; | ||
397 | undef $ret; | ||
398 | |||
399 | if ($line =~ /(^[^,]+)/) { | ||
400 | $self->{value} = $1; | ||
401 | $ret = $self; | ||
402 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
403 | |||
404 | $self->{value} =~ s/\@PLT// if (!$elf); | ||
405 | $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; | ||
406 | $self->{value} =~ s/\.L/$decor/g; | ||
407 | } | ||
408 | $ret; | ||
409 | } | ||
410 | sub out { | ||
411 | my $self = shift; | ||
412 | if ($nasm && opcode->mnemonic()=~m/^j/) { | ||
413 | "NEAR ".$self->{value}; | ||
414 | } else { | ||
415 | $self->{value}; | ||
416 | } | ||
417 | } | ||
418 | } | ||
419 | { package directive; # pick up directives, which start with . | ||
420 | sub re { | ||
421 | my $self = shift; # single instance is enough... | ||
422 | local *line = shift; | ||
423 | undef $ret; | ||
424 | my $dir; | ||
425 | my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: | ||
426 | ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, | ||
427 | "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, | ||
428 | "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, | ||
429 | "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, | ||
430 | "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, | ||
431 | "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, | ||
432 | "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, | ||
433 | "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); | ||
434 | |||
435 | if ($line =~ /^\s*(\.\w+)/) { | ||
436 | $dir = $1; | ||
437 | $ret = $self; | ||
438 | undef $self->{value}; | ||
439 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
440 | |||
441 | SWITCH: for ($dir) { | ||
442 | /\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) { | ||
443 | $dir="\t.long"; | ||
444 | $line=sprintf "0x%x,0x90000000",$opcode{$1}; | ||
445 | } | ||
446 | last; | ||
447 | }; | ||
448 | /\.global|\.globl|\.extern/ | ||
449 | && do { $globals{$line} = $prefix . $line; | ||
450 | $line = $globals{$line} if ($prefix); | ||
451 | last; | ||
452 | }; | ||
453 | /\.type/ && do { ($sym,$type,$narg) = split(',',$line); | ||
454 | if ($type eq "\@function") { | ||
455 | undef $current_function; | ||
456 | $current_function->{name} = $sym; | ||
457 | $current_function->{abi} = "svr4"; | ||
458 | $current_function->{narg} = $narg; | ||
459 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; | ||
460 | } elsif ($type eq "\@abi-omnipotent") { | ||
461 | undef $current_function; | ||
462 | $current_function->{name} = $sym; | ||
463 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; | ||
464 | } | ||
465 | $line =~ s/\@abi\-omnipotent/\@function/; | ||
466 | $line =~ s/\@function.*/\@function/; | ||
467 | last; | ||
468 | }; | ||
469 | /\.asciz/ && do { if ($line =~ /^"(.*)"$/) { | ||
470 | $dir = ".byte"; | ||
471 | $line = join(",",unpack("C*",$1),0); | ||
472 | } | ||
473 | last; | ||
474 | }; | ||
475 | /\.rva|\.long|\.quad/ | ||
476 | && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; | ||
477 | $line =~ s/\.L/$decor/g; | ||
478 | last; | ||
479 | }; | ||
480 | } | ||
481 | |||
482 | if ($gas) { | ||
483 | $self->{value} = $dir . "\t" . $line; | ||
484 | |||
485 | if ($dir =~ /\.extern/) { | ||
486 | $self->{value} = ""; # swallow extern | ||
487 | } elsif (!$elf && $dir =~ /\.type/) { | ||
488 | $self->{value} = ""; | ||
489 | $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" . | ||
490 | (defined($globals{$1})?".scl 2;":".scl 3;") . | ||
491 | "\t.type 32;\t.endef" | ||
492 | if ($win64 && $line =~ /([^,]+),\@function/); | ||
493 | } elsif (!$elf && $dir =~ /\.size/) { | ||
494 | $self->{value} = ""; | ||
495 | if (defined($current_function)) { | ||
496 | $self->{value} .= "${decor}SEH_end_$current_function->{name}:" | ||
497 | if ($win64 && $current_function->{abi} eq "svr4"); | ||
498 | undef $current_function; | ||
499 | } | ||
500 | } elsif (!$elf && $dir =~ /\.align/) { | ||
501 | $self->{value} = ".p2align\t" . (log($line)/log(2)); | ||
502 | } elsif ($dir eq ".section") { | ||
503 | $current_segment=$line; | ||
504 | if (!$elf && $current_segment eq ".init") { | ||
505 | if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; } | ||
506 | elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; } | ||
507 | } | ||
508 | } elsif ($dir =~ /\.(text|data)/) { | ||
509 | $current_segment=".$1"; | ||
510 | } | ||
511 | $line = ""; | ||
512 | return $self; | ||
513 | } | ||
514 | |||
515 | # non-gas case or nasm/masm | ||
516 | SWITCH: for ($dir) { | ||
517 | /\.text/ && do { my $v=undef; | ||
518 | if ($nasm) { | ||
519 | $v="section .text code align=64\n"; | ||
520 | } else { | ||
521 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
522 | $current_segment = ".text\$"; | ||
523 | $v.="$current_segment\tSEGMENT "; | ||
524 | $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; | ||
525 | $v.=" 'CODE'"; | ||
526 | } | ||
527 | $self->{value} = $v; | ||
528 | last; | ||
529 | }; | ||
530 | /\.data/ && do { my $v=undef; | ||
531 | if ($nasm) { | ||
532 | $v="section .data data align=8\n"; | ||
533 | } else { | ||
534 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
535 | $current_segment = "_DATA"; | ||
536 | $v.="$current_segment\tSEGMENT"; | ||
537 | } | ||
538 | $self->{value} = $v; | ||
539 | last; | ||
540 | }; | ||
541 | /\.section/ && do { my $v=undef; | ||
542 | $line =~ s/([^,]*).*/$1/; | ||
543 | $line = ".CRT\$XCU" if ($line eq ".init"); | ||
544 | if ($nasm) { | ||
545 | $v="section $line"; | ||
546 | if ($line=~/\.([px])data/) { | ||
547 | $v.=" rdata align="; | ||
548 | $v.=$1 eq "p"? 4 : 8; | ||
549 | } elsif ($line=~/\.CRT\$/i) { | ||
550 | $v.=" rdata align=8"; | ||
551 | } | ||
552 | } else { | ||
553 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
554 | $v.="$line\tSEGMENT"; | ||
555 | if ($line=~/\.([px])data/) { | ||
556 | $v.=" READONLY"; | ||
557 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref); | ||
558 | } elsif ($line=~/\.CRT\$/i) { | ||
559 | $v.=" READONLY DWORD"; | ||
560 | } | ||
561 | } | ||
562 | $current_segment = $line; | ||
563 | $self->{value} = $v; | ||
564 | last; | ||
565 | }; | ||
566 | /\.extern/ && do { $self->{value} = "EXTERN\t".$line; | ||
567 | $self->{value} .= ":NEAR" if ($masm); | ||
568 | last; | ||
569 | }; | ||
570 | /\.globl|.global/ | ||
571 | && do { $self->{value} = $masm?"PUBLIC":"global"; | ||
572 | $self->{value} .= "\t".$line; | ||
573 | last; | ||
574 | }; | ||
575 | /\.size/ && do { if (defined($current_function)) { | ||
576 | undef $self->{value}; | ||
577 | if ($current_function->{abi} eq "svr4") { | ||
578 | $self->{value}="${decor}SEH_end_$current_function->{name}:"; | ||
579 | $self->{value}.=":\n" if($masm); | ||
580 | } | ||
581 | $self->{value}.="$current_function->{name}\tENDP" if($masm); | ||
582 | undef $current_function; | ||
583 | } | ||
584 | last; | ||
585 | }; | ||
586 | /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; | ||
587 | /\.(value|long|rva|quad)/ | ||
588 | && do { my $sz = substr($1,0,1); | ||
589 | my @arr = split(/,\s*/,$line); | ||
590 | my $last = pop(@arr); | ||
591 | my $conv = sub { my $var=shift; | ||
592 | $var=~s/^(0b[0-1]+)/oct($1)/eig; | ||
593 | $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm); | ||
594 | if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva")) | ||
595 | { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; } | ||
596 | $var; | ||
597 | }; | ||
598 | |||
599 | $sz =~ tr/bvlrq/BWDDQ/; | ||
600 | $self->{value} = "\tD$sz\t"; | ||
601 | for (@arr) { $self->{value} .= &$conv($_).","; } | ||
602 | $self->{value} .= &$conv($last); | ||
603 | last; | ||
604 | }; | ||
605 | /\.byte/ && do { my @str=split(/,\s*/,$line); | ||
606 | map(s/(0b[0-1]+)/oct($1)/eig,@str); | ||
607 | map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm); | ||
608 | while ($#str>15) { | ||
609 | $self->{value}.="DB\t" | ||
610 | .join(",",@str[0..15])."\n"; | ||
611 | foreach (0..15) { shift @str; } | ||
612 | } | ||
613 | $self->{value}.="DB\t" | ||
614 | .join(",",@str) if (@str); | ||
615 | last; | ||
616 | }; | ||
617 | } | ||
618 | $line = ""; | ||
619 | } | ||
620 | |||
621 | $ret; | ||
622 | } | ||
623 | sub out { | ||
624 | my $self = shift; | ||
625 | $self->{value}; | ||
626 | } | ||
627 | } | ||
628 | |||
629 | print "#include <machine/asm.h>\n"; | ||
630 | |||
631 | if ($nasm) { | ||
632 | print <<___; | ||
633 | default rel | ||
634 | ___ | ||
635 | } elsif ($masm) { | ||
636 | print <<___; | ||
637 | OPTION DOTNAME | ||
638 | ___ | ||
639 | } | ||
640 | while($line=<>) { | ||
641 | |||
642 | chomp($line); | ||
643 | |||
644 | $line =~ s|[#!].*$||; # get rid of asm-style comments... | ||
645 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
646 | $line =~ s|^\s+||; # ... and skip white spaces in beginning | ||
647 | |||
648 | undef $label; | ||
649 | undef $opcode; | ||
650 | undef $sz; | ||
651 | undef @args; | ||
652 | |||
653 | if ($label=label->re(\$line)) { print $label->out(); } | ||
654 | |||
655 | if (directive->re(\$line)) { | ||
656 | printf "%s",directive->out(); | ||
657 | } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: while (1) { | ||
658 | my $arg; | ||
659 | |||
660 | if ($arg=register->re(\$line)) { opcode->size($arg->size()); } | ||
661 | elsif ($arg=const->re(\$line)) { } | ||
662 | elsif ($arg=ea->re(\$line)) { } | ||
663 | elsif ($arg=expr->re(\$line)) { } | ||
664 | else { last ARGUMENT; } | ||
665 | |||
666 | push @args,$arg; | ||
667 | |||
668 | last ARGUMENT if ($line !~ /^,/); | ||
669 | |||
670 | $line =~ s/^,\s*//; | ||
671 | } # ARGUMENT: | ||
672 | |||
673 | $sz=opcode->size(); | ||
674 | |||
675 | if ($#args>=0) { | ||
676 | my $insn; | ||
677 | if ($gas) { | ||
678 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); | ||
679 | } else { | ||
680 | $insn = $opcode->out(); | ||
681 | $insn .= $sz if (map($_->out() =~ /x?mm/,@args)); | ||
682 | @args = reverse(@args); | ||
683 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); | ||
684 | } | ||
685 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); | ||
686 | } else { | ||
687 | printf "\t%s",$opcode->out(); | ||
688 | } | ||
689 | } | ||
690 | |||
691 | print $line,"\n"; | ||
692 | } | ||
693 | |||
694 | print "\n$current_segment\tENDS\n" if ($current_segment && $masm); | ||
695 | print "END\n" if ($masm); | ||
696 | |||
697 | close STDOUT; | ||
698 | |||
699 | ################################################# | ||
700 | # Cross-reference x86_64 ABI "card" | ||
701 | # | ||
702 | # Unix Win64 | ||
703 | # %rax * * | ||
704 | # %rbx - - | ||
705 | # %rcx #4 #1 | ||
706 | # %rdx #3 #2 | ||
707 | # %rsi #2 - | ||
708 | # %rdi #1 - | ||
709 | # %rbp - - | ||
710 | # %rsp - - | ||
711 | # %r8 #5 #3 | ||
712 | # %r9 #6 #4 | ||
713 | # %r10 * * | ||
714 | # %r11 * * | ||
715 | # %r12 - - | ||
716 | # %r13 - - | ||
717 | # %r14 - - | ||
718 | # %r15 - - | ||
719 | # | ||
720 | # (*) volatile register | ||
721 | # (-) preserved by callee | ||
722 | # (#) Nth argument, volatile | ||
723 | # | ||
724 | # In Unix terms top of stack is argument transfer area for arguments | ||
725 | # which could not be accomodated in registers. Or in other words 7th | ||
726 | # [integer] argument resides at 8(%rsp) upon function entry point. | ||
727 | # 128 bytes above %rsp constitute a "red zone" which is not touched | ||
728 | # by signal handlers and can be used as temporal storage without | ||
729 | # allocating a frame. | ||
730 | # | ||
731 | # In Win64 terms N*8 bytes on top of stack is argument transfer area, | ||
732 | # which belongs to/can be overwritten by callee. N is the number of | ||
733 | # arguments passed to callee, *but* not less than 4! This means that | ||
734 | # upon function entry point 5th argument resides at 40(%rsp), as well | ||
735 | # as that 32 bytes from 8(%rsp) can always be used as temporal | ||
736 | # storage [without allocating a frame]. One can actually argue that | ||
737 | # one can assume a "red zone" above stack pointer under Win64 as well. | ||
738 | # Point is that at apparently no occasion Windows kernel would alter | ||
739 | # the area above user stack pointer in true asynchronous manner... | ||
740 | # | ||
741 | # All the above means that if assembler programmer adheres to Unix | ||
742 | # register and stack layout, but disregards the "red zone" existense, | ||
743 | # it's possible to use following prologue and epilogue to "gear" from | ||
744 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments. | ||
745 | # | ||
746 | # omnipotent_function: | ||
747 | # ifdef WIN64 | ||
748 | # movq %rdi,8(%rsp) | ||
749 | # movq %rsi,16(%rsp) | ||
750 | # movq %rcx,%rdi ; if 1st argument is actually present | ||
751 | # movq %rdx,%rsi ; if 2nd argument is actually ... | ||
752 | # movq %r8,%rdx ; if 3rd argument is ... | ||
753 | # movq %r9,%rcx ; if 4th argument ... | ||
754 | # movq 40(%rsp),%r8 ; if 5th ... | ||
755 | # movq 48(%rsp),%r9 ; if 6th ... | ||
756 | # endif | ||
757 | # ... | ||
758 | # ifdef WIN64 | ||
759 | # movq 8(%rsp),%rdi | ||
760 | # movq 16(%rsp),%rsi | ||
761 | # endif | ||
762 | # ret | ||
763 | # | ||
764 | ################################################# | ||
765 | # Win64 SEH, Structured Exception Handling. | ||
766 | # | ||
767 | # Unlike on Unix systems(*) lack of Win64 stack unwinding information | ||
768 | # has undesired side-effect at run-time: if an exception is raised in | ||
769 | # assembler subroutine such as those in question (basically we're | ||
770 | # referring to segmentation violations caused by malformed input | ||
771 | # parameters), the application is briskly terminated without invoking | ||
772 | # any exception handlers, most notably without generating memory dump | ||
773 | # or any user notification whatsoever. This poses a problem. It's | ||
774 | # possible to address it by registering custom language-specific | ||
775 | # handler that would restore processor context to the state at | ||
776 | # subroutine entry point and return "exception is not handled, keep | ||
777 | # unwinding" code. Writing such handler can be a challenge... But it's | ||
778 | # doable, though requires certain coding convention. Consider following | ||
779 | # snippet: | ||
780 | # | ||
781 | # .type function,@function | ||
782 | # function: | ||
783 | # movq %rsp,%rax # copy rsp to volatile register | ||
784 | # pushq %r15 # save non-volatile registers | ||
785 | # pushq %rbx | ||
786 | # pushq %rbp | ||
787 | # movq %rsp,%r11 | ||
788 | # subq %rdi,%r11 # prepare [variable] stack frame | ||
789 | # andq $-64,%r11 | ||
790 | # movq %rax,0(%r11) # check for exceptions | ||
791 | # movq %r11,%rsp # allocate [variable] stack frame | ||
792 | # movq %rax,0(%rsp) # save original rsp value | ||
793 | # magic_point: | ||
794 | # ... | ||
795 | # movq 0(%rsp),%rcx # pull original rsp value | ||
796 | # movq -24(%rcx),%rbp # restore non-volatile registers | ||
797 | # movq -16(%rcx),%rbx | ||
798 | # movq -8(%rcx),%r15 | ||
799 | # movq %rcx,%rsp # restore original rsp | ||
800 | # ret | ||
801 | # .size function,.-function | ||
802 | # | ||
803 | # The key is that up to magic_point copy of original rsp value remains | ||
804 | # in chosen volatile register and no non-volatile register, except for | ||
805 | # rsp, is modified. While past magic_point rsp remains constant till | ||
806 | # the very end of the function. In this case custom language-specific | ||
807 | # exception handler would look like this: | ||
808 | # | ||
809 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, | ||
810 | # CONTEXT *context,DISPATCHER_CONTEXT *disp) | ||
811 | # { ULONG64 *rsp = (ULONG64 *)context->Rax; | ||
812 | # if (context->Rip >= magic_point) | ||
813 | # { rsp = ((ULONG64 **)context->Rsp)[0]; | ||
814 | # context->Rbp = rsp[-3]; | ||
815 | # context->Rbx = rsp[-2]; | ||
816 | # context->R15 = rsp[-1]; | ||
817 | # } | ||
818 | # context->Rsp = (ULONG64)rsp; | ||
819 | # context->Rdi = rsp[1]; | ||
820 | # context->Rsi = rsp[2]; | ||
821 | # | ||
822 | # memcpy (disp->ContextRecord,context,sizeof(CONTEXT)); | ||
823 | # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase, | ||
824 | # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord, | ||
825 | # &disp->HandlerData,&disp->EstablisherFrame,NULL); | ||
826 | # return ExceptionContinueSearch; | ||
827 | # } | ||
828 | # | ||
829 | # It's appropriate to implement this handler in assembler, directly in | ||
830 | # function's module. In order to do that one has to know members' | ||
831 | # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant | ||
832 | # values. Here they are: | ||
833 | # | ||
834 | # CONTEXT.Rax 120 | ||
835 | # CONTEXT.Rcx 128 | ||
836 | # CONTEXT.Rdx 136 | ||
837 | # CONTEXT.Rbx 144 | ||
838 | # CONTEXT.Rsp 152 | ||
839 | # CONTEXT.Rbp 160 | ||
840 | # CONTEXT.Rsi 168 | ||
841 | # CONTEXT.Rdi 176 | ||
842 | # CONTEXT.R8 184 | ||
843 | # CONTEXT.R9 192 | ||
844 | # CONTEXT.R10 200 | ||
845 | # CONTEXT.R11 208 | ||
846 | # CONTEXT.R12 216 | ||
847 | # CONTEXT.R13 224 | ||
848 | # CONTEXT.R14 232 | ||
849 | # CONTEXT.R15 240 | ||
850 | # CONTEXT.Rip 248 | ||
851 | # CONTEXT.Xmm6 512 | ||
852 | # sizeof(CONTEXT) 1232 | ||
853 | # DISPATCHER_CONTEXT.ControlPc 0 | ||
854 | # DISPATCHER_CONTEXT.ImageBase 8 | ||
855 | # DISPATCHER_CONTEXT.FunctionEntry 16 | ||
856 | # DISPATCHER_CONTEXT.EstablisherFrame 24 | ||
857 | # DISPATCHER_CONTEXT.TargetIp 32 | ||
858 | # DISPATCHER_CONTEXT.ContextRecord 40 | ||
859 | # DISPATCHER_CONTEXT.LanguageHandler 48 | ||
860 | # DISPATCHER_CONTEXT.HandlerData 56 | ||
861 | # UNW_FLAG_NHANDLER 0 | ||
862 | # ExceptionContinueSearch 1 | ||
863 | # | ||
864 | # In order to tie the handler to the function one has to compose | ||
865 | # couple of structures: one for .xdata segment and one for .pdata. | ||
866 | # | ||
867 | # UNWIND_INFO structure for .xdata segment would be | ||
868 | # | ||
869 | # function_unwind_info: | ||
870 | # .byte 9,0,0,0 | ||
871 | # .rva handler | ||
872 | # | ||
873 | # This structure designates exception handler for a function with | ||
874 | # zero-length prologue, no stack frame or frame register. | ||
875 | # | ||
876 | # To facilitate composing of .pdata structures, auto-generated "gear" | ||
877 | # prologue copies rsp value to rax and denotes next instruction with | ||
878 | # .LSEH_begin_{function_name} label. This essentially defines the SEH | ||
879 | # styling rule mentioned in the beginning. Position of this label is | ||
880 | # chosen in such manner that possible exceptions raised in the "gear" | ||
881 | # prologue would be accounted to caller and unwound from latter's frame. | ||
882 | # End of function is marked with respective .LSEH_end_{function_name} | ||
883 | # label. To summarize, .pdata segment would contain | ||
884 | # | ||
885 | # .rva .LSEH_begin_function | ||
886 | # .rva .LSEH_end_function | ||
887 | # .rva function_unwind_info | ||
888 | # | ||
889 | # Reference to functon_unwind_info from .xdata segment is the anchor. | ||
890 | # In case you wonder why references are 32-bit .rvas and not 64-bit | ||
891 | # .quads. References put into these two segments are required to be | ||
892 | # *relative* to the base address of the current binary module, a.k.a. | ||
893 | # image base. No Win64 module, be it .exe or .dll, can be larger than | ||
894 | # 2GB and thus such relative references can be and are accommodated in | ||
895 | # 32 bits. | ||
896 | # | ||
897 | # Having reviewed the example function code, one can argue that "movq | ||
898 | # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix | ||
899 | # rax would contain an undefined value. If this "offends" you, use | ||
900 | # another register and refrain from modifying rax till magic_point is | ||
901 | # reached, i.e. as if it was a non-volatile register. If more registers | ||
902 | # are required prior [variable] frame setup is completed, note that | ||
903 | # nobody says that you can have only one "magic point." You can | ||
904 | # "liberate" non-volatile registers by denoting last stack off-load | ||
905 | # instruction and reflecting it in finer grade unwind logic in handler. | ||
906 | # After all, isn't it why it's called *language-specific* handler... | ||
907 | # | ||
908 | # Attentive reader can notice that exceptions would be mishandled in | ||
909 | # auto-generated "gear" epilogue. Well, exception effectively can't | ||
910 | # occur there, because if memory area used by it was subject to | ||
911 | # segmentation violation, then it would be raised upon call to the | ||
912 | # function (and as already mentioned be accounted to caller, which is | ||
913 | # not a problem). If you're still not comfortable, then define tail | ||
914 | # "magic point" just prior ret instruction and have handler treat it... | ||
915 | # | ||
916 | # (*) Note that we're talking about run-time, not debug-time. Lack of | ||
917 | # unwind information makes debugging hard on both Windows and | ||
918 | # Unix. "Unlike" referes to the fact that on Unix signal handler | ||
919 | # will always be invoked, core dumped and appropriate exit code | ||
920 | # 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 4756a28e59..0000000000 --- a/src/lib/libcrypto/perlasm/x86asm.pl +++ /dev/null | |||
@@ -1,221 +0,0 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | # require 'x86asm.pl'; | ||
4 | # &asm_init(<flavor>,"des-586.pl"[,$i386only]); | ||
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 | # label management | ||
91 | $lbdecor="L"; # local label decoration, set by package | ||
92 | $label="000"; | ||
93 | |||
94 | sub ::islabel # see is argument is a known label | ||
95 | { my $i; | ||
96 | foreach $i (values %label) { return $i if ($i eq $_[0]); } | ||
97 | $label{$_[0]}; # can be undef | ||
98 | } | ||
99 | |||
100 | sub ::label # instantiate a function-scope label | ||
101 | { if (!defined($label{$_[0]})) | ||
102 | { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; } | ||
103 | $label{$_[0]}; | ||
104 | } | ||
105 | |||
106 | sub ::LABEL # instantiate a file-scope label | ||
107 | { $label{$_[0]}=$_[1] if (!defined($label{$_[0]})); | ||
108 | $label{$_[0]}; | ||
109 | } | ||
110 | |||
111 | sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); } | ||
112 | |||
113 | sub ::set_label_B { push(@out,"@_:\n"); } | ||
114 | sub ::set_label | ||
115 | { my $label=&::label($_[0]); | ||
116 | &::align($_[1]) if ($_[1]>1); | ||
117 | &::set_label_B($label); | ||
118 | $label; | ||
119 | } | ||
120 | |||
121 | sub ::wipe_labels # wipes function-scope labels | ||
122 | { foreach $i (keys %label) | ||
123 | { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); } | ||
124 | } | ||
125 | |||
126 | # subroutine management | ||
127 | sub ::function_begin | ||
128 | { &function_begin_B(@_); | ||
129 | $stack=4; | ||
130 | &push("ebp"); | ||
131 | &push("ebx"); | ||
132 | &push("esi"); | ||
133 | &push("edi"); | ||
134 | } | ||
135 | |||
136 | sub ::function_end | ||
137 | { &pop("edi"); | ||
138 | &pop("esi"); | ||
139 | &pop("ebx"); | ||
140 | &pop("ebp"); | ||
141 | &ret(); | ||
142 | &function_end_B(@_); | ||
143 | $stack=0; | ||
144 | &wipe_labels(); | ||
145 | } | ||
146 | |||
147 | sub ::function_end_A | ||
148 | { &pop("edi"); | ||
149 | &pop("esi"); | ||
150 | &pop("ebx"); | ||
151 | &pop("ebp"); | ||
152 | &ret(); | ||
153 | $stack+=16; # readjust esp as if we didn't pop anything | ||
154 | } | ||
155 | |||
156 | sub ::asciz | ||
157 | { my @str=unpack("C*",shift); | ||
158 | push @str,0; | ||
159 | while ($#str>15) { | ||
160 | &data_byte(@str[0..15]); | ||
161 | foreach (0..15) { shift @str; } | ||
162 | } | ||
163 | &data_byte(@str) if (@str); | ||
164 | } | ||
165 | |||
166 | sub ::asm_finish | ||
167 | { &file_end(); | ||
168 | print @out; | ||
169 | } | ||
170 | |||
171 | sub ::asm_init | ||
172 | { my ($type,$fn,$cpu)=@_; | ||
173 | |||
174 | $filename=$fn; | ||
175 | $i386=$cpu; | ||
176 | |||
177 | $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$openbsd=0; | ||
178 | if (($type eq "elf")) | ||
179 | { $elf=1; require "x86gas.pl"; } | ||
180 | elsif (($type eq "a\.out")) | ||
181 | { $aout=1; require "x86gas.pl"; } | ||
182 | elsif (($type eq "coff" or $type eq "gaswin")) | ||
183 | { $coff=1; require "x86gas.pl"; } | ||
184 | elsif (($type eq "win32n")) | ||
185 | { $win32=1; require "x86nasm.pl"; } | ||
186 | elsif (($type eq "nw-nasm")) | ||
187 | { $netware=1; require "x86nasm.pl"; } | ||
188 | #elsif (($type eq "nw-mwasm")) | ||
189 | #{ $netware=1; $mwerks=1; require "x86nasm.pl"; } | ||
190 | elsif (($type eq "win32")) | ||
191 | { $win32=1; require "x86masm.pl"; } | ||
192 | elsif (($type eq "macosx")) | ||
193 | { $aout=1; $macosx=1; require "x86gas.pl"; } | ||
194 | elsif (($type eq "openbsd-elf")) | ||
195 | { $openbsd=$elf=1; require "x86gas.pl"; } | ||
196 | elsif (($type eq "openbsd-a.out")) | ||
197 | { $openbsd=1; require "x86gas.pl"; } | ||
198 | else | ||
199 | { print STDERR <<"EOF"; | ||
200 | Pick one target type from | ||
201 | elf - Linux, FreeBSD, Solaris x86, etc. | ||
202 | a.out - DJGPP, elder OpenBSD, etc. | ||
203 | coff - GAS/COFF such as Win32 targets | ||
204 | win32n - Windows 95/Windows NT NASM format | ||
205 | openbsd-elf - OpenBSD elf | ||
206 | openbsd-a.out - OpenBSD a.out | ||
207 | nw-nasm - NetWare NASM format | ||
208 | macosx - Mac OS X | ||
209 | EOF | ||
210 | exit(1); | ||
211 | } | ||
212 | |||
213 | $pic=0; | ||
214 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
215 | |||
216 | ::emitraw("#include <machine/asm.h>\n") if $openbsd; | ||
217 | $filename =~ s/\.pl$//; | ||
218 | &file($filename); | ||
219 | } | ||
220 | |||
221 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86gas.pl b/src/lib/libcrypto/perlasm/x86gas.pl deleted file mode 100644 index 15e17f25d0..0000000000 --- a/src/lib/libcrypto/perlasm/x86gas.pl +++ /dev/null | |||
@@ -1,267 +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 ($opcode =~ m/^j/o && $#_==1) { &::emit($opcode,@arg); } | ||
49 | elsif ($opcode eq "call" && $#_==1) { &::emit($opcode,@arg); } | ||
50 | elsif ($opcode =~ m/^set/&& $#_==1) { &::emit($opcode,@arg); } | ||
51 | else { &::emit($opcode.$suffix,@arg);} | ||
52 | |||
53 | 1; | ||
54 | } | ||
55 | # | ||
56 | # opcodes not covered by ::generic above, mostly inconsistent namings... | ||
57 | # | ||
58 | sub ::movzx { &::movzb(@_); } | ||
59 | sub ::pushfd { &::pushfl; } | ||
60 | sub ::popfd { &::popfl; } | ||
61 | sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); } | ||
62 | sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); } | ||
63 | |||
64 | sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } | ||
65 | sub ::call_ptr { &::generic("call","*$_[0]"); } | ||
66 | sub ::jmp_ptr { &::generic("jmp","*$_[0]"); } | ||
67 | |||
68 | *::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386); | ||
69 | |||
70 | sub ::DWP | ||
71 | { my($addr,$reg1,$reg2,$idx)=@_; | ||
72 | my $ret=""; | ||
73 | |||
74 | $addr =~ s/^\s+//; | ||
75 | # prepend global references with optional underscore | ||
76 | $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige; | ||
77 | |||
78 | $reg1 = "%$reg1" if ($reg1); | ||
79 | $reg2 = "%$reg2" if ($reg2); | ||
80 | |||
81 | $ret .= $addr if (($addr ne "") && ($addr ne 0)); | ||
82 | |||
83 | if ($reg2) | ||
84 | { $idx!= 0 or $idx=1; | ||
85 | $ret .= "($reg1,$reg2,$idx)"; | ||
86 | } | ||
87 | elsif ($reg1) | ||
88 | { $ret .= "($reg1)"; } | ||
89 | |||
90 | $ret; | ||
91 | } | ||
92 | sub ::QWP { &::DWP(@_); } | ||
93 | sub ::BP { &::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 (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) { | ||
153 | my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,4"; | ||
154 | if ($::elf) { push (@out,"$tmp,4\n"); } | ||
155 | else { push (@out,"$tmp\n"); } | ||
156 | } | ||
157 | if ($::macosx) | ||
158 | { if (%non_lazy_ptr) | ||
159 | { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n"); | ||
160 | foreach $i (keys %non_lazy_ptr) | ||
161 | { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); } | ||
162 | } | ||
163 | } | ||
164 | push(@out,$initseg) if ($initseg); | ||
165 | } | ||
166 | |||
167 | sub ::data_byte { push(@out,".byte\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 | sub ::picmeup | ||
181 | { my($dst,$sym,$base,$reflabel)=@_; | ||
182 | |||
183 | if ($::openbsd) | ||
184 | { &::emitraw("#ifdef PIC"); | ||
185 | &::emitraw("PIC_PROLOGUE"); | ||
186 | &::mov($dst, &::DWP("PIC_GOT($sym)")); | ||
187 | &::emitraw("PIC_EPILOGUE"); | ||
188 | &::emitraw("#else /* PIC */"); | ||
189 | &::lea($dst,&::DWP($sym)); | ||
190 | &::emitraw("#endif /* PIC */"); | ||
191 | } | ||
192 | elsif ($::pic && ($::elf || $::aout)) | ||
193 | { if (!defined($base)) | ||
194 | { &::call(&::label("PIC_me_up")); | ||
195 | &::set_label("PIC_me_up"); | ||
196 | &::blindpop($dst); | ||
197 | $base=$dst; | ||
198 | $reflabel=&::label("PIC_me_up"); | ||
199 | } | ||
200 | if ($::macosx) | ||
201 | { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr"); | ||
202 | &::mov($dst,&::DWP("$indirect-$reflabel",$base)); | ||
203 | $non_lazy_ptr{"$nmdecor$sym"}=$indirect; | ||
204 | } | ||
205 | else | ||
206 | { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", | ||
207 | $base)); | ||
208 | &::mov($dst,&::DWP("$sym\@GOT",$dst)); | ||
209 | } | ||
210 | } | ||
211 | else | ||
212 | { &::lea($dst,&::DWP($sym)); } | ||
213 | } | ||
214 | |||
215 | sub ::initseg | ||
216 | { my $f=$nmdecor.shift; | ||
217 | |||
218 | if ($::openbsd) | ||
219 | { $initseg.=<<___; | ||
220 | .section .init | ||
221 | PIC_PROLOGUE | ||
222 | call PIC_PLT($f) | ||
223 | PIC_EPILOGUE | ||
224 | jmp .Linitalign | ||
225 | .align $align | ||
226 | .Linitalign: | ||
227 | ___ | ||
228 | } | ||
229 | elsif ($::elf) | ||
230 | { $initseg.=<<___; | ||
231 | .section .init | ||
232 | call $f | ||
233 | jmp .Linitalign | ||
234 | .align $align | ||
235 | .Linitalign: | ||
236 | ___ | ||
237 | } | ||
238 | elsif ($::coff) | ||
239 | { $initseg.=<<___; # applies to both Cygwin and Mingw | ||
240 | .section .ctors | ||
241 | .long $f | ||
242 | ___ | ||
243 | } | ||
244 | elsif ($::macosx) | ||
245 | { $initseg.=<<___; | ||
246 | .mod_init_func | ||
247 | .align 2 | ||
248 | .long $f | ||
249 | ___ | ||
250 | } | ||
251 | elsif ($::aout) | ||
252 | { my $ctor="${nmdecor}_GLOBAL_\$I\$$f"; | ||
253 | $initseg.=".text\n"; | ||
254 | $initseg.=".type $ctor,\@function\n" if ($::pic); | ||
255 | $initseg.=<<___; # OpenBSD way... | ||
256 | .globl $ctor | ||
257 | .align 2 | ||
258 | $ctor: | ||
259 | jmp $f | ||
260 | ___ | ||
261 | } | ||
262 | } | ||
263 | |||
264 | sub ::dataseg | ||
265 | { push(@out,".data\n"); } | ||
266 | |||
267 | 1; | ||