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