選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1170 行
36 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. if ($flavour eq "elf") {
  465. $self->{value} .= "\n.hidden $line";
  466. } else {
  467. $self->{value} = "";
  468. }
  469. } elsif (!$elf && $dir =~ /\.type/) {
  470. $self->{value} = "";
  471. $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
  472. (defined($globals{$1})?".scl 2;":".scl 3;") .
  473. "\t.type 32;\t.endef"
  474. if ($win64 && $line =~ /([^,]+),\@function/);
  475. } elsif (!$elf && $dir =~ /\.size/) {
  476. $self->{value} = "";
  477. if (defined($current_function)) {
  478. $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
  479. if ($win64 && $current_function->{abi} eq "svr4");
  480. undef $current_function;
  481. }
  482. } elsif (!$elf && $dir =~ /\.align/) {
  483. $self->{value} = ".p2align\t" . (log($line)/log(2));
  484. } elsif ($dir eq ".section") {
  485. $current_segment=$line;
  486. if (!$elf && $current_segment eq ".init") {
  487. if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
  488. elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
  489. }
  490. } elsif ($dir =~ /\.(text|data)/) {
  491. $current_segment=".$1";
  492. } elsif ($dir =~ /\.global|\.globl|\.extern/) {
  493. if ($flavour eq "macosx") {
  494. $self->{value} .= "\n.private_extern $line";
  495. } else {
  496. $self->{value} .= "\n.hidden $line";
  497. }
  498. } elsif ($dir =~ /\.hidden/) {
  499. if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$line"; }
  500. elsif ($flavour eq "mingw64") { $self->{value} = ""; }
  501. } elsif ($dir =~ /\.comm/) {
  502. $self->{value} = "$dir\t$prefix$line";
  503. $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
  504. }
  505. $line = "";
  506. return $self;
  507. }
  508. # non-gas case or nasm/masm
  509. SWITCH: for ($dir) {
  510. /\.text/ && do { my $v=undef;
  511. if ($nasm) {
  512. $v="section .text code align=64\n";
  513. } else {
  514. $v="$current_segment\tENDS\n" if ($current_segment);
  515. $current_segment = ".text\$";
  516. $v.="$current_segment\tSEGMENT ";
  517. $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
  518. $v.=" 'CODE'";
  519. }
  520. $self->{value} = $v;
  521. last;
  522. };
  523. /\.data/ && do { my $v=undef;
  524. if ($nasm) {
  525. $v="section .data data align=8\n";
  526. } else {
  527. $v="$current_segment\tENDS\n" if ($current_segment);
  528. $current_segment = "_DATA";
  529. $v.="$current_segment\tSEGMENT";
  530. }
  531. $self->{value} = $v;
  532. last;
  533. };
  534. /\.section/ && do { my $v=undef;
  535. $line =~ s/([^,]*).*/$1/;
  536. $line = ".CRT\$XCU" if ($line eq ".init");
  537. if ($nasm) {
  538. $v="section $line";
  539. if ($line=~/\.([px])data/) {
  540. $v.=" rdata align=";
  541. $v.=$1 eq "p"? 4 : 8;
  542. } elsif ($line=~/\.CRT\$/i) {
  543. $v.=" rdata align=8";
  544. }
  545. } else {
  546. $v="$current_segment\tENDS\n" if ($current_segment);
  547. $v.="$line\tSEGMENT";
  548. if ($line=~/\.([px])data/) {
  549. $v.=" READONLY";
  550. $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
  551. } elsif ($line=~/\.CRT\$/i) {
  552. $v.=" READONLY ";
  553. $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
  554. }
  555. }
  556. $current_segment = $line;
  557. $self->{value} = $v;
  558. last;
  559. };
  560. /\.extern/ && do { $self->{value} = "EXTERN\t".$line;
  561. $self->{value} .= ":NEAR" if ($masm);
  562. last;
  563. };
  564. /\.globl|.global/
  565. && do { $self->{value} = $masm?"PUBLIC":"global";
  566. $self->{value} .= "\t".$line;
  567. last;
  568. };
  569. /\.size/ && do { if (defined($current_function)) {
  570. undef $self->{value};
  571. if ($current_function->{abi} eq "svr4") {
  572. $self->{value}="${decor}SEH_end_$current_function->{name}:";
  573. $self->{value}.=":\n" if($masm);
  574. }
  575. $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
  576. undef $current_function;
  577. }
  578. last;
  579. };
  580. /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
  581. /\.(value|long|rva|quad)/
  582. && do { my $sz = substr($1,0,1);
  583. my @arr = split(/,\s*/,$line);
  584. my $last = pop(@arr);
  585. my $conv = sub { my $var=shift;
  586. $var=~s/^(0b[0-1]+)/oct($1)/eig;
  587. $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
  588. if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
  589. { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
  590. $var;
  591. };
  592. $sz =~ tr/bvlrq/BWDDQ/;
  593. $self->{value} = "\tD$sz\t";
  594. for (@arr) { $self->{value} .= &$conv($_).","; }
  595. $self->{value} .= &$conv($last);
  596. last;
  597. };
  598. /\.byte/ && do { my @str=split(/,\s*/,$line);
  599. map(s/(0b[0-1]+)/oct($1)/eig,@str);
  600. map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
  601. while ($#str>15) {
  602. $self->{value}.="DB\t"
  603. .join(",",@str[0..15])."\n";
  604. foreach (0..15) { shift @str; }
  605. }
  606. $self->{value}.="DB\t"
  607. .join(",",@str) if (@str);
  608. last;
  609. };
  610. /\.comm/ && do { my @str=split(/,\s*/,$line);
  611. my $v=undef;
  612. if ($nasm) {
  613. $v.="common $prefix@str[0] @str[1]";
  614. } else {
  615. $v="$current_segment\tENDS\n" if ($current_segment);
  616. $current_segment = "_DATA";
  617. $v.="$current_segment\tSEGMENT\n";
  618. $v.="COMM @str[0]:DWORD:".@str[1]/4;
  619. }
  620. $self->{value} = $v;
  621. last;
  622. };
  623. }
  624. $line = "";
  625. }
  626. $ret;
  627. }
  628. sub out {
  629. my $self = shift;
  630. $self->{value};
  631. }
  632. }
  633. sub rex {
  634. local *opcode=shift;
  635. my ($dst,$src,$rex)=@_;
  636. $rex|=0x04 if($dst>=8);
  637. $rex|=0x01 if($src>=8);
  638. push @opcode,($rex|0x40) if ($rex);
  639. }
  640. # older gas and ml64 don't handle SSE>2 instructions
  641. my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
  642. "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
  643. my $movq = sub { # elderly gas can't handle inter-register movq
  644. my $arg = shift;
  645. my @opcode=(0x66);
  646. if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
  647. my ($src,$dst)=($1,$2);
  648. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  649. rex(\@opcode,$src,$dst,0x8);
  650. push @opcode,0x0f,0x7e;
  651. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  652. @opcode;
  653. } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
  654. my ($src,$dst)=($2,$1);
  655. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  656. rex(\@opcode,$src,$dst,0x8);
  657. push @opcode,0x0f,0x6e;
  658. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  659. @opcode;
  660. } else {
  661. ();
  662. }
  663. };
  664. my $pextrd = sub {
  665. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
  666. my @opcode=(0x66);
  667. $imm=$1;
  668. $src=$2;
  669. $dst=$3;
  670. if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
  671. elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
  672. rex(\@opcode,$src,$dst);
  673. push @opcode,0x0f,0x3a,0x16;
  674. push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
  675. push @opcode,$imm;
  676. @opcode;
  677. } else {
  678. ();
  679. }
  680. };
  681. my $pinsrd = sub {
  682. if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
  683. my @opcode=(0x66);
  684. $imm=$1;
  685. $src=$2;
  686. $dst=$3;
  687. if ($src =~ /%r([0-9]+)/) { $src = $1; }
  688. elsif ($src =~ /%e/) { $src = $regrm{$src}; }
  689. rex(\@opcode,$dst,$src);
  690. push @opcode,0x0f,0x3a,0x22;
  691. push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
  692. push @opcode,$imm;
  693. @opcode;
  694. } else {
  695. ();
  696. }
  697. };
  698. my $pshufb = sub {
  699. if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  700. my @opcode=(0x66);
  701. rex(\@opcode,$2,$1);
  702. push @opcode,0x0f,0x38,0x00;
  703. push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
  704. @opcode;
  705. } else {
  706. ();
  707. }
  708. };
  709. my $palignr = sub {
  710. if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  711. my @opcode=(0x66);
  712. rex(\@opcode,$3,$2);
  713. push @opcode,0x0f,0x3a,0x0f;
  714. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  715. push @opcode,$1;
  716. @opcode;
  717. } else {
  718. ();
  719. }
  720. };
  721. my $pclmulqdq = sub {
  722. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  723. my @opcode=(0x66);
  724. rex(\@opcode,$3,$2);
  725. push @opcode,0x0f,0x3a,0x44;
  726. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  727. my $c=$1;
  728. push @opcode,$c=~/^0/?oct($c):$c;
  729. @opcode;
  730. } else {
  731. ();
  732. }
  733. };
  734. my $rdrand = sub {
  735. if (shift =~ /%[er](\w+)/) {
  736. my @opcode=();
  737. my $dst=$1;
  738. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  739. rex(\@opcode,0,$1,8);
  740. push @opcode,0x0f,0xc7,0xf0|($dst&7);
  741. @opcode;
  742. } else {
  743. ();
  744. }
  745. };
  746. my $rdseed = sub {
  747. if (shift =~ /%[er](\w+)/) {
  748. my @opcode=();
  749. my $dst=$1;
  750. if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
  751. rex(\@opcode,0,$1,8);
  752. push @opcode,0x0f,0xc7,0xf8|($dst&7);
  753. @opcode;
  754. } else {
  755. ();
  756. }
  757. };
  758. sub rxb {
  759. local *opcode=shift;
  760. my ($dst,$src1,$src2,$rxb)=@_;
  761. $rxb|=0x7<<5;
  762. $rxb&=~(0x04<<5) if($dst>=8);
  763. $rxb&=~(0x01<<5) if($src1>=8);
  764. $rxb&=~(0x02<<5) if($src2>=8);
  765. push @opcode,$rxb;
  766. }
  767. my $vprotd = sub {
  768. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  769. my @opcode=(0x8f);
  770. rxb(\@opcode,$3,$2,-1,0x08);
  771. push @opcode,0x78,0xc2;
  772. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  773. my $c=$1;
  774. push @opcode,$c=~/^0/?oct($c):$c;
  775. @opcode;
  776. } else {
  777. ();
  778. }
  779. };
  780. my $vprotq = sub {
  781. if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
  782. my @opcode=(0x8f);
  783. rxb(\@opcode,$3,$2,-1,0x08);
  784. push @opcode,0x78,0xc3;
  785. push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
  786. my $c=$1;
  787. push @opcode,$c=~/^0/?oct($c):$c;
  788. @opcode;
  789. } else {
  790. ();
  791. }
  792. };
  793. if ($nasm) {
  794. print <<___;
  795. default rel
  796. %define XMMWORD
  797. %define YMMWORD
  798. %define ZMMWORD
  799. ___
  800. } elsif ($masm) {
  801. print <<___;
  802. OPTION DOTNAME
  803. ___
  804. }
  805. print STDOUT "#if defined(__x86_64__)\n" if ($gas);
  806. while($line=<>) {
  807. chomp($line);
  808. $line =~ s|[#!].*$||; # get rid of asm-style comments...
  809. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  810. $line =~ s|^\s+||; # ... and skip white spaces in beginning
  811. $line =~ s|\s+$||; # ... and at the end
  812. undef $label;
  813. undef $opcode;
  814. undef @args;
  815. if ($label=label->re(\$line)) { print $label->out(); }
  816. if (directive->re(\$line)) {
  817. printf "%s",directive->out();
  818. } elsif ($opcode=opcode->re(\$line)) {
  819. my $asm = eval("\$".$opcode->mnemonic());
  820. undef @bytes;
  821. if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) {
  822. print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
  823. next;
  824. }
  825. ARGUMENT: while (1) {
  826. my $arg;
  827. if ($arg=register->re(\$line)) { opcode->size($arg->size()); }
  828. elsif ($arg=const->re(\$line)) { }
  829. elsif ($arg=ea->re(\$line)) { }
  830. elsif ($arg=expr->re(\$line)) { }
  831. else { last ARGUMENT; }
  832. push @args,$arg;
  833. last ARGUMENT if ($line !~ /^,/);
  834. $line =~ s/^,\s*//;
  835. } # ARGUMENT:
  836. if ($#args>=0) {
  837. my $insn;
  838. my $sz=opcode->size();
  839. if ($gas) {
  840. $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
  841. @args = map($_->out($sz),@args);
  842. printf "\t%s\t%s",$insn,join(",",@args);
  843. } else {
  844. $insn = $opcode->out();
  845. foreach (@args) {
  846. my $arg = $_->out();
  847. # $insn.=$sz compensates for movq, pinsrw, ...
  848. if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
  849. if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
  850. if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
  851. if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
  852. }
  853. @args = reverse(@args);
  854. undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
  855. if ($insn eq "movq" && $#args == 1 && $args[0]->out($sz) eq "xmm0" && $args[1]->out($sz) eq "rax") {
  856. # I have no clue why MASM can't parse this instruction.
  857. printf "DB 66h, 48h, 0fh, 6eh, 0c0h";
  858. } else {
  859. printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
  860. }
  861. }
  862. } else {
  863. printf "\t%s",$opcode->out();
  864. }
  865. }
  866. print $line,"\n";
  867. }
  868. print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
  869. print "END\n" if ($masm);
  870. print "#endif\n" if ($gas);
  871. close STDOUT;
  872. #################################################
  873. # Cross-reference x86_64 ABI "card"
  874. #
  875. # Unix Win64
  876. # %rax * *
  877. # %rbx - -
  878. # %rcx #4 #1
  879. # %rdx #3 #2
  880. # %rsi #2 -
  881. # %rdi #1 -
  882. # %rbp - -
  883. # %rsp - -
  884. # %r8 #5 #3
  885. # %r9 #6 #4
  886. # %r10 * *
  887. # %r11 * *
  888. # %r12 - -
  889. # %r13 - -
  890. # %r14 - -
  891. # %r15 - -
  892. #
  893. # (*) volatile register
  894. # (-) preserved by callee
  895. # (#) Nth argument, volatile
  896. #
  897. # In Unix terms top of stack is argument transfer area for arguments
  898. # which could not be accomodated in registers. Or in other words 7th
  899. # [integer] argument resides at 8(%rsp) upon function entry point.
  900. # 128 bytes above %rsp constitute a "red zone" which is not touched
  901. # by signal handlers and can be used as temporal storage without
  902. # allocating a frame.
  903. #
  904. # In Win64 terms N*8 bytes on top of stack is argument transfer area,
  905. # which belongs to/can be overwritten by callee. N is the number of
  906. # arguments passed to callee, *but* not less than 4! This means that
  907. # upon function entry point 5th argument resides at 40(%rsp), as well
  908. # as that 32 bytes from 8(%rsp) can always be used as temporal
  909. # storage [without allocating a frame]. One can actually argue that
  910. # one can assume a "red zone" above stack pointer under Win64 as well.
  911. # Point is that at apparently no occasion Windows kernel would alter
  912. # the area above user stack pointer in true asynchronous manner...
  913. #
  914. # All the above means that if assembler programmer adheres to Unix
  915. # register and stack layout, but disregards the "red zone" existense,
  916. # it's possible to use following prologue and epilogue to "gear" from
  917. # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
  918. #
  919. # omnipotent_function:
  920. # ifdef WIN64
  921. # movq %rdi,8(%rsp)
  922. # movq %rsi,16(%rsp)
  923. # movq %rcx,%rdi ; if 1st argument is actually present
  924. # movq %rdx,%rsi ; if 2nd argument is actually ...
  925. # movq %r8,%rdx ; if 3rd argument is ...
  926. # movq %r9,%rcx ; if 4th argument ...
  927. # movq 40(%rsp),%r8 ; if 5th ...
  928. # movq 48(%rsp),%r9 ; if 6th ...
  929. # endif
  930. # ...
  931. # ifdef WIN64
  932. # movq 8(%rsp),%rdi
  933. # movq 16(%rsp),%rsi
  934. # endif
  935. # ret
  936. #
  937. #################################################
  938. # Win64 SEH, Structured Exception Handling.
  939. #
  940. # Unlike on Unix systems(*) lack of Win64 stack unwinding information
  941. # has undesired side-effect at run-time: if an exception is raised in
  942. # assembler subroutine such as those in question (basically we're
  943. # referring to segmentation violations caused by malformed input
  944. # parameters), the application is briskly terminated without invoking
  945. # any exception handlers, most notably without generating memory dump
  946. # or any user notification whatsoever. This poses a problem. It's
  947. # possible to address it by registering custom language-specific
  948. # handler that would restore processor context to the state at
  949. # subroutine entry point and return "exception is not handled, keep
  950. # unwinding" code. Writing such handler can be a challenge... But it's
  951. # doable, though requires certain coding convention. Consider following
  952. # snippet:
  953. #
  954. # .type function,@function
  955. # function:
  956. # movq %rsp,%rax # copy rsp to volatile register
  957. # pushq %r15 # save non-volatile registers
  958. # pushq %rbx
  959. # pushq %rbp
  960. # movq %rsp,%r11
  961. # subq %rdi,%r11 # prepare [variable] stack frame
  962. # andq $-64,%r11
  963. # movq %rax,0(%r11) # check for exceptions
  964. # movq %r11,%rsp # allocate [variable] stack frame
  965. # movq %rax,0(%rsp) # save original rsp value
  966. # magic_point:
  967. # ...
  968. # movq 0(%rsp),%rcx # pull original rsp value
  969. # movq -24(%rcx),%rbp # restore non-volatile registers
  970. # movq -16(%rcx),%rbx
  971. # movq -8(%rcx),%r15
  972. # movq %rcx,%rsp # restore original rsp
  973. # ret
  974. # .size function,.-function
  975. #
  976. # The key is that up to magic_point copy of original rsp value remains
  977. # in chosen volatile register and no non-volatile register, except for
  978. # rsp, is modified. While past magic_point rsp remains constant till
  979. # the very end of the function. In this case custom language-specific
  980. # exception handler would look like this:
  981. #
  982. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  983. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  984. # { ULONG64 *rsp = (ULONG64 *)context->Rax;
  985. # if (context->Rip >= magic_point)
  986. # { rsp = ((ULONG64 **)context->Rsp)[0];
  987. # context->Rbp = rsp[-3];
  988. # context->Rbx = rsp[-2];
  989. # context->R15 = rsp[-1];
  990. # }
  991. # context->Rsp = (ULONG64)rsp;
  992. # context->Rdi = rsp[1];
  993. # context->Rsi = rsp[2];
  994. #
  995. # memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
  996. # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
  997. # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
  998. # &disp->HandlerData,&disp->EstablisherFrame,NULL);
  999. # return ExceptionContinueSearch;
  1000. # }
  1001. #
  1002. # It's appropriate to implement this handler in assembler, directly in
  1003. # function's module. In order to do that one has to know members'
  1004. # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
  1005. # values. Here they are:
  1006. #
  1007. # CONTEXT.Rax 120
  1008. # CONTEXT.Rcx 128
  1009. # CONTEXT.Rdx 136
  1010. # CONTEXT.Rbx 144
  1011. # CONTEXT.Rsp 152
  1012. # CONTEXT.Rbp 160
  1013. # CONTEXT.Rsi 168
  1014. # CONTEXT.Rdi 176
  1015. # CONTEXT.R8 184
  1016. # CONTEXT.R9 192
  1017. # CONTEXT.R10 200
  1018. # CONTEXT.R11 208
  1019. # CONTEXT.R12 216
  1020. # CONTEXT.R13 224
  1021. # CONTEXT.R14 232
  1022. # CONTEXT.R15 240
  1023. # CONTEXT.Rip 248
  1024. # CONTEXT.Xmm6 512
  1025. # sizeof(CONTEXT) 1232
  1026. # DISPATCHER_CONTEXT.ControlPc 0
  1027. # DISPATCHER_CONTEXT.ImageBase 8
  1028. # DISPATCHER_CONTEXT.FunctionEntry 16
  1029. # DISPATCHER_CONTEXT.EstablisherFrame 24
  1030. # DISPATCHER_CONTEXT.TargetIp 32
  1031. # DISPATCHER_CONTEXT.ContextRecord 40
  1032. # DISPATCHER_CONTEXT.LanguageHandler 48
  1033. # DISPATCHER_CONTEXT.HandlerData 56
  1034. # UNW_FLAG_NHANDLER 0
  1035. # ExceptionContinueSearch 1
  1036. #
  1037. # In order to tie the handler to the function one has to compose
  1038. # couple of structures: one for .xdata segment and one for .pdata.
  1039. #
  1040. # UNWIND_INFO structure for .xdata segment would be
  1041. #
  1042. # function_unwind_info:
  1043. # .byte 9,0,0,0
  1044. # .rva handler
  1045. #
  1046. # This structure designates exception handler for a function with
  1047. # zero-length prologue, no stack frame or frame register.
  1048. #
  1049. # To facilitate composing of .pdata structures, auto-generated "gear"
  1050. # prologue copies rsp value to rax and denotes next instruction with
  1051. # .LSEH_begin_{function_name} label. This essentially defines the SEH
  1052. # styling rule mentioned in the beginning. Position of this label is
  1053. # chosen in such manner that possible exceptions raised in the "gear"
  1054. # prologue would be accounted to caller and unwound from latter's frame.
  1055. # End of function is marked with respective .LSEH_end_{function_name}
  1056. # label. To summarize, .pdata segment would contain
  1057. #
  1058. # .rva .LSEH_begin_function
  1059. # .rva .LSEH_end_function
  1060. # .rva function_unwind_info
  1061. #
  1062. # Reference to functon_unwind_info from .xdata segment is the anchor.
  1063. # In case you wonder why references are 32-bit .rvas and not 64-bit
  1064. # .quads. References put into these two segments are required to be
  1065. # *relative* to the base address of the current binary module, a.k.a.
  1066. # image base. No Win64 module, be it .exe or .dll, can be larger than
  1067. # 2GB and thus such relative references can be and are accommodated in
  1068. # 32 bits.
  1069. #
  1070. # Having reviewed the example function code, one can argue that "movq
  1071. # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
  1072. # rax would contain an undefined value. If this "offends" you, use
  1073. # another register and refrain from modifying rax till magic_point is
  1074. # reached, i.e. as if it was a non-volatile register. If more registers
  1075. # are required prior [variable] frame setup is completed, note that
  1076. # nobody says that you can have only one "magic point." You can
  1077. # "liberate" non-volatile registers by denoting last stack off-load
  1078. # instruction and reflecting it in finer grade unwind logic in handler.
  1079. # After all, isn't it why it's called *language-specific* handler...
  1080. #
  1081. # Attentive reader can notice that exceptions would be mishandled in
  1082. # auto-generated "gear" epilogue. Well, exception effectively can't
  1083. # occur there, because if memory area used by it was subject to
  1084. # segmentation violation, then it would be raised upon call to the
  1085. # function (and as already mentioned be accounted to caller, which is
  1086. # not a problem). If you're still not comfortable, then define tail
  1087. # "magic point" just prior ret instruction and have handler treat it...
  1088. #
  1089. # (*) Note that we're talking about run-time, not debug-time. Lack of
  1090. # unwind information makes debugging hard on both Windows and
  1091. # Unix. "Unlike" referes to the fact that on Unix signal handler
  1092. # will always be invoked, core dumped and appropriate exit code
  1093. # returned to parent (for user notification).