At least OpenBSD implements PIC in the same way ELF does.
[openssl.git] / crypto / perlasm / x86unix.pl
index 3e6ff685b7adfaa94dcaf83c2bc7f7e3bc9f9fe9..23a389f39e57e0abf1b3820251d04bbfbafe4e5f 100644 (file)
@@ -4,6 +4,7 @@ package x86unix;
 
 $label="L000";
 $const="";
+$constl=0;
 
 $align=($main'aout)?"4":"16";
 $under=($main'aout)?"_":"";
@@ -80,18 +81,18 @@ sub main'DWP
        local($addr,$reg1,$reg2,$idx)=@_;
 
        $ret="";
-       $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
+       $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
        $reg1="$regs{$reg1}" if defined($regs{$reg1});
        $reg2="$regs{$reg2}" if defined($regs{$reg2});
        $ret.=$addr if ($addr ne "") && ($addr ne 0);
        if ($reg2 ne "")
                {
-               if($idx ne "")
+               if($idx ne "" && $idx != 0)
                    { $ret.="($reg1,$reg2,$idx)"; }
                else
                    { $ret.="($reg1,$reg2)"; }
                }
-       else
+       elsif ($reg1 ne "")
                { $ret.="($reg1)" }
        return($ret);
        }
@@ -163,8 +164,10 @@ sub main'dec       { &out1("decl",@_); }
 sub main'inc   { &out1("incl",@_); }
 sub main'push  { &out1("pushl",@_); $stack+=4; }
 sub main'pop   { &out1("popl",@_); $stack-=4; }
+sub main'pushf { &out0("pushf"); $stack+=4; }
+sub main'popf  { &out0("popf"); $stack-=4; }
 sub main'not   { &out1("notl",@_); }
-sub main'call  { &out1("call",$under.$_[0]); }
+sub main'call  { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); }
 sub main'ret   { &out0("ret"); }
 sub main'nop   { &out0("nop"); }
 
@@ -342,13 +345,10 @@ sub main'function_end
        popl    %ebx
        popl    %ebp
        ret
+.${func}_end:
 EOF
        push(@out,$tmp);
 
-       push(@out,$const);
-       $const="";
-       push(@out,".${func}_end:\n");
-
        if ($main'cpp)
                { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
        elsif ($main'gaswin)
@@ -426,6 +426,11 @@ sub main'swtmp
 
 sub main'comment
        {
+       if ($main'elf)  # GNU and SVR4 as'es use different comment delimiters,
+               {       # so we just skip comments...
+               push(@out,"\n");
+               return;
+               }
        foreach (@_)
                {
                if (/^\s*$/)
@@ -458,6 +463,12 @@ sub main'set_label
 
 sub main'file_end
        {
+       if ($const ne "")
+               {
+               push(@out,".section .rodata\n");
+               push(@out,$const);
+               $const="";
+               }
        }
 
 sub main'data_word
@@ -465,23 +476,110 @@ sub main'data_word
        push(@out,"\t.long $_[0]\n");
        }
 
+# debug output functions: puts, putx, printf
+
 sub main'puts
        {
-           $constl++;
-           &main'push('$Lstring' . $constl);
-           &main'call('puts');
-           &main'add("esp",4);
-
-           $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
+       &pushvars();
+       &main'push('$Lstring' . ++$constl);
+       &main'call('puts');
+       $stack-=4;
+       &main'add("esp",4);
+       &popvars();
+
+       $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
        }
 
 sub main'putx
        {
-           $constl++;
-           &main'push($_[0]);
-           &main'push('$Lstring' . $constl);
-           &main'call('printf');
-           &main'add("esp",8);
+       &pushvars();
+       &main'push($_[0]);
+       &main'push('$Lstring' . ++$constl);
+       &main'call('printf');
+       &main'add("esp",8);
+       $stack-=8;
+       &popvars();
+
+       $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
+       }
+
+sub main'printf
+       {
+       $ostack = $stack;
+       &pushvars();
+       for ($i = @_ - 1; $i >= 0; $i--)
+               {
+               if ($i == 0) # change this to support %s format strings
+                       {
+                       &main'push('$Lstring' . ++$constl);
+                       $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
+                       }
+               else
+                       {
+                       if ($_[$i] =~ /([0-9]*)\(%esp\)/)
+                               {
+                               &main'push(($1 + $stack - $ostack) . '(%esp)');
+                               }
+                       else
+                               {
+                               &main'push($_[$i]);
+                               }
+                       }
+               }
+       &main'call('printf');
+       $stack-=4*@_;
+       &main'add("esp",4*@_);
+       &popvars();
+       }
 
-           $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
+sub pushvars
+       {
+       &main'pushf();
+       &main'push("edx");
+       &main'push("ecx");
+       &main'push("eax");
        }
+
+sub popvars
+       {
+       &main'pop("eax");
+       &main'pop("ecx");
+       &main'pop("edx");
+       &main'popf();
+       }
+
+sub main'picmeup
+       {
+       local($dst,$sym)=@_;
+       if ($main'cpp)
+               {
+               local($tmp)=<<___;
+#if (defined(ELF) || defined(SOL)) && defined(PIC)
+       .align  8
+       call    1f
+1:     popl    $regs{$dst}
+       addl    \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
+       movl    $sym\@GOT($regs{$dst}),$regs{$dst}
+#else
+       leal    $sym,$regs{$dst}
+#endif
+___
+               push(@out,$tmp);
+               }
+       elsif ($main'pic && ($main'elf || $main'aout))
+               {
+               push(@out,"\t.align\t8\n");
+               &main'call(&main'label("PIC_me_up"));
+               &main'set_label("PIC_me_up");
+               &main'blindpop($dst);
+               &main'add($dst,"\$$under"."_GLOBAL_OFFSET_TABLE_+[.-".
+                               &main'label("PIC_me_up") . "]");
+               &main'mov($dst,&main'DWP($sym."\@GOT",$dst));
+               }
+       else
+               {
+               &main'lea($dst,&main'DWP($sym));
+               }
+       }
+
+sub main'blindpop { &out1("popl",@_); }