]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/perlasm/x86gas.pl
x86: Add endbranch to indirect branch targets for Intel CET
[thirdparty/openssl.git] / crypto / perlasm / x86gas.pl
CommitLineData
e0a65194
RS
1#! /usr/bin/env perl
2# Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
3#
a5d9549d 4# Licensed under the Apache License 2.0 (the "License"). You may not use
e0a65194
RS
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
d02b48c6 9
43d8f27d 10package x86gas;
d02b48c6 11
4b67fefe
AP
12*out=\@::out;
13
43d8f27d 14$::lbdecor=$::aout?"L":".L"; # local label decoration
8789af8d 15$nmdecor=($::aout or $::coff)?"_":""; # external name decoration
d02b48c6 16
43d8f27d 17$initseg="";
8789af8d
AP
18
19$align=16;
20$align=log($align)/log(2) if ($::aout);
4b67fefe
AP
21$com_start="#" if ($::aout or $::coff);
22
23sub opsize()
24{ my $reg=shift;
25 if ($reg =~ m/^%e/o) { "l"; }
26 elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; }
a98c648e 27 elsif ($reg =~ m/^%[yxm]/o) { undef; }
4b67fefe
AP
28 else { "w"; }
29}
30
31# swap arguments;
32# expand opcode with size suffix;
33# prefix numeric constants with $;
34sub ::generic
4db48824
AP
35{ my($opcode,@arg)=@_;
36 my($suffix,$dst,$src);
37
38 @arg=reverse(@arg);
39
40 for (@arg)
41 { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers
42 s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers
43 s/^(\-?[0-9]+)$/\$$1/o; # constants
44 s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants
4b67fefe
AP
45 }
46
4db48824
AP
47 $dst = $arg[$#arg] if ($#arg>=0);
48 $src = $arg[$#arg-1] if ($#arg>=1);
4b67fefe
AP
49 if ($dst =~ m/^%/o) { $suffix=&opsize($dst); }
50 elsif ($src =~ m/^%/o) { $suffix=&opsize($src); }
51 else { $suffix="l"; }
52 undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);
53
54 if ($#_==0) { &::emit($opcode); }
7ffa48ad
AP
55 elsif ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o)
56 { &::emit($opcode,@arg); }
4b67fefe
AP
57 else { &::emit($opcode.$suffix,@arg);}
58
59 1;
60}
58964a49 61#
4b67fefe 62# opcodes not covered by ::generic above, mostly inconsistent namings...
58964a49 63#
43d8f27d
AP
64sub ::movzx { &::movzb(@_); }
65sub ::pushfd { &::pushfl; }
66sub ::popfd { &::popfl; }
4b67fefe
AP
67sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); }
68sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); }
69
43d8f27d 70sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
4b67fefe
AP
71sub ::call_ptr { &::generic("call","*$_[0]"); }
72sub ::jmp_ptr { &::generic("jmp","*$_[0]"); }
73
74*::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386);
75
4b67fefe
AP
76sub ::DWP
77{ my($addr,$reg1,$reg2,$idx)=@_;
78 my $ret="";
79
c5cd28bd
AP
80 if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
81
4b67fefe
AP
82 $addr =~ s/^\s+//;
83 # prepend global references with optional underscore
43d8f27d 84 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
4b67fefe
AP
85
86 $reg1 = "%$reg1" if ($reg1);
87 $reg2 = "%$reg2" if ($reg2);
88
89 $ret .= $addr if (($addr ne "") && ($addr ne 0));
90
91 if ($reg2)
92 { $idx!= 0 or $idx=1;
93 $ret .= "($reg1,$reg2,$idx)";
94 }
95 elsif ($reg1)
96 { $ret .= "($reg1)"; }
97
98 $ret;
99}
100sub ::QWP { &::DWP(@_); }
101sub ::BP { &::DWP(@_); }
d08eae1b 102sub ::WP { &::DWP(@_); }
4b67fefe
AP
103sub ::BC { @_; }
104sub ::DWC { @_; }
105
106sub ::file
e195c8a2 107{ push(@out,".text\n"); }
4b67fefe
AP
108
109sub ::function_begin_B
43d8f27d 110{ my $func=shift;
8789af8d 111 my $global=($func !~ /^_/);
43d8f27d 112 my $begin="${::lbdecor}_${func}_begin";
4b67fefe 113
43d8f27d 114 &::LABEL($func,$global?"$begin":"$nmdecor$func");
8789af8d 115 $func=$nmdecor.$func;
4b67fefe 116
8789af8d 117 push(@out,".globl\t$func\n") if ($global);
4b67fefe 118 if ($::coff)
b94551e8 119 { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
df774284 120 elsif (($::aout and !$::pic) or $::macosx)
4b67fefe
AP
121 { }
122 else
123 { push(@out,".type $func,\@function\n"); }
124 push(@out,".align\t$align\n");
125 push(@out,"$func:\n");
8789af8d 126 push(@out,"$begin:\n") if ($global);
e558ae49 127 &::endbranch();
4b67fefe
AP
128 $::stack=4;
129}
130
131sub ::function_end_B
43d8f27d
AP
132{ my $func=shift;
133 push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
4b67fefe 134 $::stack=0;
43d8f27d 135 &::wipe_labels();
4b67fefe
AP
136}
137
138sub ::comment
139 {
140 if (!defined($com_start) or $::elf)
141 { # Regarding $::elf above...
34413fca
AP
142 # GNU and SVR4 as'es use different comment delimiters,
143 push(@out,"\n"); # so we just skip ELF comments...
3cc9a89d
AP
144 return;
145 }
d02b48c6
RE
146 foreach (@_)
147 {
148 if (/^\s*$/)
58964a49 149 { push(@out,"\n"); }
d02b48c6 150 else
58964a49 151 { push(@out,"\t$com_start $_ $com_end\n"); }
d02b48c6
RE
152 }
153 }
154
43d8f27d 155sub ::external_label
26e71a18 156{ foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
4b67fefe
AP
157
158sub ::public_label
43d8f27d 159{ push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
4b67fefe
AP
160
161sub ::file_end
c30a2505 162{ if ($::macosx)
26e71a18 163 { if (%non_lazy_ptr)
df774284
AP
164 { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
165 foreach $i (keys %non_lazy_ptr)
166 { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
167 }
168 }
c30a2505 169 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
c5cd28bd 170 my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
4a5397fb
AP
171 if ($::macosx) { push (@out,"$tmp,2\n"); }
172 elsif ($::elf) { push (@out,"$tmp,4\n"); }
c30a2505
AP
173 else { push (@out,"$tmp\n"); }
174 }
43d8f27d 175 push(@out,$initseg) if ($initseg);
4b67fefe
AP
176}
177
178sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
f84a8ea5 179sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); }
4b67fefe
AP
180sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
181
182sub ::align
f9c5e5d9 183{ my $val=$_[0];
4b67fefe 184 if ($::aout)
f9c5e5d9 185 { $val=int(log($val)/log(2));
4b67fefe
AP
186 $val.=",0x90";
187 }
188 push(@out,".align\t$val\n");
189}
190
191sub ::picmeup
192{ my($dst,$sym,$base,$reflabel)=@_;
193
ce876d83 194 if (($::pic && ($::elf || $::aout)) || $::macosx)
4b67fefe
AP
195 { if (!defined($base))
196 { &::call(&::label("PIC_me_up"));
197 &::set_label("PIC_me_up");
198 &::blindpop($dst);
df774284
AP
199 $base=$dst;
200 $reflabel=&::label("PIC_me_up");
201 }
202 if ($::macosx)
203 { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
204 &::mov($dst,&::DWP("$indirect-$reflabel",$base));
205 $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
b50118ca 206 }
ce876d83
AP
207 elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
208 { &::lea($dst,&::DWP("$sym-$reflabel",$base)); }
3cc9a89d 209 else
26e71a18 210 { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
4b67fefe 211 $base));
26e71a18 212 &::mov($dst,&::DWP("$sym\@GOT",$dst));
6f7ac8e1 213 }
4b67fefe
AP
214 }
215 else
216 { &::lea($dst,&::DWP($sym)); }
217}
6f7ac8e1 218
4b67fefe 219sub ::initseg
43d8f27d 220{ my $f=$nmdecor.shift;
14e21f86 221
8fcdb1e6
AP
222 if ($::android)
223 { $initseg.=<<___;
224.section .init_array
225.align 4
226.long $f
227___
228 }
229 elsif ($::elf)
43d8f27d 230 { $initseg.=<<___;
a8c65b40 231.section .init
43d8f27d 232 call $f
14e21f86 233___
4b67fefe
AP
234 }
235 elsif ($::coff)
43d8f27d 236 { $initseg.=<<___; # applies to both Cygwin and Mingw
a8c65b40 237.section .ctors
43d8f27d 238.long $f
df774284
AP
239___
240 }
241 elsif ($::macosx)
242 { $initseg.=<<___;
243.mod_init_func
244.align 2
245.long $f
a8c65b40 246___
4b67fefe
AP
247 }
248 elsif ($::aout)
43d8f27d
AP
249 { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
250 $initseg.=".text\n";
251 $initseg.=".type $ctor,\@function\n" if ($::pic);
252 $initseg.=<<___; # OpenBSD way...
16760a30 253.globl $ctor
a8c65b40 254.align 2
16760a30 255$ctor:
43d8f27d 256 jmp $f
a8c65b40 257___
4b67fefe 258 }
4b67fefe 259}
a8c65b40 260
b94551e8
AP
261sub ::dataseg
262{ push(@out,".data\n"); }
263
6206682a
AP
264*::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
265
a8c65b40 2661;