|
1 /* |
|
2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.Collections.Generic; |
|
20 using System.Text; |
|
21 using System.IO; |
|
22 using SymbianUtils; |
|
23 using SymbianUtils.Range; |
|
24 using SymbianUtils.Tracer; |
|
25 using SymbianUtils.FileTypes; |
|
26 using SymbianStructuresLib.Debug.Code; |
|
27 using SymbianStructuresLib.CodeSegments; |
|
28 using SymbianCodeLib.DbgEnginePlugin; |
|
29 using SymbianCodeLib.SourceManagement.Source; |
|
30 using SymbianCodeLib.QueryAPI; |
|
31 |
|
32 namespace SymbianCodeLib.Relocator |
|
33 { |
|
34 internal class CodeRelocator : DisposableObject, IEnumerable<CodeSourceAndCollection> |
|
35 { |
|
36 #region Constructors |
|
37 public CodeRelocator( CodePlugin aPlugin ) |
|
38 { |
|
39 iPlugin = aPlugin; |
|
40 } |
|
41 #endregion |
|
42 |
|
43 #region API |
|
44 public CodeCollection Activate( CodeSegDefinition aCodeSegment ) |
|
45 { |
|
46 CodeCollection ret = null; |
|
47 |
|
48 // Find the corresponding Code seg |
|
49 CodeSourceAndCollection pair = SourceManager[ aCodeSegment ]; |
|
50 if ( pair != null ) |
|
51 { |
|
52 CodeCollection col = pair.Collection; |
|
53 if ( col.IsFixed ) |
|
54 { |
|
55 // Cannot activate a fixed Code segment - TODO: should this return "true", i.e. already activated? |
|
56 } |
|
57 else |
|
58 { |
|
59 bool safe = CheckSafeToActivate( aCodeSegment ); |
|
60 if ( safe ) |
|
61 { |
|
62 // Deep copy the collection |
|
63 CodeCollection dupe = CodeCollection.NewCopy( iPlugin.ProvisioningManager.IdAllocator, col ); |
|
64 |
|
65 // Set new process-specific relocated base address. This causes the underlying code to be |
|
66 // decompressed/read if not already done so. |
|
67 dupe.Relocate( aCodeSegment.Base ); |
|
68 |
|
69 // At this point, the code owned by 'col' will have definitely been read (if available) |
|
70 // and that means col.IsCodeAvailable is probably true. However, dupe.IsCodeAvailable |
|
71 // is almost certainly false so we may need to copy the code over... |
|
72 if ( dupe.IsCodeAvailable == false && col.IsCodeAvailable == true ) |
|
73 { |
|
74 dupe.Code = col.Code; |
|
75 } |
|
76 System.Diagnostics.Debug.Assert( dupe.IsCodeAvailable == col.IsCodeAvailable ); |
|
77 |
|
78 // Save so that we can unload it later |
|
79 pair = new CodeSourceAndCollection( pair, dupe ); |
|
80 AddToActivationList( pair, aCodeSegment ); |
|
81 |
|
82 // We managed to activate a binary, so return the collection |
|
83 ret = dupe; |
|
84 |
|
85 iPlugin.Trace( "[C] ACTIVATE - {0}", aCodeSegment ); |
|
86 } |
|
87 } |
|
88 } |
|
89 // |
|
90 return ret; |
|
91 } |
|
92 |
|
93 public bool Deactivate( CodeSegDefinition aCodeSegment ) |
|
94 { |
|
95 bool activated = iActivationLUT.ContainsKey( aCodeSegment ); |
|
96 // |
|
97 if ( activated ) |
|
98 { |
|
99 CodeSourceAndCollection pair = iActivationLUT[ aCodeSegment ]; |
|
100 // |
|
101 iActivatedCollections.Remove( pair.Collection ); |
|
102 iActivationLUT.Remove( aCodeSegment ); |
|
103 // |
|
104 iPlugin.Trace( "[C] DEACTIVATE - {0} @ {1}", pair.Collection.FileName, aCodeSegment ); |
|
105 } |
|
106 // |
|
107 return activated; |
|
108 } |
|
109 #endregion |
|
110 |
|
111 #region Properties |
|
112 public CodeCollectionList CollectionList |
|
113 { |
|
114 get { return iActivatedCollections; } |
|
115 } |
|
116 |
|
117 public int Count |
|
118 { |
|
119 get |
|
120 { |
|
121 System.Diagnostics.Debug.Assert( iActivatedCollections.Count == iActivationLUT.Count ); |
|
122 return iActivationLUT.Count; |
|
123 } |
|
124 } |
|
125 #endregion |
|
126 |
|
127 #region Internal methods |
|
128 internal CodePlugin Plugin |
|
129 { |
|
130 get { return iPlugin; } |
|
131 } |
|
132 |
|
133 internal CodeSourceManager SourceManager |
|
134 { |
|
135 get { return Plugin.SourceManager; } |
|
136 } |
|
137 |
|
138 private void AddToActivationList( CodeSourceAndCollection aEntry, CodeSegDefinition aCodeSegment ) |
|
139 { |
|
140 lock ( iActivatedCollections ) |
|
141 { |
|
142 iActivatedCollections.Add( aEntry.Collection ); |
|
143 } |
|
144 lock ( iActivationLUT ) |
|
145 { |
|
146 iActivationLUT.Add( aCodeSegment, aEntry ); |
|
147 } |
|
148 } |
|
149 |
|
150 private bool CheckSafeToActivate( CodeSegDefinition aCodeSegment ) |
|
151 { |
|
152 lock ( iActivationLUT ) |
|
153 { |
|
154 bool alreadyExists = iActivationLUT.ContainsKey( aCodeSegment ); |
|
155 if ( alreadyExists ) |
|
156 { |
|
157 // Specified Code segment already activated |
|
158 return false; |
|
159 } |
|
160 else |
|
161 { |
|
162 // We must check that there's no overlap in activation ranges between Code segments. |
|
163 foreach ( KeyValuePair<CodeSegDefinition, CodeSourceAndCollection> kvp in iActivationLUT ) |
|
164 { |
|
165 AddressRange range = kvp.Key; |
|
166 if ( range.Contains( aCodeSegment ) ) |
|
167 { |
|
168 // Overlaps with existing activated Code segment |
|
169 return false; |
|
170 } |
|
171 } |
|
172 } |
|
173 // |
|
174 return true; |
|
175 } |
|
176 } |
|
177 #endregion |
|
178 |
|
179 #region From IEnumerable<CodeSourceAndCollection> |
|
180 public IEnumerator<CodeSourceAndCollection> GetEnumerator() |
|
181 { |
|
182 foreach ( KeyValuePair<CodeSegDefinition, CodeSourceAndCollection> kvp in iActivationLUT ) |
|
183 { |
|
184 yield return kvp.Value; |
|
185 } |
|
186 } |
|
187 |
|
188 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
189 { |
|
190 foreach ( KeyValuePair<CodeSegDefinition, CodeSourceAndCollection> kvp in iActivationLUT ) |
|
191 { |
|
192 yield return kvp.Value; |
|
193 } |
|
194 } |
|
195 #endregion |
|
196 |
|
197 #region From DisposableObject |
|
198 protected override void CleanupManagedResources() |
|
199 { |
|
200 try |
|
201 { |
|
202 base.CleanupManagedResources(); |
|
203 } |
|
204 finally |
|
205 { |
|
206 foreach ( CodeCollection col in iActivatedCollections ) |
|
207 { |
|
208 // Fixed collections are immediately discarded |
|
209 if ( !col.IsFixed ) |
|
210 { |
|
211 col.Dispose(); |
|
212 } |
|
213 } |
|
214 |
|
215 iActivatedCollections.Clear(); |
|
216 iActivationLUT.Clear(); |
|
217 } |
|
218 } |
|
219 #endregion |
|
220 |
|
221 #region Data members |
|
222 private readonly CodePlugin iPlugin; |
|
223 private CodeCollectionList iActivatedCollections = new CodeCollectionList(); |
|
224 private Dictionary<CodeSegDefinition, CodeSourceAndCollection> iActivationLUT = new Dictionary<CodeSegDefinition, CodeSourceAndCollection>(); |
|
225 #endregion |
|
226 } |
|
227 } |