This repository has been archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
uCodeOptimize.pas
4138 lines (3904 loc) · 207 KB
/
uCodeOptimize.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit uCodeOptimize;
interface
uses
System.SysUtils,Winapi.Windows,System.Classes,Vcl.ComCtrls,
System.Generics.Collections,
UnivDisasm.Cnsts.Instructions,
UnivDisasm.Cnsts.Mnemonics,
UnivDisasm.Cnsts,
UnivDisasm.Cnsts.Regs,
UnivDisasm.Disasm,
uDisAsm,
UnivDisasm.SyntaxManager;
Function _ProcessaHandler(sFile: string;VAAddr: UInt64): string;
procedure ParsingV2(var aListAsm: TList___DisAsm);
procedure ParsingCodeXJcc(var aListAsm: TList___DisAsm);
function EmuCPU(const OpCode: Word; const Size: Byte; var val1: UInt64; val2: TImmediate): Boolean;
function IsEgualBaseReg(R1, R2 : TReg ): Boolean;
var
LstD_ASM : TList___DisAsm;
fIs64 : Boolean;
implementation
function isSpReg(nReg: TReg):Boolean;
begin
Result := (nReg = REG_RSP) or (nReg = REG_ESP) or (nReg = REG_SP)
End;
function IsEgualBaseReg(R1, R2 : TReg ): Boolean;
var
R1bit8HFix,
R2bit8HFix : Byte;
begin
R1bit8HFix := 0;
R2bit8HFix := 0;
if (R1 = REG_AH) or (R1 = REG_CH) or (R1 = REG_DH) or (R1 = REG_BH) then R1bit8HFix := 4;
if (R2 = REG_AH) or (R2 = REG_CH) or (R2 = REG_DH) or (R2 = REG_BH) then R2bit8HFix := 4;
Result := (R1 and REGS_TYPE_MASK) = (R2 and REGS_TYPE_MASK);
Result := Result and ((R1 and $0F) - R1bit8HFix = (R2 and $0F) - R2bit8HFix);
end;
function Compare2MemRef(m1,m2: PMemory):Boolean ;
var
scale1,scale2 : Byte;
begin
// scale 0 disassembla scale 1
scale1 := m1.Scale;
scale2 := m2.Scale;
if scale1 = 1 then scale1 := 0;
if scale2 = 1 then scale2 := 0;
Result := m1.Flags = m2.Flags;
Result := Result and (m1.BaseReg = m2.BaseReg);
Result := Result and (m1.IndexReg = m2.IndexReg);
Result := Result and (scale1 = scale2);
end;
function Compare2Imm(im1,im2: PImmediate):Boolean ;
begin
Result := Cardinal(im1.Value) = Cardinal(im2.Value);
Result := Result and (im1.Size = im2.Size);
end;
function Compare2ImmEx(im1x,im2x: PImmediate):Boolean ;
begin
Result := CompareMem(im1x,im2x,SizeOf(TImmediate))
end;
function Compare2Reg(r1,r2: TReg):Boolean ;
begin
Result := CompareMem(@r1,@r2,SizeOf(TReg))
end;
function Compare2Arg(arg1,arg2: TArgument):Boolean ;
begin
// diverificare ne caso del parsin 64 bit
// si trovano reg. a 32bit e reg. 64bit equivalenti
Result := True;
if ((arg1.Flags and AF_TYPE_MASK) = AF_REG) or ((arg2.Flags and AF_TYPE_MASK) = AF_REG) then
begin
if ((arg1.Size = SIZE_QWORD) and (arg2.Size = SIZE_DWORD))or((arg1.Size = SIZE_DWORD) and (arg2.Size = SIZE_QWORD)) then
Result := IsEgualBaseReg(arg1.Reg,arg2.Reg)
else
Result := Compare2Reg(arg1.Reg,arg2.Reg);
end;
Result := Result and Compare2MemRef(@arg1.Mem,@arg2.Mem);
if ((arg1.Size = SIZE_QWORD) and (arg2.Size = SIZE_DWORD))or((arg1.Size = SIZE_DWORD) and (arg2.Size = SIZE_QWORD)) then
// Modified by Max 24/01/2016 18:02:09 per operazioni su codice a 64bit tanto sopno sempre dword in questo caso!!!!!!
Result := Result and (Cardinal(arg1.Imm.Value) = Cardinal(arg2.Imm.Value))
else
Result := Result and Compare2Imm(@arg1.Imm,@arg2.Imm);
Result := Result and Compare2ImmEx(@arg1.ImmEx,@arg2.ImmEx);
Result := Result and (arg1.Flags = arg2.Flags);
end;
function FindFoldable(Cmd : Word; isMov, isOneOp, isTwoOp : byte): Boolean;
begin
if ( isMov <> 1) or (Cmd <> INST_ID_MOV ) then
begin
if (isOneOp <> 1) or ( (Cmd <> INST_ID_INC ) and (Cmd <> INST_ID_DEC ) and (Cmd <> INST_ID_NEG ) and (Cmd <> INST_ID_NOT ) ) then
Result := (isTwoOp = 1) and ( (Cmd = INST_ID_ADD) or (Cmd = INST_ID_SUB) or (Cmd = INST_ID_XOR) or (Cmd = INST_ID_OR) or
(Cmd = INST_ID_AND) or (Cmd = INST_ID_SHL) or (Cmd = INST_ID_SHR))
else
Result := True;
end else
Result := True;
end;
function FIndIncDec(Operando : TInstruction): Boolean;
begin
Result := ((Operando.InstID = INST_ID_ADD) or (Operando.InstID = INST_ID_SUB)) and
(Operando.Arg2.Flags and AF_TYPE_MASK = AF_IMM) and
( (UInt64(Operando.Arg2.Imm.Value) = UInt64(-1) ) or (Operando.Arg2.Imm.Value = 1) )
end;
function IsFoldableReg(Reg:TReg):Boolean;
var
bReg : Word;
begin
Result := False;
bReg := Reg and $FF;
if (bReg = RID_EAX) or (bReg = RID_ECX) or (bReg = RID_EDX) or (bReg = RID_EBX) or (bReg = RID_EBP)then
Result := True;
if Result = False then
Result := (Reg = REG_AL) or (Reg = REG_CL) or (Reg = REG_DL) or (Reg = REG_BL) or (Reg = REG_AH) or
(Reg = REG_CH) or (Reg = REG_DH) or (Reg = REG_BH)
end;
function EmuCPU(const OpCode: Word; const Size: Byte; var val1: UInt64; val2: TImmediate): Boolean; overload;
const
UNITY = $20002; (* operand equals 1 *)
SBYTEWORD = $40002; (* operand is in the range -128..127 mod 2^16 *)
SBYTEDWORD = $80002; (* operand is in the range -128..127 mod 2^32 *)
SDWORD = $100002; (* operand is in the range -0x80000000..0x7FFFFFFF *)
UDWORD = $200002; (* operand is in the range 0..0xFFFFFFFF *)
UBYTEWORD = $400002; (* operand is in the range 0..0xFF mod 2^16 *)
UBYTEDWORD = $800002; (* operand is in the range 0..0xFF mod 2^32 *)
var
nVal32 : Int32;
nValU32: UInt32;
nValI64: Int64;
nValU64: UInt64;
nB : Byte absolute val1;
nW : Word absolute val1;
nD : DWord absolute val1;
nInt : Int64;
ImmSize: DWORD;
procedure CalcRealSize(n: UInt64);
begin
ImmSize := 0;
nInt := n and $7FFFFFFFFFFFFFFF;
if n <= High(Byte) then nInt := n and $7F;
if n <= High(Word) then nInt := n and $7FFF;
if n <= High(Cardinal) then nInt := n and $7FFFFFFF;
if (Cardinal(nInt + 128) <= 255) then ImmSize := ImmSize or SBYTEDWORD;
if (Cardinal(nInt) <= 255) then ImmSize := ImmSize or UBYTEDWORD;
if (UInt16(nInt + 128) <= 255) then ImmSize := ImmSize or SBYTEWORD;
if (UInt16(nInt) <= 255) then ImmSize := ImmSize or UBYTEWORD;
if (nInt <= $FFFFFFFF) then ImmSize := ImmSize or UDWORD;
if (nInt + $80000000 <= $FFFFFFFF)then ImmSize := ImmSize or SDWORD;
end;
function GetI32(n : TImmediate): Int32;
begin
Result := 0;
case n.Size of
SIZE_BYTE : Result := Int8(n.Value);
SIZE_WORD : Result := Int16(n.Value);
SIZE_DWORD: Result := Int32(n.Value);
end;
if n.Size = SIZE_DWORD then
begin
if n.Value = 255 then
Result := -1;
end;
end;
function GetI64(n : TImmediate): Int64;
begin
Result := 0;
case n.Size of
SIZE_BYTE : Result := Int8(n.Value);
SIZE_WORD : Result := Int16(n.Value);
SIZE_DWORD: Result := Int32(n.Value);
SIZE_QWORD: Result := Int64(n.Value);
end;
CalcRealSize(n.Value);
// fix signed /unsigned constant in x64 context
if int32(n.Value) <> int64(n.Value) then
Result := Uint64(n.Value) ;
end;
begin
Result := True;
if fIs64 = False then
begin
nValU32 := UInt32(val1) ;
nVal32 := GetI32(val2);
// Modified by Max 24/01/2016 22:36:46 fb00000007
Val1 := 0;
if Size = SIZE_DWORD then
begin
case OpCode of
INST_ID_SHL: nD := nValU32 shl nVal32;
INST_ID_SHR: nD := nValU32 shr nVal32;
INST_ID_XOR: nD := nValU32 xor nVal32;
INST_ID_OR: nD := nValU32 or nVal32;
INST_ID_SUB: nD := nValU32 - nVal32;
INST_ID_NOT: nD := not nValU32;
INST_ID_INC: nD := nValU32 + 1 ;
INST_ID_DEC: nD := nValU32 - 1;
INST_ID_NEG: nD := -nValU32;
INST_ID_AND: nD := nValU32 and nVal32;
INST_ID_ADD: nD := nValU32 + nVal32;
else
Result := False
end;
end
else if Size = SIZE_WORD then
begin
case OpCode of
INST_ID_SHL: nW := nValU32 shl nVal32;
INST_ID_SHR: nW := nValU32 shr nVal32;
INST_ID_XOR: nW := nValU32 xor nVal32;
INST_ID_OR: nW := nValU32 or nVal32;
INST_ID_SUB: nW := nValU32 - nVal32;
INST_ID_NOT: nW := not nValU32;
INST_ID_INC: nW := nValU32 + 1 ;
INST_ID_DEC: nW := nValU32 - 1;
INST_ID_NEG: nW := -nValU32;
INST_ID_AND: nW := nValU32 and nVal32;
INST_ID_ADD: nW := nValU32 + nVal32;
else
Result := False
end;
end
else if Size = SIZE_BYTE then
begin
case OpCode of
INST_ID_SHL: nB := nValU32 shl nVal32;
INST_ID_SHR: nB := nValU32 shr nVal32;
INST_ID_XOR: nB := nValU32 xor nVal32;
INST_ID_OR: nB := nValU32 or nVal32;
INST_ID_SUB: nB := nValU32 - nVal32;
INST_ID_NOT: nB := not nValU32;
INST_ID_INC: nB := nValU32 + 1 ;
INST_ID_DEC: nB := nValU32 - 1;
INST_ID_NEG: nB := -nValU32;
INST_ID_AND: nB := nValU32 and nVal32;
INST_ID_ADD: nB := nValU32 + nVal32;
else
Result := False
end;
end;
end else {per operazioni 64bit }
begin
nValU64 := val1 ;
nValI64 := GetI64(val2);
// Modified by Max 24/01/2016 22:36:46 fb00000007
Val1 := 0;
if Size = SIZE_QWORD then
begin
case OpCode of
INST_ID_SHL: val1 := nValU64 shl nValI64;
INST_ID_SHR: val1 := nValU64 shr nValI64;
INST_ID_XOR: val1 := nValU64 xor nValI64;
INST_ID_OR: val1 := nValU64 or nValI64;
INST_ID_SUB: val1 := nValU64 - nValI64;
INST_ID_NOT: val1 := not nValU64;
INST_ID_INC: val1 := nValU64 + 1 ;
INST_ID_DEC: val1 := nValU64 - 1;
INST_ID_NEG: val1 := -nValU64;
INST_ID_AND: val1 := nValU64 and nValI64;
INST_ID_ADD: val1 := nValU64 + nValI64;
else
Result := False
end
end
else if Size = SIZE_DWORD then
begin
case OpCode of
INST_ID_SHL: nD := nValU64 shl nValI64;
INST_ID_SHR: nD := nValU64 shr nValI64;
INST_ID_XOR: nD := nValU64 xor nValI64;
INST_ID_OR: nD := nValU64 or nValI64;
INST_ID_SUB: nD := nValU64 - nValI64;
INST_ID_NOT: nD := not nValU64;
INST_ID_INC: nD := nValU64 + 1 ;
INST_ID_DEC: nD := nValU64 - 1;
INST_ID_NEG: nD := -nValU64;
INST_ID_AND: nD := nValU64 and nValI64;
INST_ID_ADD: nD := nValU64 + nValI64;
else
Result := False
end;
end
else if Size = SIZE_WORD then
begin
case OpCode of
INST_ID_SHL: nW := nValU64 shl nValI64;
INST_ID_SHR: nW := nValU64 shr nValI64;
INST_ID_XOR: nW := nValU64 xor nValI64;
INST_ID_OR: nW := nValU64 or nValI64;
INST_ID_SUB: nW := nValU64 - nValI64;
INST_ID_NOT: nW := not nValU64;
INST_ID_INC: nW := nValU64 + 1 ;
INST_ID_DEC: nW := nValU64 - 1;
INST_ID_NEG: nW := -nValU64;
INST_ID_AND: nW := nValU64 and nValI64;
INST_ID_ADD: nW := nValU64 + nValI64;
else
Result := False
end;
end
else if Size = SIZE_BYTE then
begin
case OpCode of
INST_ID_SHL: nB := nValU64 shl nValI64;
INST_ID_SHR: nB := nValU64 shr nValI64;
INST_ID_XOR: nB := nValU64 xor nValI64;
INST_ID_OR: nB := nValU64 or nValI64;
INST_ID_SUB: nB := nValU64 - nValI64;
INST_ID_NOT: nB := not nValU64;
INST_ID_INC: nB := nValU64 + 1 ;
INST_ID_DEC: nB := nValU64 - 1;
INST_ID_NEG: nB := -nValU64;
INST_ID_AND: nB := nValU64 and nValI64;
INST_ID_ADD: nB := nValU64 + nValI64;
else
Result := False
end;
end;
CalcRealSize(val1);
// Fix cont emu in x64 ctx
if ((ImmSize and UBYTEDWORD ) = UBYTEDWORD ) then
begin
if ((ImmSize and UDWORD ) <> UDWORD ) then
val1 := val1 and $FF;
end
else if ((ImmSize and UBYTEWORD ) = UBYTEWORD ) then
begin
if ((ImmSize and UDWORD ) <> UDWORD ) then
val1 := val1 and $FF
end;
end;
if Result = False then
MessageBoxA(0, '[EmuCPU] Id Comando Sconosciuto', 'Errore', MB_OK);
end;
function EmuCPU_EFLAGS(iCmd : Word; vSize: Byte; Operand1, Operand2: UInt64; var eFlags: NativeUInt): Byte;
var
dwEFlags: DWORD;
begin
Result := 1;
asm
pushad
cmp vSize,1
je @isByte
cmp vSize,2
je @isWord
jmp @isNativeuInt
@isByte:
mov al, Byte(Operand1)
mov cl, Byte(Operand2)
jmp @TipoOperaz
@isWord:
mov ax, Word(Operand1)
mov cx, Word(Operand2)
jmp @TipoOperaz
@isNativeuInt:
mov eax, dword(Operand1)
mov ecx, dword(Operand2)
jmp @TipoOperaz
@TipoOperaz:
cmp iCmd, INST_ID_SHL
je @_shl
cmp iCmd, INST_ID_SHR
je @_shr
cmp iCmd, INST_ID_XOR
je @_xor
cmp iCmd, INST_ID_OR
je @_or
cmp iCmd, INST_ID_SUB
je @_sub
cmp iCmd, INST_ID_NOT
je @_not
cmp iCmd, INST_ID_INC
je @_inc
cmp iCmd, INST_ID_DEC
je @_dec
cmp iCmd, INST_ID_NEG
je @_neg
cmp iCmd, INST_ID_AND
je @_and
cmp iCmd, INST_ID_ADD
je @_add
jmp @NotFound
@_shl:
cmp vSize,1
jne @nextSize
shl al,cl
jmp @GetEFlags
@nextSize:
cmp vSize,2
jne @nextSize1
shl ax,cl
jmp @GetEFlags
@nextSize1:
shl eax,cl
jmp @GetEFlags
@_shr:
cmp vSize,1
jne @nextSize2
shr al,cl
jmp @GetEFlags
@nextSize2:
cmp vSize,2
jne @nextSize3
shr ax,cl
jmp @GetEFlags
@nextSize3:
shr eax,cl
jmp @GetEFlags
@_xor:
cmp vSize,1
jne @nextSize4
xor al,cl
jmp @GetEFlags
@nextSize4:
cmp vSize,2
jne @nextSize5
xor ax,cx
jmp @GetEFlags
@nextSize5:
xor eax,ecx
jmp @GetEFlags
@_or:
cmp vSize,1
jne @nextSize6
or al,cl
jmp @GetEFlags
@nextSize6:
cmp vSize,2
jne @nextSize7
or ax,cx
jmp @GetEFlags
@nextSize7:
or eax,ecx
jmp @GetEFlags
@_sub:
cmp vSize,1
jne @nextSize8
sub al,cl
jmp @GetEFlags
@nextSize8:
cmp vSize,2
jne @nextSize9
sub ax,cx
jmp @GetEFlags
@nextSize9:
sub eax,ecx
jmp @GetEFlags
@_not:
cmp vSize,1
jne @nextSizeA
not al
jmp @GetEFlags
@nextSizeA:
cmp vSize,2
jne @nextSizeB
not ax
jmp @GetEFlags
@nextSizeB:
not eax
jmp @GetEFlags
@_inc:
cmp vSize,1
jne @nextSizeC
inc al
jmp @GetEFlags
@nextSizeC:
cmp vSize,2
jne @nextSizeD
inc ax
jmp @GetEFlags
@nextSizeD:
inc eax
jmp @GetEFlags
@_dec:
cmp vSize,1
jne @nextSizeE
dec al
jmp @GetEFlags
@nextSizeE:
cmp vSize,2
jne @nextSizeF
dec ax
jmp @GetEFlags
@nextSizeF:
dec eax
jmp @GetEFlags
@_neg:
cmp vSize,1
jne @nextSize10
neg al
jmp @GetEFlags
@nextSize10:
cmp vSize,2
jne @nextSize11
neg ax
jmp @GetEFlags
@nextSize11:
neg eax
jmp @GetEFlags
@_and:
cmp vSize,1
jne @nextSize12
and al,cl
jmp @GetEFlags
@nextSize12:
cmp vSize,2
jne @nextSize13
and ax,cx
jmp @GetEFlags
@nextSize13:
and eax,ecx
jmp @GetEFlags
@_add:
cmp vSize,1
jne @nextSize14
add al,cl
jmp @GetEFlags
@nextSize14:
cmp vSize,2
jne @nextSize15
add ax,cx
jmp @GetEFlags
@nextSize15:
add eax,ecx
jmp @GetEFlags
@NotFound:
mov Result,0
jmp @Fine
@GetEFlags:
Pushfd
Pop dwEFlags
@Fine:
popad
end;
eFlags := dwEFlags;
if Result = 0 then
MessageBoxA(0, '[EmuCPU_EFLAGS] Id Comando Sconosciuto', 'Errore', MB_OK);
end;
function TestEFlagsJcc(jcc_Cmd, eFlags: Word): Boolean;
begin
case jcc_Cmd of
INST_ID_JNBE: Result := ((eFlags and 1)= 0) and (((eFlags shr 6) and 1)=0); // Ja
INST_ID_JNB: Result := (eFlags and 1) = 0; // Jnb
INST_ID_JB: Result := (eFlags and 1) = 1; // Jb
INST_ID_JBE: Result := ( (eFlags and 1) = 1 ) or ( ((eFlags shr 6) and 1) = 1 ); // Jbe
INST_ID_JZ: Result := ((eFlags shr 6) and 1) = 1; // Je
INST_ID_JNLE: Result := ((eFlags shr 7) and 1) = ((HI(eFlags) shr 3) and 1) and ( not ((eFlags shr 6) and 1) ) ; // Jg
INST_ID_JNL: Result := ((eFlags shr 7) and 1) = ((HI(eFlags) shr 3) and 1); // Jge
INST_ID_JL: Result := ((eFlags shr 7) and 1) <> ((HI(eFlags) shr 3) and 1); // Jl
INST_ID_JLE: Result := ( ((eFlags shr 7) and 1) <> ((HI(eFlags) shr 3) and 1) ) or ( ((eFlags shr 6) and 1) = 1 ); // Jle
INST_ID_JNZ: Result := ((eFlags shr 6) and 1) = 0; // Jnz
INST_ID_JNO: Result := ((HI(eFlags) shr 3) and 1) = 0; // Jno
INST_ID_JNP: Result := ((eFlags shr 2) and 1) = 0; // Jnp
INST_ID_JNS: Result := ((eFlags shr 7) and 1) = 0; // Jns
INST_ID_JO: Result := ((HI(eFlags) shr 3) and 1) = 1; // Jo
INST_ID_JP: Result := ((eFlags shr 2) and 1) = 1; // Jp
INST_ID_JS: Result := ((eFlags shr 7) and 1) = 1; // Js
else begin
MessageBoxA(0, '[TestEFlagsJcc] Id Jcc Sconosciuto', 'Errore', MB_OK);
Result := False;
end;
end;
end;
Function j_GetDati_Operaz(ListaIstr: TList___DisAsm; var Istruz_Index_Start: NativeUInt; var nCostante: UInt64): Boolean;
var
vIndexIstr, LastIdx : NativeUInt;
begin
vIndexIstr := ListaIstr.Count ;
LastIdx := vIndexIstr - 1;
while true do
Begin
if vIndexIstr = 0 then break;
Dec(vIndexIstr);
if not FindFoldable(ListaIstr[vIndexIstr].ins.InstID, 1, 1, 1) then
begin
Result := False;
Exit;
end;
if FindFoldable(ListaIstr[vIndexIstr].ins.InstID, 1, 0, 1) then
begin
if (ListaIstr[vIndexIstr].ins.Arg1.Flags and AF_TYPE_MASK <> AF_REG) and (ListaIstr[vIndexIstr].ins.Arg1.Flags and AF_TYPE_MASK <> AF_MEM ) then
begin
Result := False;
Exit;
end;
if ListaIstr[vIndexIstr].ins.Arg2.Flags and AF_TYPE_MASK <> AF_IMM then
begin
Result := False;
Exit;
end;
if not Compare2Arg(ListaIstr[vIndexIstr].ins.Arg1,ListaIstr[LastIdx].ins.Arg1) then
Begin
Result := False;
Exit;
end;
if FindFoldable(ListaIstr[vIndexIstr].ins.InstID, 1, 0, 0) then
begin
nCostante := ListaIstr[vIndexIstr].ins.Arg2.Imm.Value;
Istruz_Index_Start := vIndexIstr;
Result := True;
Exit;
end;
end;
if FindFoldable(ListaIstr[vIndexIstr].ins.InstID, 0, 1, 0) then
begin
if (ListaIstr[vIndexIstr].ins.Arg1.Flags and AF_TYPE_MASK <> AF_REG) and (ListaIstr[vIndexIstr].ins.Arg1.Flags and AF_TYPE_MASK <> AF_MEM ) then
begin
Result := False;
Exit;
end;
if not Compare2Arg(ListaIstr[vIndexIstr].ins.Arg1,ListaIstr[LastIdx].ins.Arg1) then
Begin
Result := False;
Exit;
end;
end;
end; // While
Result := True;
end;
function MultiBranchSystem(var ListaIstr: TList___DisAsm; jcc_Cmd: Word; var lEsegueSalto: Boolean):Boolean;
var
nLastItem,i : Integer;
RetArg1 : UInt64;
Eflags,idxStart : NativeUInt;
function ParsingAndGetDati: Boolean;
begin
ParsingCodeXJcc(ListaIstr);
Result := j_GetDati_Operaz(ListaIstr,idxStart,UInt64(RetArg1));
nLastItem := ListaIstr.Count - 1;
end;
begin
nLastItem := ListaIstr.Count - 1;
if nLastItem < 0 then
begin
Result := False;
Exit;
end;
// Mov
if ( ListaIstr[nLastItem ].ins.InstID = INST_ID_MOV ) then
Result := False
// cmp
else if ( ListaIstr[nLastItem ].ins.InstID = INST_ID_CMP) or ( ListaIstr[nLastItem ].ins.InstID = INST_ID_CMPXCHG) then
Result := True
// test
else if ( ListaIstr[nLastItem ].ins.InstID = INST_ID_TEST ) then
Result := True
// or reg_1,Reg_1
else if ( ListaIstr[nLastItem ].ins.InstID = INST_ID_OR) and
( ListaIstr[nLastItem ].ins.Arg1.Flags and AF_TYPE_MASK = AF_REG) and
( ListaIstr[nLastItem ].ins.Arg2.Flags and AF_TYPE_MASK = AF_REG) and
( ListaIstr[nLastItem ].ins.Arg1.Reg = ListaIstr[nLastItem].ins.Arg2.Reg) then
Result := True
else begin
if j_GetDati_Operaz(ListaIstr,idxStart,UInt64(RetArg1)) or ParsingAndGetDati then
begin
for i := idxStart+1 to nLastItem do
begin
if ListaIstr[nLastItem - 1].ins.Arg1.Flags and AF_TYPE_MASK = AF_REG then
begin
EmuCPU_EFLAGS(ListaIstr[i].ins.InstID,
(ListaIstr[i].ins.Arg1.Reg shr 8) and $F,
RetArg1,
ListaIstr[i].ins.Arg2.Imm.Value,
Eflags);
EmuCPU (ListaIstr[i].ins.InstID,
(ListaIstr[i].ins.Arg1.Reg shr 8) and $F,
RetArg1,
ListaIstr[i].ins.Arg2.Imm);
end
else begin
EmuCPU_EFLAGS(ListaIstr[i].ins.InstID,
ListaIstr[i].ins.Arg1.Size ,
RetArg1,
ListaIstr[i].ins.Arg2.Imm.Value,
Eflags);
EmuCPU (ListaIstr[i].ins.InstID,
ListaIstr[i].ins.Arg1.Size,
RetArg1,
ListaIstr[i].ins.Arg2.Imm);
end;
end;
lEsegueSalto := TestEFlagsJcc(jcc_Cmd, Eflags and $FFFF);
Result := True;
end
else Result := False;
end;
end;
Function _ProcessaHandler(sFile: string;VAAddr: UInt64): string;
var
DisAsm : TDisAsm;
k : Integer;
len : Integer;
Indirizzo : UInt64;
bx86 : Boolean;
s1 : AnsiString;
isFollowJcc : Boolean;
FAsmList : TStringStream;
emulEFlags : DWORD;
function IsCodExist(Addr: UInt64): Boolean;
var
i : Integer;
begin
Result := False;
for i := 0 to LstD_ASM.Count - 1 do
begin
if UInt64(LstD_ASM.Items[i].VAddr) = Addr then
begin
Result := True;
Break;
end;
end;
end;
function Validat_Jcc: Boolean;
begin
Result := False ;
if LstD_ASM.Count < 1 then
Exit;
with LstD_ASM.Items[LstD_ASM.Count - 1] do
begin
if (ins.InstID = INST_ID_CMP) or (ins.InstID = INST_ID_CMPXCHG) then
Result := True
else if ins.InstID = INST_ID_TEST then
Result := True
else if (ins.InstID = INST_ID_OR) and (ins.Arg1.Flags and AF_TYPE_MASK = AF_REG) and (ins.Arg2.Flags and AF_TYPE_MASK = AF_REG) and (ins.Arg1.Reg = ins.Arg2.Reg) then
Result := True
end;
end;
begin
Result := 'Proc_'+ IntToHex(VAAddr,8)+'.asm';
Indirizzo := VAAddr;
asm
pushfd
pop emulEFlags
end;
DisAsm := TDisAsm.Create(AnsiString(sFile));
LstD_ASM := TList<TInsData>.Create;
bx86 := (DisAsm.BinType = CPUX32);
FAsmList := TStringStream.Create;
try
try
while True do
begin
if LstD_ASM.Count > 20000 then Break;
DisAsm.DisAssembleVA(VAAddr);
len := DisAsm.InsData.nsize;
if len <= 0 then len := 1;
VAAddr := VAAddr + len;
if DisAsm.InsData.ins.InstID = INST_ID_RDTSC then
begin
Continue;
end
else if (DisAsm.InsData.ins.InstID = INST_ID_CALL) and (DisAsm.InsData.ins.DstAddr.Addr <> nil) then
begin
DisAsm.DisAssembleVA(UInt64(DisAsm.InsData.ins.DstAddr.Addr));
if (DisAsm.InsData.ins.InstID = INST_ID_JMP) and (DisAsm.InsData.ins.DstAddr.Addr <> nil) then
begin
VAAddr := UInt64(DisAsm.InsData.ins.DstAddr.Addr);
Continue;
end else
begin
VAAddr := VAAddr - len ;
DisAsm.DisAssembleVA(VAAddr);
len := DisAsm.InsData.nsize;
if len <= 0 then len := 1;
VAAddr := VAAddr + len;
end;
end
else if (DisAsm.InsData.ins.InstID = INST_ID_JMP) and (DisAsm.InsData.ins.DstAddr.Addr <> nil) then
begin
if not IsCodExist(UInt64(DisAsm.InsData.ins.DstAddr.Addr)) then
begin
VAAddr := UInt64(DisAsm.InsData.ins.DstAddr.Addr);
Continue;
end
else begin
//SetLength(LstD_ASM,Length(LstD_ASM)-1);
//Continue;
end;
end
else if (DisAsm.InsData.ins.InstID = INST_ID_JMP) and ((DisAsm.InsData.ins.Arg1.Flags and AF_TYPE_MASK) = AF_REG) then
begin
LstD_ASM.Add(DisAsm.InsData);
Break;
end
else if (DisAsm.InsData.ins.InstID = INST_ID_JMP) and ((DisAsm.InsData.ins.Arg1.Flags and AF_TYPE_MASK) = AF_MEM) then
begin
LstD_ASM.Add(DisAsm.InsData);
Break;
end
else if (DisAsm.InsData.ins.InstID = INST_ID_RET) then
begin
LstD_ASM.Add(DisAsm.InsData);
Break;
end
else if (DisAsm.InsData.ins.InstID >= INST_ID_JB) and (DisAsm.InsData.ins.InstID <= INST_ID_JZ) then
begin
isFollowJcc := False;
LstD_ASM.TrimExcess;
if not MultiBranchSystem(LstD_ASM,DisAsm.InsData.ins.InstID,isFollowJcc) then
begin
if Uint64(DisAsm.InsData.ins.DstAddr.Addr) = VAAddr then
isFollowJcc := True
else begin
isFollowJcc := TestEFlagsJcc(DisAsm.InsData.ins.InstID, emulEFlags and $FFFF);
//s1 := Format('Address: %08x Id: %d Follow Jump?',[VAAddr-len, FDisAsm.LasDisData.CmdIdx]);
//if MessageBox(0,PChar(s1),'MultiBranch Info',MB_YESNO) = IDYES then isFollowJcc := True;
end;
end;
if isFollowJcc then
VAAddr := UInt64(DisAsm.InsData.ins.DstAddr.Addr);
if not Validat_Jcc then
begin
// SetLength(LstD_ASM,Length(LstD_ASM)-1);
Continue;
end;
if IsCodExist(VAAddr) then
break;
end;
LstD_ASM.Add(DisAsm.InsData);
end;
FAsmList.Seek(0,soFromEnd);
FAsmList.WriteString('; '+sFile +#13#10#13#10);
FAsmList.WriteString(';<'+IntToHex(Indirizzo,8)+'>' +#13#10#13#10);
if bx86 then
FAsmList.WriteString('Bits 32'+#13#10#13#10)
else
FAsmList.WriteString('Bits 64'+#13#10#13#10);
for k := 0 to LstD_ASM.Count - 1 do
begin
FAsmList.Seek(0,soFromEnd);
s1 := LstD_ASM.Items[k].ins.InstStr;
if bx86 then FAsmList.WriteString(IntToHex(UInt64(LstD_ASM.Items[k].VAddr),4)+': '+String(s1)+#13#10)
else FAsmList.WriteString(IntToHex(UInt64(LstD_ASM.Items[k].VAddr),8)+': '+String(s1)+#13#10) ;
end;
FAsmList.SaveToFile(Result);
except
MessageBox(0,'linea errore','errore',MB_OK);
end;
finally
FAsmList.Free;
DisAsm.Free;
end;
end;
procedure AggiornaIstruzione(var aList : TList___DisAsm; idx: Integer);
var
Istr : TInsData;
p : PByte;
begin
Istr := aList[idx];
P := PByte(Istr.ins.InstStr);
SyntaxManager.SyntaxDecoderArray[Istr.ins.InternalData.SyntaxID](@Istr.ins);
Istr.ins.InstStr := PAnsiChar(P);
aList[idx] := Istr;
end;
(*********************)
function PeepHolePush(var aList : TList___DisAsm): Boolean;
var
i,k : Integer;
Istr : TInsData;
begin
i := 0;
Result := True;
try
repeat
if (i+1) <= aList.Count - 1 then
begin
// Junk_Push_01
//-------------
// sub esp, 2/4
// mov [esp 16/32], x -----> push x
// if item3 Add [esp],2/4 -----> push x
if (aList[i].ins.InstID = INST_ID_SUB) and (aList[i+1].ins.InstID = INST_ID_MOV) and
(aList[i].ins.Arg1.Flags and AF_TYPE_MASK = AF_REG) and (aList[i+1].ins.Arg2.Flags and AF_TYPE_MASK = AF_REG) and
(aList[i].ins.Arg2.Flags and AF_TYPE_MASK = AF_IMM) and (aList[i+1].ins.Arg1.Flags and AF_TYPE_MASK = AF_MEM) and
(aList[i+1].ins.Arg1.Size <> SIZE_BYTE) and ( isSpReg(aList[i].ins.Arg1.Reg) ) and
((aList[i].ins.Arg2.Imm.Value = 2) or (aList[i].ins.Arg2.Imm.Value = 4)or (aList[i].ins.Arg2.Imm.Value = 8)) and
( isSpReg(aList[i+1].ins.Arg1.Mem.BaseReg)) and
(aList[i+1].ins.Arg1.Mem.IndexReg = REG_NIL) and
(aList[i+1].ins.Arg1.Mem.Scale = 0) and
(aList[i+1].ins.disp.Value = 0) then
begin
if isSpReg(aList[i+1].ins.Arg2.Reg) then
begin
if (aList[i+2].ins.InstID = INST_ID_ADD) and
(aList[i+2].ins.Arg1.Flags and AF_TYPE_MASK = AF_MEM) and
(aList[i+2].ins.Arg2.Flags and AF_TYPE_MASK = AF_IMM) and
(isSpReg(aList[i+2].ins.Arg1.Mem.BaseReg)) and
(aList[i+2].ins.Arg1.Mem.IndexReg = REG_NIL) and
(aList[i+2].ins.Arg1.Mem.Scale = 0) and
(aList[i+2].ins.Disp.Value = 0) and
((aList[i+2].ins.Arg2.Imm.Value = 2) or (aList[i+2].ins.Arg2.Imm.Value = 4)or (aList[i+2].ins.Arg2.Imm.Value = 8)) then
begin
// Aggiorna Istruzione
Istr := aList[i];
istr.ins.Mnem := MNEM_PUSH;
istr.ins.InstID := INST_ID_PUSH;
Istr.ins.nArg := 1;
Istr.ins.Arg1 := aList[i+1].ins.Arg2;
if Istr.ins.Arg1.Flags and AF_TYPE_MASK = AF_MEM then
Istr.ins.Disp := aList[i+1].ins.Disp;
ZeroMemory(@Istr.ins.Arg2,SizeOf(TArgument));
aList[i] := Istr;
AggiornaIstruzione(aList,i);
aList.Delete(i+1);