forked from crypto2011/KBBUILDER
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCU32.pas
4851 lines (4669 loc) · 132 KB
/
DCU32.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 DCU32;
(*
The DCU parser module of the DCU32INT utility by Alexei Hmelnov.
(All the DCU data structures are described here and in the DCURecs module)
----------------------------------------------------------------------------
E-Mail: [email protected]
http://hmelnov.icc.ru/DCU/
----------------------------------------------------------------------------
See the file "readme.txt" for more details.
------------------------------------------------------------------------
IMPORTANT NOTE:
This software is provided 'as-is', without any expressed or implied warranty.
In no event will the author be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented, you must not
claim that you wrote the original software.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
{$IFNDEF VER90}
{$IFNDEF VER100}
{$REALCOMPATIBILITY ON}
{$ENDIF}
{$ENDIF}
{$IFDEF MSWINDOWS}
{$DEFINE WIN}
{$ENDIF}
{$IFNDEF WIN}
{$IFDEF WIN32}
{$DEFINE WIN}
{$ENDIF}
{$ENDIF}
interface
uses
{$IFDEF UNICODE}AnsiStrings,{$ENDIF}
{SysUtils, moved to implementation to check ANSIStrings.StrScan} Classes, DasmDefs, DCU_In, DCU_Out, FixUp, DCURecs, Win64SEH
{$IFDEF WIN},Windows{$ENDIF};
{$IFDEF UNICODE}
{$IF declared(StrScan)} //declared(AnsiStrings.StrScan) doesn`t work
{$DEFINE ANSIStr}
{$IFEND}
{$ENDIF}
const {My own (AX) codes for Delphi/Kylix versions, the Delphi codes
correspond to the Delphi Product Versions}
verD2=2;
verD3=3;
verD4=4;
verD5=5;
verD6=6;
verD7=7;
verD8=8;
verD2005=9; //2005
verD2006=10; //2006
verD2009=12; //2009
verD2010=14; //2010
verD_XE=15; //XE
verD_XE2=16; //XE2
verD_XE3=17; //XE3
verD_XE4=18; //XE4
verD_XE5=19; //XE5
verD_XE6=20; //XE6
//verAppMethod=20; //AppMethod
verD_XE7=21; //XE7&AppMethod
verD_XE8=22; //XE8
verD_10=23; //10 Seattle
verD_10_1=24; //10.1 Berlin
verD_10_2=25; //10.2 Tokyo
verD_10_3=26; //10.2 Tokyo
verK1=100; //Kylix 1.0
verK2=101; //Kylix 2.0
verK3=102; //Kylix 3.0
MaxDelphiVer = 26;
type
TDCUPlatform = (dcuplWin32,dcuplWin64,dcuplOsx32,dcuplIOSEmulator,
dcuplIOSDevice,dcuplIOSDevice64,dcuplAndroid,dcuplLinux64);
const
MobilePlatforms = [dcuplIOSEmulator,dcuplIOSDevice,dcuplIOSDevice64,dcuplAndroid];
LLVMPlatforms = [dcuplIOSDevice,dcuplIOSDevice64,dcuplAndroid,dcuplLinux64];
Platforms64bit = [dcuplWin64,dcuplIOSDevice64,dcuplLinux64];
{ Internal unit types }
const
drStop=0;
drStop_a=$61{'a'}; //Last Tag in all files
drAssemblyData=$62{'b'}; //The data structure was found in .<PackageName> units of D8 packages
drStop1=$63{'c'};
drUnit=$64{'d'};
drUnit1=$65{'e'}; //in implementation
drImpType=$66{'f'};
drImpVal=$67{'g'};
drDLL=$68{'h'};
drExport=$69{'i'};
drEmbeddedProcStart=$6A{'j'};
drEmbeddedProcEnd=$6B{'k'};
drCBlock=$6C{'l'};
drFixUp=$6D{'m'};
drImpTypeDef=$6E{'n'}; //import of type definition by "A = type B"
drSrc=$70{'p'};
drObj=$71{'q'};
drRes=$72{'r'};
drAsm=$73{'s'}; //Found in D5 Debug versions
drAssemblySrc=$74{'t'}; //For .net assembly the *.DCP is generated automatically, so the assembly is its source
drStop2=$9F{'Ÿ'}; //!!!
drConst=$25{'%'};
drResStr=$32{'2'};
drType=$2A{'*'};
drTypeP=$26{'&'};
drProc=$28{'('};
drSysProc=$29{')'};
drVoid=$40{'@'};
drVar=$20{' '};
drThreadVar=$31{'1'};
drVarC=$27{'''};
drBoolRangeDef=$41{'A'};
drChRangeDef=$42{'B'};
drEnumDef=$43{'C'};
drRangeDef=$44{'D'};
drPtrDef=$45{'E'};
drClassDef=$46{'F'};
drObjVMTDef=$47{'G'};
drProcTypeDef=$48{'H'};
drFloatDef=$49{'I'};
drSetDef=$4A{'J'};
drShortStrDef=$4B{'K'};
drArrayDef=$4C{'L'};
drRecDef=$4D{'M'};
drObjDef=$4E{'N'};
drFileDef=$4F{'O'};
drTextDef=$50{'P'};
drWCharRangeDef=$51{'Q'}; //WideChar
drStringDef=$52{'R'};
drVariantDef=$53{'S'};
drInterfaceDef=$54{'T'};
drWideStrDef=$55{'U'};
drWideRangeDef=$56{'V'};
//Various tables
drCodeLines=$90;
drLinNum=$91;
drStrucScope=$92;
drSymbolRef=$93;
drLocVarTbl=$94;
drUnitFlags=$96;
//ver70 or higher tags (all of unknown purpose)
drUnitAddInfo=$34{'4'};
drCPPFlags=$98;
drConstAddInfo=$9C;
drProcAddInfo=$9E;
drAssemblyInfo=$9D; //Ver 2005,2006 .Net
//ver80 or higher tags (some of unknown purpose)
drORec=$6F{'o'}; //goes before drCBlock in MSIL
drStrConstRec=$35{'5'};
drMetaClassDef=$57{'W'};
//Kylix specific flags
{ drUnit3=$E0; //4-bytes record, present in almost all units
drUnit3c=$06; //4-bytes record, present in System, SysInit
}
drUnit4=$0F; //5-bytes record, was observed in QOpenBanner.dcu only
//ver10 and higher tags
// drAddInfo6=$36{'6'};
drSpecVar=$37{'7'};
arClassVarReal=$2D {real value}{'-'};
arClassVar=$36 {technical value};
drCLine=$A0;
drA1Info=$A1;
drA2Info=$A2;
arCopyDecl=$A3;
//ver12 and higher tags
drDynArrayDef=Ord('X'); //Separate from drArrayDef tag now
drTemplateArgDef=Ord('Y');
drTemplateCall=Ord('Z');
drUnicodeStringDef=Ord('[');
//drA3Info=$A3;
drA5Info=$A5;
drA6Info=$A6;
drA7Info=$A7;
drA8Info=$A8;
drDelayedImpInfo=$B0;
//ver13 and higher tags
drUnitInlineSrc=Ord('v');
arAnonymousBlock=$01;
arVal=$21{'!'};
arVar=$22{'"'};
arResult=$23{'#'};
arAbsLocVar=$24{'$'};
arLabel=$2B{'+'};
//ver verD_XE2 and higher tags
//mode64 only till verD_XE5, all modes since verD_XE6
drSegInfo=$B1;
drAddrToSegInfo=$B2;
//ver verD_XE3 and higher tags
arFinalFlag=$C2;
//ver verD_XE4 and higher tags
drA9Info=$A9;
//ver verD_XE7 and higher tags
drNextOverload=$B6;
//ver verD_10 and higher tags
drDependencyInfo=$B5;
//Fields
arFld=$2C{','};
arMethod=$2D{'-'};
arConstr=$2E{'.'};
arDestr=$2F{'/'};
arProperty=$30{'0'};
arSetDeft=$9A;
arCDecl=$81;
arPascal=$82;
arStdCall=$83;
arSafeCall=$84;
type
TProcCallTag=arCDecl..arSafeCall;
type
{ Auxiliary data types }
TDeclSepFlags = set of (dsComma,dsLast,dsNoFirst,dsNL,dsSoftNL,
dsSmallSameNL,dsOfsProc);
TDeclSecKinds = set of TDeclSecKind;
const
RecSecKinds: array[Boolean]of TDeclSecKinds = ([],[skConst,skType,skPrivate,skPublic]);
ProcSecKinds: TDeclSecKinds = [];
BlockSecKinds: TDeclSecKinds = [skNone,skLabel,skConst,skType,skVar,
skThreadVar,skResStr,skExport,skProc];
ClassSecKinds: array[Boolean]of TDeclSecKinds = ([skPrivate,skProtected,skPublic, skPublished],
[skPrivate,skProtected,skPublic, skPublished,skType,skConst,skVar]);
type
PSrcFileRec = ^TSrcFileRec;
TSrcFileRec = record
Next: PSrcFileRec;
Def: PNameDef;
FT: LongInt;
NDX: integer;
Lines: TStringList;
end ;
type //AUX, not real file structures
PCodeLineRec = ^TCodeLineRec;
TCodeLineRec = record
Ofs,L: integer;
end ;
PCodeLineTbl = ^TCodeLineTbl;
TCodeLineTbl = array[Word] of TCodeLineRec;
//Using the $I directive DCU file can be composed of several source files
//The TLineRangeRec sructure represents the mapping of DCU internal line numbers
//to the real line numbers of source files
PLineRangeRec = ^TLineRangeRec;
TLineRangeRec = record
Line0,LineNum,Num0: integer;
SrcF: PSrcFileRec;
end ;
PLineRangeTbl = ^TLineRangeTbl;
TLineRangeTbl = array[Word] of TLineRangeRec;
//for verD_XE - fix orphaned local types problem
PEmbeddedTypeInf = ^TEmbeddedTypeInf;
TEmbeddedTypeInf = record
TD: TTypeDecl;
Depth: Integer
end ;
PEmbeddedListInf = ^TEmbeddedListInf;
TEmbeddedListInf = record
List: TDCURec{TNameDecl};
ListEnd: PTDCURec{PTNameDecl};
end ;
PEmbeddedListInfTbl = ^TEmbeddedListInfTbl;
TEmbeddedListInfTbl = array[Byte]of TEmbeddedListInf;
type
TUnit = class;
PUnitImpRec = ^TUnitImpRec;
TUnitImpFlags = set of (ufImpl,ufDLL);
TUnitImpRec = record
Ref: TUnitImpDef;
Name: PName;
Decls: TBaseDef;
// Types: TBaseDef;
// Addrs: TBaseDef;
Flags: TUnitImpFlags;
U: TUnit;
end ;
TUnitClass = class of TUnit;
TSeqCmdAction = function(DP: TIncPtr; {Dump address} CmdSz {Command size}: Cardinal;
Ofs0 {offset in DCU data block - for fixups}: Cardinal;
FixCnt: integer; FixTbl: PFixupTbl; Ok: Boolean; Proc: TProcDecl; IP: Pointer): Boolean{Stop};
TUnit = class
protected
FMemPtr: TIncPtr;
FMemSize: Cardinal;
FFromPackage: boolean;
FVer: integer;
FIsMSIL: boolean;
FPlatform: TDCUPlatform;
FPtrSize: Cardinal;
FStamp,FFlags,FUnitPrior: integer;
FFName,FFExt: String;
FUnitName: AnsiString;
FSrcFiles: PSrcFileRec;
FUnitImp: TList;
FTypes: TList;
FAddrs: TList;
FhNextAddr: integer; //Required for ProcAddInfo in Ver>verD8
FExportNames: TStringList;
FDecls: TDCURec{TNameDecl};
FOtherRecords: TDCURec; //The records, which where not included into FDecls or their fields
// FDefs: TBaseDef;
FTypeDefCnt: integer;
FTypeShowStack: TList;
{Data block}
FDataBlPtr: TIncPtr;
FDataBlSize: Cardinal;
FDataBlOfs: Cardinal;
{Fixups}
// FfxStart,FfxEnd: Byte;
FFixupCnt: integer;
FFixupTbl: PFixupTbl;
FCodeLineCnt: integer;
FCodeLineTbl: PCodeLineTbl;
FLineRangeCnt: integer;
FLineRangeTbl: PLineRangeTbl;
FLocVarCnt: integer;
FLocVarSize: integer; //The actual number of records in FLocVarTbl (required for XE2 64bit)
FLocVarTbl: PLocVarTbl;
FSegCnt: Integer;
FSegKindTbl: PSegKindTbl;
FLoaded: boolean;
function GetVersionStr: String;
function ReadMagic(Magic: ulong): Boolean;
procedure SetupFixups;
procedure ReadUnitHeader;
procedure ReadSourceFiles;
procedure ShowSourceFiles;
function ShowUses(const PfxS: AnsiString; FRq: TUnitImpFlags): boolean;
procedure ReadUses(TagRq: TDCURecTag);
function ReadUnitAddInfo: TUnitAddInfo;
procedure SetListDefName(L: TList; hDef,hDecl: integer; Name: PName);
procedure SetDeclMem(hDef: integer; Ofs,Sz: Cardinal);
// procedure AddAddrName(hDef: integer; Name: PName);
function GetTypeName(hDef: integer): PName;
function GetAddrName(hDef: integer): PName;
{-------------------------}
function GetUnitImpRec(hUnit: integer): PUnitImpRec;
function GetUnitImp(hUnit: integer): TUnit;
procedure SetExportNames(Decl: TDCURec{TNameDecl});
procedure SetEnumConsts(var Decl: TDCURec{TNameDecl});
function GetExportDecl(const Name: String; Stamp: integer): TNameFDecl;
function GetExportType(const Name: String; Stamp: integer): TBaseDef{TTypeDef};
{-------------------------}
procedure LoadFixups;
procedure LoadCodeLines;
function GetSrcFile(N: integer): PSrcFileRec;
procedure LoadLineRanges;
procedure LoadStrucScope;
procedure LoadSymbolInfo;
procedure LoadLocVarTbl;
procedure LoadAddrToSegInfo;
function GetNextFixup(iStart: integer; Ofs: Cardinal): integer;
function GetStartCodeLine(Ofs: integer): integer;
procedure GetCodeLineRec(i: integer; var CL: TCodeLineRec);
function GetStartLineRange(L: integer): integer;
procedure GetLineRange(i: integer; var LR: TLineRangeRec);
// function RegDataBl(BlSz: Cardinal): Cardinal;
procedure DetectUniqueNames;
function ForEachCodeBlSeqCmd(Ofs0,BlOfs,BlSz,SzMax: Cardinal; Proc: TProcDecl;
Action: TSeqCmdAction; IP: Pointer): Cardinal{CmdOfs};
procedure DasmCodeBlSeq(Ofs0,BlOfs,BlSz,SzMax: Cardinal; WasPartMsg: Boolean;
Proc: TProcDecl); virtual;
procedure DasmCodeBlCtlFlow(Ofs0,BlOfs,BlSz: Cardinal; TraceDataFlow: Boolean;
Proc: TProcDecl); virtual;
function ReadConstAddInfo(LastProcDecl: TNameDecl): integer;
procedure SetProcAddInfo(V: integer{; LastProcDecl: TNameDecl});
function GetAddrCount: Integer;
function GetTypeCount: Integer;
function ShowMSILExcHandlers(Ofs0,BlOfs,Sz: Cardinal): Cardinal;
procedure SetUnitPackageInfo(hDecl: Integer; const sInfo: String);
procedure ReadDependencyInfo;
public { Exported for DCURecs: }
function AddAddrDef(ND: TDCURec): integer;
function AppendAddrDef(ND: TDCURec): integer;
procedure RefAddrDef(V: integer);
function GetAddrDef(hDef: integer): TDCURec;
function GetGlobalAddrDef(hDef: integer; var U: TUnit): TDCURec;
procedure AddTypeDef(TD: TTypeDef);
function GetLastAddedTypeDef: TTypeDef;
procedure ClearLastTypeDef(TD: TTypeDef);
procedure ClearAddrDef(ND: TNameDecl);
function GetTypeDef(hDef: integer): TBaseDef;
function GetTypeSize(hDef: integer): integer;
procedure AddTypeName(hDef,hDecl: integer; Name: PName);
function ShowTypeName(hDef: integer): boolean;
function TypeIsVoid(hDef: integer): boolean;
function RegTypeShow(T: TBaseDef): boolean;
procedure UnRegTypeShow(T: TBaseDef);
procedure ShowDataBl(Ofs0,BlOfs,BlSz: Cardinal);
procedure ShowDataBlP(DP: Pointer; DS,Ofs0: Cardinal);
procedure ShowCodeBl(Ofs0,BlOfs,BlSz: Cardinal; Proc: TProcDecl);virtual;
procedure ShowTypeDef(hDef: integer; N: PName);
function GetLocalTypeDef(hDef: integer): TTypeDef;
function GetGlobalTypeDef(hDef: integer; var U: TUnit): TTypeDef;
function ShowTypeValue(T: TTypeDef; DP: Pointer; DS: Cardinal;
ConstKind: Integer; IsNamed: Boolean): integer {Size used};
function ShowGlobalTypeValue(hDef: TNDX; DP: Pointer; DS: Cardinal;
AndRest: boolean; ConstKind: Integer; IsNamed: Boolean): integer {Size used};
function GetGlobalTypeValKind(hDef: TNDX): TTypeValKind;
function ShowGlobalConstValue(hDef: integer): boolean;
function GetOfsQualifierEx(hDef: integer; Ofs,QSz: integer; QI: PQualInfo; QS: PAnsiString): Boolean;
function GetOfsQualifier(hDef: integer; Ofs: integer): AnsiString;
function GetRefOfsQualifierEx(hDef: integer; Ofs,QSz: integer; QI: PQualInfo; QS: PAnsiString): Boolean;
function GetRefOfsQualifier(hDef: integer; Ofs: integer): AnsiString;
function GetMethodProcDecl(M: TMethodDecl): TProcDecl;
function GetBlockMem(BlOfs,BlSz: Cardinal; var ResSz: Cardinal): Pointer;
function FixTag(Tag: TDCURecTag): TDCURecTag;
procedure ReadDeclList(LK: TDeclListKind; Owner: TDCURec; var Result: TDCURec{TNameDecl});
function ShowDeclList(LK: TDeclListKind; MainRec: TDCURec;
Decl: TDCURec{TNameDecl}; Ofs: Cardinal; dScopeOfs: integer; SepF: TDeclSepFlags;
ValidKinds: TDeclSecKinds; skDefault: TDeclSecKind): TDeclSecKind;
function GetStartFixup(Ofs: Cardinal): integer;
procedure SetStartFixupInfo(Fix0: integer);
property DataBlPtr: TIncPtr read FDataBlPtr;
property UnitImpRec[hUnit: integer]: PUnitImpRec read GetUnitImpRec;
procedure DoShowFixupTbl;
procedure FillProcLocVarTbls;
procedure DoShowLocVarTbl;
protected
FEmbedDepth,FMaxEmbedDepth,FEmbedLimit: Integer;
FEmbeddedLists: PEmbeddedListInfTbl; //contains the lists which were not consumed
function IncEmbedDepth(var HeadBuf: TDCURec{TNameDecl}): PTDCURec{PTNameDecl};
function ConsumeEmbedded: TDCURec{TNameDecl};
protected //for verD_XE - fix orphaned local types problem
FEmbeddedTypes: TList; //contains PEmbeddedTypeInf, it is indexed by TD.hDef, not by FEmbedDepth
procedure RegisterEmbeddedTypes(var Embedded: TDCURec{TNameDecl}; Depth: Integer);
procedure BindEmbeddedTypes;
public
constructor Create; virtual; //Allows to extend TUnit
function Load(const FName: String; VerRq: integer; MSILRq: boolean;
PlatformRq: TDCUPlatform; AMem: Pointer): boolean; //Load instead of Create
//to prevent from Destroy after Exception in constructor
destructor Destroy; override;
procedure Show;
property VersionStr: String read GetVersionStr;
function GetAddrStr(hDef: integer; ShowNDX: boolean): AnsiString;
procedure PutAddrStr(hDef: integer; ShowNDX: boolean);
procedure PutAddrStrRmClassName(hDef: integer; ShowNDX: boolean);
function IsValidMemPtr(DP: Pointer): Boolean;
function HasFixups: Boolean;
function GetFixupsFor(DP: Pointer; DS: ULong; var Fix: PFixupRec): Integer{FixCnt};
procedure GetAllUses(SL: TStrings; FRq: TUnitImpFlags);
property UnitName: AnsiString read FUnitName;
property FileName: String read FFName;
property ExportDecls[const Name: String; Stamp: integer]: TNameFDecl read GetExportDecl;
property ExportTypes[const Name: String; Stamp: integer]: TBaseDef{TTypeDef it may be TImpTypeDefRec for type <T>} read GetExportType;
property Ver: integer read FVer;
property IsMSIL: boolean read FIsMSIL;
property Platform: TDCUPlatform read FPlatform;
property PtrSize: Cardinal read FPtrSize;
property Stamp: integer read FStamp;
property FromPackage: boolean read FFromPackage;
// property fxStart: Byte read FfxStart;
// property fxEnd: Byte read FfxEnd;
property AddrName[hDef: integer]: PName read GetAddrName;
property TypeName[hDef: integer]: PName read GetTypeName;
property AddrCount: Integer read GetAddrCount;
property TypeCount: Integer read GetTypeCount;
property DeclList: TDCURec{TNameDecl} read FDecls;
property MemPtr: TIncPtr read FMemPtr;
end ;
var
MainUnit: TUnit = Nil;
CurUnit: TUnit;
CurMainRec: TDCURec {of ShowDeclList};
CurDeclList: TDCURec{TNameDecl} {of ShowDeclList};
{ Exported for other TUnitClass: }
procedure RegCommandRef(RefP: LongInt; RefKind: Byte; IP: Pointer);
{ Exported for DCURecs: }
function GetDCURecStr(D: TDCURec; hDef: integer; ShowNDX: boolean): AnsiString;
procedure PutDCURecStr(D: TDCURec; hDef: integer; ShowNDX: boolean);
implementation
uses
SysUtils, DCUTbl, DCP, DasmX86, DasmMSIL, DasmCF{, Op}, InlineOp;
{procedure FreeDCURecTList(L: TList);
var
Tmp: TDCURec;
i: integer;
begin
if L=Nil then
Exit;
for i:=0 to L.Count-1 do begin
Tmp := L[i];
Tmp.Free;
end ;
L.Free;
end ;}
function FileDateToStr(FT: TDCUFileTime): AnsiString;
const
DaySec=24*60*60;
var
T: TDateTime;
begin
if CurUnit.Ver<verK1 then
T := FileDateToDateTime(FT)
else
T := EncodeDate(1970,1,1)+FT/DaySec{Unix Time to Delphi time};
Result := FormatDateTime('c',T);
end ;
function GetDCURecStr(D: TDCURec; hDef: integer; ShowNDX: boolean): AnsiString;
var
N: PName;
Ch,ScopeCh: AnsiChar;
Pfx: AnsiString;
CP: PAnsiChar;
cd: integer;
begin
if D=Nil then
N := @NoName
else
N := D.Name;
Ch := N^.Get1stChar;
if Ch{N^[0]}=#0 then begin
Pfx := NoNamePrefix;
Result := {$IFDEF UNICODE}AnsiStrings.{$ENDIF}Format('%x',[hDef]);
ShowNDX := false;
end
else if Ch{N^[1]}='.' then begin
Pfx := DotNamePrefix;
Result := N^.GetRightStr(1){Copy(N^,2,255)};
end
else begin
Result := N^.GetStr;
Pfx := '';
end ;
if Pfx<>'' then begin
CP := {$IFDEF ANSIStr}AnsiStrings.{$ENDIF}StrScan(PAnsiChar(Pfx),'%');
if CP<>Nil then begin
if D=Nil then
ScopeCh := 'N'
else begin
if (D is TTypeDecl)or(D is TTypeDef) then
ScopeCh := 'T'
else if D is TVarDecl then
ScopeCh := 'V'
else if D is TConstDecl then
ScopeCh := 'C'
else if D is TProcDecl then
ScopeCh := 'F'
else if D is TLabelDecl then
ScopeCh := 'L'
else if (D is TPropDecl)or(D is TDispPropDecl) then
ScopeCh := 'P'
else if D is TLocalDecl then
ScopeCh := 'v'
else if D is TMethodDecl then
ScopeCh := 'M'
else if D is TExportDecl then
ScopeCh := 'E'
else
ScopeCh := 'n';
end ;
cd := CP-PAnsiChar(Pfx);
UniqueString(Pfx);{Make the string unique - it will be altered}
CP := PAnsiChar(Pfx)+cd;
repeat
CP^ := ScopeCh;
CP := {$IFDEF ANSIStr}AnsiStrings.{$ENDIF}StrScan(CP+1,'%');
until CP=Nil;
end ;
Result := Pfx+Result;
end ;
if ShowNDX then
Result := {$IFDEF UNICODE}AnsiStrings.{$ENDIF}Format('%s{$%x}',[Result, hDef]);
end ;
procedure PutDCURecStr(D: TDCURec; hDef: integer; ShowNDX: boolean);
var
S: AnsiString;
begin
S := GetDCURecStr(D,hDef,false{ShowNDX});
PutAddrDefStr(S,hDef);
if ShowNDX then
PutSFmtRem('#$%x', [hDef]);
end ;
{$IFDEF WIN}
function GetFileVersionStr: String;
{Copied from SysUtils of D7 and modified}
var
FileName: string;
InfoSize, Wnd: DWORD;
VerBuf: Pointer;
FI: PVSFixedFileInfo;
VerSize,V,B: DWORD;
begin
Result := '';
FileName := ParamStr(0);
InfoSize := GetFileVersionInfoSize(PChar(FileName), Wnd);
if InfoSize <> 0 then
begin
GetMem(VerBuf, InfoSize);
try
if GetFileVersionInfo(PChar(FileName), Wnd, InfoSize, VerBuf) then
if VerQueryValue(VerBuf, '\', Pointer(FI), VerSize) then begin
V := FI^.dwFileVersionMS;
B := FI^.dwFileVersionLS;
Result := Format('Version: %d.%d',[V shr 16, V and $FFFF]);
//Result := Format('Version: %d.%d, build %d.%d',[V shr 16, V and $FFFF,B shr 16, B and $FFFF]);
//Delphi XE2 writes wrong build numbers into VersionInfo
end ;
finally
FreeMem(VerBuf);
end;
end;
end;
{$ENDIF}
{ TUnit. }
function TUnit.GetVersionStr: String;
const
verStrDelphi: array[0..MaxDelphiVer]of String = (
'Error','Error 1','2','3','4','5','6','7','8','2005','2006','?2007','2009',
'Error 13','2010','XE','XE2','XE3','XE4','XE5','XE6','XE7','XE8','10 Seattle',
'10.1 Berlin','10.2 Tokyo','10.3 Rio');
platfStr: array[TDCUPlatform]of String = ('Win32','Win64','Osx32',
'iOSEmulator','iOSDevice','iOSDevice64','Android','Linux64');
begin
if Ver<verK1 then begin
Result := 'Delphi '+verStrDelphi[Ver];
if Ver>=verD_XE2 then
Result := Format('%s (%s)',[Result,platfStr[FPlatform]]);
end
else
Result := Format('Kylix %d',[Ver-verK1+1]);
end ;
procedure TUnit.ReadSourceFiles;
var
{hSrc,}F: integer;
SrcFName: ShortString;
SP,CP: PAnsiChar;
// FT: TDCUFileTime;
// B: Byte;
SFRP: ^PSrcFileRec;
SFR,SFRMain: PSrcFileRec;
begin
// NLOfs := 0;
// hSrc := 0;
FSrcFiles := Nil;
SFRMain := Nil;
SFRP := @FSrcFiles;
while (Tag=drSrc)or(Tag=drRes)or(Tag=drObj)or(Tag=drAsm)or
(Ver>=verD2010)and(Ver<verK1)and(Tag=drUnitInlineSrc) or
(Ver>=verD2005)and(Ver<verK1)and IsMSIL and(Tag=drAssemblyInfo)or
{(Ver=verD8)and(Ver<verK1)and} IsMSIL and(Tag=drAssemblySrc) do
begin
New(SFR);
SFR^.Next := Nil;
SFRP^ := SFR;
SFRP := @SFR^.Next;
SFR^.Def := DefStart;
SFR^.Lines := Nil;
if Tag=drAssemblyInfo then begin
ReadNDXStr;
SFR^.FT := 0;
SFR^.Ndx := 0;
end
else begin
ReadName;
SFR^.FT := ReadULong;
F := ReadUIndex;
if F=0 then
SFRMain := SFR;
SFR^.Ndx := F;
if IsMSIL and(Tag<>drRes)and(Tag<>drAssemblySrc) then begin
SrcFName := ReadStr; //Ignored by now, because it's always empty
end ;
end ;
Tag := ReadTag;
end ;
if FSrcFiles=Nil then begin
if not FromPackage then
DCUError('No source files');
end ;
if SFRMain=Nil{Paranoic} then
SFRMain := FSrcFiles;
if (SFRMain=Nil)or(SFRMain^.Def^.Tag in [drAssemblyInfo,drAssemblySrc]) then begin
FUnitName := ChangeFileExt(ExtractFileNamePkg(FFName),'')
end
else begin
FUnitName := ExtractFileNameAnySep(SFRMain^.Def^.Name.GetStr);
SP := PAnsiChar(FUnitName);
CP := {$IFDEF ANSIStr}AnsiStrings.{$ENDIF}StrRScan(SP,'.');
if (CP<>Nil)and(CP>SP) then
SetLength(FUnitName,CP-SP);
end ;
end ;
procedure TUnit.ShowSourceFiles;
var
SFR: PSrcFileRec;
T: TDCURecTag;
begin
PutKWSp('unit');
PutSFmt('%s;',[FUnitName]);
OpenAux;
if Ver>verD2 then begin
PutSpace;
RemOpen;
PutSFmt('Flags: $%x',[FFlags]);
if Ver>verD3 then
PutSFmt(', Priority: $%x',[FUnitPrior]);
RemClose;
end ;
CloseAux;
NL;
RemOpen;
NL;
PutSFmt('Compiled by %s',[VersionStr]);
NL;
{$IFDEF WIN}
PutSFmt('Decompiled by DCU32INT %s',[GetFileVersionStr]);
NL;
{$ENDIF}
if FSrcFiles<>Nil then begin
PutS('Source files:');
Writer.NLOfs := 2;
SFR := FSrcFiles;
NL;
while true do begin
T := SFR^.Def^.Tag;
case T of
drObj: PutS('$L ');
drRes: PutS('$R ');
drAssemblyInfo: PutS('$Assembly ');
drAssemblySrc: PutS('$Assembly8 ');
end ;
if T=drAssemblyInfo then
PutS(GetNDXStr(@SFR^.Def^.Name))
else
PutS(SFR^.Def^.Name.GetStr);
if (integer(SFR^.FT)<>-1)and(integer(SFR^.FT)<>0) then
PutSFmt(' (%s)',[FileDateToStr(SFR^.FT)]);
SFR := SFR^.Next;
if SFR=Nil then
Break;
PutS(','+cSoftNL)
end ;
end ;
RemClose;
Writer.NLOfs := 0;
NL;
NL;
end ;
function TUnit.ShowUses(const PfxS: AnsiString; FRq: TUnitImpFlags): boolean;
var
i,Cnt,hImp: integer;
U: PUnitImpRec;
Decl: TBaseDef;
NLOfs0: Cardinal;
begin
Result := false;
if FUnitImp.Count=0 then
Exit;
Cnt := 0;
NLOfs0 := Writer.NLOfs;
for i:=0 to FUnitImp.Count-1 do begin
U := FUnitImp[i];
if FRq<>U.Flags then
Continue;
if Cnt>0 then
PutCh(',')
else begin
NL;
PutKW(PfxS);
ShiftNLOfs(2);
end ;
NL;
PutS(U^.Name^.GetStr);
if U^.Ref.sPackage<>'' then begin
RemOpen;
PutS(U^.Ref.sPackage);
RemClose;
end ;
Inc(Cnt);
if ShowImpNames then begin
Decl := U^.Decls;
hImp := 0;
while Decl<>Nil do begin
if hImp>0 then begin
PutCh(',');
SoftNL;
end
else begin
PutSpace;
RemOpen;
ShiftNLOfs(2);
NL;
end ;
// PutSFmt('%s%x: %s',[Ch,NDX,ImpN^]);
// PutSFmt('%s%x: ',[Ch,NDX]);
MarkDefStart(Decl.hDecl);
Decl.Show;
Inc(hImp);
Decl := Decl.Next as TBaseDef;
end ;
if hImp>0 then begin
RemClose;
ShiftNLOfs(-2);
end ;
end ;
end ;
Writer.NLOfs := NLOfs0;
Result := Cnt>0;
if Result then
PutCh(';');
end ;
procedure TUnit.GetAllUses(SL: TStrings; FRq: TUnitImpFlags);
var
i: integer;
U: PUnitImpRec;
Decl: TBaseDef;
begin
if FUnitImp.Count=0 then
Exit;
for i:=0 to FUnitImp.Count-1 do begin
U := FUnitImp[i];
if FRq<>U.Flags then
Continue;
Decl := U^.Decls;
while Decl<>Nil do begin
SL.AddObject(Decl.Name^.GetStr,Decl);
Decl := Decl.Next as TBaseDef;
end ;
end ;
end ;
procedure TUnit.ReadUses(TagRq: TDCURecTag);
var
hUses,hImp,hPack: integer;
UseName: PName;
ImpN: PName;
//B: Byte;
RTTISz: Cardinal;
L,L1,L2: LongInt;
Ch: AnsiChar;
hUnit: integer;
U: PUnitImpRec;
TR,AR: TBaseDef;
IR: TImpDef;
UIR: TUnitImpDef;
DeclEnd: ^TBaseDef;
// TypesEnd,AddrsEnd: ^TBaseDef;
NDX,ImpBase,ImpBase0,ImpReBase: integer;
begin
hUses := 0;
ImpBase := 0;
while Tag=TagRq do begin
UseName := ReadName;
{if hUses>0 then
PutCh(',')
else begin
PutS('uses');
NLOfs := 2;
end ;
NL;
PutS(UseName^);}
New(U);
FillChar(U^,SizeOf(TUnitImpRec),0);
U^.Name := UseName;
Ch := '?';
case TagRq of
drUnit1: begin Ch := 'U'; U^.Flags := [ufImpl]; end ;
drDLL: begin Ch := 'D'; U^.Flags := [ufDLL]; end ;
end ;
hUnit := FUnitImp.Count;
FUnitImp.Add(U);
hPack := 0;
if (TagRq<>drDLL)and(Ver>=verD8)and(Ver<verK1) then
hPack := ReadUIndex;
if (Ver>=verD2006)and(Ver<verK1) then
L := ReadUIndex
else
L := ReadULong;
//if (Ver>=verD7)and(Ver<verK1) then begin
if (Ver=verD7)and(Ver<verK1)or(Ver>=verD8)and(Ver<verK1)and(TagRq=drDLL) then begin
L1 := ReadULong;
end ;
if (Ver>=verD2009)and(Ver<verK1) then
L2 := ReadUIndex;
{TypesEnd := @U^.Types;
AddrsEnd := @U^.Addrs;}
DeclEnd := @U^.Decls;
hImp := 0;
UIR := TUnitImpDef.Create(Ch,UseName,L,Nil{DefStart},hUnit) {Unit reference};
U^.Ref := UIR;
ImpBase0 := ImpBase;
ImpBase := AddAddrDef(UIR); //FAddrs.Add(IR);
if (hPack>0)and(Ver<verD2009{or may be MSIL only}) then
RefAddrDef(hPack); //Reserve index for unit package number
while true do begin
Tag := ReadTag;
case Tag of
drImpType,drImpTypeDef: if TagRq<>drDLL then begin
Ch := 'T';
ImpN := ReadName;
if Tag=drImpTypeDef then begin
//B := ReadByte;
RTTISz := ReadUIndex;
{ImpN := Format('%s[%d]',[ImpN,B]);}
end ;
L := ReadULong;
if Tag=drImpTypeDef then
TR := TImpTypeDefRec.Create(ImpN,L,RTTISz{B},Nil{DefStart},hUnit)
else
TR := TImpDef.Create('T',ImpN,L,Nil{DefStart},hUnit);
{TypesEnd^ := TR;
TypesEnd := @TR.Next;}
FTypes.Add(TR);
TR.hDecl := AddAddrDef(TR); //FAddrs.Add(TR); {TypeInfo}
ndx := FTypes.Count;
FTypeDefCnt := ndx;
end ;
drImpVal: begin
Ch := 'A';
ImpN := ReadName;
L := ReadULong;
if TagRq<>drDLL then
AR := TImpDef.Create('A',ImpN,L,Nil{DefStart},hUnit)
else
AR := TDLLImpRec.Create(ImpN,L,Nil,hUnit);
{AddrsEnd^ := AR;
AddrsEnd := @AR.Next;}
ndx := AddAddrDef(AR); //FAddrs.Add(AR);
AR.hDecl := ndx;
//ndx := FAddrs.Count;
TR := AR;
end ;
drStop2: begin
//Imports drConstAddInfo may be for the prev. drImpVal always
L := -1;
if (Ver>=verD8)and(Ver<verK1) then
L := ReadULong; //==IP for the imported drConstAddInfo
Continue;
end ;
drConstAddInfo: begin
if not IsMSIL then
break;
if hImp<>0 then
DCUErrorFmt('ConstAddInfo encountered for %s in subrecord #%d',[UseName,hImp]);
ImpReBase := ReadConstAddInfo(Nil); //Just skip it by now