x86_64-xlate.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #!/usr/bin/env perl
  2. # Ascetic x86_64 AT&T to MASM 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. # - indirect calls and jumps are not supported;
  23. #
  24. # Dual-ABI styling rules.
  25. #
  26. # 1. Adhere to Unix register and stack layout [see the end for
  27. # explanation].
  28. # 2. Forget about "red zone," stick to more traditional blended
  29. # stack frame allocation. If volatile storage is actually required
  30. # that is. If not, just leave the stack as is.
  31. # 3. Functions tagged with ".type name,@function" get crafted with
  32. # unified Win64 prologue and epilogue automatically. If you want
  33. # to take care of ABI differences yourself, tag functions as
  34. # ".type name,@abi-omnipotent" instead.
  35. # 4. To optimize the Win64 prologue you can specify number of input
  36. # arguments as ".type name,@function,N." Keep in mind that if N is
  37. # larger than 6, then you *have to* write "abi-omnipotent" code,
  38. # because >6 cases can't be addressed with unified prologue.
  39. # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
  40. # (sorry about latter).
  41. # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
  42. # required to identify the spots, where to inject Win64 epilogue!
  43. # But on the pros, it's then prefixed with rep automatically:-)
  44. # 7. Due to MASM limitations [and certain general counter-intuitivity
  45. # of ip-relative addressing] generation of position-independent
  46. # code is assisted by synthetic directive, .picmeup, which puts
  47. # address of the *next* instruction into target register.
  48. #
  49. # Example 1:
  50. # .picmeup %rax
  51. # lea .Label-.(%rax),%rax
  52. # Example 2:
  53. # .picmeup %rcx
  54. # .Lpic_point:
  55. # ...
  56. # lea .Label-.Lpic_point(%rcx),%rbp
  57. my $output = shift;
  58. { my ($stddev,$stdino,@junk)=stat(STDOUT);
  59. my ($outdev,$outino,@junk)=stat($output);
  60. open STDOUT,">$output" || die "can't open $output: $!"
  61. if ($stddev!=$outdev || $stdino!=$outino);
  62. }
  63. my $masm=1 if ($output =~ /\.asm/);
  64. my $current_segment;
  65. my $current_function;
  66. { package opcode; # pick up opcodes
  67. sub re {
  68. my $self = shift; # single instance in enough...
  69. local *line = shift;
  70. undef $ret;
  71. if ($line =~ /^([a-z][a-z0-9]*)/i) {
  72. $self->{op} = $1;
  73. $ret = $self;
  74. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  75. undef $self->{sz};
  76. if ($self->{op} =~ /(movz)b.*/) { # movz is pain...
  77. $self->{op} = $1;
  78. $self->{sz} = "b";
  79. } elsif ($self->{op} =~ /call/) {
  80. $self->{sz} = ""
  81. } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])/) {
  82. $self->{op} = $1;
  83. $self->{sz} = $2;
  84. }
  85. }
  86. $ret;
  87. }
  88. sub size {
  89. my $self = shift;
  90. my $sz = shift;
  91. $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
  92. $self->{sz};
  93. }
  94. sub out {
  95. my $self = shift;
  96. if (!$masm) {
  97. if ($self->{op} eq "movz") { # movz is pain...
  98. sprintf "%s%s%s",$self->{op},$self->{sz},shift;
  99. } elsif ($self->{op} =~ /^set/) {
  100. "$self->{op}";
  101. } elsif ($self->{op} eq "ret") {
  102. ".byte 0xf3,0xc3";
  103. } else {
  104. "$self->{op}$self->{sz}";
  105. }
  106. } else {
  107. $self->{op} =~ s/movz/movzx/;
  108. if ($self->{op} eq "ret") {
  109. $self->{op} = "";
  110. if ($current_function->{abi} eq "svr4") {
  111. $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
  112. "mov rsi,QWORD PTR 16[rsp]\n\t";
  113. }
  114. $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
  115. }
  116. $self->{op};
  117. }
  118. }
  119. }
  120. { package const; # pick up constants, which start with $
  121. sub re {
  122. my $self = shift; # single instance in enough...
  123. local *line = shift;
  124. undef $ret;
  125. if ($line =~ /^\$([^,]+)/) {
  126. $self->{value} = $1;
  127. $ret = $self;
  128. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  129. }
  130. $ret;
  131. }
  132. sub out {
  133. my $self = shift;
  134. if (!$masm) {
  135. # Solaris /usr/ccs/bin/as can't handle multiplications
  136. # in $self->{value}
  137. $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
  138. $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
  139. sprintf "\$%s",$self->{value};
  140. } else {
  141. $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig;
  142. sprintf "%s",$self->{value};
  143. }
  144. }
  145. }
  146. { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
  147. sub re {
  148. my $self = shift; # single instance in enough...
  149. local *line = shift;
  150. undef $ret;
  151. if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) {
  152. $self->{label} = $1;
  153. ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
  154. $self->{scale} = 1 if (!defined($self->{scale}));
  155. $ret = $self;
  156. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  157. $self->{base} =~ s/^%//;
  158. $self->{index} =~ s/^%// if (defined($self->{index}));
  159. }
  160. $ret;
  161. }
  162. sub size {}
  163. sub out {
  164. my $self = shift;
  165. my $sz = shift;
  166. # Silently convert all EAs to 64-bit. This is required for
  167. # elder GNU assembler and results in more compact code,
  168. # *but* most importantly AES module depends on this feature!
  169. $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  170. $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
  171. if (!$masm) {
  172. # Solaris /usr/ccs/bin/as can't handle multiplications
  173. # in $self->{label}
  174. $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
  175. $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
  176. if (defined($self->{index})) {
  177. sprintf "%s(%%%s,%%%s,%d)",
  178. $self->{label},$self->{base},
  179. $self->{index},$self->{scale};
  180. } else {
  181. sprintf "%s(%%%s)", $self->{label},$self->{base};
  182. }
  183. } else {
  184. %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
  185. $self->{label} =~ s/\./\$/g;
  186. $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig;
  187. $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
  188. if (defined($self->{index})) {
  189. sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
  190. $self->{label},
  191. $self->{index},$self->{scale},
  192. $self->{base};
  193. } elsif ($self->{base} eq "rip") {
  194. sprintf "%s PTR %s",$szmap{$sz},$self->{label};
  195. } else {
  196. sprintf "%s PTR %s[%s]",$szmap{$sz},
  197. $self->{label},$self->{base};
  198. }
  199. }
  200. }
  201. }
  202. { package register; # pick up registers, which start with %.
  203. sub re {
  204. my $class = shift; # muliple instances...
  205. my $self = {};
  206. local *line = shift;
  207. undef $ret;
  208. if ($line =~ /^%(\w+)/) {
  209. bless $self,$class;
  210. $self->{value} = $1;
  211. $ret = $self;
  212. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  213. }
  214. $ret;
  215. }
  216. sub size {
  217. my $self = shift;
  218. undef $ret;
  219. if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
  220. elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
  221. elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
  222. elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
  223. elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
  224. elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
  225. elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
  226. elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
  227. $ret;
  228. }
  229. sub out {
  230. my $self = shift;
  231. sprintf $masm?"%s":"%%%s",$self->{value};
  232. }
  233. }
  234. { package label; # pick up labels, which end with :
  235. sub re {
  236. my $self = shift; # single instance is enough...
  237. local *line = shift;
  238. undef $ret;
  239. if ($line =~ /(^[\.\w]+\:)/) {
  240. $self->{value} = $1;
  241. $ret = $self;
  242. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  243. $self->{value} =~ s/\.L/\$L/ if ($masm);
  244. }
  245. $ret;
  246. }
  247. sub out {
  248. my $self = shift;
  249. if (!$masm) {
  250. $self->{value};
  251. } elsif ($self->{value} ne "$current_function->{name}:") {
  252. $self->{value};
  253. } elsif ($current_function->{abi} eq "svr4") {
  254. my $func = "$current_function->{name} PROC\n".
  255. " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
  256. " mov QWORD PTR 16[rsp],rsi\n";
  257. my $narg = $current_function->{narg};
  258. $narg=6 if (!defined($narg));
  259. $func .= " mov rdi,rcx\n" if ($narg>0);
  260. $func .= " mov rsi,rdx\n" if ($narg>1);
  261. $func .= " mov rdx,r8\n" if ($narg>2);
  262. $func .= " mov rcx,r9\n" if ($narg>3);
  263. $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
  264. $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
  265. $func .= "\n";
  266. } else {
  267. "$current_function->{name} PROC";
  268. }
  269. }
  270. }
  271. { package expr; # pick up expressioins
  272. sub re {
  273. my $self = shift; # single instance is enough...
  274. local *line = shift;
  275. undef $ret;
  276. if ($line =~ /(^[^,]+)/) {
  277. $self->{value} = $1;
  278. $ret = $self;
  279. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  280. $self->{value} =~ s/\.L/\$L/g if ($masm);
  281. }
  282. $ret;
  283. }
  284. sub out {
  285. my $self = shift;
  286. $self->{value};
  287. }
  288. }
  289. { package directive; # pick up directives, which start with .
  290. sub re {
  291. my $self = shift; # single instance is enough...
  292. local *line = shift;
  293. undef $ret;
  294. my $dir;
  295. my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
  296. ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
  297. "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
  298. "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
  299. "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
  300. "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
  301. "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
  302. "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
  303. "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
  304. if ($line =~ /^\s*(\.\w+)/) {
  305. if (!$masm) {
  306. $self->{value} = $1;
  307. $line =~ s/\@abi\-omnipotent/\@function/;
  308. $line =~ s/\@function.*/\@function/;
  309. if ($line =~ /\.picmeup\s+(%r[\w]+)/i) {
  310. $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1};
  311. } elsif ($line =~ /\.asciz\s+"(.*)"$/) {
  312. $self->{value} = ".byte\t".join(",",unpack("C*",$1),0);
  313. } elsif ($line =~ /\.extern/) {
  314. $self->{value} = ""; # swallow extern
  315. } else {
  316. $self->{value} = $line;
  317. }
  318. $line = "";
  319. return $self;
  320. }
  321. $dir = $1;
  322. $ret = $self;
  323. undef $self->{value};
  324. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  325. SWITCH: for ($dir) {
  326. /\.(text)/
  327. && do { my $v=undef;
  328. $v="$current_segment\tENDS\n" if ($current_segment);
  329. $current_segment = "_$1\$";
  330. $current_segment =~ tr/[a-z]/[A-Z]/;
  331. $v.="$current_segment\tSEGMENT ALIGN(64) 'CODE'";
  332. $self->{value} = $v;
  333. last;
  334. };
  335. /\.extern/ && do { $self->{value} = "EXTRN\t".$line.":BYTE"; last; };
  336. /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
  337. /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
  338. if ($type eq "\@function") {
  339. undef $current_function;
  340. $current_function->{name} = $sym;
  341. $current_function->{abi} = "svr4";
  342. $current_function->{narg} = $narg;
  343. } elsif ($type eq "\@abi-omnipotent") {
  344. undef $current_function;
  345. $current_function->{name} = $sym;
  346. }
  347. last;
  348. };
  349. /\.size/ && do { if (defined($current_function)) {
  350. $self->{value}="$current_function->{name}\tENDP";
  351. undef $current_function;
  352. }
  353. last;
  354. };
  355. /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
  356. /\.(byte|value|long|quad)/
  357. && do { my @arr = split(',',$line);
  358. my $sz = substr($1,0,1);
  359. my $last = pop(@arr);
  360. $sz =~ tr/bvlq/BWDQ/;
  361. $self->{value} = "\tD$sz\t";
  362. for (@arr) { $self->{value} .= sprintf"0%Xh,",oct; }
  363. $self->{value} .= sprintf"0%Xh",oct($last);
  364. last;
  365. };
  366. /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line};
  367. last;
  368. };
  369. /\.asciz/ && do { if ($line =~ /^"(.*)"$/) {
  370. my @str=unpack("C*",$1);
  371. push @str,0;
  372. while ($#str>15) {
  373. $self->{value}.="DB\t"
  374. .join(",",@str[0..15])."\n";
  375. foreach (0..15) { shift @str; }
  376. }
  377. $self->{value}.="DB\t"
  378. .join(",",@str) if (@str);
  379. }
  380. last;
  381. };
  382. }
  383. $line = "";
  384. }
  385. $ret;
  386. }
  387. sub out {
  388. my $self = shift;
  389. $self->{value};
  390. }
  391. }
  392. while($line=<>) {
  393. chomp($line);
  394. $line =~ s|[#!].*$||; # get rid of asm-style comments...
  395. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  396. $line =~ s|^\s+||; # ... and skip white spaces in beginning
  397. undef $label;
  398. undef $opcode;
  399. undef $dst;
  400. undef $src;
  401. undef $sz;
  402. if ($label=label->re(\$line)) { print $label->out(); }
  403. if (directive->re(\$line)) {
  404. printf "%s",directive->out();
  405. } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
  406. if ($src=register->re(\$line)) { opcode->size($src->size()); }
  407. elsif ($src=const->re(\$line)) { }
  408. elsif ($src=ea->re(\$line)) { }
  409. elsif ($src=expr->re(\$line)) { }
  410. last ARGUMENT if ($line !~ /^,/);
  411. $line = substr($line,1); $line =~ s/^\s+//;
  412. if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
  413. elsif ($dst=const->re(\$line)) { }
  414. elsif ($dst=ea->re(\$line)) { }
  415. } # ARGUMENT:
  416. $sz=opcode->size();
  417. if (defined($dst)) {
  418. if (!$masm) {
  419. printf "\t%s\t%s,%s", $opcode->out($dst->size()),
  420. $src->out($sz),$dst->out($sz);
  421. } else {
  422. printf "\t%s\t%s,%s", $opcode->out(),
  423. $dst->out($sz),$src->out($sz);
  424. }
  425. } elsif (defined($src)) {
  426. printf "\t%s\t%s",$opcode->out(),$src->out($sz);
  427. } else {
  428. printf "\t%s",$opcode->out();
  429. }
  430. }
  431. print $line,"\n";
  432. }
  433. print "\n$current_segment\tENDS\nEND\n" if ($masm);
  434. close STDOUT;
  435. #################################################
  436. # Cross-reference x86_64 ABI "card"
  437. #
  438. # Unix Win64
  439. # %rax * *
  440. # %rbx - -
  441. # %rcx #4 #1
  442. # %rdx #3 #2
  443. # %rsi #2 -
  444. # %rdi #1 -
  445. # %rbp - -
  446. # %rsp - -
  447. # %r8 #5 #3
  448. # %r9 #6 #4
  449. # %r10 * *
  450. # %r11 * *
  451. # %r12 - -
  452. # %r13 - -
  453. # %r14 - -
  454. # %r15 - -
  455. #
  456. # (*) volatile register
  457. # (-) preserved by callee
  458. # (#) Nth argument, volatile
  459. #
  460. # In Unix terms top of stack is argument transfer area for arguments
  461. # which could not be accomodated in registers. Or in other words 7th
  462. # [integer] argument resides at 8(%rsp) upon function entry point.
  463. # 128 bytes above %rsp constitute a "red zone" which is not touched
  464. # by signal handlers and can be used as temporal storage without
  465. # allocating a frame.
  466. #
  467. # In Win64 terms N*8 bytes on top of stack is argument transfer area,
  468. # which belongs to/can be overwritten by callee. N is the number of
  469. # arguments passed to callee, *but* not less than 4! This means that
  470. # upon function entry point 5th argument resides at 40(%rsp), as well
  471. # as that 32 bytes from 8(%rsp) can always be used as temporal
  472. # storage [without allocating a frame]. One can actually argue that
  473. # one can assume a "red zone" above stack pointer under Win64 as well.
  474. # Point is that at apparently no occasion Windows kernel would alter
  475. # the area above user stack pointer in true asynchronous manner...
  476. #
  477. # All the above means that if assembler programmer adheres to Unix
  478. # register and stack layout, but disregards the "red zone" existense,
  479. # it's possible to use following prologue and epilogue to "gear" from
  480. # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
  481. #
  482. # omnipotent_function:
  483. # ifdef WIN64
  484. # movq %rdi,8(%rsp)
  485. # movq %rsi,16(%rsp)
  486. # movq %rcx,%rdi ; if 1st argument is actually present
  487. # movq %rdx,%rsi ; if 2nd argument is actually ...
  488. # movq %r8,%rdx ; if 3rd argument is ...
  489. # movq %r9,%rcx ; if 4th argument ...
  490. # movq 40(%rsp),%r8 ; if 5th ...
  491. # movq 48(%rsp),%r9 ; if 6th ...
  492. # endif
  493. # ...
  494. # ifdef WIN64
  495. # movq 8(%rsp),%rdi
  496. # movq 16(%rsp),%rsi
  497. # endif
  498. # ret