6085cdb42b9e3c37cc27047db46fd1ad6e9214a7
[mono.git] / mcs / tools / cil-strip / Mono.Cecil / StructureReader.cs
1 //
2 // StructureReader.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 Jb Evain
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 namespace Mono.Cecil {
30
31         using System;
32         using System.IO;
33
34         using Mono.Cecil.Binary;
35         using Mono.Cecil.Metadata;
36
37         internal sealed class StructureReader : BaseStructureVisitor {
38
39                 ImageReader m_ir;
40                 Image m_img;
41                 bool m_manifestOnly;
42                 AssemblyDefinition m_asmDef;
43                 ModuleDefinition m_module;
44                 MetadataStreamCollection m_streams;
45                 TablesHeap m_tHeap;
46                 MetadataTableReader m_tableReader;
47
48                 public bool ManifestOnly {
49                         get { return m_manifestOnly; }
50                 }
51
52                 public ImageReader ImageReader {
53                         get { return m_ir; }
54                 }
55
56                 public Image Image {
57                         get { return m_img; }
58                 }
59
60                 public StructureReader (ImageReader ir)
61                 {
62                         if (ir.Image.CLIHeader == null)
63                                 throw new ImageFormatException ("The image is not a managed assembly");
64
65                         m_ir = ir;
66                         m_img = ir.Image;
67                         m_streams = m_img.MetadataRoot.Streams;
68                         m_tHeap = m_streams.TablesHeap;
69                         m_tableReader = ir.MetadataReader.TableReader;
70                 }
71
72                 public StructureReader (ImageReader ir, bool manifestOnly) : this (ir)
73                 {
74                         m_manifestOnly = manifestOnly;
75                 }
76
77                 byte [] ReadBlob (uint pointer)
78                 {
79                         if (pointer == 0)
80                                 return new byte [0];
81
82                         return m_streams.BlobHeap.Read (pointer);
83                 }
84
85                 string ReadString (uint pointer)
86                 {
87                         return m_streams.StringsHeap [pointer];
88                 }
89
90                 public override void VisitAssemblyDefinition (AssemblyDefinition asm)
91                 {
92                         if (!m_tHeap.HasTable (AssemblyTable.RId))
93                                 throw new ReflectionException ("No assembly manifest");
94
95                         asm.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
96                         m_asmDef = asm;
97
98                         switch (m_img.MetadataRoot.Header.Version) {
99                         case "v1.0.3705" :
100                                 asm.Runtime = TargetRuntime.NET_1_0;
101                                 break;
102                         case "v1.1.4322" :
103                                 asm.Runtime = TargetRuntime.NET_1_1;
104                                 break;
105                         case "v2.0.50727":
106                                 asm.Runtime = TargetRuntime.NET_2_0;
107                                 break;
108                         case "v4.0.30319" :
109                                 asm.Runtime = TargetRuntime.NET_4_0;
110                                 break;
111                         }
112
113                         if ((m_img.PEFileHeader.Characteristics & ImageCharacteristics.Dll) != 0)
114                                 asm.Kind = AssemblyKind.Dll;
115                         else if (m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsGui ||
116                                 m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsCeGui)
117                                 asm.Kind = AssemblyKind.Windows;
118                         else
119                                 asm.Kind = AssemblyKind.Console;
120                 }
121
122                 public override void VisitAssemblyNameDefinition (AssemblyNameDefinition name)
123                 {
124                         AssemblyTable atable = m_tableReader.GetAssemblyTable ();
125                         AssemblyRow arow = atable [0];
126                         name.Name = ReadString (arow.Name);
127                         name.Flags = arow.Flags;
128                         name.PublicKey = ReadBlob (arow.PublicKey);
129
130                         name.Culture = ReadString (arow.Culture);
131                         name.Version = new Version (
132                                 arow.MajorVersion, arow.MinorVersion,
133                                 arow.BuildNumber, arow.RevisionNumber);
134                         name.HashAlgorithm = arow.HashAlgId;
135                         name.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
136                 }
137
138                 public override void VisitAssemblyNameReferenceCollection (AssemblyNameReferenceCollection names)
139                 {
140                         if (!m_tHeap.HasTable (AssemblyRefTable.RId))
141                                 return;
142
143                         AssemblyRefTable arTable = m_tableReader.GetAssemblyRefTable ();
144                         for (int i = 0; i < arTable.Rows.Count; i++) {
145                                 AssemblyRefRow arRow = arTable [i];
146                                 AssemblyNameReference aname = new AssemblyNameReference (
147                                         ReadString (arRow.Name),
148                                         ReadString (arRow.Culture),
149                                         new Version (arRow.MajorVersion, arRow.MinorVersion,
150                                                                  arRow.BuildNumber, arRow.RevisionNumber));
151                                 aname.PublicKeyToken = ReadBlob (arRow.PublicKeyOrToken);
152                                 aname.Hash = ReadBlob (arRow.HashValue);
153                                 aname.Flags = arRow.Flags;
154                                 aname.MetadataToken = new MetadataToken (TokenType.AssemblyRef, (uint) i + 1);
155                                 names.Add (aname);
156                         }
157                 }
158
159                 public override void VisitResourceCollection (ResourceCollection resources)
160                 {
161                         if (!m_tHeap.HasTable (ManifestResourceTable.RId))
162                                 return;
163
164                         ManifestResourceTable mrTable = m_tableReader.GetManifestResourceTable ();
165                         FileTable fTable = m_tableReader.GetFileTable ();
166
167                         for (int i = 0; i < mrTable.Rows.Count; i++) {
168                                 ManifestResourceRow mrRow = mrTable [i];
169                                 if (mrRow.Implementation.RID == 0) {
170                                         EmbeddedResource eres = new EmbeddedResource (
171                                                 ReadString (mrRow.Name), mrRow.Flags);
172
173                                         BinaryReader br = m_ir.MetadataReader.GetDataReader (
174                                                 m_img.CLIHeader.Resources.VirtualAddress);
175                                         br.BaseStream.Position += mrRow.Offset;
176
177                                         eres.Data = br.ReadBytes (br.ReadInt32 ());
178
179                                         resources.Add (eres);
180                                         continue;
181                                 }
182
183                                 switch (mrRow.Implementation.TokenType) {
184                                 case TokenType.File :
185                                         FileRow fRow = fTable [(int) mrRow.Implementation.RID - 1];
186                                         LinkedResource lres = new LinkedResource (
187                                                 ReadString (mrRow.Name), mrRow.Flags,
188                                                 ReadString (fRow.Name));
189                                         lres.Hash = ReadBlob (fRow.HashValue);
190                                         resources.Add (lres);
191                                         break;
192                                 case TokenType.AssemblyRef :
193                                         AssemblyNameReference asm =
194                                                 m_module.AssemblyReferences [(int) mrRow.Implementation.RID - 1];
195                                         AssemblyLinkedResource alr = new AssemblyLinkedResource (
196                                                 ReadString (mrRow.Name),
197                                                 mrRow.Flags, asm);
198                                         resources.Add (alr);
199                                         break;
200                                 }
201                         }
202                 }
203
204                 public override void VisitModuleDefinitionCollection (ModuleDefinitionCollection modules)
205                 {
206                         ModuleTable mt = m_tableReader.GetModuleTable ();
207                         if (mt == null || mt.Rows.Count != 1)
208                                 throw new ReflectionException ("Can not read main module");
209
210                         ModuleRow mr = mt [0];
211                         string name = ReadString (mr.Name);
212                         ModuleDefinition main = new ModuleDefinition (name, m_asmDef, this, true);
213                         main.Mvid = m_streams.GuidHeap [mr.Mvid];
214                         main.MetadataToken = new MetadataToken (TokenType.Module, 1);
215                         modules.Add (main);
216                         m_module = main;
217                         m_module.Accept (this);
218
219                         FileTable ftable = m_tableReader.GetFileTable ();
220                         if (ftable == null || ftable.Rows.Count == 0)
221                                 return;
222
223                         foreach (FileRow frow in ftable.Rows) {
224                                 if (frow.Flags != FileAttributes.ContainsMetaData)
225                                         continue;
226
227                                 name = ReadString (frow.Name);
228                                 FileInfo location = new FileInfo (
229                                         m_img.FileInformation != null ? Path.Combine (m_img.FileInformation.DirectoryName, name) : name);
230                                 if (!File.Exists (location.FullName))
231                                         throw new FileNotFoundException ("Module not found : " + name);
232
233                                 try {
234                                         ImageReader module = ImageReader.Read (location.FullName);
235                                         mt = module.Image.MetadataRoot.Streams.TablesHeap [ModuleTable.RId] as ModuleTable;
236                                         if (mt == null || mt.Rows.Count != 1)
237                                                 throw new ReflectionException ("Can not read module : " + name);
238
239                                         mr = mt [0];
240                                         ModuleDefinition modext = new ModuleDefinition (name, m_asmDef,
241                                                 new StructureReader (module, m_manifestOnly), false);
242                                         modext.Mvid = module.Image.MetadataRoot.Streams.GuidHeap [mr.Mvid];
243
244                                         modules.Add (modext);
245                                         modext.Accept (this);
246                                 } catch (ReflectionException) {
247                                         throw;
248                                 } catch (Exception e) {
249                                         throw new ReflectionException ("Can not read module : " + name, e);
250                                 }
251                         }
252                 }
253
254                 public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
255                 {
256                         if (!m_tHeap.HasTable (ModuleRefTable.RId))
257                                 return;
258
259                         ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
260                         for (int i = 0; i < mrTable.Rows.Count; i++) {
261                                 ModuleRefRow mrRow = mrTable [i];
262                                 ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
263                                 mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
264                                 modules.Add (mod);
265                         }
266                 }
267
268                 public override void TerminateAssemblyDefinition (AssemblyDefinition asm)
269                 {
270                         if (m_manifestOnly)
271                                 return;
272
273                         foreach (ModuleDefinition mod in asm.Modules)
274                                 mod.Controller.Reader.VisitModuleDefinition (mod);
275                 }
276         }
277 }