This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
forked from gluck/ikvm
/
Module.cs
432 lines (375 loc) · 13.6 KB
/
Module.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
/*
Copyright (C) 2009 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors 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. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
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.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.Collections.Generic;
using IKVM.Reflection.Metadata;
using IKVM.Reflection.Reader;
namespace IKVM.Reflection
{
public sealed class RawModule : IDisposable
{
private readonly ModuleReader module;
private bool imported;
internal RawModule(ModuleReader module)
{
this.module = module;
}
public string Location
{
get { return module.FullyQualifiedName; }
}
public bool IsManifestModule
{
get { return module.Assembly != null; }
}
private void CheckManifestModule()
{
if (!IsManifestModule)
{
throw new BadImageFormatException("Module does not contain a manifest");
}
}
public AssemblyName GetAssemblyName()
{
CheckManifestModule();
return module.Assembly.GetName();
}
public AssemblyName[] GetReferencedAssemblies()
{
CheckManifestModule();
return module.Assembly.GetReferencedAssemblies();
}
public void Dispose()
{
if (!imported)
{
module.stream.Dispose();
}
}
internal Assembly ToAssembly()
{
if (imported)
{
throw new InvalidOperationException();
}
imported = true;
return module.Assembly;
}
}
public abstract class Module : ICustomAttributeProvider
{
internal readonly Universe universe;
internal readonly ModuleTable ModuleTable = new ModuleTable();
internal readonly TypeRefTable TypeRef = new TypeRefTable();
internal readonly TypeDefTable TypeDef = new TypeDefTable();
internal readonly FieldTable Field = new FieldTable();
internal readonly MemberRefTable MemberRef = new MemberRefTable();
internal readonly ConstantTable Constant = new ConstantTable();
internal readonly CustomAttributeTable CustomAttribute = new CustomAttributeTable();
internal readonly FieldMarshalTable FieldMarshal = new FieldMarshalTable();
internal readonly DeclSecurityTable DeclSecurity = new DeclSecurityTable();
internal readonly ClassLayoutTable ClassLayout = new ClassLayoutTable();
internal readonly FieldLayoutTable FieldLayout = new FieldLayoutTable();
internal readonly ParamTable Param = new ParamTable();
internal readonly InterfaceImplTable InterfaceImpl = new InterfaceImplTable();
internal readonly StandAloneSigTable StandAloneSig = new StandAloneSigTable();
internal readonly EventMapTable EventMap = new EventMapTable();
internal readonly EventTable Event = new EventTable();
internal readonly PropertyMapTable PropertyMap = new PropertyMapTable();
internal readonly PropertyTable Property = new PropertyTable();
internal readonly MethodSemanticsTable MethodSemantics = new MethodSemanticsTable();
internal readonly MethodImplTable MethodImpl = new MethodImplTable();
internal readonly ModuleRefTable ModuleRef = new ModuleRefTable();
internal readonly TypeSpecTable TypeSpec = new TypeSpecTable();
internal readonly ImplMapTable ImplMap = new ImplMapTable();
internal readonly FieldRVATable FieldRVA = new FieldRVATable();
internal readonly AssemblyTable AssemblyTable = new AssemblyTable();
internal readonly AssemblyRefTable AssemblyRef = new AssemblyRefTable();
internal readonly MethodDefTable MethodDef = new MethodDefTable();
internal readonly NestedClassTable NestedClass = new NestedClassTable();
internal readonly FileTable File = new FileTable();
internal readonly ExportedTypeTable ExportedType = new ExportedTypeTable();
internal readonly ManifestResourceTable ManifestResource = new ManifestResourceTable();
internal readonly GenericParamTable GenericParam = new GenericParamTable();
internal readonly MethodSpecTable MethodSpec = new MethodSpecTable();
internal readonly GenericParamConstraintTable GenericParamConstraint = new GenericParamConstraintTable();
internal Module(Universe universe)
{
this.universe = universe;
}
internal Table[] GetTables()
{
Table[] tables = new Table[64];
tables[ModuleTable.Index] = ModuleTable;
tables[TypeRefTable.Index] = TypeRef;
tables[TypeDefTable.Index] = TypeDef;
tables[FieldTable.Index] = Field;
tables[MemberRefTable.Index] = MemberRef;
tables[ConstantTable.Index] = Constant;
tables[CustomAttributeTable.Index] = CustomAttribute;
tables[FieldMarshalTable.Index] = FieldMarshal;
tables[DeclSecurityTable.Index] = DeclSecurity;
tables[ClassLayoutTable.Index] = ClassLayout;
tables[FieldLayoutTable.Index] = FieldLayout;
tables[ParamTable.Index] = Param;
tables[InterfaceImplTable.Index] = InterfaceImpl;
tables[StandAloneSigTable.Index] = StandAloneSig;
tables[EventMapTable.Index] = EventMap;
tables[EventTable.Index] = Event;
tables[PropertyMapTable.Index] = PropertyMap;
tables[PropertyTable.Index] = Property;
tables[MethodSemanticsTable.Index] = MethodSemantics;
tables[MethodImplTable.Index] = MethodImpl;
tables[ModuleRefTable.Index] = ModuleRef;
tables[TypeSpecTable.Index] = TypeSpec;
tables[ImplMapTable.Index] = ImplMap;
tables[FieldRVATable.Index] = FieldRVA;
tables[AssemblyTable.Index] = AssemblyTable;
tables[AssemblyRefTable.Index] = AssemblyRef;
tables[MethodDefTable.Index] = MethodDef;
tables[NestedClassTable.Index] = NestedClass;
tables[FileTable.Index] = File;
tables[ExportedTypeTable.Index] = ExportedType;
tables[ManifestResourceTable.Index] = ManifestResource;
tables[GenericParamTable.Index] = GenericParam;
tables[MethodSpecTable.Index] = MethodSpec;
tables[GenericParamConstraintTable.Index] = GenericParamConstraint;
return tables;
}
public virtual void __GetDataDirectoryEntry(int index, out int rva, out int length)
{
throw new NotSupportedException();
}
public virtual long __RelativeVirtualAddressToFileOffset(int rva)
{
throw new NotSupportedException();
}
public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
{
throw new NotSupportedException();
}
public virtual int __Subsystem
{
get { throw new NotSupportedException(); }
}
public FieldInfo GetField(string name)
{
return IsResource() ? null : GetModuleType().GetField(name);
}
public FieldInfo GetField(string name, BindingFlags bindingFlags)
{
return IsResource() ? null : GetModuleType().GetField(name, bindingFlags);
}
public FieldInfo[] GetFields()
{
return IsResource() ? Empty<FieldInfo>.Array : GetModuleType().GetFields();
}
public FieldInfo[] GetFields(BindingFlags bindingFlags)
{
return IsResource() ? Empty<FieldInfo>.Array : GetModuleType().GetFields(bindingFlags);
}
public MethodInfo GetMethod(string name)
{
return IsResource() ? null : GetModuleType().GetMethod(name);
}
public MethodInfo GetMethod(string name, Type[] types)
{
return IsResource() ? null : GetModuleType().GetMethod(name, types);
}
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers)
{
return IsResource() ? null : GetModuleType().GetMethod(name, bindingAttr, binder, callConv, types, modifiers);
}
public MethodInfo[] GetMethods()
{
return IsResource() ? Empty<MethodInfo>.Array : GetModuleType().GetMethods();
}
public MethodInfo[] GetMethods(BindingFlags bindingFlags)
{
return IsResource() ? Empty<MethodInfo>.Array : GetModuleType().GetMethods(bindingFlags);
}
public ConstructorInfo __ModuleInitializer
{
get { return IsResource() ? null : GetModuleType().TypeInitializer; }
}
public byte[] ResolveSignature(int metadataToken)
{
ModuleReader rdr = this as ModuleReader;
if (rdr != null)
{
ByteReader br = rdr.ResolveSignature(metadataToken);
return br.ReadBytes(br.Length);
}
throw new NotSupportedException();
}
public virtual __StandAloneMethodSig __ResolveStandAloneMethodSig(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
{
throw new NotSupportedException();
}
public int MetadataToken
{
get { return IsResource() ? 0 : 1; }
}
public abstract int MDStreamVersion { get ;}
public abstract Assembly Assembly { get; }
public abstract string FullyQualifiedName { get; }
public abstract string Name { get; }
public abstract Guid ModuleVersionId { get; }
public abstract Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
public abstract MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
public abstract FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
public abstract MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
public abstract string ResolveString(int metadataToken);
public abstract Type[] __ResolveOptionalParameterTypes(int metadataToken);
public abstract string ScopeName { get; }
internal abstract Type GetTypeImpl(string typeName);
internal abstract void GetTypesImpl(List<Type> list);
public Type GetType(string className)
{
return GetType(className, false, false);
}
public Type GetType(string className, bool ignoreCase)
{
return GetType(className, false, ignoreCase);
}
public Type GetType(string className, bool throwOnError, bool ignoreCase)
{
if (ignoreCase)
{
throw new NotImplementedException();
}
TypeNameParser parser = TypeNameParser.Parse(className, throwOnError);
if (parser.Error)
{
return null;
}
if (parser.AssemblyName != null)
{
if (throwOnError)
{
throw new ArgumentException("Type names passed to Module.GetType() must not specify an assembly.");
}
else
{
return null;
}
}
return parser.Expand(GetTypeImpl(parser.FirstNamePart), this.Assembly, throwOnError, className);
}
public Type[] GetTypes()
{
List<Type> list = new List<Type>();
GetTypesImpl(list);
return list.ToArray();
}
public Type[] FindTypes(TypeFilter filter, object filterCriteria)
{
List<Type> list = new List<Type>();
foreach (Type type in GetTypes())
{
if (filter(type, filterCriteria))
{
list.Add(type);
}
}
return list.ToArray();
}
public virtual bool IsResource()
{
return false;
}
public Type ResolveType(int metadataToken)
{
return ResolveType(metadataToken, null, null);
}
public MethodBase ResolveMethod(int metadataToken)
{
return ResolveMethod(metadataToken, null, null);
}
public FieldInfo ResolveField(int metadataToken)
{
return ResolveField(metadataToken, null, null);
}
public MemberInfo ResolveMember(int metadataToken)
{
return ResolveMember(metadataToken, null, null);
}
public bool IsDefined(Type attributeType, bool inherit)
{
return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
}
public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
{
return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
}
internal Type CanonicalizeType(Type type)
{
Type canon;
if (!universe.canonicalizedTypes.TryGetValue(type, out canon))
{
canon = type;
universe.canonicalizedTypes.Add(canon, canon);
}
return canon;
}
internal abstract Type GetModuleType();
internal abstract ByteReader GetBlob(int blobIndex);
internal IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
{
return GetCustomAttributes(0x00000001, attributeType);
}
internal List<CustomAttributeData> GetCustomAttributes(int metadataToken, Type attributeType)
{
List<CustomAttributeData> list = new List<CustomAttributeData>();
// TODO use binary search?
for (int i = 0; i < CustomAttribute.records.Length; i++)
{
if (CustomAttribute.records[i].Parent == metadataToken)
{
ConstructorInfo constructor = (ConstructorInfo)ResolveMethod(CustomAttribute.records[i].Type);
if (attributeType == null || attributeType.IsAssignableFrom(constructor.DeclaringType))
{
list.Add(new CustomAttributeData(this.Assembly, constructor, GetBlob(CustomAttribute.records[i].Value)));
}
}
}
return list;
}
internal IList<CustomAttributeData> GetDeclarativeSecurity(int metadataToken)
{
List<CustomAttributeData> list = new List<CustomAttributeData>();
// TODO use binary search?
for (int i = 0; i < DeclSecurity.records.Length; i++)
{
if (DeclSecurity.records[i].Parent == metadataToken)
{
int action = DeclSecurity.records[i].Action;
int permissionSet = DeclSecurity.records[i].PermissionSet;
CustomAttributeData.ReadDeclarativeSecurity(this.Assembly, list, action, GetBlob(permissionSet));
}
}
return list;
}
internal virtual void Dispose()
{
}
}
public delegate bool TypeFilter(Type m, object filterCriteria);
public delegate bool MemberFilter(MemberInfo m, object filterCriteria);
}