summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/perlasm
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/perlasm')
-rw-r--r--src/lib/libcrypto/perlasm/cbc.pl351
-rw-r--r--src/lib/libcrypto/perlasm/readme124
-rwxr-xr-xsrc/lib/libcrypto/perlasm/x86_64-xlate.pl557
-rw-r--r--src/lib/libcrypto/perlasm/x86asm.pl136
-rw-r--r--src/lib/libcrypto/perlasm/x86masm.pl184
-rw-r--r--src/lib/libcrypto/perlasm/x86nasm.pl559
-rw-r--r--src/lib/libcrypto/perlasm/x86unix.pl91
7 files changed, 1863 insertions, 139 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl
new file mode 100644
index 0000000000..e43dc9ae15
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/cbc.pl
@@ -0,0 +1,351 @@
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.
26sub 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 &xor("edx", "edx") if $ppro; # ppro friendly
162 &movb(&HB("edx"), &BP(6,$in,"",0));
163 &shl("edx",8);
164&set_label("ej6");
165 &movb(&HB("edx"), &BP(5,$in,"",0));
166&set_label("ej5");
167 &movb(&LB("edx"), &BP(4,$in,"",0));
168&set_label("ej4");
169 &mov("ecx", &DWP(0,$in,"",0));
170 &jmp(&label("ejend"));
171&set_label("ej3");
172 &movb(&HB("ecx"), &BP(2,$in,"",0));
173 &xor("ecx", "ecx") if $ppro; # ppro friendly
174 &shl("ecx",8);
175&set_label("ej2");
176 &movb(&HB("ecx"), &BP(1,$in,"",0));
177&set_label("ej1");
178 &movb(&LB("ecx"), &BP(0,$in,"",0));
179&set_label("ejend");
180
181 &xor("eax", "ecx");
182 &xor("ebx", "edx");
183
184 &bswap("eax") if $swap;
185 &bswap("ebx") if $swap;
186
187 &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
188 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
189
190 &call($enc_func);
191
192 &mov("eax", &DWP($data_off,"esp","",0));
193 &mov("ebx", &DWP($data_off+4,"esp","",0));
194
195 &bswap("eax") if $swap;
196 &bswap("ebx") if $swap;
197
198 &mov(&DWP(0,$out,"",0),"eax");
199 &mov(&DWP(4,$out,"",0),"ebx");
200
201 &jmp(&label("finish"));
202
203 #############################################################
204 #############################################################
205 &set_label("decrypt",1);
206 # decrypt start
207 &and($count,0xfffffff8);
208 # The next 2 instructions are only for if the jz is taken
209 &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
210 &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
211 &jz(&label("decrypt_finish"));
212
213 &set_label("decrypt_loop");
214 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
215 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
216
217 &bswap("eax") if $swap;
218 &bswap("ebx") if $swap;
219
220 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
221 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
222
223 &call($dec_func);
224
225 &mov("eax", &DWP($data_off,"esp","",0)); # get return
226 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
227
228 &bswap("eax") if $swap;
229 &bswap("ebx") if $swap;
230
231 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
232 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
233
234 &xor("ecx", "eax");
235 &xor("edx", "ebx");
236
237 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
238 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
239
240 &mov(&DWP(0,$out,"",0),"ecx");
241 &mov(&DWP(4,$out,"",0),"edx");
242
243 &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
244 &mov(&DWP($data_off+12,"esp","",0), "ebx"); #
245
246 &add($in, 8);
247 &add($out, 8);
248
249 &sub($count, 8);
250 &jnz(&label("decrypt_loop"));
251############################ ENDIT #######################3
252 &set_label("decrypt_finish");
253 &mov($count, &wparam(2)); # length
254 &and($count, 7);
255 &jz(&label("finish"));
256
257 &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
258 &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
259
260 &bswap("eax") if $swap;
261 &bswap("ebx") if $swap;
262
263 &mov(&DWP($data_off,"esp","",0), "eax"); # put back
264 &mov(&DWP($data_off+4,"esp","",0), "ebx"); #
265
266 &call($dec_func);
267
268 &mov("eax", &DWP($data_off,"esp","",0)); # get return
269 &mov("ebx", &DWP($data_off+4,"esp","",0)); #
270
271 &bswap("eax") if $swap;
272 &bswap("ebx") if $swap;
273
274 &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
275 &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
276
277 &xor("ecx", "eax");
278 &xor("edx", "ebx");
279
280 # this is for when we exit
281 &mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
282 &mov("ebx", &DWP(4,$in,"",0)); # next iv actually
283
284&set_label("dj7");
285 &rotr("edx", 16);
286 &movb(&BP(6,$out,"",0), &LB("edx"));
287 &shr("edx",16);
288&set_label("dj6");
289 &movb(&BP(5,$out,"",0), &HB("edx"));
290&set_label("dj5");
291 &movb(&BP(4,$out,"",0), &LB("edx"));
292&set_label("dj4");
293 &mov(&DWP(0,$out,"",0), "ecx");
294 &jmp(&label("djend"));
295&set_label("dj3");
296 &rotr("ecx", 16);
297 &movb(&BP(2,$out,"",0), &LB("ecx"));
298 &shl("ecx",16);
299&set_label("dj2");
300 &movb(&BP(1,$in,"",0), &HB("ecx"));
301&set_label("dj1");
302 &movb(&BP(0,$in,"",0), &LB("ecx"));
303&set_label("djend");
304
305 # final iv is still in eax:ebx
306 &jmp(&label("finish"));
307
308
309############################ FINISH #######################3
310 &set_label("finish",1);
311 &mov("ecx", &wparam($iv_off)); # Get iv ptr
312
313 #################################################
314 $total=16+4;
315 $total+=4 if ($p1 > 0);
316 $total+=4 if ($p2 > 0);
317 $total+=4 if ($p3 > 0);
318 &add("esp",$total);
319
320 &mov(&DWP(0,"ecx","",0), "eax"); # save iv
321 &mov(&DWP(4,"ecx","",0), "ebx"); # save iv
322
323 &function_end_A($name);
324
325 &align(64);
326 &set_label("cbc_enc_jmp_table");
327 &data_word("0");
328 &data_word(&label("ej1")."-".&label("PIC_point"));
329 &data_word(&label("ej2")."-".&label("PIC_point"));
330 &data_word(&label("ej3")."-".&label("PIC_point"));
331 &data_word(&label("ej4")."-".&label("PIC_point"));
332 &data_word(&label("ej5")."-".&label("PIC_point"));
333 &data_word(&label("ej6")."-".&label("PIC_point"));
334 &data_word(&label("ej7")."-".&label("PIC_point"));
335 # not used
336 #&set_label("cbc_dec_jmp_table",1);
337 #&data_word("0");
338 #&data_word(&label("dj1")."-".&label("PIC_point"));
339 #&data_word(&label("dj2")."-".&label("PIC_point"));
340 #&data_word(&label("dj3")."-".&label("PIC_point"));
341 #&data_word(&label("dj4")."-".&label("PIC_point"));
342 #&data_word(&label("dj5")."-".&label("PIC_point"));
343 #&data_word(&label("dj6")."-".&label("PIC_point"));
344 #&data_word(&label("dj7")."-".&label("PIC_point"));
345 &align(64);
346
347 &function_end_B($name);
348
349 }
350
3511;
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme
new file mode 100644
index 0000000000..f02bbee75a
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/readme
@@ -0,0 +1,124 @@
1The perl scripts in this directory are my 'hack' to generate
2multiple different assembler formats via the one origional script.
3
4The way to use this library is to start with adding the path to this directory
5and then include it.
6
7push(@INC,"perlasm","../../perlasm");
8require "x86asm.pl";
9
10The first thing we do is setup the file and type of assember
11
12&asm_init($ARGV[0],$0);
13
14The first argument is the 'type'. Currently
15'cpp', 'sol', 'a.out', 'elf' or 'win32'.
16Argument 2 is the file name.
17
18The reciprocal function is
19&asm_finish() which should be called at the end.
20
21There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler,
22and x86unix.pl which is the unix (gas) version.
23
24Functions 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
48So how does this all hold together? Given
49
50int 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
60So 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
99The above example is very very unoptimised but gives an idea of how
100things work.
101
102There 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
114So for example, given
115void BF_encrypt(BF_LONG *data,BF_KEY *key);
116void BF_decrypt(BF_LONG *data,BF_KEY *key);
117void 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
new file mode 100755
index 0000000000..74153b017d
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl
@@ -0,0 +1,557 @@
1#!/usr/bin/env perl
2
3# Ascetic x86_64 AT&T to MASM 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# - indirect calls and jumps are not supported;
24#
25# Dual-ABI styling rules.
26#
27# 1. Adhere to Unix register and stack layout [see the end for
28# explanation].
29# 2. Forget about "red zone," stick to more traditional blended
30# stack frame allocation. If volatile storage is actually required
31# that is. If not, just leave the stack as is.
32# 3. Functions tagged with ".type name,@function" get crafted with
33# unified Win64 prologue and epilogue automatically. If you want
34# to take care of ABI differences yourself, tag functions as
35# ".type name,@abi-omnipotent" instead.
36# 4. To optimize the Win64 prologue you can specify number of input
37# arguments as ".type name,@function,N." Keep in mind that if N is
38# larger than 6, then you *have to* write "abi-omnipotent" code,
39# because >6 cases can't be addressed with unified prologue.
40# 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
41# (sorry about latter).
42# 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
43# required to identify the spots, where to inject Win64 epilogue!
44# But on the pros, it's then prefixed with rep automatically:-)
45# 7. Due to MASM limitations [and certain general counter-intuitivity
46# of ip-relative addressing] generation of position-independent
47# code is assisted by synthetic directive, .picmeup, which puts
48# address of the *next* instruction into target register.
49#
50# Example 1:
51# .picmeup %rax
52# lea .Label-.(%rax),%rax
53# Example 2:
54# .picmeup %rcx
55# .Lpic_point:
56# ...
57# lea .Label-.Lpic_point(%rcx),%rbp
58
59my $output = shift;
60
61{ my ($stddev,$stdino,@junk)=stat(STDOUT);
62 my ($outdev,$outino,@junk)=stat($output);
63
64 open STDOUT,">$output" || die "can't open $output: $!"
65 if ($stddev!=$outdev || $stdino!=$outino);
66}
67
68my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
69my $masm=$masmref if ($output =~ /\.asm/);
70if ($masm && `ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
71{ $masm=$1 + $2*2**-16 + $4*2**-32; }
72
73my $current_segment;
74my $current_function;
75
76{ package opcode; # pick up opcodes
77 sub re {
78 my $self = shift; # single instance in enough...
79 local *line = shift;
80 undef $ret;
81
82 if ($line =~ /^([a-z][a-z0-9]*)/i) {
83 $self->{op} = $1;
84 $ret = $self;
85 $line = substr($line,@+[0]); $line =~ s/^\s+//;
86
87 undef $self->{sz};
88 if ($self->{op} =~ /^(movz)b.*/) { # movz is pain...
89 $self->{op} = $1;
90 $self->{sz} = "b";
91 } elsif ($self->{op} =~ /call/) {
92 $self->{sz} = ""
93 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
94 $self->{op} = $1;
95 $self->{sz} = $2;
96 }
97 }
98 $ret;
99 }
100 sub size {
101 my $self = shift;
102 my $sz = shift;
103 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
104 $self->{sz};
105 }
106 sub out {
107 my $self = shift;
108 if (!$masm) {
109 if ($self->{op} eq "movz") { # movz is pain...
110 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
111 } elsif ($self->{op} =~ /^set/) {
112 "$self->{op}";
113 } elsif ($self->{op} eq "ret") {
114 ".byte 0xf3,0xc3";
115 } else {
116 "$self->{op}$self->{sz}";
117 }
118 } else {
119 $self->{op} =~ s/^movz/movzx/;
120 if ($self->{op} eq "ret") {
121 $self->{op} = "";
122 if ($current_function->{abi} eq "svr4") {
123 $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
124 "mov rsi,QWORD PTR 16[rsp]\n\t";
125 }
126 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
127 }
128 $self->{op};
129 }
130 }
131}
132{ package const; # pick up constants, which start with $
133 sub re {
134 my $self = shift; # single instance in enough...
135 local *line = shift;
136 undef $ret;
137
138 if ($line =~ /^\$([^,]+)/) {
139 $self->{value} = $1;
140 $ret = $self;
141 $line = substr($line,@+[0]); $line =~ s/^\s+//;
142 }
143 $ret;
144 }
145 sub out {
146 my $self = shift;
147
148 if (!$masm) {
149 # Solaris /usr/ccs/bin/as can't handle multiplications
150 # in $self->{value}
151 $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
152 $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
153 sprintf "\$%s",$self->{value};
154 } else {
155 $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig;
156 sprintf "%s",$self->{value};
157 }
158 }
159}
160{ package ea; # pick up effective addresses: expr(%reg,%reg,scale)
161 sub re {
162 my $self = shift; # single instance in enough...
163 local *line = shift;
164 undef $ret;
165
166 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/ &&
167 !($line =~ /^PIC_(GOT|PLT)/)) {
168 $self->{label} = $1;
169 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
170 $self->{scale} = 1 if (!defined($self->{scale}));
171 $ret = $self;
172 $line = substr($line,@+[0]); $line =~ s/^\s+//;
173
174 $self->{base} =~ s/^%//;
175 $self->{index} =~ s/^%// if (defined($self->{index}));
176 }
177 $ret;
178 }
179 sub size {}
180 sub out {
181 my $self = shift;
182 my $sz = shift;
183
184 # Silently convert all EAs to 64-bit. This is required for
185 # elder GNU assembler and results in more compact code,
186 # *but* most importantly AES module depends on this feature!
187 $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
188 $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
189
190 if (!$masm) {
191 # Solaris /usr/ccs/bin/as can't handle multiplications
192 # in $self->{label}
193 $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
194 $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
195
196 if (defined($self->{index})) {
197 sprintf "%s(%%%s,%%%s,%d)",
198 $self->{label},$self->{base},
199 $self->{index},$self->{scale};
200 } else {
201 sprintf "%s(%%%s)", $self->{label},$self->{base};
202 }
203 } else {
204 %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
205
206 $self->{label} =~ s/\./\$/g;
207 $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig;
208 $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
209
210 if (defined($self->{index})) {
211 sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
212 $self->{label},
213 $self->{index},$self->{scale},
214 $self->{base};
215 } elsif ($self->{base} eq "rip") {
216 sprintf "%s PTR %s",$szmap{$sz},$self->{label};
217 } else {
218 sprintf "%s PTR %s[%s]",$szmap{$sz},
219 $self->{label},$self->{base};
220 }
221 }
222 }
223}
224{ package register; # pick up registers, which start with %.
225 sub re {
226 my $class = shift; # muliple instances...
227 my $self = {};
228 local *line = shift;
229 undef $ret;
230
231 if ($line =~ /^%(\w+)/) {
232 bless $self,$class;
233 $self->{value} = $1;
234 $ret = $self;
235 $line = substr($line,@+[0]); $line =~ s/^\s+//;
236 }
237 $ret;
238 }
239 sub size {
240 my $self = shift;
241 undef $ret;
242
243 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
244 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
245 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
246 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
247 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
248 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
249 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
250 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
251
252 $ret;
253 }
254 sub out {
255 my $self = shift;
256 sprintf $masm?"%s":"%%%s",$self->{value};
257 }
258}
259{ package label; # pick up labels, which end with :
260 sub re {
261 my $self = shift; # single instance is enough...
262 local *line = shift;
263 undef $ret;
264
265 if ($line =~ /(^[\.\w]+\:)/) {
266 $self->{value} = $1;
267 $ret = $self;
268 $line = substr($line,@+[0]); $line =~ s/^\s+//;
269
270 $self->{value} =~ s/\.L/\$L/ if ($masm);
271 }
272 $ret;
273 }
274 sub out {
275 my $self = shift;
276
277 if (!$masm) {
278 $self->{value};
279 } elsif ($self->{value} ne "$current_function->{name}:") {
280 $self->{value};
281 } elsif ($current_function->{abi} eq "svr4") {
282 my $func = "$current_function->{name} PROC\n".
283 " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
284 " mov QWORD PTR 16[rsp],rsi\n";
285 my $narg = $current_function->{narg};
286 $narg=6 if (!defined($narg));
287 $func .= " mov rdi,rcx\n" if ($narg>0);
288 $func .= " mov rsi,rdx\n" if ($narg>1);
289 $func .= " mov rdx,r8\n" if ($narg>2);
290 $func .= " mov rcx,r9\n" if ($narg>3);
291 $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
292 $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
293 $func .= "\n";
294 } else {
295 "$current_function->{name} PROC";
296 }
297 }
298}
299{ package expr; # pick up expressioins
300 sub re {
301 my $self = shift; # single instance is enough...
302 local *line = shift;
303 undef $ret;
304
305 if ($line =~ /(^[^,]+)/) {
306 $self->{value} = $1;
307 $ret = $self;
308 $line = substr($line,@+[0]); $line =~ s/^\s+//;
309
310 $self->{value} =~ s/\.L/\$L/g if ($masm);
311 }
312 $ret;
313 }
314 sub out {
315 my $self = shift;
316 $self->{value};
317 }
318}
319{ package directive; # pick up directives, which start with .
320 sub re {
321 my $self = shift; # single instance is enough...
322 local *line = shift;
323 undef $ret;
324 my $dir;
325 my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
326 ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
327 "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
328 "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
329 "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
330 "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
331 "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
332 "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
333 "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
334
335 if ($line =~ /^\s*(\.\w+)/) {
336 if (!$masm) {
337 $self->{value} = $1;
338 $line =~ s/\@abi\-omnipotent/\@function/;
339 $line =~ s/\@function.*/\@function/;
340 if ($line =~ /\.picmeup\s+(%r[\w]+)/i) {
341 $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1};
342 } elsif ($line =~ /\.asciz\s+"(.*)"$/) {
343 $self->{value} = ".byte\t".join(",",unpack("C*",$1),0);
344 } elsif ($line =~ /\.extern/) {
345 $self->{value} = ""; # swallow extern
346 } else {
347 $self->{value} = $line;
348 }
349 $line = "";
350 return $self;
351 }
352
353 $dir = $1;
354 $ret = $self;
355 undef $self->{value};
356 $line = substr($line,@+[0]); $line =~ s/^\s+//;
357 SWITCH: for ($dir) {
358 /\.(text)/
359 && do { my $v=undef;
360 $v="$current_segment\tENDS\n" if ($current_segment);
361 $current_segment = "_$1\$";
362 $current_segment =~ tr/[a-z]/[A-Z]/;
363 $v.="$current_segment\tSEGMENT ";
364 $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE";
365 $v.=" 'CODE'";
366 $self->{value} = $v;
367 last;
368 };
369 /\.extern/ && do { $self->{value} = "EXTRN\t".$line.":BYTE"; last; };
370 /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
371 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
372 if ($type eq "\@function") {
373 undef $current_function;
374 $current_function->{name} = $sym;
375 $current_function->{abi} = "svr4";
376 $current_function->{narg} = $narg;
377 } elsif ($type eq "\@abi-omnipotent") {
378 undef $current_function;
379 $current_function->{name} = $sym;
380 }
381 last;
382 };
383 /\.size/ && do { if (defined($current_function)) {
384 $self->{value}="$current_function->{name}\tENDP";
385 undef $current_function;
386 }
387 last;
388 };
389 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
390 /\.(byte|value|long|quad)/
391 && do { my @arr = split(',',$line);
392 my $sz = substr($1,0,1);
393 my $last = pop(@arr);
394 my $conv = sub { my $var=shift;
395 if ($var=~s/0x([0-9a-f]+)/0$1h/i) { $var; }
396 else { sprintf"0%Xh",$var; }
397 };
398
399 $sz =~ tr/bvlq/BWDQ/;
400 $self->{value} = "\tD$sz\t";
401 for (@arr) { $self->{value} .= &$conv($_).","; }
402 $self->{value} .= &$conv($last);
403 last;
404 };
405 /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line};
406 last;
407 };
408 /\.asciz/ && do { if ($line =~ /^"(.*)"$/) {
409 my @str=unpack("C*",$1);
410 push @str,0;
411 while ($#str>15) {
412 $self->{value}.="DB\t"
413 .join(",",@str[0..15])."\n";
414 foreach (0..15) { shift @str; }
415 }
416 $self->{value}.="DB\t"
417 .join(",",@str) if (@str);
418 }
419 last;
420 };
421 }
422 $line = "";
423 }
424
425 $ret;
426 }
427 sub out {
428 my $self = shift;
429 $self->{value};
430 }
431}
432
433print "#include <machine/asm.h>\n";
434
435while($line=<>) {
436
437 chomp($line);
438
439 $line =~ s|[#!].*$||; # get rid of asm-style comments...
440 $line =~ s|/\*.*\*/||; # ... and C-style comments...
441 $line =~ s|^\s+||; # ... and skip white spaces in beginning
442
443 undef $label;
444 undef $opcode;
445 undef $dst;
446 undef $src;
447 undef $sz;
448
449 if ($label=label->re(\$line)) { print $label->out(); }
450
451 if (directive->re(\$line)) {
452 printf "%s",directive->out();
453 } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
454
455 if ($src=register->re(\$line)) { opcode->size($src->size()); }
456 elsif ($src=const->re(\$line)) { }
457 elsif ($src=ea->re(\$line)) { }
458 elsif ($src=expr->re(\$line)) { }
459
460 last ARGUMENT if ($line !~ /^,/);
461
462 $line = substr($line,1); $line =~ s/^\s+//;
463
464 if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
465 elsif ($dst=const->re(\$line)) { }
466 elsif ($dst=ea->re(\$line)) { }
467
468 } # ARGUMENT:
469
470 $sz=opcode->size();
471
472 if (defined($dst)) {
473 if (!$masm) {
474 printf "\t%s\t%s,%s", $opcode->out($dst->size()),
475 $src->out($sz),$dst->out($sz);
476 } else {
477 printf "\t%s\t%s,%s", $opcode->out(),
478 $dst->out($sz),$src->out($sz);
479 }
480 } elsif (defined($src)) {
481 printf "\t%s\t%s",$opcode->out(),$src->out($sz);
482 } else {
483 printf "\t%s",$opcode->out();
484 }
485 }
486
487 print $line,"\n";
488}
489
490print "\n$current_segment\tENDS\nEND\n" if ($masm);
491
492close STDOUT;
493
494#################################################
495# Cross-reference x86_64 ABI "card"
496#
497# Unix Win64
498# %rax * *
499# %rbx - -
500# %rcx #4 #1
501# %rdx #3 #2
502# %rsi #2 -
503# %rdi #1 -
504# %rbp - -
505# %rsp - -
506# %r8 #5 #3
507# %r9 #6 #4
508# %r10 * *
509# %r11 * *
510# %r12 - -
511# %r13 - -
512# %r14 - -
513# %r15 - -
514#
515# (*) volatile register
516# (-) preserved by callee
517# (#) Nth argument, volatile
518#
519# In Unix terms top of stack is argument transfer area for arguments
520# which could not be accomodated in registers. Or in other words 7th
521# [integer] argument resides at 8(%rsp) upon function entry point.
522# 128 bytes above %rsp constitute a "red zone" which is not touched
523# by signal handlers and can be used as temporal storage without
524# allocating a frame.
525#
526# In Win64 terms N*8 bytes on top of stack is argument transfer area,
527# which belongs to/can be overwritten by callee. N is the number of
528# arguments passed to callee, *but* not less than 4! This means that
529# upon function entry point 5th argument resides at 40(%rsp), as well
530# as that 32 bytes from 8(%rsp) can always be used as temporal
531# storage [without allocating a frame]. One can actually argue that
532# one can assume a "red zone" above stack pointer under Win64 as well.
533# Point is that at apparently no occasion Windows kernel would alter
534# the area above user stack pointer in true asynchronous manner...
535#
536# All the above means that if assembler programmer adheres to Unix
537# register and stack layout, but disregards the "red zone" existense,
538# it's possible to use following prologue and epilogue to "gear" from
539# Unix to Win64 ABI in leaf functions with not more than 6 arguments.
540#
541# omnipotent_function:
542# ifdef WIN64
543# movq %rdi,8(%rsp)
544# movq %rsi,16(%rsp)
545# movq %rcx,%rdi ; if 1st argument is actually present
546# movq %rdx,%rsi ; if 2nd argument is actually ...
547# movq %r8,%rdx ; if 3rd argument is ...
548# movq %r9,%rcx ; if 4th argument ...
549# movq 40(%rsp),%r8 ; if 5th ...
550# movq 48(%rsp),%r9 ; if 6th ...
551# endif
552# ...
553# ifdef WIN64
554# movq 8(%rsp),%rdi
555# movq 16(%rsp),%rsi
556# endif
557# ret
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl
new file mode 100644
index 0000000000..f535c9c7fa
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86asm.pl
@@ -0,0 +1,136 @@
1#!/usr/local/bin/perl
2
3# require 'x86asm.pl';
4# &asm_init("cpp","des-586.pl");
5# XXX
6# XXX
7# main'asm_finish
8
9sub main'asm_finish
10 {
11 &file_end();
12 &asm_finish_cpp() if $cpp;
13 print &asm_get_output();
14 }
15
16sub main'asm_init
17 {
18 ($type,$fn,$i386)=@_;
19 $filename=$fn;
20
21 $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=$openbsd=0;
22 if ( ($type eq "elf"))
23 { $elf=1; require "x86unix.pl"; }
24 elsif ( ($type eq "openbsd-elf"))
25 { $openbsd=$elf=1; require "x86unix.pl"; }
26 elsif ( ($type eq "openbsd-a.out"))
27 { $openbsd=1; require "x86unix.pl"; }
28 elsif ( ($type eq "a.out"))
29 { $aout=1; require "x86unix.pl"; }
30 elsif ( ($type eq "coff" or $type eq "gaswin"))
31 { $coff=1; require "x86unix.pl"; }
32 elsif ( ($type eq "cpp"))
33 { $cpp=1; require "x86unix.pl"; }
34 elsif ( ($type eq "win32"))
35 { $win32=1; require "x86ms.pl"; }
36 elsif ( ($type eq "win32n"))
37 { $win32=1; require "x86nasm.pl"; }
38 elsif ( ($type eq "nw-nasm"))
39 { $netware=1; require "x86nasm.pl"; }
40 elsif ( ($type eq "nw-mwasm"))
41 { $netware=1; $mwerks=1; require "x86nasm.pl"; }
42 else
43 {
44 print STDERR <<"EOF";
45Pick one target type from
46 elf - Linux, FreeBSD, Solaris x86, etc.
47 a.out - OpenBSD, DJGPP, etc.
48 coff - GAS/COFF such as Win32 targets
49 win32 - Windows 95/Windows NT
50 win32n - Windows 95/Windows NT NASM format
51 openbsd-elf - OpenBSD elf
52 openbsd-a.out - OpenBSD a.out
53 nw-nasm - NetWare NASM format
54 nw-mwasm- NetWare Metrowerks Assembler
55EOF
56 exit(1);
57 }
58
59 $pic=0;
60 for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
61
62 &asm_init_output();
63
64&comment("Don't even think of reading this code");
65&comment("It was automatically generated by $filename");
66&comment("Which is a perl program used to generate the x86 assember for");
67&comment("any of ELF, a.out, COFF, Win32, ...");
68&comment("eric <eay\@cryptsoft.com>");
69&comment("");
70
71 $filename =~ s/\.pl$//;
72 &file($filename);
73 }
74
75sub asm_finish_cpp
76 {
77 return unless $cpp;
78
79 local($tmp,$i);
80 foreach $i (&get_labels())
81 {
82 $tmp.="#define $i _$i\n";
83 }
84 print <<"EOF";
85/* Run the C pre-processor over this file with one of the following defined
86 * ELF - elf object files,
87 * OUT - a.out object files,
88 * BSDI - BSDI style a.out object files
89 * SOL - Solaris style elf
90 */
91
92#define TYPE(a,b) .type a,b
93#define SIZE(a,b) .size a,b
94
95#if defined(OUT) || (defined(BSDI) && !defined(ELF))
96$tmp
97#endif
98
99#ifdef OUT
100#define OK 1
101#define ALIGN 4
102#if defined(__CYGWIN__) || defined(__DJGPP__) || (__MINGW32__)
103#undef SIZE
104#undef TYPE
105#define SIZE(a,b)
106#define TYPE(a,b) .def a; .scl 2; .type 32; .endef
107#endif /* __CYGWIN || __DJGPP */
108#endif
109
110#if defined(BSDI) && !defined(ELF)
111#define OK 1
112#define ALIGN 4
113#undef SIZE
114#undef TYPE
115#define SIZE(a,b)
116#define TYPE(a,b)
117#endif
118
119#if defined(ELF) || defined(SOL)
120#define OK 1
121#define ALIGN 16
122#endif
123
124#ifndef OK
125You need to define one of
126ELF - elf systems - linux-elf, NetBSD and DG-UX
127OUT - a.out systems - linux-a.out and FreeBSD
128SOL - solaris systems, which are elf with strange comment lines
129BSDI - a.out with a very primative version of as.
130#endif
131
132/* Let the Assembler begin :-) */
133EOF
134 }
135
1361;
diff --git a/src/lib/libcrypto/perlasm/x86masm.pl b/src/lib/libcrypto/perlasm/x86masm.pl
new file mode 100644
index 0000000000..3d50e4a786
--- /dev/null
+++ b/src/lib/libcrypto/perlasm/x86masm.pl
@@ -0,0 +1,184 @@
1#!/usr/bin/env perl
2
3package x86masm;
4
5*out=\@::out;
6
7$::lbdecor="\$L"; # local label decoration
8$nmdecor="_"; # external name decoration
9
10$initseg="";
11$segment="";
12
13sub ::generic
14{ my ($opcode,@arg)=@_;
15
16 # fix hexadecimal constants
17 for (@arg) { s/0x([0-9a-f]+)/0$1h/oi; }
18
19 if ($opcode !~ /movq/)
20 { # fix xmm references
21 $arg[0] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[1]=~/\bxmm[0-7]\b/i);
22 $arg[1] =~ s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i if ($arg[0]=~/\bxmm[0-7]\b/i);
23 }
24
25 &::emit($opcode,@arg);
26 1;
27}
28#
29# opcodes not covered by ::generic above, mostly inconsistent namings...
30#
31sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
32sub ::call_ptr { &::emit("call",@_); }
33sub ::jmp_ptr { &::emit("jmp",@_); }
34
35sub get_mem
36{ my($size,$addr,$reg1,$reg2,$idx)=@_;
37 my($post,$ret);
38
39 $ret .= "$size PTR " if ($size ne "");
40
41 $addr =~ s/^\s+//;
42 # prepend global references with optional underscore
43 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
44 # put address arithmetic expression in parenthesis
45 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
46
47 if (($addr ne "") && ($addr ne 0))
48 { if ($addr !~ /^-/) { $ret .= "$addr"; }
49 else { $post=$addr; }
50 }
51 $ret .= "[";
52
53 if ($reg2 ne "")
54 { $idx!=0 or $idx=1;
55 $ret .= "$reg2*$idx";
56 $ret .= "+$reg1" if ($reg1 ne "");
57 }
58 else
59 { $ret .= "$reg1"; }
60
61 $ret .= "$post]";
62 $ret =~ s/\+\]/]/; # in case $addr was the only argument
63 $ret =~ s/\[\s*\]//;
64
65 $ret;
66}
67sub ::BP { &get_mem("BYTE",@_); }
68sub ::DWP { &get_mem("DWORD",@_); }
69sub ::QWP { &get_mem("QWORD",@_); }
70sub ::BC { "@_"; }
71sub ::DWC { "@_"; }
72
73sub ::file
74{ my $tmp=<<___;
75TITLE $_[0].asm
76IF \@Version LT 800
77ECHO MASM version 8.00 or later is strongly recommended.
78ENDIF
79.486
80.MODEL FLAT
81OPTION DOTNAME
82IF \@Version LT 800
83.text\$ SEGMENT PAGE 'CODE'
84ELSE
85.text\$ SEGMENT ALIGN(64) 'CODE'
86ENDIF
87___
88 push(@out,$tmp);
89 $segment = ".text\$";
90}
91
92sub ::function_begin_B
93{ my $func=shift;
94 my $global=($func !~ /^_/);
95 my $begin="${::lbdecor}_${func}_begin";
96
97 &::LABEL($func,$global?"$begin":"$nmdecor$func");
98 $func="ALIGN\t16\n".$nmdecor.$func."\tPROC";
99
100 if ($global) { $func.=" PUBLIC\n${begin}::\n"; }
101 else { $func.=" PRIVATE\n"; }
102 push(@out,$func);
103 $::stack=4;
104}
105sub ::function_end_B
106{ my $func=shift;
107
108 push(@out,"$nmdecor$func ENDP\n");
109 $::stack=0;
110 &::wipe_labels();
111}
112
113sub ::file_end
114{ my $xmmheader=<<___;
115.686
116.XMM
117IF \@Version LT 800
118XMMWORD STRUCT 16
119DQ 2 dup (?)
120XMMWORD ENDS
121ENDIF
122___
123 if (grep {/\b[x]?mm[0-7]\b/i} @out) {
124 grep {s/\.[3-7]86/$xmmheader/} @out;
125 }
126
127 push(@out,"$segment ENDS\n");
128
129 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
130 { my $comm=<<___;
131.bss SEGMENT 'BSS'
132COMM ${nmdecor}OPENSSL_ia32cap_P:DWORD
133.bss ENDS
134___
135 # comment out OPENSSL_ia32cap_P declarations
136 grep {s/(^EXTERN\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
137 push (@out,$comm);
138 }
139 push (@out,$initseg) if ($initseg);
140 push (@out,"END\n");
141}
142
143sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
144
145*::set_label_B = sub
146{ my $l=shift; push(@out,$l.($l=~/^\Q${::lbdecor}\E[0-9]{3}/?":\n":"::\n")); };
147
148sub ::external_label
149{ foreach(@_)
150 { push(@out, "EXTERN\t".&::LABEL($_,$nmdecor.$_).":NEAR\n"); }
151}
152
153sub ::public_label
154{ push(@out,"PUBLIC\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
155
156sub ::data_byte
157{ push(@out,("DB\t").join(',',@_)."\n"); }
158
159sub ::data_word
160{ push(@out,("DD\t").join(',',@_)."\n"); }
161
162sub ::align
163{ push(@out,"ALIGN\t$_[0]\n"); }
164
165sub ::picmeup
166{ my($dst,$sym)=@_;
167 &::lea($dst,&::DWP($sym));
168}
169
170sub ::initseg
171{ my $f=$nmdecor.shift;
172
173 $initseg.=<<___;
174.CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
175EXTERN $f:NEAR
176DD $f
177.CRT\$XCU ENDS
178___
179}
180
181sub ::dataseg
182{ push(@out,"$segment\tENDS\n_DATA\tSEGMENT\n"); $segment="_DATA"; }
183
1841;
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl
index ce2bed9bb2..fa38f89c09 100644
--- a/src/lib/libcrypto/perlasm/x86nasm.pl
+++ b/src/lib/libcrypto/perlasm/x86nasm.pl
@@ -1,166 +1,455 @@
1#!/usr/bin/env perl 1#!/usr/local/bin/perl
2 2
3package x86nasm; 3package x86nasm;
4 4
5*out=\@::out; 5$label="L000";
6$under=($main'netware)?'':'_';
6 7
7$::lbdecor="L\$"; # local label decoration 8%lb=( 'eax', 'al',
8$nmdecor=$::netware?"":"_"; # external name decoration 9 'ebx', 'bl',
9$drdecor=$::mwerks?".":""; # directive decoration 10 'ecx', 'cl',
11 'edx', 'dl',
12 'ax', 'al',
13 'bx', 'bl',
14 'cx', 'cl',
15 'dx', 'dl',
16 );
10 17
11$initseg=""; 18%hb=( 'eax', 'ah',
19 'ebx', 'bh',
20 'ecx', 'ch',
21 'edx', 'dh',
22 'ax', 'ah',
23 'bx', 'bh',
24 'cx', 'ch',
25 'dx', 'dh',
26 );
12 27
13sub ::generic 28sub main'asm_init_output { @out=(); }
14{ my $opcode=shift; 29sub main'asm_get_output { return(@out); }
15 my $tmp; 30sub main'get_labels { return(@labels); }
16 31
17 if (!$::mwerks) 32sub main'external_label
18 { if ($opcode =~ m/^j/o && $#_==0) # optimize jumps 33{
19 { $_[0] = "NEAR $_[0]"; } 34 push(@labels,@_);
20 elsif ($opcode eq "lea" && $#_==1) # wipe storage qualifier from lea 35 foreach (@_) {
21 { $_[1] =~ s/^[^\[]*\[/\[/o; } 36 push(@out,".") if ($main'mwerks);
22 } 37 push(@out, "extern\t${under}$_\n");
23 &::emit($opcode,@_); 38 }
24 1;
25} 39}
26# 40
27# opcodes not covered by ::generic above, mostly inconsistent namings... 41sub main'LB
28# 42 {
29sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } 43 (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
30sub ::call_ptr { &::emit("call",@_); } 44 return($lb{$_[0]});
31sub ::jmp_ptr { &::emit("jmp",@_); } 45 }
46
47sub main'HB
48 {
49 (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
50 return($hb{$_[0]});
51 }
52
53sub main'BP
54 {
55 &get_mem("BYTE",@_);
56 }
57
58sub main'DWP
59 {
60 &get_mem("DWORD",@_);
61 }
62
63sub main'QWP
64 {
65 &get_mem("",@_);
66 }
67
68sub main'BC
69 {
70 return (($main'mwerks)?"":"BYTE ")."@_";
71 }
72
73sub main'DWC
74 {
75 return (($main'mwerks)?"":"DWORD ")."@_";
76 }
77
78sub main'stack_push
79 {
80 my($num)=@_;
81 $stack+=$num*4;
82 &main'sub("esp",$num*4);
83 }
84
85sub main'stack_pop
86 {
87 my($num)=@_;
88 $stack-=$num*4;
89 &main'add("esp",$num*4);
90 }
32 91
33sub get_mem 92sub get_mem
34{ my($size,$addr,$reg1,$reg2,$idx)=@_; 93 {
35 my($post,$ret); 94 my($size,$addr,$reg1,$reg2,$idx)=@_;
36 95 my($t,$post);
37 if ($size ne "") 96 my($ret)=$size;
38 { $ret .= "$size"; 97 if ($ret ne "")
39 $ret .= " PTR" if ($::mwerks); 98 {
40 $ret .= " "; 99 $ret .= " PTR" if ($main'mwerks);
41 } 100 $ret .= " ";
42 $ret .= "["; 101 }
43 102 $ret .= "[";
44 $addr =~ s/^\s+//; 103 $addr =~ s/^\s+//;
45 # prepend global references with optional underscore 104 if ($addr =~ /^(.+)\+(.+)$/)
46 $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige; 105 {
47 # put address arithmetic expression in parenthesis 106 $reg2=&conv($1);
48 $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/); 107 $addr="$under$2";
49 108 }
50 if (($addr ne "") && ($addr ne 0)) 109 elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
51 { if ($addr !~ /^-/) { $ret .= "$addr+"; } 110 {
52 else { $post=$addr; } 111 $addr="$under$addr";
53 } 112 }
54 113
55 if ($reg2 ne "") 114 if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
56 { $idx!=0 or $idx=1; 115
57 $ret .= "$reg2*$idx"; 116 $reg1="$regs{$reg1}" if defined($regs{$reg1});
58 $ret .= "+$reg1" if ($reg1 ne ""); 117 $reg2="$regs{$reg2}" if defined($regs{$reg2});
59 } 118 if (($addr ne "") && ($addr ne 0))
60 else 119 {
61 { $ret .= "$reg1"; } 120 if ($addr !~ /^-/)
62 121 { $ret.="${addr}+"; }
63 $ret .= "$post]"; 122 else { $post=$addr; }
64 $ret =~ s/\+\]/]/; # in case $addr was the only argument 123 }
65 124 if ($reg2 ne "")
66 $ret; 125 {
67} 126 $t="";
68sub ::BP { &get_mem("BYTE",@_); } 127 $t="*$idx" if ($idx != 0);
69sub ::DWP { &get_mem("DWORD",@_); } 128 $reg1="+".$reg1 if ("$reg1$post" ne "");
70sub ::QWP { &get_mem("",@_); } 129 $ret.="$reg2$t$reg1$post]";
71sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; } 130 }
72sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; } 131 else
73 132 {
74sub ::file 133 $ret.="$reg1$post]"
75{ if ($::mwerks) { push(@out,".section\t.text,64\n"); } 134 }
76 else 135 $ret =~ s/\+\]/]/; # in case $addr was the only argument
77 { my $tmp=<<___; 136 return($ret);
78%ifidn __OUTPUT_FORMAT__,obj 137 }
79section code use32 class=code align=64 138
80%elifidn __OUTPUT_FORMAT__,win32 139sub main'mov { &out2("mov",@_); }
81\$\@feat.00 equ 1 140sub main'movb { &out2("mov",@_); }
82section .text code align=64 141sub main'and { &out2("and",@_); }
142sub main'or { &out2("or",@_); }
143sub main'shl { &out2("shl",@_); }
144sub main'shr { &out2("shr",@_); }
145sub main'xor { &out2("xor",@_); }
146sub main'xorb { &out2("xor",@_); }
147sub main'add { &out2("add",@_); }
148sub main'adc { &out2("adc",@_); }
149sub main'sub { &out2("sub",@_); }
150sub main'sbb { &out2("sbb",@_); }
151sub main'rotl { &out2("rol",@_); }
152sub main'rotr { &out2("ror",@_); }
153sub main'exch { &out2("xchg",@_); }
154sub main'cmp { &out2("cmp",@_); }
155sub main'lea { &out2("lea",@_); }
156sub main'mul { &out1("mul",@_); }
157sub main'imul { &out2("imul",@_); }
158sub main'div { &out1("div",@_); }
159sub main'dec { &out1("dec",@_); }
160sub main'inc { &out1("inc",@_); }
161sub main'jmp { &out1("jmp",@_); }
162sub main'jmp_ptr { &out1p("jmp",@_); }
163
164# This is a bit of a kludge: declare all branches as NEAR.
165$near=($main'mwerks)?'':'NEAR';
166sub main'je { &out1("je $near",@_); }
167sub main'jle { &out1("jle $near",@_); }
168sub main'jz { &out1("jz $near",@_); }
169sub main'jge { &out1("jge $near",@_); }
170sub main'jl { &out1("jl $near",@_); }
171sub main'ja { &out1("ja $near",@_); }
172sub main'jae { &out1("jae $near",@_); }
173sub main'jb { &out1("jb $near",@_); }
174sub main'jbe { &out1("jbe $near",@_); }
175sub main'jc { &out1("jc $near",@_); }
176sub main'jnc { &out1("jnc $near",@_); }
177sub main'jnz { &out1("jnz $near",@_); }
178sub main'jne { &out1("jne $near",@_); }
179sub main'jno { &out1("jno $near",@_); }
180
181sub main'push { &out1("push",@_); $stack+=4; }
182sub main'pop { &out1("pop",@_); $stack-=4; }
183sub main'pushf { &out0("pushfd"); $stack+=4; }
184sub main'popf { &out0("popfd"); $stack-=4; }
185sub main'bswap { &out1("bswap",@_); &using486(); }
186sub main'not { &out1("not",@_); }
187sub main'call { &out1("call",($_[0]=~/^\@L/?'':$under).$_[0]); }
188sub main'call_ptr { &out1p("call",@_); }
189sub main'ret { &out0("ret"); }
190sub main'nop { &out0("nop"); }
191sub main'test { &out2("test",@_); }
192sub main'bt { &out2("bt",@_); }
193sub main'leave { &out0("leave"); }
194sub main'cpuid { &out0("cpuid"); }
195sub main'rdtsc { &out0("rdtsc"); }
196sub main'halt { &out0("hlt"); }
197sub main'movz { &out2("movzx",@_); }
198sub main'neg { &out1("neg",@_); }
199sub main'cld { &out0("cld"); }
200
201# SSE2
202sub main'emms { &out0("emms"); }
203sub main'movd { &out2("movd",@_); }
204sub main'movq { &out2("movq",@_); }
205sub main'movdqu { &out2("movdqu",@_); }
206sub main'movdqa { &out2("movdqa",@_); }
207sub main'movdq2q{ &out2("movdq2q",@_); }
208sub main'movq2dq{ &out2("movq2dq",@_); }
209sub main'paddq { &out2("paddq",@_); }
210sub main'pmuludq{ &out2("pmuludq",@_); }
211sub main'psrlq { &out2("psrlq",@_); }
212sub main'psllq { &out2("psllq",@_); }
213sub main'pxor { &out2("pxor",@_); }
214sub main'por { &out2("por",@_); }
215sub main'pand { &out2("pand",@_); }
216
217sub out2
218 {
219 my($name,$p1,$p2)=@_;
220 my($l,$t);
221
222 push(@out,"\t$name\t");
223 if (!$main'mwerks and $name eq "lea")
224 {
225 $p1 =~ s/^[^\[]*\[/\[/;
226 $p2 =~ s/^[^\[]*\[/\[/;
227 }
228 $t=&conv($p1).",";
229 $l=length($t);
230 push(@out,$t);
231 $l=4-($l+9)/8;
232 push(@out,"\t" x $l);
233 push(@out,&conv($p2));
234 push(@out,"\n");
235 }
236
237sub out0
238 {
239 my($name)=@_;
240
241 push(@out,"\t$name\n");
242 }
243
244sub out1
245 {
246 my($name,$p1)=@_;
247 my($l,$t);
248 push(@out,"\t$name\t".&conv($p1)."\n");
249 }
250
251sub conv
252 {
253 my($p)=@_;
254 $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
255 return $p;
256 }
257
258sub using486
259 {
260 return if $using486;
261 $using486++;
262 grep(s/\.386/\.486/,@out);
263 }
264
265sub main'file
266 {
267 if ($main'mwerks) { push(@out,".section\t.text\n"); }
268 else {
269 local $tmp=<<___;
270%ifdef __omf__
271section code use32 class=code
83%else 272%else
84section .text code 273section .text
85%endif 274%endif
86___ 275___
276 push(@out,$tmp);
277 }
278 }
279
280sub main'function_begin
281 {
282 my($func,$extra)=@_;
283
284 push(@labels,$func);
285 push(@out,".") if ($main'mwerks);
286 my($tmp)=<<"EOF";
287global $under$func
288$under$func:
289 push ebp
290 push ebx
291 push esi
292 push edi
293EOF
87 push(@out,$tmp); 294 push(@out,$tmp);
88 } 295 $stack=20;
89} 296 }
90 297
91sub ::function_begin_B 298sub main'function_begin_B
92{ my $func=shift; 299 {
93 my $global=($func !~ /^_/); 300 my($func,$extra)=@_;
94 my $begin="${::lbdecor}_${func}_begin"; 301 push(@out,".") if ($main'mwerks);
302 my($tmp)=<<"EOF";
303global $under$func
304$under$func:
305EOF
306 push(@out,$tmp);
307 $stack=4;
308 }
95 309
96 $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops 310sub main'function_end
311 {
312 my($func)=@_;
97 313
98 &::LABEL($func,$global?"$begin":"$nmdecor$func"); 314 my($tmp)=<<"EOF";
99 $func=$nmdecor.$func; 315 pop edi
316 pop esi
317 pop ebx
318 pop ebp
319 ret
320EOF
321 push(@out,$tmp);
322 $stack=0;
323 %label=();
324 }
100 325
101 push(@out,"${drdecor}global $func\n") if ($global); 326sub main'function_end_B
102 push(@out,"${drdecor}align 16\n"); 327 {
103 push(@out,"$func:\n"); 328 $stack=0;
104 push(@out,"$begin:\n") if ($global); 329 %label=();
105 $::stack=4; 330 }
106}
107 331
108sub ::function_end_B 332sub main'function_end_A
109{ $::stack=0; 333 {
110 &::wipe_labels(); 334 my($func)=@_;
111}
112 335
113sub ::file_end 336 my($tmp)=<<"EOF";
114{ if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) 337 pop edi
115 { my $comm=<<___; 338 pop esi
116${drdecor}segment .bss 339 pop ebx
117${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 4 340 pop ebp
118___ 341 ret
119 # comment out OPENSSL_ia32cap_P declarations 342EOF
120 grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out; 343 push(@out,$tmp);
121 push (@out,$comm) 344 }
122 }
123 push (@out,$initseg) if ($initseg);
124}
125 345
126sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } } 346sub main'file_end
347 {
348 }
127 349
128sub ::external_label 350sub main'wparam
129{ foreach(@_) 351 {
130 { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); } 352 my($num)=@_;
131}
132 353
133sub ::public_label 354 return(&main'DWP($stack+$num*4,"esp","",0));
134{ push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); } 355 }
135 356
136sub ::data_byte 357sub main'swtmp
137{ push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); } 358 {
359 return(&main'DWP($_[0]*4,"esp","",0));
360 }
138 361
139sub ::data_word 362# Should use swtmp, which is above esp. Linix can trash the stack above esp
140{ push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); } 363#sub main'wtmp
364# {
365# my($num)=@_;
366#
367# return(&main'DWP(-(($num+1)*4),"esp","",0));
368# }
141 369
142sub ::align 370sub main'comment
143{ push(@out,"${drdecor}align\t$_[0]\n"); } 371 {
372 foreach (@_)
373 {
374 push(@out,"\t; $_\n");
375 }
376 }
144 377
145sub ::picmeup 378sub main'public_label
146{ my($dst,$sym)=@_; 379 {
147 &::lea($dst,&::DWP($sym)); 380 $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
148} 381 push(@out,".") if ($main'mwerks);
382 push(@out,"global\t$label{$_[0]}\n");
383 }
149 384
150sub ::initseg 385sub main'label
151{ my $f=$nmdecor.shift; 386 {
152 if ($::win32) 387 if (!defined($label{$_[0]}))
153 { $initseg=<<___; 388 {
154segment .CRT\$XCU data align=4 389 $label{$_[0]}="\@${label}${_[0]}";
155extern $f 390 $label++;
156dd $f 391 }
157___ 392 return($label{$_[0]});
158 } 393 }
159}
160 394
161sub ::dataseg 395sub main'set_label
162{ if ($mwerks) { push(@out,".section\t.data,4\n"); } 396 {
163 else { push(@out,"section\t.data align=4\n"); } 397 if (!defined($label{$_[0]}))
164} 398 {
399 $label{$_[0]}="\@${label}${_[0]}";
400 $label++;
401 }
402 if ($_[1]!=0 && $_[1]>1)
403 {
404 main'align($_[1]);
405 }
406 push(@out,"$label{$_[0]}:\n");
407 }
408
409sub main'data_byte
410 {
411 push(@out,(($main'mwerks)?".byte\t":"DB\t").join(',',@_)."\n");
412 }
413
414sub main'data_word
415 {
416 push(@out,(($main'mwerks)?".long\t":"DD\t").join(',',@_)."\n");
417 }
418
419sub main'align
420 {
421 push(@out,".") if ($main'mwerks);
422 push(@out,"align\t$_[0]\n");
423 }
424
425sub out1p
426 {
427 my($name,$p1)=@_;
428 my($l,$t);
429
430 push(@out,"\t$name\t".&conv($p1)."\n");
431 }
432
433sub main'picmeup
434 {
435 local($dst,$sym)=@_;
436 &main'lea($dst,&main'DWP($sym));
437 }
438
439sub main'blindpop { &out1("pop",@_); }
440
441sub main'initseg
442 {
443 local($f)=@_;
444 if ($main'win32)
445 {
446 local($tmp)=<<___;
447segment .CRT\$XCU data
448extern $under$f
449DD $under$f
450___
451 push(@out,$tmp);
452 }
453 }
165 454
1661; 4551;
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl
index a4c947165e..ae8f0964dc 100644
--- a/src/lib/libcrypto/perlasm/x86unix.pl
+++ b/src/lib/libcrypto/perlasm/x86unix.pl
@@ -16,6 +16,12 @@ sub main'asm_get_output { return(@out); }
16sub main'get_labels { return(@labels); } 16sub main'get_labels { return(@labels); }
17sub main'external_label { push(@labels,@_); } 17sub main'external_label { push(@labels,@_); }
18 18
19if ($main'openbsd)
20 {
21 $com_start='/*';
22 $com_end='*/';
23 }
24
19if ($main'cpp) 25if ($main'cpp)
20 { 26 {
21 $align="ALIGN"; 27 $align="ALIGN";
@@ -338,6 +344,9 @@ sub main'file
338 { 344 {
339 local($file)=@_; 345 local($file)=@_;
340 346
347 if ($main'openbsd)
348 { push(@out,"#include <machine/asm.h>\n"); }
349
341 local($tmp)=<<"EOF"; 350 local($tmp)=<<"EOF";
342 .file "$file.s" 351 .file "$file.s"
343EOF 352EOF
@@ -346,11 +355,18 @@ EOF
346 355
347sub main'function_begin 356sub main'function_begin
348 { 357 {
349 local($func)=@_; 358 local($func,$junk,$llabel)=@_;
350 359
351 &main'external_label($func); 360 &main'external_label($func);
352 $func=$under.$func; 361 $func=$under.$func;
353 362
363 if ($main'openbsd)
364 {
365 push (@out, "\nENTRY($func)\n");
366 push (@out, "$llabel:\n") if $llabel;
367 goto skip;
368 }
369
354 local($tmp)=<<"EOF"; 370 local($tmp)=<<"EOF";
355.text 371.text
356.globl $func 372.globl $func
@@ -365,6 +381,7 @@ EOF
365 else { $tmp=push(@out,".type\t$func,\@function\n"); } 381 else { $tmp=push(@out,".type\t$func,\@function\n"); }
366 push(@out,".align\t$align\n"); 382 push(@out,".align\t$align\n");
367 push(@out,"$func:\n"); 383 push(@out,"$func:\n");
384skip:
368 $tmp=<<"EOF"; 385 $tmp=<<"EOF";
369 pushl %ebp 386 pushl %ebp
370 pushl %ebx 387 pushl %ebx
@@ -383,6 +400,47 @@ sub main'function_begin_B
383 &main'external_label($func); 400 &main'external_label($func);
384 $func=$under.$func; 401 $func=$under.$func;
385 402
403 if ($main'openbsd)
404 { push(@out, "\nENTRY($func)\n"); goto skip; }
405
406 local($tmp)=<<"EOF";
407.text
408.globl $func
409EOF
410 push(@out,$tmp);
411 if ($main'cpp)
412 { push(@out,"TYPE($func,\@function)\n"); }
413 elsif ($main'coff)
414 { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
415 elsif ($main'aout and !$main'pic)
416 { }
417 else { push(@out,".type $func,\@function\n"); }
418 push(@out,".align\t$align\n");
419 push(@out,"$func:\n");
420skip:
421 $stack=4;
422 }
423
424# Like function_begin_B but with static linkage
425sub main'function_begin_C
426 {
427 local($func,$extra)=@_;
428
429 &main'external_label($func);
430 $func=$under.$func;
431
432 if ($main'openbsd)
433 {
434 local($tmp)=<<"EOF";
435.text
436_ALIGN_TEXT
437.type $func,\@function
438$func:
439EOF
440 push(@out, $tmp);
441 goto skip;
442 }
443
386 local($tmp)=<<"EOF"; 444 local($tmp)=<<"EOF";
387.text 445.text
388.globl $func 446.globl $func
@@ -397,6 +455,7 @@ EOF
397 else { push(@out,".type $func,\@function\n"); } 455 else { push(@out,".type $func,\@function\n"); }
398 push(@out,".align\t$align\n"); 456 push(@out,".align\t$align\n");
399 push(@out,"$func:\n"); 457 push(@out,"$func:\n");
458skip:
400 $stack=4; 459 $stack=4;
401 } 460 }
402 461
@@ -457,6 +516,8 @@ sub main'function_end_B
457 %label=(); 516 %label=();
458 } 517 }
459 518
519sub main'function_end_C { function_end_B(@_); }
520
460sub main'wparam 521sub main'wparam
461 { 522 {
462 local($num)=@_; 523 local($num)=@_;
@@ -493,7 +554,7 @@ sub main'swtmp
493 554
494sub main'comment 555sub main'comment
495 { 556 {
496 if (!defined($com_start) or $main'elf) 557 if (!defined($com_start) or (!$main'openbsd && $main'elf))
497 { # Regarding $main'elf above... 558 { # Regarding $main'elf above...
498 # GNU and SVR4 as'es use different comment delimiters, 559 # GNU and SVR4 as'es use different comment delimiters,
499 push(@out,"\n"); # so we just skip ELF comments... 560 push(@out,"\n"); # so we just skip ELF comments...
@@ -534,7 +595,13 @@ sub main'set_label
534 if ($_[1]!=0) 595 if ($_[1]!=0)
535 { 596 {
536 if ($_[1]>1) { main'align($_[1]); } 597 if ($_[1]>1) { main'align($_[1]); }
537 else { push(@out,".align $align\n"); } 598 else
599 {
600 if ($main'openbsd)
601 { push(@out,"_ALIGN_TEXT\n"); }
602 else
603 { push(@out,".align $align\n"); }
604 }
538 } 605 }
539 push(@out,"$label{$_[0]}:\n"); 606 push(@out,"$label{$_[0]}:\n");
540 } 607 }
@@ -578,6 +645,10 @@ sub main'align
578 $val.=",0x90"; 645 $val.=",0x90";
579 } 646 }
580 push(@out,".align\t$val\n"); 647 push(@out,".align\t$val\n");
648 if ($main'openbsd)
649 { push(@out,"_ALIGN_TEXT\n"); }
650 else
651 { push(@out,".align $tval\n"); }
581 } 652 }
582 653
583# debug output functions: puts, putx, printf 654# debug output functions: puts, putx, printf
@@ -669,6 +740,16 @@ sub main'picmeup
669___ 740___
670 push(@out,$tmp); 741 push(@out,$tmp);
671 } 742 }
743 elsif ($main'openbsd)
744 {
745 push(@out, "#ifdef PIC\n");
746 push(@out, "\tPIC_PROLOGUE\n");
747 &main'mov($dst,"PIC_GOT($sym)");
748 push(@out, "\tPIC_EPILOGUE\n");
749 push(@out, "#else\n");
750 &main'lea($dst,&main'DWP($sym));
751 push(@out, "#endif\n");
752 }
672 elsif ($main'pic && ($main'elf || $main'aout)) 753 elsif ($main'pic && ($main'elf || $main'aout))
673 { 754 {
674 &main'call(&main'label("PIC_me_up")); 755 &main'call(&main'label("PIC_me_up"));
@@ -694,7 +775,9 @@ sub main'initseg
694 { 775 {
695 $tmp=<<___; 776 $tmp=<<___;
696.section .init 777.section .init
697 call $under$f 778 PIC_PROLOGUE
779 call PIC_PLT($under$f)
780 PIC_EPILOGUE
698 jmp .Linitalign 781 jmp .Linitalign
699.align $align 782.align $align
700.Linitalign: 783.Linitalign: