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
|
//
// ResolveFromAssemblyStep.cs
//
// Author:
// Jb Evain (jbevain@gmail.com)
//
// (C) 2006 Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Mono.Cecil;
using Mono.Collections.Generic;
namespace Mono.Linker.Steps
{
public class ResolveFromAssemblyStep : ResolveStep
{
AssemblyDefinition _assembly;
string _file;
RootVisibility _rootVisibility;
public enum RootVisibility
{
Any = 0,
PublicAndFamily = 1,
PublicAndFamilyAndAssembly = 2
}
public ResolveFromAssemblyStep (string assembly, RootVisibility rootVisibility = RootVisibility.Any)
{
_file = assembly;
_rootVisibility = rootVisibility;
}
public ResolveFromAssemblyStep (AssemblyDefinition assembly)
{
_assembly = assembly;
}
protected override void Process ()
{
if (_assembly != null)
Context.Resolver.CacheAssembly (_assembly);
AssemblyDefinition assembly = _assembly ?? Context.Resolve (_file);
if (_rootVisibility != RootVisibility.Any && HasInternalsVisibleTo (assembly)) {
_rootVisibility = RootVisibility.PublicAndFamilyAndAssembly;
}
switch (assembly.MainModule.Kind) {
case ModuleKind.Dll:
ProcessLibrary (assembly);
break;
default:
ProcessExecutable (assembly);
break;
}
}
protected static void SetAction (LinkContext context, AssemblyDefinition assembly, AssemblyAction action)
{
TryReadSymbols (context, assembly);
context.Annotations.SetAction (assembly, action);
}
static void TryReadSymbols (LinkContext context, AssemblyDefinition assembly)
{
context.SafeReadSymbols (assembly);
}
protected virtual void ProcessLibrary (AssemblyDefinition assembly)
{
ProcessLibrary (Context, assembly, _rootVisibility);
}
public static void ProcessLibrary (LinkContext context, AssemblyDefinition assembly, RootVisibility rootVisibility = RootVisibility.Any)
{
var action = rootVisibility == RootVisibility.Any ? AssemblyAction.Copy : AssemblyAction.Link;
SetAction (context, assembly, action);
context.Annotations.Push (assembly);
foreach (TypeDefinition type in assembly.MainModule.Types)
MarkType (context, type, rootVisibility);
if (assembly.MainModule.HasExportedTypes) {
foreach (var exported in assembly.MainModule.ExportedTypes) {
bool isForwarder = exported.IsForwarder;
var declaringType = exported.DeclaringType;
while (!isForwarder && (declaringType != null)) {
isForwarder = declaringType.IsForwarder;
declaringType = declaringType.DeclaringType;
}
if (!isForwarder)
continue;
TypeDefinition resolvedExportedType = null;
try {
resolvedExportedType = exported.Resolve ();
} catch (AssemblyResolutionException) {
continue;
}
if (resolvedExportedType == null) {
//
// It's quite common for assemblies to have broken exported types
//
// One source of them is from native csc which added all nested types of
// type-forwarded types automatically including private ones.
//
// Next source of broken type-forwarders is from custom metadata writers which
// simply write bogus information.
//
// Both cases are bugs not on our end but we still want to link all assemblies
// especially when such types cannot be used anyway
//
if (context.LogInternalExceptions)
System.Console.WriteLine ($"Cannot find declaration of exported type '{exported}' from the assembly '{assembly}'");
continue;
}
context.Resolve (resolvedExportedType.Scope);
MarkType (context, resolvedExportedType, rootVisibility);
context.Annotations.Mark (exported);
if (context.KeepTypeForwarderOnlyAssemblies) {
context.Annotations.Mark (assembly.MainModule);
}
}
}
context.Annotations.Pop ();
}
static void MarkType (LinkContext context, TypeDefinition type, RootVisibility rootVisibility)
{
bool markType;
switch (rootVisibility) {
default:
markType = true;
break;
case RootVisibility.PublicAndFamilyAndAssembly:
markType = !type.IsNestedPrivate;
break;
case RootVisibility.PublicAndFamily:
markType = type.IsPublic || type.IsNestedPublic || type.IsNestedFamily || type.IsNestedFamilyOrAssembly;
break;
}
if (!markType) {
return;
}
context.Annotations.Mark (type);
context.Annotations.Push (type);
if (type.HasFields)
MarkFields (context, type.Fields, rootVisibility);
if (type.HasMethods)
MarkMethods (context, type.Methods, rootVisibility);
if (type.HasNestedTypes)
foreach (var nested in type.NestedTypes)
MarkType (context, nested, rootVisibility);
context.Annotations.Pop ();
}
void ProcessExecutable (AssemblyDefinition assembly)
{
SetAction (Context, assembly, AssemblyAction.Link);
Annotations.Push (assembly);
Annotations.Mark (assembly.EntryPoint.DeclaringType);
MarkMethod (Context, assembly.EntryPoint, MethodAction.Parse, RootVisibility.Any);
Annotations.Pop ();
}
static void MarkFields (LinkContext context, Collection<FieldDefinition> fields, RootVisibility rootVisibility)
{
foreach (FieldDefinition field in fields) {
bool markField;
switch (rootVisibility) {
default:
markField = true;
break;
case RootVisibility.PublicAndFamily:
markField = field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly;
break;
case RootVisibility.PublicAndFamilyAndAssembly:
markField = field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly || field.IsAssembly || field.IsFamilyAndAssembly;
break;
}
if (markField) {
context.Annotations.Mark (field);
}
}
}
static void MarkMethods (LinkContext context, Collection<MethodDefinition> methods, RootVisibility rootVisibility)
{
foreach (MethodDefinition method in methods)
MarkMethod (context, method, MethodAction.ForceParse, rootVisibility);
}
static void MarkMethod (LinkContext context, MethodDefinition method, MethodAction action, RootVisibility rootVisibility)
{
bool markMethod;
switch (rootVisibility) {
default:
markMethod = true;
break;
case RootVisibility.PublicAndFamily:
markMethod = method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly;
break;
case RootVisibility.PublicAndFamilyAndAssembly:
markMethod = method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly || method.IsAssembly || method.IsFamilyAndAssembly;
break;
}
if (markMethod) {
context.Annotations.Mark (method);
context.Annotations.SetAction (method, action);
}
}
static bool HasInternalsVisibleTo (AssemblyDefinition assembly)
{
foreach (CustomAttribute attribute in assembly.CustomAttributes) {
if (attribute.Constructor.DeclaringType.FullName ==
"System.Runtime.CompilerServices.InternalsVisibleToAttribute")
return true;
}
return false;
}
}
}
|