-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathRepackImporter.cs
1010 lines (902 loc) · 43.6 KB
/
RepackImporter.cs
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
//
// Copyright (c) 2011 Francois Valdy
// Copyright (c) 2015 Timotei Dolean
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using ILRepacking.Mixins;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ILRepacking
{
internal class RepackImporter : IRepackImporter, IRepackCopier
{
private readonly ILogger _logger;
private readonly IRepackContext _repackContext;
private readonly RepackOptions _options;
private readonly Dictionary<AssemblyDefinition, int> _aspOffsets;
private readonly Dictionary<ImportDebugInformation, ImportDebugInformation> _importDebugInformations = new();
private readonly static Instruction _dummyInstruction = Instruction.Create(OpCodes.Nop);
const string ExcludeInternalizeAttName = "RepackExcludeInternalizeAttribute";
public RepackImporter(
ILogger logger,
RepackOptions options,
IRepackContext repackContext,
Dictionary<AssemblyDefinition, int> aspOffsets)
{
_logger = logger;
_options = options;
_repackContext = repackContext;
_aspOffsets = aspOffsets;
}
public void Import(ExportedType type, Collection<ExportedType> exportedTypesCollection, ModuleDefinition targetAssemblyMainModule)
{
var scope = default(IMetadataScope);
// try to skip redirects to merged assemblies
if (type.Scope is AssemblyNameReference)
{
if (_repackContext.MergedAssemblies.Any(x => x.Name.Name == ((AssemblyNameReference)type.Scope).Name))
{
return;
}
scope = _repackContext.PlatformFixer.FixPlatformVersion(((AssemblyNameReference)type.Scope));
}
else if (type.Scope is ModuleReference)
{
if (_repackContext.MergedAssemblies.SelectMany(x => x.Modules).Any(x => x.Name == ((ModuleReference)type.Scope).Name))
{
return;
}
// TODO fix scope (should probably be added to target ModuleReferences, otherwise metadatatoken will be wrong)
// I've never seen an exported type redirected to a module, doing so would be blind guessing
scope = type.Scope;
}
if (type.IsForwarder)
{
// Skip duplicated forwarders
var fullName = type.FullName;
if (exportedTypesCollection.Any(t => t.IsForwarder && t.FullName == fullName))
{
return;
}
}
var newExportedType = new ExportedType(type.Namespace, type.Name, targetAssemblyMainModule, scope)
{
Attributes = type.Attributes,
Identifier = type.Identifier, // TODO: CHECK THIS when merging multiple assemblies when exported types ?
DeclaringType = type.DeclaringType
};
exportedTypesCollection.Add(newExportedType);
}
public TypeReference Import(TypeReference reference, IGenericParameterProvider context)
{
TypeDefinition type = _repackContext.GetMergedTypeFromTypeRef(reference);
if (type != null)
return type;
_repackContext.PlatformFixer.FixPlatformVersion(reference);
try
{
if (context == null)
{
// we come here when importing types used for assembly-level custom attributes
return _repackContext.TargetAssemblyMainModule.ImportReference(reference);
}
return _repackContext.TargetAssemblyMainModule.ImportReference(reference, context);
}
catch (ArgumentOutOfRangeException) // working around a bug in Cecil
{
_logger.Error("Problem adding reference: " + reference.FullName);
throw;
}
}
public FieldReference Import(FieldReference reference, IGenericParameterProvider context)
{
_repackContext.PlatformFixer.FixPlatformVersion(reference);
return _repackContext.TargetAssemblyMainModule.ImportReference(reference, context);
}
public MethodReference Import(MethodReference reference)
{
_repackContext.PlatformFixer.FixPlatformVersion(reference);
return _repackContext.TargetAssemblyMainModule.ImportReference(reference);
}
public MethodReference Import(MethodReference reference, IGenericParameterProvider context)
{
// If this is a Method/TypeDefinition, it will be corrected to a definition again later
_repackContext.PlatformFixer.FixPlatformVersion(reference);
return _repackContext.TargetAssemblyMainModule.ImportReference(reference, context);
}
public TypeDefinition Import(TypeDefinition type, Collection<TypeDefinition> col, bool internalize)
{
_logger.Verbose("- Importing " + type);
if (ShouldDrop(type))
{
return null;
}
TypeDefinition nt = _repackContext.TargetAssemblyMainModule.GetType(type.FullName);
bool justCreatedType = false;
if (nt == null)
{
nt = CreateType(type, col, internalize, null);
justCreatedType = true;
if (IsWellKnownType(type))
{
internalize = false;
}
}
else if (DuplicateTypeAllowed(type))
{
_logger.Verbose("Merging " + type);
internalize = false;
}
else if (!type.IsPublic || internalize)
{
var originalModule = _repackContext.MappingHandler.GetOriginalModule(nt);
// rename the type previously imported.
// renaming the new one before import made Cecil throw an exception.
string other = GenerateName(nt, originalModule?.Mvid.ToString());
//Check whether renamed type already exists
TypeDefinition otherNt = _repackContext.TargetAssemblyMainModule.GetType(other);
if (otherNt != null)
{
//Create a random name
other = GenerateName(nt);
}
_logger.Verbose("Renaming " + nt.FullName + " into " + other);
nt.Name = other;
nt = CreateType(type, col, internalize, null);
justCreatedType = true;
}
else if (_options.UnionMerge)
{
_logger.Verbose("Merging " + type);
internalize = false;
}
else
{
_logger.Error("Duplicate type " + type);
throw new InvalidOperationException(
"Duplicate type " + type + " from " + type.Scope + ", was also present in " +
MappingHandler.GetScopeFullName(_repackContext.MappingHandler.GetOrigTypeScope<IMetadataScope>(nt)));
}
_repackContext.MappingHandler.StoreRemappedType(type, nt);
// nested types first (are never internalized)
foreach (TypeDefinition nested in type.NestedTypes)
{
if (ShouldDrop(nested) == false)
{
Import(nested, nt.NestedTypes, false);
}
}
foreach (FieldDefinition field in type.Fields)
{
if (ShouldDrop(field) == false)
{
CloneTo(field, nt);
}
}
// methods before fields / events
foreach (MethodDefinition meth in type.Methods)
{
if (ShouldDrop(meth) == false)
{
CloneTo(meth, nt, justCreatedType);
}
}
foreach (EventDefinition evt in type.Events)
{
if (ShouldDrop(evt) == false)
{
CloneTo(evt, nt, nt.Events);
}
}
foreach (PropertyDefinition prop in type.Properties)
{
if (ShouldDrop(prop) == false)
{
CloneTo(prop, nt, nt.Properties);
}
}
if (internalize && _options.RenameInternalized)
{
string newName = GenerateName(nt, type.Module.Mvid.ToString());
_logger.Verbose("Renaming " + nt.FullName + " into " + newName);
nt.Name = newName;
}
return nt;
}
private string GenerateName(TypeDefinition typeDefinition, string disambiguator = null)
{
disambiguator ??= Guid.NewGuid().ToString();
return $"<{disambiguator}>{typeDefinition.Name}";
}
private bool ShouldDrop<TMember>(TMember member) where TMember : ICustomAttributeProvider, IMemberDefinition
{
var dropAttributes = _options.RepackDropAttributes;
if (!dropAttributes.Any())
{
return false;
}
if (!member.HasCustomAttributes)
{
return false;
}
// skip members marked with a custom attribute named as /repackdrop:RepackDropAttribute
var dropAttribute = member.CustomAttributes.FirstOrDefault(attr =>
dropAttributes.Contains(attr.AttributeType.Name) ||
dropAttributes.Contains(attr.AttributeType.FullName));
if (dropAttribute != null)
{
_logger.Verbose("Repack dropped " + typeof(TMember).Name + ": " + member.FullName + " as it was marked with " + dropAttribute.AttributeType.FullName);
return true;
}
return false;
}
// Real stuff below //
// These methods are somehow a merge between the clone methods of Cecil 0.6 and the import ones of 0.9
// They use Cecil's MetaDataImporter to rebase imported stuff into the new assembly, but then another pass is required
// to clean the TypeRefs Cecil keeps around (although the generated IL would be kind-o valid without, whatever 'valid' means)
/// <summary>
/// Clones a field to a newly created type
/// </summary>
private void CloneTo(FieldDefinition field, TypeDefinition nt)
{
if (nt.Fields.Any(x => x.Name == field.Name))
{
return;
}
FieldDefinition nf = new FieldDefinition(field.Name, field.Attributes, Import(field.FieldType, nt));
nt.Fields.Add(nf);
if (field.HasConstant)
nf.Constant = field.Constant;
if (field.HasMarshalInfo)
nf.MarshalInfo = field.MarshalInfo;
if (field.InitialValue != null && field.InitialValue.Length > 0)
nf.InitialValue = field.InitialValue;
if (field.HasLayoutInfo)
nf.Offset = field.Offset;
CopyCustomAttributes(field.CustomAttributes, nf.CustomAttributes, nt);
}
/// <summary>
/// Clones a parameter into a newly created method
/// </summary>
private void CloneTo(ParameterDefinition param, MethodDefinition context, Collection<ParameterDefinition> col)
{
ParameterDefinition pd = new ParameterDefinition(param.Name, param.Attributes, Import(param.ParameterType, context));
if (param.HasConstant)
pd.Constant = param.Constant;
if (param.HasMarshalInfo)
pd.MarshalInfo = param.MarshalInfo;
if (param.HasCustomAttributes)
CopyCustomAttributes(param.CustomAttributes, pd.CustomAttributes, context);
col.Add(pd);
}
private void CloneTo(EventDefinition evt, TypeDefinition nt, Collection<EventDefinition> col)
{
// ignore duplicate event
if (nt.Events.Any(x => x.Name == evt.Name))
{
return;
}
EventDefinition ed = new EventDefinition(evt.Name, evt.Attributes, Import(evt.EventType, nt));
col.Add(ed);
if (evt.AddMethod != null)
ed.AddMethod = FindMethodInNewType(nt, evt.AddMethod);
if (evt.RemoveMethod != null)
ed.RemoveMethod = FindMethodInNewType(nt, evt.RemoveMethod);
if (evt.InvokeMethod != null)
ed.InvokeMethod = FindMethodInNewType(nt, evt.InvokeMethod);
if (evt.HasOtherMethods)
{
foreach (MethodDefinition meth in evt.OtherMethods)
{
var nm = FindMethodInNewType(nt, meth);
if (nm != null)
ed.OtherMethods.Add(nm);
}
}
CopyCustomAttributes(evt.CustomAttributes, ed.CustomAttributes, nt);
}
private void CloneTo(PropertyDefinition prop, TypeDefinition nt, Collection<PropertyDefinition> col)
{
// ignore duplicate property
var others = nt.Properties.Where(x => x.Name == prop.Name).ToList();
if (others.Any())
{
bool skip = false;
if (!IsIndexer(prop) || !IsIndexer(others.First()))
{
skip = true;
}
else
{
// "Item" property is used to implement Indexer operators
// It may be specified more than one, with extra arguments to get/set methods
// Note than one may also define a standard "Item" property, in which case he won't be able to define Indexers
// Here we try to prevent duplicate indexers, but allow to merge non-duplicated ones (e.g. this[int] & this[string] )
var args = ExtractIndexerParameters(prop);
if (others.Any(x => _repackContext.ReflectionHelper.AreSame(args, ExtractIndexerParameters(x))))
{
skip = true;
}
}
if (skip)
{
return;
}
}
PropertyDefinition pd = new PropertyDefinition(prop.Name, prop.Attributes, Import(prop.PropertyType, nt));
col.Add(pd);
if (prop.SetMethod != null)
pd.SetMethod = FindMethodInNewType(nt, prop.SetMethod);
if (prop.GetMethod != null)
pd.GetMethod = FindMethodInNewType(nt, prop.GetMethod);
if (prop.HasOtherMethods)
{
foreach (MethodDefinition meth in prop.OtherMethods)
{
var nm = FindMethodInNewType(nt, meth);
if (nm != null)
pd.OtherMethods.Add(nm);
}
}
CopyCustomAttributes(prop.CustomAttributes, pd.CustomAttributes, nt);
}
private void CloneTo(MethodDefinition meth, TypeDefinition type, bool typeJustCreated)
{
// ignore duplicate method for merged duplicated types
if (!typeJustCreated &&
type.Methods.Count > 0 &&
type.Methods.Any(x =>
(x.Name == meth.Name) &&
(x.Parameters.Count == meth.Parameters.Count) &&
(x.ToString() == meth.ToString()))) // TODO: better/faster comparation of parameter types?
{
return;
}
// use void placeholder as we'll do the return type import later on (after generic parameters)
MethodDefinition nm = new MethodDefinition(meth.Name, meth.Attributes, _repackContext.TargetAssemblyMainModule.TypeSystem.Void);
nm.ImplAttributes = meth.ImplAttributes;
if (meth.DebugInformation.HasCustomDebugInformations)
nm.DebugInformation.CustomDebugInformations.AddRange(meth.DebugInformation.CustomDebugInformations);
if (meth.DebugInformation.HasSequencePoints)
nm.DebugInformation.SequencePoints.AddRange(meth.DebugInformation.SequencePoints);
type.Methods.Add(nm);
CopyGenericParameters(meth.GenericParameters, nm.GenericParameters, nm);
if (meth.HasPInvokeInfo)
{
if (meth.PInvokeInfo == null)
{
// Even if this was allowed, I'm not sure it'd work out
//nm.RVA = meth.RVA;
}
else
{
nm.PInvokeInfo = new PInvokeInfo(meth.PInvokeInfo.Attributes, meth.PInvokeInfo.EntryPoint, meth.PInvokeInfo.Module);
}
}
foreach (ParameterDefinition param in meth.Parameters)
CloneTo(param, nm, nm.Parameters);
foreach (MethodReference ov in meth.Overrides)
nm.Overrides.Add(Import(ov, nm));
CopySecurityDeclarations(meth.SecurityDeclarations, nm.SecurityDeclarations, nm);
CopyCustomAttributes(meth.CustomAttributes, nm.CustomAttributes, nm);
nm.ReturnType = Import(meth.ReturnType, nm);
nm.MethodReturnType.Attributes = meth.MethodReturnType.Attributes;
if (meth.MethodReturnType.HasConstant)
nm.MethodReturnType.Constant = meth.MethodReturnType.Constant;
if (meth.MethodReturnType.HasMarshalInfo)
nm.MethodReturnType.MarshalInfo = meth.MethodReturnType.MarshalInfo;
if (meth.MethodReturnType.HasCustomAttributes)
CopyCustomAttributes(meth.MethodReturnType.CustomAttributes, nm.MethodReturnType.CustomAttributes, nm);
if (meth.HasBody)
CloneTo(meth.Body, nm);
nm.DebugInformation.Scope = CopyScope(meth.DebugInformation.Scope, nm, out _);
meth.Body = null; // frees memory
nm.IsAddOn = meth.IsAddOn;
nm.IsRemoveOn = meth.IsRemoveOn;
nm.IsGetter = meth.IsGetter;
nm.IsSetter = meth.IsSetter;
nm.CallingConvention = meth.CallingConvention;
}
private ScopeDebugInformation CopyScope(ScopeDebugInformation scope, MethodDefinition nm, out bool copied)
{
copied = false;
if (scope is null || scope.Import is null && !scope.HasConstants && !scope.HasScopes)
return scope;
var ns = new ScopeDebugInformation(_dummyInstruction, null);
ns.Start = new InstructionOffset(scope.Start.Offset);
ns.End = scope.End.IsEndOfMethod ? default : new InstructionOffset(scope.End.Offset);
if (scope.HasCustomDebugInformations)
ns.CustomDebugInformations.AddRange(scope.CustomDebugInformations);
if (scope.HasVariables)
ns.Variables.AddRange(scope.Variables);
if (scope.HasScopes)
foreach (var ps in scope.Scopes)
{
ns.Scopes.Add(CopyScope(ps, nm, out var nc));
copied |= nc;
}
if (scope.HasConstants)
{
copied = true;
foreach (var pc in scope.Constants)
{
var nc = new ConstantDebugInformation(pc.Name, Import(pc.ConstantType, nm), pc.Value);
if (pc.HasCustomDebugInformations)
nc.CustomDebugInformations.AddRange(pc.CustomDebugInformations);
ns.Constants.Add(nc);
}
}
if (scope.Import is not null)
{
copied = true;
ns.Import = CopyImport(scope.Import, nm);
}
return copied ? ns : scope;
}
private ImportDebugInformation CopyImport(ImportDebugInformation import, MethodDefinition nm)
{
if (import is null)
return null;
if (_importDebugInformations.TryGetValue(import, out var ni))
return ni;
ni = new ImportDebugInformation();
ni.Parent = CopyImport(import.Parent, nm);
if (import.HasCustomDebugInformations)
ni.CustomDebugInformations.AddRange(import.CustomDebugInformations);
if (import.HasTargets)
foreach (var pt in import.Targets)
{
var nt = new ImportTarget(pt.Kind);
nt.Alias = pt.Alias;
nt.Namespace = pt.Namespace;
if (pt.Type is not null)
nt.Type = Import(pt.Type, nm);
if (pt.AssemblyReference is not null)
nt.AssemblyReference = _repackContext.PlatformFixer.FixPlatformVersion(pt.AssemblyReference) as AssemblyNameReference;
ni.Targets.Add(nt);
}
_importDebugInformations.Add(import, ni);
return ni;
}
private void CloneTo(MethodBody body, MethodDefinition parent)
{
MethodBody nb = new MethodBody(parent);
parent.Body = nb;
nb.MaxStackSize = body.MaxStackSize;
nb.InitLocals = body.InitLocals;
nb.LocalVarToken = body.LocalVarToken;
foreach (VariableDefinition var in body.Variables)
nb.Variables.Add(new VariableDefinition(
Import(var.VariableType, parent)));
nb.Instructions.Capacity = Math.Max(nb.Instructions.Capacity, body.Instructions.Count);
_repackContext.LineIndexer.PreMethodBodyRepack(body, parent);
foreach (Instruction instr in body.Instructions)
{
Instruction ni;
if (instr.OpCode.Code == Code.Calli)
{
var callSite = (CallSite)instr.Operand;
CallSite ncs = new CallSite(Import(callSite.ReturnType, parent))
{
HasThis = callSite.HasThis,
ExplicitThis = callSite.ExplicitThis,
CallingConvention = callSite.CallingConvention
};
foreach (ParameterDefinition param in callSite.Parameters)
CloneTo(param, parent, ncs.Parameters);
ni = Instruction.Create(instr.OpCode, ncs);
}
else switch (instr.OpCode.OperandType)
{
case OperandType.InlineArg:
case OperandType.ShortInlineArg:
if (instr.Operand == body.ThisParameter)
{
ni = Instruction.Create(instr.OpCode, nb.ThisParameter);
}
else
{
int param = body.Method.Parameters.IndexOf((ParameterDefinition)instr.Operand);
ni = Instruction.Create(instr.OpCode, parent.Parameters[param]);
}
break;
case OperandType.InlineVar:
case OperandType.ShortInlineVar:
int var = body.Variables.IndexOf((VariableDefinition)instr.Operand);
ni = Instruction.Create(instr.OpCode, nb.Variables[var]);
break;
case OperandType.InlineField:
ni = Instruction.Create(instr.OpCode, Import((FieldReference)instr.Operand, parent));
break;
case OperandType.InlineMethod:
ni = Instruction.Create(instr.OpCode, Import((MethodReference)instr.Operand, parent));
FixAspNetOffset(nb.Instructions, (MethodReference)instr.Operand, parent);
break;
case OperandType.InlineType:
ni = Instruction.Create(instr.OpCode, Import((TypeReference)instr.Operand, parent));
break;
case OperandType.InlineTok:
if (instr.Operand is TypeReference)
ni = Instruction.Create(instr.OpCode, Import((TypeReference)instr.Operand, parent));
else if (instr.Operand is FieldReference)
ni = Instruction.Create(instr.OpCode, Import((FieldReference)instr.Operand, parent));
else if (instr.Operand is MethodReference)
ni = Instruction.Create(instr.OpCode, Import((MethodReference)instr.Operand, parent));
else
throw new InvalidOperationException();
break;
case OperandType.ShortInlineBrTarget:
case OperandType.InlineBrTarget:
ni = Instruction.Create(instr.OpCode, (Instruction)instr.Operand);
break;
case OperandType.InlineSwitch:
ni = Instruction.Create(instr.OpCode, (Instruction[])instr.Operand);
break;
case OperandType.InlineR:
ni = Instruction.Create(instr.OpCode, (double)instr.Operand);
break;
case OperandType.ShortInlineR:
ni = Instruction.Create(instr.OpCode, (float)instr.Operand);
break;
case OperandType.InlineNone:
ni = Instruction.Create(instr.OpCode);
break;
case OperandType.InlineString:
ni = Instruction.Create(instr.OpCode, (string)instr.Operand);
break;
case OperandType.ShortInlineI:
if (instr.OpCode == OpCodes.Ldc_I4_S)
ni = Instruction.Create(instr.OpCode, (sbyte)instr.Operand);
else
ni = Instruction.Create(instr.OpCode, (byte)instr.Operand);
break;
case OperandType.InlineI8:
ni = Instruction.Create(instr.OpCode, (long)instr.Operand);
break;
case OperandType.InlineI:
ni = Instruction.Create(instr.OpCode, (int)instr.Operand);
break;
default:
throw new InvalidOperationException();
}
nb.Instructions.Add(ni);
}
for (int i = 0; i < body.Instructions.Count; i++)
{
Instruction instr = nb.Instructions[i];
switch (instr.OpCode.OperandType)
{
case OperandType.ShortInlineBrTarget:
case OperandType.InlineBrTarget:
instr.Operand = GetInstruction(body, nb, (Instruction)body.Instructions[i].Operand);
break;
case OperandType.InlineSwitch:
instr.Operand = ((Instruction[])body.Instructions[i].Operand).Select(op => GetInstruction(body, nb, op)).ToArray();
break;
default:
break;
}
}
foreach (ExceptionHandler eh in body.ExceptionHandlers)
{
ExceptionHandler neh = new ExceptionHandler(eh.HandlerType);
neh.TryStart = GetInstruction(body, nb, eh.TryStart);
neh.TryEnd = GetInstruction(body, nb, eh.TryEnd);
neh.HandlerStart = GetInstruction(body, nb, eh.HandlerStart);
neh.HandlerEnd = GetInstruction(body, nb, eh.HandlerEnd);
switch (eh.HandlerType)
{
case ExceptionHandlerType.Catch:
neh.CatchType = Import(eh.CatchType, parent);
break;
case ExceptionHandlerType.Filter:
neh.FilterStart = GetInstruction(body, nb, eh.FilterStart);
break;
}
nb.ExceptionHandlers.Add(neh);
}
}
private TypeDefinition CreateType(TypeDefinition type, Collection<TypeDefinition> col, bool internalize, string rename)
{
TypeDefinition nt = new TypeDefinition(type.Namespace, rename ?? type.Name, type.Attributes);
col.Add(nt);
// only top-level types are internalized
if (internalize && (nt.DeclaringType == null) && nt.IsPublic && !type.CustomAttributes.Any(x => x.AttributeType.Name == ExcludeInternalizeAttName))
nt.IsPublic = false;
CopyGenericParameters(type.GenericParameters, nt.GenericParameters, nt);
if (type.BaseType != null)
nt.BaseType = Import(type.BaseType, nt);
if (type.HasLayoutInfo)
{
nt.ClassSize = type.ClassSize;
nt.PackingSize = type.PackingSize;
}
// don't copy these twice if UnionMerge==true
// TODO: we can move this down if we chek for duplicates when adding
CopySecurityDeclarations(type.SecurityDeclarations, nt.SecurityDeclarations, nt);
CopyInterfaces(type.Interfaces, nt.Interfaces, nt);
CopyCustomAttributes(type.CustomAttributes, nt.CustomAttributes, nt);
return nt;
}
private void CopyInterfaces(Collection<InterfaceImplementation> interfaces1, Collection<InterfaceImplementation> interfaces2, TypeDefinition nt)
{
foreach (var iface in interfaces1)
{
var newIface = new InterfaceImplementation(Import(iface.InterfaceType, nt));
CopyCustomAttributes(iface.CustomAttributes, newIface.CustomAttributes, nt);
interfaces2.Add(newIface);
}
}
private MethodDefinition FindMethodInNewType(TypeDefinition nt, MethodDefinition methodDefinition)
{
var ret = _repackContext.ReflectionHelper.FindMethodDefinitionInType(nt, methodDefinition);
if (ret == null)
{
_logger.Warn("Method '" + methodDefinition.FullName + "' not found in merged type '" + nt.FullName + "'");
}
return ret;
}
private void FixAspNetOffset(Collection<Instruction> instructions, MethodReference operand, MethodDefinition parent)
{
if (operand.Name == "WriteUTF8ResourceString" || operand.Name == "CreateResourceBasedLiteralControl")
{
var fullName = operand.FullName;
if (fullName == "System.Void System.Web.UI.TemplateControl::WriteUTF8ResourceString(System.Web.UI.HtmlTextWriter,System.Int32,System.Int32,System.Boolean)" ||
fullName == "System.Web.UI.LiteralControl System.Web.UI.TemplateControl::CreateResourceBasedLiteralControl(System.Int32,System.Int32,System.Boolean)")
{
int offset;
if (_aspOffsets.TryGetValue(parent.Module.Assembly, out offset))
{
int prev = (int)instructions[instructions.Count - 4].Operand;
instructions[instructions.Count - 4].Operand = prev + offset;
}
}
}
}
private static bool IsIndexer(PropertyDefinition prop)
{
if (prop.Name != "Item" && !prop.Name.EndsWith(".Item")) // cover explicitely implemented properties
return false;
var parameters = ExtractIndexerParameters(prop);
return parameters != null && parameters.Count > 0;
}
private static IList<ParameterDefinition> ExtractIndexerParameters(PropertyDefinition prop)
{
if (prop.GetMethod != null)
return prop.GetMethod.Parameters;
if (prop.SetMethod != null)
return prop.SetMethod.Parameters.ToList().GetRange(0, prop.SetMethod.Parameters.Count - 1);
return null;
}
private static Instruction GetInstruction(MethodBody oldBody, MethodBody newBody, Instruction i)
{
int pos = oldBody.Instructions.IndexOf(i);
if (pos > -1 && pos < newBody.Instructions.Count)
return newBody.Instructions[pos];
return null /*newBody.Instructions.Outside*/;
}
// https://github.com/dotnet/roslyn/blob/ee2526876b7bff3380bc110d819dda23cac668a5/src/Compilers/CSharp/Portable/Symbols/EmbeddableAttributes.cs#L10
private static readonly HashSet<string> allowUnifyTypeNames = new HashSet<string>
{
"System.Runtime.CompilerServices.IsReadOnlyAttribute",
"System.Runtime.CompilerServices.IsByRefLikeAttribute",
"System.Runtime.CompilerServices.IsUnmanagedAttribute",
"System.Runtime.CompilerServices.NullableAttribute",
"System.Runtime.CompilerServices.NullableContextAttribute",
"System.Runtime.CompilerServices.NullablePublicOnlyAttribute",
"System.Runtime.CompilerServices.NativeIntegerAttribute",
"System.Runtime.CompilerServices.ScopedRefAttribute",
"System.Runtime.CompilerServices.RefSafetyRulesAttribute",
"System.Runtime.CompilerServices.RequiresLocationAttribute",
"Microsoft.CodeAnalysis.EmbeddedAttribute"
};
private bool DuplicateTypeAllowed(TypeDefinition type)
{
if (IsWellKnownType(type))
{
return true;
}
if (_options.AllowAllDuplicateTypes || _options.AllowedDuplicateTypes.Contains(type.FullName))
return true;
var top = type;
while (top.IsNested)
top = top.DeclaringType;
string nameSpace = top.Namespace;
if (!String.IsNullOrEmpty(nameSpace) && _options.AllowedDuplicateNameSpaces.Any(s => s == nameSpace || nameSpace.StartsWith(s + ".")))
return true;
return false;
}
private bool IsWellKnownType(TypeDefinition type)
{
string fullName = type.FullName;
// Merging module because IKVM uses this class to store some fields.
// Doesn't fully work yet, as IKVM is nice enough to give all the fields the same name...
if (fullName == "<Module>" || fullName == "__<Proxy>")
return true;
// XAML helper class, identical in all assemblies, unused within the assembly, and instantiated through reflection from the outside
// We could just skip them after the first one, but merging them works just fine
if (fullName == "XamlGeneratedNamespace.GeneratedInternalTypeHelper")
return true;
// Merge should be OK since member's names are pretty unique,
// but renaming duplicate members would be safer...
if (fullName == "<PrivateImplementationDetails>" && type.IsPublic)
return true;
if (allowUnifyTypeNames.Contains(fullName))
{
return true;
}
return false;
}
/// <summary>
/// Clones a collection of SecurityDeclarations
/// </summary>
public void CopySecurityDeclarations(Collection<SecurityDeclaration> input, Collection<SecurityDeclaration> output, IGenericParameterProvider context)
{
foreach (SecurityDeclaration sec in input)
{
SecurityDeclaration newSec = null;
if (PermissionsetHelper.IsXmlPermissionSet(sec))
{
newSec = PermissionsetHelper.Xml2PermissionSet(sec, _repackContext.TargetAssemblyMainModule);
}
if (newSec == null)
{
newSec = new SecurityDeclaration(sec.Action);
foreach (SecurityAttribute sa in sec.SecurityAttributes)
{
SecurityAttribute newSa = new SecurityAttribute(Import(sa.AttributeType, context));
if (sa.HasFields)
{
foreach (CustomAttributeNamedArgument cana in sa.Fields)
{
newSa.Fields.Add(Copy(cana, context));
}
}
if (sa.HasProperties)
{
foreach (CustomAttributeNamedArgument cana in sa.Properties)
{
newSa.Properties.Add(Copy(cana, context));
}
}
newSec.SecurityAttributes.Add(newSa);
}
}
output.Add(newSec);
}
}
// helper
private static void Copy<T>(Collection<T> input, Collection<T> output, Action<T, T> action)
{
if (input.Count != output.Count)
throw new InvalidOperationException();
for (int i = 0; i < input.Count; i++)
{
action.Invoke(input[i], output[i]);
}
}
public void CopyGenericParameters(Collection<GenericParameter> input, Collection<GenericParameter> output, IGenericParameterProvider nt)
{
foreach (GenericParameter gp in input)
{
GenericParameter ngp = new GenericParameter(gp.Name, nt);
ngp.Attributes = gp.Attributes;
output.Add(ngp);
}
// delay copy to ensure all generics parameters are already present
Copy(input, output, (gp, ngp) => CopyTypeReferences(gp.Constraints, ngp.Constraints, nt));
Copy(input, output, (gp, ngp) => CopyCustomAttributes(gp.CustomAttributes, ngp.CustomAttributes, nt));
}
public void CopyCustomAttributes(Collection<CustomAttribute> input, Collection<CustomAttribute> output, IGenericParameterProvider context)
{
CopyCustomAttributes(input, output, true, context);
}
public CustomAttribute Copy(CustomAttribute ca, IGenericParameterProvider context)
{
CustomAttribute newCa = new CustomAttribute(Import(ca.Constructor));
foreach (var arg in ca.ConstructorArguments)
newCa.ConstructorArguments.Add(Copy(arg, context));
foreach (var arg in ca.Fields)
newCa.Fields.Add(Copy(arg, context));
foreach (var arg in ca.Properties)
newCa.Properties.Add(Copy(arg, context));
return newCa;
}
public void CopyCustomAttributes(Collection<CustomAttribute> input, Collection<CustomAttribute> output, bool allowMultiple, IGenericParameterProvider context)
{
var reflectionHelper = _repackContext.ReflectionHelper;
foreach (CustomAttribute ca in input)
{
if (ca.AttributeType.Name == ExcludeInternalizeAttName) continue;
var caType = ca.AttributeType;
var similarAttributes = output.Where(attr => reflectionHelper.AreSame(attr.AttributeType, caType)).ToList();
if (similarAttributes.Count != 0)
{
if (!allowMultiple)
continue;
if (!CustomAttributeTypeAllowsMultiple(caType))
continue;
if (similarAttributes.Any(x =>
reflectionHelper.AreSame(x.ConstructorArguments, ca.ConstructorArguments) &&
reflectionHelper.AreSame(x.Fields, ca.Fields) &&
reflectionHelper.AreSame(x.Properties, ca.Properties)
))
continue;
}
output.Add(Copy(ca, context));
}
}
private bool CustomAttributeTypeAllowsMultiple(TypeReference type)
{
if (type.FullName == "IKVM.Attributes.JavaModuleAttribute" || type.FullName == "IKVM.Attributes.PackageListAttribute")
{
// IKVM module attributes, although they don't allow multiple, IKVM supports the attribute being specified multiple times
return true;
}
TypeDefinition typeDef = type.Resolve();
if (typeDef != null)
{
var ca = typeDef.CustomAttributes.FirstOrDefault(x => x.AttributeType.FullName == "System.AttributeUsageAttribute");
if (ca != null)
{
var prop = ca.Properties.FirstOrDefault(y => y.Name == "AllowMultiple");
if (prop.Argument.Value is bool)
{
return (bool)prop.Argument.Value;
}
}
}
// default is false
return false;
}
public void CopyTypeReferences(Collection<TypeReference> input, Collection<TypeReference> output, IGenericParameterProvider context)
{
foreach (TypeReference ta in input)
{
output.Add(Import(ta, context));
}
}
public void CopyTypeReferences(Collection<GenericParameterConstraint> input, Collection<GenericParameterConstraint> output, IGenericParameterProvider context)
{
foreach (var gpc in input)
{
var result = new GenericParameterConstraint(Import(gpc.ConstraintType, context));
CopyCustomAttributes(gpc.CustomAttributes, result.CustomAttributes, context);
output.Add(result);
}
}
public CustomAttributeArgument Copy(CustomAttributeArgument arg, IGenericParameterProvider context)
{
return new CustomAttributeArgument(Import(arg.Type, context), ImportCustomAttributeValue(arg.Value, context));
}
public CustomAttributeNamedArgument Copy(CustomAttributeNamedArgument namedArg, IGenericParameterProvider context)
{
return new CustomAttributeNamedArgument(namedArg.Name, Copy(namedArg.Argument, context));
}
private object ImportCustomAttributeValue(object obj, IGenericParameterProvider context)
{