x86gas.pl 6.5 KB

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