Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Cecil/0.9.5/Mono.Cecil-0.9.5/Mono.Cecil/Mono.Cecil.PE/Image.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.2 KB
Line 
1//
2// Image.cs
3//
4// Author:
5//   Jb Evain (jbevain@gmail.com)
6//
7// Copyright (c) 2008 - 2011 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
29using System;
30
31using Mono;
32using Mono.Cecil.Cil;
33using Mono.Cecil.Metadata;
34
35using RVA = System.UInt32;
36
37namespace Mono.Cecil.PE {
38
39  sealed class Image {
40
41    public ModuleKind Kind;
42    public TargetRuntime Runtime;
43    public TargetArchitecture Architecture;
44    public string FileName;
45
46    public Section [] Sections;
47
48    public Section MetadataSection;
49
50    public uint EntryPointToken;
51    public ModuleAttributes Attributes;
52
53    public DataDirectory Debug;
54    public DataDirectory Resources;
55
56    public StringHeap StringHeap;
57    public BlobHeap BlobHeap;
58    public UserStringHeap UserStringHeap;
59    public GuidHeap GuidHeap;
60    public TableHeap TableHeap;
61
62    readonly int [] coded_index_sizes = new int [13];
63
64    readonly Func<Table, int> counter;
65
66    public Image ()
67    {
68      counter = GetTableLength;
69    }
70
71    public bool HasTable (Table table)
72    {
73      return GetTableLength (table) > 0;
74    }
75
76    public int GetTableLength (Table table)
77    {
78      return (int) TableHeap [table].Length;
79    }
80
81    public int GetTableIndexSize (Table table)
82    {
83      return GetTableLength (table) < 65536 ? 2 : 4;
84    }
85
86    public int GetCodedIndexSize (CodedIndex coded_index)
87    {
88      var index = (int) coded_index;
89      var size = coded_index_sizes [index];
90      if (size != 0)
91        return size;
92
93      return coded_index_sizes [index] = coded_index.GetSize (counter);
94    }
95
96    public uint ResolveVirtualAddress (RVA rva)
97    {
98      var section = GetSectionAtVirtualAddress (rva);
99      if (section == null)
100        throw new ArgumentOutOfRangeException ();
101
102      return ResolveVirtualAddressInSection (rva, section);
103    }
104
105    public uint ResolveVirtualAddressInSection (RVA rva, Section section)
106    {
107      return rva + section.PointerToRawData - section.VirtualAddress;
108    }
109
110    public Section GetSection (string name)
111    {
112      var sections = this.Sections;
113      for (int i = 0; i < sections.Length; i++) {
114        var section = sections [i];
115        if (section.Name == name)
116          return section;
117      }
118
119      return null;
120    }
121
122    public Section GetSectionAtVirtualAddress (RVA rva)
123    {
124      var sections = this.Sections;
125      for (int i = 0; i < sections.Length; i++) {
126        var section = sections [i];
127        if (rva >= section.VirtualAddress && rva < section.VirtualAddress + section.SizeOfRawData)
128          return section;
129      }
130
131      return null;
132    }
133
134    public ImageDebugDirectory GetDebugHeader (out byte [] header)
135    {
136      var section = GetSectionAtVirtualAddress (Debug.VirtualAddress);
137      var buffer = new ByteBuffer (section.Data);
138      buffer.position = (int) (Debug.VirtualAddress - section.VirtualAddress);
139
140      var directory = new ImageDebugDirectory {
141        Characteristics = buffer.ReadInt32 (),
142        TimeDateStamp = buffer.ReadInt32 (),
143        MajorVersion = buffer.ReadInt16 (),
144        MinorVersion = buffer.ReadInt16 (),
145        Type = buffer.ReadInt32 (),
146        SizeOfData = buffer.ReadInt32 (),
147        AddressOfRawData = buffer.ReadInt32 (),
148        PointerToRawData = buffer.ReadInt32 (),
149      };
150
151      buffer.position = (int) (directory.PointerToRawData - section.PointerToRawData);
152
153      header = new byte [directory.SizeOfData];
154      Buffer.BlockCopy (buffer.buffer, buffer.position, header, 0, header.Length);
155
156      return directory;
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.