x86cpuid fixes.
[openssl.git] / crypto / perlasm / x86unix.pl
index 9ceabf0705e54e1da89ff4815178464c45c1dbe7..2b9e96c8db07264ae010888373e338a128f187e2 100644 (file)
-#!/usr/local/bin/perl
+#!/usr/bin/env perl
 
-package x86unix;
+package x86unix;       # GAS actually...
+
+*out=\@::out;
 
 $label="L000";
 $const="";
 $constl=0;
 
-$align=($main'aout)?"4":"16";
-$under=($main'aout)?"_":"";
-$com_start=($main'sol)?"/":"#";
-
-sub main'asm_init_output { @out=(); }
-sub main'asm_get_output { return(@out); }
-sub main'get_labels { return(@labels); }
-sub main'external_label { push(@labels,@_); }
-
-if ($main'cpp)
-       {
-       $align="ALIGN";
-       $under="";
-       $com_start='/*';
-       $com_end='*/';
-       }
-
-%lb=(  'eax',  '%al',
-       'ebx',  '%bl',
-       'ecx',  '%cl',
-       'edx',  '%dl',
-       'ax',   '%al',
-       'bx',   '%bl',
-       'cx',   '%cl',
-       'dx',   '%dl',
-       );
-
-%hb=(  'eax',  '%ah',
-       'ebx',  '%bh',
-       'ecx',  '%ch',
-       'edx',  '%dh',
-       'ax',   '%ah',
-       'bx',   '%bh',
-       'cx',   '%ch',
-       'dx',   '%dh',
-       );
-
-%regs=(        'eax',  '%eax',
-       'ebx',  '%ebx',
-       'ecx',  '%ecx',
-       'edx',  '%edx',
-       'esi',  '%esi',
-       'edi',  '%edi',
-       'ebp',  '%ebp',
-       'esp',  '%esp',
-       );
-
-%reg_val=(
-       'eax',  0x00,
-       'ebx',  0x03,
-       'ecx',  0x01,
-       'edx',  0x02,
-       'esi',  0x06,
-       'edi',  0x07,
-       'ebp',  0x05,
-       'esp',  0x04,
-       );
-
-sub main'LB
-       {
-       (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
-       return($lb{$_[0]});
-       }
-
-sub main'HB
-       {
-       (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
-       return($hb{$_[0]});
-       }
-
-sub main'DWP
-       {
-       local($addr,$reg1,$reg2,$idx)=@_;
-
-       $ret="";
-       $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 "")
-                   { $ret.="($reg1,$reg2,$idx)"; }
-               else
-                   { $ret.="($reg1,$reg2)"; }
-               }
-       else
-               { $ret.="($reg1)" }
-       return($ret);
-       }
-
-sub main'BP
-       {
-       return(&main'DWP(@_));
-       }
-
-sub main'BC
-       {
-       return @_;
-       }
-
-sub main'DWC
-       {
-       return @_;
-       }
-
-#sub main'BP
-#      {
-#      local($addr,$reg1,$reg2,$idx)=@_;
+$align=($::aout)?"4":"16";
+$under=($::aout or $::coff)?"_":"";
+$dot=($::aout)?"":".";
+$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
+{ my($opcode,$dst,$src)=@_;
+  my($tmp,$suffix,@arg);
+
+    if (defined($src))
+    {  $src =~ s/^(e?[a-dsixphl]{2})$/%$1/o;
+       $src =~ s/^(x?mm[0-7])$/%$1/o;
+       $src =~ s/^(\-?[0-9]+)$/\$$1/o;
+       $src =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o;
+       push(@arg,$src);
+    }
+    if (defined($dst))
+    {  $dst =~ s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o;
+       $dst =~ s/^(x?mm[0-7])$/%$1/o;
+       $dst =~ s/^(\-?[0-9]+)$/\$$1/o          if(!defined($src));
+       $dst =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o     if(!defined($src));
+       push(@arg,$dst);
+    }
+
+    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);        }
+    elsif ($opcode =~ m/^set/&& $#_==1)        { &::emit($opcode,@arg);        }
+    else                               { &::emit($opcode.$suffix,@arg);}
+
+  1;
+}
 #
-#      $ret="";
+# opcodes not covered by ::generic above, mostly inconsistent namings...
 #
-#      $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \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 "")
-#              { $ret.="($reg1,$reg2,$idx)"; }
-#      else
-#              { $ret.="($reg1)" }
-#      return($ret);
-#      }
-
-sub main'mov   { &out2("movl",@_); }
-sub main'movb  { &out2("movb",@_); }
-sub main'and   { &out2("andl",@_); }
-sub main'or    { &out2("orl",@_); }
-sub main'shl   { &out2("sall",@_); }
-sub main'shr   { &out2("shrl",@_); }
-sub main'xor   { &out2("xorl",@_); }
-sub main'xorb  { &out2("xorb",@_); }
-sub main'add   { &out2("addl",@_); }
-sub main'adc   { &out2("adcl",@_); }
-sub main'sub   { &out2("subl",@_); }
-sub main'rotl  { &out2("roll",@_); }
-sub main'rotr  { &out2("rorl",@_); }
-sub main'exch  { &out2("xchg",@_); }
-sub main'cmp   { &out2("cmpl",@_); }
-sub main'lea   { &out2("leal",@_); }
-sub main'mul   { &out1("mull",@_); }
-sub main'div   { &out1("divl",@_); }
-sub main'jmp   { &out1("jmp",@_); }
-sub main'jmp_ptr { &out1p("jmp",@_); }
-sub main'je    { &out1("je",@_); }
-sub main'jle   { &out1("jle",@_); }
-sub main'jne   { &out1("jne",@_); }
-sub main'jnz   { &out1("jnz",@_); }
-sub main'jz    { &out1("jz",@_); }
-sub main'jge   { &out1("jge",@_); }
-sub main'jl    { &out1("jl",@_); }
-sub main'jb    { &out1("jb",@_); }
-sub main'jc    { &out1("jc",@_); }
-sub main'jnc   { &out1("jnc",@_); }
-sub main'jno   { &out1("jno",@_); }
-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'ret   { &out0("ret"); }
-sub main'nop   { &out0("nop"); }
-
-# The bswapl instruction is new for the 486. Emulate if i386.
-sub main'bswap
-       {
-       if ($main'i386)
-               {
-               &main'comment("bswapl @_");
-               &main'exch(main'HB(@_),main'LB(@_));
-               &main'rotr(@_,16);
-               &main'exch(main'HB(@_),main'LB(@_));
-               }
-       else
-               {
-               &out1("bswapl",@_);
-               }
-       }
-
-sub out2
-       {
-       local($name,$p1,$p2)=@_;
-       local($l,$ll,$t);
-       local(%special)=(       "roll",0xD1C0,"rorl",0xD1C8,
-                               "rcll",0xD1D0,"rcrl",0xD1D8,
-                               "shll",0xD1E0,"shrl",0xD1E8,
-                               "sarl",0xD1F8);
-       
-       if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
-               {
-               $op=$special{$name}|$reg_val{$p1};
-               $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
-               $tmp2=sprintf(".byte %d\t",$op     &0xff);
-               push(@out,$tmp1);
-               push(@out,$tmp2);
-
-               $p2=&conv($p2);
-               $p1=&conv($p1);
-               &main'comment("$name $p2 $p1");
+sub ::movz     { &::movzb(@_);                 }
+sub ::pushf    { &::pushfl;                    }
+sub ::popf     { &::popfl;                     }
+sub ::cpuid    { &::emit(".byte\t0x0f,0xa2");  }
+sub ::rdtsc    { &::emit(".byte\t0x0f,0x31");  }
+
+sub ::call     { &::emit("call",(&islabel($_[0]) or "$under$_[0]")); }
+sub ::call_ptr { &::generic("call","*$_[0]");  }
+sub ::jmp_ptr  { &::generic("jmp","*$_[0]");   }
+
+*::bswap = sub { &::emit("bswap","%$_[0]");    } if (!$::i386);
+
+# chosen SSE instructions
+sub ::movq
+{ my($p1,$p2,$optimize)=@_;
+    if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
+    # movq between mmx registers can sink Intel CPUs
+    {  &::pshufw($p1,$p2,0xe4);        }
+    else
+    {  &::generic("movq",@_);  }
+}
+sub ::pshufw
+{ my($dst,$src,$magic)=@_;
+    &::emit("pshufw","\$$magic","%$src","%$dst");
+}
+
+sub ::DWP
+{ my($addr,$reg1,$reg2,$idx)=@_;
+  my $ret="";
+
+    $addr =~ s/^\s+//;
+    # prepend global references with optional underscore
+    $addr =~ s/^([^\+\-0-9][^\+\-]*)/islabel($1) or "$under$1"/ige;
+
+    $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(@_);   }
+sub ::BC       { @_;           }
+sub ::DWC      { @_;           }
+
+sub ::file
+{   push(@out,".file\t\"$_[0].s\"\n"); }
+
+sub ::function_begin_B
+{ my($func,$extra)=@_;
+  my $tmp;
+
+    &::external_label($func);
+    $func=$under.$func;
+
+    push(@out,".text\n.globl\t$func\n");
+    if ($::coff)
+    {  push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
+    elsif ($::aout and !$::pic)
+    { }
+    else
+    {  push(@out,".type        $func,\@function\n"); }
+    push(@out,".align\t$align\n");
+    push(@out,"$func:\n");
+    $::stack=4;
+}
+
+sub ::function_end_B
+{ my($func)=@_;
+
+    $func=$under.$func;
+    push(@out,"${dot}L_${func}_end:\n");
+    if ($::elf)
+    {  push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
+    $::stack=0;
+    %label=();
+}
+
+sub ::comment
+       {
+       if (!defined($com_start) or $::elf)
+               {       # Regarding $::elf above...
+                       # GNU and SVR4 as'es use different comment delimiters,
+               push(@out,"\n");        # so we just skip ELF comments...
                return;
                }
-
-       push(@out,"\t$name\t");
-       $t=&conv($p2).",";
-       $l=length($t);
-       push(@out,$t);
-       $ll=4-($l+9)/8;
-       $tmp1=sprintf("\t" x $ll);
-       push(@out,$tmp1);
-       push(@out,&conv($p1)."\n");
-       }
-
-sub out1
-       {
-       local($name,$p1)=@_;
-       local($l,$t);
-       local(%special)=("bswapl",0x0FC8);
-
-       if ((defined($special{$name})) && defined($regs{$p1}))
-               {
-               $op=$special{$name}|$reg_val{$p1};
-               $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
-               $tmp2=sprintf(".byte %d\t",$op     &0xff);
-               push(@out,$tmp1);
-               push(@out,$tmp2);
-
-               $p2=&conv($p2);
-               $p1=&conv($p1);
-               &main'comment("$name $p2 $p1");
-               return;
-               }
-
-       push(@out,"\t$name\t".&conv($p1)."\n");
-       }
-
-sub out1p
-       {
-       local($name,$p1)=@_;
-       local($l,$t);
-
-       push(@out,"\t$name\t*".&conv($p1)."\n");
-       }
-
-sub out0
-       {
-       push(@out,"\t$_[0]\n");
-       }
-
-sub conv
-       {
-       local($p)=@_;
-
-#      $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
-
-       $p=$regs{$p} if (defined($regs{$p}));
-
-       $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
-       $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
-       return $p;
-       }
-
-sub main'file
-       {
-       local($file)=@_;
-
-       local($tmp)=<<"EOF";
-       .file   "$file.s"
-       .version        "01.01"
-gcc2_compiled.:
-EOF
-       push(@out,$tmp);
-       }
-
-sub main'function_begin
-       {
-       local($func)=@_;
-
-       &main'external_label($func);
-       $func=$under.$func;
-
-       local($tmp)=<<"EOF";
-.text
-       .align $align
-.globl $func
-EOF
-       push(@out,$tmp);
-       if ($main'cpp)
-               { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
-       elsif ($main'gaswin)
-               { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
-       else    { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
-       push(@out,"$func:\n");
-       $tmp=<<"EOF";
-       pushl   %ebp
-       pushl   %ebx
-       pushl   %esi
-       pushl   %edi
-
-EOF
-       push(@out,$tmp);
-       $stack=20;
-       }
-
-sub main'function_begin_B
-       {
-       local($func,$extra)=@_;
-
-       &main'external_label($func);
-       $func=$under.$func;
-
-       local($tmp)=<<"EOF";
-.text
-       .align $align
-.globl $func
-EOF
-       push(@out,$tmp);
-       if ($main'cpp)
-               { push(@out,"\tTYPE($func,\@function)\n"); }
-       elsif ($main'gaswin)
-               { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
-       else    { push(@out,"\t.type    $func,\@function\n"); }
-       push(@out,"$func:\n");
-       $stack=4;
-       }
-
-sub main'function_end
-       {
-       local($func)=@_;
-
-       $func=$under.$func;
-
-       local($tmp)=<<"EOF";
-       popl    %edi
-       popl    %esi
-       popl    %ebx
-       popl    %ebp
-       ret
-.${func}_end:
-EOF
-       push(@out,$tmp);
-
-       if ($main'cpp)
-               { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
-       elsif ($main'gaswin)
-                { $tmp=push(@out,"\t.align 4\n"); }
-       else    { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
-       push(@out,".ident       \"$func\"\n");
-       $stack=0;
-       %label=();
-       }
-
-sub main'function_end_A
-       {
-       local($func)=@_;
-
-       local($tmp)=<<"EOF";
-       popl    %edi
-       popl    %esi
-       popl    %ebx
-       popl    %ebp
-       ret
-EOF
-       push(@out,$tmp);
-       }
-
-sub main'function_end_B
-       {
-       local($func)=@_;
-
-       $func=$under.$func;
-
-       push(@out,".L_${func}_end:\n");
-       if ($main'cpp)
-               { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
-        elsif ($main'gaswin)
-                { push(@out,"\t.align 4\n"); }
-       else    { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
-       push(@out,".ident       \"desasm.pl\"\n");
-       $stack=0;
-       %label=();
-       }
-
-sub main'wparam
-       {
-       local($num)=@_;
-
-       return(&main'DWP($stack+$num*4,"esp","",0));
-       }
-
-sub main'stack_push
-       {
-       local($num)=@_;
-       $stack+=$num*4;
-       &main'sub("esp",$num*4);
-       }
-
-sub main'stack_pop
-       {
-       local($num)=@_;
-       $stack-=$num*4;
-       &main'add("esp",$num*4);
-       }
-
-sub main'swtmp
-       {
-       return(&main'DWP($_[0]*4,"esp","",0));
-       }
-
-# Should use swtmp, which is above esp.  Linix can trash the stack above esp
-#sub main'wtmp
-#      {
-#      local($num)=@_;
-#
-#      return(&main'DWP(-($num+1)*4,"esp","",0));
-#      }
-
-sub main'comment
-       {
        foreach (@_)
                {
                if (/^\s*$/)
@@ -435,110 +162,151 @@ sub main'comment
                }
        }
 
-sub main'label
-       {
-       if (!defined($label{$_[0]}))
-               {
-               $label{$_[0]}=".${label}${_[0]}";
-               $label++;
-               }
-       return($label{$_[0]});
-       }
-
-sub main'set_label
-       {
-       if (!defined($label{$_[0]}))
-               {
-               $label{$_[0]}=".${label}${_[0]}";
-               $label++;
-               }
-       push(@out,".align $align\n") if ($_[1] != 0);
-       push(@out,"$label{$_[0]}:\n");
-       }
-
-sub main'file_end
-       {
-       if ($const ne "")
-               {
-               push(@out,".section .rodata\n");
-               push(@out,$const);
-               $const="";
-               }
-       }
-
-sub main'data_word
-       {
-       push(@out,"\t.long $_[0]\n");
-       }
-
-# debug output functions: puts, putx, printf
-
-sub main'puts
-       {
-       &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
-       {
-       &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();
-       }
-
-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 islabel    # see is argument is a known label
+{ my $i;
+    foreach $i (%label) { return $label{$i} if ($label{$i} eq $_[0]); }
+  undef;
+}
+
+sub ::external_label { push(@labels,@_); }
+
+sub ::public_label
+{   $label{$_[0]}="${under}${_[0]}"    if (!defined($label{$_[0]}));
+    push(@out,".globl\t$label{$_[0]}\n");
+}
+
+sub ::label
+{   if (!defined($label{$_[0]}))
+    {  $label{$_[0]}="${dot}${label}${_[0]}"; $label++;   }
+  $label{$_[0]};
+}
+
+sub ::set_label
+{ my $label=&::label($_[0]);
+    &::align($_[1]) if ($_[1]>1);
+    push(@out,"$label:\n");
+}
+
+sub ::file_end
+{   # try to detect if SSE2 or MMX extensions were used on ELF platform...
+    if ($::elf && grep {/%[x]?mm[0-7]/i} @out){
+       my $tmp;
+
+       push (@out,"\n.section\t.bss\n");
+       push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
+
+       push (@out,".section\t.init\n");
+       # One can argue that it's wasteful to craft every
+       # SSE/MMX module with this snippet... Well, it's 72
+       # bytes long and for the moment we have two modules.
+       # Let's argue when we have 7 modules or so...
+       #
+       # $1<<10 sets a reserved bit to signal that variable
+       # was initialized already...
+       &::picmeup("edx","OPENSSL_ia32cap_P");
+       $tmp=<<___;
+       cmpl    \$0,(%edx)
+       jne     1f
+       movl    \$1<<10,(%edx)
+       pushf
+       popl    %eax
+       movl    %eax,%ecx
+       xorl    \$1<<21,%eax
+       pushl   %eax
+       popf
+       pushf
+       popl    %eax
+       xorl    %ecx,%eax
+       btl     \$21,%eax
+       jnc     1f
+       pushl   %edi
+       pushl   %ebx
+       movl    %edx,%edi
+       movl    \$1,%eax
+       .byte   0x0f,0xa2
+       orl     \$1<<10,%edx
+       movl    %edx,0(%edi)
+       popl    %ebx
+       popl    %edi
+       jmp     1f
+       .align  $align
+       1:
+___
+       push (@out,$tmp);
+    }
+
+    if ($const ne "")
+    {  push(@out,".section .rodata\n");
+       push(@out,$const);
+       $const="";
+    }
+}
+
+sub ::data_byte        {   push(@out,".byte\t".join(',',@_)."\n");   }
+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);
+           &::add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
+                           &::label("PIC_me_up") . "]");
        }
+       else
+       {   &::lea($dst,&::DWP("${under}_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
+                           $base));
+       }
+       &::mov($dst,&::DWP($under.$sym."\@GOT",$dst));
+    }
+    else
+    {  &::lea($dst,&::DWP($sym));      }
+}
+
+sub ::initseg
+{ my($f)=@_;
+  my($tmp,$ctor);
+
+    if ($::elf)
+    {  $tmp=<<___;
+.section       .init
+       call    $under$f
+       jmp     .Linitalign
+.align $align
+.Linitalign:
+___
+    }
+    elsif ($::coff)
+    {   $tmp=<<___;    # applies to both Cygwin and Mingw
+.section       .ctors
+.long  $under$f
+___
+    }
+    elsif ($::aout)
+    {  $ctor="${under}_GLOBAL_\$I\$$f";
+       $tmp=".text\n";
+       $tmp.=".type    $ctor,\@function\n" if ($::pic);
+       $tmp.=<<___;    # OpenBSD way...
+.globl $ctor
+.align 2
+$ctor:
+       jmp     $under$f
+___
+    }
+    push(@out,$tmp) if ($tmp);
+}
+
+1;