x86_64-xlate.pl 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2022 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. # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
  9. #
  10. # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
  11. # format is way easier to parse. Because it's simpler to "gear" from
  12. # Unix ABI to Windows one [see cross-reference "card" at the end of
  13. # file]. Because Linux targets were available first...
  14. #
  15. # In addition the script also "distills" code suitable for GNU
  16. # assembler, so that it can be compiled with more rigid assemblers,
  17. # such as Solaris /usr/ccs/bin/as.
  18. #
  19. # This translator is not designed to convert *arbitrary* assembler
  20. # code from AT&T format to MASM one. It's designed to convert just
  21. # enough to provide for dual-ABI OpenSSL modules development...
  22. # There *are* limitations and you might have to modify your assembler
  23. # code or this script to achieve the desired result...
  24. #
  25. # Currently recognized limitations:
  26. #
  27. # - can't use multiple ops per line;
  28. #
  29. # Dual-ABI styling rules.
  30. #
  31. # 1. Adhere to Unix register and stack layout [see cross-reference
  32. # ABI "card" at the end for explanation].
  33. # 2. Forget about "red zone," stick to more traditional blended
  34. # stack frame allocation. If volatile storage is actually required
  35. # that is. If not, just leave the stack as is.
  36. # 3. Functions tagged with ".type name,@function" get crafted with
  37. # unified Win64 prologue and epilogue automatically. If you want
  38. # to take care of ABI differences yourself, tag functions as
  39. # ".type name,@abi-omnipotent" instead.
  40. # 4. To optimize the Win64 prologue you can specify number of input
  41. # arguments as ".type name,@function,N." Keep in mind that if N is
  42. # larger than 6, then you *have to* write "abi-omnipotent" code,
  43. # because >6 cases can't be addressed with unified prologue.
  44. # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
  45. # (sorry about latter).
  46. # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
  47. # required to identify the spots, where to inject Win64 epilogue!
  48. # But on the pros, it's then prefixed with rep automatically:-)
  49. # 7. Stick to explicit ip-relative addressing. If you have to use
  50. # GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
  51. # Both are recognized and translated to proper Win64 addressing
  52. # modes.
  53. #
  54. # 8. In order to provide for structured exception handling unified
  55. # Win64 prologue copies %rsp value to %rax. For further details
  56. # see SEH paragraph at the end.
  57. # 9. .init segment is allowed to contain calls to functions only.
  58. # a. If function accepts more than 4 arguments *and* >4th argument
  59. # is declared as non 64-bit value, do clear its upper part.
  60. use strict;
  61. my $flavour = shift;
  62. my $output = shift;
  63. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  64. open STDOUT,">$output" || die "can't open $output: $!"
  65. if (defined($output));
  66. my $gas=1; $gas=0 if ($output =~ /\.asm$/);
  67. my $elf=1; $elf=0 if (!$gas);
  68. my $win64=0;
  69. my $prefix="";
  70. my $decor=".L";
  71. my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
  72. my $masm=0;
  73. my $PTR=" PTR";
  74. my $nasmref=2.03;
  75. my $nasm=0;
  76. # GNU as indicator, as opposed to $gas, which indicates acceptable
  77. # syntax
  78. my $gnuas=0;
  79. if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
  80. $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
  81. $prefix =~ s|\R$||; # Better chomp
  82. }
  83. elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
  84. elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
  85. elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
  86. elsif (!$gas)
  87. { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
  88. { $nasm = $1 + $2*0.01; $PTR=""; }
  89. elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
  90. { $masm = $1 + $2*2**-16 + $4*2**-32; }
  91. die "no assembler found on %PATH%" if (!($nasm || $masm));
  92. $win64=1;
  93. $elf=0;
  94. $decor="\$L\$";
  95. }
  96. # Find out if we're using GNU as
  97. elsif (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  98. =~ /GNU assembler version ([2-9]\.[0-9]+)/)
  99. {
  100. $gnuas=1;
  101. }
  102. elsif (`$ENV{CC} --version 2>/dev/null`
  103. =~ /(clang .*|Intel.*oneAPI .*)/)
  104. {
  105. $gnuas=1;
  106. }
  107. elsif (`$ENV{CC} -V 2>/dev/null`
  108. =~ /nvc .*/)
  109. {
  110. $gnuas=1;
  111. }
  112. my $cet_property;
  113. if ($flavour =~ /elf/) {
  114. # Always generate .note.gnu.property section for ELF outputs to
  115. # mark Intel CET support since all input files must be marked
  116. # with Intel CET support in order for linker to mark output with
  117. # Intel CET support.
  118. my $p2align=3; $p2align=2 if ($flavour eq "elf32");
  119. my $section='.note.gnu.property, #alloc';
  120. $section='".note.gnu.property", "a"' if $gnuas;
  121. $cet_property = <<_____;
  122. .section $section
  123. .p2align $p2align
  124. .long 1f - 0f
  125. .long 4f - 1f
  126. .long 5
  127. 0:
  128. # "GNU" encoded with .byte, since .asciz isn't supported
  129. # on Solaris.
  130. .byte 0x47
  131. .byte 0x4e
  132. .byte 0x55
  133. .byte 0
  134. 1:
  135. .p2align $p2align
  136. .long 0xc0000002
  137. .long 3f - 2f
  138. 2:
  139. .long 3
  140. 3:
  141. .p2align $p2align
  142. 4:
  143. _____
  144. }
  145. my $current_segment;
  146. my $current_function;
  147. my %globals;
  148. { package opcode; # pick up opcodes
  149. sub re {
  150. my ($class, $line) = @_;
  151. my $self = {};
  152. my $ret;
  153. if ($$line =~ /^([a-z][a-z0-9]*)/i) {
  154. bless $self,$class;
  155. $self->{op} = $1;
  156. $ret = $self;
  157. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  158. undef $self->{sz};
  159. if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
  160. $self->{op} = $1;
  161. $self->{sz} = $2;
  162. } elsif ($self->{op} =~ /call|jmp/) {
  163. $self->{sz} = "";
  164. } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
  165. $self->{sz} = "";
  166. } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
  167. $self->{sz} = "";
  168. } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
  169. $self->{sz} = "";
  170. } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
  171. $self->{op} = $1;
  172. $self->{sz} = $2;
  173. }
  174. }
  175. $ret;
  176. }
  177. sub size {
  178. my ($self, $sz) = @_;
  179. $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
  180. $self->{sz};
  181. }
  182. sub out {
  183. my $self = shift;
  184. if ($gas) {
  185. if ($self->{op} eq "movz") { # movz is pain...
  186. sprintf "%s%s%s",$self->{op},$self->{sz},shift;
  187. } elsif ($self->{op} =~ /^set/) {
  188. "$self->{op}";
  189. } elsif ($self->{op} eq "ret") {
  190. my $epilogue = "";
  191. if ($win64 && $current_function->{abi} eq "svr4") {
  192. $epilogue = "movq 8(%rsp),%rdi\n\t" .
  193. "movq 16(%rsp),%rsi\n\t";
  194. }
  195. $epilogue . ".byte 0xf3,0xc3";
  196. } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
  197. ".p2align\t3\n\t.quad";
  198. } else {
  199. "$self->{op}$self->{sz}";
  200. }
  201. } else {
  202. $self->{op} =~ s/^movz/movzx/;
  203. if ($self->{op} eq "ret") {
  204. $self->{op} = "";
  205. if ($win64 && $current_function->{abi} eq "svr4") {
  206. $self->{op} = "mov rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
  207. "mov rsi,QWORD$PTR\[16+rsp\]\n\t";
  208. }
  209. $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
  210. } elsif ($self->{op} =~ /^(pop|push)f/) {
  211. $self->{op} .= $self->{sz};
  212. } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
  213. $self->{op} = "\tDQ";
  214. }
  215. $self->{op};
  216. }
  217. }
  218. sub mnemonic {
  219. my ($self, $op) = @_;
  220. $self->{op}=$op if (defined($op));
  221. $self->{op};
  222. }
  223. }
  224. { package const; # pick up constants, which start with $
  225. sub re {
  226. my ($class, $line) = @_;
  227. my $self = {};
  228. my $ret;
  229. if ($$line =~ /^\$([^,]+)/) {
  230. bless $self, $class;
  231. $self->{value} = $1;
  232. $ret = $self;
  233. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  234. }
  235. $ret;
  236. }
  237. sub out {
  238. my $self = shift;
  239. $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
  240. if ($gas) {
  241. # Solaris /usr/ccs/bin/as can't handle multiplications
  242. # in $self->{value}
  243. my $value = $self->{value};
  244. no warnings; # oct might complain about overflow, ignore here...
  245. $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
  246. if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
  247. $self->{value} = $value;
  248. }
  249. sprintf "\$%s",$self->{value};
  250. } else {
  251. my $value = $self->{value};
  252. $value =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
  253. sprintf "%s",$value;
  254. }
  255. }
  256. }
  257. { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
  258. my %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR",
  259. l=>"DWORD$PTR", d=>"DWORD$PTR",
  260. q=>"QWORD$PTR", o=>"OWORD$PTR",
  261. x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
  262. z=>"ZMMWORD$PTR" ) if (!$gas);
  263. sub re {
  264. my ($class, $line, $opcode) = @_;
  265. my $self = {};
  266. my $ret;
  267. # optional * ----vvv--- appears in indirect jmp/call
  268. if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
  269. bless $self, $class;
  270. $self->{asterisk} = $1;
  271. $self->{label} = $2;
  272. ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
  273. $self->{scale} = 1 if (!defined($self->{scale}));
  274. $self->{opmask} = $4;
  275. $ret = $self;
  276. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  277. if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
  278. die if ($opcode->mnemonic() ne "mov");
  279. $opcode->mnemonic("lea");
  280. }
  281. $self->{base} =~ s/^%//;
  282. $self->{index} =~ s/^%// if (defined($self->{index}));
  283. $self->{opcode} = $opcode;
  284. }
  285. $ret;
  286. }
  287. sub size {}
  288. sub out {
  289. my ($self, $sz) = @_;
  290. $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  291. $self->{label} =~ s/\.L/$decor/g;
  292. # Silently convert all EAs to 64-bit. This is required for
  293. # elder GNU assembler and results in more compact code,
  294. # *but* most importantly AES module depends on this feature!
  295. $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  296. $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  297. # Solaris /usr/ccs/bin/as can't handle multiplications
  298. # in $self->{label}...
  299. use integer;
  300. $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
  301. $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
  302. # Some assemblers insist on signed presentation of 32-bit
  303. # offsets, but sign extension is a tricky business in perl...
  304. if ((1<<31)<<1) {
  305. $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
  306. } else {
  307. $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
  308. }
  309. # if base register is %rbp or %r13, see if it's possible to
  310. # flip base and index registers [for better performance]
  311. if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
  312. $self->{base} =~ /(rbp|r13)/) {
  313. $self->{base} = $self->{index}; $self->{index} = $1;
  314. }
  315. if ($gas) {
  316. $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
  317. if (defined($self->{index})) {
  318. sprintf "%s%s(%s,%%%s,%d)%s",
  319. $self->{asterisk},$self->{label},
  320. $self->{base}?"%$self->{base}":"",
  321. $self->{index},$self->{scale},
  322. $self->{opmask};
  323. } else {
  324. sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
  325. $self->{base},$self->{opmask};
  326. }
  327. } else {
  328. $self->{label} =~ s/\./\$/g;
  329. $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
  330. $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
  331. my $mnemonic = $self->{opcode}->mnemonic();
  332. ($self->{asterisk}) && ($sz="q") ||
  333. ($mnemonic =~ /^v?mov([qd])$/) && ($sz=$1) ||
  334. ($mnemonic =~ /^v?pinsr([qdwb])$/) && ($sz=$1) ||
  335. ($mnemonic =~ /^vpbroadcast([qdwb])$/) && ($sz=$1) ||
  336. ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/) && ($sz="x");
  337. $self->{opmask} =~ s/%(k[0-7])/$1/;
  338. if (defined($self->{index})) {
  339. sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
  340. $self->{label}?"$self->{label}+":"",
  341. $self->{index},$self->{scale},
  342. $self->{base}?"+$self->{base}":"",
  343. $self->{opmask};
  344. } elsif ($self->{base} eq "rip") {
  345. sprintf "%s[%s]",$szmap{$sz},$self->{label};
  346. } else {
  347. sprintf "%s[%s%s]%s", $szmap{$sz},
  348. $self->{label}?"$self->{label}+":"",
  349. $self->{base},$self->{opmask};
  350. }
  351. }
  352. }
  353. }
  354. { package register; # pick up registers, which start with %.
  355. sub re {
  356. my ($class, $line, $opcode) = @_;
  357. my $self = {};
  358. my $ret;
  359. # optional * ----vvv--- appears in indirect jmp/call
  360. if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
  361. bless $self,$class;
  362. $self->{asterisk} = $1;
  363. $self->{value} = $2;
  364. $self->{opmask} = $3;
  365. $opcode->size($self->size());
  366. $ret = $self;
  367. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  368. }
  369. $ret;
  370. }
  371. sub size {
  372. my $self = shift;
  373. my $ret;
  374. if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
  375. elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
  376. elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
  377. elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
  378. elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
  379. elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
  380. elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
  381. elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
  382. $ret;
  383. }
  384. sub out {
  385. my $self = shift;
  386. if ($gas) { sprintf "%s%%%s%s", $self->{asterisk},
  387. $self->{value},
  388. $self->{opmask}; }
  389. else { $self->{opmask} =~ s/%(k[0-7])/$1/;
  390. $self->{value}.$self->{opmask}; }
  391. }
  392. }
  393. { package label; # pick up labels, which end with :
  394. sub re {
  395. my ($class, $line) = @_;
  396. my $self = {};
  397. my $ret;
  398. if ($$line =~ /(^[\.\w]+)\:/) {
  399. bless $self,$class;
  400. $self->{value} = $1;
  401. $ret = $self;
  402. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  403. $self->{value} =~ s/^\.L/$decor/;
  404. }
  405. $ret;
  406. }
  407. sub out {
  408. my $self = shift;
  409. if ($gas) {
  410. my $func = ($globals{$self->{value}} or $self->{value}) . ":";
  411. if ($win64 && $current_function->{name} eq $self->{value}
  412. && $current_function->{abi} eq "svr4") {
  413. $func .= "\n";
  414. $func .= " movq %rdi,8(%rsp)\n";
  415. $func .= " movq %rsi,16(%rsp)\n";
  416. $func .= " movq %rsp,%rax\n";
  417. $func .= "${decor}SEH_begin_$current_function->{name}:\n";
  418. my $narg = $current_function->{narg};
  419. $narg=6 if (!defined($narg));
  420. $func .= " movq %rcx,%rdi\n" if ($narg>0);
  421. $func .= " movq %rdx,%rsi\n" if ($narg>1);
  422. $func .= " movq %r8,%rdx\n" if ($narg>2);
  423. $func .= " movq %r9,%rcx\n" if ($narg>3);
  424. $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
  425. $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
  426. }
  427. $func;
  428. } elsif ($self->{value} ne "$current_function->{name}") {
  429. # Make all labels in masm global.
  430. $self->{value} .= ":" if ($masm);
  431. $self->{value} . ":";
  432. } elsif ($win64 && $current_function->{abi} eq "svr4") {
  433. my $func = "$current_function->{name}" .
  434. ($nasm ? ":" : "\tPROC $current_function->{scope}") .
  435. "\n";
  436. $func .= " mov QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
  437. $func .= " mov QWORD$PTR\[16+rsp\],rsi\n";
  438. $func .= " mov rax,rsp\n";
  439. $func .= "${decor}SEH_begin_$current_function->{name}:";
  440. $func .= ":" if ($masm);
  441. $func .= "\n";
  442. my $narg = $current_function->{narg};
  443. $narg=6 if (!defined($narg));
  444. $func .= " mov rdi,rcx\n" if ($narg>0);
  445. $func .= " mov rsi,rdx\n" if ($narg>1);
  446. $func .= " mov rdx,r8\n" if ($narg>2);
  447. $func .= " mov rcx,r9\n" if ($narg>3);
  448. $func .= " mov r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
  449. $func .= " mov r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
  450. $func .= "\n";
  451. } else {
  452. "$current_function->{name}".
  453. ($nasm ? ":" : "\tPROC $current_function->{scope}");
  454. }
  455. }
  456. }
  457. { package expr; # pick up expressions
  458. sub re {
  459. my ($class, $line, $opcode) = @_;
  460. my $self = {};
  461. my $ret;
  462. if ($$line =~ /(^[^,]+)/) {
  463. bless $self,$class;
  464. $self->{value} = $1;
  465. $ret = $self;
  466. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  467. $self->{value} =~ s/\@PLT// if (!$elf);
  468. $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  469. $self->{value} =~ s/\.L/$decor/g;
  470. $self->{opcode} = $opcode;
  471. }
  472. $ret;
  473. }
  474. sub out {
  475. my $self = shift;
  476. if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
  477. "NEAR ".$self->{value};
  478. } else {
  479. $self->{value};
  480. }
  481. }
  482. }
  483. { package cfi_directive;
  484. # CFI directives annotate instructions that are significant for
  485. # stack unwinding procedure compliant with DWARF specification,
  486. # see http://dwarfstd.org/. Besides naturally expected for this
  487. # script platform-specific filtering function, this module adds
  488. # three auxiliary synthetic directives not recognized by [GNU]
  489. # assembler:
  490. #
  491. # - .cfi_push to annotate push instructions in prologue, which
  492. # translates to .cfi_adjust_cfa_offset (if needed) and
  493. # .cfi_offset;
  494. # - .cfi_pop to annotate pop instructions in epilogue, which
  495. # translates to .cfi_adjust_cfa_offset (if needed) and
  496. # .cfi_restore;
  497. # - [and most notably] .cfi_cfa_expression which encodes
  498. # DW_CFA_def_cfa_expression and passes it to .cfi_escape as
  499. # byte vector;
  500. #
  501. # CFA expressions were introduced in DWARF specification version
  502. # 3 and describe how to deduce CFA, Canonical Frame Address. This
  503. # becomes handy if your stack frame is variable and you can't
  504. # spare register for [previous] frame pointer. Suggested directive
  505. # syntax is made-up mix of DWARF operator suffixes [subset of]
  506. # and references to registers with optional bias. Following example
  507. # describes offloaded *original* stack pointer at specific offset
  508. # from *current* stack pointer:
  509. #
  510. # .cfi_cfa_expression %rsp+40,deref,+8
  511. #
  512. # Final +8 has everything to do with the fact that CFA is defined
  513. # as reference to top of caller's stack, and on x86_64 call to
  514. # subroutine pushes 8-byte return address. In other words original
  515. # stack pointer upon entry to a subroutine is 8 bytes off from CFA.
  516. # Below constants are taken from "DWARF Expressions" section of the
  517. # DWARF specification, section is numbered 7.7 in versions 3 and 4.
  518. my %DW_OP_simple = ( # no-arg operators, mapped directly
  519. deref => 0x06, dup => 0x12,
  520. drop => 0x13, over => 0x14,
  521. pick => 0x15, swap => 0x16,
  522. rot => 0x17, xderef => 0x18,
  523. abs => 0x19, and => 0x1a,
  524. div => 0x1b, minus => 0x1c,
  525. mod => 0x1d, mul => 0x1e,
  526. neg => 0x1f, not => 0x20,
  527. or => 0x21, plus => 0x22,
  528. shl => 0x24, shr => 0x25,
  529. shra => 0x26, xor => 0x27,
  530. );
  531. my %DW_OP_complex = ( # used in specific subroutines
  532. constu => 0x10, # uleb128
  533. consts => 0x11, # sleb128
  534. plus_uconst => 0x23, # uleb128
  535. lit0 => 0x30, # add 0-31 to opcode
  536. reg0 => 0x50, # add 0-31 to opcode
  537. breg0 => 0x70, # add 0-31 to opcole, sleb128
  538. regx => 0x90, # uleb28
  539. fbreg => 0x91, # sleb128
  540. bregx => 0x92, # uleb128, sleb128
  541. piece => 0x93, # uleb128
  542. );
  543. # Following constants are defined in x86_64 ABI supplement, for
  544. # example available at https://www.uclibc.org/docs/psABI-x86_64.pdf,
  545. # see section 3.7 "Stack Unwind Algorithm".
  546. my %DW_reg_idx = (
  547. "%rax"=>0, "%rdx"=>1, "%rcx"=>2, "%rbx"=>3,
  548. "%rsi"=>4, "%rdi"=>5, "%rbp"=>6, "%rsp"=>7,
  549. "%r8" =>8, "%r9" =>9, "%r10"=>10, "%r11"=>11,
  550. "%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
  551. );
  552. my ($cfa_reg, $cfa_rsp);
  553. my @cfa_stack;
  554. # [us]leb128 format is variable-length integer representation base
  555. # 2^128, with most significant bit of each byte being 0 denoting
  556. # *last* most significant digit. See "Variable Length Data" in the
  557. # DWARF specification, numbered 7.6 at least in versions 3 and 4.
  558. sub sleb128 {
  559. use integer; # get right shift extend sign
  560. my $val = shift;
  561. my $sign = ($val < 0) ? -1 : 0;
  562. my @ret = ();
  563. while(1) {
  564. push @ret, $val&0x7f;
  565. # see if remaining bits are same and equal to most
  566. # significant bit of the current digit, if so, it's
  567. # last digit...
  568. last if (($val>>6) == $sign);
  569. @ret[-1] |= 0x80;
  570. $val >>= 7;
  571. }
  572. return @ret;
  573. }
  574. sub uleb128 {
  575. my $val = shift;
  576. my @ret = ();
  577. while(1) {
  578. push @ret, $val&0x7f;
  579. # see if it's last significant digit...
  580. last if (($val >>= 7) == 0);
  581. @ret[-1] |= 0x80;
  582. }
  583. return @ret;
  584. }
  585. sub const {
  586. my $val = shift;
  587. if ($val >= 0 && $val < 32) {
  588. return ($DW_OP_complex{lit0}+$val);
  589. }
  590. return ($DW_OP_complex{consts}, sleb128($val));
  591. }
  592. sub reg {
  593. my $val = shift;
  594. return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
  595. my $reg = $DW_reg_idx{$1};
  596. my $off = eval ("0 $2 $3");
  597. return (($DW_OP_complex{breg0} + $reg), sleb128($off));
  598. # Yes, we use DW_OP_bregX+0 to push register value and not
  599. # DW_OP_regX, because latter would require even DW_OP_piece,
  600. # which would be a waste under the circumstances. If you have
  601. # to use DWP_OP_reg, use "regx:N"...
  602. }
  603. sub cfa_expression {
  604. my $line = shift;
  605. my @ret;
  606. foreach my $token (split(/,\s*/,$line)) {
  607. if ($token =~ /^%r/) {
  608. push @ret,reg($token);
  609. } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
  610. push @ret,reg("$2+$1");
  611. } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
  612. my $i = 1*eval($2);
  613. push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
  614. } elsif (my $i = 1*eval($token) or $token eq "0") {
  615. if ($token =~ /^\+/) {
  616. push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
  617. } else {
  618. push @ret,const($i);
  619. }
  620. } else {
  621. push @ret,$DW_OP_simple{$token};
  622. }
  623. }
  624. # Finally we return DW_CFA_def_cfa_expression, 15, followed by
  625. # length of the expression and of course the expression itself.
  626. return (15,scalar(@ret),@ret);
  627. }
  628. sub re {
  629. my ($class, $line) = @_;
  630. my $self = {};
  631. my $ret;
  632. if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
  633. bless $self,$class;
  634. $ret = $self;
  635. undef $self->{value};
  636. my $dir = $1;
  637. SWITCH: for ($dir) {
  638. # What is $cfa_rsp? Effectively it's difference between %rsp
  639. # value and current CFA, Canonical Frame Address, which is
  640. # why it starts with -8. Recall that CFA is top of caller's
  641. # stack...
  642. /startproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
  643. /endproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", 0);
  644. # .cfi_remember_state directives that are not
  645. # matched with .cfi_restore_state are
  646. # unnecessary.
  647. die "unpaired .cfi_remember_state" if (@cfa_stack);
  648. last;
  649. };
  650. /def_cfa_register/
  651. && do { $cfa_reg = $$line; last; };
  652. /def_cfa_offset/
  653. && do { $cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
  654. last;
  655. };
  656. /adjust_cfa_offset/
  657. && do { $cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
  658. last;
  659. };
  660. /def_cfa/ && do { if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
  661. $cfa_reg = $1;
  662. $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
  663. }
  664. last;
  665. };
  666. /push/ && do { $dir = undef;
  667. $cfa_rsp -= 8;
  668. if ($cfa_reg eq "%rsp") {
  669. $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
  670. }
  671. $self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
  672. last;
  673. };
  674. /pop/ && do { $dir = undef;
  675. $cfa_rsp += 8;
  676. if ($cfa_reg eq "%rsp") {
  677. $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
  678. }
  679. $self->{value} .= ".cfi_restore\t$$line";
  680. last;
  681. };
  682. /cfa_expression/
  683. && do { $dir = undef;
  684. $self->{value} = ".cfi_escape\t" .
  685. join(",", map(sprintf("0x%02x", $_),
  686. cfa_expression($$line)));
  687. last;
  688. };
  689. /remember_state/
  690. && do { push @cfa_stack, [$cfa_reg, $cfa_rsp];
  691. last;
  692. };
  693. /restore_state/
  694. && do { ($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
  695. last;
  696. };
  697. }
  698. $self->{value} = ".cfi_$dir\t$$line" if ($dir);
  699. $$line = "";
  700. }
  701. return $ret;
  702. }
  703. sub out {
  704. my $self = shift;
  705. return ($elf ? $self->{value} : undef);
  706. }
  707. }
  708. { package directive; # pick up directives, which start with .
  709. sub re {
  710. my ($class, $line) = @_;
  711. my $self = {};
  712. my $ret;
  713. my $dir;
  714. # chain-call to cfi_directive
  715. $ret = cfi_directive->re($line) and return $ret;
  716. if ($$line =~ /^\s*(\.\w+)/) {
  717. bless $self,$class;
  718. $dir = $1;
  719. $ret = $self;
  720. undef $self->{value};
  721. $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
  722. SWITCH: for ($dir) {
  723. /\.global|\.globl|\.extern/
  724. && do { $globals{$$line} = $prefix . $$line;
  725. $$line = $globals{$$line} if ($prefix);
  726. last;
  727. };
  728. /\.type/ && do { my ($sym,$type,$narg) = split(',',$$line);
  729. if ($type eq "\@function") {
  730. undef $current_function;
  731. $current_function->{name} = $sym;
  732. $current_function->{abi} = "svr4";
  733. $current_function->{narg} = $narg;
  734. $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
  735. } elsif ($type eq "\@abi-omnipotent") {
  736. undef $current_function;
  737. $current_function->{name} = $sym;
  738. $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
  739. }
  740. $$line =~ s/\@abi\-omnipotent/\@function/;
  741. $$line =~ s/\@function.*/\@function/;
  742. last;
  743. };
  744. /\.asciz/ && do { if ($$line =~ /^"(.*)"$/) {
  745. $dir = ".byte";
  746. $$line = join(",",unpack("C*",$1),0);
  747. }
  748. last;
  749. };
  750. /\.rva|\.long|\.quad|\.byte/
  751. && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  752. $$line =~ s/\.L/$decor/g;
  753. last;
  754. };
  755. }
  756. if ($gas) {
  757. $self->{value} = $dir . "\t" . $$line;
  758. if ($dir =~ /\.extern/) {
  759. $self->{value} = ""; # swallow extern
  760. } elsif (!$elf && $dir =~ /\.type/) {
  761. $self->{value} = "";
  762. $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
  763. (defined($globals{$1})?".scl 2;":".scl 3;") .
  764. "\t.type 32;\t.endef"
  765. if ($win64 && $$line =~ /([^,]+),\@function/);
  766. } elsif (!$elf && $dir =~ /\.size/) {
  767. $self->{value} = "";
  768. if (defined($current_function)) {
  769. $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
  770. if ($win64 && $current_function->{abi} eq "svr4");
  771. undef $current_function;
  772. }
  773. } elsif (!$elf && $dir =~ /\.align/) {
  774. $self->{value} = ".p2align\t" . (log($$line)/log(2));
  775. } elsif ($dir eq ".section") {
  776. $current_segment=$$line;
  777. if (!$elf && $current_segment eq ".init") {
  778. if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
  779. elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
  780. }
  781. } elsif ($dir =~ /\.(text|data)/) {
  782. $current_segment=".$1";
  783. } elsif ($dir =~ /\.hidden/) {
  784. if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$$line"; }
  785. elsif ($flavour eq "mingw64") { $self->{value} = ""; }
  786. } elsif ($dir =~ /\.comm/) {
  787. $self->{value} = "$dir\t$prefix$$line";
  788. $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
  789. }
  790. $$line = "";
  791. return $self;
  792. }
  793. # non-gas case or nasm/masm
  794. SWITCH: for ($dir) {
  795. /\.text/ && do { my $v=undef;
  796. if ($nasm) {
  797. $v="section .text code align=64\n";
  798. } else {
  799. $v="$current_segment\tENDS\n" if ($current_segment);
  800. $current_segment = ".text\$";
  801. $v.="$current_segment\tSEGMENT ";
  802. $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
  803. $v.=" 'CODE'";
  804. }
  805. $self->{value} = $v;
  806. last;
  807. };
  808. /\.data/ && do { my $v=undef;
  809. if ($nasm) {
  810. $v="section .data data align=8\n";
  811. } else {
  812. $v="$current_segment\tENDS\n" if ($current_segment);
  813. $current_segment = "_DATA";
  814. $v.="$current_segment\tSEGMENT";
  815. }
  816. $self->{value} = $v;
  817. last;
  818. };
  819. /\.section/ && do { my $v=undef;
  820. $$line =~ s/([^,]*).*/$1/;
  821. $$line = ".CRT\$XCU" if ($$line eq ".init");
  822. if ($nasm) {
  823. $v="section $$line";
  824. if ($$line=~/\.([px])data/) {
  825. $v.=" rdata align=";
  826. $v.=$1 eq "p"? 4 : 8;
  827. } elsif ($$line=~/\.CRT\$/i) {
  828. $v.=" rdata align=8";
  829. }
  830. } else {
  831. $v="$current_segment\tENDS\n" if ($current_segment);
  832. $v.="$$line\tSEGMENT";
  833. if ($$line=~/\.([px])data/) {
  834. $v.=" READONLY";
  835. $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
  836. } elsif ($$line=~/\.CRT\$/i) {
  837. $v.=" READONLY ";
  838. $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
  839. }
  840. }
  841. $current_segment = $$line;
  842. $self->{value} = $v;
  843. last;
  844. };
  845. /\.extern/ && do { $self->{value} = "EXTERN\t".$$line;
  846. $self->{value} .= ":NEAR" if ($masm);
  847. last;
  848. };
  849. /\.globl|.global/
  850. && do { $self->{value} = $masm?"PUBLIC":"global";
  851. $self->{value} .= "\t".$$line;
  852. last;
  853. };
  854. /\.size/ && do { if (defined($current_function)) {
  855. undef $self->{value};
  856. if ($current_function->{abi} eq "svr4") {
  857. $self->{value}="${decor}SEH_end_$current_function->{name}:";
  858. $self->{value}.=":\n" if($masm);
  859. }
  860. $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
  861. undef $current_function;
  862. }
  863. last;
  864. };
  865. /\.align/ && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
  866. $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
  867. last;
  868. };
  869. /\.(value|long|rva|quad)/
  870. && do { my $sz = substr($1,0,1);
  871. my @arr = split(/,\s*/,$$line);
  872. my $last = pop(@arr);
  873. my $conv = sub { my $var=shift;
  874. $var=~s/^(0b[0-1]+)/oct($1)/eig;
  875. $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
  876. if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
  877. { $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
  878. $var;
  879. };
  880. $sz =~ tr/bvlrq/BWDDQ/;
  881. $self->{value} = "\tD$sz\t";
  882. for (@arr) { $self->{value} .= &$conv($_).","; }
  883. $self->{value} .= &$conv($last);
  884. last;
  885. };
  886. /\.byte/ && do { my @str=split(/,\s*/,$$line);
  887. map(s/(0b[0-1]+)/oct($1)/eig,@str);
  888. map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
  889. while ($#str>15) {
  890. $self->{value}.="DB\t"
  891. .join(",",@str[0..15])."\n";
  892. foreach (0..15) { shift @str; }
  893. }
  894. $self->{value}.="DB\t"
  895. .join(",",@str) if (@str);
  896. last;
  897. };
  898. /\.comm/ && do { my @str=split(/,\s*/,$$line);
  899. my $v=undef;
  900. if ($nasm) {
  901. $v.="common $prefix@str[0] @str[1]";
  902. } else {
  903. $v="$current_segment\tENDS\n" if ($current_segment);
  904. $current_segment = "_DATA";
  905. $v.="$current_segment\tSEGMENT\n";
  906. $v.="COMM @str[0]:DWORD:".@str[1]/4;
  907. }
  908. $self->{value} = $v;
  909. last;
  910. };
  911. }
  912. $$line = "";
  913. }
  914. $ret;
  915. }
  916. sub out {
  917. my $self = shift;
  918. $self->{value};
  919. }
  920. }
  921. # Upon initial x86_64 introduction SSE>2 extensions were not introduced
  922. # yet. In order not to be bothered by tracing exact assembler versions,
  923. # but at the same time to provide a bare security minimum of AES-NI, we
  924. # hard-code some instructions. Extensions past AES-NI on the other hand
  925. # are traced by examining assembler version in individual perlasm
  926. # modules...
  927. my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
  928. "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
  929. sub rex {
  930. my $opcode=shift;
  931. my ($dst,$src,$rex)=@_;
  932. $rex|=0x04 if($dst>=8);
  933. $rex|=0x01 if($src>=8);
  934. push @$opcode,($rex|0x40) if ($rex);
  935. }
  936. my $movq = sub { # elderly gas can't handle inter-register movq
  937. my $arg = shift;
  938. my @opcode=(0x66);
  939. if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
  940. my ($src,$dst)=($1,$2);
  941. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  942. rex(\@opcode,$src,$dst,0x8);
  943. push @opcode,0x0f,0x7e;
  944. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  945. @opcode;
  946. } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
  947. my ($src,$dst)=($2,$1);
  948. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  949. rex(\@opcode,$src,$dst,0x8);
  950. push @opcode,0x0f,0x6e;
  951. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  952. @opcode;
  953. } else {
  954. ();
  955. }
  956. };
  957. my $pextrd = sub {
  958. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
  959. my @opcode=(0x66);
  960. my $imm=$1;
  961. my $src=$2;
  962. my $dst=$3;
  963. if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
  964. elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
  965. rex(\@opcode,$src,$dst);
  966. push @opcode,0x0f,0x3a,0x16;
  967. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  968. push @opcode,$imm;
  969. @opcode;
  970. } else {
  971. ();
  972. }
  973. };
  974. my $pinsrd = sub {
  975. if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
  976. my @opcode=(0x66);
  977. my $imm=$1;
  978. my $src=$2;
  979. my $dst=$3;
  980. if ($src =~ /%r([0-9]+)/) { $src = $1; }
  981. elsif ($src =~ /%e/) { $src = $regrm{$src}; }
  982. rex(\@opcode,$dst,$src);
  983. push @opcode,0x0f,0x3a,0x22;
  984. push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
  985. push @opcode,$imm;
  986. @opcode;
  987. } else {
  988. ();
  989. }
  990. };
  991. my $pshufb = sub {
  992. if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  993. my @opcode=(0x66);
  994. rex(\@opcode,$2,$1);
  995. push @opcode,0x0f,0x38,0x00;
  996. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  997. @opcode;
  998. } else {
  999. ();
  1000. }
  1001. };
  1002. my $palignr = sub {
  1003. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1004. my @opcode=(0x66);
  1005. rex(\@opcode,$3,$2);
  1006. push @opcode,0x0f,0x3a,0x0f;
  1007. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1008. push @opcode,$1;
  1009. @opcode;
  1010. } else {
  1011. ();
  1012. }
  1013. };
  1014. my $pclmulqdq = sub {
  1015. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1016. my @opcode=(0x66);
  1017. rex(\@opcode,$3,$2);
  1018. push @opcode,0x0f,0x3a,0x44;
  1019. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1020. my $c=$1;
  1021. push @opcode,$c=~/^0/?oct($c):$c;
  1022. @opcode;
  1023. } else {
  1024. ();
  1025. }
  1026. };
  1027. my $rdrand = sub {
  1028. if (shift =~ /%[er](\w+)/) {
  1029. my @opcode=();
  1030. my $dst=$1;
  1031. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  1032. rex(\@opcode,0,$dst,8);
  1033. push @opcode,0x0f,0xc7,0xf0|($dst&7);
  1034. @opcode;
  1035. } else {
  1036. ();
  1037. }
  1038. };
  1039. my $rdseed = sub {
  1040. if (shift =~ /%[er](\w+)/) {
  1041. my @opcode=();
  1042. my $dst=$1;
  1043. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  1044. rex(\@opcode,0,$dst,8);
  1045. push @opcode,0x0f,0xc7,0xf8|($dst&7);
  1046. @opcode;
  1047. } else {
  1048. ();
  1049. }
  1050. };
  1051. # Not all AVX-capable assemblers recognize AMD XOP extension. Since we
  1052. # are using only two instructions hand-code them in order to be excused
  1053. # from chasing assembler versions...
  1054. sub rxb {
  1055. my $opcode=shift;
  1056. my ($dst,$src1,$src2,$rxb)=@_;
  1057. $rxb|=0x7<<5;
  1058. $rxb&=~(0x04<<5) if($dst>=8);
  1059. $rxb&=~(0x01<<5) if($src1>=8);
  1060. $rxb&=~(0x02<<5) if($src2>=8);
  1061. push @$opcode,$rxb;
  1062. }
  1063. my $vprotd = sub {
  1064. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1065. my @opcode=(0x8f);
  1066. rxb(\@opcode,$3,$2,-1,0x08);
  1067. push @opcode,0x78,0xc2;
  1068. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1069. my $c=$1;
  1070. push @opcode,$c=~/^0/?oct($c):$c;
  1071. @opcode;
  1072. } else {
  1073. ();
  1074. }
  1075. };
  1076. my $vprotq = sub {
  1077. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  1078. my @opcode=(0x8f);
  1079. rxb(\@opcode,$3,$2,-1,0x08);
  1080. push @opcode,0x78,0xc3;
  1081. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  1082. my $c=$1;
  1083. push @opcode,$c=~/^0/?oct($c):$c;
  1084. @opcode;
  1085. } else {
  1086. ();
  1087. }
  1088. };
  1089. # Intel Control-flow Enforcement Technology extension. All functions and
  1090. # indirect branch targets will have to start with this instruction...
  1091. my $endbranch = sub {
  1092. (0xf3,0x0f,0x1e,0xfa);
  1093. };
  1094. ########################################################################
  1095. if ($nasm) {
  1096. print <<___;
  1097. default rel
  1098. %define XMMWORD
  1099. %define YMMWORD
  1100. %define ZMMWORD
  1101. ___
  1102. } elsif ($masm) {
  1103. print <<___;
  1104. OPTION DOTNAME
  1105. ___
  1106. }
  1107. while(defined(my $line=<>)) {
  1108. $line =~ s|\R$||; # Better chomp
  1109. $line =~ s|[#!].*$||; # get rid of asm-style comments...
  1110. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  1111. $line =~ s|^\s+||; # ... and skip whitespaces in beginning
  1112. $line =~ s|\s+$||; # ... and at the end
  1113. if (my $label=label->re(\$line)) { print $label->out(); }
  1114. if (my $directive=directive->re(\$line)) {
  1115. printf "%s",$directive->out();
  1116. } elsif (my $opcode=opcode->re(\$line)) {
  1117. my $asm = eval("\$".$opcode->mnemonic());
  1118. if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
  1119. print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
  1120. next;
  1121. }
  1122. my @args;
  1123. ARGUMENT: while (1) {
  1124. my $arg;
  1125. ($arg=register->re(\$line, $opcode))||
  1126. ($arg=const->re(\$line)) ||
  1127. ($arg=ea->re(\$line, $opcode)) ||
  1128. ($arg=expr->re(\$line, $opcode)) ||
  1129. last ARGUMENT;
  1130. push @args,$arg;
  1131. last ARGUMENT if ($line !~ /^,/);
  1132. $line =~ s/^,\s*//;
  1133. } # ARGUMENT:
  1134. if ($#args>=0) {
  1135. my $insn;
  1136. my $sz=$opcode->size();
  1137. if ($gas) {
  1138. $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
  1139. @args = map($_->out($sz),@args);
  1140. printf "\t%s\t%s",$insn,join(",",@args);
  1141. } else {
  1142. $insn = $opcode->out();
  1143. foreach (@args) {
  1144. my $arg = $_->out();
  1145. # $insn.=$sz compensates for movq, pinsrw, ...
  1146. if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
  1147. if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
  1148. if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
  1149. if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
  1150. }
  1151. @args = reverse(@args);
  1152. undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
  1153. printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
  1154. }
  1155. } else {
  1156. printf "\t%s",$opcode->out();
  1157. }
  1158. }
  1159. print $line,"\n";
  1160. }
  1161. print "$cet_property" if ($cet_property);
  1162. print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
  1163. print "END\n" if ($masm);
  1164. close STDOUT or die "error closing STDOUT: $!;"
  1165. #################################################
  1166. # Cross-reference x86_64 ABI "card"
  1167. #
  1168. # Unix Win64
  1169. # %rax * *
  1170. # %rbx - -
  1171. # %rcx #4 #1
  1172. # %rdx #3 #2
  1173. # %rsi #2 -
  1174. # %rdi #1 -
  1175. # %rbp - -
  1176. # %rsp - -
  1177. # %r8 #5 #3
  1178. # %r9 #6 #4
  1179. # %r10 * *
  1180. # %r11 * *
  1181. # %r12 - -
  1182. # %r13 - -
  1183. # %r14 - -
  1184. # %r15 - -
  1185. #
  1186. # (*) volatile register
  1187. # (-) preserved by callee
  1188. # (#) Nth argument, volatile
  1189. #
  1190. # In Unix terms top of stack is argument transfer area for arguments
  1191. # which could not be accommodated in registers. Or in other words 7th
  1192. # [integer] argument resides at 8(%rsp) upon function entry point.
  1193. # 128 bytes above %rsp constitute a "red zone" which is not touched
  1194. # by signal handlers and can be used as temporal storage without
  1195. # allocating a frame.
  1196. #
  1197. # In Win64 terms N*8 bytes on top of stack is argument transfer area,
  1198. # which belongs to/can be overwritten by callee. N is the number of
  1199. # arguments passed to callee, *but* not less than 4! This means that
  1200. # upon function entry point 5th argument resides at 40(%rsp), as well
  1201. # as that 32 bytes from 8(%rsp) can always be used as temporal
  1202. # storage [without allocating a frame]. One can actually argue that
  1203. # one can assume a "red zone" above stack pointer under Win64 as well.
  1204. # Point is that at apparently no occasion Windows kernel would alter
  1205. # the area above user stack pointer in true asynchronous manner...
  1206. #
  1207. # All the above means that if assembler programmer adheres to Unix
  1208. # register and stack layout, but disregards the "red zone" existence,
  1209. # it's possible to use following prologue and epilogue to "gear" from
  1210. # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
  1211. #
  1212. # omnipotent_function:
  1213. # ifdef WIN64
  1214. # movq %rdi,8(%rsp)
  1215. # movq %rsi,16(%rsp)
  1216. # movq %rcx,%rdi ; if 1st argument is actually present
  1217. # movq %rdx,%rsi ; if 2nd argument is actually ...
  1218. # movq %r8,%rdx ; if 3rd argument is ...
  1219. # movq %r9,%rcx ; if 4th argument ...
  1220. # movq 40(%rsp),%r8 ; if 5th ...
  1221. # movq 48(%rsp),%r9 ; if 6th ...
  1222. # endif
  1223. # ...
  1224. # ifdef WIN64
  1225. # movq 8(%rsp),%rdi
  1226. # movq 16(%rsp),%rsi
  1227. # endif
  1228. # ret
  1229. #
  1230. #################################################
  1231. # Win64 SEH, Structured Exception Handling.
  1232. #
  1233. # Unlike on Unix systems(*) lack of Win64 stack unwinding information
  1234. # has undesired side-effect at run-time: if an exception is raised in
  1235. # assembler subroutine such as those in question (basically we're
  1236. # referring to segmentation violations caused by malformed input
  1237. # parameters), the application is briskly terminated without invoking
  1238. # any exception handlers, most notably without generating memory dump
  1239. # or any user notification whatsoever. This poses a problem. It's
  1240. # possible to address it by registering custom language-specific
  1241. # handler that would restore processor context to the state at
  1242. # subroutine entry point and return "exception is not handled, keep
  1243. # unwinding" code. Writing such handler can be a challenge... But it's
  1244. # doable, though requires certain coding convention. Consider following
  1245. # snippet:
  1246. #
  1247. # .type function,@function
  1248. # function:
  1249. # movq %rsp,%rax # copy rsp to volatile register
  1250. # pushq %r15 # save non-volatile registers
  1251. # pushq %rbx
  1252. # pushq %rbp
  1253. # movq %rsp,%r11
  1254. # subq %rdi,%r11 # prepare [variable] stack frame
  1255. # andq $-64,%r11
  1256. # movq %rax,0(%r11) # check for exceptions
  1257. # movq %r11,%rsp # allocate [variable] stack frame
  1258. # movq %rax,0(%rsp) # save original rsp value
  1259. # magic_point:
  1260. # ...
  1261. # movq 0(%rsp),%rcx # pull original rsp value
  1262. # movq -24(%rcx),%rbp # restore non-volatile registers
  1263. # movq -16(%rcx),%rbx
  1264. # movq -8(%rcx),%r15
  1265. # movq %rcx,%rsp # restore original rsp
  1266. # magic_epilogue:
  1267. # ret
  1268. # .size function,.-function
  1269. #
  1270. # The key is that up to magic_point copy of original rsp value remains
  1271. # in chosen volatile register and no non-volatile register, except for
  1272. # rsp, is modified. While past magic_point rsp remains constant till
  1273. # the very end of the function. In this case custom language-specific
  1274. # exception handler would look like this:
  1275. #
  1276. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  1277. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  1278. # { ULONG64 *rsp = (ULONG64 *)context->Rax;
  1279. # ULONG64 rip = context->Rip;
  1280. #
  1281. # if (rip >= magic_point)
  1282. # { rsp = (ULONG64 *)context->Rsp;
  1283. # if (rip < magic_epilogue)
  1284. # { rsp = (ULONG64 *)rsp[0];
  1285. # context->Rbp = rsp[-3];
  1286. # context->Rbx = rsp[-2];
  1287. # context->R15 = rsp[-1];
  1288. # }
  1289. # }
  1290. # context->Rsp = (ULONG64)rsp;
  1291. # context->Rdi = rsp[1];
  1292. # context->Rsi = rsp[2];
  1293. #
  1294. # memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
  1295. # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
  1296. # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
  1297. # &disp->HandlerData,&disp->EstablisherFrame,NULL);
  1298. # return ExceptionContinueSearch;
  1299. # }
  1300. #
  1301. # It's appropriate to implement this handler in assembler, directly in
  1302. # function's module. In order to do that one has to know members'
  1303. # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
  1304. # values. Here they are:
  1305. #
  1306. # CONTEXT.Rax 120
  1307. # CONTEXT.Rcx 128
  1308. # CONTEXT.Rdx 136
  1309. # CONTEXT.Rbx 144
  1310. # CONTEXT.Rsp 152
  1311. # CONTEXT.Rbp 160
  1312. # CONTEXT.Rsi 168
  1313. # CONTEXT.Rdi 176
  1314. # CONTEXT.R8 184
  1315. # CONTEXT.R9 192
  1316. # CONTEXT.R10 200
  1317. # CONTEXT.R11 208
  1318. # CONTEXT.R12 216
  1319. # CONTEXT.R13 224
  1320. # CONTEXT.R14 232
  1321. # CONTEXT.R15 240
  1322. # CONTEXT.Rip 248
  1323. # CONTEXT.Xmm6 512
  1324. # sizeof(CONTEXT) 1232
  1325. # DISPATCHER_CONTEXT.ControlPc 0
  1326. # DISPATCHER_CONTEXT.ImageBase 8
  1327. # DISPATCHER_CONTEXT.FunctionEntry 16
  1328. # DISPATCHER_CONTEXT.EstablisherFrame 24
  1329. # DISPATCHER_CONTEXT.TargetIp 32
  1330. # DISPATCHER_CONTEXT.ContextRecord 40
  1331. # DISPATCHER_CONTEXT.LanguageHandler 48
  1332. # DISPATCHER_CONTEXT.HandlerData 56
  1333. # UNW_FLAG_NHANDLER 0
  1334. # ExceptionContinueSearch 1
  1335. #
  1336. # In order to tie the handler to the function one has to compose
  1337. # couple of structures: one for .xdata segment and one for .pdata.
  1338. #
  1339. # UNWIND_INFO structure for .xdata segment would be
  1340. #
  1341. # function_unwind_info:
  1342. # .byte 9,0,0,0
  1343. # .rva handler
  1344. #
  1345. # This structure designates exception handler for a function with
  1346. # zero-length prologue, no stack frame or frame register.
  1347. #
  1348. # To facilitate composing of .pdata structures, auto-generated "gear"
  1349. # prologue copies rsp value to rax and denotes next instruction with
  1350. # .LSEH_begin_{function_name} label. This essentially defines the SEH
  1351. # styling rule mentioned in the beginning. Position of this label is
  1352. # chosen in such manner that possible exceptions raised in the "gear"
  1353. # prologue would be accounted to caller and unwound from latter's frame.
  1354. # End of function is marked with respective .LSEH_end_{function_name}
  1355. # label. To summarize, .pdata segment would contain
  1356. #
  1357. # .rva .LSEH_begin_function
  1358. # .rva .LSEH_end_function
  1359. # .rva function_unwind_info
  1360. #
  1361. # Reference to function_unwind_info from .xdata segment is the anchor.
  1362. # In case you wonder why references are 32-bit .rvas and not 64-bit
  1363. # .quads. References put into these two segments are required to be
  1364. # *relative* to the base address of the current binary module, a.k.a.
  1365. # image base. No Win64 module, be it .exe or .dll, can be larger than
  1366. # 2GB and thus such relative references can be and are accommodated in
  1367. # 32 bits.
  1368. #
  1369. # Having reviewed the example function code, one can argue that "movq
  1370. # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
  1371. # rax would contain an undefined value. If this "offends" you, use
  1372. # another register and refrain from modifying rax till magic_point is
  1373. # reached, i.e. as if it was a non-volatile register. If more registers
  1374. # are required prior [variable] frame setup is completed, note that
  1375. # nobody says that you can have only one "magic point." You can
  1376. # "liberate" non-volatile registers by denoting last stack off-load
  1377. # instruction and reflecting it in finer grade unwind logic in handler.
  1378. # After all, isn't it why it's called *language-specific* handler...
  1379. #
  1380. # SE handlers are also involved in unwinding stack when executable is
  1381. # profiled or debugged. Profiling implies additional limitations that
  1382. # are too subtle to discuss here. For now it's sufficient to say that
  1383. # in order to simplify handlers one should either a) offload original
  1384. # %rsp to stack (like discussed above); or b) if you have a register to
  1385. # spare for frame pointer, choose volatile one.
  1386. #
  1387. # (*) Note that we're talking about run-time, not debug-time. Lack of
  1388. # unwind information makes debugging hard on both Windows and
  1389. # Unix. "Unlike" refers to the fact that on Unix signal handler
  1390. # will always be invoked, core dumped and appropriate exit code
  1391. # returned to parent (for user notification).