x86_64-xlate.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. open STDOUT,">$output" || die "can't open $output: $!";
  59. my $masm=1 if ($output =~ /\.asm/);
  60. my $current_segment;
  61. my $current_function;
  62. { package opcode; # pick up opcodes
  63. sub re {
  64. my $self = shift; # single instance in enough...
  65. local *line = shift;
  66. undef $ret;
  67. if ($line =~ /^([a-z]+)/i) {
  68. $self->{op} = $1;
  69. $ret = $self;
  70. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  71. undef $self->{sz};
  72. if ($self->{op} =~ /(movz)b.*/) { # movz is pain...
  73. $self->{op} = $1;
  74. $self->{sz} = "b";
  75. } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])/) {
  76. $self->{op} = $1;
  77. $self->{sz} = $2;
  78. }
  79. }
  80. $ret;
  81. }
  82. sub size {
  83. my $self = shift;
  84. my $sz = shift;
  85. $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
  86. $self->{sz};
  87. }
  88. sub out {
  89. my $self = shift;
  90. if (!$masm) {
  91. if ($self->{op} eq "movz") { # movz in pain...
  92. sprintf "%s%s%s",$self->{op},$self->{sz},shift;
  93. } elsif ($self->{op} eq "ret") {
  94. ".byte 0xf3,0xc3";
  95. } else {
  96. "$self->{op}$self->{sz}";
  97. }
  98. } else {
  99. $self->{op} =~ s/movz/movzx/;
  100. if ($self->{op} eq "ret") {
  101. $self->{op} = "";
  102. if ($current_function->{abi} eq "svr4") {
  103. $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
  104. "mov rsi,QWORD PTR 16[rsp]\n\t";
  105. }
  106. $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
  107. }
  108. $self->{op};
  109. }
  110. }
  111. }
  112. { package const; # pick up constants, which start with $
  113. sub re {
  114. my $self = shift; # single instance in enough...
  115. local *line = shift;
  116. undef $ret;
  117. if ($line =~ /^\$([^,]+)/) {
  118. $self->{value} = $1;
  119. $ret = $self;
  120. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  121. }
  122. $ret;
  123. }
  124. sub out {
  125. my $self = shift;
  126. if (!$masm) {
  127. sprintf "\$%s",$self->{value};
  128. } else {
  129. $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig;
  130. sprintf "%s",$self->{value};
  131. }
  132. }
  133. }
  134. { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
  135. sub re {
  136. my $self = shift; # single instance in enough...
  137. local *line = shift;
  138. undef $ret;
  139. if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) {
  140. $self->{label} = $1;
  141. ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
  142. $self->{scale} = 1 if (!defined($self->{scale}));
  143. $ret = $self;
  144. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  145. $self->{base} =~ s/^%//;
  146. $self->{index} =~ s/^%// if (defined($self->{index}));
  147. }
  148. $ret;
  149. }
  150. sub size {}
  151. sub out {
  152. my $self = shift;
  153. my $sz = shift;
  154. if (!$masm) {
  155. # elder GNU assembler insists on 64-bit EAs:-(
  156. # on pros side, this results in more compact code:-)
  157. $self->{index} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
  158. $self->{base} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
  159. # Solaris /usr/ccs/bin/as can't handle multiplications
  160. # in $self->{label}
  161. $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/eg;
  162. $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
  163. if (defined($self->{index})) {
  164. sprintf "%s(%%%s,%%%s,%d)",
  165. $self->{label},$self->{base},
  166. $self->{index},$self->{scale};
  167. } else {
  168. sprintf "%s(%%%s)", $self->{label},$self->{base};
  169. }
  170. } else {
  171. %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
  172. $self->{label} =~ s/\./\$/g;
  173. $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig;
  174. $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
  175. if (defined($self->{index})) {
  176. sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
  177. $self->{label},
  178. $self->{index},$self->{scale},
  179. $self->{base};
  180. } else {
  181. sprintf "%s PTR %s[%s]",$szmap{$sz},
  182. $self->{label},$self->{base};
  183. }
  184. }
  185. }
  186. }
  187. { package register; # pick up registers, which start with %.
  188. sub re {
  189. my $class = shift; # muliple instances...
  190. my $self = {};
  191. local *line = shift;
  192. undef $ret;
  193. if ($line =~ /^%(\w+)/) {
  194. bless $self,$class;
  195. $self->{value} = $1;
  196. $ret = $self;
  197. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  198. }
  199. $ret;
  200. }
  201. sub size {
  202. my $self = shift;
  203. undef $ret;
  204. if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
  205. elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
  206. elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
  207. elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
  208. elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
  209. elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
  210. elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
  211. elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
  212. $ret;
  213. }
  214. sub out {
  215. my $self = shift;
  216. sprintf $masm?"%s":"%%%s",$self->{value};
  217. }
  218. }
  219. { package label; # pick up labels, which end with :
  220. sub re {
  221. my $self = shift; # single instance is enough...
  222. local *line = shift;
  223. undef $ret;
  224. if ($line =~ /(^[\.\w]+\:)/) {
  225. $self->{value} = $1;
  226. $ret = $self;
  227. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  228. $self->{value} =~ s/\.L/\$L/ if ($masm);
  229. }
  230. $ret;
  231. }
  232. sub out {
  233. my $self = shift;
  234. if (!$masm) {
  235. $self->{value};
  236. } elsif ($self->{value} ne "$current_function->{name}:") {
  237. $self->{value};
  238. } elsif ($current_function->{abi} eq "svr4") {
  239. my $func = "$current_function->{name} PROC\n".
  240. " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
  241. " mov QWORD PTR 16[rsp],rsi\n";
  242. my $narg = $current_function->{narg};
  243. $narg=6 if (!defined($narg));
  244. $func .= " mov rdi,rcx\n" if ($narg>0);
  245. $func .= " mov rsi,rdx\n" if ($narg>1);
  246. $func .= " mov rdx,r8\n" if ($narg>2);
  247. $func .= " mov rcx,r9\n" if ($narg>3);
  248. $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
  249. $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
  250. $func .= "\n";
  251. } else {
  252. "$current_function->{name} PROC";
  253. }
  254. }
  255. }
  256. { package expr; # pick up expressioins
  257. sub re {
  258. my $self = shift; # single instance is enough...
  259. local *line = shift;
  260. undef $ret;
  261. if ($line =~ /(^[^,]+)/) {
  262. $self->{value} = $1;
  263. $ret = $self;
  264. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  265. $self->{value} =~ s/\.L/\$L/g if ($masm);
  266. }
  267. $ret;
  268. }
  269. sub out {
  270. my $self = shift;
  271. $self->{value};
  272. }
  273. }
  274. { package directive; # pick up directives, which start with .
  275. sub re {
  276. my $self = shift; # single instance is enough...
  277. local *line = shift;
  278. undef $ret;
  279. my $dir;
  280. my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
  281. ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
  282. "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
  283. "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
  284. "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
  285. "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
  286. "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
  287. "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
  288. "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
  289. if ($line =~ /^\s*(\.\w+)/) {
  290. if (!$masm) {
  291. $self->{value} = $1;
  292. $line =~ s/\@abi\-omnipotent/\@function/;
  293. $line =~ s/\@function.*/\@function/;
  294. if ($line =~ /\.picmeup\s+(%r[\w]+)/i) {
  295. $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1};
  296. } else {
  297. $self->{value} = $line;
  298. }
  299. $line = "";
  300. return $self;
  301. }
  302. $dir = $1;
  303. $ret = $self;
  304. undef $self->{value};
  305. $line = substr($line,@+[0]); $line =~ s/^\s+//;
  306. SWITCH: for ($dir) {
  307. /\.(text)/
  308. && do { my $v=undef;
  309. $v="$current_segment\tENDS\n" if ($current_segment);
  310. $current_segment = "_$1\$";
  311. $current_segment =~ tr/[a-z]/[A-Z]/;
  312. $v.="$current_segment\tSEGMENT ALIGN(64) 'CODE'";
  313. $self->{value} = $v;
  314. last;
  315. };
  316. /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
  317. /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
  318. if ($type eq "\@function") {
  319. undef $current_function;
  320. $current_function->{name} = $sym;
  321. $current_function->{abi} = "svr4";
  322. $current_function->{narg} = $narg;
  323. } elsif ($type eq "\@abi-omnipotent") {
  324. undef $current_function;
  325. $current_function->{name} = $sym;
  326. }
  327. last;
  328. };
  329. /\.size/ && do { if (defined($current_function)) {
  330. $self->{value}="$current_function->{name}\tENDP";
  331. undef $current_function;
  332. }
  333. last;
  334. };
  335. /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
  336. /\.(byte|value|long|quad)/
  337. && do { my @arr = split(',',$line);
  338. my $sz = substr($1,0,1);
  339. my $last = pop(@arr);
  340. $sz =~ tr/bvlq/BWDQ/;
  341. $self->{value} = "\tD$sz\t";
  342. for (@arr) { $self->{value} .= sprintf"0%Xh,",oct; }
  343. $self->{value} .= sprintf"0%Xh",oct($last);
  344. last;
  345. };
  346. /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line};
  347. last;
  348. };
  349. }
  350. $line = "";
  351. }
  352. $ret;
  353. }
  354. sub out {
  355. my $self = shift;
  356. $self->{value};
  357. }
  358. }
  359. while($line=<>) {
  360. chomp($line);
  361. $line =~ s|[#!].*$||; # get rid of asm-style comments...
  362. $line =~ s|/\*.*\*/||; # ... and C-style comments...
  363. $line =~ s|^\s+||; # ... and skip white spaces in beginning
  364. undef $label;
  365. undef $opcode;
  366. undef $dst;
  367. undef $src;
  368. undef $sz;
  369. if ($label=label->re(\$line)) { print $label->out(); }
  370. if (directive->re(\$line)) {
  371. printf "%s",directive->out();
  372. } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
  373. if ($src=register->re(\$line)) { opcode->size($src->size()); }
  374. elsif ($src=const->re(\$line)) { }
  375. elsif ($src=ea->re(\$line)) { }
  376. elsif ($src=expr->re(\$line)) { }
  377. last ARGUMENT if ($line !~ /^,/);
  378. $line = substr($line,1); $line =~ s/^\s+//;
  379. if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
  380. elsif ($dst=const->re(\$line)) { }
  381. elsif ($dst=ea->re(\$line)) { }
  382. } # ARGUMENT:
  383. $sz=opcode->size();
  384. if (defined($dst)) {
  385. if (!$masm) {
  386. printf "\t%s\t%s,%s", $opcode->out($dst->size()),
  387. $src->out($sz),$dst->out($sz);
  388. } else {
  389. printf "\t%s\t%s,%s", $opcode->out(),
  390. $dst->out($sz),$src->out($sz);
  391. }
  392. } elsif (defined($src)) {
  393. printf "\t%s\t%s",$opcode->out(),$src->out($sz);
  394. } else {
  395. printf "\t%s",$opcode->out();
  396. }
  397. }
  398. print $line,"\n";
  399. }
  400. print "\n$current_segment\tENDS\nEND\n" if ($masm);
  401. close STDOUT;
  402. #################################################
  403. # Cross-reference x86_64 ABI "card"
  404. #
  405. # Unix Win64
  406. # %rax * *
  407. # %rbx - -
  408. # %rcx #4 #1
  409. # %rdx #3 #2
  410. # %rsi #2 -
  411. # %rdi #1 -
  412. # %rbp - -
  413. # %rsp - -
  414. # %r8 #5 #3
  415. # %r9 #6 #4
  416. # %r10 * *
  417. # %r11 * *
  418. # %r12 - -
  419. # %r13 - -
  420. # %r14 - -
  421. # %r15 - -
  422. #
  423. # (*) volatile register
  424. # (-) preserved by callee
  425. # (#) Nth argument, volatile
  426. #
  427. # In Unix terms top of stack is argument transfer area for arguments
  428. # which could not be accomodated in registers. Or in other words 7th
  429. # [integer] argument resides at 8(%rsp) upon function entry point.
  430. # 128 bytes above %rsp constitute a "red zone" which is not touched
  431. # by signal handlers and can be used as temporal storage without
  432. # allocating a frame.
  433. #
  434. # In Win64 terms N*8 bytes on top of stack is argument transfer area,
  435. # which belongs to/can be overwritten by callee. N is the number of
  436. # arguments passed to callee, *but* not less than 4! This means that
  437. # upon function entry point 5th argument resides at 40(%rsp), as well
  438. # as that 32 bytes from 8(%rsp) can always be used as temporal
  439. # storage [without allocating a frame].
  440. #
  441. # All the above means that if assembler programmer adheres to Unix
  442. # register and stack layout, but disregards the "red zone" existense,
  443. # it's possible to use following prologue and epilogue to "gear" from
  444. # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
  445. #
  446. # omnipotent_function:
  447. # ifdef WIN64
  448. # movq %rdi,8(%rsp)
  449. # movq %rsi,16(%rsp)
  450. # movq %rcx,%rdi ; if 1st argument is actually present
  451. # movq %rdx,%rsi ; if 2nd argument is actually ...
  452. # movq %r8,%rdx ; if 3rd argument is ...
  453. # movq %r9,%rcx ; if 4th argument ...
  454. # movq 40(%rsp),%r8 ; if 5th ...
  455. # movq 48(%rsp),%r9 ; if 6th ...
  456. # endif
  457. # ...
  458. # ifdef WIN64
  459. # movq 8(%rsp),%rdi
  460. # movq 16(%rsp),%rsi
  461. # endif
  462. # ret