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.
 
 
 
 
 
 

1166 lines
35 KiB

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