x86gas.pl 6.3 KB

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