Debian Codegraph @

external/ikvm/reflect/Reader/AssemblyReader.cs

raw
1 /*
2 Copyright (C) 2009 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 [email protected]
22
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Configuration.Assemblies;
27 using System.IO;
28 using IKVM.Reflection.Metadata;
29
30 namespace IKVM.Reflection.Reader
31 {
32 sealed class AssemblyReader : Assembly
33 {
34 private const int ContainsNoMetaData = 0x0001;
35 private readonly string location;
36 private readonly ModuleReader manifestModule;
37 private readonly Module[] externalModules;
38
39 internal AssemblyReader(string location, ModuleReader manifestModule)
40 : base(manifestModule.universe)
41 {
42 this.location = location;
43 this.manifestModule = manifestModule;
44 externalModules = new Module[manifestModule.File.records.Length];
45 }
46
47 public override string Location
48 {
49 get { return location ?? ""; }
50 }
51
52 public override AssemblyName GetName()
53 {
54 return GetNameImpl(ref manifestModule.AssemblyTable.records[0]);
55 }
56
57 private AssemblyName GetNameImpl(ref AssemblyTable.Record rec)
58 {
59 AssemblyName name = new AssemblyName();
60 name.Name = manifestModule.GetString(rec.Name);
61 name.Version = new Version(rec.MajorVersion, rec.MinorVersion, rec.BuildNumber, rec.RevisionNumber);
62 if (rec.PublicKey != 0)
63 {
64 name.SetPublicKey(manifestModule.GetBlobCopy(rec.PublicKey));
65 }
66 else
67 {
68 name.SetPublicKey(Empty<byte>.Array);
69 }
70 if (rec.Culture != 0)
71 {
72 name.Culture = manifestModule.GetString(rec.Culture);
73 }
74 else
75 {
76 name.Culture = "";
77 }
78 name.HashAlgorithm = (AssemblyHashAlgorithm)rec.HashAlgId;
79 name.CodeBase = this.CodeBase;
80 PortableExecutableKinds peKind;
81 ImageFileMachine machine;
82 manifestModule.GetPEKind(out peKind, out machine);
83 switch (machine)
84 {
85 case ImageFileMachine.I386:
86 // FXBUG we copy the .NET bug that Preferred32Bit implies x86
87 if ((peKind & (PortableExecutableKinds.Required32Bit | PortableExecutableKinds.Preferred32Bit)) != 0)
88 {
89 name.ProcessorArchitecture = ProcessorArchitecture.X86;
90 }
91 else if ((rec.Flags & 0x70) == 0x70)
92 {
93 // it's a reference assembly
94 name.ProcessorArchitecture = ProcessorArchitecture.None;
95 }
96 else
97 {
98 name.ProcessorArchitecture = ProcessorArchitecture.MSIL;
99 }
100 break;
101 case ImageFileMachine.IA64:
102 name.ProcessorArchitecture = ProcessorArchitecture.IA64;
103 break;
104 case ImageFileMachine.AMD64:
105 name.ProcessorArchitecture = ProcessorArchitecture.Amd64;
106 break;
107 case ImageFileMachine.ARM:
108 name.ProcessorArchitecture = ProcessorArchitecture.Arm;
109 break;
110 }
111 name.RawFlags = (AssemblyNameFlags)rec.Flags;
112 return name;
113 }
114
115 public override Type[] GetTypes()
116 {
117 if (externalModules.Length == 0)
118 {
119 return manifestModule.GetTypes();
120 }
121
122 List<Type> list = new List<Type>();
123 foreach (Module module in GetModules(false))
124 {
125 list.AddRange(module.GetTypes());
126 }
127 return list.ToArray();
128 }
129
130 internal override Type FindType(TypeName typeName)
131 {
132 Type type = manifestModule.FindType(typeName);
133 for (int i = 0; type == null && i < externalModules.Length; i++)
134 {
135 if ((manifestModule.File.records[i].Flags & ContainsNoMetaData) == 0)
136 {
137 type = GetModule(i).FindType(typeName);
138 }
139 }
140 return type;
141 }
142
143 internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
144 {
145 Type type = manifestModule.FindTypeIgnoreCase(lowerCaseName);
146 for (int i = 0; type == null && i < externalModules.Length; i++)
147 {
148 if ((manifestModule.File.records[i].Flags & ContainsNoMetaData) == 0)
149 {
150 type = GetModule(i).FindTypeIgnoreCase(lowerCaseName);
151 }
152 }
153 return type;
154 }
155
156 public override string ImageRuntimeVersion
157 {
158 get { return manifestModule.__ImageRuntimeVersion; }
159 }
160
161 public override Module ManifestModule
162 {
163 get { return manifestModule; }
164 }
165
166 public override Module[] GetLoadedModules(bool getResourceModules)
167 {
168 List<Module> list = new List<Module>();
169 list.Add(manifestModule);
170 foreach (Module m in externalModules)
171 {
172 if (m != null)
173 {
174 list.Add(m);
175 }
176 }
177 return list.ToArray();
178 }
179
180 public override Module[] GetModules(bool getResourceModules)
181 {
182 if (externalModules.Length == 0)
183 {
184 return new Module[] { manifestModule };
185 }
186 else
187 {
188 List<Module> list = new List<Module>();
189 list.Add(manifestModule);
190 for (int i = 0; i < manifestModule.File.records.Length; i++)
191 {
192 if (getResourceModules || (manifestModule.File.records[i].Flags & ContainsNoMetaData) == 0)
193 {
194 list.Add(GetModule(i));
195 }
196 }
197 return list.ToArray();
198 }
199 }
200
201 public override Module GetModule(string name)
202 {
203 if (name.Equals(manifestModule.ScopeName, StringComparison.OrdinalIgnoreCase))
204 {
205 return manifestModule;
206 }
207 int index = GetModuleIndex(name);
208 if (index != -1)
209 {
210 return GetModule(index);
211 }
212 return null;
213 }
214
215 private int GetModuleIndex(string name)
216 {
217 for (int i = 0; i < manifestModule.File.records.Length; i++)
218 {
219 if (name.Equals(manifestModule.GetString(manifestModule.File.records[i].Name), StringComparison.OrdinalIgnoreCase))
220 {
221 return i;
222 }
223 }
224 return -1;
225 }
226
227 private Module GetModule(int index)
228 {
229 if (externalModules[index] != null)
230 {
231 return externalModules[index];
232 }
233 return LoadModule(index, null, manifestModule.GetString(manifestModule.File.records[index].Name));
234 }
235
236 private Module LoadModule(int index, byte[] rawModule, string name)
237 {
238 string location = name == null ? null : Path.Combine(Path.GetDirectoryName(this.location), name);
239 if ((manifestModule.File.records[index].Flags & ContainsNoMetaData) != 0)
240 {
241 return externalModules[index] = new ResourceModule(manifestModule, index, location);
242 }
243 else
244 {
245 if (rawModule == null)
246 {
247 try
248 {
249 rawModule = File.ReadAllBytes(location);
250 }
251 catch (FileNotFoundException)
252 {
253 if (resolvers != null)
254 {
255 ResolveEventArgs arg = new ResolveEventArgs(name, this);
256 foreach (ModuleResolveEventHandler resolver in resolvers)
257 {
258 Module module = resolver(this, arg);
259 if (module != null)
260 {
261 return module;
262 }
263 }
264 }
265 if (universe.MissingMemberResolution)
266 {
267 return externalModules[index] = new MissingModule(this, index);
268 }
269 throw;
270 }
271 }
272 return externalModules[index] = new ModuleReader(this, manifestModule.universe, new MemoryStream(rawModule), location, false);
273 }
274 }
275
276 public override Module LoadModule(string moduleName, byte[] rawModule)
277 {
278 int index = GetModuleIndex(moduleName);
279 if (index == -1)
280 {
281 throw new ArgumentException();
282 }
283 if (externalModules[index] != null)
284 {
285 return externalModules[index];
286 }
287 return LoadModule(index, rawModule, null);
288 }
289
290 public override MethodInfo EntryPoint
291 {
292 get { return manifestModule.GetEntryPoint(); }
293 }
294
295 public override string[] GetManifestResourceNames()
296 {
297 return manifestModule.GetManifestResourceNames();
298 }
299
300 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName)
301 {
302 return manifestModule.GetManifestResourceInfo(resourceName);
303 }
304
305 public override Stream GetManifestResourceStream(string resourceName)
306 {
307 return manifestModule.GetManifestResourceStream(resourceName);
308 }
309
310 public override AssemblyName[] GetReferencedAssemblies()
311 {
312 return manifestModule.__GetReferencedAssemblies();
313 }
314
315 protected override AssemblyNameFlags GetAssemblyFlags()
316 {
317 return (AssemblyNameFlags)manifestModule.AssemblyTable.records[0].Flags;
318 }
319
320 internal string Name
321 {
322 get { return manifestModule.GetString(manifestModule.AssemblyTable.records[0].Name); }
323 }
324
325 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
326 {
327 return CustomAttributeData.GetCustomAttributesImpl(null, manifestModule, 0x20000001, attributeType) ?? CustomAttributeData.EmptyList;
328 }
329 }
330 }