2006-09-18 19:17:09 +00:00
|
|
|
#!/usr/bin/env perl
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
package x86gas;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
*out=\@::out;
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
$::lbdecor=$::aout?"L":".L"; # local label decoration
|
2007-11-24 16:03:57 +00:00
|
|
|
$nmdecor=($::aout or $::coff)?"_":""; # external name decoration
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
$initseg="";
|
2007-11-24 16:03:57 +00:00
|
|
|
|
|
|
|
$align=16;
|
|
|
|
$align=log($align)/log(2) if ($::aout);
|
2006-09-18 19:17:09 +00:00
|
|
|
$com_start="#" if ($::aout or $::coff);
|
|
|
|
|
|
|
|
sub opsize()
|
|
|
|
{ my $reg=shift;
|
|
|
|
if ($reg =~ m/^%e/o) { "l"; }
|
|
|
|
elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; }
|
|
|
|
elsif ($reg =~ m/^%[xm]/o) { undef; }
|
|
|
|
else { "w"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
# swap arguments;
|
|
|
|
# expand opcode with size suffix;
|
|
|
|
# prefix numeric constants with $;
|
|
|
|
sub ::generic
|
2008-12-17 19:56:48 +00:00
|
|
|
{ my($opcode,@arg)=@_;
|
|
|
|
my($suffix,$dst,$src);
|
|
|
|
|
|
|
|
@arg=reverse(@arg);
|
|
|
|
|
|
|
|
for (@arg)
|
|
|
|
{ s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers
|
|
|
|
s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers
|
|
|
|
s/^(\-?[0-9]+)$/\$$1/o; # constants
|
|
|
|
s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 19:56:48 +00:00
|
|
|
$dst = $arg[$#arg] if ($#arg>=0);
|
|
|
|
$src = $arg[$#arg-1] if ($#arg>=1);
|
2006-09-18 19:17:09 +00:00
|
|
|
if ($dst =~ m/^%/o) { $suffix=&opsize($dst); }
|
|
|
|
elsif ($src =~ m/^%/o) { $suffix=&opsize($src); }
|
|
|
|
else { $suffix="l"; }
|
|
|
|
undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);
|
|
|
|
|
|
|
|
if ($#_==0) { &::emit($opcode); }
|
|
|
|
elsif ($opcode =~ m/^j/o && $#_==1) { &::emit($opcode,@arg); }
|
|
|
|
elsif ($opcode eq "call" && $#_==1) { &::emit($opcode,@arg); }
|
2011-04-19 19:09:18 +00:00
|
|
|
elsif ($opcode eq "clflush" && $#_==1){ &::emit($opcode,@arg); }
|
2007-05-19 17:52:51 +00:00
|
|
|
elsif ($opcode =~ m/^set/&& $#_==1) { &::emit($opcode,@arg); }
|
2006-09-18 19:17:09 +00:00
|
|
|
else { &::emit($opcode.$suffix,@arg);}
|
|
|
|
|
|
|
|
1;
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
#
|
2006-09-18 19:17:09 +00:00
|
|
|
# opcodes not covered by ::generic above, mostly inconsistent namings...
|
1998-12-21 10:56:39 +00:00
|
|
|
#
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::movzx { &::movzb(@_); }
|
|
|
|
sub ::pushfd { &::pushfl; }
|
|
|
|
sub ::popfd { &::popfl; }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); }
|
|
|
|
sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); }
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::call_ptr { &::generic("call","*$_[0]"); }
|
|
|
|
sub ::jmp_ptr { &::generic("jmp","*$_[0]"); }
|
|
|
|
|
|
|
|
*::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386);
|
|
|
|
|
|
|
|
sub ::DWP
|
|
|
|
{ my($addr,$reg1,$reg2,$idx)=@_;
|
|
|
|
my $ret="";
|
|
|
|
|
|
|
|
$addr =~ s/^\s+//;
|
|
|
|
# prepend global references with optional underscore
|
2007-12-18 09:18:49 +00:00
|
|
|
$addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
|
2006-09-18 19:17:09 +00:00
|
|
|
|
|
|
|
$reg1 = "%$reg1" if ($reg1);
|
|
|
|
$reg2 = "%$reg2" if ($reg2);
|
|
|
|
|
|
|
|
$ret .= $addr if (($addr ne "") && ($addr ne 0));
|
|
|
|
|
|
|
|
if ($reg2)
|
|
|
|
{ $idx!= 0 or $idx=1;
|
|
|
|
$ret .= "($reg1,$reg2,$idx)";
|
|
|
|
}
|
|
|
|
elsif ($reg1)
|
|
|
|
{ $ret .= "($reg1)"; }
|
|
|
|
|
|
|
|
$ret;
|
|
|
|
}
|
|
|
|
sub ::QWP { &::DWP(@_); }
|
|
|
|
sub ::BP { &::DWP(@_); }
|
2010-06-04 13:13:18 +00:00
|
|
|
sub ::WP { &::DWP(@_); }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::BC { @_; }
|
|
|
|
sub ::DWC { @_; }
|
|
|
|
|
|
|
|
sub ::file
|
2007-12-18 09:18:49 +00:00
|
|
|
{ push(@out,".file\t\"$_[0].s\"\n.text\n"); }
|
2006-09-18 19:17:09 +00:00
|
|
|
|
|
|
|
sub ::function_begin_B
|
2007-12-18 09:18:49 +00:00
|
|
|
{ my $func=shift;
|
2007-11-24 16:03:57 +00:00
|
|
|
my $global=($func !~ /^_/);
|
2007-12-18 09:18:49 +00:00
|
|
|
my $begin="${::lbdecor}_${func}_begin";
|
2006-09-18 19:17:09 +00:00
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
&::LABEL($func,$global?"$begin":"$nmdecor$func");
|
2007-11-24 16:03:57 +00:00
|
|
|
$func=$nmdecor.$func;
|
2006-09-18 19:17:09 +00:00
|
|
|
|
2007-11-24 16:03:57 +00:00
|
|
|
push(@out,".globl\t$func\n") if ($global);
|
2006-09-18 19:17:09 +00:00
|
|
|
if ($::coff)
|
2008-07-22 08:44:31 +00:00
|
|
|
{ push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
|
2007-12-18 17:28:22 +00:00
|
|
|
elsif (($::aout and !$::pic) or $::macosx)
|
2006-09-18 19:17:09 +00:00
|
|
|
{ }
|
|
|
|
else
|
|
|
|
{ push(@out,".type $func,\@function\n"); }
|
|
|
|
push(@out,".align\t$align\n");
|
|
|
|
push(@out,"$func:\n");
|
2007-11-24 16:03:57 +00:00
|
|
|
push(@out,"$begin:\n") if ($global);
|
2006-09-18 19:17:09 +00:00
|
|
|
$::stack=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::function_end_B
|
2007-12-18 09:18:49 +00:00
|
|
|
{ my $func=shift;
|
|
|
|
push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
|
2006-09-18 19:17:09 +00:00
|
|
|
$::stack=0;
|
2007-12-18 09:18:49 +00:00
|
|
|
&::wipe_labels();
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ::comment
|
|
|
|
{
|
|
|
|
if (!defined($com_start) or $::elf)
|
|
|
|
{ # Regarding $::elf above...
|
2004-08-01 21:16:26 +00:00
|
|
|
# GNU and SVR4 as'es use different comment delimiters,
|
|
|
|
push(@out,"\n"); # so we just skip ELF comments...
|
2003-01-03 17:37:53 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
foreach (@_)
|
|
|
|
{
|
|
|
|
if (/^\s*$/)
|
1998-12-21 10:56:39 +00:00
|
|
|
{ push(@out,"\n"); }
|
1998-12-21 10:52:47 +00:00
|
|
|
else
|
1998-12-21 10:56:39 +00:00
|
|
|
{ push(@out,"\t$com_start $_ $com_end\n"); }
|
1998-12-21 10:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:18:49 +00:00
|
|
|
sub ::external_label
|
2008-01-04 22:58:50 +00:00
|
|
|
{ foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
|
2006-09-18 19:17:09 +00:00
|
|
|
|
|
|
|
sub ::public_label
|
2007-12-18 09:18:49 +00:00
|
|
|
{ push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
|
2006-09-18 19:17:09 +00:00
|
|
|
|
|
|
|
sub ::file_end
|
2011-05-18 16:28:53 +00:00
|
|
|
{ if ($::macosx)
|
2008-01-04 22:58:50 +00:00
|
|
|
{ if (%non_lazy_ptr)
|
2007-12-18 17:28:22 +00:00
|
|
|
{ push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
|
|
|
|
foreach $i (keys %non_lazy_ptr)
|
|
|
|
{ push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 16:28:53 +00:00
|
|
|
if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
|
|
|
|
my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,8";
|
|
|
|
if ($::elf) { push (@out,"$tmp,4\n"); }
|
|
|
|
else { push (@out,"$tmp\n"); }
|
|
|
|
}
|
2007-12-18 09:18:49 +00:00
|
|
|
push(@out,$initseg) if ($initseg);
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
|
2011-02-10 21:24:24 +00:00
|
|
|
sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); }
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
|
|
|
|
|
|
|
|
sub ::align
|
|
|
|
{ my $val=$_[0],$p2,$i;
|
|
|
|
if ($::aout)
|
|
|
|
{ for ($p2=0;$val!=0;$val>>=1) { $p2++; }
|
|
|
|
$val=$p2-1;
|
|
|
|
$val.=",0x90";
|
|
|
|
}
|
|
|
|
push(@out,".align\t$val\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ::picmeup
|
|
|
|
{ my($dst,$sym,$base,$reflabel)=@_;
|
|
|
|
|
|
|
|
if ($::pic && ($::elf || $::aout))
|
|
|
|
{ if (!defined($base))
|
|
|
|
{ &::call(&::label("PIC_me_up"));
|
|
|
|
&::set_label("PIC_me_up");
|
|
|
|
&::blindpop($dst);
|
2007-12-18 17:28:22 +00:00
|
|
|
$base=$dst;
|
|
|
|
$reflabel=&::label("PIC_me_up");
|
|
|
|
}
|
|
|
|
if ($::macosx)
|
|
|
|
{ my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
|
|
|
|
&::mov($dst,&::DWP("$indirect-$reflabel",$base));
|
|
|
|
$non_lazy_ptr{"$nmdecor$sym"}=$indirect;
|
2000-12-05 05:10:05 +00:00
|
|
|
}
|
2003-01-03 17:37:53 +00:00
|
|
|
else
|
2008-01-04 22:58:50 +00:00
|
|
|
{ &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
|
2006-09-18 19:17:09 +00:00
|
|
|
$base));
|
2008-01-04 22:58:50 +00:00
|
|
|
&::mov($dst,&::DWP("$sym\@GOT",$dst));
|
2002-12-13 17:56:14 +00:00
|
|
|
}
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ &::lea($dst,&::DWP($sym)); }
|
|
|
|
}
|
2002-12-13 17:56:14 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
sub ::initseg
|
2007-12-18 09:18:49 +00:00
|
|
|
{ my $f=$nmdecor.shift;
|
2004-07-26 20:18:55 +00:00
|
|
|
|
2006-09-18 19:17:09 +00:00
|
|
|
if ($::elf)
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $initseg.=<<___;
|
2004-08-29 16:10:27 +00:00
|
|
|
.section .init
|
2007-12-18 09:18:49 +00:00
|
|
|
call $f
|
2005-05-31 11:07:27 +00:00
|
|
|
jmp .Linitalign
|
2005-05-21 17:49:10 +00:00
|
|
|
.align $align
|
2005-05-31 11:07:27 +00:00
|
|
|
.Linitalign:
|
2004-07-26 20:18:55 +00:00
|
|
|
___
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
elsif ($::coff)
|
2007-12-18 09:18:49 +00:00
|
|
|
{ $initseg.=<<___; # applies to both Cygwin and Mingw
|
2004-08-29 16:10:27 +00:00
|
|
|
.section .ctors
|
2007-12-18 09:18:49 +00:00
|
|
|
.long $f
|
2007-12-18 17:28:22 +00:00
|
|
|
___
|
|
|
|
}
|
|
|
|
elsif ($::macosx)
|
|
|
|
{ $initseg.=<<___;
|
|
|
|
.mod_init_func
|
|
|
|
.align 2
|
|
|
|
.long $f
|
2004-08-29 16:10:27 +00:00
|
|
|
___
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
elsif ($::aout)
|
2007-12-18 09:18:49 +00:00
|
|
|
{ my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
|
|
|
|
$initseg.=".text\n";
|
|
|
|
$initseg.=".type $ctor,\@function\n" if ($::pic);
|
|
|
|
$initseg.=<<___; # OpenBSD way...
|
2004-08-29 21:36:37 +00:00
|
|
|
.globl $ctor
|
2004-08-29 16:10:27 +00:00
|
|
|
.align 2
|
2004-08-29 21:36:37 +00:00
|
|
|
$ctor:
|
2007-12-18 09:18:49 +00:00
|
|
|
jmp $f
|
2004-08-29 16:10:27 +00:00
|
|
|
___
|
2006-09-18 19:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-29 16:10:27 +00:00
|
|
|
|
2008-07-22 08:44:31 +00:00
|
|
|
sub ::dataseg
|
|
|
|
{ push(@out,".data\n"); }
|
|
|
|
|
2004-08-29 16:10:27 +00:00
|
|
|
1;
|