-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathdasm.cpp
7829 lines (7201 loc) · 310 KB
/
dasm.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "ildasmpch.h"
#include <crtdbg.h>
#include <utilcode.h>
#include "specstrings.h"
#include "debugmacros.h"
#include "corpriv.h"
#include "ceeload.h"
#include "dynamicarray.h"
#include <metamodelpub.h>
#include "formattype.h"
#include "readytorun.h"
#define DECLARE_DATA
#include "dasmenum.hpp"
#include "dis.h"
#include "resource.h"
#include "dasm_sz.h"
//#define MAX_FILENAME_LENGTH 2048 //moved to dis.h
#include <corsym.h>
#include <clrversion.h>
// Disable the "initialization of static local vars is no thread safe" error
#ifdef _MSC_VER
#pragma warning(disable : 4640)
#endif
#ifdef TARGET_UNIX
#include "resourcestring.h"
#define NATIVE_STRING_RESOURCE_NAME dasm_rc
DECLARE_NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME);
#endif
#include "mdfileformat.h"
struct MIDescriptor
{
mdToken tkClass; // defining class token
mdToken tkDecl; // implemented method token
mdToken tkBody; // implementing method token
mdToken tkBodyParent; // parent of the implementing method
};
ISymUnmanagedReader* g_pSymReader = NULL;
IMDInternalImport* g_pImport = NULL;
IMetaDataImport2* g_pPubImport;
extern IMetaDataAssemblyImport* g_pAssemblyImport;
PELoader * g_pPELoader;
void * g_pMetaData;
unsigned g_cbMetaData;
IMAGE_COR20_HEADER * g_CORHeader;
DynamicArray<__int32> *g_pPtrTags = NULL; //to keep track of all "ldptr"
DynamicArray<DWORD> *g_pPtrSize= NULL; //to keep track of all "ldptr"
int g_iPtrCount = 0;
mdToken * g_cl_list = NULL;
mdToken * g_cl_enclosing = NULL;
BYTE* g_enum_td_type = NULL; // enum (TD) underlying types
BYTE* g_enum_tr_type = NULL; // enum (TR) underlying types
IMDInternalImport** g_asmref_import = NULL; // IMDInternalImports for external assemblies
DynamicArray<MIDescriptor> *g_pmi_list = NULL;
DWORD g_NumMI;
DWORD g_NumClasses;
DWORD g_NumTypeRefs;
DWORD g_NumAsmRefs;
DWORD g_NumModules;
BOOL g_fDumpIL = TRUE;
BOOL g_fDumpHeader = FALSE;
BOOL g_fDumpAsmCode = TRUE;
extern BOOL g_fDumpTokens; // declared in formatType.cpp
BOOL g_fDumpStats = FALSE;
BOOL g_fTDC = TRUE;
BOOL g_fShowCA = TRUE;
BOOL g_fCAVerbal = FALSE;
BOOL g_fShowRefs = FALSE;
BOOL g_fDumpToPerfWriter = FALSE;
HANDLE g_PerfDataFilePtr = NULL;
BOOL g_fDumpClassList = FALSE;
BOOL g_fDumpTypeList = FALSE;
BOOL g_fDumpSummary = FALSE;
BOOL g_fDecompile = FALSE; // still in progress
BOOL g_fShowBytes = FALSE;
BOOL g_fShowSource = FALSE;
BOOL g_fPrettyPrint = FALSE;
BOOL g_fInsertSourceLines = FALSE;
BOOL g_fThisIsInstanceMethod;
BOOL g_fTryInCode = TRUE;
BOOL g_fLimitedVisibility = FALSE;
BOOL g_fR2RNativeManifestMetadata = FALSE;
BOOL g_fHidePub = TRUE;
BOOL g_fHidePriv = TRUE;
BOOL g_fHideFam = TRUE;
BOOL g_fHideAsm = TRUE;
BOOL g_fHideFAA = TRUE;
BOOL g_fHideFOA = TRUE;
BOOL g_fHidePrivScope = TRUE;
BOOL g_fProject = FALSE; // if .winmd file, transform to .NET view
extern BOOL g_fQuoteAllNames; // declared in formatType.cpp, init to FALSE
BOOL g_fForwardDecl=FALSE;
char g_szAsmCodeIndent[MAX_MEMBER_LENGTH];
char g_szNamespace[MAX_MEMBER_LENGTH];
DWORD g_Mode = MODE_DUMP_ALL;
char g_pszClassToDump[MAX_CLASSNAME_LENGTH];
char g_pszMethodToDump[MAX_MEMBER_LENGTH];
char g_pszSigToDump[MAX_SIGNATURE_LENGTH];
BOOL g_fCustomInstructionEncodingSystem = FALSE;
COR_FIELD_OFFSET *g_rFieldOffset = NULL;
ULONG g_cFieldsMax, g_cFieldOffsets;
char g_szInputFile[MAX_FILENAME_LENGTH]; // in UTF-8
WCHAR g_wszFullInputFile[MAX_PATH + 1]; // in UTF-16
char g_szOutputFile[MAX_FILENAME_LENGTH]; // in UTF-8
char* g_pszObjFileName;
FILE* g_pFile = NULL;
mdToken g_tkClassToDump = 0;
mdToken g_tkMethodToDump = 0;
unsigned g_uConsoleCP = CP_ACP;
unsigned g_uCodePage = g_uConsoleCP;
char* g_rchCA = NULL; // dyn.allocated array of CA dumped/not flags
unsigned g_uNCA = 0; // num. of CAs
struct ResourceNode;
extern DynamicArray<LocalComTypeDescr*> *g_pLocalComType;
extern ULONG g_LocalComTypeNum;
// MetaInfo integration:
#include "../tools/metainfo/mdinfo.h"
BOOL g_fDumpMetaInfo = FALSE;
ULONG g_ulMetaInfoFilter = MDInfo::dumpDefault;
// Validator module type.
DWORD g_ValModuleType = ValidatorModuleTypeInvalid;
IMetaDataDispenserEx *g_pDisp = NULL;
void DisplayFile(_In_ __nullterminated WCHAR* szFile,
BOOL isFile,
ULONG DumpFilter,
_In_opt_z_ WCHAR* szObjFile,
strPassBackFn pDisplayString);
extern mdMethodDef g_tkEntryPoint; // integration with MetaInfo
DWORD DumpResourceToFile(_In_ __nullterminated WCHAR* wzFileName); // see DRES.CPP
struct VTableRef
{
mdMethodDef tkTok;
WORD wEntry;
WORD wSlot;
};
DynamicArray<VTableRef> *g_prVTableRef = NULL;
ULONG g_nVTableRef = 0;
struct EATableRef
{
mdMethodDef tkTok;
char* pszName;
};
DynamicArray<EATableRef> *g_prEATableRef=NULL;
ULONG g_nEATableRef = 0;
ULONG g_nEATableBase = 0;
extern HINSTANCE g_hResources;
void DumpCustomAttributeProps(mdToken tkCA, mdToken tkType, mdToken tkOwner, BYTE*pBlob, ULONG ulLen, void *GUICookie, bool bWithOwner);
WCHAR* RstrW(unsigned id)
{
static WCHAR buffer[1024];
DWORD cchBuff = (DWORD)ARRAY_SIZE(buffer);
WCHAR* buff = (WCHAR*)buffer;
memset(buffer,0,sizeof(buffer));
switch(id)
{
case IDS_E_DASMOK:
case IDS_E_PARTDASM:
case IDS_E_PARAMSEQNO:
case IDS_E_MEMBRENUM:
case IDS_E_ODDMEMBER:
case IDS_E_ENUMINIT:
case IDS_E_NODATA:
case IDS_E_VTFUTABLE:
case IDS_E_BOGUSRVA:
case IDS_E_EATJTABLE:
case IDS_E_EATJSIZE:
case IDS_E_RESFLAGS:
case IDS_E_MIHENTRY:
case IDS_E_CODEMGRTBL:
case IDS_E_COMIMAGE:
case IDS_E_MDDETAILS:
case IDS_E_MISTART:
case IDS_E_MIEND:
case IDS_E_ONLYITEMS:
case IDS_E_DECOMPRESS:
case IDS_E_COMPRESSED:
case IDS_E_INSTRDECOD:
case IDS_E_INSTRTYPE:
case IDS_E_SECTHEADER:
case IDS_E_MDAIMPORT:
case IDS_E_MDAFROMMDI:
case IDS_E_MDIIMPORT:
case IDS_E_NOMANIFEST:
case IDS_W_CREATEDW32RES:
case IDS_E_CORRUPTW32RES:
case IDS_E_CANTACCESSW32RES:
case IDS_E_CANTOPENW32RES:
case IDS_ERRORREOPENINGFILE:
wcscpy_s(buffer,ARRAY_SIZE(buffer),W("// "));
buff +=3;
cchBuff -= 3;
break;
case IDS_E_AUTOCA:
case IDS_E_METHBEG:
case IDS_E_DASMNATIVE:
case IDS_E_METHODRT:
case IDS_E_CODESIZE:
case IDS_W_CREATEDMRES:
case IDS_E_READINGMRES:
wcscpy_s(buffer,ARRAY_SIZE(buffer),W("%s// "));
buff +=5;
cchBuff -= 5;
break;
case IDS_E_NORVA:
wcscpy_s(buffer,ARRAY_SIZE(buffer),W("/* "));
buff += 3;
cchBuff -= 3;
break;
default:
break;
}
#ifdef TARGET_UNIX
LoadNativeStringResource(NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME),id, buff, cchBuff, NULL);
#else
_ASSERTE(g_hResources != NULL);
WszLoadString(g_hResources,id,buff,cchBuff);
#endif
if(id == IDS_E_NORVA)
wcscat_s(buff,cchBuff,W(" */"));
return buffer;
}
char* RstrA(unsigned n, unsigned codepage)
{
static char buff[2048];
WCHAR* wz = RstrW(n);
// Unicode -> UTF-8
memset(buff,0,sizeof(buff));
if(!WszWideCharToMultiByte(codepage,0,(LPCWSTR)wz,-1,buff,sizeof(buff),NULL,NULL))
buff[0] = 0;
return buff;
}
char* RstrUTF(unsigned n)
{
return RstrA(n,CP_UTF8);
}
char* RstrANSI(unsigned n)
{
return RstrA(n,g_uConsoleCP);
}
#if 0
void PrintEncodingSystem()
{
long i;
printf("Custom opcode encoding system employed\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for (i = 0; i < 256; i++)
{
long value = g_pInstructionDecodingTable->m_SingleByteOpcodes[i];
printf("0x%02x --> ", i);
printf("%s\n", OpcodeInfo[value].pszName);
}
}
#endif
// buffers for formatType functions
extern CQuickBytes * g_szBuf_KEYWORD;
extern CQuickBytes * g_szBuf_COMMENT;
extern CQuickBytes * g_szBuf_ERRORMSG;
extern CQuickBytes * g_szBuf_ANCHORPT;
extern CQuickBytes * g_szBuf_JUMPPT;
extern CQuickBytes * g_szBuf_UnquotedProperName;
extern CQuickBytes * g_szBuf_ProperName;
BOOL Init()
{
g_szBuf_KEYWORD = new CQuickBytes();
g_szBuf_COMMENT = new CQuickBytes();
g_szBuf_ERRORMSG = new CQuickBytes();
g_szBuf_ANCHORPT = new CQuickBytes();
g_szBuf_JUMPPT = new CQuickBytes();
g_szBuf_UnquotedProperName = new CQuickBytes();
g_szBuf_ProperName = new CQuickBytes();
return TRUE;
} // Init
extern LPCSTR *rAsmRefName; // decl. in formatType.cpp -- for AsmRef aliases
extern ULONG ulNumAsmRefs; // decl. in formatType.cpp -- for AsmRef aliases
void Cleanup()
{
if (g_pAssemblyImport != NULL)
{
g_pAssemblyImport->Release();
g_pAssemblyImport = NULL;
}
if (g_pPubImport != NULL)
{
g_pPubImport->Release();
g_pPubImport = NULL;
}
if (g_pImport != NULL)
{
g_pImport->Release();
g_pImport = NULL;
TokenSigDelete();
}
if (g_pDisp != NULL)
{
g_pDisp->Release();
g_pDisp = NULL;
}
if (g_pSymReader != NULL)
{
g_pSymReader->Release();
g_pSymReader = NULL;
}
if (g_pPELoader != NULL)
{
g_pPELoader->close();
SDELETE(g_pPELoader);
}
g_iPtrCount = 0;
g_NumClasses = 0;
g_NumTypeRefs = 0;
g_NumModules = 0;
g_tkEntryPoint = 0;
g_szAsmCodeIndent[0] = 0;
g_szNamespace[0]=0;
g_pszClassToDump[0]=0;
g_pszMethodToDump[0]=0;
g_pszSigToDump[0] = 0;
g_NumDups = 0;
g_NumRefs = 0;
g_NumMI = 0;
g_LocalComTypeNum = 0;
g_nEATableRef = 0;
g_fCustomInstructionEncodingSystem = FALSE;
if (rAsmRefName != NULL)
{
for (int i = 0; (unsigned)i < ulNumAsmRefs; i++)
{
if (rAsmRefName[i] != NULL) VDELETE(rAsmRefName[i]);
}
VDELETE(rAsmRefName);
ulNumAsmRefs = 0;
}
if (g_rchCA != NULL)
VDELETE(g_rchCA);
if (g_cl_list != NULL) VDELETE(g_cl_list);
if (g_cl_enclosing != NULL) VDELETE(g_cl_enclosing);
if (g_pmi_list != NULL) SDELETE(g_pmi_list);
if (g_dups != NULL) SDELETE(g_dups);
if (g_enum_td_type != NULL) VDELETE(g_enum_td_type);
if (g_enum_tr_type != NULL) VDELETE(g_enum_tr_type);
if (g_asmref_import != NULL)
{
for (DWORD i = 0; i < g_NumAsmRefs; i++)
{
if (g_asmref_import[i] != NULL)
g_asmref_import[i]->Release();
}
VDELETE(g_asmref_import);
g_NumAsmRefs = 0;
}
} // Cleanup
void Uninit()
{
if (g_pPtrTags != NULL)
{
SDELETE(g_pPtrTags);
}
if (g_pPtrSize != NULL)
{
SDELETE(g_pPtrSize);
}
if (g_pmi_list != NULL)
{
SDELETE(g_pmi_list);
}
if (g_dups != NULL) SDELETE(g_dups);
if (g_refs != NULL) SDELETE(g_refs);
if (g_pLocalComType != NULL)
{
SDELETE(g_pLocalComType);
}
if (g_prVTableRef != NULL)
{
SDELETE(g_prVTableRef);
}
if (g_prEATableRef != NULL)
{
SDELETE(g_prEATableRef);
}
if (g_szBuf_KEYWORD != NULL)
{
SDELETE(g_szBuf_KEYWORD);
}
if (g_szBuf_COMMENT != NULL)
{
SDELETE(g_szBuf_COMMENT);
}
if (g_szBuf_ERRORMSG != NULL)
{
SDELETE(g_szBuf_ERRORMSG);
}
if (g_szBuf_ANCHORPT != NULL)
{
SDELETE(g_szBuf_ANCHORPT);
}
if (g_szBuf_JUMPPT != NULL)
{
SDELETE(g_szBuf_JUMPPT);
}
if (g_szBuf_UnquotedProperName != NULL)
{
SDELETE(g_szBuf_UnquotedProperName);
}
if (g_szBuf_ProperName != NULL)
{
SDELETE(g_szBuf_ProperName);
}
} // Uninit
HRESULT IsClassRefInScope(mdTypeRef classref)
{
HRESULT hr = S_OK;
const char *pszNameSpace;
const char *pszClassName;
mdTypeDef classdef;
mdToken tkRes;
IfFailRet(g_pImport->GetNameOfTypeRef(classref, &pszNameSpace, &pszClassName));
MAKE_NAME_IF_NONE(pszClassName,classref);
IfFailRet(g_pImport->GetResolutionScopeOfTypeRef(classref, &tkRes));
hr = g_pImport->FindTypeDef(pszNameSpace, pszClassName,
(TypeFromToken(tkRes) == mdtTypeRef) ? tkRes : mdTokenNil, &classdef);
return hr;
}
#ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:21000) // Suppress PREFast warning about overly large function
#endif
BOOL EnumClasses()
{
HRESULT hr;
HENUMInternal hEnum;
ULONG i = 0,j;
//char szString[1024];
HENUMInternal hBody;
HENUMInternal hDecl;
if(g_cl_list) VDELETE(g_cl_list);
if(g_cl_enclosing) VDELETE(g_cl_enclosing);
if (g_pmi_list) SDELETE(g_pmi_list);
if (g_dups) SDELETE(g_dups);
if (g_enum_td_type) VDELETE(g_enum_td_type);
if (g_enum_tr_type) VDELETE(g_enum_tr_type);
if (g_asmref_import)
{
for (DWORD nIndex = 0; nIndex < g_NumAsmRefs; nIndex++)
{
if (g_asmref_import[nIndex] != NULL)
g_asmref_import[nIndex]->Release();
}
VDELETE(g_asmref_import);
g_NumAsmRefs = 0;
}
//--------------------------------------------------------------
if (FAILED(g_pImport->EnumAllInit(mdtTypeRef,&hEnum)))
{
printError(g_pFile, "MetaData error: cannot enumerate all TypeRefs");
return FALSE;
}
g_NumTypeRefs = g_pImport->EnumGetCount(&hEnum);
g_pImport->EnumClose(&hEnum);
if(g_NumTypeRefs)
{
g_enum_tr_type = new BYTE[g_NumTypeRefs+1];
if(g_enum_tr_type == NULL) return FALSE;
memset(g_enum_tr_type,0xFF,g_NumTypeRefs+1);
}
//--------------------------------------------------------------
if (FAILED(g_pImport->EnumAllInit(mdtAssemblyRef, &hEnum)))
{
printError(g_pFile, "MetaData error: cannot enumerate all AssemblyRefs");
return FALSE;
}
g_NumAsmRefs = g_pImport->EnumGetCount(&hEnum);
g_pImport->EnumClose(&hEnum);
if(g_NumAsmRefs)
{
g_asmref_import = new IMDInternalImport*[g_NumAsmRefs+1];
if(g_asmref_import == NULL) return FALSE;
memset(g_asmref_import,0,(g_NumAsmRefs+1)*sizeof(IMDInternalImport*));
}
//--------------------------------------------------------------
hr = g_pImport->EnumTypeDefInit(
&hEnum);
if (FAILED(hr))
{
printError(g_pFile,RstrUTF(IDS_E_CLSENUM));
return FALSE;
}
g_NumClasses = g_pImport->EnumGetCount(&hEnum);
g_tkClassToDump = 0;
g_NumMI = 0;
g_NumDups = 0;
if(g_NumClasses == 0) return TRUE;
g_enum_td_type = new BYTE[g_NumClasses+1];
if(g_enum_td_type == NULL) return FALSE;
memset(g_enum_td_type,0xFF,g_NumClasses+1);
g_cl_list = new mdToken[g_NumClasses];
if(g_cl_list == NULL) return FALSE;
g_cl_enclosing = new mdToken[g_NumClasses];
if(g_cl_enclosing == NULL)
{
VDELETE(g_cl_list);
return FALSE;
}
g_pmi_list = new DynamicArray<MIDescriptor>;
if(g_pmi_list == NULL)
{
VDELETE(g_cl_enclosing);
VDELETE(g_cl_list);
return FALSE;
}
g_dups = new DynamicArray<mdToken>;
if(g_dups == NULL)
{
SDELETE(g_pmi_list);
VDELETE(g_cl_enclosing);
VDELETE(g_cl_list);
return FALSE;
}
// fill the list of typedef tokens
while(g_pImport->EnumNext(&hEnum, &g_cl_list[i]))
{
mdToken tkEnclosing;
if (g_Mode == MODE_DUMP_CLASS || g_Mode == MODE_DUMP_CLASS_METHOD || g_Mode == MODE_DUMP_CLASS_METHOD_SIG)
{
CQuickBytes out;
// we want plain class name without token values
BOOL fDumpTokens = g_fDumpTokens;
g_fDumpTokens = FALSE;
PAL_CPP_TRY
{
if (strcmp(PrettyPrintClass(&out, g_cl_list[i], g_pImport), g_pszClassToDump) == 0)
{
g_tkClassToDump = g_cl_list[i];
}
}
PAL_CPP_CATCH_ALL
{ }
PAL_CPP_ENDTRY;
g_fDumpTokens = fDumpTokens;
}
g_cl_enclosing[i] = mdTypeDefNil;
hr = g_pImport->GetNestedClassProps(g_cl_list[i],&tkEnclosing);
if (SUCCEEDED(hr) && RidFromToken(tkEnclosing)) // No need to check token validity here, it's done later
g_cl_enclosing[i] = tkEnclosing;
if (SUCCEEDED(g_pImport->EnumMethodImplInit(g_cl_list[i],&hBody,&hDecl)))
{
if ((j = g_pImport->EnumMethodImplGetCount(&hBody,&hDecl)))
{
mdToken tkBody,tkDecl,tkBodyParent;
for (ULONG k = 0; k < j; k++)
{
if (g_pImport->EnumMethodImplNext(&hBody,&hDecl,&tkBody,&tkDecl) == S_OK)
{
if (SUCCEEDED(g_pImport->GetParentToken(tkBody,&tkBodyParent)))
{
(*g_pmi_list)[g_NumMI].tkClass = g_cl_list[i];
(*g_pmi_list)[g_NumMI].tkBody = tkBody;
(*g_pmi_list)[g_NumMI].tkDecl = tkDecl;
(*g_pmi_list)[g_NumMI].tkBodyParent = tkBodyParent;
g_NumMI++;
}
}
}
}
g_pImport->EnumMethodImplClose(&hBody,&hDecl);
}
i++;
}
g_pImport->EnumClose(&hEnum);
// check nesting consistency (circular nesting, invalid enclosers)
for(i = 0; i < g_NumClasses; i++)
{
mdToken tkThis = g_cl_list[i];
mdToken tkEncloser = g_cl_enclosing[i];
mdToken tkPrevLevel = tkThis;
while(tkEncloser != mdTypeDefNil)
{
if(tkThis == tkEncloser)
{
sprintf_s(szString,SZSTRING_SIZE,RstrUTF(IDS_E_SELFNSTD),tkThis);
printError(g_pFile,szString);
g_cl_enclosing[i] = mdTypeDefNil;
break;
}
else
{
for(j = 0; (j < g_NumClasses)&&(tkEncloser != g_cl_list[j]); j++);
if(j == g_NumClasses)
{
sprintf_s(szString,SZSTRING_SIZE,RstrUTF(IDS_E_NOENCLOS),
tkPrevLevel,tkEncloser);
printError(g_pFile,szString);
g_cl_enclosing[i] = mdTypeDefNil;
break;
}
else
{
tkPrevLevel = tkEncloser;
tkEncloser = g_cl_enclosing[j];
}
}
} // end while(tkEncloser != mdTypeDefNil)
} // end for(i = 0; i < g_NumClasses; i++)
// register all class dups
const char *pszClassName;
const char *pszNamespace;
const char *pszClassName1;
const char *pszNamespace1;
if (FAILED(g_pImport->GetNameOfTypeDef(
g_cl_list[0],
&pszClassName,
&pszNamespace)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), g_cl_list[0]);
printLine(g_pFile, sz);
return FALSE;
}
if((g_cl_enclosing[0]==mdTypeDefNil)
&&(0==strcmp(pszClassName,"<Module>"))
&&(*pszNamespace == 0))
{
(*g_dups)[g_NumDups++] = g_cl_list[0];
}
for(i = 1; i < g_NumClasses; i++)
{
if (FAILED(g_pImport->GetNameOfTypeDef(
g_cl_list[i],
&pszClassName,
&pszNamespace)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), g_cl_list[i]);
printLine(g_pFile, sz);
return FALSE;
}
for(j = 0; j < i; j++)
{
if (FAILED(g_pImport->GetNameOfTypeDef(
g_cl_list[j],
&pszClassName1,
&pszNamespace1)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), g_cl_list[j]);
printLine(g_pFile, sz);
return FALSE;
}
if((g_cl_enclosing[i]==g_cl_enclosing[j])
&&(0==strcmp(pszClassName,pszClassName1))
&&(0==strcmp(pszNamespace,pszNamespace1)))
{
(*g_dups)[g_NumDups++] = g_cl_list[i];
break;
}
}
} // end for(i = 1; i < g_NumClasses; i++)
//register all field and method dups
for(i = 0; i <= g_NumClasses; i++)
{
HENUMInternal hEnumMember;
mdToken *pMemberList = NULL;
DWORD NumMembers,k;
// methods
if (i != 0)
{
hr = g_pImport->EnumInit(mdtMethodDef, g_cl_list[i-1], &hEnumMember);
}
else
{
hr = g_pImport->EnumGlobalFunctionsInit(&hEnumMember);
}
if (FAILED(hr))
{
printLine(g_pFile,RstrUTF(IDS_E_MEMBRENUM));
return FALSE;
}
NumMembers = g_pImport->EnumGetCount(&hEnumMember);
pMemberList = new mdToken[NumMembers];
for (j = 0; g_pImport->EnumNext(&hEnumMember, &pMemberList[j]); j++);
_ASSERTE(j == NumMembers);
g_pImport->EnumClose(&hEnumMember);
for (j = 1; j < NumMembers; j++)
{
const char *pszName;
ULONG cSig;
PCCOR_SIGNATURE pSig;
if (FAILED(g_pImport->GetNameOfMethodDef(pMemberList[j], &pszName)) ||
FAILED(g_pImport->GetSigOfMethodDef(pMemberList[j], &cSig, &pSig)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[j]);
printLine(g_pFile, sz);
return FALSE;
}
for (k = 0; k < j; k++)
{
const char *szName1;
if (FAILED(g_pImport->GetNameOfMethodDef(pMemberList[k], &szName1)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[k]);
printLine(g_pFile, sz);
return FALSE;
}
if (strcmp(pszName, szName1) == 0)
{
ULONG cSig1;
PCCOR_SIGNATURE pSig1;
if (FAILED(g_pImport->GetSigOfMethodDef(pMemberList[k], &cSig1, &pSig1)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[k]);
printLine(g_pFile, sz);
return FALSE;
}
if((cSig == cSig1)&&(0==memcmp(pSig,pSig1,cSig)))
{
(*g_dups)[g_NumDups++] = pMemberList[j];
break;
}
}
}
}
VDELETE(pMemberList);
// fields
if (i != 0)
{
hr = g_pImport->EnumInit(mdtFieldDef, g_cl_list[i-1], &hEnumMember);
}
else
{
hr = g_pImport->EnumGlobalFieldsInit(&hEnumMember);
}
if (FAILED(hr))
{
printLine(g_pFile,RstrUTF(IDS_E_MEMBRENUM));
return FALSE;
}
NumMembers = g_pImport->EnumGetCount(&hEnumMember);
pMemberList = new mdToken[NumMembers];
for (j = 0; g_pImport->EnumNext(&hEnumMember, &pMemberList[j]); j++);
_ASSERTE(j == NumMembers);
g_pImport->EnumClose(&hEnumMember);
for (j = 1; j < NumMembers; j++)
{
const char *pszName;
ULONG cSig;
PCCOR_SIGNATURE pSig;
if (FAILED(g_pImport->GetNameOfFieldDef(pMemberList[j], &pszName)) ||
FAILED(g_pImport->GetSigOfFieldDef(pMemberList[j], &cSig, &pSig)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[j]);
printLine(g_pFile, sz);
return FALSE;
}
for (k = 0; k < j; k++)
{
const char *szName1;
if (FAILED(g_pImport->GetNameOfFieldDef(pMemberList[k], &szName1)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[k]);
printLine(g_pFile, sz);
return FALSE;
}
if (strcmp(pszName, szName1) == 0)
{
ULONG cSig1;
PCCOR_SIGNATURE pSig1;
if (FAILED(g_pImport->GetSigOfFieldDef(pMemberList[k], &cSig1, &pSig1)))
{
char sz[2048];
sprintf_s(sz, 2048, RstrUTF(IDS_E_INVALIDRECORD), pMemberList[k]);
printLine(g_pFile, sz);
return FALSE;
}
if((cSig == cSig1)&&(0==memcmp(pSig,pSig1,cSig)))
{
(*g_dups)[g_NumDups++] = pMemberList[j];
break;
}
}
}
}
VDELETE(pMemberList);
} // end for(i = 0; i <= g_NumClasses; i++)
return TRUE;
}
#ifdef _PREFAST_
#pragma warning(pop)
#endif
void DumpMscorlib(void* GUICookie)
{
// In the CoreCLR with reference assemblies and redirection it is more difficult to determine if
// a particular Assembly is the System assembly, like mscorlib.dll is for the Desktop CLR.
// In the CoreCLR runtimes, the System assembly can be System.Private.CoreLib.dll, System.Runtime.dll
// or netstandard.dll and in the future a different Assembly name could be used.
// We now determine the identity of the System assembly by querying if the Assembly defines the
// well known type System.Object as that type must be defined by the System assembly
// If this type is defined then we will output the ".mscorlib" directive to indicate that this
// assembly is the System assembly.
//
mdTypeDef tkObjectTypeDef = mdTypeDefNil;
// Lookup the type System.Object and see it it has a type definition in this assembly
if (SUCCEEDED(g_pPubImport->FindTypeDefByName(W("System.Object"), mdTypeDefNil, &tkObjectTypeDef)))
{
if (tkObjectTypeDef != mdTypeDefNil)
{
// We do have a type definition for System.Object in this assembly
//
DWORD dwClassAttrs = 0;
mdToken tkExtends = mdTypeDefNil;
// Retrieve the type def properties as well, so that we can check a few more things about
// the System.Object type
//
if (SUCCEEDED(g_pPubImport->GetTypeDefProps(tkObjectTypeDef, NULL, NULL, 0, &dwClassAttrs, &tkExtends)))
{
bool bExtends = g_pPubImport->IsValidToken(tkExtends);
bool isClass = ((dwClassAttrs & tdClassSemanticsMask) == tdClass);
// We also check the type properties to make sure that we have a class and not a Value type definition
// and that this type definition isn't extending another type.
//
if (isClass & !bExtends)
{
// We will mark this assembly with the System assembly directive: .mscorlib
//
printLine(GUICookie, "");
sprintf_s(szString, SZSTRING_SIZE, "%s%s ", g_szAsmCodeIndent, KEYWORD(".mscorlib"));
printLine(GUICookie, szString);
printLine(GUICookie, "");
}
}
}
}
}
void DumpTypelist(void* GUICookie)
{
if(g_NumClasses > 1)
{
DWORD i;
CQuickBytes out;
printLine(GUICookie,"");
sprintf_s(szString,SZSTRING_SIZE,"%s%s ",g_szAsmCodeIndent,KEYWORD(".typelist"));
printLine(GUICookie,szString);
sprintf_s(szString,SZSTRING_SIZE,"%s%s",g_szAsmCodeIndent,SCOPE());
printLine(GUICookie,szString);
strcat_s(g_szAsmCodeIndent,MAX_MEMBER_LENGTH," ");
for(i = 0; i < g_NumClasses; i++)
{
out.Shrink(0);
sprintf_s(szString,SZSTRING_SIZE, "%s%s",g_szAsmCodeIndent, PrettyPrintClass(&out, g_cl_list[i], g_pImport));
printLine(GUICookie,szString);
}
g_szAsmCodeIndent[strlen(g_szAsmCodeIndent)-2] = 0;
sprintf_s(szString,SZSTRING_SIZE,"%s%s",g_szAsmCodeIndent,UNSCOPE());
printLine(GUICookie,szString);
printLine(GUICookie,"");
}
}
#define ELEMENT_TYPE_TYPEDEF (ELEMENT_TYPE_MAX+1)
BOOL EnumTypedefs()
{
HENUMInternal hEnum;
ULONG i,l;
mdToken tk;
if (g_typedefs) SDELETE(g_typedefs);
g_typedefs = new DynamicArray<TypeDefDescr>;
g_NumTypedefs = 0;
if (FAILED(g_pImport->EnumAllInit(mdtTypeSpec, &hEnum)))
{
return FALSE;
}
for (i = 0; g_pImport->EnumNext(&hEnum, &tk); i++)
{
ULONG cSig;
PCCOR_SIGNATURE sig;
if (FAILED(g_pImport->GetSigFromToken(tk, &cSig, &sig)))
{
return FALSE;
}
if (*sig == ELEMENT_TYPE_TYPEDEF)
{
TypeDefDescr* pTDD = &((*g_typedefs)[g_NumTypedefs]);
pTDD->szName = (char*)sig+1;
l = 2+(ULONG)strlen((char*)sig+1);
pTDD->tkTypeSpec = GET_UNALIGNED_VAL32(sig + l);
pTDD->tkSelf = tk;
if (TypeFromToken(pTDD->tkTypeSpec) == mdtTypeSpec)
{
if (FAILED(g_pImport->GetSigFromToken(pTDD->tkTypeSpec,&(pTDD->cb), &(pTDD->psig))))
{
return FALSE;
}
}
else if (TypeFromToken(pTDD->tkTypeSpec) == mdtCustomAttribute)
{
l += sizeof(mdToken);
pTDD->psig = sig + l;
pTDD->cb = cSig - l;
}
else
{
pTDD->psig = NULL;
pTDD->cb = 0;
}
g_NumTypedefs++;
}
}
g_pImport->EnumClose(&hEnum);
return TRUE;