| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>. |
| 4 |
# |
| 5 |
# Why AT&T to MASM and not vice versa? Several reasons. Because AT&T |
| 6 |
# format is way easier to parse. Because it's simpler to "gear" from |
| 7 |
# Unix ABI to Windows one [see cross-reference "card" at the end of |
| 8 |
# file]. Because Linux targets were available first... |
| 9 |
# |
| 10 |
# In addition the script also "distills" code suitable for GNU |
| 11 |
# assembler, so that it can be compiled with more rigid assemblers, |
| 12 |
# such as Solaris /usr/ccs/bin/as. |
| 13 |
# |
| 14 |
# This translator is not designed to convert *arbitrary* assembler |
| 15 |
# code from AT&T format to MASM one. It's designed to convert just |
| 16 |
# enough to provide for dual-ABI OpenSSL modules development... |
| 17 |
# There *are* limitations and you might have to modify your assembler |
| 18 |
# code or this script to achieve the desired result... |
| 19 |
# |
| 20 |
# Currently recognized limitations: |
| 21 |
# |
| 22 |
# - can't use multiple ops per line; |
| 23 |
# |
| 24 |
# Dual-ABI styling rules. |
| 25 |
# |
| 26 |
# 1. Adhere to Unix register and stack layout [see cross-reference |
| 27 |
# ABI "card" at the end for 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. Stick to explicit ip-relative addressing. If you have to use |
| 45 |
# GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??. |
| 46 |
# Both are recognized and translated to proper Win64 addressing |
| 47 |
# modes. To support legacy code a synthetic directive, .picmeup, |
| 48 |
# is implemented. It puts address of the *next* instruction into |
| 49 |
# target register, e.g.: |
| 50 |
# |
| 51 |
# .picmeup %rax |
| 52 |
# lea .Label-.(%rax),%rax |
| 53 |
# |
| 54 |
# 8. In order to provide for structured exception handling unified |
| 55 |
# Win64 prologue copies %rsp value to %rax. For further details |
| 56 |
# see SEH paragraph at the end. |
| 57 |
# 9. .init segment is allowed to contain calls to functions only. |
| 58 |
# a. If function accepts more than 4 arguments *and* >4th argument |
| 59 |
# is declared as non 64-bit value, do clear its upper part. |
| 60 |
|
| 61 |
my $flavour = shift; |
| 62 |
my $output = shift; |
| 63 |
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } |
| 64 |
|
| 65 |
open STDOUT,">$output" || die "can't open $output: $!" |
| 66 |
if (defined($output)); |
| 67 |
|
| 68 |
my $gas=1; $gas=0 if ($output =~ /\.asm$/); |
| 69 |
my $elf=1; $elf=0 if (!$gas); |
| 70 |
my $win64=0; |
| 71 |
my $prefix=""; |
| 72 |
my $decor=".L"; |
| 73 |
|
| 74 |
my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 |
| 75 |
my $masm=0; |
| 76 |
my $PTR=" PTR"; |
| 77 |
|
| 78 |
my $nasmref=2.03; |
| 79 |
my $nasm=0; |
| 80 |
|
| 81 |
if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; |
| 82 |
# TODO(davidben): Before supporting the |
| 83 |
# mingw64 perlasm flavour, do away with this |
| 84 |
# environment variable check. |
| 85 |
die "mingw64 not supported"; |
| 86 |
$prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`; |
| 87 |
chomp($prefix); |
| 88 |
} |
| 89 |
elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; } |
| 90 |
elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; } |
| 91 |
elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; } |
| 92 |
elsif (!$gas) { die "unknown flavour $flavour"; } |
| 93 |
|
| 94 |
my $current_segment; |
| 95 |
my $current_function; |
| 96 |
my %globals; |
| 97 |
|
| 98 |
{ package opcode; # pick up opcodes |
| 99 |
sub re { |
| 100 |
my $self = shift; # single instance in enough... |
| 101 |
local *line = shift; |
| 102 |
undef $ret; |
| 103 |
|
| 104 |
if ($line =~ /^([a-z][a-z0-9]*)/i) { |
| 105 |
$self->{op} = $1; |
| 106 |
$ret = $self; |
| 107 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 108 |
|
| 109 |
undef $self->{sz}; |
| 110 |
if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain... |
| 111 |
$self->{op} = $1; |
| 112 |
$self->{sz} = $2; |
| 113 |
} elsif ($self->{op} =~ /call|jmp/) { |
| 114 |
$self->{sz} = ""; |
| 115 |
} elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn |
| 116 |
$self->{sz} = ""; |
| 117 |
} elsif ($self->{op} =~ /^v/) { # VEX |
| 118 |
$self->{sz} = ""; |
| 119 |
} elsif ($self->{op} =~ /mov[dq]/ && $line =~ /%xmm/) { |
| 120 |
$self->{sz} = ""; |
| 121 |
} elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { |
| 122 |
$self->{op} = $1; |
| 123 |
$self->{sz} = $2; |
| 124 |
} |
| 125 |
} |
| 126 |
$ret; |
| 127 |
} |
| 128 |
sub size { |
| 129 |
my $self = shift; |
| 130 |
my $sz = shift; |
| 131 |
$self->{sz} = $sz if (defined($sz) && !defined($self->{sz})); |
| 132 |
$self->{sz}; |
| 133 |
} |
| 134 |
sub out { |
| 135 |
my $self = shift; |
| 136 |
if ($gas) { |
| 137 |
if ($self->{op} eq "movz") { # movz is pain... |
| 138 |
sprintf "%s%s%s",$self->{op},$self->{sz},shift; |
| 139 |
} elsif ($self->{op} =~ /^set/) { |
| 140 |
"$self->{op}"; |
| 141 |
} elsif ($self->{op} eq "ret") { |
| 142 |
my $epilogue = ""; |
| 143 |
if ($win64 && $current_function->{abi} eq "svr4") { |
| 144 |
$epilogue = "movq 8(%rsp),%rdi\n\t" . |
| 145 |
"movq 16(%rsp),%rsi\n\t"; |
| 146 |
} |
| 147 |
$epilogue . ".byte 0xf3,0xc3"; |
| 148 |
} elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") { |
| 149 |
".p2align\t3\n\t.quad"; |
| 150 |
} else { |
| 151 |
"$self->{op}$self->{sz}"; |
| 152 |
} |
| 153 |
} else { |
| 154 |
$self->{op} =~ s/^movz/movzx/; |
| 155 |
if ($self->{op} eq "ret") { |
| 156 |
$self->{op} = ""; |
| 157 |
if ($win64 && $current_function->{abi} eq "svr4") { |
| 158 |
$self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t". |
| 159 |
"mov rsi,QWORD${PTR}[16+rsp]\n\t"; |
| 160 |
} |
| 161 |
$self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; |
| 162 |
} elsif ($self->{op} =~ /^(pop|push)f/) { |
| 163 |
$self->{op} .= $self->{sz}; |
| 164 |
} elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") { |
| 165 |
$self->{op} = "\tDQ"; |
| 166 |
} |
| 167 |
$self->{op}; |
| 168 |
} |
| 169 |
} |
| 170 |
sub mnemonic { |
| 171 |
my $self=shift; |
| 172 |
my $op=shift; |
| 173 |
$self->{op}=$op if (defined($op)); |
| 174 |
$self->{op}; |
| 175 |
} |
| 176 |
} |
| 177 |
{ package const; # pick up constants, which start with $ |
| 178 |
sub re { |
| 179 |
my $self = shift; # single instance in enough... |
| 180 |
local *line = shift; |
| 181 |
undef $ret; |
| 182 |
|
| 183 |
if ($line =~ /^\$([^,]+)/) { |
| 184 |
$self->{value} = $1; |
| 185 |
$ret = $self; |
| 186 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 187 |
} |
| 188 |
$ret; |
| 189 |
} |
| 190 |
sub out { |
| 191 |
my $self = shift; |
| 192 |
|
| 193 |
$self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig; |
| 194 |
if ($gas) { |
| 195 |
# Solaris /usr/ccs/bin/as can't handle multiplications |
| 196 |
# in $self->{value} |
| 197 |
my $value = $self->{value}; |
| 198 |
$value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; |
| 199 |
if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) { |
| 200 |
$self->{value} = $value; |
| 201 |
} |
| 202 |
sprintf "\$%s",$self->{value}; |
| 203 |
} else { |
| 204 |
$self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm); |
| 205 |
sprintf "%s",$self->{value}; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
{ package ea; # pick up effective addresses: expr(%reg,%reg,scale) |
| 210 |
sub re { |
| 211 |
my $self = shift; # single instance in enough... |
| 212 |
local *line = shift; |
| 213 |
undef $ret; |
| 214 |
|
| 215 |
# optional * ---vvv--- appears in indirect jmp/call |
| 216 |
if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) { |
| 217 |
$self->{asterisk} = $1; |
| 218 |
$self->{label} = $2; |
| 219 |
($self->{base},$self->{index},$self->{scale})=split(/,/,$3); |
| 220 |
$self->{scale} = 1 if (!defined($self->{scale})); |
| 221 |
$ret = $self; |
| 222 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 223 |
|
| 224 |
if ($win64 && $self->{label} =~ s/\@GOTPCREL//) { |
| 225 |
die if (opcode->mnemonic() ne "mov"); |
| 226 |
opcode->mnemonic("lea"); |
| 227 |
} |
| 228 |
$self->{base} =~ s/^%//; |
| 229 |
$self->{index} =~ s/^%// if (defined($self->{index})); |
| 230 |
} |
| 231 |
$ret; |
| 232 |
} |
| 233 |
sub size {} |
| 234 |
sub out { |
| 235 |
my $self = shift; |
| 236 |
my $sz = shift; |
| 237 |
|
| 238 |
$self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 239 |
$self->{label} =~ s/\.L/$decor/g; |
| 240 |
|
| 241 |
# Silently convert all EAs to 64-bit. This is required for |
| 242 |
# elder GNU assembler and results in more compact code, |
| 243 |
# *but* most importantly AES module depends on this feature! |
| 244 |
$self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
| 245 |
$self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
| 246 |
|
| 247 |
# Solaris /usr/ccs/bin/as can't handle multiplications |
| 248 |
# in $self->{label}, new gas requires sign extension... |
| 249 |
use integer; |
| 250 |
$self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; |
| 251 |
$self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg; |
| 252 |
$self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg; |
| 253 |
|
| 254 |
if (!$self->{label} && $self->{index} && $self->{scale}==1 && |
| 255 |
$self->{base} =~ /(rbp|r13)/) { |
| 256 |
$self->{base} = $self->{index}; $self->{index} = $1; |
| 257 |
} |
| 258 |
|
| 259 |
if ($gas) { |
| 260 |
$self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64"); |
| 261 |
|
| 262 |
if (defined($self->{index})) { |
| 263 |
sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk}, |
| 264 |
$self->{label}, |
| 265 |
$self->{base}?"%$self->{base}":"", |
| 266 |
$self->{index},$self->{scale}; |
| 267 |
} else { |
| 268 |
sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; |
| 269 |
} |
| 270 |
} else { |
| 271 |
%szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", |
| 272 |
l=>"DWORD$PTR", d=>"DWORD$PTR", |
| 273 |
q=>"QWORD$PTR", o=>"OWORD$PTR", |
| 274 |
x=>"XMMWORD$PTR", y=>"YMMWORD$PTR", z=>"ZMMWORD$PTR" ); |
| 275 |
|
| 276 |
$self->{label} =~ s/\./\$/g; |
| 277 |
$self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; |
| 278 |
$self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); |
| 279 |
|
| 280 |
($self->{asterisk}) && ($sz="q") || |
| 281 |
(opcode->mnemonic() =~ /^v?mov([qd])$/) && ($sz=$1) || |
| 282 |
(opcode->mnemonic() =~ /^v?pinsr([qdwb])$/) && ($sz=$1) || |
| 283 |
(opcode->mnemonic() =~ /^vpbroadcast([qdwb])$/) && ($sz=$1) || |
| 284 |
(opcode->mnemonic() =~ /^vinsert[fi]128$/) && ($sz="x"); |
| 285 |
|
| 286 |
if (defined($self->{index})) { |
| 287 |
sprintf "%s[%s%s*%d%s]",$szmap{$sz}, |
| 288 |
$self->{label}?"$self->{label}+":"", |
| 289 |
$self->{index},$self->{scale}, |
| 290 |
$self->{base}?"+$self->{base}":""; |
| 291 |
} elsif ($self->{base} eq "rip") { |
| 292 |
sprintf "%s[%s]",$szmap{$sz},$self->{label}; |
| 293 |
} else { |
| 294 |
sprintf "%s[%s%s]",$szmap{$sz}, |
| 295 |
$self->{label}?"$self->{label}+":"", |
| 296 |
$self->{base}; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
{ package register; # pick up registers, which start with %. |
| 302 |
sub re { |
| 303 |
my $class = shift; # muliple instances... |
| 304 |
my $self = {}; |
| 305 |
local *line = shift; |
| 306 |
undef $ret; |
| 307 |
|
| 308 |
# optional * ---vvv--- appears in indirect jmp/call |
| 309 |
if ($line =~ /^(\*?)%(\w+)/) { |
| 310 |
bless $self,$class; |
| 311 |
$self->{asterisk} = $1; |
| 312 |
$self->{value} = $2; |
| 313 |
$ret = $self; |
| 314 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 315 |
} |
| 316 |
$ret; |
| 317 |
} |
| 318 |
sub size { |
| 319 |
my $self = shift; |
| 320 |
undef $ret; |
| 321 |
|
| 322 |
if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } |
| 323 |
elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } |
| 324 |
elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } |
| 325 |
elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } |
| 326 |
elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } |
| 327 |
elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } |
| 328 |
elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } |
| 329 |
elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } |
| 330 |
|
| 331 |
$ret; |
| 332 |
} |
| 333 |
sub out { |
| 334 |
my $self = shift; |
| 335 |
if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; } |
| 336 |
else { $self->{value}; } |
| 337 |
} |
| 338 |
} |
| 339 |
{ package label; # pick up labels, which end with : |
| 340 |
sub re { |
| 341 |
my $self = shift; # single instance is enough... |
| 342 |
local *line = shift; |
| 343 |
undef $ret; |
| 344 |
|
| 345 |
if ($line =~ /(^[\.\w]+)\:/) { |
| 346 |
$self->{value} = $1; |
| 347 |
$ret = $self; |
| 348 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 349 |
|
| 350 |
$self->{value} =~ s/^\.L/$decor/; |
| 351 |
} |
| 352 |
$ret; |
| 353 |
} |
| 354 |
sub out { |
| 355 |
my $self = shift; |
| 356 |
|
| 357 |
if ($gas) { |
| 358 |
my $func = ($globals{$self->{value}} or $self->{value}) . ":"; |
| 359 |
if ($win64 && |
| 360 |
$current_function->{name} eq $self->{value} && |
| 361 |
$current_function->{abi} eq "svr4") { |
| 362 |
$func .= "\n"; |
| 363 |
$func .= " movq %rdi,8(%rsp)\n"; |
| 364 |
$func .= " movq %rsi,16(%rsp)\n"; |
| 365 |
$func .= " movq %rsp,%rax\n"; |
| 366 |
$func .= "${decor}SEH_begin_$current_function->{name}:\n"; |
| 367 |
my $narg = $current_function->{narg}; |
| 368 |
$narg=6 if (!defined($narg)); |
| 369 |
$func .= " movq %rcx,%rdi\n" if ($narg>0); |
| 370 |
$func .= " movq %rdx,%rsi\n" if ($narg>1); |
| 371 |
$func .= " movq %r8,%rdx\n" if ($narg>2); |
| 372 |
$func .= " movq %r9,%rcx\n" if ($narg>3); |
| 373 |
$func .= " movq 40(%rsp),%r8\n" if ($narg>4); |
| 374 |
$func .= " movq 48(%rsp),%r9\n" if ($narg>5); |
| 375 |
} |
| 376 |
$func; |
| 377 |
} elsif ($self->{value} ne "$current_function->{name}") { |
| 378 |
$self->{value} .= ":" if ($masm && $ret!~m/^\$/); |
| 379 |
$self->{value} . ":"; |
| 380 |
} elsif ($win64 && $current_function->{abi} eq "svr4") { |
| 381 |
my $func = "$current_function->{name}" . |
| 382 |
($nasm ? ":" : "\tPROC $current_function->{scope}") . |
| 383 |
"\n"; |
| 384 |
$func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n"; |
| 385 |
$func .= " mov QWORD${PTR}[16+rsp],rsi\n"; |
| 386 |
$func .= " mov rax,rsp\n"; |
| 387 |
$func .= "${decor}SEH_begin_$current_function->{name}:"; |
| 388 |
$func .= ":" if ($masm); |
| 389 |
$func .= "\n"; |
| 390 |
my $narg = $current_function->{narg}; |
| 391 |
$narg=6 if (!defined($narg)); |
| 392 |
$func .= " mov rdi,rcx\n" if ($narg>0); |
| 393 |
$func .= " mov rsi,rdx\n" if ($narg>1); |
| 394 |
$func .= " mov rdx,r8\n" if ($narg>2); |
| 395 |
$func .= " mov rcx,r9\n" if ($narg>3); |
| 396 |
$func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4); |
| 397 |
$func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5); |
| 398 |
$func .= "\n"; |
| 399 |
} else { |
| 400 |
"$current_function->{name}". |
| 401 |
($nasm ? ":" : "\tPROC $current_function->{scope}"); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
{ package expr; # pick up expressioins |
| 406 |
sub re { |
| 407 |
my $self = shift; # single instance is enough... |
| 408 |
local *line = shift; |
| 409 |
undef $ret; |
| 410 |
|
| 411 |
if ($line =~ /(^[^,]+)/) { |
| 412 |
$self->{value} = $1; |
| 413 |
$ret = $self; |
| 414 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 415 |
|
| 416 |
$self->{value} =~ s/\@PLT// if (!$elf); |
| 417 |
$self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 418 |
$self->{value} =~ s/\.L/$decor/g; |
| 419 |
} |
| 420 |
$ret; |
| 421 |
} |
| 422 |
sub out { |
| 423 |
my $self = shift; |
| 424 |
if ($nasm && opcode->mnemonic()=~m/^j(?![re]cxz)/) { |
| 425 |
"NEAR ".$self->{value}; |
| 426 |
} else { |
| 427 |
$self->{value}; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
{ package directive; # pick up directives, which start with . |
| 432 |
sub re { |
| 433 |
my $self = shift; # single instance is enough... |
| 434 |
local *line = shift; |
| 435 |
undef $ret; |
| 436 |
my $dir; |
| 437 |
my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: |
| 438 |
( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, |
| 439 |
"%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, |
| 440 |
"%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, |
| 441 |
"%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, |
| 442 |
"%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, |
| 443 |
"%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, |
| 444 |
"%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, |
| 445 |
"%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); |
| 446 |
|
| 447 |
if ($line =~ /^\s*(\.\w+)/) { |
| 448 |
$dir = $1; |
| 449 |
$ret = $self; |
| 450 |
undef $self->{value}; |
| 451 |
$line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 452 |
|
| 453 |
SWITCH: for ($dir) { |
| 454 |
/\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) { |
| 455 |
$dir="\t.long"; |
| 456 |
$line=sprintf "0x%x,0x90000000",$opcode{$1}; |
| 457 |
} |
| 458 |
last; |
| 459 |
}; |
| 460 |
/\.global|\.globl|\.extern/ |
| 461 |
&& do { $globals{$line} = $prefix . $line; |
| 462 |
$line = $globals{$line} if ($prefix); |
| 463 |
last; |
| 464 |
}; |
| 465 |
/\.type/ && do { ($sym,$type,$narg) = split(',',$line); |
| 466 |
if ($type eq "\@function") { |
| 467 |
undef $current_function; |
| 468 |
$current_function->{name} = $sym; |
| 469 |
$current_function->{abi} = "svr4"; |
| 470 |
$current_function->{narg} = $narg; |
| 471 |
$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; |
| 472 |
} elsif ($type eq "\@abi-omnipotent") { |
| 473 |
undef $current_function; |
| 474 |
$current_function->{name} = $sym; |
| 475 |
$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; |
| 476 |
} |
| 477 |
$line =~ s/\@abi\-omnipotent/\@function/; |
| 478 |
$line =~ s/\@function.*/\@function/; |
| 479 |
last; |
| 480 |
}; |
| 481 |
/\.asciz/ && do { if ($line =~ /^"(.*)"$/) { |
| 482 |
$dir = ".byte"; |
| 483 |
$line = join(",",unpack("C*",$1),0); |
| 484 |
} |
| 485 |
last; |
| 486 |
}; |
| 487 |
/\.rva|\.long|\.quad/ |
| 488 |
&& do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 489 |
$line =~ s/\.L/$decor/g; |
| 490 |
last; |
| 491 |
}; |
| 492 |
} |
| 493 |
|
| 494 |
if ($gas) { |
| 495 |
$self->{value} = $dir . "\t" . $line; |
| 496 |
|
| 497 |
if ($dir =~ /\.extern/) { |
| 498 |
if ($flavour eq "elf") { |
| 499 |
$self->{value} .= "\n.hidden $line"; |
| 500 |
} else { |
| 501 |
$self->{value} = ""; |
| 502 |
} |
| 503 |
} elsif (!$elf && $dir =~ /\.type/) { |
| 504 |
$self->{value} = ""; |
| 505 |
$self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" . |
| 506 |
(defined($globals{$1})?".scl 2;":".scl 3;") . |
| 507 |
"\t.type 32;\t.endef" |
| 508 |
if ($win64 && $line =~ /([^,]+),\@function/); |
| 509 |
} elsif (!$elf && $dir =~ /\.size/) { |
| 510 |
$self->{value} = ""; |
| 511 |
if (defined($current_function)) { |
| 512 |
$self->{value} .= "${decor}SEH_end_$current_function->{name}:" |
| 513 |
if ($win64 && $current_function->{abi} eq "svr4"); |
| 514 |
undef $current_function; |
| 515 |
} |
| 516 |
} elsif (!$elf && $dir =~ /\.align/) { |
| 517 |
$self->{value} = ".p2align\t" . (log($line)/log(2)); |
| 518 |
} elsif ($dir eq ".section") { |
| 519 |
$current_segment=$line; |
| 520 |
if (!$elf && $current_segment eq ".init") { |
| 521 |
if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; } |
| 522 |
elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; } |
| 523 |
} |
| 524 |
} elsif ($dir =~ /\.(text|data)/) { |
| 525 |
$current_segment=".$1"; |
| 526 |
} elsif ($dir =~ /\.global|\.globl|\.extern/) { |
| 527 |
if ($flavour eq "macosx") { |
| 528 |
$self->{value} .= "\n.private_extern $line"; |
| 529 |
} else { |
| 530 |
$self->{value} .= "\n.hidden $line"; |
| 531 |
} |
| 532 |
} elsif ($dir =~ /\.hidden/) { |
| 533 |
if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$line"; } |
| 534 |
elsif ($flavour eq "mingw64") { $self->{value} = ""; } |
| 535 |
} elsif ($dir =~ /\.comm/) { |
| 536 |
$self->{value} = "$dir\t$prefix$line"; |
| 537 |
$self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx"); |
| 538 |
} |
| 539 |
$line = ""; |
| 540 |
return $self; |
| 541 |
} |
| 542 |
|
| 543 |
# non-gas case or nasm/masm |
| 544 |
SWITCH: for ($dir) { |
| 545 |
/\.text/ && do { my $v=undef; |
| 546 |
if ($nasm) { |
| 547 |
$v="section .text code align=64\n"; |
| 548 |
} else { |
| 549 |
$v="$current_segment\tENDS\n" if ($current_segment); |
| 550 |
$current_segment = ".text\$"; |
| 551 |
$v.="$current_segment\tSEGMENT "; |
| 552 |
$v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE"; |
| 553 |
$v.=" 'CODE'"; |
| 554 |
} |
| 555 |
$self->{value} = $v; |
| 556 |
last; |
| 557 |
}; |
| 558 |
/\.data/ && do { my $v=undef; |
| 559 |
if ($nasm) { |
| 560 |
$v="section .data data align=8\n"; |
| 561 |
} else { |
| 562 |
$v="$current_segment\tENDS\n" if ($current_segment); |
| 563 |
$current_segment = "_DATA"; |
| 564 |
$v.="$current_segment\tSEGMENT"; |
| 565 |
} |
| 566 |
$self->{value} = $v; |
| 567 |
last; |
| 568 |
}; |
| 569 |
/\.section/ && do { my $v=undef; |
| 570 |
$line =~ s/([^,]*).*/$1/; |
| 571 |
$line = ".CRT\$XCU" if ($line eq ".init"); |
| 572 |
if ($nasm) { |
| 573 |
$v="section $line"; |
| 574 |
if ($line=~/\.([px])data/) { |
| 575 |
$v.=" rdata align="; |
| 576 |
$v.=$1 eq "p"? 4 : 8; |
| 577 |
} elsif ($line=~/\.CRT\$/i) { |
| 578 |
$v.=" rdata align=8"; |
| 579 |
} |
| 580 |
} else { |
| 581 |
$v="$current_segment\tENDS\n" if ($current_segment); |
| 582 |
$v.="$line\tSEGMENT"; |
| 583 |
if ($line=~/\.([px])data/) { |
| 584 |
$v.=" READONLY"; |
| 585 |
$v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref); |
| 586 |
} elsif ($line=~/\.CRT\$/i) { |
| 587 |
$v.=" READONLY "; |
| 588 |
$v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD"; |
| 589 |
} |
| 590 |
} |
| 591 |
$current_segment = $line; |
| 592 |
$self->{value} = $v; |
| 593 |
last; |
| 594 |
}; |
| 595 |
/\.extern/ && do { $self->{value} = "EXTERN\t".$line; |
| 596 |
$self->{value} .= ":NEAR" if ($masm); |
| 597 |
last; |
| 598 |
}; |
| 599 |
/\.globl|.global/ |
| 600 |
&& do { $self->{value} = $masm?"PUBLIC":"global"; |
| 601 |
$self->{value} .= "\t".$line; |
| 602 |
last; |
| 603 |
}; |
| 604 |
/\.size/ && do { if (defined($current_function)) { |
| 605 |
undef $self->{value}; |
| 606 |
if ($current_function->{abi} eq "svr4") { |
| 607 |
$self->{value}="${decor}SEH_end_$current_function->{name}:"; |
| 608 |
$self->{value}.=":\n" if($masm); |
| 609 |
} |
| 610 |
$self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name}); |
| 611 |
undef $current_function; |
| 612 |
} |
| 613 |
last; |
| 614 |
}; |
| 615 |
/\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; |
| 616 |
/\.(value|long|rva|quad)/ |
| 617 |
&& do { my $sz = substr($1,0,1); |
| 618 |
my @arr = split(/,\s*/,$line); |
| 619 |
my $last = pop(@arr); |
| 620 |
my $conv = sub { my $var=shift; |
| 621 |
$var=~s/^(0b[0-1]+)/oct($1)/eig; |
| 622 |
$var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm); |
| 623 |
if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva")) |
| 624 |
{ $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; } |
| 625 |
$var; |
| 626 |
}; |
| 627 |
|
| 628 |
$sz =~ tr/bvlrq/BWDDQ/; |
| 629 |
$self->{value} = "\tD$sz\t"; |
| 630 |
for (@arr) { $self->{value} .= &$conv($_).","; } |
| 631 |
$self->{value} .= &$conv($last); |
| 632 |
last; |
| 633 |
}; |
| 634 |
/\.byte/ && do { my @str=split(/,\s*/,$line); |
| 635 |
map(s/(0b[0-1]+)/oct($1)/eig,@str); |
| 636 |
map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm); |
| 637 |
while ($#str>15) { |
| 638 |
$self->{value}.="DB\t" |
| 639 |
.join(",",@str[0..15])."\n"; |
| 640 |
foreach (0..15) { shift @str; } |
| 641 |
} |
| 642 |
$self->{value}.="DB\t" |
| 643 |
.join(",",@str) if (@str); |
| 644 |
last; |
| 645 |
}; |
| 646 |
/\.comm/ && do { my @str=split(/,\s*/,$line); |
| 647 |
my $v=undef; |
| 648 |
if ($nasm) { |
| 649 |
$v.="common $prefix@str[0] @str[1]"; |
| 650 |
} else { |
| 651 |
$v="$current_segment\tENDS\n" if ($current_segment); |
| 652 |
$current_segment = "_DATA"; |
| 653 |
$v.="$current_segment\tSEGMENT\n"; |
| 654 |
$v.="COMM @str[0]:DWORD:".@str[1]/4; |
| 655 |
} |
| 656 |
$self->{value} = $v; |
| 657 |
last; |
| 658 |
}; |
| 659 |
} |
| 660 |
$line = ""; |
| 661 |
} |
| 662 |
|
| 663 |
$ret; |
| 664 |
} |
| 665 |
sub out { |
| 666 |
my $self = shift; |
| 667 |
$self->{value}; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
sub rex { |
| 672 |
local *opcode=shift; |
| 673 |
my ($dst,$src,$rex)=@_; |
| 674 |
|
| 675 |
$rex|=0x04 if($dst>=8); |
| 676 |
$rex|=0x01 if($src>=8); |
| 677 |
push @opcode,($rex|0x40) if ($rex); |
| 678 |
} |
| 679 |
|
| 680 |
# older gas and ml64 don't handle SSE>2 instructions |
| 681 |
my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3, |
| 682 |
"%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 ); |
| 683 |
|
| 684 |
my $movq = sub { # elderly gas can't handle inter-register movq |
| 685 |
my $arg = shift; |
| 686 |
my @opcode=(0x66); |
| 687 |
if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) { |
| 688 |
my ($src,$dst)=($1,$2); |
| 689 |
if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } |
| 690 |
rex(\@opcode,$src,$dst,0x8); |
| 691 |
push @opcode,0x0f,0x7e; |
| 692 |
push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M |
| 693 |
@opcode; |
| 694 |
} elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) { |
| 695 |
my ($src,$dst)=($2,$1); |
| 696 |
if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } |
| 697 |
rex(\@opcode,$src,$dst,0x8); |
| 698 |
push @opcode,0x0f,0x6e; |
| 699 |
push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M |
| 700 |
@opcode; |
| 701 |
} else { |
| 702 |
(); |
| 703 |
} |
| 704 |
}; |
| 705 |
|
| 706 |
my $pextrd = sub { |
| 707 |
if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) { |
| 708 |
my @opcode=(0x66); |
| 709 |
$imm=$1; |
| 710 |
$src=$2; |
| 711 |
$dst=$3; |
| 712 |
if ($dst =~ /%r([0-9]+)d/) { $dst = $1; } |
| 713 |
elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; } |
| 714 |
rex(\@opcode,$src,$dst); |
| 715 |
push @opcode,0x0f,0x3a,0x16; |
| 716 |
push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M |
| 717 |
push @opcode,$imm; |
| 718 |
@opcode; |
| 719 |
} else { |
| 720 |
(); |
| 721 |
} |
| 722 |
}; |
| 723 |
|
| 724 |
my $pinsrd = sub { |
| 725 |
if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) { |
| 726 |
my @opcode=(0x66); |
| 727 |
$imm=$1; |
| 728 |
$src=$2; |
| 729 |
$dst=$3; |
| 730 |
if ($src =~ /%r([0-9]+)/) { $src = $1; } |
| 731 |
elsif ($src =~ /%e/) { $src = $regrm{$src}; } |
| 732 |
rex(\@opcode,$dst,$src); |
| 733 |
push @opcode,0x0f,0x3a,0x22; |
| 734 |
push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M |
| 735 |
push @opcode,$imm; |
| 736 |
@opcode; |
| 737 |
} else { |
| 738 |
(); |
| 739 |
} |
| 740 |
}; |
| 741 |
|
| 742 |
my $pshufb = sub { |
| 743 |
if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) { |
| 744 |
my @opcode=(0x66); |
| 745 |
rex(\@opcode,$2,$1); |
| 746 |
push @opcode,0x0f,0x38,0x00; |
| 747 |
push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M |
| 748 |
@opcode; |
| 749 |
} else { |
| 750 |
(); |
| 751 |
} |
| 752 |
}; |
| 753 |
|
| 754 |
my $palignr = sub { |
| 755 |
if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { |
| 756 |
my @opcode=(0x66); |
| 757 |
rex(\@opcode,$3,$2); |
| 758 |
push @opcode,0x0f,0x3a,0x0f; |
| 759 |
push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M |
| 760 |
push @opcode,$1; |
| 761 |
@opcode; |
| 762 |
} else { |
| 763 |
(); |
| 764 |
} |
| 765 |
}; |
| 766 |
|
| 767 |
my $pclmulqdq = sub { |
| 768 |
if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { |
| 769 |
my @opcode=(0x66); |
| 770 |
rex(\@opcode,$3,$2); |
| 771 |
push @opcode,0x0f,0x3a,0x44; |
| 772 |
push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M |
| 773 |
my $c=$1; |
| 774 |
push @opcode,$c=~/^0/?oct($c):$c; |
| 775 |
@opcode; |
| 776 |
} else { |
| 777 |
(); |
| 778 |
} |
| 779 |
}; |
| 780 |
|
| 781 |
my $rdrand = sub { |
| 782 |
if (shift =~ /%[er](\w+)/) { |
| 783 |
my @opcode=(); |
| 784 |
my $dst=$1; |
| 785 |
if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } |
| 786 |
rex(\@opcode,0,$1,8); |
| 787 |
push @opcode,0x0f,0xc7,0xf0|($dst&7); |
| 788 |
@opcode; |
| 789 |
} else { |
| 790 |
(); |
| 791 |
} |
| 792 |
}; |
| 793 |
|
| 794 |
my $rdseed = sub { |
| 795 |
if (shift =~ /%[er](\w+)/) { |
| 796 |
my @opcode=(); |
| 797 |
my $dst=$1; |
| 798 |
if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; } |
| 799 |
rex(\@opcode,0,$1,8); |
| 800 |
push @opcode,0x0f,0xc7,0xf8|($dst&7); |
| 801 |
@opcode; |
| 802 |
} else { |
| 803 |
(); |
| 804 |
} |
| 805 |
}; |
| 806 |
|
| 807 |
sub rxb { |
| 808 |
local *opcode=shift; |
| 809 |
my ($dst,$src1,$src2,$rxb)=@_; |
| 810 |
|
| 811 |
$rxb|=0x7<<5; |
| 812 |
$rxb&=~(0x04<<5) if($dst>=8); |
| 813 |
$rxb&=~(0x01<<5) if($src1>=8); |
| 814 |
$rxb&=~(0x02<<5) if($src2>=8); |
| 815 |
push @opcode,$rxb; |
| 816 |
} |
| 817 |
|
| 818 |
my $vprotd = sub { |
| 819 |
if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { |
| 820 |
my @opcode=(0x8f); |
| 821 |
rxb(\@opcode,$3,$2,-1,0x08); |
| 822 |
push @opcode,0x78,0xc2; |
| 823 |
push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M |
| 824 |
my $c=$1; |
| 825 |
push @opcode,$c=~/^0/?oct($c):$c; |
| 826 |
@opcode; |
| 827 |
} else { |
| 828 |
(); |
| 829 |
} |
| 830 |
}; |
| 831 |
|
| 832 |
my $vprotq = sub { |
| 833 |
if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) { |
| 834 |
my @opcode=(0x8f); |
| 835 |
rxb(\@opcode,$3,$2,-1,0x08); |
| 836 |
push @opcode,0x78,0xc3; |
| 837 |
push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M |
| 838 |
my $c=$1; |
| 839 |
push @opcode,$c=~/^0/?oct($c):$c; |
| 840 |
@opcode; |
| 841 |
} else { |
| 842 |
(); |
| 843 |
} |
| 844 |
}; |
| 845 |
|
| 846 |
if ($nasm) { |
| 847 |
print <<___; |
| 848 |
default rel |
| 849 |
%define XMMWORD |
| 850 |
%define YMMWORD |
| 851 |
%define ZMMWORD |
| 852 |
___ |
| 853 |
} elsif ($masm) { |
| 854 |
print <<___; |
| 855 |
OPTION DOTNAME |
| 856 |
___ |
| 857 |
} |
| 858 |
|
| 859 |
print STDOUT "#if defined(__x86_64__)\n" if ($gas); |
| 860 |
|
| 861 |
while($line=<>) { |
| 862 |
|
| 863 |
chomp($line); |
| 864 |
|
| 865 |
$line =~ s|[#!].*$||; # get rid of asm-style comments... |
| 866 |
$line =~ s|/\*.*\*/||; # ... and C-style comments... |
| 867 |
$line =~ s|^\s+||; # ... and skip white spaces in beginning |
| 868 |
$line =~ s|\s+$||; # ... and at the end |
| 869 |
|
| 870 |
undef $label; |
| 871 |
undef $opcode; |
| 872 |
undef @args; |
| 873 |
|
| 874 |
if ($label=label->re(\$line)) { print $label->out(); } |
| 875 |
|
| 876 |
if (directive->re(\$line)) { |
| 877 |
printf "%s",directive->out(); |
| 878 |
} elsif ($opcode=opcode->re(\$line)) { |
| 879 |
my $asm = eval("\$".$opcode->mnemonic()); |
| 880 |
undef @bytes; |
| 881 |
|
| 882 |
if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) { |
| 883 |
print $gas?".byte\t":"DB\t",join(',',@bytes),"\n"; |
| 884 |
next; |
| 885 |
} |
| 886 |
|
| 887 |
ARGUMENT: while (1) { |
| 888 |
my $arg; |
| 889 |
|
| 890 |
if ($arg=register->re(\$line)) { opcode->size($arg->size()); } |
| 891 |
elsif ($arg=const->re(\$line)) { } |
| 892 |
elsif ($arg=ea->re(\$line)) { } |
| 893 |
elsif ($arg=expr->re(\$line)) { } |
| 894 |
else { last ARGUMENT; } |
| 895 |
|
| 896 |
push @args,$arg; |
| 897 |
|
| 898 |
last ARGUMENT if ($line !~ /^,/); |
| 899 |
|
| 900 |
$line =~ s/^,\s*//; |
| 901 |
} # ARGUMENT: |
| 902 |
|
| 903 |
if ($#args>=0) { |
| 904 |
my $insn; |
| 905 |
my $sz=opcode->size(); |
| 906 |
|
| 907 |
if ($gas) { |
| 908 |
$insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); |
| 909 |
@args = map($_->out($sz),@args); |
| 910 |
printf "\t%s\t%s",$insn,join(",",@args); |
| 911 |
} else { |
| 912 |
$insn = $opcode->out(); |
| 913 |
foreach (@args) { |
| 914 |
my $arg = $_->out(); |
| 915 |
# $insn.=$sz compensates for movq, pinsrw, ... |
| 916 |
if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; } |
| 917 |
if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; } |
| 918 |
if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; } |
| 919 |
if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; } |
| 920 |
} |
| 921 |
@args = reverse(@args); |
| 922 |
undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); |
| 923 |
|
| 924 |
if ($insn eq "movq" && $#args == 1 && $args[0]->out($sz) eq "xmm0" && $args[1]->out($sz) eq "rax") { |
| 925 |
# I have no clue why MASM can't parse this instruction. |
| 926 |
printf "DB 66h, 48h, 0fh, 6eh, 0c0h"; |
| 927 |
} else { |
| 928 |
printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); |
| 929 |
} |
| 930 |
} |
| 931 |
} else { |
| 932 |
printf "\t%s",$opcode->out(); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
print $line,"\n"; |
| 937 |
} |
| 938 |
|
| 939 |
print "\n$current_segment\tENDS\n" if ($current_segment && $masm); |
| 940 |
print "END\n" if ($masm); |
| 941 |
print "#endif\n" if ($gas); |
| 942 |
|
| 943 |
|
| 944 |
close STDOUT; |
| 945 |
|
| 946 |
################################################# |
| 947 |
# Cross-reference x86_64 ABI "card" |
| 948 |
# |
| 949 |
# Unix Win64 |
| 950 |
# %rax * * |
| 951 |
# %rbx - - |
| 952 |
# %rcx #4 #1 |
| 953 |
# %rdx #3 #2 |
| 954 |
# %rsi #2 - |
| 955 |
# %rdi #1 - |
| 956 |
# %rbp - - |
| 957 |
# %rsp - - |
| 958 |
# %r8 #5 #3 |
| 959 |
# %r9 #6 #4 |
| 960 |
# %r10 * * |
| 961 |
# %r11 * * |
| 962 |
# %r12 - - |
| 963 |
# %r13 - - |
| 964 |
# %r14 - - |
| 965 |
# %r15 - - |
| 966 |
# |
| 967 |
# (*) volatile register |
| 968 |
# (-) preserved by callee |
| 969 |
# (#) Nth argument, volatile |
| 970 |
# |
| 971 |
# In Unix terms top of stack is argument transfer area for arguments |
| 972 |
# which could not be accomodated in registers. Or in other words 7th |
| 973 |
# [integer] argument resides at 8(%rsp) upon function entry point. |
| 974 |
# 128 bytes above %rsp constitute a "red zone" which is not touched |
| 975 |
# by signal handlers and can be used as temporal storage without |
| 976 |
# allocating a frame. |
| 977 |
# |
| 978 |
# In Win64 terms N*8 bytes on top of stack is argument transfer area, |
| 979 |
# which belongs to/can be overwritten by callee. N is the number of |
| 980 |
# arguments passed to callee, *but* not less than 4! This means that |
| 981 |
# upon function entry point 5th argument resides at 40(%rsp), as well |
| 982 |
# as that 32 bytes from 8(%rsp) can always be used as temporal |
| 983 |
# storage [without allocating a frame]. One can actually argue that |
| 984 |
# one can assume a "red zone" above stack pointer under Win64 as well. |
| 985 |
# Point is that at apparently no occasion Windows kernel would alter |
| 986 |
# the area above user stack pointer in true asynchronous manner... |
| 987 |
# |
| 988 |
# All the above means that if assembler programmer adheres to Unix |
| 989 |
# register and stack layout, but disregards the "red zone" existense, |
| 990 |
# it's possible to use following prologue and epilogue to "gear" from |
| 991 |
# Unix to Win64 ABI in leaf functions with not more than 6 arguments. |
| 992 |
# |
| 993 |
# omnipotent_function: |
| 994 |
# ifdef WIN64 |
| 995 |
# movq %rdi,8(%rsp) |
| 996 |
# movq %rsi,16(%rsp) |
| 997 |
# movq %rcx,%rdi ; if 1st argument is actually present |
| 998 |
# movq %rdx,%rsi ; if 2nd argument is actually ... |
| 999 |
# movq %r8,%rdx ; if 3rd argument is ... |
| 1000 |
# movq %r9,%rcx ; if 4th argument ... |
| 1001 |
# movq 40(%rsp),%r8 ; if 5th ... |
| 1002 |
# movq 48(%rsp),%r9 ; if 6th ... |
| 1003 |
# endif |
| 1004 |
# ... |
| 1005 |
# ifdef WIN64 |
| 1006 |
# movq 8(%rsp),%rdi |
| 1007 |
# movq 16(%rsp),%rsi |
| 1008 |
# endif |
| 1009 |
# ret |
| 1010 |
# |
| 1011 |
################################################# |
| 1012 |
# Win64 SEH, Structured Exception Handling. |
| 1013 |
# |
| 1014 |
# Unlike on Unix systems(*) lack of Win64 stack unwinding information |
| 1015 |
# has undesired side-effect at run-time: if an exception is raised in |
| 1016 |
# assembler subroutine such as those in question (basically we're |
| 1017 |
# referring to segmentation violations caused by malformed input |
| 1018 |
# parameters), the application is briskly terminated without invoking |
| 1019 |
# any exception handlers, most notably without generating memory dump |
| 1020 |
# or any user notification whatsoever. This poses a problem. It's |
| 1021 |
# possible to address it by registering custom language-specific |
| 1022 |
# handler that would restore processor context to the state at |
| 1023 |
# subroutine entry point and return "exception is not handled, keep |
| 1024 |
# unwinding" code. Writing such handler can be a challenge... But it's |
| 1025 |
# doable, though requires certain coding convention. Consider following |
| 1026 |
# snippet: |
| 1027 |
# |
| 1028 |
# .type function,@function |
| 1029 |
# function: |
| 1030 |
# movq %rsp,%rax # copy rsp to volatile register |
| 1031 |
# pushq %r15 # save non-volatile registers |
| 1032 |
# pushq %rbx |
| 1033 |
# pushq %rbp |
| 1034 |
# movq %rsp,%r11 |
| 1035 |
# subq %rdi,%r11 # prepare [variable] stack frame |
| 1036 |
# andq $-64,%r11 |
| 1037 |
# movq %rax,0(%r11) # check for exceptions |
| 1038 |
# movq %r11,%rsp # allocate [variable] stack frame |
| 1039 |
# movq %rax,0(%rsp) # save original rsp value |
| 1040 |
# magic_point: |
| 1041 |
# ... |
| 1042 |
# movq 0(%rsp),%rcx # pull original rsp value |
| 1043 |
# movq -24(%rcx),%rbp # restore non-volatile registers |
| 1044 |
# movq -16(%rcx),%rbx |
| 1045 |
# movq -8(%rcx),%r15 |
| 1046 |
# movq %rcx,%rsp # restore original rsp |
| 1047 |
# ret |
| 1048 |
# .size function,.-function |
| 1049 |
# |
| 1050 |
# The key is that up to magic_point copy of original rsp value remains |
| 1051 |
# in chosen volatile register and no non-volatile register, except for |
| 1052 |
# rsp, is modified. While past magic_point rsp remains constant till |
| 1053 |
# the very end of the function. In this case custom language-specific |
| 1054 |
# exception handler would look like this: |
| 1055 |
# |
| 1056 |
# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, |
| 1057 |
# CONTEXT *context,DISPATCHER_CONTEXT *disp) |
| 1058 |
# { ULONG64 *rsp = (ULONG64 *)context->Rax; |
| 1059 |
# if (context->Rip >= magic_point) |
| 1060 |
# { rsp = ((ULONG64 **)context->Rsp)[0]; |
| 1061 |
# context->Rbp = rsp[-3]; |
| 1062 |
# context->Rbx = rsp[-2]; |
| 1063 |
# context->R15 = rsp[-1]; |
| 1064 |
# } |
| 1065 |
# context->Rsp = (ULONG64)rsp; |
| 1066 |
# context->Rdi = rsp[1]; |
| 1067 |
# context->Rsi = rsp[2]; |
| 1068 |
# |
| 1069 |
# memcpy (disp->ContextRecord,context,sizeof(CONTEXT)); |
| 1070 |
# RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase, |
| 1071 |
# dips->ControlPc,disp->FunctionEntry,disp->ContextRecord, |
| 1072 |
# &disp->HandlerData,&disp->EstablisherFrame,NULL); |
| 1073 |
# return ExceptionContinueSearch; |
| 1074 |
# } |
| 1075 |
# |
| 1076 |
# It's appropriate to implement this handler in assembler, directly in |
| 1077 |
# function's module. In order to do that one has to know members' |
| 1078 |
# offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant |
| 1079 |
# values. Here they are: |
| 1080 |
# |
| 1081 |
# CONTEXT.Rax 120 |
| 1082 |
# CONTEXT.Rcx 128 |
| 1083 |
# CONTEXT.Rdx 136 |
| 1084 |
# CONTEXT.Rbx 144 |
| 1085 |
# CONTEXT.Rsp 152 |
| 1086 |
# CONTEXT.Rbp 160 |
| 1087 |
# CONTEXT.Rsi 168 |
| 1088 |
# CONTEXT.Rdi 176 |
| 1089 |
# CONTEXT.R8 184 |
| 1090 |
# CONTEXT.R9 192 |
| 1091 |
# CONTEXT.R10 200 |
| 1092 |
# CONTEXT.R11 208 |
| 1093 |
# CONTEXT.R12 216 |
| 1094 |
# CONTEXT.R13 224 |
| 1095 |
# CONTEXT.R14 232 |
| 1096 |
# CONTEXT.R15 240 |
| 1097 |
# CONTEXT.Rip 248 |
| 1098 |
# CONTEXT.Xmm6 512 |
| 1099 |
# sizeof(CONTEXT) 1232 |
| 1100 |
# DISPATCHER_CONTEXT.ControlPc 0 |
| 1101 |
# DISPATCHER_CONTEXT.ImageBase 8 |
| 1102 |
# DISPATCHER_CONTEXT.FunctionEntry 16 |
| 1103 |
# DISPATCHER_CONTEXT.EstablisherFrame 24 |
| 1104 |
# DISPATCHER_CONTEXT.TargetIp 32 |
| 1105 |
# DISPATCHER_CONTEXT.ContextRecord 40 |
| 1106 |
# DISPATCHER_CONTEXT.LanguageHandler 48 |
| 1107 |
# DISPATCHER_CONTEXT.HandlerData 56 |
| 1108 |
# UNW_FLAG_NHANDLER 0 |
| 1109 |
# ExceptionContinueSearch 1 |
| 1110 |
# |
| 1111 |
# In order to tie the handler to the function one has to compose |
| 1112 |
# couple of structures: one for .xdata segment and one for .pdata. |
| 1113 |
# |
| 1114 |
# UNWIND_INFO structure for .xdata segment would be |
| 1115 |
# |
| 1116 |
# function_unwind_info: |
| 1117 |
# .byte 9,0,0,0 |
| 1118 |
# .rva handler |
| 1119 |
# |
| 1120 |
# This structure designates exception handler for a function with |
| 1121 |
# zero-length prologue, no stack frame or frame register. |
| 1122 |
# |
| 1123 |
# To facilitate composing of .pdata structures, auto-generated "gear" |
| 1124 |
# prologue copies rsp value to rax and denotes next instruction with |
| 1125 |
# .LSEH_begin_{function_name} label. This essentially defines the SEH |
| 1126 |
# styling rule mentioned in the beginning. Position of this label is |
| 1127 |
# chosen in such manner that possible exceptions raised in the "gear" |
| 1128 |
# prologue would be accounted to caller and unwound from latter's frame. |
| 1129 |
# End of function is marked with respective .LSEH_end_{function_name} |
| 1130 |
# label. To summarize, .pdata segment would contain |
| 1131 |
# |
| 1132 |
# .rva .LSEH_begin_function |
| 1133 |
# .rva .LSEH_end_function |
| 1134 |
# .rva function_unwind_info |
| 1135 |
# |
| 1136 |
# Reference to functon_unwind_info from .xdata segment is the anchor. |
| 1137 |
# In case you wonder why references are 32-bit .rvas and not 64-bit |
| 1138 |
# .quads. References put into these two segments are required to be |
| 1139 |
# *relative* to the base address of the current binary module, a.k.a. |
| 1140 |
# image base. No Win64 module, be it .exe or .dll, can be larger than |
| 1141 |
# 2GB and thus such relative references can be and are accommodated in |
| 1142 |
# 32 bits. |
| 1143 |
# |
| 1144 |
# Having reviewed the example function code, one can argue that "movq |
| 1145 |
# %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix |
| 1146 |
# rax would contain an undefined value. If this "offends" you, use |
| 1147 |
# another register and refrain from modifying rax till magic_point is |
| 1148 |
# reached, i.e. as if it was a non-volatile register. If more registers |
| 1149 |
# are required prior [variable] frame setup is completed, note that |
| 1150 |
# nobody says that you can have only one "magic point." You can |
| 1151 |
# "liberate" non-volatile registers by denoting last stack off-load |
| 1152 |
# instruction and reflecting it in finer grade unwind logic in handler. |
| 1153 |
# After all, isn't it why it's called *language-specific* handler... |
| 1154 |
# |
| 1155 |
# Attentive reader can notice that exceptions would be mishandled in |
| 1156 |
# auto-generated "gear" epilogue. Well, exception effectively can't |
| 1157 |
# occur there, because if memory area used by it was subject to |
| 1158 |
# segmentation violation, then it would be raised upon call to the |
| 1159 |
# function (and as already mentioned be accounted to caller, which is |
| 1160 |
# not a problem). If you're still not comfortable, then define tail |
| 1161 |
# "magic point" just prior ret instruction and have handler treat it... |
| 1162 |
# |
| 1163 |
# (*) Note that we're talking about run-time, not debug-time. Lack of |
| 1164 |
# unwind information makes debugging hard on both Windows and |
| 1165 |
# Unix. "Unlike" referes to the fact that on Unix signal handler |
| 1166 |
# will always be invoked, core dumped and appropriate exit code |
| 1167 |
# returned to parent (for user notification). |