5 // Jb Evain (jbevain@gmail.com)
7 // Copyright (c) 2008 - 2010 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.
33 using RVA = System.UInt32;
35 namespace Mono.Cecil.PE {
60 sealed class TextMap {
62 readonly Range [] map = new Range [16 /*Enum.GetValues (typeof (TextSegment)).Length*/];
64 public void AddMap (TextSegment segment, int length)
66 map [(int) segment] = new Range (GetStart (segment), (uint) length);
69 public void AddMap (TextSegment segment, int length, int align)
73 AddMap (segment, (length + align) & ~align);
76 public void AddMap (TextSegment segment, Range range)
78 map [(int) segment] = range;
81 public Range GetRange (TextSegment segment)
83 return map [(int) segment];
86 public DataDirectory GetDataDirectory (TextSegment segment)
88 var range = map [(int) segment];
90 return new DataDirectory (range.Length == 0 ? 0 : range.Start, range.Length);
93 public RVA GetRVA (TextSegment segment)
95 return map [(int) segment].Start;
98 public RVA GetNextRVA (TextSegment segment)
100 var i = (int) segment;
101 return map [i].Start + map [i].Length;
104 public int GetLength (TextSegment segment)
106 return (int) map [(int) segment].Length;
109 RVA GetStart (TextSegment segment)
111 var index = (int) segment;
112 return index == 0 ? ImageWriter.text_rva : ComputeStart (index);
115 RVA ComputeStart (int index)
118 return map [index].Start + map [index].Length;
121 public uint GetLength ()
123 var range = map [(int) TextSegment.StartupStub];
124 return range.Start - ImageWriter.text_rva + range.Length;