You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x86gas.pl 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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,".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 ($::macosx) {
  93. push(@out,".private_extern\t$func\n");
  94. } else {
  95. push(@out,".hidden\t$func\n");
  96. }
  97. if ($::coff)
  98. { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
  99. elsif (($::aout and !$::pic) or $::macosx)
  100. { }
  101. else
  102. { push(@out,".type $func,\@function\n"); }
  103. push(@out,".align\t$align\n");
  104. push(@out,"$func:\n");
  105. push(@out,"$begin:\n") if ($global);
  106. $::stack=4;
  107. }
  108. sub ::function_end_B
  109. { my $func=shift;
  110. push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
  111. $::stack=0;
  112. &::wipe_labels();
  113. }
  114. sub ::comment
  115. {
  116. if (!defined($com_start) or $::elf)
  117. { # Regarding $::elf above...
  118. # GNU and SVR4 as'es use different comment delimiters,
  119. push(@out,"\n"); # so we just skip ELF comments...
  120. return;
  121. }
  122. foreach (@_)
  123. {
  124. if (/^\s*$/)
  125. { push(@out,"\n"); }
  126. else
  127. { push(@out,"\t$com_start $_ $com_end\n"); }
  128. }
  129. }
  130. sub ::external_label
  131. { foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
  132. sub ::public_label
  133. { push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  134. sub ::file_end
  135. { if ($::macosx)
  136. { if (%non_lazy_ptr)
  137. { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
  138. foreach $i (keys %non_lazy_ptr)
  139. { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
  140. }
  141. }
  142. if (0 && grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
  143. my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
  144. if ($::macosx) { push (@out,"$tmp,2\n"); }
  145. elsif ($::elf) { push (@out,"$tmp,4\n"); }
  146. else { push (@out,"$tmp\n"); }
  147. }
  148. push(@out,$initseg) if ($initseg);
  149. }
  150. sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
  151. sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); }
  152. sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
  153. sub ::align
  154. { my $val=$_[0];
  155. if ($::aout)
  156. { $val=int(log($val)/log(2));
  157. $val.=",0x90";
  158. }
  159. push(@out,".align\t$val\n");
  160. }
  161. sub ::picmeup
  162. { my($dst,$sym,$base,$reflabel)=@_;
  163. if (($::pic && ($::elf || $::aout)) || $::macosx)
  164. { if (!defined($base))
  165. { &::call(&::label("PIC_me_up"));
  166. &::set_label("PIC_me_up");
  167. &::blindpop($dst);
  168. $base=$dst;
  169. $reflabel=&::label("PIC_me_up");
  170. }
  171. if ($::macosx)
  172. { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
  173. &::mov($dst,&::DWP("$indirect-$reflabel",$base));
  174. $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
  175. }
  176. elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
  177. { &::lea($dst,&::DWP("$sym-$reflabel",$base)); }
  178. else
  179. { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
  180. $base));
  181. &::mov($dst,&::DWP("$sym\@GOT",$dst));
  182. }
  183. }
  184. else
  185. { &::lea($dst,&::DWP($sym)); }
  186. }
  187. sub ::initseg
  188. { my $f=$nmdecor.shift;
  189. if ($::android)
  190. { $initseg.=<<___;
  191. .section .init_array
  192. .align 4
  193. .long $f
  194. ___
  195. }
  196. elsif ($::elf)
  197. { $initseg.=<<___;
  198. .section .init
  199. call $f
  200. ___
  201. }
  202. elsif ($::coff)
  203. { $initseg.=<<___; # applies to both Cygwin and Mingw
  204. .section .ctors
  205. .long $f
  206. ___
  207. }
  208. elsif ($::macosx)
  209. { $initseg.=<<___;
  210. .mod_init_func
  211. .align 2
  212. .long $f
  213. ___
  214. }
  215. elsif ($::aout)
  216. { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
  217. $initseg.=".text\n";
  218. $initseg.=".type $ctor,\@function\n" if ($::pic);
  219. $initseg.=<<___; # OpenBSD way...
  220. .globl $ctor
  221. .align 2
  222. $ctor:
  223. jmp $f
  224. ___
  225. }
  226. }
  227. sub ::dataseg
  228. { push(@out,".data\n"); }
  229. *::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
  230. 1;