Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 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 | |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 65 | { my ($stddev,$stdino,@junk)=stat(STDOUT); |
| 66 | my ($outdev,$outino,@junk)=stat($output); |
| 67 | |
| 68 | open STDOUT,">$output" || die "can't open $output: $!" |
| 69 | if ($stddev!=$outdev || $stdino!=$outino); |
| 70 | } |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 71 | |
| 72 | my $gas=1; $gas=0 if ($output =~ /\.asm$/); |
| 73 | my $elf=1; $elf=0 if (!$gas); |
| 74 | my $win64=0; |
| 75 | my $prefix=""; |
| 76 | my $decor=".L"; |
| 77 | |
| 78 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 |
| 79 | my $masm=0; |
| 80 | my $PTR=" PTR"; |
| 81 | |
| 82 | my $nasmref=2.03; |
| 83 | my $nasm=0; |
| 84 | |
| 85 | if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; |
| 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) |
| 93 | { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i) |
| 94 | { $nasm = $1 + $2*0.01; $PTR=""; } |
| 95 | elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/) |
| 96 | { $masm = $1 + $2*2**-16 + $4*2**-32; } |
| 97 | die "no assembler found on %PATH" if (!($nasm || $masm)); |
| 98 | $win64=1; |
| 99 | $elf=0; |
| 100 | $decor="\$L\$"; |
| 101 | } |
| 102 | |
| 103 | my $current_segment; |
| 104 | my $current_function; |
| 105 | my %globals; |
| 106 | |
| 107 | { package opcode; # pick up opcodes |
| 108 | sub re { |
| 109 | my $self = shift; # single instance in enough... |
| 110 | local *line = shift; |
| 111 | undef $ret; |
| 112 | |
| 113 | if ($line =~ /^([a-z][a-z0-9]*)/i) { |
| 114 | $self->{op} = $1; |
| 115 | $ret = $self; |
| 116 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 117 | |
| 118 | undef $self->{sz}; |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 119 | if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 120 | $self->{op} = $1; |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 121 | $self->{sz} = "b"; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 122 | } elsif ($self->{op} =~ /call|jmp/) { |
| 123 | $self->{sz} = ""; |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 124 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op)/) { # SSEn |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 125 | $self->{sz} = ""; |
| 126 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { |
| 127 | $self->{op} = $1; |
| 128 | $self->{sz} = $2; |
| 129 | } |
| 130 | } |
| 131 | $ret; |
| 132 | } |
| 133 | sub size { |
| 134 | my $self = shift; |
| 135 | my $sz = shift; |
| 136 | $self->{sz} = $sz if (defined($sz) && !defined($self->{sz})); |
| 137 | $self->{sz}; |
| 138 | } |
| 139 | sub out { |
| 140 | my $self = shift; |
| 141 | if ($gas) { |
| 142 | if ($self->{op} eq "movz") { # movz is pain... |
| 143 | sprintf "%s%s%s",$self->{op},$self->{sz},shift; |
| 144 | } elsif ($self->{op} =~ /^set/) { |
| 145 | "$self->{op}"; |
| 146 | } elsif ($self->{op} eq "ret") { |
| 147 | my $epilogue = ""; |
| 148 | if ($win64 && $current_function->{abi} eq "svr4") { |
| 149 | $epilogue = "movq 8(%rsp),%rdi\n\t" . |
| 150 | "movq 16(%rsp),%rsi\n\t"; |
| 151 | } |
| 152 | $epilogue . ".byte 0xf3,0xc3"; |
| 153 | } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") { |
| 154 | ".p2align\t3\n\t.quad"; |
| 155 | } else { |
| 156 | "$self->{op}$self->{sz}"; |
| 157 | } |
| 158 | } else { |
| 159 | $self->{op} =~ s/^movz/movzx/; |
| 160 | if ($self->{op} eq "ret") { |
| 161 | $self->{op} = ""; |
| 162 | if ($win64 && $current_function->{abi} eq "svr4") { |
| 163 | $self->{op} = "mov rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t". |
| 164 | "mov rsi,QWORD${PTR}[16+rsp]\n\t"; |
| 165 | } |
| 166 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; |
| 167 | } elsif ($self->{op} =~ /^(pop|push)f/) { |
| 168 | $self->{op} .= $self->{sz}; |
| 169 | } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") { |
| 170 | $self->{op} = "\tDQ"; |
| 171 | } |
| 172 | $self->{op}; |
| 173 | } |
| 174 | } |
| 175 | sub mnemonic { |
| 176 | my $self=shift; |
| 177 | my $op=shift; |
| 178 | $self->{op}=$op if (defined($op)); |
| 179 | $self->{op}; |
| 180 | } |
| 181 | } |
| 182 | { package const; # pick up constants, which start with $ |
| 183 | sub re { |
| 184 | my $self = shift; # single instance in enough... |
| 185 | local *line = shift; |
| 186 | undef $ret; |
| 187 | |
| 188 | if ($line =~ /^\$([^,]+)/) { |
| 189 | $self->{value} = $1; |
| 190 | $ret = $self; |
| 191 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 192 | } |
| 193 | $ret; |
| 194 | } |
| 195 | sub out { |
| 196 | my $self = shift; |
| 197 | |
| 198 | if ($gas) { |
| 199 | # Solaris /usr/ccs/bin/as can't handle multiplications |
| 200 | # in $self->{value} |
| 201 | $self->{value} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; |
| 202 | $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; |
| 203 | sprintf "\$%s",$self->{value}; |
| 204 | } else { |
| 205 | $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig; |
| 206 | $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm); |
| 207 | sprintf "%s",$self->{value}; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | { package ea; # pick up effective addresses: expr(%reg,%reg,scale) |
| 212 | sub re { |
| 213 | my $self = shift; # single instance in enough... |
| 214 | local *line = shift; |
| 215 | undef $ret; |
| 216 | |
| 217 | # optional * ---vvv--- appears in indirect jmp/call |
| 218 | if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) { |
| 219 | $self->{asterisk} = $1; |
| 220 | $self->{label} = $2; |
| 221 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$3); |
| 222 | $self->{scale} = 1 if (!defined($self->{scale})); |
| 223 | $ret = $self; |
| 224 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 225 | |
| 226 | if ($win64 && $self->{label} =~ s/\@GOTPCREL//) { |
| 227 | die if (opcode->mnemonic() ne "mov"); |
| 228 | opcode->mnemonic("lea"); |
| 229 | } |
| 230 | $self->{base} =~ s/^%//; |
| 231 | $self->{index} =~ s/^%// if (defined($self->{index})); |
| 232 | } |
| 233 | $ret; |
| 234 | } |
| 235 | sub size {} |
| 236 | sub out { |
| 237 | my $self = shift; |
| 238 | my $sz = shift; |
| 239 | |
| 240 | $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 241 | $self->{label} =~ s/\.L/$decor/g; |
| 242 | |
| 243 | # Silently convert all EAs to 64-bit. This is required for |
| 244 | # elder GNU assembler and results in more compact code, |
| 245 | # *but* most importantly AES module depends on this feature! |
| 246 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
| 247 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
| 248 | |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 249 | if ($gas) { |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 250 | # Solaris /usr/ccs/bin/as can't handle multiplications |
| 251 | # in $self->{label}, new gas requires sign extension... |
| 252 | use integer; |
| 253 | $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; |
| 254 | $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; |
| 255 | $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 256 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64"); |
| 257 | |
| 258 | if (defined($self->{index})) { |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 259 | sprintf "%s%s(%%%s,%%%s,%d)",$self->{asterisk}, |
| 260 | $self->{label},$self->{base}, |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 261 | $self->{index},$self->{scale}; |
| 262 | } else { |
| 263 | sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; |
| 264 | } |
| 265 | } else { |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 266 | %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", q=>"QWORD$PTR" ); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 267 | |
| 268 | $self->{label} =~ s/\./\$/g; |
| 269 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; |
| 270 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 271 | $sz="q" if ($self->{asterisk}); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 272 | |
| 273 | if (defined($self->{index})) { |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 274 | sprintf "%s[%s%s*%d+%s]",$szmap{$sz}, |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 275 | $self->{label}?"$self->{label}+":"", |
| 276 | $self->{index},$self->{scale}, |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 277 | $self->{base}; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 278 | } elsif ($self->{base} eq "rip") { |
| 279 | sprintf "%s[%s]",$szmap{$sz},$self->{label}; |
| 280 | } else { |
| 281 | sprintf "%s[%s%s]",$szmap{$sz}, |
| 282 | $self->{label}?"$self->{label}+":"", |
| 283 | $self->{base}; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | { package register; # pick up registers, which start with %. |
| 289 | sub re { |
| 290 | my $class = shift; # muliple instances... |
| 291 | my $self = {}; |
| 292 | local *line = shift; |
| 293 | undef $ret; |
| 294 | |
| 295 | # optional * ---vvv--- appears in indirect jmp/call |
| 296 | if ($line =~ /^(\*?)%(\w+)/) { |
| 297 | bless $self,$class; |
| 298 | $self->{asterisk} = $1; |
| 299 | $self->{value} = $2; |
| 300 | $ret = $self; |
| 301 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 302 | } |
| 303 | $ret; |
| 304 | } |
| 305 | sub size { |
| 306 | my $self = shift; |
| 307 | undef $ret; |
| 308 | |
| 309 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } |
| 310 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } |
| 311 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } |
| 312 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } |
| 313 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } |
| 314 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } |
| 315 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } |
| 316 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } |
| 317 | |
| 318 | $ret; |
| 319 | } |
| 320 | sub out { |
| 321 | my $self = shift; |
| 322 | if ($gas) { sprintf "%s%%%s",$self->{asterisk},$self->{value}; } |
| 323 | else { $self->{value}; } |
| 324 | } |
| 325 | } |
| 326 | { package label; # pick up labels, which end with : |
| 327 | sub re { |
| 328 | my $self = shift; # single instance is enough... |
| 329 | local *line = shift; |
| 330 | undef $ret; |
| 331 | |
| 332 | if ($line =~ /(^[\.\w]+)\:/) { |
| 333 | $self->{value} = $1; |
| 334 | $ret = $self; |
| 335 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 336 | |
| 337 | $self->{value} =~ s/^\.L/$decor/; |
| 338 | } |
| 339 | $ret; |
| 340 | } |
| 341 | sub out { |
| 342 | my $self = shift; |
| 343 | |
| 344 | if ($gas) { |
| 345 | my $func = ($globals{$self->{value}} or $self->{value}) . ":"; |
| 346 | if ($win64 && |
| 347 | $current_function->{name} eq $self->{value} && |
| 348 | $current_function->{abi} eq "svr4") { |
| 349 | $func .= "\n"; |
| 350 | $func .= " movq %rdi,8(%rsp)\n"; |
| 351 | $func .= " movq %rsi,16(%rsp)\n"; |
| 352 | $func .= " movq %rsp,%rax\n"; |
| 353 | $func .= "${decor}SEH_begin_$current_function->{name}:\n"; |
| 354 | my $narg = $current_function->{narg}; |
| 355 | $narg=6 if (!defined($narg)); |
| 356 | $func .= " movq %rcx,%rdi\n" if ($narg>0); |
| 357 | $func .= " movq %rdx,%rsi\n" if ($narg>1); |
| 358 | $func .= " movq %r8,%rdx\n" if ($narg>2); |
| 359 | $func .= " movq %r9,%rcx\n" if ($narg>3); |
| 360 | $func .= " movq 40(%rsp),%r8\n" if ($narg>4); |
| 361 | $func .= " movq 48(%rsp),%r9\n" if ($narg>5); |
| 362 | } |
| 363 | $func; |
| 364 | } elsif ($self->{value} ne "$current_function->{name}") { |
| 365 | $self->{value} .= ":" if ($masm && $ret!~m/^\$/); |
| 366 | $self->{value} . ":"; |
| 367 | } elsif ($win64 && $current_function->{abi} eq "svr4") { |
| 368 | my $func = "$current_function->{name}" . |
| 369 | ($nasm ? ":" : "\tPROC $current_function->{scope}") . |
| 370 | "\n"; |
| 371 | $func .= " mov QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n"; |
| 372 | $func .= " mov QWORD${PTR}[16+rsp],rsi\n"; |
| 373 | $func .= " mov rax,rsp\n"; |
| 374 | $func .= "${decor}SEH_begin_$current_function->{name}:"; |
| 375 | $func .= ":" if ($masm); |
| 376 | $func .= "\n"; |
| 377 | my $narg = $current_function->{narg}; |
| 378 | $narg=6 if (!defined($narg)); |
| 379 | $func .= " mov rdi,rcx\n" if ($narg>0); |
| 380 | $func .= " mov rsi,rdx\n" if ($narg>1); |
| 381 | $func .= " mov rdx,r8\n" if ($narg>2); |
| 382 | $func .= " mov rcx,r9\n" if ($narg>3); |
| 383 | $func .= " mov r8,QWORD${PTR}[40+rsp]\n" if ($narg>4); |
| 384 | $func .= " mov r9,QWORD${PTR}[48+rsp]\n" if ($narg>5); |
| 385 | $func .= "\n"; |
| 386 | } else { |
| 387 | "$current_function->{name}". |
| 388 | ($nasm ? ":" : "\tPROC $current_function->{scope}"); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | { package expr; # pick up expressioins |
| 393 | sub re { |
| 394 | my $self = shift; # single instance is enough... |
| 395 | local *line = shift; |
| 396 | undef $ret; |
| 397 | |
| 398 | if ($line =~ /(^[^,]+)/) { |
| 399 | $self->{value} = $1; |
| 400 | $ret = $self; |
| 401 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 402 | |
| 403 | $self->{value} =~ s/\@PLT// if (!$elf); |
| 404 | $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 405 | $self->{value} =~ s/\.L/$decor/g; |
| 406 | } |
| 407 | $ret; |
| 408 | } |
| 409 | sub out { |
| 410 | my $self = shift; |
| 411 | if ($nasm && opcode->mnemonic()=~m/^j/) { |
| 412 | "NEAR ".$self->{value}; |
| 413 | } else { |
| 414 | $self->{value}; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | { package directive; # pick up directives, which start with . |
| 419 | sub re { |
| 420 | my $self = shift; # single instance is enough... |
| 421 | local *line = shift; |
| 422 | undef $ret; |
| 423 | my $dir; |
| 424 | my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: |
| 425 | ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, |
| 426 | "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, |
| 427 | "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, |
| 428 | "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, |
| 429 | "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, |
| 430 | "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, |
| 431 | "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, |
| 432 | "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); |
| 433 | |
| 434 | if ($line =~ /^\s*(\.\w+)/) { |
| 435 | $dir = $1; |
| 436 | $ret = $self; |
| 437 | undef $self->{value}; |
| 438 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
| 439 | |
| 440 | SWITCH: for ($dir) { |
| 441 | /\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) { |
| 442 | $dir="\t.long"; |
| 443 | $line=sprintf "0x%x,0x90000000",$opcode{$1}; |
| 444 | } |
| 445 | last; |
| 446 | }; |
| 447 | /\.global|\.globl|\.extern/ |
| 448 | && do { $globals{$line} = $prefix . $line; |
| 449 | $line = $globals{$line} if ($prefix); |
| 450 | last; |
| 451 | }; |
| 452 | /\.type/ && do { ($sym,$type,$narg) = split(',',$line); |
| 453 | if ($type eq "\@function") { |
| 454 | undef $current_function; |
| 455 | $current_function->{name} = $sym; |
| 456 | $current_function->{abi} = "svr4"; |
| 457 | $current_function->{narg} = $narg; |
| 458 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; |
| 459 | } elsif ($type eq "\@abi-omnipotent") { |
| 460 | undef $current_function; |
| 461 | $current_function->{name} = $sym; |
| 462 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE"; |
| 463 | } |
| 464 | $line =~ s/\@abi\-omnipotent/\@function/; |
| 465 | $line =~ s/\@function.*/\@function/; |
| 466 | last; |
| 467 | }; |
| 468 | /\.asciz/ && do { if ($line =~ /^"(.*)"$/) { |
| 469 | $dir = ".byte"; |
| 470 | $line = join(",",unpack("C*",$1),0); |
| 471 | } |
| 472 | last; |
| 473 | }; |
| 474 | /\.rva|\.long|\.quad/ |
| 475 | && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei; |
| 476 | $line =~ s/\.L/$decor/g; |
| 477 | last; |
| 478 | }; |
| 479 | } |
| 480 | |
| 481 | if ($gas) { |
| 482 | $self->{value} = $dir . "\t" . $line; |
| 483 | |
| 484 | if ($dir =~ /\.extern/) { |
| 485 | $self->{value} = ""; # swallow extern |
| 486 | } elsif (!$elf && $dir =~ /\.type/) { |
| 487 | $self->{value} = ""; |
| 488 | $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" . |
| 489 | (defined($globals{$1})?".scl 2;":".scl 3;") . |
| 490 | "\t.type 32;\t.endef" |
| 491 | if ($win64 && $line =~ /([^,]+),\@function/); |
| 492 | } elsif (!$elf && $dir =~ /\.size/) { |
| 493 | $self->{value} = ""; |
| 494 | if (defined($current_function)) { |
| 495 | $self->{value} .= "${decor}SEH_end_$current_function->{name}:" |
| 496 | if ($win64 && $current_function->{abi} eq "svr4"); |
| 497 | undef $current_function; |
| 498 | } |
| 499 | } elsif (!$elf && $dir =~ /\.align/) { |
| 500 | $self->{value} = ".p2align\t" . (log($line)/log(2)); |
| 501 | } elsif ($dir eq ".section") { |
| 502 | $current_segment=$line; |
| 503 | if (!$elf && $current_segment eq ".init") { |
| 504 | if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; } |
| 505 | elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; } |
| 506 | } |
| 507 | } elsif ($dir =~ /\.(text|data)/) { |
| 508 | $current_segment=".$1"; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 509 | } |
| 510 | $line = ""; |
| 511 | return $self; |
| 512 | } |
| 513 | |
| 514 | # non-gas case or nasm/masm |
| 515 | SWITCH: for ($dir) { |
| 516 | /\.text/ && do { my $v=undef; |
| 517 | if ($nasm) { |
| 518 | $v="section .text code align=64\n"; |
| 519 | } else { |
| 520 | $v="$current_segment\tENDS\n" if ($current_segment); |
| 521 | $current_segment = ".text\$"; |
| 522 | $v.="$current_segment\tSEGMENT "; |
| 523 | $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; |
| 524 | $v.=" 'CODE'"; |
| 525 | } |
| 526 | $self->{value} = $v; |
| 527 | last; |
| 528 | }; |
| 529 | /\.data/ && do { my $v=undef; |
| 530 | if ($nasm) { |
| 531 | $v="section .data data align=8\n"; |
| 532 | } else { |
| 533 | $v="$current_segment\tENDS\n" if ($current_segment); |
| 534 | $current_segment = "_DATA"; |
| 535 | $v.="$current_segment\tSEGMENT"; |
| 536 | } |
| 537 | $self->{value} = $v; |
| 538 | last; |
| 539 | }; |
| 540 | /\.section/ && do { my $v=undef; |
| 541 | $line =~ s/([^,]*).*/$1/; |
| 542 | $line = ".CRT\$XCU" if ($line eq ".init"); |
| 543 | if ($nasm) { |
| 544 | $v="section $line"; |
| 545 | if ($line=~/\.([px])data/) { |
| 546 | $v.=" rdata align="; |
| 547 | $v.=$1 eq "p"? 4 : 8; |
| 548 | } elsif ($line=~/\.CRT\$/i) { |
| 549 | $v.=" rdata align=8"; |
| 550 | } |
| 551 | } else { |
| 552 | $v="$current_segment\tENDS\n" if ($current_segment); |
| 553 | $v.="$line\tSEGMENT"; |
| 554 | if ($line=~/\.([px])data/) { |
| 555 | $v.=" READONLY"; |
| 556 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref); |
| 557 | } elsif ($line=~/\.CRT\$/i) { |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 558 | $v.=" READONLY DWORD"; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | $current_segment = $line; |
| 562 | $self->{value} = $v; |
| 563 | last; |
| 564 | }; |
| 565 | /\.extern/ && do { $self->{value} = "EXTERN\t".$line; |
| 566 | $self->{value} .= ":NEAR" if ($masm); |
| 567 | last; |
| 568 | }; |
| 569 | /\.globl|.global/ |
| 570 | && do { $self->{value} = $masm?"PUBLIC":"global"; |
| 571 | $self->{value} .= "\t".$line; |
| 572 | last; |
| 573 | }; |
| 574 | /\.size/ && do { if (defined($current_function)) { |
| 575 | undef $self->{value}; |
| 576 | if ($current_function->{abi} eq "svr4") { |
| 577 | $self->{value}="${decor}SEH_end_$current_function->{name}:"; |
| 578 | $self->{value}.=":\n" if($masm); |
| 579 | } |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 580 | $self->{value}.="$current_function->{name}\tENDP" if($masm); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 581 | undef $current_function; |
| 582 | } |
| 583 | last; |
| 584 | }; |
| 585 | /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; |
| 586 | /\.(value|long|rva|quad)/ |
| 587 | && do { my $sz = substr($1,0,1); |
| 588 | my @arr = split(/,\s*/,$line); |
| 589 | my $last = pop(@arr); |
| 590 | my $conv = sub { my $var=shift; |
| 591 | $var=~s/^(0b[0-1]+)/oct($1)/eig; |
| 592 | $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm); |
| 593 | if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva")) |
| 594 | { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; } |
| 595 | $var; |
| 596 | }; |
| 597 | |
| 598 | $sz =~ tr/bvlrq/BWDDQ/; |
| 599 | $self->{value} = "\tD$sz\t"; |
| 600 | for (@arr) { $self->{value} .= &$conv($_).","; } |
| 601 | $self->{value} .= &$conv($last); |
| 602 | last; |
| 603 | }; |
| 604 | /\.byte/ && do { my @str=split(/,\s*/,$line); |
| 605 | map(s/(0b[0-1]+)/oct($1)/eig,@str); |
| 606 | map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm); |
| 607 | while ($#str>15) { |
| 608 | $self->{value}.="DB\t" |
| 609 | .join(",",@str[0..15])."\n"; |
| 610 | foreach (0..15) { shift @str; } |
| 611 | } |
| 612 | $self->{value}.="DB\t" |
| 613 | .join(",",@str) if (@str); |
| 614 | last; |
| 615 | }; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 616 | } |
| 617 | $line = ""; |
| 618 | } |
| 619 | |
| 620 | $ret; |
| 621 | } |
| 622 | sub out { |
| 623 | my $self = shift; |
| 624 | $self->{value}; |
| 625 | } |
| 626 | } |
| 627 | |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 628 | if ($nasm) { |
| 629 | print <<___; |
| 630 | default rel |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 631 | ___ |
| 632 | } elsif ($masm) { |
| 633 | print <<___; |
| 634 | OPTION DOTNAME |
| 635 | ___ |
| 636 | } |
| 637 | while($line=<>) { |
| 638 | |
| 639 | chomp($line); |
| 640 | |
| 641 | $line =~ s|[#!].*$||; # get rid of asm-style comments... |
| 642 | $line =~ s|/\*.*\*/||; # ... and C-style comments... |
| 643 | $line =~ s|^\s+||; # ... and skip white spaces in beginning |
| 644 | |
| 645 | undef $label; |
| 646 | undef $opcode; |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 647 | undef $sz; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 648 | undef @args; |
| 649 | |
| 650 | if ($label=label->re(\$line)) { print $label->out(); } |
| 651 | |
| 652 | if (directive->re(\$line)) { |
| 653 | printf "%s",directive->out(); |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 654 | } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: while (1) { |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 655 | my $arg; |
| 656 | |
| 657 | if ($arg=register->re(\$line)) { opcode->size($arg->size()); } |
| 658 | elsif ($arg=const->re(\$line)) { } |
| 659 | elsif ($arg=ea->re(\$line)) { } |
| 660 | elsif ($arg=expr->re(\$line)) { } |
| 661 | else { last ARGUMENT; } |
| 662 | |
| 663 | push @args,$arg; |
| 664 | |
| 665 | last ARGUMENT if ($line !~ /^,/); |
| 666 | |
| 667 | $line =~ s/^,\s*//; |
| 668 | } # ARGUMENT: |
| 669 | |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 670 | $sz=opcode->size(); |
| 671 | |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 672 | if ($#args>=0) { |
| 673 | my $insn; |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 674 | if ($gas) { |
| 675 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 676 | } else { |
| 677 | $insn = $opcode->out(); |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 678 | $insn .= $sz if (map($_->out() =~ /x?mm/,@args)); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 679 | @args = reverse(@args); |
| 680 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 681 | } |
Alexandre Savard | 7541067 | 2012-08-08 09:50:01 -0400 | [diff] [blame] | 682 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); |
Alexandre Savard | 1b09e31 | 2012-08-07 20:33:29 -0400 | [diff] [blame] | 683 | } else { |
| 684 | printf "\t%s",$opcode->out(); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | print $line,"\n"; |
| 689 | } |
| 690 | |
| 691 | print "\n$current_segment\tENDS\n" if ($current_segment && $masm); |
| 692 | print "END\n" if ($masm); |
| 693 | |
| 694 | close STDOUT; |
| 695 | |
| 696 | ################################################# |
| 697 | # Cross-reference x86_64 ABI "card" |
| 698 | # |
| 699 | # Unix Win64 |
| 700 | # %rax * * |
| 701 | # %rbx - - |
| 702 | # %rcx #4 #1 |
| 703 | # %rdx #3 #2 |
| 704 | # %rsi #2 - |
| 705 | # %rdi #1 - |
| 706 | # %rbp - - |
| 707 | # %rsp - - |
| 708 | # %r8 #5 #3 |
| 709 | # %r9 #6 #4 |
| 710 | # %r10 * * |
| 711 | # %r11 * * |
| 712 | # %r12 - - |
| 713 | # %r13 - - |
| 714 | # %r14 - - |
| 715 | # %r15 - - |
| 716 | # |
| 717 | # (*) volatile register |
| 718 | # (-) preserved by callee |
| 719 | # (#) Nth argument, volatile |
| 720 | # |
| 721 | # In Unix terms top of stack is argument transfer area for arguments |
| 722 | # which could not be accomodated in registers. Or in other words 7th |
| 723 | # [integer] argument resides at 8(%rsp) upon function entry point. |
| 724 | # 128 bytes above %rsp constitute a "red zone" which is not touched |
| 725 | # by signal handlers and can be used as temporal storage without |
| 726 | # allocating a frame. |
| 727 | # |
| 728 | # In Win64 terms N*8 bytes on top of stack is argument transfer area, |
| 729 | # which belongs to/can be overwritten by callee. N is the number of |
| 730 | # arguments passed to callee, *but* not less than 4! This means that |
| 731 | # upon function entry point 5th argument resides at 40(%rsp), as well |
| 732 | # as that 32 bytes from 8(%rsp) can always be used as temporal |
| 733 | # storage [without allocating a frame]. One can actually argue that |
| 734 | # one can assume a "red zone" above stack pointer under Win64 as well. |
| 735 | # Point is that at apparently no occasion Windows kernel would alter |
| 736 | # the area above user stack pointer in true asynchronous manner... |
| 737 | # |
| 738 | # All the above means that if assembler programmer adheres to Unix |
| 739 | # register and stack layout, but disregards the "red zone" existense, |
| 740 | # it's possible to use following prologue and epilogue to "gear" from |
| 741 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments. |
| 742 | # |
| 743 | # omnipotent_function: |
| 744 | # ifdef WIN64 |
| 745 | # movq %rdi,8(%rsp) |
| 746 | # movq %rsi,16(%rsp) |
| 747 | # movq %rcx,%rdi ; if 1st argument is actually present |
| 748 | # movq %rdx,%rsi ; if 2nd argument is actually ... |
| 749 | # movq %r8,%rdx ; if 3rd argument is ... |
| 750 | # movq %r9,%rcx ; if 4th argument ... |
| 751 | # movq 40(%rsp),%r8 ; if 5th ... |
| 752 | # movq 48(%rsp),%r9 ; if 6th ... |
| 753 | # endif |
| 754 | # ... |
| 755 | # ifdef WIN64 |
| 756 | # movq 8(%rsp),%rdi |
| 757 | # movq 16(%rsp),%rsi |
| 758 | # endif |
| 759 | # ret |
| 760 | # |
| 761 | ################################################# |
| 762 | # Win64 SEH, Structured Exception Handling. |
| 763 | # |
| 764 | # Unlike on Unix systems(*) lack of Win64 stack unwinding information |
| 765 | # has undesired side-effect at run-time: if an exception is raised in |
| 766 | # assembler subroutine such as those in question (basically we're |
| 767 | # referring to segmentation violations caused by malformed input |
| 768 | # parameters), the application is briskly terminated without invoking |
| 769 | # any exception handlers, most notably without generating memory dump |
| 770 | # or any user notification whatsoever. This poses a problem. It's |
| 771 | # possible to address it by registering custom language-specific |
| 772 | # handler that would restore processor context to the state at |
| 773 | # subroutine entry point and return "exception is not handled, keep |
| 774 | # unwinding" code. Writing such handler can be a challenge... But it's |
| 775 | # doable, though requires certain coding convention. Consider following |
| 776 | # snippet: |
| 777 | # |
| 778 | # .type function,@function |
| 779 | # function: |
| 780 | # movq %rsp,%rax # copy rsp to volatile register |
| 781 | # pushq %r15 # save non-volatile registers |
| 782 | # pushq %rbx |
| 783 | # pushq %rbp |
| 784 | # movq %rsp,%r11 |
| 785 | # subq %rdi,%r11 # prepare [variable] stack frame |
| 786 | # andq $-64,%r11 |
| 787 | # movq %rax,0(%r11) # check for exceptions |
| 788 | # movq %r11,%rsp # allocate [variable] stack frame |
| 789 | # movq %rax,0(%rsp) # save original rsp value |
| 790 | # magic_point: |
| 791 | # ... |
| 792 | # movq 0(%rsp),%rcx # pull original rsp value |
| 793 | # movq -24(%rcx),%rbp # restore non-volatile registers |
| 794 | # movq -16(%rcx),%rbx |
| 795 | # movq -8(%rcx),%r15 |
| 796 | # movq %rcx,%rsp # restore original rsp |
| 797 | # ret |
| 798 | # .size function,.-function |
| 799 | # |
| 800 | # The key is that up to magic_point copy of original rsp value remains |
| 801 | # in chosen volatile register and no non-volatile register, except for |
| 802 | # rsp, is modified. While past magic_point rsp remains constant till |
| 803 | # the very end of the function. In this case custom language-specific |
| 804 | # exception handler would look like this: |
| 805 | # |
| 806 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, |
| 807 | # CONTEXT *context,DISPATCHER_CONTEXT *disp) |
| 808 | # { ULONG64 *rsp = (ULONG64 *)context->Rax; |
| 809 | # if (context->Rip >= magic_point) |
| 810 | # { rsp = ((ULONG64 **)context->Rsp)[0]; |
| 811 | # context->Rbp = rsp[-3]; |
| 812 | # context->Rbx = rsp[-2]; |
| 813 | # context->R15 = rsp[-1]; |
| 814 | # } |
| 815 | # context->Rsp = (ULONG64)rsp; |
| 816 | # context->Rdi = rsp[1]; |
| 817 | # context->Rsi = rsp[2]; |
| 818 | # |
| 819 | # memcpy (disp->ContextRecord,context,sizeof(CONTEXT)); |
| 820 | # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase, |
| 821 | # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord, |
| 822 | # &disp->HandlerData,&disp->EstablisherFrame,NULL); |
| 823 | # return ExceptionContinueSearch; |
| 824 | # } |
| 825 | # |
| 826 | # It's appropriate to implement this handler in assembler, directly in |
| 827 | # function's module. In order to do that one has to know members' |
| 828 | # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant |
| 829 | # values. Here they are: |
| 830 | # |
| 831 | # CONTEXT.Rax 120 |
| 832 | # CONTEXT.Rcx 128 |
| 833 | # CONTEXT.Rdx 136 |
| 834 | # CONTEXT.Rbx 144 |
| 835 | # CONTEXT.Rsp 152 |
| 836 | # CONTEXT.Rbp 160 |
| 837 | # CONTEXT.Rsi 168 |
| 838 | # CONTEXT.Rdi 176 |
| 839 | # CONTEXT.R8 184 |
| 840 | # CONTEXT.R9 192 |
| 841 | # CONTEXT.R10 200 |
| 842 | # CONTEXT.R11 208 |
| 843 | # CONTEXT.R12 216 |
| 844 | # CONTEXT.R13 224 |
| 845 | # CONTEXT.R14 232 |
| 846 | # CONTEXT.R15 240 |
| 847 | # CONTEXT.Rip 248 |
| 848 | # CONTEXT.Xmm6 512 |
| 849 | # sizeof(CONTEXT) 1232 |
| 850 | # DISPATCHER_CONTEXT.ControlPc 0 |
| 851 | # DISPATCHER_CONTEXT.ImageBase 8 |
| 852 | # DISPATCHER_CONTEXT.FunctionEntry 16 |
| 853 | # DISPATCHER_CONTEXT.EstablisherFrame 24 |
| 854 | # DISPATCHER_CONTEXT.TargetIp 32 |
| 855 | # DISPATCHER_CONTEXT.ContextRecord 40 |
| 856 | # DISPATCHER_CONTEXT.LanguageHandler 48 |
| 857 | # DISPATCHER_CONTEXT.HandlerData 56 |
| 858 | # UNW_FLAG_NHANDLER 0 |
| 859 | # ExceptionContinueSearch 1 |
| 860 | # |
| 861 | # In order to tie the handler to the function one has to compose |
| 862 | # couple of structures: one for .xdata segment and one for .pdata. |
| 863 | # |
| 864 | # UNWIND_INFO structure for .xdata segment would be |
| 865 | # |
| 866 | # function_unwind_info: |
| 867 | # .byte 9,0,0,0 |
| 868 | # .rva handler |
| 869 | # |
| 870 | # This structure designates exception handler for a function with |
| 871 | # zero-length prologue, no stack frame or frame register. |
| 872 | # |
| 873 | # To facilitate composing of .pdata structures, auto-generated "gear" |
| 874 | # prologue copies rsp value to rax and denotes next instruction with |
| 875 | # .LSEH_begin_{function_name} label. This essentially defines the SEH |
| 876 | # styling rule mentioned in the beginning. Position of this label is |
| 877 | # chosen in such manner that possible exceptions raised in the "gear" |
| 878 | # prologue would be accounted to caller and unwound from latter's frame. |
| 879 | # End of function is marked with respective .LSEH_end_{function_name} |
| 880 | # label. To summarize, .pdata segment would contain |
| 881 | # |
| 882 | # .rva .LSEH_begin_function |
| 883 | # .rva .LSEH_end_function |
| 884 | # .rva function_unwind_info |
| 885 | # |
| 886 | # Reference to functon_unwind_info from .xdata segment is the anchor. |
| 887 | # In case you wonder why references are 32-bit .rvas and not 64-bit |
| 888 | # .quads. References put into these two segments are required to be |
| 889 | # *relative* to the base address of the current binary module, a.k.a. |
| 890 | # image base. No Win64 module, be it .exe or .dll, can be larger than |
| 891 | # 2GB and thus such relative references can be and are accommodated in |
| 892 | # 32 bits. |
| 893 | # |
| 894 | # Having reviewed the example function code, one can argue that "movq |
| 895 | # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix |
| 896 | # rax would contain an undefined value. If this "offends" you, use |
| 897 | # another register and refrain from modifying rax till magic_point is |
| 898 | # reached, i.e. as if it was a non-volatile register. If more registers |
| 899 | # are required prior [variable] frame setup is completed, note that |
| 900 | # nobody says that you can have only one "magic point." You can |
| 901 | # "liberate" non-volatile registers by denoting last stack off-load |
| 902 | # instruction and reflecting it in finer grade unwind logic in handler. |
| 903 | # After all, isn't it why it's called *language-specific* handler... |
| 904 | # |
| 905 | # Attentive reader can notice that exceptions would be mishandled in |
| 906 | # auto-generated "gear" epilogue. Well, exception effectively can't |
| 907 | # occur there, because if memory area used by it was subject to |
| 908 | # segmentation violation, then it would be raised upon call to the |
| 909 | # function (and as already mentioned be accounted to caller, which is |
| 910 | # not a problem). If you're still not comfortable, then define tail |
| 911 | # "magic point" just prior ret instruction and have handler treat it... |
| 912 | # |
| 913 | # (*) Note that we're talking about run-time, not debug-time. Lack of |
| 914 | # unwind information makes debugging hard on both Windows and |
| 915 | # Unix. "Unlike" referes to the fact that on Unix signal handler |
| 916 | # will always be invoked, core dumped and appropriate exit code |
| 917 | # returned to parent (for user notification). |