A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1161 lines
35 KiB

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