25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

x86_64-xlate.pl 35 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #!/usr/bin/env perl
  2. # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
  3. #
  4. # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
  5. # format is way easier to parse. Because it's simpler to "gear" from
  6. # Unix ABI to Windows one [see cross-reference "card" at the end of
  7. # file]. Because Linux targets were available first...
  8. #
  9. # In addition the script also "distills" code suitable for GNU
  10. # assembler, so that it can be compiled with more rigid assemblers,
  11. # such as Solaris /usr/ccs/bin/as.
  12. #
  13. # This translator is not designed to convert *arbitrary* assembler
  14. # code from AT&T format to MASM one. It's designed to convert just
  15. # enough to provide for dual-ABI OpenSSL modules development...
  16. # There *are* limitations and you might have to modify your assembler
  17. # code or this script to achieve the desired result...
  18. #
  19. # Currently recognized limitations:
  20. #
  21. # - can't use multiple ops per line;
  22. #
  23. # Dual-ABI styling rules.
  24. #
  25. # 1. Adhere to Unix register and stack layout [see cross-reference
  26. # ABI "card" at the end for explanation].
  27. # 2. Forget about "red zone," stick to more traditional blended
  28. # stack frame allocation. If volatile storage is actually required
  29. # that is. If not, just leave the stack as is.
  30. # 3. Functions tagged with ".type name,@function" get crafted with
  31. # unified Win64 prologue and epilogue automatically. If you want
  32. # to take care of ABI differences yourself, tag functions as
  33. # ".type name,@abi-omnipotent" instead.
  34. # 4. To optimize the Win64 prologue you can specify number of input
  35. # arguments as ".type name,@function,N." Keep in mind that if N is
  36. # larger than 6, then you *have to* write "abi-omnipotent" code,
  37. # because >6 cases can't be addressed with unified prologue.
  38. # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
  39. # (sorry about latter).
  40. # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
  41. # required to identify the spots, where to inject Win64 epilogue!
  42. # But on the pros, it's then prefixed with rep automatically:-)
  43. # 7. Stick to explicit ip-relative addressing. If you have to use
  44. # GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
  45. # Both are recognized and translated to proper Win64 addressing
  46. # modes. To support legacy code a synthetic directive, .picmeup,
  47. # is implemented. It puts address of the *next* instruction into
  48. # target register, e.g.:
  49. #
  50. # .picmeup %rax
  51. # lea .Label-.(%rax),%rax
  52. #
  53. # 8. In order to provide for structured exception handling unified
  54. # Win64 prologue copies %rsp value to %rax. For further details
  55. # see SEH paragraph at the end.
  56. # 9. .init segment is allowed to contain calls to functions only.
  57. # a. If function accepts more than 4 arguments *and* >4th argument
  58. # is declared as non 64-bit value, do clear its upper part.
  59. my $flavour = shift;
  60. my $output = shift;
  61. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  62. open STDOUT,">$output" || die "can't open $output: $!"
  63. if (defined($output));
  64. my $gas=1; $gas=0 if ($output =~ /\.asm$/);
  65. my $elf=1; $elf=0 if (!$gas);
  66. my $win64=0;
  67. my $prefix="";
  68. my $decor=".L";
  69. my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
  70. my $masm=0;
  71. my $PTR=" PTR";
  72. my $nasmref=2.03;
  73. my $nasm=0;
  74. if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
  75. $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
  76. chomp($prefix);
  77. }
  78. elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
  79. elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
  80. elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
  81. elsif (!$gas)
  82. { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
  83. { $nasm = $1 + $2*0.01; $PTR=""; }
  84. elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
  85. { $masm = $1 + $2*2**-16 + $4*2**-32; }
  86. die "no assembler found on %PATH" if (!($nasm || $masm));
  87. $win64=1;
  88. $elf=0;
  89. $decor="\$L\$";
  90. }
  91. my $current_segment;
  92. my $current_function;
  93. my %globals;
  94. { package opcode; # pick up opcodes
  95. sub re {
  96. my $self = shift; # single instance in enough...
  97. local *line = shift;
  98. undef $ret;
  99. if ($line =~ /^([a-z][a-z0-9]*)/i) {
  100. $self->{op} = $1;
  101. $ret = $self;
  102. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  103. undef $self->{sz};
  104. undef $self->{arg_sz};
  105. if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
  106. $self->{op} = $1;
  107. $self->{sz} = $2;
  108. } elsif ($self->{op} =~ /call|jmp/) {
  109. $self->{sz} = "";
  110. } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
  111. $self->{sz} = "";
  112. } elsif ($self->{op} eq "vpbroadcastq") {
  113. $self->{arg_sz} = "q";
  114. } elsif ($self->{op} eq "vpbroadcastb") {
  115. $self->{arg_sz} = "b";
  116. } elsif ($self->{op} =~ /^vinserti128/) {
  117. $self->{arg_sz} = "x";
  118. } elsif ($self->{op} =~ /^v/) { # VEX
  119. $self->{sz} = "";
  120. } elsif ($self->{op} =~ /movq/ && $line =~ /%xmm/) {
  121. $self->{sz} = "";
  122. } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
  123. $self->{op} = $1;
  124. $self->{sz} = $2;
  125. }
  126. }
  127. $ret;
  128. }
  129. sub size {
  130. my $self = shift;
  131. my $sz = shift;
  132. $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
  133. $self->{sz};
  134. }
  135. sub arg_size {
  136. my $self = shift;
  137. $self->{arg_sz};
  138. }
  139. sub out {
  140. my $self = shift;
  141. if ($gas) {
  142. if ($self->{op} eq "movz") { # movz is pain...
  143. sprintf "%s%s%s",$self->{op},$self->{sz},shift;
  144. } elsif ($self->{op} =~ /^set/) {
  145. "$self->{op}";
  146. } elsif ($self->{op} eq "ret") {
  147. my $epilogue = "";
  148. if ($win64 && $current_function->{abi} eq "svr4") {
  149. $epilogue = "movq 8(%rsp),%rdi\n\t" .
  150. "movq 16(%rsp),%rsi\n\t";
  151. }
  152. $epilogue . ".byte 0xf3,0xc3";
  153. } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
  154. ".p2align\t3\n\t.quad";
  155. } else {
  156. "$self->{op}$self->{sz}";
  157. }
  158. } else {
  159. $self->{op} =~ s/^movz/movzx/;
  160. if ($self->{op} eq "ret") {
  161. $self->{op} = "";
  162. if ($win64 && $current_function->{abi} eq "svr4") {
  163. $self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t".
  164. "mov rsi,QWORD${PTR}[16+rsp]\n\t";
  165. }
  166. $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
  167. } elsif ($self->{op} =~ /^(pop|push)f/) {
  168. $self->{op} .= $self->{sz};
  169. } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
  170. $self->{op} = "\tDQ";
  171. }
  172. $self->{op};
  173. }
  174. }
  175. sub mnemonic {
  176. my $self=shift;
  177. my $op=shift;
  178. $self->{op}=$op if (defined($op));
  179. $self->{op};
  180. }
  181. }
  182. { package const; # pick up constants, which start with $
  183. sub re {
  184. my $self = shift; # single instance in enough...
  185. local *line = shift;
  186. undef $ret;
  187. if ($line =~ /^\$([^,]+)/) {
  188. $self->{value} = $1;
  189. $ret = $self;
  190. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  191. }
  192. $ret;
  193. }
  194. sub out {
  195. my $self = shift;
  196. if ($gas) {
  197. # Solaris /usr/ccs/bin/as can't handle multiplications
  198. # in $self->{value}
  199. $self->{value} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
  200. $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
  201. sprintf "\$%s",$self->{value};
  202. } else {
  203. $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig;
  204. $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
  205. sprintf "%s",$self->{value};
  206. }
  207. }
  208. }
  209. { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
  210. sub re {
  211. my $self = shift; # single instance in enough...
  212. local *line = shift;
  213. undef $ret;
  214. # optional * ---vvv--- appears in indirect jmp/call
  215. if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) {
  216. $self->{asterisk} = $1;
  217. $self->{label} = $2;
  218. ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
  219. $self->{scale} = 1 if (!defined($self->{scale}));
  220. $ret = $self;
  221. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  222. if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
  223. die if (opcode->mnemonic() ne "mov");
  224. opcode->mnemonic("lea");
  225. }
  226. $self->{base} =~ s/^%//;
  227. $self->{index} =~ s/^%// if (defined($self->{index}));
  228. }
  229. $ret;
  230. }
  231. sub size {}
  232. sub out {
  233. my $self = shift;
  234. my $sz = shift;
  235. $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  236. $self->{label} =~ s/\.L/$decor/g;
  237. # Silently convert all EAs to 64-bit. This is required for
  238. # elder GNU assembler and results in more compact code,
  239. # *but* most importantly AES module depends on this feature!
  240. $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  241. $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  242. # Solaris /usr/ccs/bin/as can't handle multiplications
  243. # in $self->{label}, new gas requires sign extension...
  244. use integer;
  245. $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
  246. $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
  247. $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
  248. if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
  249. $self->{base} =~ /(rbp|r13)/) {
  250. $self->{base} = $self->{index}; $self->{index} = $1;
  251. }
  252. if ($gas) {
  253. $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
  254. if (defined($self->{index})) {
  255. sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk},
  256. $self->{label},
  257. $self->{base}?"%$self->{base}":"",
  258. $self->{index},$self->{scale};
  259. } else {
  260. sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base};
  261. }
  262. } else {
  263. %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR",
  264. q=>"QWORD$PTR",o=>"OWORD$PTR",x=>"XMMWORD$PTR",
  265. y=>"YMMWORD$PTR" );
  266. $self->{label} =~ s/\./\$/g;
  267. $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
  268. $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
  269. $sz="q" if ($self->{asterisk} || opcode->mnemonic() =~ /^v?movq$/);
  270. $sz="l" if (opcode->mnemonic() =~ /^v?movd$/);
  271. if (defined($self->{index})) {
  272. sprintf "%s[%s%s*%d%s]",$szmap{$sz},
  273. $self->{label}?"$self->{label}+":"",
  274. $self->{index},$self->{scale},
  275. $self->{base}?"+$self->{base}":"";
  276. } elsif ($self->{base} eq "rip") {
  277. sprintf "%s[%s]",$szmap{$sz},$self->{label};
  278. } else {
  279. sprintf "%s[%s%s]",$szmap{$sz},
  280. $self->{label}?"$self->{label}+":"",
  281. $self->{base};
  282. }
  283. }
  284. }
  285. }
  286. { package register; # pick up registers, which start with %.
  287. sub re {
  288. my $class = shift; # muliple instances...
  289. my $self = {};
  290. local *line = shift;
  291. undef $ret;
  292. # optional * ---vvv--- appears in indirect jmp/call
  293. if ($line =~ /^(\*?)%(\w+)/) {
  294. bless $self,$class;
  295. $self->{asterisk} = $1;
  296. $self->{value} = $2;
  297. $ret = $self;
  298. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  299. }
  300. $ret;
  301. }
  302. sub size {
  303. my $self = shift;
  304. undef $ret;
  305. if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
  306. elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
  307. elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
  308. elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
  309. elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
  310. elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
  311. elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
  312. elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
  313. $ret;
  314. }
  315. sub out {
  316. my $self = shift;
  317. if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; }
  318. else { $self->{value}; }
  319. }
  320. }
  321. { package label; # pick up labels, which end with :
  322. sub re {
  323. my $self = shift; # single instance is enough...
  324. local *line = shift;
  325. undef $ret;
  326. if ($line =~ /(^[\.\w]+)\:/) {
  327. $self->{value} = $1;
  328. $ret = $self;
  329. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  330. $self->{value} =~ s/^\.L/$decor/;
  331. }
  332. $ret;
  333. }
  334. sub out {
  335. my $self = shift;
  336. if ($gas) {
  337. my $func = ($globals{$self->{value}} or $self->{value}) . ":";
  338. if ($win64 &&
  339. $current_function->{name} eq $self->{value} &&
  340. $current_function->{abi} eq "svr4") {
  341. $func .= "\n";
  342. $func .= " movq %rdi,8(%rsp)\n";
  343. $func .= " movq %rsi,16(%rsp)\n";
  344. $func .= " movq %rsp,%rax\n";
  345. $func .= "${decor}SEH_begin_$current_function->{name}:\n";
  346. my $narg = $current_function->{narg};
  347. $narg=6 if (!defined($narg));
  348. $func .= " movq %rcx,%rdi\n" if ($narg>0);
  349. $func .= " movq %rdx,%rsi\n" if ($narg>1);
  350. $func .= " movq %r8,%rdx\n" if ($narg>2);
  351. $func .= " movq %r9,%rcx\n" if ($narg>3);
  352. $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
  353. $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
  354. }
  355. $func;
  356. } elsif ($self->{value} ne "$current_function->{name}") {
  357. $self->{value} .= ":" if ($masm && $ret!~m/^\$/);
  358. $self->{value} . ":";
  359. } elsif ($win64 && $current_function->{abi} eq "svr4") {
  360. my $func = "$current_function->{name}" .
  361. ($nasm ? ":" : "\tPROC $current_function->{scope}") .
  362. "\n";
  363. $func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n";
  364. $func .= " mov QWORD${PTR}[16+rsp],rsi\n";
  365. $func .= " mov rax,rsp\n";
  366. $func .= "${decor}SEH_begin_$current_function->{name}:";
  367. $func .= ":" if ($masm);
  368. $func .= "\n";
  369. my $narg = $current_function->{narg};
  370. $narg=6 if (!defined($narg));
  371. $func .= " mov rdi,rcx\n" if ($narg>0);
  372. $func .= " mov rsi,rdx\n" if ($narg>1);
  373. $func .= " mov rdx,r8\n" if ($narg>2);
  374. $func .= " mov rcx,r9\n" if ($narg>3);
  375. $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4);
  376. $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5);
  377. $func .= "\n";
  378. } else {
  379. "$current_function->{name}".
  380. ($nasm ? ":" : "\tPROC $current_function->{scope}");
  381. }
  382. }
  383. }
  384. { package expr; # pick up expressioins
  385. sub re {
  386. my $self = shift; # single instance is enough...
  387. local *line = shift;
  388. undef $ret;
  389. if ($line =~ /(^[^,]+)/) {
  390. $self->{value} = $1;
  391. $ret = $self;
  392. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  393. $self->{value} =~ s/\@PLT// if (!$elf);
  394. $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  395. $self->{value} =~ s/\.L/$decor/g;
  396. }
  397. $ret;
  398. }
  399. sub out {
  400. my $self = shift;
  401. if ($nasm && opcode->mnemonic()=~m/^j(?![re]cxz)/) {
  402. "NEAR ".$self->{value};
  403. } else {
  404. $self->{value};
  405. }
  406. }
  407. }
  408. { package directive; # pick up directives, which start with .
  409. sub re {
  410. my $self = shift; # single instance is enough...
  411. local *line = shift;
  412. undef $ret;
  413. my $dir;
  414. my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
  415. ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
  416. "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
  417. "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
  418. "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
  419. "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
  420. "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
  421. "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
  422. "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
  423. if ($line =~ /^\s*(\.\w+)/) {
  424. $dir = $1;
  425. $ret = $self;
  426. undef $self->{value};
  427. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  428. SWITCH: for ($dir) {
  429. /\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) {
  430. $dir="\t.long";
  431. $line=sprintf "0x%x,0x90000000",$opcode{$1};
  432. }
  433. last;
  434. };
  435. /\.global|\.globl|\.extern/
  436. && do { $globals{$line} = $prefix . $line;
  437. $line = $globals{$line} if ($prefix);
  438. last;
  439. };
  440. /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
  441. if ($type eq "\@function") {
  442. undef $current_function;
  443. $current_function->{name} = $sym;
  444. $current_function->{abi} = "svr4";
  445. $current_function->{narg} = $narg;
  446. $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
  447. } elsif ($type eq "\@abi-omnipotent") {
  448. undef $current_function;
  449. $current_function->{name} = $sym;
  450. $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
  451. }
  452. $line =~ s/\@abi\-omnipotent/\@function/;
  453. $line =~ s/\@function.*/\@function/;
  454. last;
  455. };
  456. /\.asciz/ && do { if ($line =~ /^"(.*)"$/) {
  457. $dir = ".byte";
  458. $line = join(",",unpack("C*",$1),0);
  459. }
  460. last;
  461. };
  462. /\.rva|\.long|\.quad/
  463. && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
  464. $line =~ s/\.L/$decor/g;
  465. last;
  466. };
  467. }
  468. if ($gas) {
  469. $self->{value} = $dir . "\t" . $line;
  470. if ($dir =~ /\.extern/) {
  471. $self->{value} = ""; # swallow extern
  472. } elsif (!$elf && $dir =~ /\.type/) {
  473. $self->{value} = "";
  474. $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
  475. (defined($globals{$1})?".scl 2;":".scl 3;") .
  476. "\t.type 32;\t.endef"
  477. if ($win64 && $line =~ /([^,]+),\@function/);
  478. } elsif (!$elf && $dir =~ /\.size/) {
  479. $self->{value} = "";
  480. if (defined($current_function)) {
  481. $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
  482. if ($win64 && $current_function->{abi} eq "svr4");
  483. undef $current_function;
  484. }
  485. } elsif (!$elf && $dir =~ /\.align/) {
  486. $self->{value} = ".p2align\t" . (log($line)/log(2));
  487. } elsif ($dir eq ".section") {
  488. $current_segment=$line;
  489. if (!$elf && $current_segment eq ".init") {
  490. if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
  491. elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
  492. }
  493. } elsif ($dir =~ /\.(text|data)/) {
  494. $current_segment=".$1";
  495. } elsif ($dir =~ /\.hidden/) {
  496. if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$line"; }
  497. elsif ($flavour eq "mingw64") { $self->{value} = ""; }
  498. } elsif ($dir =~ /\.comm/) {
  499. $self->{value} = "$dir\t$prefix$line";
  500. $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
  501. }
  502. $line = "";
  503. return $self;
  504. }
  505. # non-gas case or nasm/masm
  506. SWITCH: for ($dir) {
  507. /\.text/ && do { my $v=undef;
  508. if ($nasm) {
  509. $v="section .text code align=64\n";
  510. } else {
  511. $v="$current_segment\tENDS\n" if ($current_segment);
  512. $current_segment = ".text\$";
  513. $v.="$current_segment\tSEGMENT ";
  514. $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE";
  515. $v.=" 'CODE'";
  516. }
  517. $self->{value} = $v;
  518. last;
  519. };
  520. /\.data/ && do { my $v=undef;
  521. if ($nasm) {
  522. $v="section .data data align=8\n";
  523. } else {
  524. $v="$current_segment\tENDS\n" if ($current_segment);
  525. $current_segment = "_DATA";
  526. $v.="$current_segment\tSEGMENT";
  527. }
  528. $self->{value} = $v;
  529. last;
  530. };
  531. /\.section/ && do { my $v=undef;
  532. $line =~ s/([^,]*).*/$1/;
  533. $line = ".CRT\$XCU" if ($line eq ".init");
  534. if ($nasm) {
  535. $v="section $line";
  536. if ($line=~/\.([px])data/) {
  537. $v.=" rdata align=";
  538. $v.=$1 eq "p"? 4 : 8;
  539. } elsif ($line=~/\.CRT\$/i) {
  540. $v.=" rdata align=8";
  541. }
  542. } else {
  543. $v="$current_segment\tENDS\n" if ($current_segment);
  544. $v.="$line\tSEGMENT";
  545. if ($line=~/\.([px])data/) {
  546. $v.=" READONLY";
  547. $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
  548. } elsif ($line=~/\.CRT\$/i) {
  549. $v.=" READONLY ";
  550. $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
  551. }
  552. }
  553. $current_segment = $line;
  554. $self->{value} = $v;
  555. last;
  556. };
  557. /\.extern/ && do { $self->{value} = "EXTERN\t".$line;
  558. $self->{value} .= ":NEAR" if ($masm);
  559. last;
  560. };
  561. /\.globl|.global/
  562. && do { $self->{value} = $masm?"PUBLIC":"global";
  563. $self->{value} .= "\t".$line;
  564. last;
  565. };
  566. /\.size/ && do { if (defined($current_function)) {
  567. undef $self->{value};
  568. if ($current_function->{abi} eq "svr4") {
  569. $self->{value}="${decor}SEH_end_$current_function->{name}:";
  570. $self->{value}.=":\n" if($masm);
  571. }
  572. $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
  573. undef $current_function;
  574. }
  575. last;
  576. };
  577. /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
  578. /\.(value|long|rva|quad)/
  579. && do { my $sz = substr($1,0,1);
  580. my @arr = split(/,\s*/,$line);
  581. my $last = pop(@arr);
  582. my $conv = sub { my $var=shift;
  583. $var=~s/^(0b[0-1]+)/oct($1)/eig;
  584. $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
  585. if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
  586. { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
  587. $var;
  588. };
  589. $sz =~ tr/bvlrq/BWDDQ/;
  590. $self->{value} = "\tD$sz\t";
  591. for (@arr) { $self->{value} .= &$conv($_).","; }
  592. $self->{value} .= &$conv($last);
  593. last;
  594. };
  595. /\.byte/ && do { my @str=split(/,\s*/,$line);
  596. map(s/(0b[0-1]+)/oct($1)/eig,@str);
  597. map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
  598. while ($#str>15) {
  599. $self->{value}.="DB\t"
  600. .join(",",@str[0..15])."\n";
  601. foreach (0..15) { shift @str; }
  602. }
  603. $self->{value}.="DB\t"
  604. .join(",",@str) if (@str);
  605. last;
  606. };
  607. /\.comm/ && do { my @str=split(/,\s*/,$line);
  608. my $v=undef;
  609. if ($nasm) {
  610. $v.="common $prefix@str[0] @str[1]";
  611. } else {
  612. $v="$current_segment\tENDS\n" if ($current_segment);
  613. $current_segment = "_DATA";
  614. $v.="$current_segment\tSEGMENT\n";
  615. $v.="COMM @str[0]:DWORD:".@str[1]/4;
  616. }
  617. $self->{value} = $v;
  618. last;
  619. };
  620. }
  621. $line = "";
  622. }
  623. $ret;
  624. }
  625. sub out {
  626. my $self = shift;
  627. $self->{value};
  628. }
  629. }
  630. sub rex {
  631. local *opcode=shift;
  632. my ($dst,$src,$rex)=@_;
  633. $rex|=0x04 if($dst>=8);
  634. $rex|=0x01 if($src>=8);
  635. push @opcode,($rex|0x40) if ($rex);
  636. }
  637. # older gas and ml64 don't handle SSE>2 instructions
  638. my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
  639. "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
  640. my $movq = sub { # elderly gas can't handle inter-register movq
  641. my $arg = shift;
  642. my @opcode=(0x66);
  643. if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
  644. my ($src,$dst)=($1,$2);
  645. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  646. rex(\@opcode,$src,$dst,0x8);
  647. push @opcode,0x0f,0x7e;
  648. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  649. @opcode;
  650. } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
  651. my ($src,$dst)=($2,$1);
  652. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  653. rex(\@opcode,$src,$dst,0x8);
  654. push @opcode,0x0f,0x6e;
  655. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  656. @opcode;
  657. } else {
  658. ();
  659. }
  660. };
  661. my $pextrd = sub {
  662. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
  663. my @opcode=(0x66);
  664. $imm=$1;
  665. $src=$2;
  666. $dst=$3;
  667. if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
  668. elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
  669. rex(\@opcode,$src,$dst);
  670. push @opcode,0x0f,0x3a,0x16;
  671. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  672. push @opcode,$imm;
  673. @opcode;
  674. } else {
  675. ();
  676. }
  677. };
  678. my $pinsrd = sub {
  679. if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
  680. my @opcode=(0x66);
  681. $imm=$1;
  682. $src=$2;
  683. $dst=$3;
  684. if ($src =~ /%r([0-9]+)/) { $src = $1; }
  685. elsif ($src =~ /%e/) { $src = $regrm{$src}; }
  686. rex(\@opcode,$dst,$src);
  687. push @opcode,0x0f,0x3a,0x22;
  688. push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
  689. push @opcode,$imm;
  690. @opcode;
  691. } else {
  692. ();
  693. }
  694. };
  695. my $pshufb = sub {
  696. if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  697. my @opcode=(0x66);
  698. rex(\@opcode,$2,$1);
  699. push @opcode,0x0f,0x38,0x00;
  700. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  701. @opcode;
  702. } else {
  703. ();
  704. }
  705. };
  706. my $palignr = sub {
  707. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  708. my @opcode=(0x66);
  709. rex(\@opcode,$3,$2);
  710. push @opcode,0x0f,0x3a,0x0f;
  711. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  712. push @opcode,$1;
  713. @opcode;
  714. } else {
  715. ();
  716. }
  717. };
  718. my $pclmulqdq = sub {
  719. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  720. my @opcode=(0x66);
  721. rex(\@opcode,$3,$2);
  722. push @opcode,0x0f,0x3a,0x44;
  723. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  724. my $c=$1;
  725. push @opcode,$c=~/^0/?oct($c):$c;
  726. @opcode;
  727. } else {
  728. ();
  729. }
  730. };
  731. my $rdrand = sub {
  732. if (shift =~ /%[er](\w+)/) {
  733. my @opcode=();
  734. my $dst=$1;
  735. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  736. rex(\@opcode,0,$1,8);
  737. push @opcode,0x0f,0xc7,0xf0|($dst&7);
  738. @opcode;
  739. } else {
  740. ();
  741. }
  742. };
  743. sub rxb {
  744. local *opcode=shift;
  745. my ($dst,$src1,$src2,$rxb)=@_;
  746. $rxb|=0x7<<5;
  747. $rxb&=~(0x04<<5) if($dst>=8);
  748. $rxb&=~(0x01<<5) if($src1>=8);
  749. $rxb&=~(0x02<<5) if($src2>=8);
  750. push @opcode,$rxb;
  751. }
  752. my $vprotd = sub {
  753. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  754. my @opcode=(0x8f);
  755. rxb(\@opcode,$3,$2,-1,0x08);
  756. push @opcode,0x78,0xc2;
  757. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  758. my $c=$1;
  759. push @opcode,$c=~/^0/?oct($c):$c;
  760. @opcode;
  761. } else {
  762. ();
  763. }
  764. };
  765. my $vprotq = sub {
  766. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  767. my @opcode=(0x8f);
  768. rxb(\@opcode,$3,$2,-1,0x08);
  769. push @opcode,0x78,0xc3;
  770. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  771. my $c=$1;
  772. push @opcode,$c=~/^0/?oct($c):$c;
  773. @opcode;
  774. } else {
  775. ();
  776. }
  777. };
  778. if ($nasm) {
  779. print <<___;
  780. default rel
  781. %define XMMWORD
  782. ___
  783. } elsif ($masm) {
  784. print <<___;
  785. OPTION DOTNAME
  786. ___
  787. }
  788. print STDOUT "#if defined(__x86_64__)\n" if ($gas);
  789. while($line=<>) {
  790. chomp($line);
  791. $line =~ s|[#!].*$||; # get rid of asm-style comments...
  792. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  793. $line =~ s|^\s+||; # ... and skip white spaces in beginning
  794. $line =~ s|\s+$||; # ... and at the end
  795. undef $label;
  796. undef $opcode;
  797. undef @args;
  798. if ($label=label->re(\$line)) { print $label->out(); }
  799. if (directive->re(\$line)) {
  800. printf "%s",directive->out();
  801. } elsif ($opcode=opcode->re(\$line)) {
  802. my $asm = eval("\$".$opcode->mnemonic());
  803. undef @bytes;
  804. if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) {
  805. print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
  806. next;
  807. }
  808. ARGUMENT: while (1) {
  809. my $arg;
  810. if ($arg=register->re(\$line)) { opcode->size($arg->size()); }
  811. elsif ($arg=const->re(\$line)) { }
  812. elsif ($arg=ea->re(\$line)) { }
  813. elsif ($arg=expr->re(\$line)) { }
  814. else { last ARGUMENT; }
  815. push @args,$arg;
  816. last ARGUMENT if ($line !~ /^,/);
  817. $line =~ s/^,\s*//;
  818. } # ARGUMENT:
  819. if ($#args>=0) {
  820. my $insn;
  821. my $sz=opcode->size();
  822. my $arg_sz=opcode->arg_size();
  823. if ($gas) {
  824. $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
  825. @args = map($_->out($sz),@args);
  826. printf "\t%s\t%s",$insn,join(",",@args);
  827. } else {
  828. $insn = $opcode->out();
  829. foreach (@args) {
  830. my $arg = $_->out();
  831. # $insn.=$sz compensates for movq, pinsrw, ...
  832. if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
  833. if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
  834. if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
  835. }
  836. $sz=$arg_sz if($arg_sz);
  837. @args = reverse(@args);
  838. undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
  839. if ($insn eq "movq" && $#args == 1 && $args[0]->out($sz) eq "xmm0" && $args[1]->out($sz) eq "rax") {
  840. # I have no clue why MASM can't parse this instruction.
  841. printf "DB 66h, 48h, 0fh, 6eh, 0c0h";
  842. } else {
  843. printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
  844. }
  845. }
  846. } else {
  847. printf "\t%s",$opcode->out();
  848. }
  849. }
  850. print $line,"\n";
  851. }
  852. print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
  853. print "END\n" if ($masm);
  854. print "#endif" if ($gas);
  855. close STDOUT;
  856. #################################################
  857. # Cross-reference x86_64 ABI "card"
  858. #
  859. # Unix Win64
  860. # %rax * *
  861. # %rbx - -
  862. # %rcx #4 #1
  863. # %rdx #3 #2
  864. # %rsi #2 -
  865. # %rdi #1 -
  866. # %rbp - -
  867. # %rsp - -
  868. # %r8 #5 #3
  869. # %r9 #6 #4
  870. # %r10 * *
  871. # %r11 * *
  872. # %r12 - -
  873. # %r13 - -
  874. # %r14 - -
  875. # %r15 - -
  876. #
  877. # (*) volatile register
  878. # (-) preserved by callee
  879. # (#) Nth argument, volatile
  880. #
  881. # In Unix terms top of stack is argument transfer area for arguments
  882. # which could not be accomodated in registers. Or in other words 7th
  883. # [integer] argument resides at 8(%rsp) upon function entry point.
  884. # 128 bytes above %rsp constitute a "red zone" which is not touched
  885. # by signal handlers and can be used as temporal storage without
  886. # allocating a frame.
  887. #
  888. # In Win64 terms N*8 bytes on top of stack is argument transfer area,
  889. # which belongs to/can be overwritten by callee. N is the number of
  890. # arguments passed to callee, *but* not less than 4! This means that
  891. # upon function entry point 5th argument resides at 40(%rsp), as well
  892. # as that 32 bytes from 8(%rsp) can always be used as temporal
  893. # storage [without allocating a frame]. One can actually argue that
  894. # one can assume a "red zone" above stack pointer under Win64 as well.
  895. # Point is that at apparently no occasion Windows kernel would alter
  896. # the area above user stack pointer in true asynchronous manner...
  897. #
  898. # All the above means that if assembler programmer adheres to Unix
  899. # register and stack layout, but disregards the "red zone" existense,
  900. # it's possible to use following prologue and epilogue to "gear" from
  901. # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
  902. #
  903. # omnipotent_function:
  904. # ifdef WIN64
  905. # movq %rdi,8(%rsp)
  906. # movq %rsi,16(%rsp)
  907. # movq %rcx,%rdi ; if 1st argument is actually present
  908. # movq %rdx,%rsi ; if 2nd argument is actually ...
  909. # movq %r8,%rdx ; if 3rd argument is ...
  910. # movq %r9,%rcx ; if 4th argument ...
  911. # movq 40(%rsp),%r8 ; if 5th ...
  912. # movq 48(%rsp),%r9 ; if 6th ...
  913. # endif
  914. # ...
  915. # ifdef WIN64
  916. # movq 8(%rsp),%rdi
  917. # movq 16(%rsp),%rsi
  918. # endif
  919. # ret
  920. #
  921. #################################################
  922. # Win64 SEH, Structured Exception Handling.
  923. #
  924. # Unlike on Unix systems(*) lack of Win64 stack unwinding information
  925. # has undesired side-effect at run-time: if an exception is raised in
  926. # assembler subroutine such as those in question (basically we're
  927. # referring to segmentation violations caused by malformed input
  928. # parameters), the application is briskly terminated without invoking
  929. # any exception handlers, most notably without generating memory dump
  930. # or any user notification whatsoever. This poses a problem. It's
  931. # possible to address it by registering custom language-specific
  932. # handler that would restore processor context to the state at
  933. # subroutine entry point and return "exception is not handled, keep
  934. # unwinding" code. Writing such handler can be a challenge... But it's
  935. # doable, though requires certain coding convention. Consider following
  936. # snippet:
  937. #
  938. # .type function,@function
  939. # function:
  940. # movq %rsp,%rax # copy rsp to volatile register
  941. # pushq %r15 # save non-volatile registers
  942. # pushq %rbx
  943. # pushq %rbp
  944. # movq %rsp,%r11
  945. # subq %rdi,%r11 # prepare [variable] stack frame
  946. # andq $-64,%r11
  947. # movq %rax,0(%r11) # check for exceptions
  948. # movq %r11,%rsp # allocate [variable] stack frame
  949. # movq %rax,0(%rsp) # save original rsp value
  950. # magic_point:
  951. # ...
  952. # movq 0(%rsp),%rcx # pull original rsp value
  953. # movq -24(%rcx),%rbp # restore non-volatile registers
  954. # movq -16(%rcx),%rbx
  955. # movq -8(%rcx),%r15
  956. # movq %rcx,%rsp # restore original rsp
  957. # ret
  958. # .size function,.-function
  959. #
  960. # The key is that up to magic_point copy of original rsp value remains
  961. # in chosen volatile register and no non-volatile register, except for
  962. # rsp, is modified. While past magic_point rsp remains constant till
  963. # the very end of the function. In this case custom language-specific
  964. # exception handler would look like this:
  965. #
  966. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  967. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  968. # { ULONG64 *rsp = (ULONG64 *)context->Rax;
  969. # if (context->Rip >= magic_point)
  970. # { rsp = ((ULONG64 **)context->Rsp)[0];
  971. # context->Rbp = rsp[-3];
  972. # context->Rbx = rsp[-2];
  973. # context->R15 = rsp[-1];
  974. # }
  975. # context->Rsp = (ULONG64)rsp;
  976. # context->Rdi = rsp[1];
  977. # context->Rsi = rsp[2];
  978. #
  979. # memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
  980. # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
  981. # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
  982. # &disp->HandlerData,&disp->EstablisherFrame,NULL);
  983. # return ExceptionContinueSearch;
  984. # }
  985. #
  986. # It's appropriate to implement this handler in assembler, directly in
  987. # function's module. In order to do that one has to know members'
  988. # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
  989. # values. Here they are:
  990. #
  991. # CONTEXT.Rax 120
  992. # CONTEXT.Rcx 128
  993. # CONTEXT.Rdx 136
  994. # CONTEXT.Rbx 144
  995. # CONTEXT.Rsp 152
  996. # CONTEXT.Rbp 160
  997. # CONTEXT.Rsi 168
  998. # CONTEXT.Rdi 176
  999. # CONTEXT.R8 184
  1000. # CONTEXT.R9 192
  1001. # CONTEXT.R10 200
  1002. # CONTEXT.R11 208
  1003. # CONTEXT.R12 216
  1004. # CONTEXT.R13 224
  1005. # CONTEXT.R14 232
  1006. # CONTEXT.R15 240
  1007. # CONTEXT.Rip 248
  1008. # CONTEXT.Xmm6 512
  1009. # sizeof(CONTEXT) 1232
  1010. # DISPATCHER_CONTEXT.ControlPc 0
  1011. # DISPATCHER_CONTEXT.ImageBase 8
  1012. # DISPATCHER_CONTEXT.FunctionEntry 16
  1013. # DISPATCHER_CONTEXT.EstablisherFrame 24
  1014. # DISPATCHER_CONTEXT.TargetIp 32
  1015. # DISPATCHER_CONTEXT.ContextRecord 40
  1016. # DISPATCHER_CONTEXT.LanguageHandler 48
  1017. # DISPATCHER_CONTEXT.HandlerData 56
  1018. # UNW_FLAG_NHANDLER 0
  1019. # ExceptionContinueSearch 1
  1020. #
  1021. # In order to tie the handler to the function one has to compose
  1022. # couple of structures: one for .xdata segment and one for .pdata.
  1023. #
  1024. # UNWIND_INFO structure for .xdata segment would be
  1025. #
  1026. # function_unwind_info:
  1027. # .byte 9,0,0,0
  1028. # .rva handler
  1029. #
  1030. # This structure designates exception handler for a function with
  1031. # zero-length prologue, no stack frame or frame register.
  1032. #
  1033. # To facilitate composing of .pdata structures, auto-generated "gear"
  1034. # prologue copies rsp value to rax and denotes next instruction with
  1035. # .LSEH_begin_{function_name} label. This essentially defines the SEH
  1036. # styling rule mentioned in the beginning. Position of this label is
  1037. # chosen in such manner that possible exceptions raised in the "gear"
  1038. # prologue would be accounted to caller and unwound from latter's frame.
  1039. # End of function is marked with respective .LSEH_end_{function_name}
  1040. # label. To summarize, .pdata segment would contain
  1041. #
  1042. # .rva .LSEH_begin_function
  1043. # .rva .LSEH_end_function
  1044. # .rva function_unwind_info
  1045. #
  1046. # Reference to functon_unwind_info from .xdata segment is the anchor.
  1047. # In case you wonder why references are 32-bit .rvas and not 64-bit
  1048. # .quads. References put into these two segments are required to be
  1049. # *relative* to the base address of the current binary module, a.k.a.
  1050. # image base. No Win64 module, be it .exe or .dll, can be larger than
  1051. # 2GB and thus such relative references can be and are accommodated in
  1052. # 32 bits.
  1053. #
  1054. # Having reviewed the example function code, one can argue that "movq
  1055. # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
  1056. # rax would contain an undefined value. If this "offends" you, use
  1057. # another register and refrain from modifying rax till magic_point is
  1058. # reached, i.e. as if it was a non-volatile register. If more registers
  1059. # are required prior [variable] frame setup is completed, note that
  1060. # nobody says that you can have only one "magic point." You can
  1061. # "liberate" non-volatile registers by denoting last stack off-load
  1062. # instruction and reflecting it in finer grade unwind logic in handler.
  1063. # After all, isn't it why it's called *language-specific* handler...
  1064. #
  1065. # Attentive reader can notice that exceptions would be mishandled in
  1066. # auto-generated "gear" epilogue. Well, exception effectively can't
  1067. # occur there, because if memory area used by it was subject to
  1068. # segmentation violation, then it would be raised upon call to the
  1069. # function (and as already mentioned be accounted to caller, which is
  1070. # not a problem). If you're still not comfortable, then define tail
  1071. # "magic point" just prior ret instruction and have handler treat it...
  1072. #
  1073. # (*) Note that we're talking about run-time, not debug-time. Lack of
  1074. # unwind information makes debugging hard on both Windows and
  1075. # Unix. "Unlike" referes to the fact that on Unix signal handler
  1076. # will always be invoked, core dumped and appropriate exit code
  1077. # returned to parent (for user notification).