2
0

x86_64-xlate.pl 44 KB

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