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:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
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>
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
;
51 internal BinaryReader reader
;
53 public Image(string name
)
61 dosHdr
= new DOSHeader();
62 coffHdr
= new COFFHeader();
63 peHdr
= new PEHeader();
64 corHdr
= new CorHeader();
66 sections
= new Hashtable();
76 public Hashtable Sections
{
84 lock (this) if (!open
) {
85 FileInfo pe
= new FileInfo(name
);
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.");
101 lock (this) if (open
) {
108 public void Dispose()
116 return peHdr
.IsCLIImage
;
120 public MetaDataRoot MetadataRoot
{
128 public void ReadHeaders()
131 throw new Exception("You must open image before trying to read it.");
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
);
148 sectionsPos
= reader
.BaseStream
.Position
;
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
);
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
);
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
);
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();
201 sections
[sect
.Name
] = sect
;
205 protected void WriteSections (BinaryWriter writer
)
207 foreach (Section section
in sections
.Values
) {
208 section
.Write (writer
);
215 /// <param name="writer"></param>
216 public void Dump(TextWriter writer
)
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
+
231 /// <returns></returns>
232 public override string ToString()
234 StringWriter sw
= new StringWriter();
236 return sw
.ToString();
241 /// Returns name of the section for the given RVA.
243 /// <param name="rva"></param>
244 /// <returns></returns>
245 public string RVAToSectionName(RVA rva
)
248 foreach (Section s
in Sections
.Values
) {
249 RVA sva
= s
.VirtualAddress
;
250 if (rva
>= sva
&& rva
< sva
+ s
.SizeOfRawData
) {
258 public long RVAToVA(RVA rva
)
260 string sectName
= RVAToSectionName(rva
);
262 if (sectName
!= null) {
263 Section s
= (Section
) Sections
[sectName
];
264 res
= rva
+ (s
.PointerToRawData
- s
.VirtualAddress
);
269 public MetaDataRoot MetaDataRoot
{
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
;
284 writer
.WriteLine("not present.");
289 public void DumpStreamHeader(string name
)
291 DumpStreamHeader(Console
.Out
, name
);