x86_64-xlate.pl 33 KB

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