wok view linld/stuff/src/pipehole.awk @ rev 22229
Up chkrootkit (0.53)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Nov 11 22:57:56 2019 +0100 (2019-11-11) |
parents | 08212b207b25 |
children | 664d83fb04f9 |
line source
1 BEGIN { hold=0; is386=0; isload=0; isiso=0; istazboot=0; wascall=0; ishimem=0; label="none"; xlabel=""; file="" }
2 function isnum(n) { return match(n,/^[0-9+-]/) }
3 {
4 sub(/segment word public/,"segment byte public")
6 if (/^ ; $/) next
7 if (/^@.*:$/ || / endp$/) afterjmp=0
8 if (/^ \.386p$/) is386=1
9 if (file == "" && /debug S/) { file=$3; gsub(/\"/,"",file) }
10 if (/debug S/) print " %PAGESIZE 1000"
11 if (file == "tazboot.cpp") {
12 if (/add si,2/) $0=" lodsw ; " $0
13 if (/add si,4/) { print " lodsw"; $0=" lodsw ; " $0 }
14 if (/add di,2/) $0=" scasw ; " $0
15 if (/int argc/) istazboot=1
16 if (istazboot == 1) {
17 if (/push.*i$/) $0="; " $0
18 if (/word ptr/) { istazboot=0; $0="; " $0 }
19 }
20 } # file == "tazboot.cpp"
21 if (file == "linld.cpp") {
22 if (/add si,2/) $0=" lodsw ; " $0
23 if (/add di,2/) $0=" scasw ; " $0
24 if (/bx,offset DGROUP:s@\+26/) sub(/mov/,";mov")
25 if (islinld==1) {
26 print "; " $0
27 if (!/word ptr/) next
28 islinld=0
29 sub(/,word.*/,",di ; argv")
30 if (/di,di/) { print "; " $0; next }
31 }
32 if (/^_main proc/) islinld=1
33 if (/== 0x662F/) islinld=2
34 if (islinld==2) {
35 if (/cpuhaslm/) islinld=0
36 if (/mov/) { print "; " $0; next }
37 }
38 if (/image\|initrd/) islinld=3
39 if (islinld==3) {
40 if (/bx,word ptr/) { islinld=0; print "; " $0; next }
41 }
42 if (/fileexist\$qpxzc/) islinld=4
43 if (islinld==4) {
44 if (/ax,-1/) print " mov bx,word ptr [si]"
45 if (/ax,word ptr/) $0=" xchg ax,bx"
46 if (/\[si\]$/) { islinld=0; print "; " $0; next }
47 }
48 } # file == "linld.cpp"
49 if (file == "himem.cpp") {
50 if (/void load_image/) ishimem=1
51 if (ishimem == 1 && is386 == 0) {
52 if (/si\+8\]$/ || /si\+4\]$/ || /si\+16\]$/) next
53 if (/si\+6\]$/ || /si\+2\]$/ || /si\+14\]$/) sub(/mov dx,/,"les dx,d")
54 if (/si\+12\],ax/ || /si\+16\],ax/ || /bp-2\],ax/) sub(/,ax/,",es")
55 }
56 if (ishimem == 1) {
57 if (/do \{/) ishimem=2
58 if (/byte ptr DGROUP:_vcpi,0/) print " mov bx,si"
59 if (/bx,si/) next
60 if (/sp,6/) {
61 print " push si"
62 print " push si"
63 next
64 }
65 }
66 if (ishimem == 2) {
67 if (/movzx/) print " cwde"
68 if (/bp-6/) next
69 if (/storepage.bufv/) {
70 print " inc ax"
71 print " push ax"
72 }
73 if (/buf \+= size;/) {
74 print " pop ax"
75 }
76 if (/Read error/) ishimem=0
77 }
78 } # file == "himem.cpp"
79 if (file == "load.cpp") {
80 if (/== 0x200/) isload=13
81 if (isload == 13) { # LOAD.LST
82 if (/cmp/) {
83 print " mov dx,-512"
84 $0=" add ax,dx"
85 }
86 if (/dx,-512/) {
87 isload=0
88 $0="; " $0
89 }
90 }
91 if (/i\+21\],513$/) isload=11
92 if (isload == 12) { # LOAD.LST
93 if (/cmp/) next
94 if (/jb/) isload=0
95 sub(/jb/,"jcxz")
96 }
97 if (isload == 11) { # LOAD.LST
98 if (/cmp/) {
99 print " mov cx,513"
100 sub(/cmp /,"sub cx,")
101 sub(/,513/,"")
102 }
103 if (/jb/) isload=12
104 sub(/jb/,"ja")
105 }
106 sub(/_imgs\+65534/,"_imgs-2")
107 if (/m, _rm_size/) isload=10
108 if (isload == 10) { # LOAD.LST
109 if (/^ je /) next
110 if (/ptr @die\$qpxzc/) {
111 $0=" jne @die@"
112 isload=0
113 }
114 }
115 if (/setup_sects == 0/) isload=9
116 if (isload == 9) { # LOAD.LST
117 sub(/,0/,",al")
118 if (/jne/) isload=0
119 }
120 if (/fallback\)\[1\] == 0/) isload=8
121 if (isload == 8) { # LOAD.LST
122 if (/load_image/) isload=0
123 else next
124 }
125 if (/cmd_line_ptr =/ && is386 == 0) isload=7
126 if (isload == 7) { # LOAD.LST
127 if (/add/ || /xor/ || /extrn/ || /N_LXLSH@/ || /cl,4/ || /,ax/) next
128 if (/enable A20 if needed/) { print nextinst; isload=0 }
129 if (/i-463/) $0=" mov bx,-463"
130 if (/i-465/) {
131 sub(/465/,"2"); sub(/\[/,"[bx+")
132 nextinst=$0; sub(/-2\],-23745/,"],8000h",nextinst)
133 }
134 if (/,dx/) {
135 print " mov cl,12"
136 print " shr ax,cl"
137 print " mov bx,55"
138 sub(/dx/,"ax")
139 }
140 }
141 if (/\[0\] = m-\>fallback/) isload=6
142 if (isload == 6) { # LOAD.LST
143 if (/si\+2/) $0=" lodsw"
144 if (/les/) sub(/bx,/,"ax,")
145 if (/bx\+4/ || /es:/) {
146 if (/bx\+4/) isload=0
147 next
148 }
149 if (/si\+6/) {
150 print " xchg ax,di"
151 print " movsw"
152 print " movsw"
153 print " movsw"
154 print " movsw"
155 print " xchg ax,di"
156 next
157 }
158 }
159 if (/version_string = /) isload=5
160 if (isload == 5) { # LOAD.LST
161 sub(/ax,/,"bx,")
162 if (/_version_string,/) isload=0
163 if (/mov bx,ax/) next
164 }
165 if (/_base_himem\+2/ && is386 == 0) isload=4
166 if (isload == 4) { # LOAD.LST
167 if (/_base_himem\+2/) next
168 if (/_base_himem$/) {
169 sub(/mov dx,/,"les dx,d")
170 }
171 sub(/,ax/,",es")
172 if (/add ax,word ptr/) $0=" add ax,cx"
173 if (/i\+29\],0/) {
174 sub(/,0$/,"")
175 sub(/cmp /,"mov cx,")
176 }
177 sub(/je/,"jcxz")
178 if (/@strcpy/) isload=0
179 }
180 if (/void load_initrd\(\)/) isload=3
181 if (isload == 3) { # LOAD.LST
182 if(/push di/ || /pop di/) next
183 sub(/\[di/,"[bx")
184 sub(/\di,/,"bx,")
185 }
186 if (/vid_mode = vid_mode/) isload=2
187 if (isload == 2) { # LOAD.LST
188 sub(/,0/,""); sub(/cmp /,"mov cx,")
189 sub(/je/,"jcxz")
190 if (/ax,word/) next
191 sub(/,ax/,",cx")
192 if (/version_string/ || /starting linux 1\.3\.73/) isload=0
193 }
194 if (/Not a kernel/ || /_rm_size=0x200/ || /heap_top = _rm_buf/) isload=1
195 if (isload == 1) { # LOAD.LST
196 if (/ptr .die\$qpxzc/) $0="@die@:\n" $0
197 if (/mov al,byte ptr/ && is386) {
198 sub(/mov al/,"movzx eax")
199 }
200 if (is386 == 0) {
201 if (/m->size -= _rm_size/) print " cwd"
202 sub(/,0$/,",dx")
203 }
204 if (/ax,word ptr/) next
205 if (/^ call/) isload=0
206 }
207 } # file == "load.cpp"
208 if (file == "iso9660.cpp") {
209 if (/<< SECTORBITS/) isiso=9
210 if (isiso == 9) { # ISO9660.LST
211 if (/dx,/) next
212 sub(/mov ax,/,"les ax,d")
213 if (/^ call/) {
214 print " extrn N_LXLSH@ES:near"
215 sub(/N_LXLSH@/,"N_LXLSH@ES")
216 isiso=0
217 }
218 }
219 if (/filesize =/) isiso=8
220 if (isiso == 8) { # ISO9660.LST
221 if (/ax,/) next
222 sub(/mov dx,/,"les dx,d")
223 sub(/,ax/,",es")
224 if (/filemod/) isiso=0
225 }
226 if (/CD001/) isiso=7
227 if (isiso == 7) { # ISO9660.LST
228 sub(/mov ax,-1/,"dec ax")
229 if (/jmp/) isiso=0
230 }
231 if (/int len =/) isiso=6
232 if (isiso == 6) { # ISO9660.LST
233 if (/dx,ax/) next
234 sub(/ax/,"dx")
235 sub(/cx,di/,"bx,di")
236 sub(/cx,dx/,"bx,ax")
237 sub(/di,dx/,"di,ax")
238 if (/while/) isiso=2
239 }
240 if (/entrysize =/) isiso=5
241 if (isiso == 5) { # ISO9660.LST
242 if (/ax,ax/) next
243 sub(/ax/,"cx")
244 sub(/je/,"jcxz")
245 if (/return/) isiso=0
246 }
247 if (/x->curdirsize == 0xFFFF/) isiso=4
248 if (isiso == 4) { # ISO9660.LST
249 sub(/DGROUP:_isostate\+18/,"[si+18]")
250 sub(/DGROUP:_isostate\+20/,"[si+20]")
251 if (/goto restarted/) isiso=0
252 }
253 } # file == "iso9660.cpp"
254 if (file == "iso9660.cpp" || file == "tazboot.cpp") {
255 if (/do s\+\+; while/) isiso=3
256 if (/for \(p = s; \*s && \*s \!=/) isiso=3 # tazboot/main
257 if (isiso == 3) { # ISO9660.LST, TAZBOOT.LST
258 sub(/cmp byte ptr \[.i\]/,"sub al")
259 if (/mov byte ptr \[bp-5\],al/) $0=" push ax"
260 if (/mov al,byte ptr \[bp-5\]/) $0=" pop ax"
261 if (/inc /) { r=$2; print; next }
262 if (/al,0/) print " mov al,[" r "]"
263 if (/al,byte ptr/) sub(/mov/,"xchg")
264 if (/byte ptr \[.*\],0/) next
265 if (/jmp/) {
266 print " mov bx,si"
267 $0=" db 0A8h ; test al,xx instead of " $0
268 }
269 if (/word ptr \[bp-4\]/) next
270 if (/\) s\+\+;/ || /\],-1/) isiso=0
271 }
272 } # file == "iso9660.cpp" || file == "tazboot.cpp"
273 if (file == "iso9660.cpp") {
274 if (/endname = NULL/) isiso=2
275 if (isiso == 2) { # ISO9660.LST
276 if (/mov bx,cx/) next
277 gsub(/cx/,"bx")
278 sub(/DGROUP:_isostate\+35/,"[si+35]")
279 }
280 if (/const char \*n = name/) isiso=1
281 if (isiso == 1) { # ISO9660.LST
282 if ((/mov word ptr \[si\+32\],ax/ ) ||
283 (/mov ax,word ptr \[si\+2\]/) ||
284 (/bx,word ptr \[si\+32\]/) || (/ax,dx/)) next
285 if (/dx,/) sub(/dx/,"ax")
286 if ((/sub ax,word ptr \[si\+2\]/) ||
287 (/\[si\+16\]/) || (/ax,di/)) sub(/ax/,"bx")
288 if (/add word ptr \[si\+32\],ax/) $0=" add bx,word ptr [si+16]"
289 if (/al,/ || /,al/) sub(/al/,"cl")
290 if (/cmp byte ptr \[si\+34\],0/) $0=" or cl,cl"
291 if (/jne @@0$/) next
292 if (/jmp @3@58$/) $0=" je @3@58"
293 sub(/mov ax,-1/,"dec ax")
294 }
295 } # file == "iso9660.cpp"
296 if (/endp/) { xlabel = ""; goto2=0 }
297 if (/isoopen\(s\+7\)/ && xlabel == "") goto2=1 # tazboot/bootiso
298 if (/_vid_mode,ax/ && xlabel == "") goto2=1 # tazboot/main
299 if (/_initrd_name,si/ && xlabel == "") goto2=1 # tazboot/main
300 if (/_base_himem\+2,/ && xlabel == "@") goto2=1 # tazboot/bootiso tazboot/main
301 if (/DGROUP:_skip_alloc/ && xlabel == "@") goto2=1 # tazboot/bootiso tazboot/main
302 if (/puts\(cmdline\)/ && xlabel == "@@") goto2=1
303 if (goto2 == 1 && /jmp/) { # TAZBOOT.LST && LINLD.LST
304 print $NF xlabel "@:"
305 label=$NF
306 }
307 if (goto2 > 0 && label == $NF) {
308 $0=$0 xlabel
309 if (goto2++ == 1) xlabel=xlabel "@"
310 }
311 if (file == "tazboot.cpp" && /close\(x/) isotazboot=16
312 if (isotazboot == 160) { # TAZBOOT.LST
313 $0="; " $0
314 if (/ret/) isotazboot=0
315 }
316 if (isotazboot == 16) { # TAZBOOT.LST
317 if (/@.@/) {
318 isotazboot=160
319 next
320 }
321 }
322 if (file == "tazboot.cpp" && /jne @@2/) isotazboot=15
323 if (isotazboot == 15) { # TAZBOOT.LST
324 if (/@.@/) {
325 print " pop di"
326 print " pop si"
327 print " mov sp,bp"
328 print " pop bp"
329 print " ret"
330 next
331 }
332 if (/skip_alloc/) isotazboot=0
333 }
334 if (/if\(\*s>=/) isotazboot=14
335 if (isotazboot == 14) { # LINLD.LST
336 if (/jmp/) {
337 $0=" db 0A9h ; test ax,xxxx instead of " $0
338 isotazboot=0
339 }
340 }
341 if (file == "tazboot.cpp" && /; s \+= 4/) isotazboot=13 # tazboot/main
342 if (isotazboot == 13) { # TAZBOOT.LST
343 if (/si,4/) $0=" lea bx,[si+4]"
344 if (/bx,si/) next
345 if (/DGROUP:_topmem/ || /set_iso/) isotazboot=0
346 }
347 if (file == "tazboot.cpp" && /case 0x652F:/) isotazboot=12 # tazboot/main
348 if (isotazboot == 12) { # TAZBOOT.LST
349 sub(/si,word/,"bx,word")
350 if (/short/) isotazboot=0
351 }
352 if (/return load_kernel/) isotazboot=11 # tazboot/isokernel
353 if (isotazboot == 11) { # TAZBOOT.LST
354 sub(/call/,"jmp")
355 if (/ret/ || /pop/) next
356 if (/endp/) isotazboot=0
357 }
358 if (/cmdline=s\+=3/ || /magic \!= 0/ || /&root_dev =/) { isotazboot=10; j="" } # ,tazboot/bootiso,tazboot/main
359 if (isotazboot == 10) { # TAZBOOT.LST && LINLD.LST
360 if (/je/ || /jne/) { j=$1; next }
361 if (/jmp/) {
362 if (j=="jne") sub(/jmp/,"je")
363 else if (j=="je") sub(/jmp/,"jne")
364 isotazboot=0
365 }
366 }
367 if (/static const unsigned long initrddesc = 18L/) isotazboot=9 # tazboot/bootiso
368 if (isotazboot == 9) { # TAZBOOT.LST
369 if (/,0/) {
370 split($4,y,",")
371 print " mov bx,offset " y[1]
372 sub(/DGROUP:.*,/,"[bx],")
373 }
374 if (/mov/ && $3 == y[1]) next
375 if (/je/) next
376 if (/jmp/) sub(/jmp/,"jne")
377 sub(/ax,offset/,"bx,offset")
378 if (/bx,ax/) { isotazboot=0; next }
379 }
380 if (/isoopen\(s\+7\) != -1/) isotazboot=8 # tazboot/bootiso
381 if (isotazboot == 8) { # TAZBOOT.LST
382 sub(/\[bx/,"[si")
383 if (/bx,si/) next
384 if (/magic/) isotazboot=0
385 }
386 if (/isoopen\(\"bzImage\"\)/) isotazboot=7 # tazboot/bootiso
387 if (isotazboot == 7) { # TAZBOOT.LST
388 if (/inc/ || /,al/) next
389 if (/al,byte/) sub (/mov al,/,"inc ")
390 if (/isokernel/) isotazboot=0
391 }
392 if (/if \(c\) s\+\+;/) isotazboot=6 # tazboot/main
393 if (isotazboot == 6) { # TAZBOOT.LST
394 if (/cmp/) {
395 $0=" cmp al,0"
396 isotazboot=0
397 }
398 }
399 if (/static void next_chunk/) isotazboot=5 # tazboot/next_chunk
400 if (isotazboot == 501) {
401 if (/ret/) {
402 print "@1@86:"
403 isotazboot=0
404 }
405 }
406 if (isotazboot == 5 || isotazboot == 500) { # TAZBOOT.LST
407 if (/cx,ax/) $0=" xchg ax,bx"
408 if (/ax,word ptr \[si\+28\]/ && isotazboot == 500) next
409 if (/bx,cx/) next
410 if (/push/ || /pop/ || /bp,sp/ || /si,/) next
411 sub(/\[si/,"[di")
412 if (/initrd_info/) isotazboot=500
413 if (/bx\+6\]/) next
414 if (/bx\+4\]/) sub(/mov dx,/,"les dx,d")
415 sub(/di\+24\],ax/,"di+24],es")
416 sub(/call/,"jmp")
417 if (/ret/ || /pop/ || /^@1@86:/) next
418 if (/_isostate\+14/) next
419 if (/_isostate\+12/) {
420 sub(/mov ax,/,"les ax,d")
421 print
422 print " mov dx,es"
423 next
424 }
425 if (/ax,-4/) isotazboot++
426 }
427 if (/0x7FF0/) isotazboot=4 # tazboot/bootiso
428 if (isotazboot == 4) { # TAZBOOT.LST
429 if (/ax,word ptr/) {
430 print " mov ax,32752"
431 print " cwd"
432 sub(/mov/,"sub")
433 }
434 if (/bx,/ || /cx,/ || /dx,/) next
435 sub(/,0/,",dx")
436 sub(/,bx/,",dx")
437 sub(/,cx/,",ax")
438 if (/@addinitrd\$qv/) isotazboot=0
439 }
440 if (/c = x->filename/) isotazboot=3 # tazboot/bootiso
441 if (isotazboot == 3) { # TAZBOOT.LST
442 if (/ax,/) $0=" xchg ax,bx"
443 if (/\]$/) next
444 if (/@strcpy\$qpxzct1/) isotazboot=0
445 }
446 if (/base_himem = memtop/) isotazboot=2 # tazboot/bootiso
447 if (isotazboot == 2) { # TAZBOOT.LST
448 if (/word ptr \[si\+2\],0/) {
449 print s; hold=0
450 print " mov bx,word ptr [si+2]"
451 $0=" or bx,bx"
452 }
453 if (/\[bp-4\],ax/) sub(/ax/,"bx")
454 if (/ax,word ptr \[si\+2\]/ || /bx,ax/) next
455 if (/_base_himem\+2,dx/) {
456 print " mov bx,offset DGROUP:_base_himem+2"
457 }
458 sub(/DGROUP:_base_himem,/,"[bx-2],")
459 sub(/DGROUP:_base_himem\+2,/,"[bx],")
460 sub(/DGROUP:_base_himem\+3,/,"[bx+1],")
461 if (/@strcmp\$qpxzct1/) isotazboot=0
462 }
463 if (/static void addinitrd/) isotazboot=100 # tazboot/addinitrd
464 if (isotazboot == 100) { # TAZBOOT.LST
465 if (/cx,ax/) {
466 print " mov si,offset _isostate+8"
467 print " push ds"
468 print " pop es"
469 print " xchg ax,di"
470 print " movsw"
471 print " movsw"
472 print " movsw"
473 print " movsw"
474 $0=" xchg ax,di"
475 }
476 if (/mov/ && !/si/ && !/cl/) next
477 if (/void load_initrds/) isotazboot=101
478 }
479 if (isotazboot == 101 || isotazboot == 102) { # TAZBOOT.LST
480 sub(/\[si/,"[di"); sub(/si,/,"di,"); sub(/si$/,"di")
481 sub(/DGROUP:_imgs\+38$/,"[di+38-32]")
482 sub(/DGROUP:_imgs\+40$/,"[di+40-32]")
483 if (/isofd/) isotazboot=102
484 if (/push/ && isotazboot == 102) next
485 if (/pop/ && isotazboot == 102) next
486 if (/load_initrd/) isotazboot=101
487 if (/isokernel/) isotazboot=103
488 }
489 if (isotazboot > 102) { # TAZBOOT.LST
490 if (/push/ || /pop/) next
491 sub(/\[si/,"[bx")
492 sub(/si,/,"bx,")
493 if (/static void bootiso/) isotazboot=0
494 }
495 if (wascall) {
496 if (rcall != "") {
497 if (/,ax$/) print " mov " rcall ",ax"
498 else print " xchg ax," rcall
499 wascall=0
500 }
501 else if (/^ mov .i,ax$/) {
502 split($2,y,",")
503 rcall=y[1]
504 next
505 }
506 else wascall=0
507 }
508 if (/^ call /) { wascall=1; rcall="" }
509 if (hold == 0) {
510 s=$0
511 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
512 r=$2; kept=0
513 hold=1; split($2,regs,","); next
514 }
515 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
516 hold=2; r=$2; next
517 }
518 if (/^ mov [abcds][ix],/ && ! /,.s/) {
519 hold=3; split($2,regs,","); next
520 }
521 if (/^ movzx eax,ax$/) { hold=4; next }
522 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
523 split($0,regs,",")
524 if (isnum(regs[2]) && regs[2] != 0 &&
525 (regs[2] % 256) == 0) {
526 hold=5; next
527 }
528 }
529 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
530 if (/^ mov cl,4$/) { hold=7; next }
531 if (/^ cmp word ptr DGROUP:.*,0$/) {
532 hold=8; split($2,regs,","); next
533 }
534 if (/^ cbw/) { hold=11; kept=0; next }
535 if (/^ add [abcds][ix],2$/) {
536 split($2,regs,","); hold=12; next
537 }
538 if (/^ sub [abcds][ix],2$/) {
539 split($2,regs,","); hold=13; next
540 }
541 if (/^ push dx$/) {
542 hold=14; next;
543 }
544 }
545 else if (hold == 1) {
546 if (/^ ;/) { line[kept++]=$0; next }
547 hold=0; split($2,args,","); op=""
548 if ($1 == "add") op="+"
549 if ($1 == "sub") op="-"
550 if ($1 == "inc") { op="+"; args[2]="1"; }
551 if ($1 == "dec") { op="-"; args[2]="1"; }
552 if (op != "" && regs[1] == args[1]) {
553 if (isnum(args[2])) {
554 for (i = kept++; i > 0; i--) line[i] = line[i-1]
555 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
556 hold=10; next
557 }
558 line[kept++]=$0
559 hold=1
560 next
561 }
562 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
563 print " xchg " r
564 }
565 else print s
566 for (i = 0; i < kept; i++) print line[i]; kept=0
567 }
568 else if (hold == 2) {
569 split($0,args,",")
570 if (/^ mov / && r == args[2]) { print s; s=$0; next }
571 split($2,args,",")
572 hold=0; print s
573 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
574 }
575 else if (hold == 3) {
576 hold=0
577 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
578 if (/^ add [abcds][ix],/) {
579 split($2,regs2,",")
580 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
581 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
582 print t; print s; next
583 }
584 }
585 print s
586 }
587 else if (hold == 4) {
588 hold=0
589 if (/^ push eax$/) {
590 print " push 0"; print " push ax"; next
591 } else { print s }
592 }
593 else if (hold == 5) {
594 hold=0
595 if ($1 == "jae" || $1 == "jb") {
596 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
597 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
598 s = s "/256"
599 }
600 print s
601 }
602 else if (hold == 6) {
603 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
604 line[kept++]=$0
605 next
606 }
607 p=$0
608 if (/^ movzx eax,ax$/) {
609 s=" mov eax,cs"; p=""
610 }
611 print s
612 for (i = 0; i < kept; i++) print line[i]; kept=0
613 if (p != "") print p
614 hold=0; next
615 }
616 else if (hold == 7) {
617 hold=0
618 if (/^ call near ptr N_LXURSH@$/) {
619 print " extrn N_LXURSH@4:near"
620 print " call near ptr N_LXURSH@4"
621 next
622 }
623 if (/^ call near ptr N_LXLSH@$/) {
624 print " extrn N_LXLSH@4:near"
625 print " call near ptr N_LXLSH@4"
626 next
627 }
628 print s
629 }
630 else if (hold == 8) {
631 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
632 hold=0
633 print s
634 }
635 else if (hold == 9) {
636 hold=0; split($2,args,",")
637 if (/^ mov ax,/ && args[2] == regs[1]) {
638 print; print " or ax,ax"; print p; next
639 }
640 print s; print p;
641 }
642 else if (hold == 10) {
643 split($2,args,","); op=""
644 if ($1 == "add") op="+"
645 if ($1 == "sub") op="-"
646 if ($1 == "inc") { op="+"; args[2]="1"; }
647 if ($1 == "dec") { op="-"; args[2]="1"; }
648 if (op != "" && isnum(args[2])) {
649 split(line[0],reg,",")
650 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
651 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
652 next
653 }
654 }
655 hold=0
656 if (/^ mov [sd]i,ax$/) {
657 split($2,args,",")
658 for (i = 0; i < kept; i++) {
659 sub(/ax/,args[1],line[i]); print line[i]
660 }
661 next
662 }
663 for (i = 0; i < kept; i++) print line[i]
664 }
665 else if (hold == 11) {
666 if (/^ inc ax$/ || /^ dec ax$/) {
667 line[kept++]=$0; next
668 }
669 split($2,args,",")
670 if (/^ mov cl,/) {
671 split($2,args,",")
672 if (args[2] >= 8) {
673 line[kept++]=$0; next
674 }
675 }
676 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
677 print " cbw "
678 }
679 for (i = 0; i < kept; i++) print line[i]
680 hold=kept=0
681 }
682 else if (hold == 12) {
683 hold=0
684 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
685 print " inc " regs[1]
686 print " inc " regs[1]
687 }
688 else print " add " regs[1] ",2"
689 }
690 else if (hold == 13) {
691 hold=0
692 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
693 print " dec " regs[1]
694 print " dec " regs[1]
695 }
696 else print " sub " regs[1] ",2"
697 }
698 else if (hold == 14) {
699 if (/^ push ax$/) { hold++; next; }
700 print " push dx";
701 hold=0;
702 }
703 else if (hold == 15) {
704 if (/^ pop eax$/) { hold++; next; }
705 print " push dx";
706 print " push ax";
707 hold=0;
708 }
709 else if (hold == 16) {
710 hold=0;
711 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
712 print " push dx";
713 print " push ax";
714 print " pop eax";
715 }
716 else if (hold == 17) {
717 hold=0;
718 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
719 }
720 if (/^ call near ptr @fileexist\$/ || # return boolean :
721 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
722 /^ call near ptr @isoreset\$/ ||
723 /^ call near ptr @isoopen\$/ ||
724 /^ call near ptr @isoreadsector\$/ ||
725 /^ call near ptr @strhead\$/ ||
726 /^ call near ptr @argstr\$/ ||
727 /^ call near ptr @argnum\$/) { print; hold=17; next; }
728 s=$0
729 # These optimisation may break ZF or CF
730 if (/^ sub sp,2$/) { print " push ax"; next }
731 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
732 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
733 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
734 sub(/mov/,"and",s); print s; next # slower
735 }
736 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
737 sub(/mov/,"or",s); print s; next # slower
738 }
739 if (/^ or .*,0$/ || /^ and .*,-1$/) next
740 if (/^ or [abcd]x,/) {
741 split($2,args,",")
742 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
743 print " or " substr(args[1],1,1) "l," args[2]; next
744 }
745 }
746 if (/^ and [abcd]x,/) {
747 split($2,args,",")
748 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
749 print " and " substr(args[1],1,1) "l," args[2]; next
750 }
751 }
752 if (/^ or e[abcd]x,/) {
753 split($2,args,",")
754 if (args[2] == "large") { args[2] = $3 }
755 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
756 print " or " substr(args[1],2,1) "l," args[2]; next
757 }
758 }
759 if (/^ and e[abcd]x,/) {
760 split($2,args,",")
761 if (args[2] == "large") { args[2] = $3 }
762 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
763 print " and " substr(args[1],2,1) "l," args[2]; next
764 }
765 }
766 if (/^ or e[abcds][ix],/) {
767 split($2,args,",")
768 if (args[2] == "large") { args[2] = $3 }
769 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
770 print " or " substr(args[1],2) "," args[2]; next
771 }
772 }
773 if (/^ and e[abcds][ix],/) {
774 split($2,args,",")
775 if (args[2] == "large") { args[2] = $3 }
776 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
777 print " and " substr(args[1],2) "," args[2]; next
778 }
779 }
780 if (/^ add word ptr/ || /^ sub word ptr/ ||
781 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
782 split($0,args,",")
783 if (isnum(args[2]) && (args[2] % 256 == 0)) {
784 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
785 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
786 print s "/256"; next
787 }
788 }
789 if (/^ add dword ptr/ || /^ sub dword ptr/) {
790 split($0,args,",")
791 if (args[2] == "large") { args[2] = $3 }
792 if (isnum(args[2])) {
793 if (args[2] % 16777216 == 0) {
794 sub(/dword/,"byte",s)
795 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
796 print s "/16777216"; next
797 }
798 if (args[2] % 65536 == 0) {
799 sub(/dword/,"word",s)
800 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
801 print s "/65536"; next
802 }
803 }
804 }
805 if (/^ mov e.x,/) {
806 split($2,args,",")
807 r=args[1]
808 if (args[2] == "large") { args[2] = $3 }
809 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
810 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
811 print " xor " r "," r
812 if (args[2] == 0) next
813 x=" mov " substr(r,2,1)
814 if (args[2] % 256 == 0) {
815 print x "h," args[2] "/256"
816 }
817 else { print x "l," args[2] }
818 next
819 }
820 }
821 }
822 if (afterjmp) print ";" $0
823 else print
824 if (/^ jmp / || /^ call near ptr _boot_kernel/ ||
825 /^ call near ptr @die$qpxzc/) afterjmp=1
826 }