📄 x86_64-xlate.pl
字号:
$self->{value}; } elsif ($current_function->{abi} eq "svr4") { my $func = "$current_function->{name} PROC\n". " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n". " mov QWORD PTR 16[rsp],rsi\n"; my $narg = $current_function->{narg}; $narg=6 if (!defined($narg)); $func .= " mov rdi,rcx\n" if ($narg>0); $func .= " mov rsi,rdx\n" if ($narg>1); $func .= " mov rdx,r8\n" if ($narg>2); $func .= " mov rcx,r9\n" if ($narg>3); $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4); $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5); $func .= "\n"; } else { "$current_function->{name} PROC"; } }}{ package expr; # pick up expressioins sub re { my $self = shift; # single instance is enough... local *line = shift; undef $ret; if ($line =~ /(^[^,]+)/) { $self->{value} = $1; $ret = $self; $line = substr($line,@+[0]); $line =~ s/^\s+//; $self->{value} =~ s/\.L/\$L/g if ($masm); } $ret; } sub out { my $self = shift; $self->{value}; }}{ package directive; # pick up directives, which start with . sub re { my $self = shift; # single instance is enough... local *line = shift; undef $ret; my $dir; my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); if ($line =~ /^\s*(\.\w+)/) { if (!$masm) { $self->{value} = $1; $line =~ s/\@abi\-omnipotent/\@function/; $line =~ s/\@function.*/\@function/; if ($line =~ /\.picmeup\s+(%r[\w]+)/i) { $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1}; } elsif ($line =~ /\.asciz\s+"(.*)"$/) { $self->{value} = ".byte\t".join(",",unpack("C*",$1),0); } elsif ($line =~ /\.extern/) { $self->{value} = ""; # swallow extern } else { $self->{value} = $line; } $line = ""; return $self; } $dir = $1; $ret = $self; undef $self->{value}; $line = substr($line,@+[0]); $line =~ s/^\s+//; SWITCH: for ($dir) { /\.(text)/ && do { my $v=undef; $v="$current_segment\tENDS\n" if ($current_segment); $current_segment = "_$1\$"; $current_segment =~ tr/[a-z]/[A-Z]/; $v.="$current_segment\tSEGMENT "; $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; $v.=" 'CODE'"; $self->{value} = $v; last; }; /\.extern/ && do { $self->{value} = "EXTRN\t".$line.":BYTE"; last; }; /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; }; /\.type/ && do { ($sym,$type,$narg) = split(',',$line); if ($type eq "\@function") { undef $current_function; $current_function->{name} = $sym; $current_function->{abi} = "svr4"; $current_function->{narg} = $narg; } elsif ($type eq "\@abi-omnipotent") { undef $current_function; $current_function->{name} = $sym; } last; }; /\.size/ && do { if (defined($current_function)) { $self->{value}="$current_function->{name}\tENDP"; undef $current_function; } last; }; /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; /\.(byte|value|long|quad)/ && do { my @arr = split(',',$line); my $sz = substr($1,0,1); my $last = pop(@arr); my $conv = sub { my $var=shift; if ($var=~s/0x([0-9a-f]+)/0$1h/i) { $var; } else { sprintf"0%Xh",$var; } }; $sz =~ tr/bvlq/BWDQ/; $self->{value} = "\tD$sz\t"; for (@arr) { $self->{value} .= &$conv($_).","; } $self->{value} .= &$conv($last); last; }; /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line}; last; }; /\.asciz/ && do { if ($line =~ /^"(.*)"$/) { my @str=unpack("C*",$1); push @str,0; while ($#str>15) { $self->{value}.="DB\t" .join(",",@str[0..15])."\n"; foreach (0..15) { shift @str; } } $self->{value}.="DB\t" .join(",",@str) if (@str); } last; }; } $line = ""; } $ret; } sub out { my $self = shift; $self->{value}; }}while($line=<>) { chomp($line); $line =~ s|[#!].*$||; # get rid of asm-style comments... $line =~ s|/\*.*\*/||; # ... and C-style comments... $line =~ s|^\s+||; # ... and skip white spaces in beginning undef $label; undef $opcode; undef $dst; undef $src; undef $sz; if ($label=label->re(\$line)) { print $label->out(); } if (directive->re(\$line)) { printf "%s",directive->out(); } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: { if ($src=register->re(\$line)) { opcode->size($src->size()); } elsif ($src=const->re(\$line)) { } elsif ($src=ea->re(\$line)) { } elsif ($src=expr->re(\$line)) { } last ARGUMENT if ($line !~ /^,/); $line = substr($line,1); $line =~ s/^\s+//; if ($dst=register->re(\$line)) { opcode->size($dst->size()); } elsif ($dst=const->re(\$line)) { } elsif ($dst=ea->re(\$line)) { } } # ARGUMENT: $sz=opcode->size(); if (defined($dst)) { if (!$masm) { printf "\t%s\t%s,%s", $opcode->out($dst->size()), $src->out($sz),$dst->out($sz); } else { printf "\t%s\t%s,%s", $opcode->out(), $dst->out($sz),$src->out($sz); } } elsif (defined($src)) { printf "\t%s\t%s",$opcode->out(),$src->out($sz); } else { printf "\t%s",$opcode->out(); } } print $line,"\n";}print "\n$current_segment\tENDS\nEND\n" if ($masm);close STDOUT;################################################## Cross-reference x86_64 ABI "card"## Unix Win64# %rax * *# %rbx - -# %rcx #4 #1# %rdx #3 #2# %rsi #2 -# %rdi #1 -# %rbp - -# %rsp - -# %r8 #5 #3# %r9 #6 #4# %r10 * *# %r11 * *# %r12 - -# %r13 - -# %r14 - -# %r15 - -# # (*) volatile register# (-) preserved by callee# (#) Nth argument, volatile## In Unix terms top of stack is argument transfer area for arguments# which could not be accomodated in registers. Or in other words 7th# [integer] argument resides at 8(%rsp) upon function entry point.# 128 bytes above %rsp constitute a "red zone" which is not touched# by signal handlers and can be used as temporal storage without# allocating a frame.## In Win64 terms N*8 bytes on top of stack is argument transfer area,# which belongs to/can be overwritten by callee. N is the number of# arguments passed to callee, *but* not less than 4! This means that# upon function entry point 5th argument resides at 40(%rsp), as well# as that 32 bytes from 8(%rsp) can always be used as temporal# storage [without allocating a frame]. One can actually argue that# one can assume a "red zone" above stack pointer under Win64 as well.# Point is that at apparently no occasion Windows kernel would alter# the area above user stack pointer in true asynchronous manner...## All the above means that if assembler programmer adheres to Unix# register and stack layout, but disregards the "red zone" existense,# it's possible to use following prologue and epilogue to "gear" from# Unix to Win64 ABI in leaf functions with not more than 6 arguments.## omnipotent_function:# ifdef WIN64# movq %rdi,8(%rsp)# movq %rsi,16(%rsp)# movq %rcx,%rdi ; if 1st argument is actually present# movq %rdx,%rsi ; if 2nd argument is actually ...# movq %r8,%rdx ; if 3rd argument is ...# movq %r9,%rcx ; if 4th argument ...# movq 40(%rsp),%r8 ; if 5th ...# movq 48(%rsp),%r9 ; if 6th ...# endif# ...# ifdef WIN64# movq 8(%rsp),%rdi# movq 16(%rsp),%rsi# endif# ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -