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