forked from gluck/il-repack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInitializersRepackStep.cs
More file actions
222 lines (186 loc) · 8.31 KB
/
ModuleInitializersRepackStep.cs
File metadata and controls
222 lines (186 loc) · 8.31 KB
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
//
// Copyright (c) 2025 David Rettenbacher
//
// 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 Mono.Cecil;
using Mono.Cecil.Cil;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ILRepacking.Steps
{
internal class ModuleInitializersRepackStep : IRepackStep
{
private readonly ILogger _logger;
private readonly IRepackContext _repackContext;
public ModuleInitializersRepackStep(
ILogger logger,
IRepackContext repackContext,
RepackOptions repackOptions)
{
_logger = logger;
_repackContext = repackContext;
}
public void Perform()
{
RepackModuleInitializers();
}
private void RepackModuleInitializers()
{
_logger.Verbose("Processing module initializers");
var assemblies = _repackContext.OtherAssemblies
.Concat([_repackContext.PrimaryAssemblyDefinition])
.ToHashSet();
var orderedAssemblies = TopologicalSort(assemblies);
var modulesToMerge = orderedAssemblies // dependency-assemblies should be deep-first, so the call order is deep-first
.SelectMany(x => x.Modules);
MergeModuleInitializers(_repackContext.TargetAssemblyMainModule, modulesToMerge);
}
/// <summary>
/// Checks if there are other module initializers to call from the primary module initializer.
/// If that is the case, a new initializer is added which calls all found module initializers.
/// All found initializers are renamed to be unique while still conveying their origin.
/// </summary>
/// <param name="targetModule">Target module which gets the new module initializer</param>
/// <param name="modulesToMerge">Modules which should be scanned for module initializers.</param>
private void MergeModuleInitializers(ModuleDefinition targetModule, IEnumerable<ModuleDefinition> modulesToMerge)
{
var anyModuleInitializersToMerge = modulesToMerge.Any(m =>
m.Types.Any(t =>
t.Name == "<Module>" &&
t.Methods.Any(m =>
m.IsStatic &&
m.Name == ".cctor")));
if (!anyModuleInitializersToMerge)
{
_logger.Verbose("- Found no module initializers to be merged - skip");
return;
}
var targetModuleType = targetModule.Types.FirstOrDefault(t => t.Name == "<Module>");
if (targetModuleType is null)
{
targetModuleType = new TypeDefinition(
"",
"<Module>",
TypeAttributes.NotPublic | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit,
targetModule.TypeSystem.Object
);
targetModule.Types.Add(targetModuleType);
}
var targetInitializer = new MethodDefinition(
".cctor",
MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
targetModule.TypeSystem.Void
);
var newIl = targetInitializer.Body.GetILProcessor();
foreach (var moduleToMerge in modulesToMerge)
{
// search inside the assembly the module initializer type "<Module>"
var type = moduleToMerge.Types.FirstOrDefault(t => t.Name == "<Module>");
if (type is null)
continue;
// search its static constructor (the module initializer method)
var subInitializer = type.Methods.FirstOrDefault(m => m.Name == ".cctor" && m.IsStatic);
if (subInitializer is null)
continue;
_logger.Verbose($"- Process module initializer of '{moduleToMerge.Assembly.Name.Name}'");
DemoteModuleInitializerMethodToNormalMethod(subInitializer);
var call = newIl.Create(OpCodes.Call, targetModule.ImportReference(subInitializer));
newIl.Append(call);
}
newIl.Append(newIl.Create(OpCodes.Ret));
targetModuleType.Methods.Add(targetInitializer);
}
private void DemoteModuleInitializerMethodToNormalMethod(MethodDefinition initializer)
{
var newName = $"{initializer.Module.Assembly.Name}_{Path.GetFileNameWithoutExtension(initializer.Module.Name)}_ModuleInitializer";
newName = newName.Replace(" ", "_").Replace("=", "_").Replace(",", "").Replace(".", "_");
_logger.Verbose($" - Rename module initializer of '{initializer.Module.Assembly.Name.Name}' to '{newName}'");
initializer.Name = newName;
initializer.IsSpecialName = false;
initializer.IsRuntimeSpecialName = false;
}
private Dictionary<string, AssemblyDefinition> ToDictionarySkipDuplicates(IEnumerable<AssemblyDefinition> assemblies)
{
var dict = new Dictionary<string, AssemblyDefinition>();
foreach (var assembly in assemblies)
{
if (!dict.ContainsKey(assembly.Name.Name))
{
dict[assembly.Name.Name] = assembly;
}
else
{
_logger.Verbose($"- Duplicate key found: {assembly.Name.Name} - skipping");
}
}
return dict;
}
private List<AssemblyDefinition> TopologicalSort(HashSet<AssemblyDefinition> assemblies)
{
var loadedAssemblies = ToDictionarySkipDuplicates(assemblies); // Ensure quick lookup
var visited = new HashSet<AssemblyDefinition>();
var deepFirstAssemblies = new List<AssemblyDefinition>(assemblies.Count);
_logger.Verbose("- Sort dependencies");
foreach (var assembly in assemblies)
{
if (DepthFirstSearch(assembly))
{
break;
}
}
return deepFirstAssemblies;
bool DepthFirstSearch(AssemblyDefinition assembly)
{
if (!visited.Add(assembly)) // already visited
return false;
foreach (var reference in assembly.MainModule.AssemblyReferences)
{
if (!loadedAssemblies.TryGetValue(reference.Name, out var referencedAsm))
{
try
{
referencedAsm = _repackContext.GlobalAssemblyResolver.Resolve(reference);
loadedAssemblies[reference.Name] = referencedAsm;
_logger.Verbose($" - Loaded {reference.Name}");
}
catch
{
// noop
}
if (referencedAsm is null)
{
_logger.Verbose($"- Warning: Could not find {reference.Name}");
continue;
}
}
if (DepthFirstSearch(referencedAsm))
{
return true;
}
}
if (assemblies.Contains(assembly))
{
deepFirstAssemblies.Add(assembly);
}
// found all assemblies yet?
if (deepFirstAssemblies.Count == assemblies.Count)
{
return true;
}
return false;
}
}
}
}