5 // Jb Evain (jbevain@gmail.com)
7 // (C) 2005 - 2007 Jb Evain
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
.Binary
{
34 using Mono
.Cecil
.Metadata
;
36 public sealed class Image
: IBinaryVisitable
{
38 DOSHeader m_dosHeader
;
39 PEFileHeader m_peFileHeader
;
40 PEOptionalHeader m_peOptionalHeader
;
42 SectionCollection m_sections
;
43 Section m_textSection
;
45 ImportAddressTable m_importAddressTable
;
46 CLIHeader m_cliHeader
;
47 ImportTable m_importTable
;
48 ImportLookupTable m_importLookupTable
;
49 HintNameTable m_hintNameTable
;
50 ExportTable m_exportTable
;
52 DebugHeader m_debugHeader
;
53 MetadataRoot m_mdRoot
;
55 ResourceDirectoryTable m_rsrcRoot
;
59 public DOSHeader DOSHeader
{
60 get { return m_dosHeader; }
63 public PEFileHeader PEFileHeader
{
64 get { return m_peFileHeader; }
67 public PEOptionalHeader PEOptionalHeader
{
68 get { return m_peOptionalHeader; }
71 public SectionCollection Sections
{
72 get { return m_sections; }
75 public Section TextSection
{
76 get { return m_textSection; }
77 set { m_textSection = value; }
80 public ImportAddressTable ImportAddressTable
{
81 get { return m_importAddressTable; }
84 public CLIHeader CLIHeader
{
85 get { return m_cliHeader; }
86 set { m_cliHeader = value; }
89 public DebugHeader DebugHeader
{
90 get { return m_debugHeader; }
91 set { m_debugHeader = value; }
94 public MetadataRoot MetadataRoot
{
95 get { return m_mdRoot; }
98 public ImportTable ImportTable
{
99 get { return m_importTable; }
102 public ImportLookupTable ImportLookupTable
{
103 get { return m_importLookupTable; }
106 public HintNameTable HintNameTable
{
107 get { return m_hintNameTable; }
110 public ExportTable ExportTable
{
111 get { return m_exportTable; }
112 set { m_exportTable = value; }
115 internal ResourceDirectoryTable ResourceDirectoryRoot
{
116 get { return m_rsrcRoot; }
117 set { m_rsrcRoot = value; }
120 public FileInfo FileInformation
{
121 get { return m_img; }
126 m_dosHeader
= new DOSHeader ();
127 m_peFileHeader
= new PEFileHeader ();
128 m_peOptionalHeader
= new PEOptionalHeader ();
129 m_sections
= new SectionCollection ();
130 m_importAddressTable
= new ImportAddressTable ();
131 m_importTable
= new ImportTable ();
132 m_importLookupTable
= new ImportLookupTable ();
133 m_hintNameTable
= new HintNameTable ();
134 m_mdRoot
= new MetadataRoot (this);
137 internal Image (FileInfo img
) : this ()
142 public long ResolveVirtualAddress (RVA rva
)
144 foreach (Section sect
in this.Sections
) {
145 if (rva
>= sect
.VirtualAddress
&&
146 rva
< sect
.VirtualAddress
+ sect
.SizeOfRawData
)
148 return rva
+ sect
.PointerToRawData
- sect
.VirtualAddress
;
151 throw new ArgumentOutOfRangeException ("Cannot map the rva to any section");
154 internal Section
GetSectionAtVirtualAddress (RVA rva
)
156 foreach (Section sect
in this.Sections
) {
157 if (rva
>= sect
.VirtualAddress
&&
158 rva
< sect
.VirtualAddress
+ sect
.SizeOfRawData
) {
165 public BinaryReader
GetReaderAtVirtualAddress (RVA rva
)
167 Section sect
= GetSectionAtVirtualAddress (rva
);
171 BinaryReader br
= new BinaryReader (new MemoryStream (sect
.Data
));
172 br
.BaseStream
.Position
= rva
- sect
.VirtualAddress
;
176 public void AddDebugHeader ()
178 m_debugHeader
= new DebugHeader ();
179 m_debugHeader
.SetDefaultValues ();
182 internal void SetFileInfo (FileInfo file
)
187 public void Accept (IBinaryVisitor visitor
)
189 visitor
.VisitImage (this);
191 m_dosHeader
.Accept (visitor
);
192 m_peFileHeader
.Accept (visitor
);
193 m_peOptionalHeader
.Accept (visitor
);
195 m_sections
.Accept (visitor
);
197 m_importAddressTable
.Accept (visitor
);
199 AcceptIfNotNull (m_cliHeader
, visitor
);
200 AcceptIfNotNull (m_debugHeader
, visitor
);
202 m_importTable
.Accept (visitor
);
203 m_importLookupTable
.Accept (visitor
);
204 m_hintNameTable
.Accept (visitor
);
205 AcceptIfNotNull (m_exportTable
, visitor
);
207 visitor
.TerminateImage (this);
210 static void AcceptIfNotNull (IBinaryVisitable visitable
, IBinaryVisitor visitor
)
212 if (visitable
== null)
215 visitable
.Accept (visitor
);
218 public static Image
CreateImage ()
220 Image img
= new Image ();
222 ImageInitializer init
= new ImageInitializer (img
);
228 public static Image
GetImage (string file
)
230 return ImageReader
.Read (file
).Image
;
233 public static Image
GetImage (byte [] image
)
235 return ImageReader
.Read (image
).Image
;
238 public static Image
GetImage (Stream stream
)
240 return ImageReader
.Read (stream
).Image
;