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