**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.PEToolkit / Image.cs
blobbbe34fba8013c48a585a542d49650e4113fee0a6
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
26 using System;
27 using System.IO;
28 using System.Collections;
29 using System.Runtime.InteropServices;
31 using Mono.PEToolkit.Metadata;
33 namespace Mono.PEToolkit {
35 public class Image : IDisposable {
37 internal DOSHeader dosHdr;
38 internal COFFHeader coffHdr;
39 internal PEHeader peHdr;
41 internal CorHeader corHdr;
43 internal Hashtable sections;
44 // File position right after PEHeader (NT Optional Header).
45 protected long sectionsPos;
47 private MetaDataRoot mdRoot;
49 private string name;
50 private bool open;
51 internal BinaryReader reader;
53 public Image(string name)
55 this.name = name;
56 open = false;
57 reader = null;
59 mdRoot = null;
61 dosHdr = new DOSHeader();
62 coffHdr = new COFFHeader();
63 peHdr = new PEHeader();
64 corHdr = new CorHeader();
66 sections = new Hashtable();
67 sectionsPos = -1;
70 ~Image()
72 Close();
76 public Hashtable Sections {
77 get {
78 return sections;
82 public void Open()
84 lock (this) if (!open) {
85 FileInfo pe = new FileInfo(name);
86 if (!pe.Exists) {
87 throw new Exception("Invalid file path.");
90 reader = new BinaryReader(new BufferedStream(pe.OpenRead()));
91 if (!reader.BaseStream.CanSeek) {
92 throw new Exception("Can't seek.");
95 open = true;
99 public void Close()
101 lock (this) if (open) {
102 reader.Close();
103 open = false;
107 // IDisposable
108 public void Dispose()
110 Close();
114 public bool IsCLI {
115 get {
116 return peHdr.IsCLIImage;
120 public MetaDataRoot MetadataRoot {
121 get {
122 return mdRoot;
126 /// <summary>
127 /// </summary>
128 public void ReadHeaders()
130 if (!open) {
131 throw new Exception("You must open image before trying to read it.");
134 dosHdr.Read(reader);
135 reader.BaseStream.Position = dosHdr.Lfanew;
136 ExeSignature peSig = (ExeSignature) reader.ReadUInt16();
137 if (peSig != ExeSignature.NT) {
138 throw new Exception ("Invalid image format: cannot find PE signature.");
140 peSig = (ExeSignature) reader.ReadUInt16();
141 if (peSig != ExeSignature.NT2) {
142 throw new Exception ("Invalid image format: cannot find PE signature.");
145 coffHdr.Read(reader);
146 peHdr.Read(reader);
148 sectionsPos = reader.BaseStream.Position;
149 ReadSections();
151 if (this.IsCLI) {
153 reader.BaseStream.Position = RVAToVA(peHdr.CLIHdrDir.virtAddr);
154 corHdr.Read (reader);
156 mdRoot = new MetaDataRoot(this);
157 reader.BaseStream.Position = RVAToVA(corHdr.MetaData.virtAddr);
158 mdRoot.Read(reader);
163 public void WriteHeaders (BinaryWriter writer)
165 dosHdr.Write (writer);
166 writer.BaseStream.Position = dosHdr.Lfanew;
167 writer.Write ((ushort)ExeSignature.NT);
168 writer.Write ((ushort)ExeSignature.NT2);
170 coffHdr.Write (writer);
171 peHdr.Write (writer);
173 WriteSections (writer);
175 if (this.IsCLI) {
177 writer.BaseStream.Position = RVAToVA (peHdr.CLIHdrDir.virtAddr);
178 corHdr.Write (writer);
180 long pos = RVAToVA (corHdr.MetaData.virtAddr);
181 writer.BaseStream.Position = pos;
182 mdRoot.Write (writer);
188 /// <summary>
189 /// </summary>
190 protected void ReadSections()
192 if (sectionsPos < 0) {
193 throw new Exception("Read headers first.");
195 reader.BaseStream.Position = sectionsPos;
197 int n = coffHdr.NumberOfSections;
198 for (int i = n; --i >=0;) {
199 Section sect = new Section();
200 sect.Read(reader);
201 sections [sect.Name] = sect;
205 protected void WriteSections (BinaryWriter writer)
207 foreach (Section section in sections.Values) {
208 section.Write (writer);
213 /// <summary>
214 /// </summary>
215 /// <param name="writer"></param>
216 public void Dump(TextWriter writer)
218 writer.WriteLine (
219 "COFF Header:" + Environment.NewLine +
220 coffHdr.ToString() + Environment.NewLine +
221 "PE Header:" + Environment.NewLine +
222 peHdr.ToString() + Environment.NewLine +
223 "Core Header:" + Environment.NewLine +
224 corHdr.ToString()
229 /// <summary>
230 /// </summary>
231 /// <returns></returns>
232 public override string ToString()
234 StringWriter sw = new StringWriter();
235 Dump(sw);
236 return sw.ToString();
240 /// <summary>
241 /// Returns name of the section for the given RVA.
242 /// </summary>
243 /// <param name="rva"></param>
244 /// <returns></returns>
245 public string RVAToSectionName(RVA rva)
247 string res = null;
248 foreach (Section s in Sections.Values) {
249 RVA sva = s.VirtualAddress;
250 if (rva >= sva && rva < sva + s.SizeOfRawData) {
251 res = s.Name;
252 break;
255 return res;
258 public long RVAToVA(RVA rva)
260 string sectName = RVAToSectionName(rva);
261 long res = 0;
262 if (sectName != null) {
263 Section s = (Section) Sections [sectName];
264 res = rva + (s.PointerToRawData - s.VirtualAddress);
266 return res;
269 public MetaDataRoot MetaDataRoot {
270 get {
271 return mdRoot;
275 public void DumpStreamHeader(TextWriter writer, string name)
277 if (mdRoot == null || name == null || name == String.Empty || writer == null) return;
278 writer.Write(name + " header: ");
279 MDStream s = MetaDataRoot.Streams[name] as MDStream;
280 if (s != null) {
281 writer.WriteLine();
282 writer.WriteLine(s);
283 } else {
284 writer.WriteLine("not present.");
285 writer.WriteLine();
289 public void DumpStreamHeader(string name)
291 DumpStreamHeader(Console.Out, name);