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 switch (m_img.MetadataRoot.Header.Version) {
100 asm.Runtime = TargetRuntime.NET_1_0;
103 asm.Runtime = TargetRuntime.NET_1_1;
106 asm.Runtime = TargetRuntime.NET_2_0;
110 if ((m_img.PEFileHeader.Characteristics & ImageCharacteristics.Dll) != 0)
111 asm.Kind = AssemblyKind.Dll;
112 else if (m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsGui ||
113 m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsCeGui)
114 asm.Kind = AssemblyKind.Windows;
116 asm.Kind = AssemblyKind.Console;
119 public override void VisitAssemblyNameDefinition (AssemblyNameDefinition name)
121 AssemblyTable atable = m_tableReader.GetAssemblyTable ();
122 AssemblyRow arow = atable [0];
123 name.Name = ReadString (arow.Name);
124 name.Flags = arow.Flags;
125 name.PublicKey = ReadBlob (arow.PublicKey);
127 name.Culture = ReadString (arow.Culture);
128 name.Version = new Version (
129 arow.MajorVersion, arow.MinorVersion,
130 arow.BuildNumber, arow.RevisionNumber);
131 name.HashAlgorithm = arow.HashAlgId;
132 name.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
135 public override void VisitAssemblyNameReferenceCollection (AssemblyNameReferenceCollection names)
137 if (!m_tHeap.HasTable (AssemblyRefTable.RId))
140 AssemblyRefTable arTable = m_tableReader.GetAssemblyRefTable ();
141 for (int i = 0; i < arTable.Rows.Count; i++) {
142 AssemblyRefRow arRow = arTable [i];
143 AssemblyNameReference aname = new AssemblyNameReference (
144 ReadString (arRow.Name),
145 ReadString (arRow.Culture),
146 new Version (arRow.MajorVersion, arRow.MinorVersion,
147 arRow.BuildNumber, arRow.RevisionNumber));
148 aname.PublicKeyToken = ReadBlob (arRow.PublicKeyOrToken);
149 aname.Hash = ReadBlob (arRow.HashValue);
150 aname.Flags = arRow.Flags;
151 aname.MetadataToken = new MetadataToken (TokenType.AssemblyRef, (uint) i + 1);
156 public override void VisitResourceCollection (ResourceCollection resources)
158 if (!m_tHeap.HasTable (ManifestResourceTable.RId))
161 ManifestResourceTable mrTable = m_tableReader.GetManifestResourceTable ();
162 FileTable fTable = m_tableReader.GetFileTable ();
164 for (int i = 0; i < mrTable.Rows.Count; i++) {
165 ManifestResourceRow mrRow = mrTable [i];
166 if (mrRow.Implementation.RID == 0) {
167 EmbeddedResource eres = new EmbeddedResource (
168 ReadString (mrRow.Name), mrRow.Flags);
170 BinaryReader br = m_ir.MetadataReader.GetDataReader (
171 m_img.CLIHeader.Resources.VirtualAddress);
172 br.BaseStream.Position += mrRow.Offset;
174 eres.Data = br.ReadBytes (br.ReadInt32 ());
176 resources.Add (eres);
180 switch (mrRow.Implementation.TokenType) {
181 case TokenType.File :
182 FileRow fRow = fTable [(int) mrRow.Implementation.RID - 1];
183 LinkedResource lres = new LinkedResource (
184 ReadString (mrRow.Name), mrRow.Flags,
185 ReadString (fRow.Name));
186 lres.Hash = ReadBlob (fRow.HashValue);
187 resources.Add (lres);
189 case TokenType.AssemblyRef :
190 AssemblyNameReference asm =
191 m_module.AssemblyReferences [(int) mrRow.Implementation.RID - 1];
192 AssemblyLinkedResource alr = new AssemblyLinkedResource (
193 ReadString (mrRow.Name),
201 public override void VisitModuleDefinitionCollection (ModuleDefinitionCollection modules)
203 ModuleTable mt = m_tableReader.GetModuleTable ();
204 if (mt == null || mt.Rows.Count != 1)
205 throw new ReflectionException ("Can not read main module");
207 ModuleRow mr = mt [0];
208 string name = ReadString (mr.Name);
209 ModuleDefinition main = new ModuleDefinition (name, m_asmDef, this, true);
210 main.Mvid = m_streams.GuidHeap [mr.Mvid];
211 main.MetadataToken = new MetadataToken (TokenType.Module, 1);
214 m_module.Accept (this);
216 FileTable ftable = m_tableReader.GetFileTable ();
217 if (ftable == null || ftable.Rows.Count == 0)
220 foreach (FileRow frow in ftable.Rows) {
221 if (frow.Flags != FileAttributes.ContainsMetaData)
224 name = ReadString (frow.Name);
225 FileInfo location = new FileInfo (
226 m_img.FileInformation != null ? Path.Combine (m_img.FileInformation.DirectoryName, name) : name);
227 if (!File.Exists (location.FullName))
228 throw new FileNotFoundException ("Module not found : " + name);
231 ImageReader module = ImageReader.Read (location.FullName);
232 mt = module.Image.MetadataRoot.Streams.TablesHeap [ModuleTable.RId] as ModuleTable;
233 if (mt == null || mt.Rows.Count != 1)
234 throw new ReflectionException ("Can not read module : " + name);
237 ModuleDefinition modext = new ModuleDefinition (name, m_asmDef,
238 new StructureReader (module, m_manifestOnly), false);
239 modext.Mvid = module.Image.MetadataRoot.Streams.GuidHeap [mr.Mvid];
241 modules.Add (modext);
242 modext.Accept (this);
243 } catch (ReflectionException) {
245 } catch (Exception e) {
246 throw new ReflectionException ("Can not read module : " + name, e);
251 public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
253 if (!m_tHeap.HasTable (ModuleRefTable.RId))
256 ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
257 for (int i = 0; i < mrTable.Rows.Count; i++) {
258 ModuleRefRow mrRow = mrTable [i];
259 ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
260 mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
265 public override void TerminateAssemblyDefinition (AssemblyDefinition asm)
270 foreach (ModuleDefinition mod in asm.Modules)
271 mod.Controller.Reader.VisitModuleDefinition (mod);