5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
34 using Mono.Cecil.Binary;
35 using Mono.Cecil.Metadata;
37 internal sealed class StructureReader : BaseStructureVisitor {
42 AssemblyDefinition m_asmDef;
43 ModuleDefinition m_module;
44 MetadataStreamCollection m_streams;
46 MetadataTableReader m_tableReader;
48 public bool ManifestOnly {
49 get { return m_manifestOnly; }
52 public ImageReader ImageReader {
60 public StructureReader (ImageReader ir)
62 if (ir.Image.CLIHeader == null)
63 throw new ImageFormatException ("The image is not a managed assembly");
67 m_streams = m_img.MetadataRoot.Streams;
68 m_tHeap = m_streams.TablesHeap;
69 m_tableReader = ir.MetadataReader.TableReader;
72 public StructureReader (ImageReader ir, bool manifestOnly) : this (ir)
74 m_manifestOnly = manifestOnly;
77 byte [] ReadBlob (uint pointer)
82 return m_streams.BlobHeap.Read (pointer);
85 string ReadString (uint pointer)
87 return m_streams.StringsHeap [pointer];
90 public override void VisitAssemblyDefinition (AssemblyDefinition asm)
92 if (!m_tHeap.HasTable (AssemblyTable.RId))
93 throw new ReflectionException ("No assembly manifest");
95 asm.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
98 string version = m_img.MetadataRoot.Header.Version;
102 asm.Runtime = TargetRuntime.NET_1_0;
105 asm.Runtime = TargetRuntime.NET_1_1;
108 asm.Runtime = TargetRuntime.NET_2_0;
111 asm.Runtime = TargetRuntime.NET_2_1;
114 if (version.StartsWith ("v1"))
115 asm.Runtime = TargetRuntime.NET_1_1;
117 asm.Runtime = TargetRuntime.NET_2_0;
121 if ((m_img.PEFileHeader.Characteristics & ImageCharacteristics.Dll) != 0)
122 asm.Kind = AssemblyKind.Dll;
123 else if (m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsGui ||
124 m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsCeGui)
125 asm.Kind = AssemblyKind.Windows;
127 asm.Kind = AssemblyKind.Console;
130 public override void VisitAssemblyNameDefinition (AssemblyNameDefinition name)
132 AssemblyTable atable = m_tableReader.GetAssemblyTable ();
133 AssemblyRow arow = atable [0];
134 name.Name = ReadString (arow.Name);
135 name.Flags = arow.Flags;
136 name.PublicKey = ReadBlob (arow.PublicKey);
138 name.Culture = ReadString (arow.Culture);
139 name.Version = new Version (
140 arow.MajorVersion, arow.MinorVersion,
141 arow.BuildNumber, arow.RevisionNumber);
142 name.HashAlgorithm = arow.HashAlgId;
143 name.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
146 public override void VisitAssemblyNameReferenceCollection (AssemblyNameReferenceCollection names)
148 if (!m_tHeap.HasTable (AssemblyRefTable.RId))
151 AssemblyRefTable arTable = m_tableReader.GetAssemblyRefTable ();
152 for (int i = 0; i < arTable.Rows.Count; i++) {
153 AssemblyRefRow arRow = arTable [i];
154 AssemblyNameReference aname = new AssemblyNameReference (
155 ReadString (arRow.Name),
156 ReadString (arRow.Culture),
157 new Version (arRow.MajorVersion, arRow.MinorVersion,
158 arRow.BuildNumber, arRow.RevisionNumber));
159 aname.PublicKeyToken = ReadBlob (arRow.PublicKeyOrToken);
160 aname.Hash = ReadBlob (arRow.HashValue);
161 aname.Flags = arRow.Flags;
162 aname.MetadataToken = new MetadataToken (TokenType.AssemblyRef, (uint) i + 1);
167 public override void VisitResourceCollection (ResourceCollection resources)
169 if (!m_tHeap.HasTable (ManifestResourceTable.RId))
172 ManifestResourceTable mrTable = m_tableReader.GetManifestResourceTable ();
173 FileTable fTable = m_tableReader.GetFileTable ();
175 for (int i = 0; i < mrTable.Rows.Count; i++) {
176 ManifestResourceRow mrRow = mrTable [i];
177 if (mrRow.Implementation.RID == 0) {
178 EmbeddedResource eres = new EmbeddedResource (
179 ReadString (mrRow.Name), mrRow.Flags);
181 BinaryReader br = m_ir.MetadataReader.GetDataReader (
182 m_img.CLIHeader.Resources.VirtualAddress);
183 br.BaseStream.Position += mrRow.Offset;
185 eres.Data = br.ReadBytes (br.ReadInt32 ());
187 resources.Add (eres);
191 switch (mrRow.Implementation.TokenType) {
192 case TokenType.File :
193 FileRow fRow = fTable [(int) mrRow.Implementation.RID - 1];
194 LinkedResource lres = new LinkedResource (
195 ReadString (mrRow.Name), mrRow.Flags,
196 ReadString (fRow.Name));
197 lres.Hash = ReadBlob (fRow.HashValue);
198 resources.Add (lres);
200 case TokenType.AssemblyRef :
201 AssemblyNameReference asm =
202 m_module.AssemblyReferences [(int) mrRow.Implementation.RID - 1];
203 AssemblyLinkedResource alr = new AssemblyLinkedResource (
204 ReadString (mrRow.Name),
212 public override void VisitModuleDefinitionCollection (ModuleDefinitionCollection modules)
214 ModuleTable mt = m_tableReader.GetModuleTable ();
215 if (mt == null || mt.Rows.Count != 1)
216 throw new ReflectionException ("Can not read main module");
218 ModuleRow mr = mt [0];
219 string name = ReadString (mr.Name);
220 ModuleDefinition main = new ModuleDefinition (name, m_asmDef, this, true);
221 main.Mvid = m_streams.GuidHeap [mr.Mvid];
222 main.MetadataToken = new MetadataToken (TokenType.Module, 1);
225 m_module.Accept (this);
227 FileTable ftable = m_tableReader.GetFileTable ();
228 if (ftable == null || ftable.Rows.Count == 0)
231 foreach (FileRow frow in ftable.Rows) {
232 if (frow.Flags != FileAttributes.ContainsMetaData)
235 name = ReadString (frow.Name);
236 FileInfo location = new FileInfo (
237 m_img.FileInformation != null ? Path.Combine (m_img.FileInformation.DirectoryName, name) : name);
238 if (!File.Exists (location.FullName))
239 throw new FileNotFoundException ("Module not found : " + name);
242 ImageReader module = ImageReader.Read (location.FullName);
243 mt = module.Image.MetadataRoot.Streams.TablesHeap [ModuleTable.RId] as ModuleTable;
244 if (mt == null || mt.Rows.Count != 1)
245 throw new ReflectionException ("Can not read module : " + name);
248 ModuleDefinition modext = new ModuleDefinition (name, m_asmDef,
249 new StructureReader (module, m_manifestOnly), false);
250 modext.Mvid = module.Image.MetadataRoot.Streams.GuidHeap [mr.Mvid];
252 modules.Add (modext);
253 modext.Accept (this);
254 } catch (ReflectionException) {
256 } catch (Exception e) {
257 throw new ReflectionException ("Can not read module : " + name, e);
262 public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
264 if (!m_tHeap.HasTable (ModuleRefTable.RId))
267 ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
268 for (int i = 0; i < mrTable.Rows.Count; i++) {
269 ModuleRefRow mrRow = mrTable [i];
270 ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
271 mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
276 public override void TerminateAssemblyDefinition (AssemblyDefinition asm)
281 foreach (ModuleDefinition mod in asm.Modules)
282 mod.Controller.Reader.VisitModuleDefinition (mod);