📄 x86_64-xlate.pl
字号:
#!/usr/bin/env perl# Ascetic x86_64 AT&T to MASM assembler translator by <appro>.## Why AT&T to MASM and not vice versa? Several reasons. Because AT&T# format is way easier to parse. Because it's simpler to "gear" from# Unix ABI to Windows one [see cross-reference "card" at the end of# file]. Because Linux targets were available first...## In addition the script also "distills" code suitable for GNU# assembler, so that it can be compiled with more rigid assemblers,# such as Solaris /usr/ccs/bin/as.## This translator is not designed to convert *arbitrary* assembler# code from AT&T format to MASM one. It's designed to convert just# enough to provide for dual-ABI OpenSSL modules development...# There *are* limitations and you might have to modify your assembler# code or this script to achieve the desired result...## Currently recognized limitations:## - can't use multiple ops per line;# - indirect calls and jumps are not supported;## Dual-ABI styling rules.## 1. Adhere to Unix register and stack layout [see the end for# explanation].# 2. Forget about "red zone," stick to more traditional blended# stack frame allocation. If volatile storage is actually required# that is. If not, just leave the stack as is.# 3. Functions tagged with ".type name,@function" get crafted with# unified Win64 prologue and epilogue automatically. If you want# to take care of ABI differences yourself, tag functions as# ".type name,@abi-omnipotent" instead.# 4. To optimize the Win64 prologue you can specify number of input# arguments as ".type name,@function,N." Keep in mind that if N is# larger than 6, then you *have to* write "abi-omnipotent" code,# because >6 cases can't be addressed with unified prologue.# 5. Name local labels as .L*, do *not* use dynamic labels such as 1:# (sorry about latter).# 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is# required to identify the spots, where to inject Win64 epilogue!# But on the pros, it's then prefixed with rep automatically:-)# 7. Due to MASM limitations [and certain general counter-intuitivity# of ip-relative addressing] generation of position-independent# code is assisted by synthetic directive, .picmeup, which puts# address of the *next* instruction into target register.## Example 1:# .picmeup %rax# lea .Label-.(%rax),%rax# Example 2:# .picmeup %rcx# .Lpic_point:# ...# lea .Label-.Lpic_point(%rcx),%rbpmy $output = shift;{ my ($stddev,$stdino,@junk)=stat(STDOUT); my ($outdev,$outino,@junk)=stat($output); open STDOUT,">$output" || die "can't open $output: $!" if ($stddev!=$outdev || $stdino!=$outino);}my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005my $masm=$masmref if ($output =~ /\.asm/);if ($masm && `ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/){ $masm=$1 + $2*2**-16 + $4*2**-32; }my $current_segment;my $current_function;{ package opcode; # pick up opcodes sub re { my $self = shift; # single instance in enough... local *line = shift; undef $ret; if ($line =~ /^([a-z][a-z0-9]*)/i) { $self->{op} = $1; $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; undef $self->{sz}; if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... $self->{op} = $1; $self->{sz} = "b"; } elsif ($self->{op} =~ /call/) { $self->{sz} = "" } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { $self->{op} = $1; $self->{sz} = $2; } } $ret; } sub size { my $self = shift; my $sz = shift; $self->{sz} = $sz if (defined($sz) && !defined($self->{sz})); $self->{sz}; } sub out { my $self = shift; if (!$masm) { if ($self->{op} eq "movz") { # movz is pain... sprintf "%s%s%s",$self->{op},$self->{sz},shift; } elsif ($self->{op} =~ /^set/) { "$self->{op}"; } elsif ($self->{op} eq "ret") { ".byte 0xf3,0xc3"; } else { "$self->{op}$self->{sz}"; } } else { $self->{op} =~ s/^movz/movzx/; if ($self->{op} eq "ret") { $self->{op} = ""; if ($current_function->{abi} eq "svr4") { $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t". "mov rsi,QWORD PTR 16[rsp]\n\t"; } $self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; } $self->{op}; } }}{ package const; # pick up constants, which start with $ sub re { my $self = shift; # single instance in enough... local *line = shift; undef $ret; if ($line =~ /^\$([^,]+)/) { $self->{value} = $1; $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; } $ret; } sub out { my $self = shift; if (!$masm) { # Solaris /usr/ccs/bin/as can't handle multiplications # in $self->{value} $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; sprintf "\$%s",$self->{value}; } else { $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig; sprintf "%s",$self->{value}; } }}{ package ea; # pick up effective addresses: expr(%reg,%reg,scale) sub re { my $self = shift; # single instance in enough... local *line = shift; undef $ret; if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) { $self->{label} = $1; ($self->{base},$self->{index},$self->{scale})=split(/,/,$2); $self->{scale} = 1 if (!defined($self->{scale})); $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; $self->{base} =~ s/^%//; $self->{index} =~ s/^%// if (defined($self->{index})); } $ret; } sub size {} sub out { my $self = shift; my $sz = shift; # Silently convert all EAs to 64-bit. This is required for # elder GNU assembler and results in more compact code, # *but* most importantly AES module depends on this feature! $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; if (!$masm) { # Solaris /usr/ccs/bin/as can't handle multiplications # in $self->{label} $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; if (defined($self->{index})) { sprintf "%s(%%%s,%%%s,%d)", $self->{label},$self->{base}, $self->{index},$self->{scale}; } else { sprintf "%s(%%%s)", $self->{label},$self->{base}; } } else { %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" ); $self->{label} =~ s/\./\$/g; $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig; $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); if (defined($self->{index})) { sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz}, $self->{label}, $self->{index},$self->{scale}, $self->{base}; } elsif ($self->{base} eq "rip") { sprintf "%s PTR %s",$szmap{$sz},$self->{label}; } else { sprintf "%s PTR %s[%s]",$szmap{$sz}, $self->{label},$self->{base}; } } }}{ package register; # pick up registers, which start with %. sub re { my $class = shift; # muliple instances... my $self = {}; local *line = shift; undef $ret; if ($line =~ /^%(\w+)/) { bless $self,$class; $self->{value} = $1; $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; } $ret; } sub size { my $self = shift; undef $ret; if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } $ret; } sub out { my $self = shift; sprintf $masm?"%s":"%%%s",$self->{value}; }}{ package label; # pick up labels, which end with : sub re { my $self = shift; # single instance is enough... local *line = shift; undef $ret; if ($line =~ /(^[\.\w]+\:)/) { $self->{value} = $1; $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; $self->{value} =~ s/\.L/\$L/ if ($masm); } $ret; } sub out { my $self = shift; if (!$masm) { $self->{value}; } elsif ($self->{value} ne "$current_function->{name}:") {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -