|
1 /* |
|
2 * Copyright (c) 2009 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 using System; |
|
18 using System.Text; |
|
19 using System.IO; |
|
20 using System.Collections; |
|
21 using System.Collections.Generic; |
|
22 using System.Collections.Specialized; |
|
23 using SymbolLib.CodeSegDef; |
|
24 using SymbolLib.Generics; |
|
25 using SymbolLib.Engines; |
|
26 using SymbolLib.Sources.Map.File; |
|
27 using SymbolLib.Sources.Map.Parser; |
|
28 using SymbolLib.Sources.Map.Symbol; |
|
29 using SymbianUtils; |
|
30 using SymbianUtils.Range; |
|
31 using SymbianUtils.Tracer; |
|
32 |
|
33 namespace SymbolLib.Sources.Map.Engine |
|
34 { |
|
35 internal class MapFileEngine : GenericSymbolEngine, SymbolEntryCreator |
|
36 { |
|
37 #region Events |
|
38 public event AsyncReaderBase.Observer Observer; |
|
39 #endregion |
|
40 |
|
41 #region Constructor & destructor |
|
42 public MapFileEngine( ITracer aTracer ) |
|
43 : base( aTracer ) |
|
44 { |
|
45 } |
|
46 #endregion |
|
47 |
|
48 #region API |
|
49 public static bool IsMapFile( string aFileName ) |
|
50 { |
|
51 string extension = Path.GetExtension( aFileName ).ToLower(); |
|
52 return ( extension == CodeSegDefinition.KMapFileExtension ); |
|
53 } |
|
54 |
|
55 internal bool IsLoaded( CodeSegDefinition aDefinition ) |
|
56 { |
|
57 bool ret = iMapFile.HostBinaryFileName == aDefinition.ImageFileNameAndPath; |
|
58 return ret; |
|
59 } |
|
60 |
|
61 internal void LoadFromFile( string aMapFileName, TSynchronicity aSynchronicity ) |
|
62 { |
|
63 iMapFileName = aMapFileName; |
|
64 iMapFile = MapFile.NewByHostMapFileName( aMapFileName ); |
|
65 // |
|
66 iParser = new MapFileParser( this, aMapFileName, this ); |
|
67 iParser.Tag = this; |
|
68 iParser.iObserver += new SymbianUtils.AsyncReaderBase.Observer( Parser_Observer ); |
|
69 iParser.SymbolCreated += new MapFileParser.SymbolCreatedHandler( Parser_SymbolCreated ); |
|
70 iParser.BaseAddressHandler += new MapFileParser.MapFileBaseAddressHandler( Parser_BaseAddressHandler ); |
|
71 iParser.Read( aSynchronicity ); |
|
72 } |
|
73 |
|
74 internal void Load( CodeSegDefinition aDefinition ) |
|
75 { |
|
76 iMapFile.Fixup( aDefinition ); |
|
77 } |
|
78 |
|
79 internal bool Unload( CodeSegDefinition aDefinition ) |
|
80 { |
|
81 return true; |
|
82 } |
|
83 |
|
84 internal void UnloadAll() |
|
85 { |
|
86 } |
|
87 #endregion |
|
88 |
|
89 #region Properties |
|
90 public MapFile MapFile |
|
91 { |
|
92 get { return iMapFile; } |
|
93 } |
|
94 |
|
95 public string MapFileName |
|
96 { |
|
97 get { return iMapFileName; } |
|
98 } |
|
99 #endregion |
|
100 |
|
101 #region From GenericSymbolEngine |
|
102 public override void Reset() |
|
103 { |
|
104 // Do nothing - we cannot unload our only file. This is taken |
|
105 // care of by parent class. |
|
106 iMapFile = null; |
|
107 iParser = null; |
|
108 iMapFileName = string.Empty; |
|
109 } |
|
110 |
|
111 public override bool IsReady |
|
112 { |
|
113 get |
|
114 { |
|
115 return true; |
|
116 } |
|
117 } |
|
118 |
|
119 public override bool IsLoaded( string aFileName ) |
|
120 { |
|
121 bool ret = MapFileName == aFileName; |
|
122 return ret; |
|
123 } |
|
124 |
|
125 public override GenericSymbolCollection this[ int aIndex ] |
|
126 { |
|
127 get |
|
128 { |
|
129 return iMapFile; |
|
130 } |
|
131 } |
|
132 |
|
133 public override AddressRange Range |
|
134 { |
|
135 get |
|
136 { |
|
137 return iMapFile.AddressRange; |
|
138 } |
|
139 } |
|
140 |
|
141 internal override void UnloadUntagged() |
|
142 { |
|
143 |
|
144 } |
|
145 #endregion |
|
146 |
|
147 #region From IGenericSymbolCollectionStatisticsInterface |
|
148 public override int NumberOfCollections |
|
149 { |
|
150 get |
|
151 { |
|
152 return 1; |
|
153 } |
|
154 } |
|
155 #endregion |
|
156 |
|
157 #region From SymbolEntryCreator |
|
158 public MapSymbol CreateSymbol() |
|
159 { |
|
160 return MapSymbol.New( iMapFile ); |
|
161 } |
|
162 #endregion |
|
163 |
|
164 #region Map file parser observer |
|
165 private void Parser_Observer( SymbianUtils.AsyncReaderBase.TEvent aEvent, SymbianUtils.AsyncReaderBase aSender ) |
|
166 { |
|
167 if ( Observer != null ) |
|
168 { |
|
169 Observer( aEvent, aSender ); |
|
170 } |
|
171 |
|
172 if ( aEvent == SymbianUtils.AsyncReaderBase.TEvent.EReadingComplete ) |
|
173 { |
|
174 iMapFile.EnsureAllEntriesHaveSize(); |
|
175 iMapFile.Sort(); |
|
176 |
|
177 int x = 0; |
|
178 if ( x != 0 ) |
|
179 { |
|
180 iMapFile.Dump(); |
|
181 } |
|
182 |
|
183 iParser = null; |
|
184 } |
|
185 } |
|
186 |
|
187 private void Parser_SymbolCreated( MapSymbol aSymbol ) |
|
188 { |
|
189 bool addItem = true; |
|
190 |
|
191 if ( iMapFile.Count > 0 ) |
|
192 { |
|
193 int lastEntryIndex = iMapFile.Count - 1; |
|
194 MapSymbol lastEntry = (MapSymbol) iMapFile[ lastEntryIndex ]; |
|
195 // |
|
196 long lastEntrySize = lastEntry.Size; |
|
197 long newEntrySize = aSymbol.Size; |
|
198 // |
|
199 if ( lastEntry.Address == aSymbol.Address ) |
|
200 { |
|
201 if ( lastEntrySize == 0 && newEntrySize > 0 ) |
|
202 { |
|
203 // Remove an entry with zero size to replace it with a better definition... |
|
204 iMapFile.RemoveAt( lastEntryIndex ); |
|
205 } |
|
206 else if ( newEntrySize == 0 && lastEntrySize > 0 ) |
|
207 { |
|
208 // Don't replace an entry with zero size when we already |
|
209 // have a good item. |
|
210 addItem = false; |
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 if ( addItem ) |
|
216 { |
|
217 iMapFile.Add( this, aSymbol ); |
|
218 } |
|
219 } |
|
220 |
|
221 private void Parser_BaseAddressHandler( uint aBaseAddress ) |
|
222 { |
|
223 iMapFile.GlobalBaseAddress = aBaseAddress; |
|
224 } |
|
225 #endregion |
|
226 |
|
227 #region Internal methods |
|
228 #endregion |
|
229 |
|
230 #region Data members |
|
231 private MapFile iMapFile = null; |
|
232 private MapFileParser iParser = null; |
|
233 private string iMapFileName = string.Empty; |
|
234 #endregion |
|
235 } |
|
236 } |