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