1#!/usr/bin/env perl 2 3package 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 16sub 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 $; 27sub ::generic 28{ my($opcode,@arg)=@_; 29 my($suffix,$dst,$src); 30 31 @arg=reverse(@arg); 32 33 for (@arg) 34 { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers 35 s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers 36 s/^(\-?[0-9]+)$/\$$1/o; # constants 37 s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants 38 } 39 40 $dst = $arg[$#arg] if ($#arg>=0); 41 $src = $arg[$#arg-1] if ($#arg>=1); 42 if ($dst =~ m/^%/o) { $suffix=&opsize($dst); } 43 elsif ($src =~ m/^%/o) { $suffix=&opsize($src); } 44 else { $suffix="l"; } 45 undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o); 46 47 if ($#_==0) { &::emit($opcode); } 48 elsif ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o) 49 { &::emit($opcode,@arg); } 50 else { &::emit($opcode.$suffix,@arg);} 51 52 1; 53} 54# 55# opcodes not covered by ::generic above, mostly inconsistent namings... 56# 57sub ::movzx { &::movzb(@_); } 58sub ::pushfd { &::pushfl; } 59sub ::popfd { &::popfl; } 60sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); } 61sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); } 62 63sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } 64sub ::call_ptr { &::generic("call","*$_[0]"); } 65sub ::jmp_ptr { &::generic("jmp","*$_[0]"); } 66 67*::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386); 68 69sub ::DWP 70{ my($addr,$reg1,$reg2,$idx)=@_; 71 my $ret=""; 72 73 $addr =~ s/^\s+//; 74 # prepend global references with optional underscore 75 $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige; 76 77 $reg1 = "%$reg1" if ($reg1); 78 $reg2 = "%$reg2" if ($reg2); 79 80 $ret .= $addr if (($addr ne "") && ($addr ne 0)); 81 82 if ($reg2) 83 { $idx!= 0 or $idx=1; 84 $ret .= "($reg1,$reg2,$idx)"; 85 } 86 elsif ($reg1) 87 { $ret .= "($reg1)"; } 88 89 $ret; 90} 91sub ::QWP { &::DWP(@_); } 92sub ::BP { &::DWP(@_); } 93sub ::WP { &::DWP(@_); } 94sub ::BC { @_; } 95sub ::DWC { @_; } 96 97sub ::file 98{ push(@out,".file\t\"$_[0].s\"\n.text\n"); } 99 100sub ::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 121sub ::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 128sub ::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 145sub ::external_label 146{ foreach(@_) { &::LABEL($_,$nmdecor.$_); } } 147 148sub ::public_label 149{ push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); } 150 151sub ::file_end 152{ if ($::macosx) 153 { if (%non_lazy_ptr) 154 { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n"); 155 foreach $i (keys %non_lazy_ptr) 156 { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); } 157 } 158 } 159 if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) { 160 push (@out, ".extern\t${nmdecor}OPENSSL_ia32cap_P\n"); 161 push (@out, ".hidden\t${nmdecor}OPENSSL_ia32cap_P\n"); 162 } 163 push(@out,$initseg) if ($initseg); 164} 165 166sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); } 167sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); } 168sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); } 169 170sub ::align 171{ my $val=$_[0],$p2,$i; 172 if ($::aout) 173 { for ($p2=0;$val!=0;$val>>=1) { $p2++; } 174 $val=$p2-1; 175 $val.=",0x90"; 176 } 177 push(@out,".align\t$val\n"); 178} 179 180# 181# PIC data access wrappers 182# 183# Usage: 184# picsetup($base) 185# - only allowed once per function (because of hardcoded label name), 186# sets up pic access, uses $base register as temporary 187# picsymbol($dst, $sym, $base) 188# - loads the address of symbol $sym into $dst with the help of $base 189# initialized by picsetup 190# picadjust($sym, $base) 191# - adjusts a code pointer read from a code_sym table with the help of 192# $base initialized by picsetup 193# code_sym($sym) 194# - emits a pointer to the given code symbol, relative to the GOT if 195# PIC. This pointer will need to be adjusted with picadjust above 196# before use. 197 198sub ::picsetup 199{ my($base)=@_; 200 201 if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) 202 { 203 &::call(&::label("PIC_setup")); 204 &::set_label("PIC_setup"); 205 &::blindpop($base); 206 if ($::macosx) 207 { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr"); 208 $non_lazy_ptr{"$nmdecor$sym"}=$indirect; 209 } 210 } 211} 212 213sub ::picsymbol 214{ my($dst,$sym,$base)=@_; 215 216 if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) 217 { 218 my $reflabel=&::label("PIC_setup"); 219 if ($::macosx) 220 { my $indirect=$non_lazy_ptr{"$nmdecor$sym"}; 221 &::mov($dst,&::DWP("$indirect-$reflabel",$base)); 222 } 223 else 224 { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", 225 $base)); 226 &::mov($dst,&::DWP("$sym\@GOT",$dst)); 227 } 228 } 229 else 230 { &::lea($dst,&::DWP($sym)); } 231} 232 233sub ::picadjust 234{ my($sym,$base)=@_; 235 236 if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) 237 { 238 my $reflabel=&::label("PIC_setup"); 239 &::lea($sym,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", 240 $base,$sym)); 241 } 242} 243 244sub ::code_sym 245{ my($sym)=@_; 246 247 if (($::pic && ($::openbsd || $::elf || $::aout)) || $::macosx) 248 { 249 $sym."\@GOTOFF"; 250 } 251 else 252 { 253 $sym; 254 } 255} 256 257sub ::initseg 258{ my $f=$nmdecor.shift; 259 260 if ($::openbsd) 261 { $initseg.=<<___; 262.section .init 263PIC_PROLOGUE 264 call PIC_PLT($f) 265PIC_EPILOGUE 266___ 267 } elsif ($::android) 268 { $initseg.=<<___; 269.section .init_array 270.align 4 271.long $f 272___ 273 } 274 elsif ($::elf) 275 { $initseg.=<<___; 276.section .init 277 call $f 278___ 279 } 280 elsif ($::coff) 281 { $initseg.=<<___; # applies to both Cygwin and Mingw 282.section .ctors 283.long $f 284___ 285 } 286 elsif ($::macosx) 287 { $initseg.=<<___; 288.mod_init_func 289.align 2 290.long $f 291___ 292 } 293 elsif ($::aout) 294 { my $ctor="${nmdecor}_GLOBAL_\$I\$$f"; 295 $initseg.=".text\n"; 296 $initseg.=".type $ctor,\@function\n" if ($::pic); 297 $initseg.=<<___; # OpenBSD way... 298.globl $ctor 299.align 2 300$ctor: 301 jmp $f 302___ 303 } 304} 305 306sub ::dataseg 307{ push(@out,".data\n"); } 308 309sub ::rodataseg 310{ push(@out,".section .rodata\n"); } 311 312sub ::previous 313{ push(@out,".previous\n"); } 314 3151; 316