x86_64-xlate.pl 15 KB

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