1 | #! /usr/bin/env perl
|
---|
2 | # Copyright 2005-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | #
|
---|
4 | # Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | # this file except in compliance with the License. You can obtain a copy
|
---|
6 | # in the file LICENSE in the source distribution or at
|
---|
7 | # https://www.openssl.org/source/license.html
|
---|
8 |
|
---|
9 |
|
---|
10 | # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
|
---|
11 | #
|
---|
12 | # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
|
---|
13 | # format is way easier to parse. Because it's simpler to "gear" from
|
---|
14 | # Unix ABI to Windows one [see cross-reference "card" at the end of
|
---|
15 | # file]. Because Linux targets were available first...
|
---|
16 | #
|
---|
17 | # In addition the script also "distills" code suitable for GNU
|
---|
18 | # assembler, so that it can be compiled with more rigid assemblers,
|
---|
19 | # such as Solaris /usr/ccs/bin/as.
|
---|
20 | #
|
---|
21 | # This translator is not designed to convert *arbitrary* assembler
|
---|
22 | # code from AT&T format to MASM one. It's designed to convert just
|
---|
23 | # enough to provide for dual-ABI OpenSSL modules development...
|
---|
24 | # There *are* limitations and you might have to modify your assembler
|
---|
25 | # code or this script to achieve the desired result...
|
---|
26 | #
|
---|
27 | # Currently recognized limitations:
|
---|
28 | #
|
---|
29 | # - can't use multiple ops per line;
|
---|
30 | #
|
---|
31 | # Dual-ABI styling rules.
|
---|
32 | #
|
---|
33 | # 1. Adhere to Unix register and stack layout [see cross-reference
|
---|
34 | # ABI "card" at the end for explanation].
|
---|
35 | # 2. Forget about "red zone," stick to more traditional blended
|
---|
36 | # stack frame allocation. If volatile storage is actually required
|
---|
37 | # that is. If not, just leave the stack as is.
|
---|
38 | # 3. Functions tagged with ".type name,@function" get crafted with
|
---|
39 | # unified Win64 prologue and epilogue automatically. If you want
|
---|
40 | # to take care of ABI differences yourself, tag functions as
|
---|
41 | # ".type name,@abi-omnipotent" instead.
|
---|
42 | # 4. To optimize the Win64 prologue you can specify number of input
|
---|
43 | # arguments as ".type name,@function,N." Keep in mind that if N is
|
---|
44 | # larger than 6, then you *have to* write "abi-omnipotent" code,
|
---|
45 | # because >6 cases can't be addressed with unified prologue.
|
---|
46 | # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
|
---|
47 | # (sorry about latter).
|
---|
48 | # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
|
---|
49 | # required to identify the spots, where to inject Win64 epilogue!
|
---|
50 | # But on the pros, it's then prefixed with rep automatically:-)
|
---|
51 | # 7. Stick to explicit ip-relative addressing. If you have to use
|
---|
52 | # GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
|
---|
53 | # Both are recognized and translated to proper Win64 addressing
|
---|
54 | # modes.
|
---|
55 | #
|
---|
56 | # 8. In order to provide for structured exception handling unified
|
---|
57 | # Win64 prologue copies %rsp value to %rax. For further details
|
---|
58 | # see SEH paragraph at the end.
|
---|
59 | # 9. .init segment is allowed to contain calls to functions only.
|
---|
60 | # a. If function accepts more than 4 arguments *and* >4th argument
|
---|
61 | # is declared as non 64-bit value, do clear its upper part.
|
---|
62 | |
---|
63 |
|
---|
64 |
|
---|
65 | use strict;
|
---|
66 |
|
---|
67 | my $flavour = shift;
|
---|
68 | my $output = shift;
|
---|
69 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
|
---|
70 |
|
---|
71 | open STDOUT,">$output" || die "can't open $output: $!"
|
---|
72 | if (defined($output));
|
---|
73 |
|
---|
74 | my $gas=1; $gas=0 if ($output =~ /\.asm$/);
|
---|
75 | my $elf=1; $elf=0 if (!$gas);
|
---|
76 | my $win64=0;
|
---|
77 | my $prefix="";
|
---|
78 | my $decor=".L";
|
---|
79 |
|
---|
80 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005
|
---|
81 | my $masm=0;
|
---|
82 | my $PTR=" PTR";
|
---|
83 |
|
---|
84 | my $nasmref=2.03;
|
---|
85 | my $nasm=0;
|
---|
86 |
|
---|
87 | # GNU as indicator, as opposed to $gas, which indicates acceptable
|
---|
88 | # syntax
|
---|
89 | my $gnuas=0;
|
---|
90 |
|
---|
91 | if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1;
|
---|
92 | $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
|
---|
93 | $prefix =~ s|\R$||; # Better chomp
|
---|
94 | }
|
---|
95 | elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
|
---|
96 | elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
|
---|
97 | elsif ($flavour eq "nasm") { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
|
---|
98 | elsif (!$gas)
|
---|
99 | { if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
|
---|
100 | { $nasm = $1 + $2*0.01; $PTR=""; }
|
---|
101 | elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
|
---|
102 | { $masm = $1 + $2*2**-16 + $4*2**-32; }
|
---|
103 | die "no assembler found on %PATH%" if (!($nasm || $masm));
|
---|
104 | $win64=1;
|
---|
105 | $elf=0;
|
---|
106 | $decor="\$L\$";
|
---|
107 | }
|
---|
108 | # Find out if we're using GNU as
|
---|
109 | =pod VBOX: Begin commenting out
|
---|
110 | elsif (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
|
---|
111 | =~ /GNU assembler version ([2-9]\.[0-9]+)/)
|
---|
112 | {
|
---|
113 | $gnuas=1;
|
---|
114 | }
|
---|
115 | elsif (`$ENV{CC} --version 2>/dev/null`
|
---|
116 | =~ /(clang .*|Intel.*oneAPI .*)/)
|
---|
117 | {
|
---|
118 | $gnuas=1;
|
---|
119 | }
|
---|
120 | =cut
|
---|
121 | else
|
---|
122 | {
|
---|
123 | $gnuas=1;
|
---|
124 | }
|
---|
125 |
|
---|
126 | my $cet_property;
|
---|
127 | if ($flavour =~ /elf/) {
|
---|
128 | # Always generate .note.gnu.property section for ELF outputs to
|
---|
129 | # mark Intel CET support since all input files must be marked
|
---|
130 | # with Intel CET support in order for linker to mark output with
|
---|
131 | # Intel CET support.
|
---|
132 | my $p2align=3; $p2align=2 if ($flavour eq "elf32");
|
---|
133 | my $section='.note.gnu.property, #alloc';
|
---|
134 | $section='".note.gnu.property", "a"' if $gnuas;
|
---|
135 | $cet_property = <<_____;
|
---|
136 | .section $section
|
---|
137 | .p2align $p2align
|
---|
138 | .long 1f - 0f
|
---|
139 | .long 4f - 1f
|
---|
140 | .long 5
|
---|
141 | 0:
|
---|
142 | # "GNU" encoded with .byte, since .asciz isn't supported
|
---|
143 | # on Solaris.
|
---|
144 | .byte 0x47
|
---|
145 | .byte 0x4e
|
---|
146 | .byte 0x55
|
---|
147 | .byte 0
|
---|
148 | 1:
|
---|
149 | .p2align $p2align
|
---|
150 | .long 0xc0000002
|
---|
151 | .long 3f - 2f
|
---|
152 | 2:
|
---|
153 | .long 3
|
---|
154 | 3:
|
---|
155 | .p2align $p2align
|
---|
156 | 4:
|
---|
157 | _____
|
---|
158 | }
|
---|
159 |
|
---|
160 | my $current_segment;
|
---|
161 | my $current_function;
|
---|
162 | my %globals;
|
---|
163 |
|
---|
164 | { package opcode; # pick up opcodes
|
---|
165 | sub re {
|
---|
166 | my ($class, $line) = @_;
|
---|
167 | my $self = {};
|
---|
168 | my $ret;
|
---|
169 |
|
---|
170 | if ($$line =~ /^([a-z][a-z0-9]*)/i) {
|
---|
171 | bless $self,$class;
|
---|
172 | $self->{op} = $1;
|
---|
173 | $ret = $self;
|
---|
174 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
175 |
|
---|
176 | undef $self->{sz};
|
---|
177 | if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain...
|
---|
178 | $self->{op} = $1;
|
---|
179 | $self->{sz} = $2;
|
---|
180 | } elsif ($self->{op} =~ /call|jmp/) {
|
---|
181 | $self->{sz} = "";
|
---|
182 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
|
---|
183 | $self->{sz} = "";
|
---|
184 | } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
|
---|
185 | $self->{sz} = "";
|
---|
186 | } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
|
---|
187 | $self->{sz} = "";
|
---|
188 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
|
---|
189 | $self->{op} = $1;
|
---|
190 | $self->{sz} = $2;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | $ret;
|
---|
194 | }
|
---|
195 | sub size {
|
---|
196 | my ($self, $sz) = @_;
|
---|
197 | $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
|
---|
198 | $self->{sz};
|
---|
199 | }
|
---|
200 | sub out {
|
---|
201 | my $self = shift;
|
---|
202 | if ($gas) {
|
---|
203 | if ($self->{op} eq "movz") { # movz is pain...
|
---|
204 | sprintf "%s%s%s",$self->{op},$self->{sz},shift;
|
---|
205 | } elsif ($self->{op} =~ /^set/) {
|
---|
206 | "$self->{op}";
|
---|
207 | } elsif ($self->{op} eq "ret") {
|
---|
208 | my $epilogue = "";
|
---|
209 | if ($win64 && $current_function->{abi} eq "svr4") {
|
---|
210 | $epilogue = "movq 8(%rsp),%rdi\n\t" .
|
---|
211 | "movq 16(%rsp),%rsi\n\t";
|
---|
212 | }
|
---|
213 | $epilogue . ".byte 0xf3,0xc3";
|
---|
214 | } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
|
---|
215 | ".p2align\t3\n\t.quad";
|
---|
216 | } else {
|
---|
217 | "$self->{op}$self->{sz}";
|
---|
218 | }
|
---|
219 | } else {
|
---|
220 | $self->{op} =~ s/^movz/movzx/;
|
---|
221 | if ($self->{op} eq "ret") {
|
---|
222 | $self->{op} = "";
|
---|
223 | if ($win64 && $current_function->{abi} eq "svr4") {
|
---|
224 | $self->{op} = "mov rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
|
---|
225 | "mov rsi,QWORD$PTR\[16+rsp\]\n\t";
|
---|
226 | }
|
---|
227 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
|
---|
228 | } elsif ($self->{op} =~ /^(pop|push)f/) {
|
---|
229 | $self->{op} .= $self->{sz};
|
---|
230 | } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
|
---|
231 | $self->{op} = "\tDQ";
|
---|
232 | }
|
---|
233 | $self->{op};
|
---|
234 | }
|
---|
235 | }
|
---|
236 | sub mnemonic {
|
---|
237 | my ($self, $op) = @_;
|
---|
238 | $self->{op}=$op if (defined($op));
|
---|
239 | $self->{op};
|
---|
240 | }
|
---|
241 | }
|
---|
242 | { package const; # pick up constants, which start with $
|
---|
243 | sub re {
|
---|
244 | my ($class, $line) = @_;
|
---|
245 | my $self = {};
|
---|
246 | my $ret;
|
---|
247 |
|
---|
248 | if ($$line =~ /^\$([^,]+)/) {
|
---|
249 | bless $self, $class;
|
---|
250 | $self->{value} = $1;
|
---|
251 | $ret = $self;
|
---|
252 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
253 | }
|
---|
254 | $ret;
|
---|
255 | }
|
---|
256 | sub out {
|
---|
257 | my $self = shift;
|
---|
258 |
|
---|
259 | $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
|
---|
260 | if ($gas) {
|
---|
261 | # Solaris /usr/ccs/bin/as can't handle multiplications
|
---|
262 | # in $self->{value}
|
---|
263 | my $value = $self->{value};
|
---|
264 | no warnings; # oct might complain about overflow, ignore here...
|
---|
265 | $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
|
---|
266 | if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
|
---|
267 | $self->{value} = $value;
|
---|
268 | }
|
---|
269 | sprintf "\$%s",$self->{value};
|
---|
270 | } else {
|
---|
271 | my $value = $self->{value};
|
---|
272 | $value =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
|
---|
273 | sprintf "%s",$value;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 | { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
|
---|
278 |
|
---|
279 | my %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR",
|
---|
280 | l=>"DWORD$PTR", d=>"DWORD$PTR",
|
---|
281 | q=>"QWORD$PTR", o=>"OWORD$PTR",
|
---|
282 | x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
|
---|
283 | z=>"ZMMWORD$PTR" ) if (!$gas);
|
---|
284 |
|
---|
285 | sub re {
|
---|
286 | my ($class, $line, $opcode) = @_;
|
---|
287 | my $self = {};
|
---|
288 | my $ret;
|
---|
289 |
|
---|
290 | # optional * ----vvv--- appears in indirect jmp/call
|
---|
291 | if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
|
---|
292 | bless $self, $class;
|
---|
293 | $self->{asterisk} = $1;
|
---|
294 | $self->{label} = $2;
|
---|
295 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
|
---|
296 | $self->{scale} = 1 if (!defined($self->{scale}));
|
---|
297 | $self->{opmask} = $4;
|
---|
298 | $ret = $self;
|
---|
299 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
300 |
|
---|
301 | if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
|
---|
302 | die if ($opcode->mnemonic() ne "mov");
|
---|
303 | $opcode->mnemonic("lea");
|
---|
304 | }
|
---|
305 | $self->{base} =~ s/^%//;
|
---|
306 | $self->{index} =~ s/^%// if (defined($self->{index}));
|
---|
307 | $self->{opcode} = $opcode;
|
---|
308 | }
|
---|
309 | $ret;
|
---|
310 | }
|
---|
311 | sub size {}
|
---|
312 | sub out {
|
---|
313 | my ($self, $sz) = @_;
|
---|
314 |
|
---|
315 | $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
316 | $self->{label} =~ s/\.L/$decor/g;
|
---|
317 |
|
---|
318 | # Silently convert all EAs to 64-bit. This is required for
|
---|
319 | # elder GNU assembler and results in more compact code,
|
---|
320 | # *but* most importantly AES module depends on this feature!
|
---|
321 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
|
---|
322 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
|
---|
323 |
|
---|
324 | # Solaris /usr/ccs/bin/as can't handle multiplications
|
---|
325 | # in $self->{label}...
|
---|
326 | use integer;
|
---|
327 | $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
|
---|
328 | $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
|
---|
329 |
|
---|
330 | # Some assemblers insist on signed presentation of 32-bit
|
---|
331 | # offsets, but sign extension is a tricky business in perl...
|
---|
332 | if ((1<<31)<<1) {
|
---|
333 | $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
|
---|
334 | } else {
|
---|
335 | $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
|
---|
336 | }
|
---|
337 |
|
---|
338 | # if base register is %rbp or %r13, see if it's possible to
|
---|
339 | # flip base and index registers [for better performance]
|
---|
340 | if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
|
---|
341 | $self->{base} =~ /(rbp|r13)/) {
|
---|
342 | $self->{base} = $self->{index}; $self->{index} = $1;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if ($gas) {
|
---|
346 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64");
|
---|
347 |
|
---|
348 | if (defined($self->{index})) {
|
---|
349 | sprintf "%s%s(%s,%%%s,%d)%s",
|
---|
350 | $self->{asterisk},$self->{label},
|
---|
351 | $self->{base}?"%$self->{base}":"",
|
---|
352 | $self->{index},$self->{scale},
|
---|
353 | $self->{opmask};
|
---|
354 | } else {
|
---|
355 | sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
|
---|
356 | $self->{base},$self->{opmask};
|
---|
357 | }
|
---|
358 | } else {
|
---|
359 | $self->{label} =~ s/\./\$/g;
|
---|
360 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
|
---|
361 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
|
---|
362 |
|
---|
363 | my $mnemonic = $self->{opcode}->mnemonic();
|
---|
364 | ($self->{asterisk}) && ($sz="q") ||
|
---|
365 | ($mnemonic =~ /^v?mov([qd])$/) && ($sz=$1) ||
|
---|
366 | ($mnemonic =~ /^v?pinsr([qdwb])$/) && ($sz=$1) ||
|
---|
367 | ($mnemonic =~ /^vpbroadcast([qdwb])$/) && ($sz=$1) ||
|
---|
368 | ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/) && ($sz="x");
|
---|
369 |
|
---|
370 | $self->{opmask} =~ s/%(k[0-7])/$1/;
|
---|
371 |
|
---|
372 | if (defined($self->{index})) {
|
---|
373 | sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
|
---|
374 | $self->{label}?"$self->{label}+":"",
|
---|
375 | $self->{index},$self->{scale},
|
---|
376 | $self->{base}?"+$self->{base}":"",
|
---|
377 | $self->{opmask};
|
---|
378 | } elsif ($self->{base} eq "rip") {
|
---|
379 | sprintf "%s[%s]",$szmap{$sz},$self->{label};
|
---|
380 | } else {
|
---|
381 | sprintf "%s[%s%s]%s", $szmap{$sz},
|
---|
382 | $self->{label}?"$self->{label}+":"",
|
---|
383 | $self->{base},$self->{opmask};
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|
388 | { package register; # pick up registers, which start with %.
|
---|
389 | sub re {
|
---|
390 | my ($class, $line, $opcode) = @_;
|
---|
391 | my $self = {};
|
---|
392 | my $ret;
|
---|
393 |
|
---|
394 | # optional * ----vvv--- appears in indirect jmp/call
|
---|
395 | if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
|
---|
396 | bless $self,$class;
|
---|
397 | $self->{asterisk} = $1;
|
---|
398 | $self->{value} = $2;
|
---|
399 | $self->{opmask} = $3;
|
---|
400 | $opcode->size($self->size());
|
---|
401 | $ret = $self;
|
---|
402 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
403 | }
|
---|
404 | $ret;
|
---|
405 | }
|
---|
406 | sub size {
|
---|
407 | my $self = shift;
|
---|
408 | my $ret;
|
---|
409 |
|
---|
410 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
|
---|
411 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
|
---|
412 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
|
---|
413 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
|
---|
414 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
|
---|
415 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
|
---|
416 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
|
---|
417 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
|
---|
418 |
|
---|
419 | $ret;
|
---|
420 | }
|
---|
421 | sub out {
|
---|
422 | my $self = shift;
|
---|
423 | if ($gas) { sprintf "%s%%%s%s", $self->{asterisk},
|
---|
424 | $self->{value},
|
---|
425 | $self->{opmask}; }
|
---|
426 | else { $self->{opmask} =~ s/%(k[0-7])/$1/;
|
---|
427 | $self->{value}.$self->{opmask}; }
|
---|
428 | }
|
---|
429 | }
|
---|
430 | { package label; # pick up labels, which end with :
|
---|
431 | sub re {
|
---|
432 | my ($class, $line) = @_;
|
---|
433 | my $self = {};
|
---|
434 | my $ret;
|
---|
435 |
|
---|
436 | if ($$line =~ /(^[\.\w]+)\:/) {
|
---|
437 | bless $self,$class;
|
---|
438 | $self->{value} = $1;
|
---|
439 | $ret = $self;
|
---|
440 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
441 |
|
---|
442 | $self->{value} =~ s/^\.L/$decor/;
|
---|
443 | }
|
---|
444 | $ret;
|
---|
445 | }
|
---|
446 | sub out {
|
---|
447 | my $self = shift;
|
---|
448 |
|
---|
449 | if ($gas) {
|
---|
450 | my $func = ($globals{$self->{value}} or $self->{value}) . ":";
|
---|
451 | if ($win64 && $current_function->{name} eq $self->{value}
|
---|
452 | && $current_function->{abi} eq "svr4") {
|
---|
453 | $func .= "\n";
|
---|
454 | $func .= " movq %rdi,8(%rsp)\n";
|
---|
455 | $func .= " movq %rsi,16(%rsp)\n";
|
---|
456 | $func .= " movq %rsp,%rax\n";
|
---|
457 | $func .= "${decor}SEH_begin_$current_function->{name}:\n";
|
---|
458 | my $narg = $current_function->{narg};
|
---|
459 | $narg=6 if (!defined($narg));
|
---|
460 | $func .= " movq %rcx,%rdi\n" if ($narg>0);
|
---|
461 | $func .= " movq %rdx,%rsi\n" if ($narg>1);
|
---|
462 | $func .= " movq %r8,%rdx\n" if ($narg>2);
|
---|
463 | $func .= " movq %r9,%rcx\n" if ($narg>3);
|
---|
464 | $func .= " movq 40(%rsp),%r8\n" if ($narg>4);
|
---|
465 | $func .= " movq 48(%rsp),%r9\n" if ($narg>5);
|
---|
466 | }
|
---|
467 | $func;
|
---|
468 | } elsif ($self->{value} ne "$current_function->{name}") {
|
---|
469 | # Make all labels in masm global.
|
---|
470 | $self->{value} .= ":" if ($masm);
|
---|
471 | $self->{value} . ":";
|
---|
472 | } elsif ($win64 && $current_function->{abi} eq "svr4") {
|
---|
473 | my $func = "$current_function->{name}" .
|
---|
474 | ($nasm ? ":" : "\tPROC $current_function->{scope}") .
|
---|
475 | "\n";
|
---|
476 | $func .= " mov QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
|
---|
477 | $func .= " mov QWORD$PTR\[16+rsp\],rsi\n";
|
---|
478 | $func .= " mov rax,rsp\n";
|
---|
479 | $func .= "${decor}SEH_begin_$current_function->{name}:";
|
---|
480 | $func .= ":" if ($masm);
|
---|
481 | $func .= "\n";
|
---|
482 | my $narg = $current_function->{narg};
|
---|
483 | $narg=6 if (!defined($narg));
|
---|
484 | $func .= " mov rdi,rcx\n" if ($narg>0);
|
---|
485 | $func .= " mov rsi,rdx\n" if ($narg>1);
|
---|
486 | $func .= " mov rdx,r8\n" if ($narg>2);
|
---|
487 | $func .= " mov rcx,r9\n" if ($narg>3);
|
---|
488 | $func .= " mov r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
|
---|
489 | $func .= " mov r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
|
---|
490 | $func .= "\n";
|
---|
491 | } else {
|
---|
492 | "$current_function->{name}".
|
---|
493 | ($nasm ? ":" : "\tPROC $current_function->{scope}");
|
---|
494 | }
|
---|
495 | }
|
---|
496 | }
|
---|
497 | { package expr; # pick up expressions
|
---|
498 | sub re {
|
---|
499 | my ($class, $line, $opcode) = @_;
|
---|
500 | my $self = {};
|
---|
501 | my $ret;
|
---|
502 |
|
---|
503 | if ($$line =~ /(^[^,]+)/) {
|
---|
504 | bless $self,$class;
|
---|
505 | $self->{value} = $1;
|
---|
506 | $ret = $self;
|
---|
507 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
508 |
|
---|
509 | $self->{value} =~ s/\@PLT// if (!$elf);
|
---|
510 | $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
511 | $self->{value} =~ s/\.L/$decor/g;
|
---|
512 | $self->{opcode} = $opcode;
|
---|
513 | }
|
---|
514 | $ret;
|
---|
515 | }
|
---|
516 | sub out {
|
---|
517 | my $self = shift;
|
---|
518 | if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
|
---|
519 | "NEAR ".$self->{value};
|
---|
520 | } else {
|
---|
521 | $self->{value};
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|
525 | { package cfi_directive;
|
---|
526 | # CFI directives annotate instructions that are significant for
|
---|
527 | # stack unwinding procedure compliant with DWARF specification,
|
---|
528 | # see http://dwarfstd.org/. Besides naturally expected for this
|
---|
529 | # script platform-specific filtering function, this module adds
|
---|
530 | # three auxiliary synthetic directives not recognized by [GNU]
|
---|
531 | # assembler:
|
---|
532 | #
|
---|
533 | # - .cfi_push to annotate push instructions in prologue, which
|
---|
534 | # translates to .cfi_adjust_cfa_offset (if needed) and
|
---|
535 | # .cfi_offset;
|
---|
536 | # - .cfi_pop to annotate pop instructions in epilogue, which
|
---|
537 | # translates to .cfi_adjust_cfa_offset (if needed) and
|
---|
538 | # .cfi_restore;
|
---|
539 | # - [and most notably] .cfi_cfa_expression which encodes
|
---|
540 | # DW_CFA_def_cfa_expression and passes it to .cfi_escape as
|
---|
541 | # byte vector;
|
---|
542 | #
|
---|
543 | # CFA expressions were introduced in DWARF specification version
|
---|
544 | # 3 and describe how to deduce CFA, Canonical Frame Address. This
|
---|
545 | # becomes handy if your stack frame is variable and you can't
|
---|
546 | # spare register for [previous] frame pointer. Suggested directive
|
---|
547 | # syntax is made-up mix of DWARF operator suffixes [subset of]
|
---|
548 | # and references to registers with optional bias. Following example
|
---|
549 | # describes offloaded *original* stack pointer at specific offset
|
---|
550 | # from *current* stack pointer:
|
---|
551 | #
|
---|
552 | # .cfi_cfa_expression %rsp+40,deref,+8
|
---|
553 | #
|
---|
554 | # Final +8 has everything to do with the fact that CFA is defined
|
---|
555 | # as reference to top of caller's stack, and on x86_64 call to
|
---|
556 | # subroutine pushes 8-byte return address. In other words original
|
---|
557 | # stack pointer upon entry to a subroutine is 8 bytes off from CFA.
|
---|
558 |
|
---|
559 | # Below constants are taken from "DWARF Expressions" section of the
|
---|
560 | # DWARF specification, section is numbered 7.7 in versions 3 and 4.
|
---|
561 | my %DW_OP_simple = ( # no-arg operators, mapped directly
|
---|
562 | deref => 0x06, dup => 0x12,
|
---|
563 | drop => 0x13, over => 0x14,
|
---|
564 | pick => 0x15, swap => 0x16,
|
---|
565 | rot => 0x17, xderef => 0x18,
|
---|
566 |
|
---|
567 | abs => 0x19, and => 0x1a,
|
---|
568 | div => 0x1b, minus => 0x1c,
|
---|
569 | mod => 0x1d, mul => 0x1e,
|
---|
570 | neg => 0x1f, not => 0x20,
|
---|
571 | or => 0x21, plus => 0x22,
|
---|
572 | shl => 0x24, shr => 0x25,
|
---|
573 | shra => 0x26, xor => 0x27,
|
---|
574 | );
|
---|
575 |
|
---|
576 | my %DW_OP_complex = ( # used in specific subroutines
|
---|
577 | constu => 0x10, # uleb128
|
---|
578 | consts => 0x11, # sleb128
|
---|
579 | plus_uconst => 0x23, # uleb128
|
---|
580 | lit0 => 0x30, # add 0-31 to opcode
|
---|
581 | reg0 => 0x50, # add 0-31 to opcode
|
---|
582 | breg0 => 0x70, # add 0-31 to opcole, sleb128
|
---|
583 | regx => 0x90, # uleb28
|
---|
584 | fbreg => 0x91, # sleb128
|
---|
585 | bregx => 0x92, # uleb128, sleb128
|
---|
586 | piece => 0x93, # uleb128
|
---|
587 | );
|
---|
588 |
|
---|
589 | # Following constants are defined in x86_64 ABI supplement, for
|
---|
590 | # example available at https://www.uclibc.org/docs/psABI-x86_64.pdf,
|
---|
591 | # see section 3.7 "Stack Unwind Algorithm".
|
---|
592 | my %DW_reg_idx = (
|
---|
593 | "%rax"=>0, "%rdx"=>1, "%rcx"=>2, "%rbx"=>3,
|
---|
594 | "%rsi"=>4, "%rdi"=>5, "%rbp"=>6, "%rsp"=>7,
|
---|
595 | "%r8" =>8, "%r9" =>9, "%r10"=>10, "%r11"=>11,
|
---|
596 | "%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
|
---|
597 | );
|
---|
598 |
|
---|
599 | my ($cfa_reg, $cfa_rsp);
|
---|
600 | my @cfa_stack;
|
---|
601 |
|
---|
602 | # [us]leb128 format is variable-length integer representation base
|
---|
603 | # 2^128, with most significant bit of each byte being 0 denoting
|
---|
604 | # *last* most significant digit. See "Variable Length Data" in the
|
---|
605 | # DWARF specification, numbered 7.6 at least in versions 3 and 4.
|
---|
606 | sub sleb128 {
|
---|
607 | use integer; # get right shift extend sign
|
---|
608 |
|
---|
609 | my $val = shift;
|
---|
610 | my $sign = ($val < 0) ? -1 : 0;
|
---|
611 | my @ret = ();
|
---|
612 |
|
---|
613 | while(1) {
|
---|
614 | push @ret, $val&0x7f;
|
---|
615 |
|
---|
616 | # see if remaining bits are same and equal to most
|
---|
617 | # significant bit of the current digit, if so, it's
|
---|
618 | # last digit...
|
---|
619 | last if (($val>>6) == $sign);
|
---|
620 |
|
---|
621 | @ret[-1] |= 0x80;
|
---|
622 | $val >>= 7;
|
---|
623 | }
|
---|
624 |
|
---|
625 | return @ret;
|
---|
626 | }
|
---|
627 | sub uleb128 {
|
---|
628 | my $val = shift;
|
---|
629 | my @ret = ();
|
---|
630 |
|
---|
631 | while(1) {
|
---|
632 | push @ret, $val&0x7f;
|
---|
633 |
|
---|
634 | # see if it's last significant digit...
|
---|
635 | last if (($val >>= 7) == 0);
|
---|
636 |
|
---|
637 | @ret[-1] |= 0x80;
|
---|
638 | }
|
---|
639 |
|
---|
640 | return @ret;
|
---|
641 | }
|
---|
642 | sub const {
|
---|
643 | my $val = shift;
|
---|
644 |
|
---|
645 | if ($val >= 0 && $val < 32) {
|
---|
646 | return ($DW_OP_complex{lit0}+$val);
|
---|
647 | }
|
---|
648 | return ($DW_OP_complex{consts}, sleb128($val));
|
---|
649 | }
|
---|
650 | sub reg {
|
---|
651 | my $val = shift;
|
---|
652 |
|
---|
653 | return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
|
---|
654 |
|
---|
655 | my $reg = $DW_reg_idx{$1};
|
---|
656 | my $off = eval ("0 $2 $3");
|
---|
657 |
|
---|
658 | return (($DW_OP_complex{breg0} + $reg), sleb128($off));
|
---|
659 | # Yes, we use DW_OP_bregX+0 to push register value and not
|
---|
660 | # DW_OP_regX, because latter would require even DW_OP_piece,
|
---|
661 | # which would be a waste under the circumstances. If you have
|
---|
662 | # to use DWP_OP_reg, use "regx:N"...
|
---|
663 | }
|
---|
664 | sub cfa_expression {
|
---|
665 | my $line = shift;
|
---|
666 | my @ret;
|
---|
667 |
|
---|
668 | foreach my $token (split(/,\s*/,$line)) {
|
---|
669 | if ($token =~ /^%r/) {
|
---|
670 | push @ret,reg($token);
|
---|
671 | } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
|
---|
672 | push @ret,reg("$2+$1");
|
---|
673 | } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
|
---|
674 | my $i = 1*eval($2);
|
---|
675 | push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
|
---|
676 | } elsif (my $i = 1*eval($token) or $token eq "0") {
|
---|
677 | if ($token =~ /^\+/) {
|
---|
678 | push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
|
---|
679 | } else {
|
---|
680 | push @ret,const($i);
|
---|
681 | }
|
---|
682 | } else {
|
---|
683 | push @ret,$DW_OP_simple{$token};
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | # Finally we return DW_CFA_def_cfa_expression, 15, followed by
|
---|
688 | # length of the expression and of course the expression itself.
|
---|
689 | return (15,scalar(@ret),@ret);
|
---|
690 | }
|
---|
691 | sub re {
|
---|
692 | my ($class, $line) = @_;
|
---|
693 | my $self = {};
|
---|
694 | my $ret;
|
---|
695 |
|
---|
696 | if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
|
---|
697 | bless $self,$class;
|
---|
698 | $ret = $self;
|
---|
699 | undef $self->{value};
|
---|
700 | my $dir = $1;
|
---|
701 |
|
---|
702 | SWITCH: for ($dir) {
|
---|
703 | # What is $cfa_rsp? Effectively it's difference between %rsp
|
---|
704 | # value and current CFA, Canonical Frame Address, which is
|
---|
705 | # why it starts with -8. Recall that CFA is top of caller's
|
---|
706 | # stack...
|
---|
707 | /startproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
|
---|
708 | /endproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", 0);
|
---|
709 | # .cfi_remember_state directives that are not
|
---|
710 | # matched with .cfi_restore_state are
|
---|
711 | # unnecessary.
|
---|
712 | die "unpaired .cfi_remember_state" if (@cfa_stack);
|
---|
713 | last;
|
---|
714 | };
|
---|
715 | /def_cfa_register/
|
---|
716 | && do { $cfa_reg = $$line; last; };
|
---|
717 | /def_cfa_offset/
|
---|
718 | && do { $cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
|
---|
719 | last;
|
---|
720 | };
|
---|
721 | /adjust_cfa_offset/
|
---|
722 | && do { $cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
|
---|
723 | last;
|
---|
724 | };
|
---|
725 | /def_cfa/ && do { if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
|
---|
726 | $cfa_reg = $1;
|
---|
727 | $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
|
---|
728 | }
|
---|
729 | last;
|
---|
730 | };
|
---|
731 | /push/ && do { $dir = undef;
|
---|
732 | $cfa_rsp -= 8;
|
---|
733 | if ($cfa_reg eq "%rsp") {
|
---|
734 | $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
|
---|
735 | }
|
---|
736 | $self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
|
---|
737 | last;
|
---|
738 | };
|
---|
739 | /pop/ && do { $dir = undef;
|
---|
740 | $cfa_rsp += 8;
|
---|
741 | if ($cfa_reg eq "%rsp") {
|
---|
742 | $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
|
---|
743 | }
|
---|
744 | $self->{value} .= ".cfi_restore\t$$line";
|
---|
745 | last;
|
---|
746 | };
|
---|
747 | /cfa_expression/
|
---|
748 | && do { $dir = undef;
|
---|
749 | $self->{value} = ".cfi_escape\t" .
|
---|
750 | join(",", map(sprintf("0x%02x", $_),
|
---|
751 | cfa_expression($$line)));
|
---|
752 | last;
|
---|
753 | };
|
---|
754 | /remember_state/
|
---|
755 | && do { push @cfa_stack, [$cfa_reg, $cfa_rsp];
|
---|
756 | last;
|
---|
757 | };
|
---|
758 | /restore_state/
|
---|
759 | && do { ($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
|
---|
760 | last;
|
---|
761 | };
|
---|
762 | }
|
---|
763 |
|
---|
764 | $self->{value} = ".cfi_$dir\t$$line" if ($dir);
|
---|
765 |
|
---|
766 | $$line = "";
|
---|
767 | }
|
---|
768 |
|
---|
769 | return $ret;
|
---|
770 | }
|
---|
771 | sub out {
|
---|
772 | my $self = shift;
|
---|
773 | return ($elf ? $self->{value} : undef);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | { package directive; # pick up directives, which start with .
|
---|
777 | sub re {
|
---|
778 | my ($class, $line) = @_;
|
---|
779 | my $self = {};
|
---|
780 | my $ret;
|
---|
781 | my $dir;
|
---|
782 |
|
---|
783 | # chain-call to cfi_directive
|
---|
784 | $ret = cfi_directive->re($line) and return $ret;
|
---|
785 |
|
---|
786 | if ($$line =~ /^\s*(\.\w+)/) {
|
---|
787 | bless $self,$class;
|
---|
788 | $dir = $1;
|
---|
789 | $ret = $self;
|
---|
790 | undef $self->{value};
|
---|
791 | $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
|
---|
792 |
|
---|
793 | SWITCH: for ($dir) {
|
---|
794 | /\.global|\.globl|\.extern/
|
---|
795 | && do { $globals{$$line} = $prefix . $$line;
|
---|
796 | $$line = $globals{$$line} if ($prefix);
|
---|
797 | last;
|
---|
798 | };
|
---|
799 | /\.type/ && do { my ($sym,$type,$narg) = split(',',$$line);
|
---|
800 | if ($type eq "\@function") {
|
---|
801 | undef $current_function;
|
---|
802 | $current_function->{name} = $sym;
|
---|
803 | $current_function->{abi} = "svr4";
|
---|
804 | $current_function->{narg} = $narg;
|
---|
805 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
|
---|
806 | } elsif ($type eq "\@abi-omnipotent") {
|
---|
807 | undef $current_function;
|
---|
808 | $current_function->{name} = $sym;
|
---|
809 | $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
|
---|
810 | }
|
---|
811 | $$line =~ s/\@abi\-omnipotent/\@function/;
|
---|
812 | $$line =~ s/\@function.*/\@function/;
|
---|
813 | last;
|
---|
814 | };
|
---|
815 | /\.asciz/ && do { if ($$line =~ /^"(.*)"$/) {
|
---|
816 | $dir = ".byte";
|
---|
817 | $$line = join(",",unpack("C*",$1),0);
|
---|
818 | }
|
---|
819 | last;
|
---|
820 | };
|
---|
821 | /\.rva|\.long|\.quad|\.byte/
|
---|
822 | && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
|
---|
823 | $$line =~ s/\.L/$decor/g;
|
---|
824 | last;
|
---|
825 | };
|
---|
826 | }
|
---|
827 |
|
---|
828 | if ($gas) {
|
---|
829 | $self->{value} = $dir . "\t" . $$line;
|
---|
830 |
|
---|
831 | if ($dir =~ /\.extern/) {
|
---|
832 | $self->{value} = ""; # swallow extern
|
---|
833 | } elsif (!$elf && $dir =~ /\.type/) {
|
---|
834 | $self->{value} = "";
|
---|
835 | $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
|
---|
836 | (defined($globals{$1})?".scl 2;":".scl 3;") .
|
---|
837 | "\t.type 32;\t.endef"
|
---|
838 | if ($win64 && $$line =~ /([^,]+),\@function/);
|
---|
839 | } elsif (!$elf && $dir =~ /\.size/) {
|
---|
840 | $self->{value} = "";
|
---|
841 | if (defined($current_function)) {
|
---|
842 | $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
|
---|
843 | if ($win64 && $current_function->{abi} eq "svr4");
|
---|
844 | undef $current_function;
|
---|
845 | }
|
---|
846 | } elsif (!$elf && $dir =~ /\.align/) {
|
---|
847 | $self->{value} = ".p2align\t" . (log($$line)/log(2));
|
---|
848 | } elsif ($dir eq ".section") {
|
---|
849 | $current_segment=$$line;
|
---|
850 | if (!$elf && $current_segment eq ".init") {
|
---|
851 | if ($flavour eq "macosx") { $self->{value} = ".mod_init_func"; }
|
---|
852 | elsif ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
|
---|
853 | }
|
---|
854 | } elsif ($dir =~ /\.(text|data)/) {
|
---|
855 | $current_segment=".$1";
|
---|
856 | } elsif ($dir =~ /\.hidden/) {
|
---|
857 | if ($flavour eq "macosx") { $self->{value} = ".private_extern\t$prefix$$line"; }
|
---|
858 | elsif ($flavour eq "mingw64") { $self->{value} = ""; }
|
---|
859 | } elsif ($dir =~ /\.comm/) {
|
---|
860 | $self->{value} = "$dir\t$prefix$$line";
|
---|
861 | $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
|
---|
862 | }
|
---|
863 | $$line = "";
|
---|
864 | return $self;
|
---|
865 | }
|
---|
866 |
|
---|
867 | # non-gas case or nasm/masm
|
---|
868 | SWITCH: for ($dir) {
|
---|
869 | /\.text/ && do { my $v=undef;
|
---|
870 | if ($nasm) {
|
---|
871 | $v="section .text code align=64\n";
|
---|
872 | } else {
|
---|
873 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
874 | $current_segment = ".text\$";
|
---|
875 | $v.="$current_segment\tSEGMENT ";
|
---|
876 | $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
|
---|
877 | $v.=" 'CODE'";
|
---|
878 | }
|
---|
879 | $self->{value} = $v;
|
---|
880 | last;
|
---|
881 | };
|
---|
882 | /\.data/ && do { my $v=undef;
|
---|
883 | if ($nasm) {
|
---|
884 | $v="section .data data align=8\n";
|
---|
885 | } else {
|
---|
886 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
887 | $current_segment = "_DATA";
|
---|
888 | $v.="$current_segment\tSEGMENT";
|
---|
889 | }
|
---|
890 | $self->{value} = $v;
|
---|
891 | last;
|
---|
892 | };
|
---|
893 | /\.section/ && do { my $v=undef;
|
---|
894 | $$line =~ s/([^,]*).*/$1/;
|
---|
895 | $$line = ".CRT\$XCU" if ($$line eq ".init");
|
---|
896 | if ($nasm) {
|
---|
897 | $v="section $$line";
|
---|
898 | if ($$line=~/\.([px])data/) {
|
---|
899 | $v.=" rdata align=";
|
---|
900 | $v.=$1 eq "p"? 4 : 8;
|
---|
901 | } elsif ($$line=~/\.CRT\$/i) {
|
---|
902 | $v.=" rdata align=8";
|
---|
903 | }
|
---|
904 | } else {
|
---|
905 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
906 | $v.="$$line\tSEGMENT";
|
---|
907 | if ($$line=~/\.([px])data/) {
|
---|
908 | $v.=" READONLY";
|
---|
909 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
|
---|
910 | } elsif ($$line=~/\.CRT\$/i) {
|
---|
911 | $v.=" READONLY ";
|
---|
912 | $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
|
---|
913 | }
|
---|
914 | }
|
---|
915 | $current_segment = $$line;
|
---|
916 | $self->{value} = $v;
|
---|
917 | last;
|
---|
918 | };
|
---|
919 | /\.extern/ && do { $self->{value} = "EXTERN\t".$$line;
|
---|
920 | $self->{value} .= ":NEAR" if ($masm);
|
---|
921 | last;
|
---|
922 | };
|
---|
923 | /\.globl|.global/
|
---|
924 | && do { $self->{value} = $masm?"PUBLIC":"global";
|
---|
925 | $self->{value} .= "\t".$$line;
|
---|
926 | last;
|
---|
927 | };
|
---|
928 | /\.size/ && do { if (defined($current_function)) {
|
---|
929 | undef $self->{value};
|
---|
930 | if ($current_function->{abi} eq "svr4") {
|
---|
931 | $self->{value}="${decor}SEH_end_$current_function->{name}:";
|
---|
932 | $self->{value}.=":\n" if($masm);
|
---|
933 | }
|
---|
934 | $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
|
---|
935 | undef $current_function;
|
---|
936 | }
|
---|
937 | last;
|
---|
938 | };
|
---|
939 | /\.align/ && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
|
---|
940 | $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
|
---|
941 | last;
|
---|
942 | };
|
---|
943 | /\.(value|long|rva|quad)/
|
---|
944 | && do { my $sz = substr($1,0,1);
|
---|
945 | my @arr = split(/,\s*/,$$line);
|
---|
946 | my $last = pop(@arr);
|
---|
947 | my $conv = sub { my $var=shift;
|
---|
948 | $var=~s/^(0b[0-1]+)/oct($1)/eig;
|
---|
949 | $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
|
---|
950 | if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
|
---|
951 | { $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
|
---|
952 | $var;
|
---|
953 | };
|
---|
954 |
|
---|
955 | $sz =~ tr/bvlrq/BWDDQ/;
|
---|
956 | $self->{value} = "\tD$sz\t";
|
---|
957 | for (@arr) { $self->{value} .= &$conv($_).","; }
|
---|
958 | $self->{value} .= &$conv($last);
|
---|
959 | last;
|
---|
960 | };
|
---|
961 | /\.byte/ && do { my @str=split(/,\s*/,$$line);
|
---|
962 | map(s/(0b[0-1]+)/oct($1)/eig,@str);
|
---|
963 | map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
|
---|
964 | while ($#str>15) {
|
---|
965 | $self->{value}.="DB\t"
|
---|
966 | .join(",",@str[0..15])."\n";
|
---|
967 | foreach (0..15) { shift @str; }
|
---|
968 | }
|
---|
969 | $self->{value}.="DB\t"
|
---|
970 | .join(",",@str) if (@str);
|
---|
971 | last;
|
---|
972 | };
|
---|
973 | /\.comm/ && do { my @str=split(/,\s*/,$$line);
|
---|
974 | my $v=undef;
|
---|
975 | if ($nasm) {
|
---|
976 | $v.="common $prefix@str[0] @str[1]";
|
---|
977 | } else {
|
---|
978 | $v="$current_segment\tENDS\n" if ($current_segment);
|
---|
979 | $current_segment = "_DATA";
|
---|
980 | $v.="$current_segment\tSEGMENT\n";
|
---|
981 | $v.="COMM @str[0]:DWORD:".@str[1]/4;
|
---|
982 | }
|
---|
983 | $self->{value} = $v;
|
---|
984 | last;
|
---|
985 | };
|
---|
986 | }
|
---|
987 | $$line = "";
|
---|
988 | }
|
---|
989 |
|
---|
990 | $ret;
|
---|
991 | }
|
---|
992 | sub out {
|
---|
993 | my $self = shift;
|
---|
994 | $self->{value};
|
---|
995 | }
|
---|
996 | }
|
---|
997 |
|
---|
998 | # Upon initial x86_64 introduction SSE>2 extensions were not introduced
|
---|
999 | # yet. In order not to be bothered by tracing exact assembler versions,
|
---|
1000 | # but at the same time to provide a bare security minimum of AES-NI, we
|
---|
1001 | # hard-code some instructions. Extensions past AES-NI on the other hand
|
---|
1002 | # are traced by examining assembler version in individual perlasm
|
---|
1003 | # modules...
|
---|
1004 |
|
---|
1005 | my %regrm = ( "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
|
---|
1006 | "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7 );
|
---|
1007 |
|
---|
1008 | sub rex {
|
---|
1009 | my $opcode=shift;
|
---|
1010 | my ($dst,$src,$rex)=@_;
|
---|
1011 |
|
---|
1012 | $rex|=0x04 if($dst>=8);
|
---|
1013 | $rex|=0x01 if($src>=8);
|
---|
1014 | push @$opcode,($rex|0x40) if ($rex);
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | my $movq = sub { # elderly gas can't handle inter-register movq
|
---|
1018 | my $arg = shift;
|
---|
1019 | my @opcode=(0x66);
|
---|
1020 | if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
|
---|
1021 | my ($src,$dst)=($1,$2);
|
---|
1022 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1023 | rex(\@opcode,$src,$dst,0x8);
|
---|
1024 | push @opcode,0x0f,0x7e;
|
---|
1025 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
1026 | @opcode;
|
---|
1027 | } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
|
---|
1028 | my ($src,$dst)=($2,$1);
|
---|
1029 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1030 | rex(\@opcode,$src,$dst,0x8);
|
---|
1031 | push @opcode,0x0f,0x6e;
|
---|
1032 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
1033 | @opcode;
|
---|
1034 | } else {
|
---|
1035 | ();
|
---|
1036 | }
|
---|
1037 | };
|
---|
1038 |
|
---|
1039 | my $pextrd = sub {
|
---|
1040 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
|
---|
1041 | my @opcode=(0x66);
|
---|
1042 | my $imm=$1;
|
---|
1043 | my $src=$2;
|
---|
1044 | my $dst=$3;
|
---|
1045 | if ($dst =~ /%r([0-9]+)d/) { $dst = $1; }
|
---|
1046 | elsif ($dst =~ /%e/) { $dst = $regrm{$dst}; }
|
---|
1047 | rex(\@opcode,$src,$dst);
|
---|
1048 | push @opcode,0x0f,0x3a,0x16;
|
---|
1049 | push @opcode,0xc0|(($src&7)<<3)|($dst&7); # ModR/M
|
---|
1050 | push @opcode,$imm;
|
---|
1051 | @opcode;
|
---|
1052 | } else {
|
---|
1053 | ();
|
---|
1054 | }
|
---|
1055 | };
|
---|
1056 |
|
---|
1057 | my $pinsrd = sub {
|
---|
1058 | if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
|
---|
1059 | my @opcode=(0x66);
|
---|
1060 | my $imm=$1;
|
---|
1061 | my $src=$2;
|
---|
1062 | my $dst=$3;
|
---|
1063 | if ($src =~ /%r([0-9]+)/) { $src = $1; }
|
---|
1064 | elsif ($src =~ /%e/) { $src = $regrm{$src}; }
|
---|
1065 | rex(\@opcode,$dst,$src);
|
---|
1066 | push @opcode,0x0f,0x3a,0x22;
|
---|
1067 | push @opcode,0xc0|(($dst&7)<<3)|($src&7); # ModR/M
|
---|
1068 | push @opcode,$imm;
|
---|
1069 | @opcode;
|
---|
1070 | } else {
|
---|
1071 | ();
|
---|
1072 | }
|
---|
1073 | };
|
---|
1074 |
|
---|
1075 | my $pshufb = sub {
|
---|
1076 | if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1077 | my @opcode=(0x66);
|
---|
1078 | rex(\@opcode,$2,$1);
|
---|
1079 | push @opcode,0x0f,0x38,0x00;
|
---|
1080 | push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
|
---|
1081 | @opcode;
|
---|
1082 | } else {
|
---|
1083 | ();
|
---|
1084 | }
|
---|
1085 | };
|
---|
1086 |
|
---|
1087 | my $palignr = sub {
|
---|
1088 | if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1089 | my @opcode=(0x66);
|
---|
1090 | rex(\@opcode,$3,$2);
|
---|
1091 | push @opcode,0x0f,0x3a,0x0f;
|
---|
1092 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1093 | push @opcode,$1;
|
---|
1094 | @opcode;
|
---|
1095 | } else {
|
---|
1096 | ();
|
---|
1097 | }
|
---|
1098 | };
|
---|
1099 |
|
---|
1100 | my $pclmulqdq = sub {
|
---|
1101 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1102 | my @opcode=(0x66);
|
---|
1103 | rex(\@opcode,$3,$2);
|
---|
1104 | push @opcode,0x0f,0x3a,0x44;
|
---|
1105 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1106 | my $c=$1;
|
---|
1107 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1108 | @opcode;
|
---|
1109 | } else {
|
---|
1110 | ();
|
---|
1111 | }
|
---|
1112 | };
|
---|
1113 |
|
---|
1114 | my $rdrand = sub {
|
---|
1115 | if (shift =~ /%[er](\w+)/) {
|
---|
1116 | my @opcode=();
|
---|
1117 | my $dst=$1;
|
---|
1118 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1119 | rex(\@opcode,0,$dst,8);
|
---|
1120 | push @opcode,0x0f,0xc7,0xf0|($dst&7);
|
---|
1121 | @opcode;
|
---|
1122 | } else {
|
---|
1123 | ();
|
---|
1124 | }
|
---|
1125 | };
|
---|
1126 |
|
---|
1127 | my $rdseed = sub {
|
---|
1128 | if (shift =~ /%[er](\w+)/) {
|
---|
1129 | my @opcode=();
|
---|
1130 | my $dst=$1;
|
---|
1131 | if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
|
---|
1132 | rex(\@opcode,0,$dst,8);
|
---|
1133 | push @opcode,0x0f,0xc7,0xf8|($dst&7);
|
---|
1134 | @opcode;
|
---|
1135 | } else {
|
---|
1136 | ();
|
---|
1137 | }
|
---|
1138 | };
|
---|
1139 |
|
---|
1140 | # Not all AVX-capable assemblers recognize AMD XOP extension. Since we
|
---|
1141 | # are using only two instructions hand-code them in order to be excused
|
---|
1142 | # from chasing assembler versions...
|
---|
1143 |
|
---|
1144 | sub rxb {
|
---|
1145 | my $opcode=shift;
|
---|
1146 | my ($dst,$src1,$src2,$rxb)=@_;
|
---|
1147 |
|
---|
1148 | $rxb|=0x7<<5;
|
---|
1149 | $rxb&=~(0x04<<5) if($dst>=8);
|
---|
1150 | $rxb&=~(0x01<<5) if($src1>=8);
|
---|
1151 | $rxb&=~(0x02<<5) if($src2>=8);
|
---|
1152 | push @$opcode,$rxb;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | my $vprotd = sub {
|
---|
1156 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1157 | my @opcode=(0x8f);
|
---|
1158 | rxb(\@opcode,$3,$2,-1,0x08);
|
---|
1159 | push @opcode,0x78,0xc2;
|
---|
1160 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1161 | my $c=$1;
|
---|
1162 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1163 | @opcode;
|
---|
1164 | } else {
|
---|
1165 | ();
|
---|
1166 | }
|
---|
1167 | };
|
---|
1168 |
|
---|
1169 | my $vprotq = sub {
|
---|
1170 | if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
|
---|
1171 | my @opcode=(0x8f);
|
---|
1172 | rxb(\@opcode,$3,$2,-1,0x08);
|
---|
1173 | push @opcode,0x78,0xc3;
|
---|
1174 | push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
|
---|
1175 | my $c=$1;
|
---|
1176 | push @opcode,$c=~/^0/?oct($c):$c;
|
---|
1177 | @opcode;
|
---|
1178 | } else {
|
---|
1179 | ();
|
---|
1180 | }
|
---|
1181 | };
|
---|
1182 |
|
---|
1183 | # Intel Control-flow Enforcement Technology extension. All functions and
|
---|
1184 | # indirect branch targets will have to start with this instruction...
|
---|
1185 |
|
---|
1186 | my $endbranch = sub {
|
---|
1187 | (0xf3,0x0f,0x1e,0xfa);
|
---|
1188 | };
|
---|
1189 |
|
---|
1190 | ########################################################################
|
---|
1191 |
|
---|
1192 | if ($nasm) {
|
---|
1193 | print <<___;
|
---|
1194 | default rel
|
---|
1195 | %define XMMWORD
|
---|
1196 | %define YMMWORD
|
---|
1197 | %define ZMMWORD
|
---|
1198 | ___
|
---|
1199 | } elsif ($masm) {
|
---|
1200 | print <<___;
|
---|
1201 | OPTION DOTNAME
|
---|
1202 | ___
|
---|
1203 | }
|
---|
1204 | while(defined(my $line=<>)) {
|
---|
1205 |
|
---|
1206 | $line =~ s|\R$||; # Better chomp
|
---|
1207 |
|
---|
1208 | $line =~ s|[#!].*$||; # get rid of asm-style comments...
|
---|
1209 | $line =~ s|/\*.*\*/||; # ... and C-style comments...
|
---|
1210 | $line =~ s|^\s+||; # ... and skip whitespaces in beginning
|
---|
1211 | $line =~ s|\s+$||; # ... and at the end
|
---|
1212 |
|
---|
1213 | if (my $label=label->re(\$line)) { print $label->out(); }
|
---|
1214 |
|
---|
1215 | if (my $directive=directive->re(\$line)) {
|
---|
1216 | printf "%s",$directive->out();
|
---|
1217 | } elsif (my $opcode=opcode->re(\$line)) {
|
---|
1218 | my $asm = eval("\$".$opcode->mnemonic());
|
---|
1219 |
|
---|
1220 | if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
|
---|
1221 | print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
|
---|
1222 | next;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | my @args;
|
---|
1226 | ARGUMENT: while (1) {
|
---|
1227 | my $arg;
|
---|
1228 |
|
---|
1229 | ($arg=register->re(\$line, $opcode))||
|
---|
1230 | ($arg=const->re(\$line)) ||
|
---|
1231 | ($arg=ea->re(\$line, $opcode)) ||
|
---|
1232 | ($arg=expr->re(\$line, $opcode)) ||
|
---|
1233 | last ARGUMENT;
|
---|
1234 |
|
---|
1235 | push @args,$arg;
|
---|
1236 |
|
---|
1237 | last ARGUMENT if ($line !~ /^,/);
|
---|
1238 |
|
---|
1239 | $line =~ s/^,\s*//;
|
---|
1240 | } # ARGUMENT:
|
---|
1241 |
|
---|
1242 | if ($#args>=0) {
|
---|
1243 | my $insn;
|
---|
1244 | my $sz=$opcode->size();
|
---|
1245 |
|
---|
1246 | if ($gas) {
|
---|
1247 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
|
---|
1248 | @args = map($_->out($sz),@args);
|
---|
1249 | printf "\t%s\t%s",$insn,join(",",@args);
|
---|
1250 | } else {
|
---|
1251 | $insn = $opcode->out();
|
---|
1252 | foreach (@args) {
|
---|
1253 | my $arg = $_->out();
|
---|
1254 | # $insn.=$sz compensates for movq, pinsrw, ...
|
---|
1255 | if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
|
---|
1256 | if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
|
---|
1257 | if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
|
---|
1258 | if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; }
|
---|
1259 | }
|
---|
1260 | @args = reverse(@args);
|
---|
1261 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
|
---|
1262 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
|
---|
1263 | }
|
---|
1264 | } else {
|
---|
1265 | printf "\t%s",$opcode->out();
|
---|
1266 | }
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | print $line,"\n";
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | print "$cet_property" if ($cet_property);
|
---|
1273 | print "\n$current_segment\tENDS\n" if ($current_segment && $masm);
|
---|
1274 | print "END\n" if ($masm);
|
---|
1275 |
|
---|
1276 | close STDOUT or die "error closing STDOUT: $!;"
|
---|
1277 |
|
---|
1278 | |
---|
1279 | #################################################
|
---|
1280 | # Cross-reference x86_64 ABI "card"
|
---|
1281 | #
|
---|
1282 | # Unix Win64
|
---|
1283 | # %rax * *
|
---|
1284 | # %rbx - -
|
---|
1285 | # %rcx #4 #1
|
---|
1286 | # %rdx #3 #2
|
---|
1287 | # %rsi #2 -
|
---|
1288 | # %rdi #1 -
|
---|
1289 | # %rbp - -
|
---|
1290 | # %rsp - -
|
---|
1291 | # %r8 #5 #3
|
---|
1292 | # %r9 #6 #4
|
---|
1293 | # %r10 * *
|
---|
1294 | # %r11 * *
|
---|
1295 | # %r12 - -
|
---|
1296 | # %r13 - -
|
---|
1297 | # %r14 - -
|
---|
1298 | # %r15 - -
|
---|
1299 | #
|
---|
1300 | # (*) volatile register
|
---|
1301 | # (-) preserved by callee
|
---|
1302 | # (#) Nth argument, volatile
|
---|
1303 | #
|
---|
1304 | # In Unix terms top of stack is argument transfer area for arguments
|
---|
1305 | # which could not be accommodated in registers. Or in other words 7th
|
---|
1306 | # [integer] argument resides at 8(%rsp) upon function entry point.
|
---|
1307 | # 128 bytes above %rsp constitute a "red zone" which is not touched
|
---|
1308 | # by signal handlers and can be used as temporal storage without
|
---|
1309 | # allocating a frame.
|
---|
1310 | #
|
---|
1311 | # In Win64 terms N*8 bytes on top of stack is argument transfer area,
|
---|
1312 | # which belongs to/can be overwritten by callee. N is the number of
|
---|
1313 | # arguments passed to callee, *but* not less than 4! This means that
|
---|
1314 | # upon function entry point 5th argument resides at 40(%rsp), as well
|
---|
1315 | # as that 32 bytes from 8(%rsp) can always be used as temporal
|
---|
1316 | # storage [without allocating a frame]. One can actually argue that
|
---|
1317 | # one can assume a "red zone" above stack pointer under Win64 as well.
|
---|
1318 | # Point is that at apparently no occasion Windows kernel would alter
|
---|
1319 | # the area above user stack pointer in true asynchronous manner...
|
---|
1320 | #
|
---|
1321 | # All the above means that if assembler programmer adheres to Unix
|
---|
1322 | # register and stack layout, but disregards the "red zone" existence,
|
---|
1323 | # it's possible to use following prologue and epilogue to "gear" from
|
---|
1324 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
|
---|
1325 | #
|
---|
1326 | # omnipotent_function:
|
---|
1327 | # ifdef WIN64
|
---|
1328 | # movq %rdi,8(%rsp)
|
---|
1329 | # movq %rsi,16(%rsp)
|
---|
1330 | # movq %rcx,%rdi ; if 1st argument is actually present
|
---|
1331 | # movq %rdx,%rsi ; if 2nd argument is actually ...
|
---|
1332 | # movq %r8,%rdx ; if 3rd argument is ...
|
---|
1333 | # movq %r9,%rcx ; if 4th argument ...
|
---|
1334 | # movq 40(%rsp),%r8 ; if 5th ...
|
---|
1335 | # movq 48(%rsp),%r9 ; if 6th ...
|
---|
1336 | # endif
|
---|
1337 | # ...
|
---|
1338 | # ifdef WIN64
|
---|
1339 | # movq 8(%rsp),%rdi
|
---|
1340 | # movq 16(%rsp),%rsi
|
---|
1341 | # endif
|
---|
1342 | # ret
|
---|
1343 | #
|
---|
1344 | |
---|
1345 | #################################################
|
---|
1346 | # Win64 SEH, Structured Exception Handling.
|
---|
1347 | #
|
---|
1348 | # Unlike on Unix systems(*) lack of Win64 stack unwinding information
|
---|
1349 | # has undesired side-effect at run-time: if an exception is raised in
|
---|
1350 | # assembler subroutine such as those in question (basically we're
|
---|
1351 | # referring to segmentation violations caused by malformed input
|
---|
1352 | # parameters), the application is briskly terminated without invoking
|
---|
1353 | # any exception handlers, most notably without generating memory dump
|
---|
1354 | # or any user notification whatsoever. This poses a problem. It's
|
---|
1355 | # possible to address it by registering custom language-specific
|
---|
1356 | # handler that would restore processor context to the state at
|
---|
1357 | # subroutine entry point and return "exception is not handled, keep
|
---|
1358 | # unwinding" code. Writing such handler can be a challenge... But it's
|
---|
1359 | # doable, though requires certain coding convention. Consider following
|
---|
1360 | # snippet:
|
---|
1361 | #
|
---|
1362 | # .type function,@function
|
---|
1363 | # function:
|
---|
1364 | # movq %rsp,%rax # copy rsp to volatile register
|
---|
1365 | # pushq %r15 # save non-volatile registers
|
---|
1366 | # pushq %rbx
|
---|
1367 | # pushq %rbp
|
---|
1368 | # movq %rsp,%r11
|
---|
1369 | # subq %rdi,%r11 # prepare [variable] stack frame
|
---|
1370 | # andq $-64,%r11
|
---|
1371 | # movq %rax,0(%r11) # check for exceptions
|
---|
1372 | # movq %r11,%rsp # allocate [variable] stack frame
|
---|
1373 | # movq %rax,0(%rsp) # save original rsp value
|
---|
1374 | # magic_point:
|
---|
1375 | # ...
|
---|
1376 | # movq 0(%rsp),%rcx # pull original rsp value
|
---|
1377 | # movq -24(%rcx),%rbp # restore non-volatile registers
|
---|
1378 | # movq -16(%rcx),%rbx
|
---|
1379 | # movq -8(%rcx),%r15
|
---|
1380 | # movq %rcx,%rsp # restore original rsp
|
---|
1381 | # magic_epilogue:
|
---|
1382 | # ret
|
---|
1383 | # .size function,.-function
|
---|
1384 | #
|
---|
1385 | # The key is that up to magic_point copy of original rsp value remains
|
---|
1386 | # in chosen volatile register and no non-volatile register, except for
|
---|
1387 | # rsp, is modified. While past magic_point rsp remains constant till
|
---|
1388 | # the very end of the function. In this case custom language-specific
|
---|
1389 | # exception handler would look like this:
|
---|
1390 | #
|
---|
1391 | # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
|
---|
1392 | # CONTEXT *context,DISPATCHER_CONTEXT *disp)
|
---|
1393 | # { ULONG64 *rsp = (ULONG64 *)context->Rax;
|
---|
1394 | # ULONG64 rip = context->Rip;
|
---|
1395 | #
|
---|
1396 | # if (rip >= magic_point)
|
---|
1397 | # { rsp = (ULONG64 *)context->Rsp;
|
---|
1398 | # if (rip < magic_epilogue)
|
---|
1399 | # { rsp = (ULONG64 *)rsp[0];
|
---|
1400 | # context->Rbp = rsp[-3];
|
---|
1401 | # context->Rbx = rsp[-2];
|
---|
1402 | # context->R15 = rsp[-1];
|
---|
1403 | # }
|
---|
1404 | # }
|
---|
1405 | # context->Rsp = (ULONG64)rsp;
|
---|
1406 | # context->Rdi = rsp[1];
|
---|
1407 | # context->Rsi = rsp[2];
|
---|
1408 | #
|
---|
1409 | # memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
|
---|
1410 | # RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
|
---|
1411 | # dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
|
---|
1412 | # &disp->HandlerData,&disp->EstablisherFrame,NULL);
|
---|
1413 | # return ExceptionContinueSearch;
|
---|
1414 | # }
|
---|
1415 | #
|
---|
1416 | # It's appropriate to implement this handler in assembler, directly in
|
---|
1417 | # function's module. In order to do that one has to know members'
|
---|
1418 | # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
|
---|
1419 | # values. Here they are:
|
---|
1420 | #
|
---|
1421 | # CONTEXT.Rax 120
|
---|
1422 | # CONTEXT.Rcx 128
|
---|
1423 | # CONTEXT.Rdx 136
|
---|
1424 | # CONTEXT.Rbx 144
|
---|
1425 | # CONTEXT.Rsp 152
|
---|
1426 | # CONTEXT.Rbp 160
|
---|
1427 | # CONTEXT.Rsi 168
|
---|
1428 | # CONTEXT.Rdi 176
|
---|
1429 | # CONTEXT.R8 184
|
---|
1430 | # CONTEXT.R9 192
|
---|
1431 | # CONTEXT.R10 200
|
---|
1432 | # CONTEXT.R11 208
|
---|
1433 | # CONTEXT.R12 216
|
---|
1434 | # CONTEXT.R13 224
|
---|
1435 | # CONTEXT.R14 232
|
---|
1436 | # CONTEXT.R15 240
|
---|
1437 | # CONTEXT.Rip 248
|
---|
1438 | # CONTEXT.Xmm6 512
|
---|
1439 | # sizeof(CONTEXT) 1232
|
---|
1440 | # DISPATCHER_CONTEXT.ControlPc 0
|
---|
1441 | # DISPATCHER_CONTEXT.ImageBase 8
|
---|
1442 | # DISPATCHER_CONTEXT.FunctionEntry 16
|
---|
1443 | # DISPATCHER_CONTEXT.EstablisherFrame 24
|
---|
1444 | # DISPATCHER_CONTEXT.TargetIp 32
|
---|
1445 | # DISPATCHER_CONTEXT.ContextRecord 40
|
---|
1446 | # DISPATCHER_CONTEXT.LanguageHandler 48
|
---|
1447 | # DISPATCHER_CONTEXT.HandlerData 56
|
---|
1448 | # UNW_FLAG_NHANDLER 0
|
---|
1449 | # ExceptionContinueSearch 1
|
---|
1450 | #
|
---|
1451 | # In order to tie the handler to the function one has to compose
|
---|
1452 | # couple of structures: one for .xdata segment and one for .pdata.
|
---|
1453 | #
|
---|
1454 | # UNWIND_INFO structure for .xdata segment would be
|
---|
1455 | #
|
---|
1456 | # function_unwind_info:
|
---|
1457 | # .byte 9,0,0,0
|
---|
1458 | # .rva handler
|
---|
1459 | #
|
---|
1460 | # This structure designates exception handler for a function with
|
---|
1461 | # zero-length prologue, no stack frame or frame register.
|
---|
1462 | #
|
---|
1463 | # To facilitate composing of .pdata structures, auto-generated "gear"
|
---|
1464 | # prologue copies rsp value to rax and denotes next instruction with
|
---|
1465 | # .LSEH_begin_{function_name} label. This essentially defines the SEH
|
---|
1466 | # styling rule mentioned in the beginning. Position of this label is
|
---|
1467 | # chosen in such manner that possible exceptions raised in the "gear"
|
---|
1468 | # prologue would be accounted to caller and unwound from latter's frame.
|
---|
1469 | # End of function is marked with respective .LSEH_end_{function_name}
|
---|
1470 | # label. To summarize, .pdata segment would contain
|
---|
1471 | #
|
---|
1472 | # .rva .LSEH_begin_function
|
---|
1473 | # .rva .LSEH_end_function
|
---|
1474 | # .rva function_unwind_info
|
---|
1475 | #
|
---|
1476 | # Reference to function_unwind_info from .xdata segment is the anchor.
|
---|
1477 | # In case you wonder why references are 32-bit .rvas and not 64-bit
|
---|
1478 | # .quads. References put into these two segments are required to be
|
---|
1479 | # *relative* to the base address of the current binary module, a.k.a.
|
---|
1480 | # image base. No Win64 module, be it .exe or .dll, can be larger than
|
---|
1481 | # 2GB and thus such relative references can be and are accommodated in
|
---|
1482 | # 32 bits.
|
---|
1483 | #
|
---|
1484 | # Having reviewed the example function code, one can argue that "movq
|
---|
1485 | # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
|
---|
1486 | # rax would contain an undefined value. If this "offends" you, use
|
---|
1487 | # another register and refrain from modifying rax till magic_point is
|
---|
1488 | # reached, i.e. as if it was a non-volatile register. If more registers
|
---|
1489 | # are required prior [variable] frame setup is completed, note that
|
---|
1490 | # nobody says that you can have only one "magic point." You can
|
---|
1491 | # "liberate" non-volatile registers by denoting last stack off-load
|
---|
1492 | # instruction and reflecting it in finer grade unwind logic in handler.
|
---|
1493 | # After all, isn't it why it's called *language-specific* handler...
|
---|
1494 | #
|
---|
1495 | # SE handlers are also involved in unwinding stack when executable is
|
---|
1496 | # profiled or debugged. Profiling implies additional limitations that
|
---|
1497 | # are too subtle to discuss here. For now it's sufficient to say that
|
---|
1498 | # in order to simplify handlers one should either a) offload original
|
---|
1499 | # %rsp to stack (like discussed above); or b) if you have a register to
|
---|
1500 | # spare for frame pointer, choose volatile one.
|
---|
1501 | #
|
---|
1502 | # (*) Note that we're talking about run-time, not debug-time. Lack of
|
---|
1503 | # unwind information makes debugging hard on both Windows and
|
---|
1504 | # Unix. "Unlike" refers to the fact that on Unix signal handler
|
---|
1505 | # will always be invoked, core dumped and appropriate exit code
|
---|
1506 | # returned to parent (for user notification).
|
---|