author | Chetan Kapoor <chetank@symbian.org> |
Tue, 04 May 2010 16:57:20 +0100 | |
changeset 104 | 466a0df5c15a |
parent 90 | 947f0dc9f7a8 |
child 148 | 31ea0f8e3c99 |
child 198 | 2bb754abd467 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 |
// All rights reserved. |
|
3 |
// This component and the accompanying materials are made available |
|
4 |
// under the terms of the License "Eclipse Public License v1.0" |
|
5 |
// which accompanies this distribution, and is available |
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 |
// |
|
8 |
// Initial Contributors: |
|
9 |
// Nokia Corporation - initial contribution. |
|
10 |
// |
|
11 |
// Contributors: |
|
12 |
// |
|
13 |
// Description: |
|
14 |
// f32\sfile\sf_lepoc.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include "sf_std.h" |
|
19 |
||
20 |
#include <e32std.h> |
|
21 |
#include <e32std_private.h> |
|
22 |
#include <e32base.h> |
|
23 |
#include <e32base_private.h> |
|
24 |
#include <e32math.h> |
|
25 |
#include <e32svr.h> |
|
26 |
#include <e32ver.h> |
|
27 |
#include <e32hal.h> |
|
28 |
#include <u32exec.h> |
|
29 |
#define INCLUDE_E32IMAGEHEADER_IMPLEMENTATION |
|
30 |
#include "sf_ldr.h" |
|
31 |
#include <f32image.h> |
|
32 |
#include "sf_image.h" |
|
33 |
#include <e32uid.h> |
|
34 |
#include <e32rom.h> |
|
35 |
#include "sf_cache.h" |
|
36 |
||
37 |
#include "sf_pgcompr.h" |
|
38 |
||
39 |
_LIT(KLitFinderInconsistent, "LDR-FINDER-INC"); |
|
40 |
_LIT(KLitSysBinError, "LDR-SYS\\BIN ERR"); |
|
41 |
_LIT8(KSysBin,":\\sys\\bin\\"); |
|
42 |
||
43 |
#ifdef _DEBUG |
|
44 |
||
45 |
enum TLdrEpocPanic |
|
46 |
{ |
|
47 |
EFuaiNoFixupTable = 0x10, |
|
48 |
EBcbmNotCodePaged = 0x20, |
|
49 |
ELfiCodePagingNotSupported = 0x30, |
|
50 |
EFprUnexpectedFixup = 0x40, |
|
51 |
}; |
|
52 |
||
53 |
static void Panic(TLdrEpocPanic aPanic) |
|
54 |
{ |
|
55 |
_LIT(KPanicCat, "LDR-PNC"); |
|
56 |
User::Panic(KPanicCat, aPanic); |
|
57 |
} |
|
58 |
||
59 |
extern TRequestStatus* ProcessDestructStatPtr; |
|
60 |
extern TBool ProcessCreated; |
|
61 |
||
62 |
#endif |
|
63 |
||
64 |
extern void DumpImageHeader(const E32ImageHeader*); |
|
65 |
extern TDriveCacheHeader* gDriveFileNamesCache[]; |
|
66 |
||
67 |
TBuf8<KMaxPath> gLoadeePath; |
|
68 |
TUint NextCodeSegId; |
|
69 |
||
70 |
const TInt KMaxHeaderSize = sizeof(E32ImageHeaderV) + 65536/8; |
|
71 |
||
72 |
||
73 |
#ifdef __X86__ |
|
74 |
extern TInt UseFloppy; |
|
75 |
#endif |
|
76 |
||
77 |
||
78 |
||
79 |
// -------- demand paging -------- |
|
80 |
||
81 |
/** Page size as a power of two. */ |
|
82 |
const TUint32 KPageSizeShift = 12; |
|
83 |
/** Page size, as defined for code relocations. This same page size is used for demand paging. */ |
|
84 |
const TUint32 KPageSize = 1<<KPageSizeShift; |
|
85 |
/** Apply this mask to an address to get the page offset. */ |
|
86 |
const TUint32 KPageOffsetMask = KPageSize - 1; |
|
87 |
||
88 |
/** |
|
89 |
Calculate the number of pages required to contain the supplied number of bytes. |
|
90 |
||
91 |
@param aSizeInBytes Size of are which has to be contained in whole blocks. |
|
92 |
@return Number of KPageSize pages required to contain area. |
|
93 |
*/ |
|
94 |
inline TInt SizeToPageCount(TInt aSizeInBytes) |
|
95 |
{ |
|
96 |
return (aSizeInBytes + KPageOffsetMask) >> KPageSizeShift; |
|
97 |
} |
|
98 |
||
99 |
||
100 |
/** |
|
101 |
Allocate a block which indexes the reallocations by page. This can be used for demand paging. |
|
102 |
||
103 |
@param aSection Pointer to relocation section to process. |
|
104 |
@param aAreaSize Size in bytes of area described by reloc section. |
|
105 |
@param aLoadAddress Address of relocation section in memory |
|
106 |
@param aProcessedBlock On success (return == KErrNone) this is set to the processed |
|
107 |
relocation section which is allocated on the current thread's heap. |
|
108 |
The caller takes ownership. The contents are undefined on failure. |
|
109 |
@return KErrNoMemory if could not allocate memory for processed block |
|
110 |
and auxiliary structures; KErrNone otherwise. |
|
111 |
*/ |
|
112 |
TInt E32Image::AllocateRelocationData(E32RelocSection* aSection, TUint32 aAreaSize, TUint32 aLoadAddress, TUint32*& aProcessedBlock) |
|
113 |
{ |
|
114 |
__IF_DEBUG(Printf("AllocateRelocationData")); |
|
115 |
||
116 |
TUint32 sectionSize = aSection->iSize; |
|
117 |
TUint32 numRelocs = aSection->iNumberOfRelocs; |
|
118 |
TInt pageCount = SizeToPageCount(aAreaSize); |
|
119 |
||
120 |
// The file format documentation (SOSI ch10) does not guarantee that each page has |
|
121 |
// relocation information, or that the pages are listed in order, so store them in |
|
122 |
// page order here. |
|
123 |
||
124 |
TUint8** subBlocks = (TUint8**)User::AllocZ(sizeof(TUint8*)*pageCount); |
|
125 |
if(subBlocks == 0) |
|
126 |
return KErrNoMemory; |
|
127 |
||
128 |
const TUint8* subBlockPtr = (TUint8*)(aSection+1); |
|
129 |
while(sectionSize > 0) |
|
130 |
{ |
|
131 |
TUint32 pageOffset = *(TUint32*)(subBlockPtr); |
|
132 |
TUint32 subBlockSize = *(TUint32*)(subBlockPtr+4); |
|
133 |
||
134 |
subBlocks[pageOffset >> KPageSizeShift] = (TUint8*)subBlockPtr; |
|
135 |
||
136 |
sectionSize -= subBlockSize; |
|
137 |
subBlockPtr += subBlockSize; // move to next sub-block |
|
138 |
} |
|
139 |
||
140 |
// now have each relocation page in memory, build lookup table |
|
141 |
TUint32 indexSize = (pageCount + 1) * sizeof(TUint32); // include sentinel |
|
142 |
TUint32 totalRelocations = numRelocs; |
|
143 |
iCodeRelocTableSize = indexSize + totalRelocations * sizeof(TUint16); |
|
144 |
TUint8* table = (TUint8*) User::Alloc(iCodeRelocTableSize); |
|
145 |
||
146 |
if(table == 0) |
|
147 |
{ |
|
148 |
User::Free(subBlocks); |
|
149 |
return KErrNoMemory; |
|
150 |
} |
|
151 |
||
152 |
// where sub-block positions are written to in the table |
|
153 |
TUint32* destSubBlock = (TUint32*)table; |
|
154 |
// where entries are written to in the table |
|
155 |
TUint16* destEntry = (TUint16*)(table + indexSize); |
|
156 |
||
157 |
TInt i; |
|
158 |
for(i = 0; i < pageCount; ++i) |
|
159 |
{ |
|
160 |
*destSubBlock++ = TUint32(destEntry) - TUint32(table); |
|
161 |
||
162 |
// see if a relocation page was defined for this page |
|
163 |
const TUint8* subBlock = subBlocks[i]; |
|
164 |
if(subBlock == 0) |
|
165 |
continue; |
|
166 |
||
167 |
// get number of entries in this sub-block, including padding |
|
168 |
TUint32 sbEntryCount; |
|
169 |
TUint32 pageOffset = *(TUint32*)subBlock; // offset of page from start of section |
|
170 |
sbEntryCount = *(TUint32*)(subBlock + 4); // sub-block size |
|
171 |
sbEntryCount -= 8; // exclude sub-block header |
|
172 |
sbEntryCount /= 2; // each entry is two bytes |
|
173 |
const TUint16* srcEntry = (TUint16*)(subBlock + 8); |
|
174 |
||
175 |
while(sbEntryCount--) |
|
176 |
{ |
|
177 |
TUint16 entry = *srcEntry++; |
|
178 |
if(entry==0) // ignore null padding values |
|
179 |
continue; |
|
180 |
||
181 |
// Replace inferred fixup type with actual fixup type |
|
182 |
TUint type = entry & 0xf000; |
|
183 |
if(type==KInferredRelocType) |
|
184 |
{ |
|
185 |
TUint32* ptr = (TUint32*)(aLoadAddress + pageOffset + (entry & 0x0fff)); |
|
186 |
TUint32 word = *ptr; |
|
187 |
type = (TUint(word - iHeader->iCodeBase) < TUint(iHeader->iCodeSize)) ? KTextRelocType : KDataRelocType; |
|
188 |
entry = (entry & 0x0fff) | type; |
|
189 |
} |
|
190 |
||
191 |
*destEntry++ = entry; |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
// sentinel entry marks the byte following last sub-block in table |
|
196 |
// This gives the size of the last processed sub-block. |
|
197 |
*destSubBlock = TUint32(destEntry) - TUint32(table); |
|
198 |
||
199 |
aProcessedBlock = (TUint32*) table; |
|
200 |
User::Free(subBlocks); |
|
201 |
||
202 |
#ifdef _DEBUG |
|
203 |
__IF_DEBUG(Printf("processed reloc table (size=%d,pageCount=%d)", iCodeRelocTableSize, pageCount)); |
|
204 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
// Dump the processed reloc table if loader tracing enabled. The dump is in |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
206 |
// two parts; first, the page indexes (1 word per page), then the entries |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
207 |
// describing the items to be relocated on each of these pages, formatted |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
// with up to 8 entries per line but starting a new line for each page. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
// Each of these entries has the relocation type in the first nibble, and |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
// the offset within the page in the remaining 3 nibbles. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
const TUint32* table32 = (const TUint32*)table; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
for (i = 0; i <= pageCount; ++i) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
__IF_DEBUG(Printf("%04x: %08x", i*4, table32[i])); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
for (i = 0; i < pageCount; ++i) |
0 | 216 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
TUint start = table32[i]; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
TInt nbytes = table32[i+1] - start; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
while (nbytes) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
TBuf8<0x100> buf; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
buf.Format(_L8("%04x:"), start); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
const TUint16* p = (const TUint16*)(table+start); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
TInt n = nbytes <= 16 ? nbytes : 16; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
for (nbytes -= n, start += n; n > 0; n -= 2) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
buf.AppendFormat(_L8(" %04x"), *p++); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
buf.AppendFormat(_L8("\r\n")); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
__IF_DEBUG(RawPrint(buf)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
} |
0 | 232 |
} |
233 |
#endif |
|
234 |
return KErrNone; |
|
235 |
} |
|
236 |
||
237 |
||
238 |
/******************************************************************************* |
|
239 |
* These functions run in supervisor mode since they require access to the |
|
240 |
* chunks of the newly-created process or DLL while they are still in the |
|
241 |
* home section. |
|
242 |
******************************************************************************/ |
|
243 |
||
244 |
/** |
|
245 |
Vector which ::ExecuteInSupervisorMode invokes. |
|
246 |
*/ |
|
247 |
TInt (*ExecuteInSupervisorModeVector)(TSupervisorFunction, TAny*); |
|
248 |
||
249 |
/** |
|
250 |
Executute aFunction in supervisor mode (if the memory model requires this.) |
|
251 |
*/ |
|
252 |
TInt ExecuteInSupervisorMode(TSupervisorFunction aFunction, TAny* aParameter) |
|
253 |
{ |
|
254 |
return(*ExecuteInSupervisorModeVector)(aFunction, aParameter); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
Implementation of ::ExecuteInSupervisorMode which actually executes the |
|
259 |
function in user mode. |
|
260 |
*/ |
|
261 |
TInt UserModeExecuteInSupervisorMode(TSupervisorFunction aFunction, TAny* aParameter) |
|
262 |
{ |
|
263 |
return (*aFunction)(aParameter); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
Decide whether any Loader code actually needs to execute in supervisor mode |
|
268 |
and set ::ExecuteInSupervisorModeVector so that invocations of ::ExecuteInSupervisorMode |
|
269 |
call the appropriate function. |
|
270 |
*/ |
|
271 |
void InitExecuteInSupervisorMode() |
|
272 |
{ |
|
273 |
// work out if we need to really 'execute in supervisor mode'... |
|
274 |
TUint32 memModelAttrs = (TUint32)UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL); |
|
275 |
TUint32 memModel = memModelAttrs & EMemModelTypeMask; |
|
276 |
if(memModel==EMemModelTypeFlexible) |
|
277 |
{ |
|
278 |
// we can do everything user side... |
|
279 |
ExecuteInSupervisorModeVector = UserModeExecuteInSupervisorMode; |
|
280 |
gExecutesInSupervisorMode = EFalse; |
|
281 |
} |
|
282 |
else |
|
283 |
{ |
|
284 |
// we need to go kernel side... |
|
285 |
ExecuteInSupervisorModeVector = UserSvr::ExecuteInSupervisorMode; |
|
286 |
gExecutesInSupervisorMode = ETrue; |
|
287 |
} |
|
288 |
} |
|
289 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
290 |
// A version that will work in user or supervisor mode |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
void MyPrintf(const char* aFmt, ...) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
VA_LIST list; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
VA_START(list, aFmt); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
TPtrC8 fmt((const TText8*)aFmt); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
TBuf8<0x100> buf; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
buf.AppendFormatList(fmt, list); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
buf.AppendFormat(_L8("\r\n")); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
RDebug::RawPrint(buf); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
VA_END(list); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
301 |
} |
0 | 302 |
|
303 |
/** |
|
304 |
Arguments for svRelocateSection. |
|
305 |
||
306 |
The relocation information (at iRelocsBuf) has list sub blocks, each referring to a 4kB |
|
307 |
page within the section. See E32RelocBlock. |
|
308 |
*/ |
|
309 |
struct SRelocateSectionInfo |
|
310 |
{ |
|
311 |
E32Image* iImage; ///< The executable being relocated. |
|
312 |
TUint8* iRelocsBuf; ///< Pointer to relocation info. |
|
313 |
TUint32 iNumRelocs; ///< Total number of relocations to apply. |
|
314 |
TUint32 iLoadAddress; ///< Virtual address where section is currently located in memory. |
|
315 |
}; |
|
316 |
||
317 |
/** |
|
318 |
Apply relocations to a code or data section. |
|
319 |
||
320 |
@param aPtr Pointer to SRelocateSectionInfo. |
|
321 |
*/ |
|
322 |
TInt svRelocateSection(TAny* aPtr) |
|
323 |
{ |
|
324 |
SRelocateSectionInfo& info=*(SRelocateSectionInfo*)aPtr; |
|
325 |
||
326 |
E32Image& img = *(E32Image*)info.iImage; |
|
327 |
TUint8* relocs = info.iRelocsBuf; |
|
328 |
TUint32 numRelocs = info.iNumRelocs; |
|
329 |
TUint32 loadAddress = info.iLoadAddress; |
|
330 |
||
331 |
TUint32 codeStart = img.iHeader->iCodeBase; |
|
332 |
TUint32 codeFinish = codeStart+img.iHeader->iCodeSize; |
|
333 |
TUint32 codeDelta = img.iCodeDelta; |
|
334 |
TUint32 dataDelta = img.iDataDelta; |
|
335 |
||
336 |
while(numRelocs>0) |
|
337 |
{ |
|
338 |
TUint32 pageAddress = ((TUint32*)relocs)[0]; |
|
339 |
TUint32 pageSize = ((TUint32*)relocs)[1]; |
|
340 |
TUint8* relocsEnd = relocs+pageSize; |
|
341 |
relocs += 8; |
|
342 |
||
343 |
while(relocs<relocsEnd) |
|
344 |
{ |
|
345 |
TUint16 relocOffset = *(TUint16*)relocs; |
|
346 |
relocs += 2; |
|
347 |
if(!relocOffset) |
|
348 |
continue; |
|
349 |
||
350 |
TUint32 offset = pageAddress+(TUint32)(relocOffset&0x0fff); |
|
351 |
TUint32* destPtr = (TUint32*)(loadAddress+offset); |
|
352 |
TUint16 relocType = relocOffset&0xf000; |
|
353 |
||
354 |
TUint32 relocAddr = *destPtr; |
|
355 |
if(relocType==KTextRelocType) |
|
356 |
relocAddr += codeDelta; // points to text/rdata section |
|
357 |
else if(relocType==KDataRelocType) |
|
358 |
relocAddr += dataDelta; // points to data section |
|
359 |
else if (relocAddr>=codeStart && relocAddr<codeFinish) |
|
360 |
relocAddr += codeDelta; // points to text/rdata section |
|
361 |
else |
|
362 |
relocAddr += dataDelta; // points to data section |
|
363 |
*destPtr = relocAddr; |
|
364 |
||
365 |
--numRelocs; |
|
366 |
} |
|
367 |
} |
|
368 |
return 0; |
|
369 |
} |
|
370 |
||
371 |
||
372 |
/** |
|
373 |
Fix up the export directory |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
374 |
Only performed on PE images. ELF image's exports are marked as relocatable |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
375 |
and therefore relocated by svRelocateSection along with the text section |
0 | 376 |
*/ |
377 |
TInt svRelocateExports(TAny* aPtr) |
|
378 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
379 |
E32Image& exporter = *(E32Image*)aPtr; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
380 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
381 |
// Dump everything potentially useful that we know about the exporter ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
382 |
__LDRTRACE(MyPrintf("RelocateExports: paged? %d, iRomImageHeader@%08x, iHeader@%08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
383 |
exporter.iUseCodePaging, exporter.iRomImageHeader, exporter.iHeader)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
384 |
__LDRTRACE(MyPrintf(" iCodeLoadAddress %08x, iCodeRunAddress %08x, iCodeSize %x iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
385 |
exporter.iCodeLoadAddress, exporter.iCodeRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
386 |
exporter.iCodeSize, exporter.iTextSize)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
387 |
__LDRTRACE(MyPrintf(" iDataLoadAddress %08x, iDataRunAddress %08x, iDataSize %x iBssSize %x iTotalDataSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
388 |
exporter.iDataLoadAddress, exporter.iDataRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
389 |
exporter.iDataSize, exporter.iBssSize, exporter.iTotalDataSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
390 |
__LDRTRACE(MyPrintf(" iCodeDelta, %x iDataDelta %x, iExportDirEntryDelta %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
391 |
exporter.iCodeDelta, exporter.iDataDelta, exporter.iExportDirEntryDelta)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
392 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
393 |
// It turns out that very little of the exporter info is useful! For |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
// example, the required code and data deltas are NOT those provided |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
// by the exporter, nor are the load addresses relevant ... :( |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
396 |
// |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
397 |
// In the case of a PE-derived image, the entries in the export table |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
398 |
// are expressed in terms of offsets into the image file, rather than |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
399 |
// locations in memory. Each therefore needs to be relocated by the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
400 |
// difference between its file offset and its run address. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
// |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
402 |
// It is assumed that the code segment appears before the data segment |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
403 |
// in the file; therefore, export table entries with values between 0 |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
// and (exporter.iCodeSize) refer to the text segment, while higher |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
// values represent references to data addresses. Since the run addresses |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
406 |
// of code and data segments may be different, each type of export must |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
407 |
// be relocated with respect to the correct section. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
408 |
// |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
409 |
// The following express the start and finish of each section in terms of |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
// file offsets and then derive the required adjustments to the entries |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
411 |
// in the export table ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
412 |
TUint32 codeStart = 0; // compiler whinges if this is 'const' :( |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
413 |
const TUint32 codeFinish = codeStart + exporter.iCodeSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
414 |
const TUint32 dataStart = codeFinish; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
415 |
const TUint32 dataFinish = dataStart + exporter.iTotalDataSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
416 |
const TUint32 codeDelta = exporter.iCodeRunAddress - codeStart; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
417 |
const TUint32 dataDelta = exporter.iDataRunAddress - dataStart; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
418 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
419 |
TUint32* destExport = (TUint32*)exporter.iExportDirLoad; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
420 |
for (TInt i = exporter.iExportDirCount; --i >= 0; ) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
421 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
422 |
TUint32 relocAddr = *destExport; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
423 |
TUint32 newValue; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
424 |
if (relocAddr >= codeStart && relocAddr < codeFinish) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
newValue = relocAddr + codeDelta; // points to text/rdata section |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
else if (relocAddr >= dataStart && relocAddr < dataFinish) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
427 |
newValue = relocAddr + dataDelta; // points to data/bss section |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
428 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
429 |
newValue = relocAddr; // unknown - just leave it alone |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
430 |
*destExport++ = newValue; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
431 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
432 |
__LDRTRACE(MyPrintf("RelocateExports: export %d %08x => %08x %c", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
433 |
exporter.iExportDirCount-i, relocAddr, newValue, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
434 |
(relocAddr >= codeStart && relocAddr < codeFinish) ? 'C' : |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
435 |
(relocAddr >= dataStart && relocAddr < dataFinish) ? 'D' : 'X')); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
436 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
437 |
|
0 | 438 |
return 0; |
439 |
} |
|
440 |
||
441 |
||
442 |
struct SFixupImportAddressesInfo |
|
443 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
444 |
TUint32* iIat; // Next part of IAT to be fixed up |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
445 |
E32Image* iExporter; // Module from which we're importing |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
446 |
TInt iNumImports; // Number of imports from this exporter |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
447 |
|
0 | 448 |
/** |
449 |
For demand paging, this points to the buffer which is populated |
|
450 |
so each page can be fixed up as it is loaded in. |
|
451 |
*/ |
|
452 |
TUint64* iFixup64; |
|
453 |
// For ElfDerived... |
|
454 |
TUint32 iCodeLoadAddress; |
|
455 |
TUint32* iImportOffsetList; |
|
456 |
}; |
|
457 |
||
458 |
||
459 |
/** |
|
460 |
Fix up the import address table, used for 'PE derived' executables. |
|
461 |
@param aPtr Pointer to function arguments (SFixupImportAddressesInfo structure). |
|
462 |
SFixupImportAddressesInfo::iIat is updated by this function. |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
463 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
464 |
For a given importer, this function will be called once for each image from which |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
465 |
objects are imported, and each time it will update the relevant portion of the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
466 |
importer's IAT, until all imports from all exporters have been processed. |
0 | 467 |
*/ |
468 |
TInt svFixupImportAddresses(TAny* aPtr) |
|
469 |
{ |
|
470 |
SFixupImportAddressesInfo& info = *(SFixupImportAddressesInfo*)aPtr; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
471 |
E32Image& exporter = *info.iExporter; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
472 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
#ifdef _DEBUG |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
474 |
__LDRTRACE(MyPrintf(">svFixupImportAddresses %d imports, code@%08x, fixup@%08x exporter@%08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
475 |
info.iNumImports, info.iCodeLoadAddress, info.iFixup64, info.iExporter)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
476 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
477 |
// Dump everything potentially useful that we know about the exporter ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
478 |
__LDRTRACE(MyPrintf("%S: paged? %d, iRomImageHeader@%08x, iHeader@%08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
479 |
&exporter.iFileName, exporter.iUseCodePaging, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
480 |
exporter.iRomImageHeader, exporter.iHeader)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
481 |
__LDRTRACE(MyPrintf("iCodeLoadAddress %08x, iCodeRunAddress %08x, iCodeSize %x iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
482 |
exporter.iCodeLoadAddress, exporter.iCodeRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
483 |
exporter.iCodeSize, exporter.iTextSize)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
484 |
__LDRTRACE(MyPrintf("iDataLoadAddress %08x, iDataRunAddress %08x, iDataSize %x iBssSize %x iTotalDataSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
485 |
exporter.iDataLoadAddress, exporter.iDataRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
486 |
exporter.iDataSize, exporter.iBssSize, exporter.iTotalDataSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
487 |
__LDRTRACE(MyPrintf("iCodeDelta, %x iDataDelta %x, iExportDirEntryDelta %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
488 |
exporter.iCodeDelta, exporter.iDataDelta, exporter.iExportDirEntryDelta)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
489 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
490 |
if (exporter.iRomImageHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
491 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
492 |
const TRomImageHeader& rh = *exporter.iRomImageHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
493 |
__LDRTRACE(MyPrintf("ROM: iCodeAddress %08x, iCodeSize %x, iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
494 |
rh.iCodeAddress, rh.iCodeSize, rh.iTextSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
495 |
__LDRTRACE(MyPrintf("ROM: iDataAddress %08x, iDataSize %x, iBssSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
496 |
rh.iDataAddress, rh.iDataSize, rh.iBssSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
497 |
__LDRTRACE(MyPrintf("ROM: iDataBssLinearBase %08x, iTotalDataSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
498 |
rh.iDataBssLinearBase, rh.iTotalDataSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
499 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
500 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
501 |
if (exporter.iHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
502 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
503 |
const E32ImageHeader& ih = *exporter.iHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
504 |
__LDRTRACE(MyPrintf("HEAD: iCodeBase %08x, iCodeSize %x, iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
505 |
ih.iCodeBase, ih.iCodeSize, ih.iTextSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
506 |
__LDRTRACE(MyPrintf("HEAD: iDataBase %08x, iDataSize %x, iBssSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
ih.iDataBase, ih.iDataSize, ih.iBssSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
509 |
#endif // _DEBUG |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
510 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
// 'exportDir' points to the address of the 0th ordinal (symbol name data); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
// ordinary exports start from ordinal 1 |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
const TUint32* const exportDir = (TUint32*)exporter.iExportDirLoad - KOrdinalBase; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
const TUint32 maxOrdinal = (TUint32)exporter.iExportDirCount; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
const TUint32 absentOrdinal = (TUint32)exporter.iFileEntryPoint; |
0 | 516 |
|
517 |
TUint32* iat = info.iIat; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
TUint32* const iatEnd = iat + info.iNumImports; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
519 |
for (; iat < iatEnd; ++iat) |
0 | 520 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
521 |
// Each IAT slot contains the ordinal number of the export to be imported from |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
522 |
// the exporter. We use that index to locate the address of the export itself. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
523 |
TUint32 ordinal = *iat; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
524 |
if (ordinal > maxOrdinal) |
0 | 525 |
return KErrNotSupported; |
526 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
527 |
// If the import number is 0 (symbol name data), and the exporter doesn't provide |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
528 |
// this, we don't regard it as an error; we just skip this block, leaving the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
529 |
// address set to 0. For all other valid cases, we index the export directory to |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
530 |
// find the exported object's address (which has already been relocated) ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
531 |
TUint32 newValue = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
532 |
if (ordinal > 0 || (exporter.iAttr & ECodeSegAttNmdExpData)) |
0 | 533 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
534 |
TUint32 expAddr = exportDir[ordinal]; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
535 |
if (expAddr == 0 || expAddr == absentOrdinal) |
0 | 536 |
return KErrNotSupported; |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
// The new value is just the address of the export, no adjustment needed |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
538 |
newValue = expAddr; |
0 | 539 |
} |
540 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
541 |
__LDRTRACE(MyPrintf("svFixupImportAddresses: import[%d]@%08x is export[%d] == %08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
542 |
iat - info.iIat, iat, ordinal, newValue)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
543 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
544 |
// In non-paged code, we can simply replace the ordinals in the IAT with the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
545 |
// object addresses to which they refer once and for all. However, in a code |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
546 |
// paging system, the IAT may be thrown away and later reloaded from the code |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
547 |
// image; therefore, we need to save the updates in the buffer pointed to by |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
548 |
// 'iFixup64' so that they can be reapplied each time the code page(s) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
549 |
// containing (parts of the) IAT are reloaded. The fixup entries are in the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
550 |
// form of 64-bit words, with the 32-bit address-to-be-fixed-up in the upper |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
551 |
// half and the value-to-be-stored-there in the lower half -- the multiple |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
552 |
// casts are needed to stop some compilers whinging about converting a |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
553 |
// pointer to a 64-bit integral type :( |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
554 |
if (!info.iFixup64) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
555 |
*iat = newValue; |
0 | 556 |
else |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
557 |
*info.iFixup64++ = ((TUint64)(TUintPtr)iat << 32) | newValue; |
0 | 558 |
} |
559 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
560 |
// Finally, update 'info.iIat' to show which imports have been processed |
0 | 561 |
info.iIat = iat; |
562 |
return KErrNone; |
|
563 |
} |
|
564 |
||
565 |
||
566 |
/** |
|
567 |
Fix up the import addresses, used for 'elf derived' executables. |
|
568 |
@param aPtr Pointer to function arguments (SFixupImportAddressesInfo structure). |
|
569 |
*/ |
|
570 |
TInt svElfDerivedFixupImportAddresses(TAny* aPtr) |
|
571 |
{ |
|
572 |
SFixupImportAddressesInfo& info = *(SFixupImportAddressesInfo*)aPtr; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
573 |
E32Image& exporter = *info.iExporter; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
574 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
575 |
#ifdef _DEBUG |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
576 |
__LDRTRACE(MyPrintf(">svElfDerivedFixupImportAddresses %d imports, code@%08x, fixup@%08x exporter@%08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
577 |
info.iNumImports, info.iCodeLoadAddress, info.iFixup64, info.iExporter)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
578 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
579 |
// Dump everything potentially useful that we know about the exporter ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
580 |
__LDRTRACE(MyPrintf("%S: paged? %d, iRomImageHeader@%08x, iHeader@%08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
581 |
&exporter.iFileName, exporter.iUseCodePaging, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
exporter.iRomImageHeader, exporter.iHeader)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
583 |
__LDRTRACE(MyPrintf("iCodeLoadAddress %08x, iCodeRunAddress %08x, iCodeSize %x iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
584 |
exporter.iCodeLoadAddress, exporter.iCodeRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
exporter.iCodeSize, exporter.iTextSize)) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
586 |
__LDRTRACE(MyPrintf("iDataLoadAddress %08x, iDataRunAddress %08x, iDataSize %x iBssSize %x iTotalDataSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
587 |
exporter.iDataLoadAddress, exporter.iDataRunAddress, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
588 |
exporter.iDataSize, exporter.iBssSize, exporter.iTotalDataSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
589 |
__LDRTRACE(MyPrintf("iCodeDelta, %x iDataDelta %x, iExportDirEntryDelta %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
590 |
exporter.iCodeDelta, exporter.iDataDelta, exporter.iExportDirEntryDelta)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
591 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
592 |
if (exporter.iRomImageHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
593 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
594 |
const TRomImageHeader& rh = *exporter.iRomImageHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
595 |
__LDRTRACE(MyPrintf("ROM: iCodeAddress %08x, iCodeSize %x, iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
596 |
rh.iCodeAddress, rh.iCodeSize, rh.iTextSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
597 |
__LDRTRACE(MyPrintf("ROM: iDataAddress %08x, iDataSize %x, iBssSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
598 |
rh.iDataAddress, rh.iDataSize, rh.iBssSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
599 |
__LDRTRACE(MyPrintf("ROM: iDataBssLinearBase %08x, iTotalDataSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
600 |
rh.iDataBssLinearBase, rh.iTotalDataSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
601 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
602 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
603 |
if (exporter.iHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
604 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
605 |
const E32ImageHeader& ih = *exporter.iHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
606 |
__LDRTRACE(MyPrintf("HEAD: iCodeBase %08x, iCodeSize %x, iTextSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
607 |
ih.iCodeBase, ih.iCodeSize, ih.iTextSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
608 |
__LDRTRACE(MyPrintf("HEAD: iDataBase %08x, iDataSize %x, iBssSize %x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
609 |
ih.iDataBase, ih.iDataSize, ih.iBssSize)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
610 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
611 |
#endif // _DEBUG |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
612 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
613 |
// Here we calculate the bounds of each section of the exporter, as |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
614 |
// code and data exports may have to be offset by different amounts. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
615 |
// Unfortunately, the required information seems to be in several |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
616 |
// different places, depending on whether the code is ROM or RAM, etc |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
617 |
TUint32 codeStart = exporter.iCodeRunAddress; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
618 |
TUint32 codeEnd = codeStart + exporter.iCodeSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
619 |
TUint32 dataStart = exporter.iDataRunAddress; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
620 |
TUint32 dataEnd = dataStart + exporter.iTotalDataSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
621 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
622 |
if (exporter.iRomImageHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
623 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
624 |
const TRomImageHeader& rh = *exporter.iRomImageHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
625 |
codeStart = rh.iCodeAddress; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
626 |
codeEnd = codeStart + rh.iCodeSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
627 |
dataStart = rh.iDataBssLinearBase; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
628 |
dataEnd = dataStart + rh.iTotalDataSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
629 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
630 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
631 |
if (exporter.iHeader) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
632 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
633 |
const E32ImageHeader& ih = *exporter.iHeader; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
634 |
codeStart = ih.iCodeBase; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
635 |
codeEnd = codeStart + ih.iCodeSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
636 |
dataStart = ih.iDataBase; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
637 |
dataEnd = dataStart + ih.iDataSize + ih.iBssSize; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
638 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
639 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
640 |
// 'exportDir' points to the address of the 0th ordinal (symbol name data); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
641 |
// ordinary exports start from ordinal 1 |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
642 |
const TUint32* const exportDir = (TUint32*)exporter.iExportDirLoad - KOrdinalBase; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
643 |
const TUint32 maxOrdinal = (TUint32)exporter.iExportDirCount; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
644 |
const TUint32 absentOrdinal = (TUint32)exporter.iFileEntryPoint; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
645 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
646 |
const TUint32 codeDelta = exporter.iCodeDelta; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
647 |
const TUint32 dataDelta = exporter.iDataDelta; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
648 |
const TUint32 dirDelta = exporter.iExportDirEntryDelta; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
649 |
TUint8* const codeBase = (TUint8*)info.iCodeLoadAddress; |
0 | 650 |
|
651 |
TUint32* iol = info.iImportOffsetList; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
652 |
TUint32* const iolEnd = iol + info.iNumImports; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
653 |
for(; iol < iolEnd; ++iol) |
0 | 654 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
655 |
// Whereas the PE format's IAT contains ordinals to be imported, the ELF IOL |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
656 |
// (Import Offset List) is a list of offsets (within the importer's code) of |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
657 |
// the locations that contain references to imported objects. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
658 |
// |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
659 |
// At the start of this process, each such location contains a composite value, |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
660 |
// of which the low 16 bits indicate the ordinal to be imported from the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
661 |
// exporter's directory, and the upper 16 provide an optional adjustment to |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
662 |
// be added to the imported value. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
663 |
// |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
664 |
// This composite value has to be replaced by the actual address of the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
665 |
// object being imported (plus the adjustment factor, if any). |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
666 |
TUint32 codeOffset = *iol; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
667 |
TUint32* codePtr = (TUint32*)(codeBase+codeOffset); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
668 |
TUint32 importInfo = *codePtr; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
669 |
TUint32 ordinal = importInfo & 0xffff; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
670 |
TUint32 adjustment = importInfo >> 16; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
671 |
if(ordinal > maxOrdinal) |
0 | 672 |
return KErrNotSupported; |
673 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
674 |
// If the import number is 0 (symbol name data), and the exporter doesn't provide |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
675 |
// this, we don't regard it as an error; we just skip this block, leaving the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
676 |
// address set to 0. For all other valid cases, we index the export directory to find |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
677 |
// the exported object's address (which may OR MAY NOT have already been relocated) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
678 |
TUint32 expAddr = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
679 |
TUint32 newValue = 0; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
680 |
if (ordinal > 0 || (exporter.iAttr & ECodeSegAttNmdExpData)) |
0 | 681 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
682 |
expAddr = exportDir[ordinal]; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
683 |
if(expAddr == 0 || expAddr == absentOrdinal) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
684 |
return KErrNotSupported; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
685 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
686 |
// If the exporter does not use code paging, then the entries in the export |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
687 |
// table will already have been relocated along with its text section. In |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
688 |
// the paged case, however, the relocation will have been deferred until the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
689 |
// relevant pages are (re)loaded; therefore, we have to deduce here whether |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
690 |
// each export is code or data so that we can apply the correct delta ... |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
691 |
TUint32 sectionDelta; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
692 |
if (!exporter.iUseCodePaging) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
693 |
sectionDelta = dirDelta; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
694 |
else if (expAddr >= codeStart && expAddr < codeEnd) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
695 |
sectionDelta = codeDelta; // points to text/rdata section |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
696 |
else if (expAddr >= dataStart && expAddr < dataEnd) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
697 |
sectionDelta = dataDelta; // points to data/bss section |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
698 |
else |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
699 |
sectionDelta = dirDelta; // unknown - assume nonpaged? |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
700 |
newValue = expAddr + sectionDelta + adjustment; |
0 | 701 |
} |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
702 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
703 |
__LDRTRACE(MyPrintf("svElfDerivedFixupImportAddresses: import[%d] (%08x:%08x) is export[%d] %08x+%08x => %08x", |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
704 |
iol - info.iImportOffsetList, codePtr, importInfo, ordinal, expAddr, adjustment, newValue)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
705 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
706 |
// In non-paged code, we can simply replace the ordinals in the IAT with the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
707 |
// object addresses to which they refer once and for all. However, in a code |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
708 |
// paging system, the IAT may be thrown away and later reloaded from the code |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
709 |
// image; therefore, we need to save the updates in the buffer pointed to by |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
710 |
// 'iFixup64' so that they can be reapplied each time the code page(s) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
711 |
// containing (parts of the) IAT are reloaded. The fixup entries are in the |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
712 |
// form of 64-bit words, with the 32-bit address-to-be-fixed-up in the upper |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
713 |
// half and the value-to-be-stored-there in the lower half -- the multiple |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
714 |
// casts are needed to stop some compilers whinging about converting a |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
715 |
// pointer to a 64-bit integral type :( |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
716 |
if (!info.iFixup64) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
717 |
*codePtr = newValue; |
0 | 718 |
else |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
719 |
*info.iFixup64++ = ((TUint64)(TUintPtr)codePtr << 32) | newValue; |
0 | 720 |
} |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
721 |
|
0 | 722 |
return KErrNone; |
723 |
} |
|
724 |
||
725 |
||
726 |
/** |
|
727 |
Wrapper for memory copy arguments. |
|
728 |
*/ |
|
729 |
struct SCopyDataInfo |
|
730 |
{ |
|
731 |
TAny* iDest; |
|
732 |
const TAny* iSource; |
|
733 |
TInt iNumberOfBytes; |
|
734 |
}; |
|
735 |
||
736 |
||
737 |
/** |
|
738 |
Copies word aligned memory. |
|
739 |
@param aPtr Pointer to function arguments (SCopyDataInfo structure). |
|
740 |
*/ |
|
741 |
TInt svWordCopy(TAny* aPtr) |
|
742 |
{ |
|
743 |
SCopyDataInfo& info=*(SCopyDataInfo*)aPtr; |
|
744 |
return (TInt) Mem::Move(info.iDest, info.iSource, info.iNumberOfBytes); |
|
745 |
} |
|
746 |
||
747 |
||
748 |
/** |
|
749 |
Copies memory. |
|
750 |
@param aPtr Pointer to function arguments (SCopyDataInfo structure). |
|
751 |
*/ |
|
752 |
TInt svMemCopy(TAny* aPtr) |
|
753 |
{ |
|
754 |
SCopyDataInfo& info=*(SCopyDataInfo*)aPtr; |
|
755 |
return (TInt) Mem::Copy(info.iDest, info.iSource, info.iNumberOfBytes); |
|
756 |
} |
|
757 |
||
758 |
||
759 |
/** |
|
760 |
Argument for svElfDerivedGetImportInfo. |
|
761 |
*/ |
|
762 |
struct SGetImportDataInfo |
|
763 |
{ |
|
764 |
TInt iCount; // number to extract |
|
765 |
TUint32* iDest; // destination address for data |
|
766 |
TUint32 iCodeLoadAddress; // address where code has been loaded |
|
767 |
TUint32* iImportOffsetList; // pointer to list of import offsets in E32ImportBlock |
|
768 |
}; |
|
769 |
||
770 |
/** |
|
771 |
Extract import ordinals/data |
|
772 |
@param aPtr Pointer to function arguments (SGetImportDataInfo structure). |
|
773 |
*/ |
|
774 |
TInt svElfDerivedGetImportInfo(TAny* aPtr) |
|
775 |
{ |
|
776 |
SGetImportDataInfo& info = *(SGetImportDataInfo*)aPtr; |
|
777 |
TInt count = info.iCount; |
|
778 |
TUint32* dest = info.iDest; |
|
779 |
TUint32 code = info.iCodeLoadAddress; |
|
780 |
TUint32* iol = info.iImportOffsetList; |
|
781 |
||
782 |
TUint32* iolEnd = iol+count; |
|
783 |
while(iol<iolEnd) |
|
784 |
*dest++ = *(TUint32*)(code + *iol++); |
|
785 |
||
786 |
return 0; |
|
787 |
} |
|
788 |
||
789 |
/******************************************************************************* |
|
790 |
* End of supervisor mode functions |
|
791 |
******************************************************************************/ |
|
792 |
||
793 |
||
794 |
/******************************************************************************* |
|
795 |
* RImageInfo |
|
796 |
******************************************************************************/ |
|
797 |
RImageInfo::RImageInfo() |
|
798 |
{ |
|
799 |
memclr(this, sizeof(RImageInfo)); |
|
800 |
} |
|
801 |
||
802 |
void RImageInfo::Close() |
|
803 |
{ |
|
804 |
iFile.Close(); |
|
805 |
delete iHeader; |
|
806 |
iHeader=NULL; |
|
807 |
gFileDataAllocator.Free(iFileData); |
|
808 |
iFileData=NULL; |
|
809 |
} |
|
810 |
||
811 |
void RImageInfo::Accept(RImageInfo& aInfo) |
|
812 |
{ |
|
813 |
Close(); |
|
814 |
wordmove(this, &aInfo, sizeof(RImageInfo)); |
|
815 |
memclr(&aInfo.iFile, (_FOFF(RImageInfo,iFileSize) - _FOFF(RImageInfo,iFile)) ); |
|
816 |
} |
|
817 |
||
818 |
/******************************************************************************* |
|
819 |
* EPOC executable file finders |
|
820 |
******************************************************************************/ |
|
821 |
RImageFinder::RImageFinder() |
|
822 |
: iNameMatches(0), iUidFail(0), iCapFail(0), iMajorVersionFail(0), iImportFail(0), |
|
823 |
iCurrentVersion(KModuleVersionNull), iCurrentDrive(0), iFindExact(0), iNewValid(0), |
|
824 |
iReq(0), iExisting(0) |
|
825 |
{ |
|
826 |
} |
|
827 |
||
828 |
TInt RImageFinder::Set(const RLdrReq& aReq) |
|
829 |
{ |
|
830 |
iReq = &aReq; |
|
831 |
TInt l = aReq.iFileNameInfo.BaseLen() + aReq.iFileNameInfo.ExtLen(); |
|
832 |
if (l > KMaxProcessName) |
|
833 |
return KErrBadName; |
|
834 |
aReq.iFileNameInfo.GetName(iRootName, TFileNameInfo::EIncludeBaseExt); |
|
835 |
return KErrNone; |
|
836 |
} |
|
837 |
||
838 |
void RImageFinder::Close() |
|
839 |
{ |
|
840 |
iNew.Close(); |
|
841 |
} |
|
842 |
||
843 |
_LIT8(KDefaultPathSysBin, "sys\\bin"); |
|
844 |
_LIT8(KDefaultPathSysBin2, "?:\\sys\\bin"); |
|
845 |
_LIT8(KDefaultExePath, "sys\\bin;system\\bin;system\\programs;system\\libs"); |
|
846 |
_LIT8(KDefaultDllPath, "sys\\bin;system\\bin;system\\libs"); |
|
847 |
_LIT8(KDefaultExePath2, "?:\\sys\\bin;?:\\system\\bin;?:\\system\\programs;?:\\system\\libs"); |
|
848 |
_LIT8(KDefaultDllPath2, "?:\\sys\\bin;?:\\system\\bin;?:\\system\\libs"); |
|
849 |
||
850 |
TInt RImageFinder::Search() |
|
851 |
{ |
|
852 |
__LDRTRACE(iReq->Dump(">RImageFinder::Search")); |
|
853 |
TBool exe = (iReq->iRequestedUids[0] == KExecutableImageUid); |
|
854 |
const TFileNameInfo& fi = iReq->iFileNameInfo; |
|
855 |
TInt r = KErrNone; |
|
856 |
if (fi.PathLen()) |
|
857 |
{ |
|
858 |
// path specified, so only look there |
|
859 |
TPtrC8 drive_and_path(fi.DriveAndPath()); |
|
860 |
r = Search(&drive_and_path, 0); |
|
861 |
} |
|
862 |
else |
|
863 |
{ |
|
864 |
TInt drv = -1; |
|
865 |
if (fi.DriveLen()) |
|
866 |
{ |
|
867 |
// drive specified |
|
868 |
drv = (*iReq->iFileName)[0]; |
|
869 |
} |
|
870 |
// if a search path is specified look there |
|
871 |
if (iReq->iPath) |
|
872 |
r = Search(iReq->iPath, drv); |
|
873 |
if (r == KErrNoMemory) // ignore other errors as they are a potential denial of service |
|
874 |
{ |
|
875 |
__LDRTRACE(Dump("<RImageFinder::Search", r)); |
|
876 |
return r; |
|
877 |
} |
|
878 |
const TDesC8* defpath; |
|
879 |
if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)) |
|
880 |
defpath = (drv<0) ? &KDefaultPathSysBin() : &KDefaultPathSysBin2(); |
|
881 |
else |
|
882 |
{ |
|
883 |
if (drv<0) |
|
884 |
defpath = exe ? &KDefaultExePath() : &KDefaultDllPath(); |
|
885 |
else |
|
886 |
defpath = exe ? &KDefaultExePath2() : &KDefaultDllPath2(); |
|
887 |
} |
|
888 |
r = Search(defpath, drv); |
|
889 |
} |
|
890 |
if (r == KErrNoMemory) // ignore other errors as they are a potential denial of service |
|
891 |
{ |
|
892 |
__LDRTRACE(Dump("<RImageFinder::Search", r)); |
|
893 |
return r; |
|
894 |
} |
|
895 |
if (iExisting || iNewValid) |
|
896 |
r = KErrNone; // found something suitable |
|
897 |
else if (!iNameMatches) |
|
898 |
r = KErrNotFound; // nothing matched requested name |
|
899 |
else if (iImportFail || iMajorVersionFail) |
|
900 |
r = KErrNotSupported; // something failed only on missing imports or version |
|
901 |
else if (iCapFail) |
|
902 |
r = KErrPermissionDenied; // something failed capability check |
|
903 |
else if (iUidFail) |
|
904 |
r = KErrNotSupported; // something failed UID check |
|
905 |
else |
|
906 |
r = KErrCorrupt; // a file had the correct name but was not a valid E32Image file |
|
907 |
__LDRTRACE(Dump("<RImageFinder::Search", r)); |
|
908 |
return r; |
|
909 |
} |
|
910 |
||
911 |
TInt RImageFinder::Search(const TDesC8* aPath, TInt aDrive) |
|
912 |
{ |
|
913 |
__IF_DEBUG(Printf(">Path %S Drive %02x", aPath, aDrive)); |
|
914 |
TInt ppos = 0; |
|
915 |
TInt plen = aPath->Length(); |
|
916 |
while (ppos < plen) |
|
917 |
{ |
|
918 |
TPtrC8 remain(aPath->Mid(ppos)); |
|
919 |
TInt pel = remain.Locate(';'); |
|
920 |
if (pel < 0) |
|
921 |
{ |
|
922 |
pel = remain.Length(); |
|
923 |
ppos += pel; |
|
924 |
} |
|
925 |
else |
|
926 |
{ |
|
927 |
ppos += pel + 1; |
|
928 |
} |
|
929 |
if (pel == 0) |
|
930 |
continue; |
|
931 |
TBool alldrives = EFalse; |
|
932 |
if (pel<2 || remain[1]!=':') |
|
933 |
alldrives = ETrue; |
|
934 |
else if (remain[0]!='?') |
|
935 |
aDrive = remain[0]; |
|
936 |
TInt drive = EDriveY; |
|
937 |
if (!alldrives && RFs::CharToDrive(TChar(aDrive), drive)!=KErrNone) |
|
938 |
continue; |
|
939 |
iCurrentDrive = (TUint8)drive; |
|
940 |
TInt startpos = alldrives ? 0 : 2; |
|
941 |
iCurrentPath.Set(remain.Mid(startpos, pel - startpos)); |
|
942 |
do { |
|
943 |
TInt r; |
|
944 |
#ifdef __X86__ |
|
945 |
if (alldrives && iCurrentDrive<=EDriveB && iCurrentDrive!=UseFloppy) |
|
946 |
goto bypass_drive; |
|
947 |
#endif |
|
948 |
r = SearchSingleDir(); |
|
949 |
if (r == KErrNoMemory) // ignore other errors as they are a potential denial of service |
|
950 |
{ |
|
951 |
__IF_DEBUG(Printf("OOM!")); |
|
952 |
return r; |
|
953 |
} |
|
954 |
#ifdef __X86__ |
|
955 |
bypass_drive: |
|
956 |
#endif |
|
957 |
if (!iCurrentDrive--) |
|
958 |
iCurrentDrive = EDriveZ; |
|
959 |
} while(alldrives && iCurrentDrive != EDriveY); |
|
960 |
} |
|
961 |
__IF_DEBUG(Printf("<Path %S Drive %02x", aPath, aDrive)); |
|
962 |
return KErrNone; |
|
963 |
} |
|
964 |
||
965 |
// Can't be looking for main loadee here, so iReq->iImporter is never NULL |
|
966 |
// Also gExeAttr must be set up |
|
967 |
TInt RImageFinder::SearchExisting(const RImageArray& aArray) |
|
968 |
{ |
|
969 |
__IF_DEBUG(Printf(">RImageFinder::SearchExisting")); |
|
970 |
TUint required_abi = gExeAttr & ECodeSegAttABIMask; |
|
971 |
TInt first, last, i; |
|
972 |
aArray.Find(iRootName, first, last); |
|
973 |
for (i=first; i<last; ++i) |
|
974 |
{ |
|
975 |
E32Image* e = aArray[i]; |
|
976 |
if (CheckUids(e->iUids, iReq->iRequestedUids) != KErrNone) |
|
977 |
continue; |
|
978 |
if (iReq->CheckSecInfo(e->iS) != KErrNone) |
|
979 |
continue; |
|
980 |
TInt action = DetailedCompareVersions(e->iModuleVersion, iReq->iRequestedVersion, iCurrentVersion, EFalse); |
|
981 |
if (action == EAction_Skip) |
|
982 |
continue; |
|
983 |
if (action == EAction_CheckImports || action == EAction_CheckLastImport) |
|
984 |
{ |
|
985 |
// Never optimistically link to something with a different ABI |
|
986 |
if ((e->iAttr & ECodeSegAttABIMask) != required_abi) |
|
987 |
continue; |
|
988 |
TInt r = CheckRequiredImports(iReq->iImporter, e, action); |
|
989 |
if (r != KErrNone) |
|
990 |
{ |
|
991 |
if (r != KErrNotSupported) |
|
992 |
return r; |
|
993 |
continue; |
|
994 |
} |
|
995 |
} |
|
996 |
iExisting = e; |
|
997 |
iCurrentVersion = e->iModuleVersion; |
|
998 |
} |
|
999 |
__IF_DEBUG(Printf("<RImageFinder::SearchExisting")); |
|
1000 |
return KErrNone; |
|
1001 |
} |
|
1002 |
||
1003 |
// Called for each file found with matching root name but which is not a valid E32ImageFile |
|
1004 |
void RImageFinder::RecordCorruptFile() |
|
1005 |
{ |
|
1006 |
__IF_DEBUG(Printf("RImageFinder::RecordCorruptFile")); |
|
1007 |
++iNameMatches; |
|
1008 |
} |
|
1009 |
||
1010 |
// Called for each valid E32Image file found with matching root name |
|
1011 |
TInt RImageFinder::Try(RImageInfo& aInfo, const TDesC8& aRootName, const TDesC8& aDriveAndPath) |
|
1012 |
{ |
|
1013 |
__IF_DEBUG(Printf(">RImageFinder::Try %S%S", &aDriveAndPath, &aRootName)); |
|
1014 |
__IF_DEBUG(Printf(">MA:%08x MV:%08x RV:%08x CV:%08x", aInfo.iAttr, aInfo.iModuleVersion, iReq->iRequestedVersion, iCurrentVersion)); |
|
1015 |
++iNameMatches; |
|
1016 |
if (iFindExact) |
|
1017 |
{ |
|
1018 |
if ( ((aInfo.iAttr & ECodeSegAttExpVer) && aInfo.iModuleVersion==iReq->iRequestedVersion) |
|
1019 |
|| (!(aInfo.iAttr & ECodeSegAttExpVer) && iReq->iRequestedVersion==KModuleVersionWild) |
|
1020 |
) |
|
1021 |
{ |
|
1022 |
__IF_DEBUG(Printf("<RImageFinder::Try Exact Match Found")); |
|
1023 |
iNewValid = 1; |
|
1024 |
iNew.Accept(aInfo); |
|
1025 |
SetName(aRootName, aDriveAndPath); |
|
1026 |
return KErrCompletion; |
|
1027 |
} |
|
1028 |
return KErrNotFound; |
|
1029 |
} |
|
1030 |
TUint required_abi = gExeAttr & ECodeSegAttABIMask; |
|
1031 |
TBool abi_mismatch = ((aInfo.iAttr & ECodeSegAttABIMask)!=required_abi); |
|
1032 |
TInt32* uid = (TInt32*)&iReq->iRequestedUids; |
|
1033 |
TBool dll_wanted = (uid[0] == KDynamicLibraryUidValue); |
|
1034 |
if (CheckUids(*(TUidType*)aInfo.iUid, iReq->iRequestedUids) != KErrNone) |
|
1035 |
{ |
|
1036 |
++iUidFail; |
|
1037 |
__IF_DEBUG(Printf("<RImageFinder::Try UIDFAIL")); |
|
1038 |
return KErrNotFound; |
|
1039 |
} |
|
1040 |
if (iReq->CheckSecInfo(aInfo.iS) != KErrNone) |
|
1041 |
{ |
|
1042 |
++iCapFail; |
|
1043 |
__IF_DEBUG(Printf("<RImageFinder::Try CAPFAIL")); |
|
1044 |
return KErrNotFound; |
|
1045 |
} |
|
1046 |
TInt action = DetailedCompareVersions(aInfo.iModuleVersion, iReq->iRequestedVersion, iCurrentVersion, !iReq->iImporter); |
|
1047 |
if (action == EAction_Skip) |
|
1048 |
{ |
|
1049 |
if (DetailedCompareVersions(aInfo.iModuleVersion, iReq->iRequestedVersion) == EVersion_MajorSmaller) |
|
1050 |
++iMajorVersionFail; |
|
1051 |
__IF_DEBUG(Printf("<RImageFinder::Try VERFAIL")); |
|
1052 |
return KErrNotFound; |
|
1053 |
} |
|
1054 |
if (action == EAction_CheckImports || action == EAction_CheckLastImport) |
|
1055 |
{ |
|
1056 |
// If we get here, can't be main loadee so gExeAttr must be valid |
|
1057 |
// Never optimistically link to something with a different ABI |
|
1058 |
if (abi_mismatch || CheckRequiredImports(iReq->iImporter, aInfo, action)!=KErrNone) |
|
1059 |
{ |
|
1060 |
__IF_DEBUG(Printf("<RImageFinder::Try IMPFAIL")); |
|
1061 |
++iImportFail; |
|
1062 |
return KErrNotFound; |
|
1063 |
} |
|
1064 |
} |
|
1065 |
if (!iReq->iImporter && dll_wanted && abi_mismatch) |
|
1066 |
{ |
|
1067 |
// Dynamically loading a DLL - ABI must match loading process |
|
1068 |
__IF_DEBUG(Printf("<RImageFinder::Try ABIFAIL")); |
|
1069 |
++iImportFail; |
|
1070 |
return KErrNotFound; |
|
1071 |
} |
|
1072 |
if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)) |
|
1073 |
{ |
|
1074 |
TChar driveLetter; |
|
1075 |
TInt driveNumber; |
|
1076 |
TInt r; |
|
1077 |
driveLetter=(TChar)aDriveAndPath[0]; |
|
1078 |
RFs::CharToDrive(driveLetter,driveNumber); |
|
1079 |
TDriveCacheHeader* pDH=gDriveFileNamesCache[driveNumber]; |
|
1080 |
TUint driveAtt=0; |
|
1081 |
if(pDH) |
|
1082 |
driveAtt=pDH->iDriveAtt; |
|
1083 |
else |
|
1084 |
{ |
|
1085 |
TDriveInfo driveInfo; |
|
1086 |
if ((r=gTheLoaderFs.Drive(driveInfo,driveNumber)) != KErrNone) |
|
1087 |
{ |
|
1088 |
__IF_DEBUG(Printf("<RImageFinder::Try DINFFAIL")); |
|
1089 |
++iImportFail; |
|
1090 |
return r; |
|
1091 |
} |
|
1092 |
driveAtt=driveInfo.iDriveAtt; |
|
1093 |
} |
|
1094 |
||
1095 |
if(driveAtt & KDriveAttRemovable) |
|
1096 |
{ |
|
1097 |
__IF_DEBUG(Printf("** RImageFinder::Try %S%S is on a removable drive", &aDriveAndPath, &aRootName)); |
|
1098 |
// If the cache says we already checked the hash of this file, accept it without checking again |
|
1099 |
// as any *legitimate* change to the file would've triggered the cache to be rebuilt. |
|
1100 |
if (!(aInfo.iCacheStatus & TImageInfo::EHashChecked)) |
|
1101 |
{ |
|
1102 |
//We have to pass aDriveAndPath as aInfo may not contain Drive |
|
1103 |
TRAP(r,CompareHashL(aInfo, aDriveAndPath)); |
|
1104 |
if (r == KErrNoMemory) |
|
1105 |
return r; |
|
1106 |
if(r!=KErrNone) |
|
1107 |
{ |
|
1108 |
__IF_DEBUG(Printf("<RImageFinder::Try Compare Hash Failed")); |
|
1109 |
iCapFail++; |
|
1110 |
return r; |
|
1111 |
} |
|
1112 |
aInfo.iCacheStatus |= TImageInfo::EHashChecked; |
|
1113 |
} |
|
1114 |
else |
|
1115 |
{ |
|
1116 |
// We've skipped hash checking as an optimisation, however someone could potentially have |
|
1117 |
// used external hardware to switch the data on the card since the cached hash check. Setting |
|
1118 |
// this mark means that if we actually load the file, we'll hash it then; but if it turns out |
|
1119 |
// to be already loaded, we can save the effort. |
|
1120 |
aInfo.iNeedHashCheck = 1; |
|
1121 |
} |
|
1122 |
} |
|
1123 |
} |
|
1124 |
iExisting = NULL; |
|
1125 |
iNew.Accept(aInfo); |
|
1126 |
iNewValid = 1; |
|
1127 |
iCurrentVersion = aInfo.iModuleVersion; |
|
1128 |
SetName(aRootName, aDriveAndPath); |
|
1129 |
__IF_DEBUG(Printf("<MV:%08x RV:%08x CV:%08x", aInfo.iModuleVersion, iReq->iRequestedVersion, iCurrentVersion)); |
|
1130 |
__IF_DEBUG(Printf("<RImageFinder::Try OK")); |
|
1131 |
return KErrNone; |
|
1132 |
} |
|
1133 |
||
1134 |
void RImageFinder::CompareHashL(RImageInfo& aInfo, const TDesC8& aDriveAndPath) |
|
1135 |
// |
|
1136 |
// Calculate hash and compare after checking if one already exists in c:/system/caps |
|
1137 |
// |
|
1138 |
{ |
|
1139 |
__IF_DEBUG(Printf(">RImageFinder::CompareHashL")); |
|
1140 |
||
1141 |
TInt extraFlag = 0; |
|
1142 |
TBuf8<KMaxFileName*sizeof(TText)> fileName; |
|
1143 |
TFileNameInfo fni = iReq->iFileNameInfo; |
|
1144 |
if (aInfo.iAttr & ECodeSegAttExpVer) |
|
1145 |
{ |
|
1146 |
fni.iVersion = aInfo.iModuleVersion; |
|
1147 |
extraFlag = TFileNameInfo::EForceVer; |
|
1148 |
} |
|
1149 |
||
1150 |
TFileName hashname(KSysHash); |
|
1151 |
hashname[0] = (TUint8) RFs::GetSystemDriveChar(); |
|
1152 |
fileName.SetLength(0); |
|
1153 |
fni.GetName(fileName, TFileNameInfo::EIncludeBaseExt | extraFlag); |
|
1154 |
hashname.Append(fileName.Expand()); |
|
1155 |
||
1156 |
RFile fHash; |
|
1157 |
CleanupClosePushL(fHash); |
|
1158 |
||
1159 |
__IF_DEBUG(Printf("RImageFinder::CompareHashL opening hash file %S ", &hashname)); |
|
1160 |
User::LeaveIfError(fHash.Open(gTheLoaderFs,hashname,EFileRead|EFileReadDirectIO)); |
|
1161 |
||
1162 |
TBuf8<SHA1_HASH> installhash; |
|
1163 |
User::LeaveIfError(fHash.Read(installhash)); |
|
1164 |
CleanupStack::PopAndDestroy(1); |
|
1165 |
||
1166 |
// if we get this far, we have loaded a valid hash, so calculate the file's hash |
|
1167 |
||
1168 |
CSHA1* hasher=CSHA1::NewL(); |
|
1169 |
CleanupStack::PushL(hasher); |
|
1170 |
||
1171 |
fileName.Copy(aDriveAndPath); |
|
1172 |
fni.GetName(fileName, TFileNameInfo::EIncludeBaseExt | extraFlag); |
|
1173 |
||
1174 |
CleanupClosePushL(aInfo.iFile); |
|
1175 |
TBool b = aInfo.FileOpened(); |
|
1176 |
if(!b) |
|
1177 |
{ |
|
1178 |
__IF_DEBUG(Printf("RImageFinder::CompareHashL opening the file %S", &fileName)); |
|
1179 |
User::LeaveIfError(aInfo.iFile.Open(gTheLoaderFs, fileName.Expand(), EFileRead|EFileReadDirectIO)); |
|
1180 |
} |
|
1181 |
||
1182 |
__IF_DEBUG(Printf("RImageFinder::CompareHashL calculate hash")); |
|
1183 |
TInt size; |
|
1184 |
User::LeaveIfError(aInfo.iFile.Size(size)); |
|
1185 |
aInfo.iFileData = (TUint8*)gFileDataAllocator.Alloc(size); |
|
1186 |
if (aInfo.iFileData) |
|
1187 |
aInfo.iFileSize = size; |
|
1188 |
else |
|
1189 |
User::Leave(KErrNoMemory); |
|
1190 |
TPtr8 filedata(aInfo.iFileData, size); |
|
1191 |
User::LeaveIfError(aInfo.iFile.Read(0, filedata, size)); |
|
1192 |
if (filedata.Length() != size) |
|
1193 |
User::Leave(KErrCorrupt); |
|
1194 |
CleanupStack::PopAndDestroy(1); //the file handle only->aInfo.iFile.Close(); |
|
1195 |
hasher->Update(filedata); |
|
1196 |
||
1197 |
TBuf8<SHA1_HASH> hash; |
|
1198 |
hash=hasher->Final(); |
|
1199 |
||
1200 |
||
1201 |
__IF_DEBUG(Printf("RImageFinder::CompareHashL comparing hashes...")); |
|
1202 |
if(0 != hash.Compare(installhash)) |
|
1203 |
User::Leave(KErrPermissionDenied); |
|
1204 |
CleanupStack::PopAndDestroy(1); |
|
1205 |
||
1206 |
// if we get this far the hash has passed and the file has been closed |
|
1207 |
// but some of the RImageInfo parameters will've been initialised by the cache |
|
1208 |
// and may be lies if we're being attacked, so compare them to be sure |
|
1209 |
||
1210 |
// if we already had the header, throw it away: it's from untrusted data |
|
1211 |
if (aInfo.iHeader) |
|
1212 |
{ |
|
1213 |
delete aInfo.iHeader; |
|
1214 |
aInfo.iHeader = NULL; |
|
1215 |
} |
|
1216 |
||
1217 |
// make the header and validate the cached parameters against it |
|
1218 |
User::LeaveIfError(E32ImageHeader::New(aInfo.iHeader, aInfo.iFileData, aInfo.iFileSize)); |
|
1219 |
||
1220 |
SSecurityInfo secinfo; |
|
1221 |
aInfo.iHeader->GetSecurityInfo(secinfo); |
|
1222 |
TUint32 attr = (aInfo.iHeader->iFlags & ECodeSegAttFixed) | aInfo.iHeader->ABI(); |
|
1223 |
if(aInfo.iHeader->iFlags&KImageNmdExpData) |
|
1224 |
attr |= ECodeSegAttNmdExpData; |
|
1225 |
if (Mem::Compare((TUint8*)aInfo.iUid, sizeof(aInfo.iUid), (TUint8*)&aInfo.iHeader->iUid1, sizeof(aInfo.iUid)) |
|
1226 |
|| aInfo.iModuleVersion != aInfo.iHeader->ModuleVersion() |
|
1227 |
|| Mem::Compare((TUint8*)&aInfo.iS, sizeof(aInfo.iS), (TUint8*)&secinfo, sizeof(secinfo)) |
|
1228 |
|| (aInfo.iAttr & ~ECodeSegAttExpVer) != attr) |
|
1229 |
User::Leave(KErrPermissionDenied); |
|
1230 |
||
1231 |
__IF_DEBUG(Printf("<RImageFinder::CompareHashL passed")); |
|
1232 |
} |
|
1233 |
||
1234 |
void RImageFinder::SetName(const TDesC8& aRootName, const TDesC8& aDriveAndPath) |
|
1235 |
{ |
|
1236 |
iNewFileName = aDriveAndPath; |
|
1237 |
iNewFileName.Append(aRootName); |
|
1238 |
} |
|
1239 |
||
1240 |
RImageArray::RImageArray() |
|
1241 |
: RPointerArray<E32Image>(8, 2*256) |
|
1242 |
{ |
|
1243 |
} |
|
1244 |
||
1245 |
TInt RImageArray::Add(E32Image* aImage) |
|
1246 |
{ |
|
1247 |
return InsertInOrderAllowRepeats(aImage, &E32Image::Order); |
|
1248 |
} |
|
1249 |
||
1250 |
void RImageArray::Find(const TDesC8& aRootName, TInt& aFirst, TInt& aLast) const |
|
1251 |
{ |
|
1252 |
TCodeSegCreateInfo name; |
|
1253 |
name.iFileName.Copy(aRootName); |
|
1254 |
name.iRootNameOffset = 0; |
|
1255 |
name.iRootNameLength = aRootName.Length(); |
|
1256 |
aFirst = SpecificFindInOrder((const E32Image*)&name, &E32Image::Order, EArrayFindMode_First); |
|
1257 |
aLast = aFirst; |
|
1258 |
if (aFirst >= 0) |
|
1259 |
aLast = SpecificFindInOrder((const E32Image*)&name, &E32Image::Order, EArrayFindMode_Last); |
|
1260 |
} |
|
1261 |
||
1262 |
E32Image* RImageArray::Find(const TRomImageHeader* a) const |
|
1263 |
{ |
|
1264 |
TInt c = Count(); |
|
1265 |
if (!c) |
|
1266 |
return NULL; |
|
1267 |
E32Image* const * ee = &(*this)[0]; |
|
1268 |
E32Image* const * eE = ee + c; |
|
1269 |
for (; ee<eE && (*ee)->iRomImageHeader != a; ++ee) {} |
|
1270 |
return (ee<eE) ? *ee : NULL; |
|
1271 |
} |
|
1272 |
||
1273 |
TInt E32Image::LoadProcess(const RLdrReq& aReq) |
|
1274 |
{ |
|
1275 |
__LDRTRACE(aReq.Dump("E32Image::LoadProcess")); |
|
1276 |
||
1277 |
RImageFinder finder; |
|
1278 |
TInt r = finder.Set(aReq); |
|
1279 |
if (r == KErrNone) |
|
1280 |
r = finder.Search(); |
|
1281 |
if (r!=KErrNone) |
|
1282 |
{ |
|
1283 |
finder.Close(); |
|
1284 |
return r; |
|
1285 |
} |
|
1286 |
r = Construct(finder); // needs to find it if it's already loaded |
|
1287 |
finder.Close(); |
|
1288 |
if (r!=KErrNone) |
|
1289 |
{ |
|
1290 |
return r; |
|
1291 |
} |
|
1292 |
if (iIsDll) |
|
1293 |
return KErrNotSupported; |
|
1294 |
r = aReq.iMsg->Client((RThread&)aReq.iClientThread); |
|
1295 |
if (r!=KErrNone) |
|
1296 |
{ |
|
1297 |
return r; |
|
1298 |
} |
|
1299 |
iClientHandle=aReq.iClientThread.Handle(); |
|
1300 |
||
1301 |
if(iStackSize < aReq.iMinStackSize) |
|
1302 |
iStackSize=aReq.iMinStackSize; // If the process required larger stack than the default. |
|
1303 |
||
1304 |
//initialise to zero |
|
1305 |
#ifdef _DEBUG |
|
1306 |
iDestructStat = ProcessDestructStatPtr; |
|
1307 |
#endif |
|
1308 |
iDebugAttributes = 0; |
|
1309 |
if (iRomImageHeader) |
|
1310 |
{ |
|
1311 |
if (iRomImageHeader->iFlags & KRomImageDebuggable) |
|
1312 |
iDebugAttributes |= EDebugAllowed; |
|
1313 |
} |
|
1314 |
else if (iHeader) |
|
1315 |
{ |
|
1316 |
if (iHeader->iFlags & KImageDebuggable) |
|
1317 |
iDebugAttributes |= EDebugAllowed; |
|
1318 |
} |
|
1319 |
||
1320 |
// Get the data paging flags and pass to the kernel. |
|
1321 |
__ASSERT_COMPILE(EDataPagingUnspecified == 0); |
|
1322 |
if (iRomImageHeader) |
|
1323 |
{ |
|
1324 |
TUint dataPaging = iRomImageHeader->iFlags & KRomImageDataPagingMask; |
|
1325 |
if (dataPaging == KRomImageDataPagingMask) |
|
1326 |
RETURN_FAILURE(KErrCorrupt); |
|
1327 |
if (dataPaging == KRomImageFlagDataPaged) |
|
1328 |
iFlags |= EDataPaged; |
|
1329 |
if (dataPaging == KRomImageFlagDataUnpaged) |
|
1330 |
iFlags |= EDataUnpaged; |
|
1331 |
} |
|
1332 |
else if (iHeader) |
|
1333 |
{ |
|
1334 |
TUint dataPaging = iHeader->iFlags & KImageDataPagingMask; |
|
1335 |
if (dataPaging == KImageDataPagingMask) |
|
1336 |
RETURN_FAILURE(KErrCorrupt); |
|
1337 |
if (dataPaging == KImageDataPaged) |
|
1338 |
iFlags |= EDataPaged; |
|
1339 |
if (dataPaging == KImageDataUnpaged) |
|
1340 |
iFlags |= EDataUnpaged; |
|
1341 |
} |
|
1342 |
||
1343 |
r=E32Loader::ProcessCreate(*this, aReq.iCmd); |
|
1344 |
__IF_DEBUG(Printf("Done E32Loader::ProcessCreate %d",r)); |
|
1345 |
if (r!=KErrNone) |
|
1346 |
{ |
|
1347 |
return r; |
|
1348 |
} |
|
1349 |
#ifdef _DEBUG |
|
1350 |
ProcessCreated = ETrue; |
|
1351 |
#endif |
|
1352 |
iClientProcessHandle=iProcessHandle; |
|
1353 |
if (!iAlreadyLoaded) |
|
1354 |
{ |
|
1355 |
gExeCodeSeg=iHandle; // implicitly linked DLLs must load into the new process |
|
1356 |
gExeAttr=iAttr; |
|
1357 |
if (!iRomImageHeader) |
|
1358 |
r=LoadToRam(); |
|
1359 |
if (r==KErrNone) |
|
1360 |
r=ProcessImports(); // this sets up gLoadeePath |
|
1361 |
} |
|
1362 |
// transfers ownership of clamp handle to codeseg; nulls handle if successful |
|
1363 |
if (r==KErrNone) |
|
1364 |
{ |
|
1365 |
r=E32Loader::ProcessLoaded(*this); |
|
1366 |
if ((r==KErrNone) && iUseCodePaging) |
|
1367 |
{ |
|
1368 |
iFileClamp.iCookie[0]=0;// null handle to indicate |
|
1369 |
iFileClamp.iCookie[1]=0;// transfer of ownership of clamp handle to proc's codeseg |
|
1370 |
} |
|
1371 |
} |
|
1372 |
__IF_DEBUG(Printf("Done E32Image::LoadProcess %d",r)); |
|
1373 |
return r; |
|
1374 |
} |
|
1375 |
||
1376 |
// Load a code segment, plus all imports if main loadee |
|
1377 |
TInt E32Image::LoadCodeSeg(const RLdrReq& aReq) |
|
1378 |
{ |
|
1379 |
__LDRTRACE(aReq.Dump(">E32Image::LoadCodeSeg")); |
|
1380 |
||
1381 |
#ifdef __X86__ |
|
1382 |
if (iMain==this && iClientProcessHandle) |
|
1383 |
{ |
|
1384 |
RProcess p; |
|
1385 |
p.SetHandle(iClientProcessHandle); |
|
1386 |
TFileName f(p.FileName()); |
|
1387 |
if (f.Length()>=2 && f[1]==':') |
|
1388 |
{ |
|
1389 |
TInt d = f[0]; |
|
1390 |
if (d=='a' || d=='A') |
|
1391 |
UseFloppy = EDriveA; |
|
1392 |
else if (d=='b' || d=='B') |
|
1393 |
UseFloppy = EDriveB; |
|
1394 |
} |
|
1395 |
} |
|
1396 |
#endif |
|
1397 |
||
1398 |
RImageFinder finder; |
|
1399 |
TInt r = finder.Set(aReq); |
|
1400 |
if (r == KErrNone) |
|
1401 |
r = finder.Search(); |
|
1402 |
if (r!=KErrNone) |
|
1403 |
{ |
|
1404 |
finder.Close(); |
|
1405 |
return r; |
|
1406 |
} |
|
1407 |
return DoLoadCodeSeg(aReq, finder); |
|
1408 |
} |
|
1409 |
||
1410 |
// Load a code segment, plus all imports if main loadee |
|
1411 |
TInt E32Image::DoLoadCodeSeg(const RLdrReq& aReq, RImageFinder& aFinder) |
|
1412 |
{ |
|
1413 |
__LDRTRACE(aReq.Dump(">E32Image::DoLoadCodeSeg")); |
|
1414 |
||
1415 |
TInt r = Construct(aFinder); // needs to find it if it's already loaded |
|
1416 |
aFinder.Close(); |
|
1417 |
if (r!=KErrNone) |
|
1418 |
{ |
|
1419 |
return r; |
|
1420 |
} |
|
1421 |
__IF_DEBUG(Printf("epv=%x, fep=%x, codesize=%x, textsize=%x, uid3=%x",iEntryPtVeneer,iFileEntryPoint,iCodeSize,iTextSize,iUids[2])); |
|
1422 |
__IF_DEBUG(Printf("attr=%08x, gExeAttr=%08x",iAttr,gExeAttr)); |
|
1423 |
||
1424 |
// If EXE and not main loadee, EXE code segment must be the same as the client process or newly loaded process |
|
1425 |
if (gExeCodeSeg && !iIsDll && iMain!=this && iHandle!=gExeCodeSeg) |
|
1426 |
return KErrNotSupported; |
|
1427 |
||
1428 |
// If DLL and main loadee, ABI must match the process |
|
1429 |
if (iIsDll && iMain==this && (iAttr & ECodeSegAttABIMask)!=(gExeAttr & ECodeSegAttABIMask) ) |
|
1430 |
return KErrNotSupported; |
|
1431 |
||
1432 |
// code segment already loaded |
|
1433 |
if (iAlreadyLoaded || (iMain!=this && AlwaysLoaded()) ) |
|
1434 |
return KErrNone; |
|
1435 |
||
1436 |
__IF_DEBUG(Printf("CodeSeg create")); |
|
1437 |
r=E32Loader::CodeSegCreate(*this); |
|
1438 |
if (r!=KErrNone) |
|
1439 |
return r; |
|
1440 |
||
1441 |
iCloseCodeSeg=iHandle; // so new code segment is removed if the load fails |
|
1442 |
if (!iRomImageHeader) |
|
1443 |
r=LoadToRam(); |
|
1444 |
if (r==KErrNone) |
|
1445 |
{ |
|
1446 |
iCloseCodeSeg=NULL; |
|
1447 |
if (iMain==this) |
|
1448 |
{ |
|
1449 |
r=ProcessImports(); // this sets up gLoadeePath |
|
1450 |
// transfers ownership of clamp handle to codeseg; nulls handle if successful |
|
1451 |
if (r==KErrNone) |
|
1452 |
{ |
|
1453 |
r=E32Loader::CodeSegLoaded(*this); |
|
1454 |
if ((r==KErrNone) && iUseCodePaging) |
|
1455 |
{ |
|
1456 |
iFileClamp.iCookie[0]=0;// null handle to indicate |
|
1457 |
iFileClamp.iCookie[1]=0;// transfer of ownership of clamp handle to codeseg |
|
1458 |
} |
|
1459 |
} |
|
1460 |
} |
|
1461 |
} |
|
1462 |
||
1463 |
__IF_DEBUG(Printf("<DoLoadCodeSeg, r=%d, iIsDll=%d",r,iIsDll)); |
|
1464 |
return r; |
|
1465 |
} |
|
1466 |
||
1467 |
// Load a ROM XIP code segment as part of another load |
|
1468 |
TInt E32Image::DoLoadCodeSeg(const TRomImageHeader& a) |
|
1469 |
{ |
|
1470 |
__IF_DEBUG(Printf("E32Image::DoLoadCodeSeg ROM XIP @%08x",&a)); |
|
1471 |
||
1472 |
Construct(a); |
|
1473 |
if (AlwaysLoaded()) |
|
1474 |
{ |
|
1475 |
GetRomFileName(); |
|
1476 |
return KErrNone; |
|
1477 |
} |
|
1478 |
TInt r=CheckRomXIPAlreadyLoaded(); |
|
1479 |
if (r!=KErrNone || iAlreadyLoaded) |
|
1480 |
{ |
|
1481 |
return r; |
|
1482 |
} |
|
1483 |
GetRomFileName(); |
|
1484 |
r=E32Loader::CodeSegCreate(*this); |
|
1485 |
||
1486 |
__IF_DEBUG(Printf("<DoLoadCodeSeg, r=%d",r)); |
|
1487 |
return r; |
|
1488 |
} |
|
1489 |
||
1490 |
/****************************************************************************** |
|
1491 |
* EPOC specific E32Image functions |
|
1492 |
******************************************************************************/ |
|
1493 |
||
1494 |
/** |
|
1495 |
Construct an image object which represents an XIP ROM executable. |
|
1496 |
*/ |
|
1497 |
void E32Image::Construct(const TRomImageHeader& a) |
|
1498 |
{ |
|
1499 |
__IF_DEBUG(Printf("E32Image::Construct ROM %08x",&a)); |
|
1500 |
||
1501 |
iRomImageHeader = &a; |
|
1502 |
iUids = *(const TUidType*)&a.iUid1; |
|
1503 |
iS = a.iS; |
|
1504 |
iCodeSize = a.iCodeSize; |
|
1505 |
iTextSize = a.iTextSize; |
|
1506 |
iDataSize = a.iDataSize; |
|
1507 |
iBssSize = a.iBssSize; |
|
1508 |
iTotalDataSize = a.iTotalDataSize; |
|
1509 |
iEntryPtVeneer = 0; |
|
1510 |
iFileEntryPoint = a.iEntryPoint; |
|
1511 |
iDepCount = a.iDllRefTable ? a.iDllRefTable->iNumberOfEntries : 0; |
|
1512 |
iExportDir = a.iExportDir; |
|
1513 |
iExportDirCount = a.iExportDirCount; |
|
1514 |
iCodeLoadAddress = (TUint32)&a; |
|
1515 |
iDataRunAddress = a.iDataBssLinearBase; // for fixed processes |
|
1516 |
iHeapSizeMin = a.iHeapSizeMin; |
|
1517 |
iHeapSizeMax = a.iHeapSizeMax; |
|
1518 |
iStackSize = a.iStackSize; |
|
1519 |
iPriority = a.iPriority; |
|
1520 |
iIsDll = (a.iFlags & KImageDll)!=0; |
|
1521 |
if(iExportDirCount) |
|
1522 |
iExportDirLoad = iExportDir; |
|
1523 |
||
1524 |
// setup attributes... |
|
1525 |
iAttr &= ~(ECodeSegAttKernel|ECodeSegAttGlobal|ECodeSegAttFixed|ECodeSegAttABIMask|ECodeSegAttNmdExpData); |
|
1526 |
if(a.iFlags&KRomImageFlagsKernelMask) |
|
1527 |
iAttr |= ECodeSegAttKernel; |
|
1528 |
else |
|
1529 |
iAttr |= ECodeSegAttGlobal; |
|
1530 |
if(a.iFlags&KRomImageFlagFixedAddressExe) |
|
1531 |
iAttr |= ECodeSegAttFixed; |
|
1532 |
iAttr |= (a.iFlags & KRomImageABIMask); |
|
1533 |
if(a.iFlags&KRomImageNmdExpData) |
|
1534 |
iAttr |= ECodeSegAttNmdExpData; |
|
1535 |
if(a.iFlags&KRomImageSMPSafe) |
|
1536 |
iAttr |= ECodeSegAttSMPSafe; |
|
1537 |
||
1538 |
iExceptionDescriptor = a.iExceptionDescriptor; |
|
1539 |
} |
|
1540 |
||
1541 |
||
1542 |
TBool E32Image::AlwaysLoaded() |
|
1543 |
{ |
|
1544 |
// If loaded from ROM and EXE or DLL with no static data or extension or variant, don't need code segment |
|
1545 |
TBool r=EFalse; |
|
1546 |
__IF_DEBUG(Printf(">E32Image::AlwaysLoaded %08x",iRomImageHeader)); |
|
1547 |
if (iRomImageHeader) |
|
1548 |
{ |
|
1549 |
if (iIsDll && (iRomImageHeader->iFlags & KRomImageFlagDataPresent)==0) |
|
1550 |
r=ETrue; |
|
1551 |
} |
|
1552 |
__IF_DEBUG(Printf("<E32Image::AlwaysLoaded %x",r)); |
|
1553 |
return r; |
|
1554 |
} |
|
1555 |
||
1556 |
||
1557 |
void E32Image::GetRomFileName() |
|
1558 |
{ |
|
1559 |
TBuf8<KMaxFileName> fn = _S8("z:\\"); |
|
1560 |
TFileNameInfo fni; |
|
1561 |
TPtr8 path_and_name(((TText8*)fn.Ptr())+3, 0, KMaxFileName-3); |
|
1562 |
const TRomDir& rootdir = *(const TRomDir*)UserSvr::RomRootDirectoryAddress(); |
|
1563 |
if (!TraverseDirs(rootdir, iRomImageHeader, path_and_name)) |
|
1564 |
*(const TAny**)1=iRomImageHeader; // DIE! |
|
1565 |
fn.SetLength(path_and_name.Length()+3); |
|
1566 |
fni.Set(fn, 0); |
|
1567 |
iFileName.Zero(); |
|
1568 |
fni.GetName(iFileName, TFileNameInfo::EIncludeDrivePathBaseExt); |
|
1569 |
if (fni.VerLen()) |
|
1570 |
iAttr |= ECodeSegAttExpVer; |
|
1571 |
iRootNameOffset = fni.iBasePos; |
|
1572 |
iRootNameLength = fni.BaseLen() + fni.ExtLen(); |
|
1573 |
iExtOffset = iFileName.Length() - fni.ExtLen(); |
|
1574 |
__IF_DEBUG(Printf("GetRomFileName(%08x)->%S,%d,%d,%d Attr %08x",iRomImageHeader,&iFileName,iRootNameOffset,iRootNameLength,iExtOffset,iAttr)); |
|
1575 |
} |
|
1576 |
||
1577 |
||
1578 |
/** |
|
1579 |
Starting from aDir, search for XIP executable specified by aHdr. |
|
1580 |
If found, return true and set aName to file path and name, (will cause descriptor panics if max size of aName isn't big enough.) |
|
1581 |
If not found, return false. |
|
1582 |
*/ |
|
1583 |
TBool E32Image::TraverseDirs(const TRomDir& aDir, const TRomImageHeader* aHdr, TDes8& aName) |
|
1584 |
{ |
|
1585 |
const TRomEntry* pE=&aDir.iEntry; |
|
1586 |
const TRomEntry* pEnd=(const TRomEntry*)((TUint8*)pE+aDir.iSize); |
|
1587 |
while(pE<pEnd) |
|
1588 |
{ |
|
1589 |
if ( (pE->iAtt & KEntryAttXIP) && (pE->iAddressLin==(TLinAddr)aHdr) ) |
|
1590 |
{ |
|
1591 |
// ROM XIP file found |
|
1592 |
aName.Copy(TPtrC16((const TText*)pE->iName, pE->iNameLength)); |
|
1593 |
return ETrue; |
|
1594 |
} |
|
1595 |
if (pE->iAtt & KEntryAttDir) |
|
1596 |
{ |
|
1597 |
// subdirectory found |
|
1598 |
const TRomDir& subdir = *(const TRomDir*)pE->iAddressLin; |
|
1599 |
TText8* p = (TText8*)aName.Ptr(); |
|
1600 |
TInt m = aName.MaxLength(); |
|
1601 |
TInt nl = pE->iNameLength; |
|
1602 |
TPtr8 ptr(p+nl+1, 0, m-nl-1); |
|
1603 |
if (TraverseDirs(subdir, aHdr, ptr)) |
|
1604 |
{ |
|
1605 |
// match found in subdirectory |
|
1606 |
aName.SetLength(ptr.Length()+nl+1); |
|
1607 |
const TText* s = (const TText*)pE->iName; |
|
1608 |
p[nl]='\\'; |
|
1609 |
while (nl--) |
|
1610 |
*p++ = (TText8)*s++; |
|
1611 |
return ETrue; |
|
1612 |
} |
|
1613 |
} |
|
1614 |
TInt entry_size = KRomEntrySize + pE->iNameLength*sizeof(TText); |
|
1615 |
entry_size = (entry_size+sizeof(TInt)-1)&~(sizeof(TInt)-1); |
|
1616 |
pE=(const TRomEntry*)((TUint8*)pE+entry_size); |
|
1617 |
} |
|
1618 |
return EFalse; |
|
1619 |
} |
|
1620 |
||
1621 |
||
1622 |
/** |
|
1623 |
Read data from a file. |
|
1624 |
*/ |
|
1625 |
TInt FileRead(RFile& aFile, TUint8* aDest, TInt aSize) |
|
1626 |
{ |
|
1627 |
TPtr8 p(aDest,aSize,aSize); |
|
1628 |
TInt r = aFile.Read(p,aSize); |
|
1629 |
if(r==KErrNone && p.Size()!=aSize) |
|
1630 |
RETURN_FAILURE(KErrCorrupt); |
|
1631 |
return r; |
|
1632 |
} |
|
1633 |
||
1634 |
||
1635 |
/** |
|
1636 |
Construct a new image header by reading a file. File must not be XIP. |
|
1637 |
*/ |
|
1638 |
TInt E32ImageHeader::New(E32ImageHeader*& aHdr, RFile& aFile) |
|
1639 |
{ |
|
1640 |
aHdr = NULL; |
|
1641 |
||
1642 |
TInt fileSize; |
|
1643 |
TInt r = aFile.Size(fileSize); |
|
1644 |
if(r!=KErrNone) |
|
1645 |
return r; |
|
1646 |
||
1647 |
E32ImageHeaderV tempHeader; |
|
1648 |
r = FileRead(aFile, (TUint8*)&tempHeader, sizeof(tempHeader)); |
|
1649 |
if(r!=KErrNone) |
|
1650 |
return r; |
|
1651 |
||
1652 |
TUint headerSize = tempHeader.TotalSize(); |
|
1653 |
if(headerSize<sizeof(tempHeader) || headerSize>TUint(KMaxHeaderSize)) |
|
1654 |
RETURN_FAILURE(KErrCorrupt); |
|
1655 |
||
1656 |
E32ImageHeaderV* header = (E32ImageHeaderV*)User::Alloc(headerSize); |
|
1657 |
if(!header) |
|
1658 |
return KErrNoMemory; |
|
1659 |
||
1660 |
wordmove(header, &tempHeader, sizeof(tempHeader)); |
|
1661 |
if(headerSize>sizeof(tempHeader)) |
|
1662 |
r = FileRead(aFile, ((TUint8*)header)+sizeof(tempHeader), headerSize-sizeof(tempHeader)); |
|
1663 |
||
1664 |
if(r==KErrNone) |
|
1665 |
r = header->ValidateAndAdjust(fileSize); |
|
1666 |
||
1667 |
if(r==KErrNone) |
|
1668 |
aHdr = header; |
|
1669 |
else |
|
1670 |
delete header; |
|
1671 |
||
1672 |
return r; |
|
1673 |
} |
|
1674 |
||
1675 |
||
1676 |
/** |
|
1677 |
Construct a new image header using data from the supplied buffer. |
|
1678 |
*/ |
|
1679 |
TInt E32ImageHeader::New(E32ImageHeader*& aHdr, TUint8* aFileData, TUint32 aFileSize) |
|
1680 |
{ |
|
1681 |
aHdr = NULL; |
|
1682 |
||
1683 |
E32ImageHeaderV& tempHeader = *(E32ImageHeaderV*)aFileData; |
|
1684 |
||
1685 |
if(aFileSize<sizeof(tempHeader)) |
|
1686 |
RETURN_FAILURE(KErrCorrupt); // too small to contain a header |
|
1687 |
||
1688 |
TUint headerSize = tempHeader.TotalSize(); |
|
1689 |
if(headerSize<sizeof(tempHeader) || headerSize>TUint(KMaxHeaderSize)) |
|
1690 |
RETURN_FAILURE(KErrCorrupt); |
|
1691 |
if(headerSize>aFileSize) |
|
1692 |
RETURN_FAILURE(KErrCorrupt); |
|
1693 |
||
1694 |
E32ImageHeaderV* header = (E32ImageHeaderV*)User::Alloc(headerSize); |
|
1695 |
if(!header) |
|
1696 |
return KErrNoMemory; |
|
1697 |
||
1698 |
wordmove(header, &tempHeader, headerSize); |
|
1699 |
||
1700 |
TInt r = header->ValidateAndAdjust(aFileSize); |
|
1701 |
if(r==KErrNone) |
|
1702 |
aHdr = header; |
|
1703 |
else |
|
1704 |
delete header; |
|
1705 |
||
1706 |
return r; |
|
1707 |
} |
|
1708 |
||
1709 |
||
1710 |
/** |
|
1711 |
Validate header, then adjust: |
|
1712 |
- iUncompressedSize to contain size of data even when file is not compressed. |
|
1713 |
- Platform security capability to include all disabled capabilities and exclude invalid ones. |
|
1714 |
||
1715 |
@param aFileSize Total size of the file containing the image data. |
|
1716 |
*/ |
|
1717 |
TInt E32ImageHeaderV::ValidateAndAdjust(TUint32 aFileSize) |
|
1718 |
{ |
|
1719 |
// check header is valid... |
|
1720 |
TUint32 uncompressedSize; |
|
1721 |
TInt r = ValidateHeader(aFileSize,uncompressedSize); |
|
1722 |
if(r!=KErrNone) |
|
1723 |
return r; |
|
1724 |
||
1725 |
// set size of data when uncompressed... |
|
1726 |
iUncompressedSize = uncompressedSize; |
|
1727 |
||
1728 |
// override capabilities in image to conform to system wide configuration... |
|
1729 |
for(TInt i=0; i<SCapabilitySet::ENCapW; i++) |
|
1730 |
{ |
|
1731 |
iS.iCaps[i] |= DisabledCapabilities[i]; |
|
1732 |
iS.iCaps[i] &= AllCapabilities[i]; |
|
1733 |
} |
|
1734 |
||
1735 |
return KErrNone; |
|
1736 |
} |
|
1737 |
||
1738 |
||
1739 |
TInt E32Image::Construct(RImageFinder& aFinder) |
|
1740 |
{ |
|
1741 |
__IF_DEBUG(Printf("E32Image::iMain=%08x", iMain)); |
|
1742 |
__LDRTRACE(aFinder.Dump(">E32Image::Construct", 0)); |
|
1743 |
__ASSERT_ALWAYS(aFinder.iNewValid, User::Panic(KLitFinderInconsistent, 0)); |
|
1744 |
||
1745 |
// fallback security check to ensure we don't try and load an executable from an insecure location... |
|
1746 |
if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)) |
|
1747 |
{ |
|
1748 |
__ASSERT_ALWAYS(aFinder.iNewFileName.Length()>=11, User::Panic(KLitSysBinError, 0)); |
|
1749 |
__ASSERT_ALWAYS(KSysBin().CompareF(TPtrC8(aFinder.iNewFileName.Ptr()+1,10))==0, User::Panic(KLitSysBinError, 1)); |
|
1750 |
} |
|
1751 |
||
1752 |
TInt r = KErrNone; |
|
1753 |
||
1754 |
// setup file name info... |
|
1755 |
iFileName.Copy(aFinder.iNewFileName); |
|
1756 |
TFileNameInfo fi; |
|
1757 |
fi.Set(iFileName, 0); |
|
1758 |
iRootNameOffset = fi.iBasePos; |
|
1759 |
iRootNameLength = fi.iLen - fi.iBasePos; |
|
1760 |
iExtOffset = fi.iExtPos; |
|
1761 |
||
1762 |
// setup version... |
|
1763 |
iAttr |= aFinder.iNew.iAttr & ECodeSegAttExpVer; |
|
1764 |
iModuleVersion = aFinder.iNew.iModuleVersion; |
|
1765 |
||
1766 |
if(aFinder.iNew.iRomImageHeader) |
|
1767 |
{ |
|
1768 |
// we're 'loading' an XIP executable from ROM... |
|
1769 |
Construct(*aFinder.iNew.iRomImageHeader); |
|
1770 |
if(!AlwaysLoaded() || iMain==this) |
|
1771 |
r = CheckRomXIPAlreadyLoaded(); |
|
1772 |
return r; |
|
1773 |
} |
|
1774 |
||
1775 |
// setup more image info... |
|
1776 |
iAttr |= aFinder.iNew.iAttr & (ECodeSegAttFixed|ECodeSegAttABIMask|ECodeSegAttNmdExpData); |
|
1777 |
iUids = *(const TUidType*)&aFinder.iNew.iUid; |
|
1778 |
iIsDll = !(iUids[0].iUid == KExecutableImageUidValue); |
|
1779 |
iS = aFinder.iNew.iS; |
|
1780 |
||
1781 |
// check if executable has already been loaded... |
|
1782 |
r = CheckAlreadyLoaded(); |
|
1783 |
if(r!=KErrNone) |
|
1784 |
return r; |
|
1785 |
||
1786 |
// if we are going to need to load it... |
|
1787 |
if(!iAlreadyLoaded || !iIsDll) |
|
1788 |
{ |
|
1789 |
if (aFinder.iNew.iNeedHashCheck) |
|
1790 |
{ |
|
1791 |
// we need to check the file hash; the check in RImageFinder::Try |
|
1792 |
// was skipped based on the cache. If it fails here, though, someone |
|
1793 |
// is tampering with us and we can just fail the load. |
|
1794 |
TRAP(r,aFinder.CompareHashL(aFinder.iNew, fi.DriveAndPath())); |
|
1795 |
if (r != KErrNone) |
|
1796 |
return r; |
|
1797 |
} |
|
1798 |
||
1799 |
if(aFinder.iNew.iFileData) |
|
1800 |
{ |
|
1801 |
// take ownership of the file data aFinder has already read in... |
|
1802 |
iFileData = aFinder.iNew.iFileData; |
|
1803 |
aFinder.iNew.iFileData = NULL; |
|
1804 |
iFileSize = aFinder.iNew.iFileSize; |
|
1805 |
} |
|
1806 |
else if(aFinder.iNew.FileOpened()) |
|
1807 |
{ |
|
1808 |
// take ownership of the file handle that aFinder has already opened... |
|
1809 |
iFile = aFinder.iNew.iFile; |
|
1810 |
memclr(&aFinder.iNew.iFile, sizeof(RFile)); |
|
1811 |
} |
|
1812 |
else |
|
1813 |
{ |
|
1814 |
// no resource obtained from aFinder, so create a file handle for ourselves... |
|
1815 |
r = OpenFile(); |
|
1816 |
if(r!=KErrNone) |
|
1817 |
return r; |
|
1818 |
} |
|
1819 |
||
1820 |
// take ownership of header... |
|
1821 |
iHeader = aFinder.iNew.iHeader; |
|
1822 |
aFinder.iNew.iHeader = NULL; |
|
1823 |
||
1824 |
// if there wast't a header, then create one now... |
|
1825 |
if(!iHeader) |
|
1826 |
{ |
|
1827 |
if(iFileData) |
|
1828 |
r = E32ImageHeader::New(iHeader, iFileData, iFileSize); |
|
1829 |
else |
|
1830 |
r = E32ImageHeader::New(iHeader, iFile); |
|
1831 |
if(r!=KErrNone) |
|
1832 |
return r; |
|
1833 |
} |
|
1834 |
||
1835 |
// setup info needed for process creation... |
|
1836 |
iHeapSizeMin = iHeader->iHeapSizeMin; |
|
1837 |
iHeapSizeMax = iHeader->iHeapSizeMax; |
|
1838 |
iStackSize = iHeader->iStackSize; |
|
1839 |
iPriority = iHeader->ProcessPriority(); |
|
1840 |
} |
|
1841 |
||
1842 |
// if already loaded... |
|
1843 |
if(iAlreadyLoaded) |
|
1844 |
return KErrNone; // nothing more to do |
|
1845 |
||
1846 |
// setup info needed to load an executable... |
|
1847 |
iDepCount = iHeader->iDllRefTableCount; |
|
1848 |
iExportDirCount = iHeader->iExportDirCount; |
|
1849 |
iExportDir = iHeader->iExportDirOffset-iHeader->iCodeOffset; |
|
1850 |
iTextSize = iHeader->iTextSize; |
|
1851 |
iCodeSize = iHeader->iCodeSize; |
|
1852 |
__IF_DEBUG(Printf("Code + const %x",iCodeSize)); |
|
1853 |
iDataSize = iHeader->iDataSize; |
|
1854 |
__IF_DEBUG(Printf("Data %x",iDataSize)); |
|
1855 |
iBssSize = iHeader->iBssSize; |
|
1856 |
__IF_DEBUG(Printf("Bss %x",iBssSize)); |
|
1857 |
iTotalDataSize = iDataSize+iBssSize; |
|
1858 |
||
1859 |
iFileEntryPoint = iHeader->iEntryPoint; // just an offset at this stage |
|
1860 |
iEntryPtVeneer = 0; |
|
1861 |
iExceptionDescriptor = iHeader->ExceptionDescriptor(); |
|
1862 |
if(iHeader->iExportDirOffset) |
|
1863 |
iExportDirLoad = iExportDir; // only set this if not already loaded |
|
1864 |
||
1865 |
// initialise the SMP safe flag from the image header |
|
1866 |
// this will get cleared during ProcessImports if any import is not SMP safe |
|
1867 |
if(iHeader->iFlags & KImageSMPSafe) |
|
1868 |
iAttr |= ECodeSegAttSMPSafe; |
|
1869 |
else |
|
1870 |
{ |
|
1871 |
__IF_DEBUG(Printf("%S is not marked SMP safe", &iFileName)); |
|
1872 |
iAttr &= ~ECodeSegAttSMPSafe; |
|
1873 |
} |
|
1874 |
||
1875 |
// check if executable is to be demand paged... |
|
1876 |
r = ShouldBeCodePaged(iUseCodePaging); |
|
1877 |
__IF_DEBUG(Printf("ShouldBeCodePaged r=%d,iUseCodePaging=%d", r, iUseCodePaging)); |
|
1878 |
if(iUseCodePaging==EFalse || r!=KErrNone) |
|
1879 |
return r; |
|
1880 |
||
1881 |
// image needs demand paging, create the additional information needed for this... |
|
1882 |
||
1883 |
// read compression info... |
|
1884 |
iCompressionType = iHeader->iCompressionType; |
|
1885 |
r = LoadCompressionData(); |
|
1886 |
if(r==KErrNotSupported) |
|
1887 |
{ |
|
1888 |
// Compression type not supported, so just load executable as normal, (without paging)... |
|
1889 |
iUseCodePaging = EFalse; |
|
1890 |
return KErrNone; |
|
1891 |
} |
|
1892 |
else if (r!=KErrNone) |
|
1893 |
return r; |
|
1894 |
||
1895 |
// clamp file so it doesn't get modified whilst it is being demand paged... |
|
1896 |
r = iFileClamp.Clamp(iFile); |
|
1897 |
// The clamp API will return KErrNotSupported if the media is removable: |
|
1898 |
// this implies that paging is not possible but the binary can still be loaded |
|
1899 |
if (r != KErrNone) |
|
1900 |
{ |
|
1901 |
iUseCodePaging = EFalse; |
|
1902 |
return r == KErrNotSupported ? KErrNone : r; |
|
1903 |
} |
|
1904 |
||
1905 |
// get blockmap data which indicates location of media where file contents are stored... |
|
1906 |
r = BuildCodeBlockMap(); |
|
1907 |
__IF_DEBUG(Printf("BuildCodeBlockMap r=%d", r)); |
|
1908 |
if(r==KErrNotSupported) |
|
1909 |
{ |
|
1910 |
// media doesn't support demand paging, so just load executable as normal, (without paging)... |
|
1911 |
iUseCodePaging = EFalse; |
|
1912 |
iFileClamp.Close(gTheLoaderFs); |
|
1913 |
r = KErrNone; |
|
1914 |
} |
|
1915 |
||
1916 |
return r; |
|
1917 |
} |
|
1918 |
||
1919 |
||
1920 |
TInt E32Image::CheckRomXIPAlreadyLoaded() |
|
1921 |
{ |
|
1922 |
__IF_DEBUG(Printf("ROM XIP %08x CheckAlreadyLoaded",iRomImageHeader)); |
|
1923 |
TFindCodeSeg find; |
|
1924 |
find.iRomImgHdr=iRomImageHeader; |
|
1925 |
E32Loader::CodeSegDeferDeletes(); |
|
1926 |
TAny* h=NULL; |
|
1927 |
TInt r=KErrNone; |
|
1928 |
E32Loader::CodeSegNext(h, find); |
|
1929 |
if (h) |
|
1930 |
{ |
|
1931 |
iHandle=h; |
|
1932 |
r=E32Loader::CodeSegOpen(h, iClientProcessHandle); |
|
1933 |
if (r==KErrNone) |
|
1934 |
E32Loader::CodeSegInfo(iHandle, *this); |
|
1935 |
} |
|
1936 |
E32Loader::CodeSegEndDeferDeletes(); |
|
1937 |
if (iHandle && r==KErrNone) |
|
1938 |
{ |
|
1939 |
iAlreadyLoaded=ETrue; |
|
1940 |
__IF_DEBUG(Printf("ROM XIP %08x already loaded", iHandle)); |
|
1941 |
} |
|
1942 |
__IF_DEBUG(Printf("ROM XIP CheckAlreadyLoaded returns %d",r)); |
|
1943 |
return r; |
|
1944 |
} |
|
1945 |
||
1946 |
||
1947 |
/** |
|
1948 |
Read the E32Image file into its code and data chunks, relocating them |
|
1949 |
as necessary. |
|
1950 |
Create a dll reference table from the names of dlls referenced. |
|
1951 |
Fix up the import address table and the export table for real addresses. |
|
1952 |
*/ |
|
1953 |
TInt E32Image::LoadToRam() |
|
1954 |
{ |
|
1955 |
__IF_DEBUG(Printf("E32Image::LoadToRam %S",&iFileName)); |
|
1956 |
||
1957 |
// offset of data after code which will be erad into iRestOfFileData... |
|
1958 |
iConversionOffset = iHeader->iCodeOffset + iHeader->iCodeSize; |
|
1959 |
||
1960 |
// calculate sizes... |
|
1961 |
TUint totalSize = ((E32ImageHeaderV*)iHeader)->iUncompressedSize; |
|
1962 |
TUint remainder = totalSize-iConversionOffset; |
|
1963 |
if(remainder>totalSize) |
|
1964 |
RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because header validation prevents it |
|
1965 |
||
1966 |
iRestOfFileData = (TUint8*)User::Alloc(remainder); |
|
1967 |
if(!iRestOfFileData) |
|
1968 |
return KErrNoMemory; |
|
1969 |
iRestOfFileSize = remainder; |
|
1970 |
||
1971 |
TInt r = LoadFile(); // Read everything in |
|
1972 |
if(r!=KErrNone) |
|
1973 |
return r; |
|
1974 |
||
1975 |
__IF_DEBUG(Printf("iHeader->iCodeRelocOffset %d",iHeader->iCodeRelocOffset)); |
|
1976 |
r = ((E32ImageHeaderV*)iHeader)->ValidateRelocations(iRestOfFileData,iRestOfFileSize,iHeader->iCodeRelocOffset,iHeader->iCodeSize,iCodeRelocSection); |
|
1977 |
if(r!=KErrNone) |
|
1978 |
return r; |
|
1979 |
||
1980 |
__IF_DEBUG(Printf("iHeader->iDataRelocOffset %d",iHeader->iDataRelocOffset)); |
|
1981 |
r = ((E32ImageHeaderV*)iHeader)->ValidateRelocations(iRestOfFileData,iRestOfFileSize,iHeader->iDataRelocOffset,iHeader->iDataSize,iDataRelocSection); |
|
1982 |
if(r!=KErrNone) |
|
1983 |
return r; |
|
1984 |
||
1985 |
iCodeDelta = iCodeRunAddress-iHeader->iCodeBase; |
|
1986 |
iDataDelta = iDataRunAddress-iHeader->iDataBase; |
|
1987 |
||
1988 |
if(r==KErrNone) |
|
1989 |
r = RelocateCode(); |
|
1990 |
if(r==KErrNone) |
|
1991 |
r = LoadAndRelocateData(); |
|
1992 |
if(r==KErrNone) |
|
1993 |
r = ReadImportData(); |
|
1994 |
||
1995 |
return r; |
|
1996 |
} |
|
1997 |
||
1998 |
||
1999 |
TInt E32Image::ShouldBeCodePaged(TBool& aPage) |
|
2000 |
/** |
|
2001 |
Determine whether this binary should be paged. Some of this |
|
2002 |
function is unimplemented because it requires the media pageable |
|
2003 |
attribute |
|
2004 |
||
2005 |
@param aPage On success, this variable is set to |
|
2006 |
whether the binary should be paged. Its |
|
2007 |
value is undefined if the return code is |
|
2008 |
not KErrNone. |
|
2009 |
@return Symbian OS error code. |
|
2010 |
||
2011 |
See S3.1.3.2 of PREQ1110 Design Sketch. |
|
2012 |
*/ |
|
2013 |
{ |
|
2014 |
aPage = EFalse; |
|
2015 |
||
2016 |
// kernel and global dlls can't be paged... |
|
2017 |
if(iAttr&(ECodeSegAttKernel|ECodeSegAttGlobal)) |
|
2018 |
return KErrNone; |
|
2019 |
||
2020 |
// 1. if paging policy is NOPAGING then executable is unpaged |
|
2021 |
TUint32 policy = E32Loader::PagingPolicy(); |
|
2022 |
||
2023 |
__IF_DEBUG(Printf("sbcp,policy=0x%x", policy)); |
|
2024 |
if (policy == EKernelConfigCodePagingPolicyNoPaging) |
|
2025 |
return KErrNone; |
|
2026 |
||
2027 |
// 2. if executable is on media without Pageable Media Attribute then unpaged |
|
2028 |
// 3. if executable is on removable media then unpaged |
|
2029 |
// both superseded by the BlockMap API |
|
2030 |
||
2031 |
// 3a. if executable has already been loaded into RAM for tamperproofing then |
|
2032 |
// it can't be paged |
|
2033 |
if (iFileData != NULL) |
|
2034 |
return KErrNone; |
|
2035 |
||
2036 |
// 4. if not compressed with bytepair or uncompressed then unpaged |
|
2037 |
__IF_DEBUG(Printf("sbcp,iHeader=0x%08x", iHeader)); |
|
2038 |
TUint32 comp = iHeader->CompressionType(); |
|
2039 |
__IF_DEBUG(Printf("sbcp,comp=0x%x", comp)); |
|
2040 |
if (comp != KUidCompressionBytePair && comp != KFormatNotCompressed) |
|
2041 |
return KErrNone; |
|
2042 |
||
2043 |
aPage = ETrue; |
|
2044 |
||
2045 |
// 5. if policy is ALWAYSPAGE then page |
|
2046 |
if (policy == EKernelConfigCodePagingPolicyAlwaysPage) |
|
2047 |
return KErrNone; |
|
2048 |
||
2049 |
// 6. |
|
2050 |
TUint KPagedMask = (KImageCodePaged | KImageCodeUnpaged); |
|
2051 |
TUint pagedFlags = iHeader->iFlags & KPagedMask; |
|
2052 |
__IF_DEBUG(Printf("sbcp,iHeader->iFlags=0x%x,pagedFlags=0x%x", iHeader->iFlags, pagedFlags)); |
|
2053 |
||
2054 |
// if KImageCodePaged and KImageCodeUnpaged flags present then corrupt |
|
2055 |
if (pagedFlags == KPagedMask) |
|
2056 |
RETURN_FAILURE(KErrCorrupt); |
|
2057 |
||
2058 |
// if KImageCodePaged set in executable then page |
|
2059 |
if (pagedFlags == KImageCodePaged) |
|
2060 |
return KErrNone; |
|
2061 |
||
2062 |
// if KImageCodeUnpaged set in executable then do not page |
|
2063 |
if (pagedFlags == KImageCodeUnpaged) |
|
2064 |
{ |
|
2065 |
aPage = EFalse; |
|
2066 |
return KErrNone; |
|
2067 |
} |
|
2068 |
||
2069 |
// 7. otherwise (neither paged nor unpaged set) use paging policy |
|
2070 |
||
2071 |
// policy must be EKernelConfigCodePagingPolicyDefaultUnpaged or EKernelConfigCodePagingPolicyDefaultPaged |
|
2072 |
aPage = (policy == EKernelConfigCodePagingPolicyDefaultPaged); |
|
2073 |
return KErrNone; |
|
2074 |
} |
|
2075 |
||
2076 |
TInt E32Image::BuildCodeBlockMap() |
|
2077 |
/** |
|
2078 |
Use the block map API to build an array of TBlockMapInfo |
|
2079 |
objects which the kernel can use to page in code as required. |
|
2080 |
||
2081 |
@return Symbian OS error code. KErrNotSupported means the |
|
2082 |
Block Map functionality does not support paging from |
|
2083 |
the binary's location. |
|
2084 |
*/ |
|
2085 |
{ |
|
2086 |
__IF_DEBUG(Printf("BuildCodeBlockMap,iCodeStartInFile=%d,iCodeLengthInFile=%d", iCodeStartInFile, iCodeLengthInFile)); |
|
2087 |
||
2088 |
__ASSERT_DEBUG(iUseCodePaging, Panic(EBcbmNotCodePaged)); |
|
2089 |
||
2090 |
// do nothing if no code section |
|
2091 |
if (iCodeLengthInFile == 0) |
|
2092 |
return KErrNone; |
|
2093 |
||
2094 |
// RFile::BlockMap populates an instance of this object. Need to |
|
2095 |
// retain information such as granularity which applies to all entries. |
|
2096 |
SBlockMapInfo bmi; |
|
2097 |
||
2098 |
TInt curEntriesSize = 0; |
|
2099 |
TUint8* entries8 = 0; // points to heap cell containing TBlockMapEntryBase array |
|
2100 |
||
2101 |
TInt64 bmPos = 0; |
|
2102 |
TInt64 bmEnd = iCodeStartInFile + iCodeLengthInFile; |
|
2103 |
TInt r; |
|
2104 |
do |
|
2105 |
{ |
|
2106 |
__IF_DEBUG(Printf("lfbpu:BlockMap,in,bmPos=%ld,bmEnd=%ld", bmPos, bmEnd)); |
|
2107 |
r = iFile.BlockMap(bmi, bmPos, bmEnd, EBlockMapUsagePaging); // updates bmPos to end of mapped range |
|
2108 |
__IF_DEBUG( |
|
2109 |
Printf("lfbpu:BlockMap,out,r=%d,bmPos=%ld,bmEnd=%ld,maplen=%d(%d)", |
|
2110 |
r, bmPos, bmEnd, bmi.iMap.Length(), bmi.iMap.Length() / sizeof(TBlockMapEntryBase))); |
|
2111 |
__IF_DEBUG( |
|
2112 |
Printf("lfbpu:BlockMap,out,iBlockGranularity=%u,iBlockStartOffset=%u,iStartBlockAddress=%ld,iLocalDriveNumber=%d", |
|
2113 |
bmi.iBlockGranularity, bmi.iBlockStartOffset, bmi.iStartBlockAddress, bmi.iLocalDriveNumber)); |
|
2114 |
if (r != KErrNone && r != KErrCompletion) |
|
2115 |
break; |
|
2116 |
||
2117 |
// Copy info the first time round as this gets overwritten on subsequent passes |
|
2118 |
if (curEntriesSize == 0) |
|
2119 |
iCodeBlockMapCommon = bmi; // slices the SBlockMapCommon subclass data |
|
2120 |
||
2121 |
// grow the buffer which contains the entries |
|
2122 |
TInt newEntriesSize = bmi.iMap.Length(); |
|
2123 |
TInt newArraySize = curEntriesSize + newEntriesSize; |
|
2124 |
TUint8* newEntries8 = (TUint8*) User::ReAlloc(entries8, newArraySize); |
|
2125 |
if (newEntries8 == 0) |
|
2126 |
{ |
|
2127 |
r = KErrNoMemory; |
|
2128 |
break; |
|
2129 |
} |
|
2130 |
entries8 = newEntries8; |
|
2131 |
||
2132 |
#ifdef _DEBUG |
|
2133 |
// dump the newly-returned block entries |
|
2134 |
for (TInt i = 0; i < newEntriesSize; i += sizeof(TBlockMapEntryBase)) |
|
2135 |
{ |
|
2136 |
const TBlockMapEntryBase& bme = *reinterpret_cast<const TBlockMapEntryBase*>(bmi.iMap.Ptr() + i); |
|
2137 |
__IF_DEBUG(Printf("lfbpu:bme,iNumberOfBlocks=%d,iStartBlock=%d", bme.iNumberOfBlocks, bme.iStartBlock)); |
|
2138 |
} |
|
2139 |
#endif |
|
2140 |
||
2141 |
// append the new entries to the array. |
|
2142 |
Mem::Copy(entries8 + curEntriesSize, bmi.iMap.Ptr(), newEntriesSize); |
|
2143 |
curEntriesSize = newArraySize; |
|
2144 |
} while (r != KErrCompletion); |
|
2145 |
||
2146 |
// r == KErrCompletion when mapped code section range |
|
2147 |
if (r != KErrCompletion) |
|
2148 |
{ |
|
2149 |
User::Free(entries8); |
|
2150 |
return r; |
|
2151 |
} |
|
2152 |
||
2153 |
#ifdef _DEBUG |
|
2154 |
// dump the block map table |
|
2155 |
__IF_DEBUG(Printf("lfbpu:endbme,r=%d,curEntriesSize=%d", r, curEntriesSize)); |
|
2156 |
for (TInt i = 0; i < curEntriesSize; i += 8) |
|
2157 |
{ |
|
2158 |
__IF_DEBUG(Printf( |
|
2159 |
"entries[0x%08x], %02x %02x %02x %02x %02x %02x %02x %02x", |
|
2160 |
entries8[i+0], entries8[i+1], entries8[i+2], entries8[i+3], |
|
2161 |
entries8[i+4], entries8[i+5], entries8[i+6], entries8[i+7])); |
|
2162 |
} |
|
2163 |
#endif |
|
2164 |
||
2165 |
iCodeBlockMapEntries = reinterpret_cast<TBlockMapEntryBase*>(entries8); |
|
2166 |
iCodeBlockMapEntriesSize = curEntriesSize; |
|
2167 |
||
2168 |
return KErrNone; |
|
2169 |
} |
|
2170 |
||
2171 |
||
2172 |
/** |
|
2173 |
Get the compression data relevant to demand paging |
|
2174 |
*/ |
|
2175 |
TInt E32Image::LoadCompressionData() |
|
2176 |
{ |
|
2177 |
__IF_DEBUG(Printf("E32Image::LoadCompressionData %S 0x%08x",&iFileName,iHeader->CompressionType())); |
|
2178 |
||
2179 |
TUint compression = iHeader->CompressionType(); |
|
2180 |
||
2181 |
TInt r = KErrNone; |
|
2182 |
if(compression==KFormatNotCompressed) |
|
2183 |
{ |
|
2184 |
r = LoadCompressionDataNoCompress(); |
|
2185 |
} |
|
2186 |
else if(compression==KUidCompressionBytePair) |
|
2187 |
{ |
|
2188 |
TRAP(r,LoadCompressionDataBytePairUnpakL()); |
|
2189 |
} |
|
2190 |
else |
|
2191 |
{ |
|
2192 |
r = KErrNotSupported; |
|
2193 |
} |
|
2194 |
||
2195 |
__IF_DEBUG(Printf("E32Image::LoadCompressionData exiting %S r=%d",&iFileName,r)); |
|
2196 |
return r; |
|
2197 |
} |
|
2198 |
||
2199 |
||
2200 |
TInt E32Image::LoadCompressionDataNoCompress() |
|
2201 |
{ |
|
2202 |
__IF_DEBUG(Printf("E32Image::LoadCompressionDataNoCompress %S",&iFileName)); |
|
2203 |
if (iHeader->iCodeSize) |
|
2204 |
{ |
|
2205 |
iCodeStartInFile = iHeader->iCodeOffset; |
|
2206 |
iCodeLengthInFile = iCodeSize; |
|
2207 |
} |
|
2208 |
return KErrNone; |
|
2209 |
} |
|
2210 |
||
2211 |
||
2212 |
void E32Image::LoadCompressionDataBytePairUnpakL() |
|
2213 |
{ |
|
2214 |
__IF_DEBUG(Printf("E32Image::LoadCompressionDataBytePairUnpakL %S",&iFileName)); |
|
2215 |
||
2216 |
if (iFileData) |
|
2217 |
User::Leave(KErrNotSupported); // if the file data has been loaded into RAM we can't page it! |
|
2218 |
||
2219 |
TInt pos = iHeader->TotalSize(); |
|
2220 |
User::LeaveIfError(iFile.Seek(ESeekStart,pos)); // Start at beginning of compressed data |
|
2221 |
||
2222 |
CBytePairReader* reader = CBytePairFileReader::NewLC(iFile); |
|
2223 |
||
2224 |
if (iHeader->iCodeSize) |
|
2225 |
{ |
|
2226 |
__IF_DEBUG(Printf("Code & const size %x",iCodeSize)); |
|
2227 |
__IF_DEBUG(Printf("Code & const offset %x",iHeader->iCodeOffset)); |
|
2228 |
__IF_DEBUG(Printf("Code & const dest %x",iCodeLoadAddress)); |
|
2229 |
||
2230 |
TInt pageCount; |
|
2231 |
reader->GetPageOffsetsL(pos, pageCount, iCodePageOffsets); |
|
2232 |
||
2233 |
#ifdef _DEBUG |
|
2234 |
for (TInt i = 0; i <= pageCount; ++i) |
|
2235 |
{ |
|
2236 |
__IF_DEBUG(Printf("lfbpu:raw iCodePageOffsets[%d] = %d", i, iCodePageOffsets[i])); |
|
2237 |
} |
|
2238 |
#endif |
|
2239 |
||
2240 |
// record the code start position in the file and its compressed length |
|
2241 |
// so BuildCodeBlockMap can construct a block map for the kernel if this |
|
2242 |
// file is demand paged. |
|
2243 |
iCodeStartInFile = iCodePageOffsets[0]; |
|
2244 |
iCodeLengthInFile = iCodePageOffsets[pageCount] - iCodePageOffsets[0]; |
|
2245 |
} |
|
2246 |
||
2247 |
CleanupStack::PopAndDestroy(reader); |
|
2248 |
} |
|
2249 |
||
2250 |
||
2251 |
/** |
|
2252 |
Read all image data into memory, decompressing it using the method indicated in the image header.. |
|
2253 |
If code isn't being demand paged the code part is read into #iCodeLoadAddress. |
|
2254 |
The rest of the file data after the code part is read into #iRestOfFileData. |
|
2255 |
*/ |
|
2256 |
TInt E32Image::LoadFile() |
|
2257 |
{ |
|
2258 |
__IF_DEBUG(Printf("E32Image::LoadFile %S 0x%08x",&iFileName,iHeader->CompressionType())); |
|
2259 |
||
2260 |
TUint compression = iHeader->CompressionType(); |
|
2261 |
||
2262 |
TInt r=KErrNone; |
|
2263 |
if(compression==KFormatNotCompressed) |
|
2264 |
{ |
|
2265 |
r = LoadFileNoCompress(); |
|
2266 |
CHECK_FAILURE(r); // Fuzzer can't trigger this because it only happens on file i/o error |
|
2267 |
} |
|
2268 |
else if(compression==KUidCompressionDeflate) |
|
2269 |
{ |
|
2270 |
TRAP(r,LoadFileInflateL()); |
|
2271 |
CHECK_FAILURE(r); |
|
2272 |
} |
|
2273 |
else if(compression==KUidCompressionBytePair) |
|
2274 |
{ |
|
2275 |
TRAP(r,LoadFileBytePairUnpakL()); |
|
2276 |
CHECK_FAILURE(r); |
|
2277 |
} |
|
2278 |
else |
|
2279 |
{ |
|
2280 |
r = KErrNotSupported; |
|
2281 |
CHECK_FAILURE(r); // Fuzzer can't trigger this because header validation ensures compression type is OK |
|
2282 |
} |
|
2283 |
||
2284 |
// we're done with the file contents now, free up memory before resolving imports |
|
2285 |
if(iFileData) |
|
2286 |
{ |
|
2287 |
gFileDataAllocator.Free(iFileData); |
|
2288 |
iFileData=NULL; |
|
2289 |
} |
|
2290 |
||
2291 |
__IF_DEBUG(Printf("E32Image::LoadFile exiting %S r=%d",&iFileName,r)); |
|
2292 |
return r; |
|
2293 |
} |
|
2294 |
||
2295 |
||
2296 |
/** |
|
2297 |
Read data from the image's file (or the preloaded data at #iFileData if present). |
|
2298 |
*/ |
|
2299 |
TInt E32Image::Read(TUint aPos, TUint8* aDest, TUint aSize, TBool aSvPerms) |
|
2300 |
{ |
|
2301 |
TPtr8 p(aDest,aSize,aSize); |
|
2302 |
if(iFileData) |
|
2303 |
{ |
|
2304 |
// get data from pre-loaded image data... |
|
2305 |
if(aPos+aSize>iFileSize) |
|
2306 |
RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because earlier validation prevents sizes being wrong |
|
2307 |
if (aSvPerms) |
|
2308 |
WordCopy(aDest,iFileData+aPos,aSize); |
|
2309 |
else |
|
2310 |
p.Copy(iFileData+aPos,aSize); |
|
2311 |
} |
|
2312 |
else |
|
2313 |
{ |
|
2314 |
// get data from file... |
|
2315 |
TInt r = iFile.Read(aPos,p,aSize); |
|
2316 |
if(r!=KErrNone) |
|
2317 |
return r; |
|
2318 |
} |
|
2319 |
||
2320 |
// check we got the amount of data requested... |
|
2321 |
if(TUint(p.Length())!=aSize) |
|
2322 |
{ |
|
2323 |
__IF_DEBUG(Printf("E32Image::Read() Expected:%d, read:%d", aSize, p.Length() )); |
|
2324 |
RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because requires file length to change during load |
|
2325 |
} |
|
2326 |
||
2327 |
return KErrNone; |
|
2328 |
} |
|
2329 |
||
2330 |
||
2331 |
/** |
|
2332 |
Read all image data into memory. |
|
2333 |
If code isn't being demand paged the code part is read into #iCodeLoadAddress. |
|
2334 |
The rest of the file data after the code part is read into #iRestOfFileData. |
|
2335 |
*/ |
|
2336 |
TInt E32Image::LoadFileNoCompress() |
|
2337 |
{ |
|
2338 |
__IF_DEBUG(Printf("E32Image::LoadFileNoCompress exiting %S",&iFileName)); |
|
2339 |
TInt r = KErrNone; |
|
2340 |
||
2341 |
if(iHeader->iCodeSize && !iUseCodePaging) |
|
2342 |
{ |
|
2343 |
__IF_DEBUG(Printf("Code & const size %x",iCodeSize)); |
|
2344 |
__IF_DEBUG(Printf("Code & const offset %x",iHeader->iCodeOffset)); |
|
2345 |
__IF_DEBUG(Printf("Code & const dest %x",iCodeLoadAddress)); |
|
2346 |
r = Read(iHeader->iCodeOffset, (TText8*)iCodeLoadAddress, iCodeSize, ETrue); |
|
2347 |
if(r!=KErrNone) |
|
2348 |
return r; |
|
2349 |
} |
|
2350 |
||
2351 |
if(iRestOfFileSize) |
|
2352 |
r = Read(iConversionOffset, iRestOfFileData, iRestOfFileSize); |
|
2353 |
||
2354 |
return r; |
|
2355 |
} |
|
2356 |
||
2357 |
||
2358 |
void FileCleanup(TAny* aPtr) |
|
2359 |
{ |
|
2360 |
TFileInput* f=(TFileInput*)aPtr; |
|
2361 |
f->Cancel(); |
|
2362 |
delete f; |
|
2363 |
} |
|
2364 |
||
2365 |
/** |
|
2366 |
Read all image data into memory, decompressing it using the Inflate method. |
|
2367 |
If code isn't being demand paged the code part is read into #iCodeLoadAddress. |
|
2368 |
The rest of the file data after the code part is read into #iRestOfFileData. |
|
2369 |
*/ |
|
2370 |
void E32Image::LoadFileInflateL() |
|
2371 |
{ |
|
2372 |
__IF_DEBUG(Printf("E32Image::LoadFileInflateL %S",&iFileName)); |
|
2373 |
__ASSERT_DEBUG(!iUseCodePaging, Panic(ELfiCodePagingNotSupported)); |
|
2374 |
||
2375 |
TInt pos = iHeader->TotalSize(); |
|
2376 |
TBitInput* file; |
|
2377 |
if(iFileData) |
|
2378 |
{ |
|
2379 |
if(pos < 0) |
|
2380 |
User::Leave(KErrArgument); |
|
2381 |
file = new (ELeave) TBitInput(iFileData, iFileSize*8, pos*8); |
|
2382 |
CleanupStack::PushL(file); |
|
2383 |
} |
|
2384 |
else |
|
2385 |
{ |
|
2386 |
User::LeaveIfError(iFile.Seek(ESeekStart,pos)); // Start at beginning of compressed data |
|
2387 |
file = new (ELeave) TFileInput(iFile); |
|
2388 |
CleanupStack::PushL(TCleanupItem(&FileCleanup,file)); |
|
2389 |
} |
|
2390 |
||
2391 |
CInflater* inflater=CInflater::NewLC(*file); |
|
2392 |
||
2393 |
if(iHeader->iCodeSize) |
|
2394 |
{ |
|
2395 |
__IF_DEBUG(Printf("Code & const size %x",iCodeSize)); |
|
2396 |
__IF_DEBUG(Printf("Code & const offset %x",iHeader->iCodeOffset)); |
|
2397 |
__IF_DEBUG(Printf("Code & const dest %x",iCodeLoadAddress)); |
|
2398 |
||
2399 |
TInt count = inflater->ReadL((TUint8*)iCodeLoadAddress,iCodeSize,&WordCopy); |
|
2400 |
if(count!=iCodeSize) |
|
2401 |
User::Leave(KErrCorrupt); |
|
2402 |
} |
|
2403 |
||
2404 |
if(iRestOfFileSize) |
|
2405 |
{ |
|
2406 |
TUint32 count = inflater->ReadL(iRestOfFileData,iRestOfFileSize,&Mem::Copy); |
|
2407 |
if(count!=iRestOfFileSize) |
|
2408 |
User::Leave(KErrCorrupt); |
|
2409 |
} |
|
2410 |
||
2411 |
CleanupStack::PopAndDestroy(2,file); |
|
2412 |
} |
|
2413 |
||
2414 |
||
2415 |
/** |
|
2416 |
Read all image data into memory, decompressing it using the BytePair method. |
|
2417 |
If code isn't being demand paged the code part is read into #iCodeLoadAddress. |
|
2418 |
The rest of the file data after the code part is read into #iRestOfFileData. |
|
2419 |
*/ |
|
2420 |
void E32Image::LoadFileBytePairUnpakL() |
|
2421 |
{ |
|
2422 |
__IF_DEBUG(Printf("E32Image::LoadFileBytePairUnpak %S",&iFileName)); |
|
2423 |
||
2424 |
// code starts after header |
|
2425 |
TInt pos = iHeader->TotalSize(); |
|
2426 |
||
2427 |
CBytePairReader* reader; |
|
2428 |
if(iFileData) |
|
2429 |
reader = CBytePairReader::NewLC(iFileData+pos, iFileSize-pos); |
|
2430 |
else |
|
2431 |
{ |
|
2432 |
iFile.Seek(ESeekStart, pos); |
|
2433 |
reader = CBytePairFileReader::NewLC(iFile); |
|
2434 |
} |
|
2435 |
||
2436 |
TBool codeLoaded = false; |
|
2437 |
if(iHeader->iCodeSize && !iUseCodePaging) |
|
2438 |
{ |
|
2439 |
__IF_DEBUG(Printf("Code & const size %x",iCodeSize)); |
|
2440 |
__IF_DEBUG(Printf("Code & const offset %x",iHeader->iCodeOffset)); |
|
2441 |
__IF_DEBUG(Printf("Code & const dest %x",iCodeLoadAddress)); |
|
2442 |
||
2443 |
TUint32 bytes = reader->DecompressPagesL((TUint8*)iCodeLoadAddress,iCodeSize,&WordCopy); |
|
2444 |
||
2445 |
__IF_DEBUG(Printf("bytes:%x",bytes)); |
|
2446 |
if((TInt)bytes!=iCodeSize) |
|
2447 |
User::Leave(KErrCorrupt); |
|
2448 |
||
2449 |
codeLoaded = true; |
|
2450 |
} |
|
2451 |
||
2452 |
if(iRestOfFileSize) |
|
2453 |
{ |
|
2454 |
if(!codeLoaded) |
|
2455 |
{ |
|
2456 |
// skip past code part of file... |
|
2457 |
TInt pageCount = (iCodeSize + KPageOffsetMask) >> KPageSizeShift; |
|
2458 |
||
2459 |
TInt pos = KIndexTableHeaderSize |
|
2460 |
+ pageCount * sizeof(TUint16) |
|
2461 |
+ iCodeLengthInFile; |
|
2462 |
||
2463 |
__IF_DEBUG(Printf("lfpbu:pos=%x", pos)); |
|
2464 |
reader->SeekForwardL(pos); |
|
2465 |
} |
|
2466 |
||
2467 |
__IF_DEBUG(Printf(" iRestOfFileSize==%x, iRestOfFileData==%x", iRestOfFileSize, iRestOfFileData)); |
|
2468 |
||
2469 |
TUint32 bytes = reader->DecompressPagesL(iRestOfFileData,iRestOfFileSize,NULL); |
|
2470 |
__IF_DEBUG(Printf("bytes:%x",bytes)); |
|
2471 |
if(bytes!=iRestOfFileSize) |
|
2472 |
User::Leave(KErrCorrupt); |
|
2473 |
} |
|
2474 |
||
2475 |
CleanupStack::PopAndDestroy(reader); |
|
2476 |
} |
|
2477 |
||
2478 |
||
2479 |
/** |
|
2480 |
Relocate code. |
|
2481 |
*/ |
|
2482 |
TInt E32Image::RelocateCode() |
|
2483 |
{ |
|
2484 |
if(iHeader->iExportDirOffset) |
|
2485 |
iExportDirLoad += iCodeLoadAddress; // only for RAM modules which are not already loaded |
|
2486 |
||
2487 |
__IF_DEBUG(Printf("**EntryPointVeneer %08x FileEntryPoint %08x",iEntryPtVeneer,iFileEntryPoint)); |
|
2488 |
__IF_DEBUG(Printf("**ExportDir load@%08x run@%08x",iExportDirLoad,iExportDir)); |
|
2489 |
TInt r = KErrNone; |
|
2490 |
if(iHeader->iCodeRelocOffset) |
|
2491 |
{ |
|
2492 |
__IF_DEBUG(Printf("Relocate code & const")); |
|
2493 |
||
2494 |
if(!iUseCodePaging) |
|
2495 |
r = RelocateSection(iCodeRelocSection, iCodeLoadAddress); |
|
2496 |
else |
|
2497 |
{ |
|
2498 |
r = AllocateRelocationData(iCodeRelocSection, iHeader->iCodeSize, iCodeLoadAddress, iCodeRelocTable); |
|
2499 |
iExportDirEntryDelta = iCodeDelta; // so exports get relocated |
|
2500 |
} |
|
2501 |
} |
|
2502 |
||
2503 |
if(r==KErrNone) |
|
2504 |
r = RelocateExports(); |
|
2505 |
||
2506 |
if(r==KErrNone) |
|
2507 |
{ |
|
2508 |
// put a unique ID into the third word after the entry point |
|
2509 |
||
2510 |
// address for ID... |
|
2511 |
TLinAddr csid_addr = iFileEntryPoint+KCodeSegIdOffset-iCodeRunAddress+iCodeLoadAddress; |
|
2512 |
__IF_DEBUG(Printf("csid_addr %08x", csid_addr)); |
|
2513 |
||
2514 |
// get existing ID... |
|
2515 |
TUint x; |
|
2516 |
WordCopy(&x, (const TAny*)csid_addr, sizeof(x)); |
|
2517 |
if(x==0) |
|
2518 |
{ |
|
2519 |
// generate next ID... |
|
2520 |
if(++NextCodeSegId == 0xffffffffu) |
|
2521 |
Fault(ELdrCsIdWrap); |
|
2522 |
__IF_DEBUG(Printf("NextCSID %08x", NextCodeSegId)); |
|
2523 |
// store ID... |
|
2524 |
if(!iUseCodePaging) |
|
2525 |
WordCopy((TAny*)csid_addr, &NextCodeSegId, sizeof(NextCodeSegId)); |
|
2526 |
else |
|
2527 |
{ |
|
2528 |
// demand paged code needs modifying when paged in, so add ID as a new 'fixup'... |
|
2529 |
TUint64* fixup = ExpandFixups(1); |
|
2530 |
if(!fixup) |
|
2531 |
r = KErrNoMemory; |
|
2532 |
else |
|
2533 |
*fixup = MAKE_TUINT64(csid_addr,NextCodeSegId); |
|
2534 |
} |
|
2535 |
} |
|
2536 |
} |
|
2537 |
||
2538 |
return r; |
|
2539 |
} |
|
2540 |
||
2541 |
||
2542 |
/** |
|
2543 |
Copy the data section from buffer #iRestOfFileData to the memory allocated at #iDataLoadAddress. |
|
2544 |
Then relocate this data ready for use at the executables run addresses. |
|
2545 |
*/ |
|
2546 |
TInt E32Image::LoadAndRelocateData() |
|
2547 |
{ |
|
2548 |
__IF_DEBUG(Printf("E32Image::LoadAndRelocateData %S",&iFileName)); |
|
2549 |
if(!iHeader->iDataOffset) |
|
2550 |
return KErrNone; // do data section |
|
2551 |
||
2552 |
// copy data... |
|
2553 |
__IF_DEBUG(Printf("Read Data: size %x->%08x",iDataSize,iDataLoadAddress)); |
|
2554 |
TUint32 bufferOffset=iHeader->iDataOffset-iConversionOffset; |
|
2555 |
TUint8* source=iRestOfFileData+bufferOffset; |
|
2556 |
MemCopy((TText8*)iDataLoadAddress,source,iDataSize); |
|
2557 |
||
2558 |
// relocate data... |
|
2559 |
__IF_DEBUG(Printf("Relocate data section")); |
|
2560 |
__IF_DEBUG(Printf("iDataRelocOffset %08x",iHeader->iDataRelocOffset)); |
|
2561 |
TInt r = KErrNone; |
|
2562 |
if(iHeader->iDataRelocOffset) |
|
2563 |
r = RelocateSection(iDataRelocSection, iDataLoadAddress); |
|
2564 |
||
2565 |
return r; |
|
2566 |
} |
|
2567 |
||
2568 |
||
2569 |
/** |
|
2570 |
Copies data from aDestination to aSource by running in supervisor mode. |
|
2571 |
aDest, aSource & aNumberOfBytes must be word aligned. |
|
2572 |
*/ |
|
2573 |
TUint8* E32Image::WordCopy(TAny* aDestination, const TAny* aSource, TInt aNumberOfBytes) |
|
2574 |
{ |
|
2575 |
aNumberOfBytes &= ~3; // Avoid panics for corrupt data which is not word size |
|
2576 |
SCopyDataInfo info = {aDestination,aSource, aNumberOfBytes}; |
|
2577 |
return (TUint8*) ExecuteInSupervisorMode(&svWordCopy, &info); |
|
2578 |
} |
|
2579 |
||
2580 |
||
2581 |
/** |
|
2582 |
Copies data from aDestination to aSource by running in supervisor mode. |
|
2583 |
*/ |
|
2584 |
TUint8* E32Image::MemCopy(TAny* aDestination, const TAny* aSource, TInt aNumberOfBytes) |
|
2585 |
{ |
|
2586 |
SCopyDataInfo info={aDestination,aSource, aNumberOfBytes}; |
|
2587 |
return (TUint8*) ExecuteInSupervisorMode(&svMemCopy, &info); |
|
2588 |
} |
|
2589 |
||
2590 |
||
2591 |
/** |
|
2592 |
Relocate a section, applying relocations for run addresses to values currently at their load addresses. |
|
2593 |
*/ |
|
2594 |
TInt E32Image::RelocateSection(E32RelocSection* aSection, TUint32 aLoadAddress) |
|
2595 |
{ |
|
2596 |
if(!aSection) |
|
2597 |
return KErrNone; |
|
2598 |
||
2599 |
__IF_DEBUG(Printf("Relocate: NRelocs:%08x LoadAddr:%08x", aSection->iNumberOfRelocs, aLoadAddress)); |
|
2600 |
||
2601 |
SRelocateSectionInfo info={this, (TUint8*)(aSection+1), aSection->iNumberOfRelocs, aLoadAddress}; |
|
2602 |
||
2603 |
// call function in supervisor mode to relocate the section |
|
2604 |
TInt r = ExecuteInSupervisorMode(&svRelocateSection, &info); |
|
2605 |
||
2606 |
__IF_DEBUG(Printf("Relocate returning %d",r)); |
|
2607 |
return r; |
|
2608 |
} |
|
2609 |
||
2610 |
||
2611 |
/** |
|
2612 |
Relocate the export directory for the code's run address |
|
2613 |
*/ |
|
2614 |
TInt E32Image::RelocateExports() |
|
2615 |
{ |
|
2616 |
// This only has to be done for PE-derived images, ELF marks all |
|
2617 |
// export table entries as 'relocations' so this job has already been done. |
|
2618 |
TUint impfmt = iHeader->ImportFormat(); |
|
2619 |
if (impfmt == KImageImpFmt_ELF) |
|
2620 |
return KErrNone; |
|
2621 |
||
2622 |
__IF_DEBUG(Printf("E32Image::RelocateExports %S",&iFileName)); |
|
2623 |
||
2624 |
if(iHeader->iExportDirOffset) |
|
2625 |
{ |
|
2626 |
// call function in supervisor mode to fix up export directory |
|
2627 |
ExecuteInSupervisorMode(&svRelocateExports, this); |
|
2628 |
} |
|
2629 |
return KErrNone; |
|
2630 |
} |
|
2631 |
||
2632 |
||
2633 |
/** |
|
2634 |
Validate import section data structures in iRestOfFileData. |
|
2635 |
Set iImportData to point to point to start of this. |
|
2636 |
Allocate memory (iCurrentImportList) which is big enough to store imports for a single dependency. |
|
2637 |
*/ |
|
2638 |
TInt E32Image::ReadImportData() |
|
2639 |
{ |
|
2640 |
__IF_DEBUG(Printf("E32Image::ReadImportData %S",&iFileName)); |
|
2641 |
||
2642 |
if(!iHeader->iImportOffset) |
|
2643 |
return KErrNone; |
|
2644 |
||
2645 |
TUint biggestImportCount; |
|
2646 |
TInt r = ((E32ImageHeaderV*)iHeader)->ValidateImports(iRestOfFileData,iRestOfFileSize,biggestImportCount); |
|
2647 |
if(r!=KErrNone) |
|
2648 |
return r; |
|
2649 |
||
2650 |
iImportData = (TUint32*)(iRestOfFileData+iHeader->iImportOffset-iConversionOffset); |
|
2651 |
iCurrentImportList = (TUint32*)User::Alloc(biggestImportCount * sizeof(TUint32)); |
|
2652 |
__IF_DEBUG(Printf("E32Image::ReadImportData - alloc %d current import slots at %08x", biggestImportCount, iCurrentImportList)); |
|
2653 |
if(!iCurrentImportList) |
|
2654 |
return KErrNoMemory; |
|
2655 |
||
2656 |
return KErrNone; |
|
2657 |
} |
|
2658 |
||
2659 |
||
2660 |
void E32Image::SortCurrentImportList() |
|
2661 |
{ |
|
2662 |
if (!iCurrentImportListSorted) |
|
2663 |
{ |
|
2664 |
RArray<TUint> array((TUint*)iCurrentImportList, iCurrentImportCount); |
|
2665 |
array.Sort(); |
|
2666 |
iCurrentImportListSorted = (TUint8)ETrue; |
|
2667 |
} |
|
2668 |
} |
|
2669 |
||
2670 |
||
2671 |
TInt CheckRomExports(const TRomImageHeader* aR, const E32Image* aI) |
|
2672 |
{ |
|
2673 |
__IF_DEBUG(Printf("CheckRomExports")); |
|
2674 |
if (aR->iExportDirCount == 0) |
|
2675 |
return aI->iCurrentImportCount ? KErrNotSupported : KErrNone; |
|
2676 |
const TUint32* xd = (const TUint32*)aR->iExportDir; |
|
2677 |
const TUint32* p = aI->iCurrentImportList; |
|
2678 |
const TUint32* pE = p + aI->iCurrentImportCount; |
|
2679 |
for (; p<pE; ++p) |
|
2680 |
if (xd[*p] == 0) |
|
2681 |
return KErrNotSupported; |
|
2682 |
return KErrNone; |
|
2683 |
} |
|
2684 |
||
2685 |
||
2686 |
TInt CheckRamExports(TUint aEDT, const TUint8* aED, TUint aEDC, E32Image* aI) |
|
2687 |
{ |
|
2688 |
__IF_DEBUG(Printf("CheckRamExports")); |
|
2689 |
if (aEDC == 0) |
|
2690 |
return aI->iCurrentImportCount ? KErrNotSupported : KErrNone; |
|
2691 |
if (aEDT == KImageHdr_ExpD_NoHoles) |
|
2692 |
return KErrNone; // nothing missing |
|
2693 |
||
2694 |
const TUint32* p = aI->iCurrentImportList; |
|
2695 |
const TUint32* pE = p + aI->iCurrentImportCount; |
|
2696 |
||
2697 |
if (aEDT == KImageHdr_ExpD_FullBitmap) |
|
2698 |
{ |
|
2699 |
for (; p<pE; ++p) |
|
2700 |
{ |
|
2701 |
TUint32 x = *p - 1; |
|
2702 |
if ( !(aED[x>>3] & (1u<<(x&7))) ) |
|
2703 |
return KErrNotSupported; |
|
2704 |
} |
|
2705 |
return KErrNone; |
|
2706 |
} |
|
2707 |
||
2708 |
if (aEDT != KImageHdr_ExpD_SparseBitmap8) |
|
2709 |
return KErrNotSupported; // don't know what this is |
|
2710 |
aI->SortCurrentImportList(); // sort imports to increasing order |
|
2711 |
TUint32 memsz = (aEDC + 7) >> 3; // size of complete bitmap |
|
2712 |
TUint32 mbs = (memsz + 7) >> 3; // size of meta-bitmap |
|
2713 |
const TUint8* mptr = aED; |
|
2714 |
const TUint8* gptr = mptr + mbs; |
|
2715 |
const TUint8* mptrE = mptr + mbs; |
|
2716 |
TUint xlim = 64; |
|
2717 |
for (; mptr<mptrE && p<pE; ++mptr, xlim+=64) |
|
2718 |
{ |
|
2719 |
TUint m = *mptr; |
|
2720 |
if (m==0) |
|
2721 |
{ |
|
2722 |
// nothing missing in this block of 64 exports; step to next block |
|
2723 |
for (; p<pE && *p<=xlim; ++p) {} |
|
2724 |
continue; |
|
2725 |
} |
|
2726 |
// expand this block of 64 |
|
2727 |
TUint32 g32[2] = {0xffffffffu, 0xffffffffu}; |
|
2728 |
TUint8* g = (TUint8*)g32; |
|
2729 |
for (; m; m>>=1, ++g) |
|
2730 |
if (m&1) |
|
2731 |
*g = *gptr++; |
|
2732 |
g = (TUint8*)g32; |
|
2733 |
for (; p<pE && *p<=xlim; ++p) |
|
2734 |
{ |
|
2735 |
TUint ix = *p - (xlim - 64) - 1; |
|
2736 |
if ( !(g[ix>>3] & (1u<<(ix&7))) ) |
|
2737 |
return KErrNotSupported; |
|
2738 |
} |
|
2739 |
} |
|
2740 |
return KErrNone; |
|
2741 |
} |
|
2742 |
||
2743 |
||
2744 |
TInt CheckRequiredImports(E32Image* aImporter, E32Image* aExporter, TInt aAction) |
|
2745 |
{ |
|
2746 |
__IF_DEBUG(Printf("E32Image::CheckRequiredImports (existing) %d", aAction)); |
|
2747 |
TInt last = aImporter->LastCurrentImport(); |
|
2748 |
if (last > aExporter->iExportDirCount) |
|
2749 |
return KErrNotSupported; |
|
2750 |
if (aAction == EAction_CheckLastImport) |
|
2751 |
return KErrNone; |
|
2752 |
if (aExporter->iRomImageHeader) |
|
2753 |
return CheckRomExports(aExporter->iRomImageHeader, aImporter); |
|
2754 |
if (aExporter->iHeader) |
|
2755 |
{ |
|
2756 |
E32ImageHeaderV* v = (E32ImageHeaderV*)aExporter->iHeader; |
|
2757 |
return CheckRamExports(v->iExportDescType, v->iExportDesc, v->iExportDirCount, aImporter); |
|
2758 |
} |
|
2759 |
TInt r = aExporter->ReadExportDirLoad(); |
|
2760 |
if (r != KErrNone) |
|
2761 |
return r; // could fail with OOM |
|
2762 |
TBool hasNmdExp = (aExporter->iAttr & ECodeSegAttNmdExpData); |
|
2763 |
const TUint32* p = aImporter->iCurrentImportList; |
|
2764 |
const TUint32* pE = p + aImporter->iCurrentImportCount; |
|
2765 |
const TUint32* pX = (const TUint32*)aExporter->iExportDirLoad - 1; |
|
2766 |
TUint32 xep = aExporter->iFileEntryPoint; |
|
2767 |
for (; p<pE; ++p) |
|
2768 |
{ |
|
2769 |
TUint32 x = *p; |
|
2770 |
TUint32 xx = pX[x]; |
|
2771 |
if ((xx==0 && (x!=0 || (x==0&&hasNmdExp))) || xx==xep) |
|
2772 |
return KErrNotSupported; |
|
2773 |
} |
|
2774 |
return KErrNone; |
|
2775 |
} |
|
2776 |
||
2777 |
||
2778 |
TInt CheckRequiredImports(E32Image* aImporter, const RImageInfo& aExporter, TInt aAction) |
|
2779 |
{ |
|
2780 |
__IF_DEBUG(Printf("E32Image::CheckRequiredImports (new) %d", aAction)); |
|
2781 |
TInt last = aImporter->LastCurrentImport(); |
|
2782 |
if (last > aExporter.iExportDirCount) |
|
2783 |
return KErrNotSupported; |
|
2784 |
if (aAction == EAction_CheckLastImport) |
|
2785 |
return KErrNone; |
|
2786 |
if (aExporter.iRomImageHeader) |
|
2787 |
return CheckRomExports(aExporter.iRomImageHeader, aImporter); |
|
2788 |
return CheckRamExports(aExporter.iExportDescType, aExporter.iExportDesc, aExporter.iExportDirCount, aImporter); |
|
2789 |
} |
|
2790 |
||
2791 |
||
2792 |
TInt E32Image::GetCurrentImportList(const E32ImportBlock* a) |
|
2793 |
{ |
|
2794 |
__IF_DEBUG(Printf("E32Image::GetCurrentImportList(E32ImportBlock* a:%08X)", a)); |
|
2795 |
TInt r; |
|
2796 |
TInt n = a->iNumberOfImports; |
|
2797 |
iCurrentImportCount = n; |
|
2798 |
iCurrentImportListSorted = (TUint8)EFalse; |
|
2799 |
__IF_DEBUG(Printf("iCurrentImportCount:%d, iCurrentImportListSorted:%d)", iCurrentImportCount, iCurrentImportListSorted)); |
|
2800 |
__IF_DEBUG(Printf("iHeader->ImportFormat() == KImageImpFmt_ELF:%d", (iHeader->ImportFormat() == KImageImpFmt_ELF) )); |
|
2801 |
||
2802 |
if (iHeader->ImportFormat() == KImageImpFmt_ELF) |
|
2803 |
{ |
|
2804 |
SGetImportDataInfo info; |
|
2805 |
info.iCount = n; |
|
2806 |
info.iDest = iCurrentImportList; |
|
2807 |
info.iCodeLoadAddress = iCodeLoadAddress; |
|
2808 |
info.iImportOffsetList = (TUint32*)a->Imports(); |
|
2809 |
r = ExecuteInSupervisorMode(&svElfDerivedGetImportInfo, &info); |
|
2810 |
} |
|
2811 |
else |
|
2812 |
{ |
|
2813 |
TUint32* iat = (TUint32*)(iCodeLoadAddress + iTextSize); |
|
2814 |
WordCopy(iCurrentImportList, iat + iNextImportPos, n * sizeof(TUint32)); |
|
2815 |
r = KErrNone; |
|
2816 |
} |
|
2817 |
iNextImportPos += n; |
|
2818 |
__IF_DEBUG(Printf("End of E32Image::GetCurrentImportList:%d)", r)); |
|
2819 |
return r; |
|
2820 |
} |
|
2821 |
||
2822 |
||
2823 |
TInt E32Image::LastCurrentImport() |
|
2824 |
{ |
|
2825 |
TUint32 last = 0; |
|
2826 |
if (iCurrentImportListSorted) |
|
2827 |
last = iCurrentImportList[iCurrentImportCount - 1]; |
|
2828 |
else |
|
2829 |
{ |
|
2830 |
const TUint32* p = iCurrentImportList; |
|
2831 |
const TUint32* pE = p + iCurrentImportCount; |
|
2832 |
for (; p<pE; ++p) |
|
2833 |
if (*p > last) last = *p; |
|
2834 |
} |
|
2835 |
__IF_DEBUG(Printf("E32Image::LastCurrentImport = %d", last)); |
|
2836 |
return last; |
|
2837 |
} |
|
2838 |
||
2839 |
||
2840 |
TInt E32Image::ProcessImports() |
|
2841 |
// |
|
2842 |
// This function is only ever called on the exe/dll which is loaded from |
|
2843 |
// the RProcess/RLibrary load. |
|
2844 |
// It reads this DLL/EXE's imports section and builds up a table of dlls referenced. |
|
2845 |
// It never goes recursive. |
|
2846 |
// |
|
2847 |
{ |
|
2848 |
__IF_DEBUG(Printf("E32Image::ProcessImports %S",&iFileName)); |
|
2849 |
__IF_DEBUG(Printf("DepCount=%d",iDepCount)); |
|
2850 |
||
2851 |
if (iDepCount==0 || AlwaysLoaded()) |
|
2852 |
return KErrNone; // no imports |
|
2853 |
||
2854 |
TFileNameInfo fi; |
|
2855 |
fi.Set(iFileName, 0); |
|
2856 |
gLoadeePath.Zero(); |
|
2857 |
fi.GetName(gLoadeePath, TFileNameInfo::EIncludeDrivePath); |
|
2858 |
if (PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin) |
|
2859 |
&& gLoadeePath.Length()==11 |
|
2860 |
&& KSysBin().CompareF(TPtrC8(gLoadeePath.Ptr()+1,10))==0) |
|
2861 |
{ |
|
2862 |
// Main loadee is in the default path, so unset this in order to |
|
2863 |
// search normally for dependents |
|
2864 |
gLoadeePath.Zero(); |
|
2865 |
} |
|
2866 |
#ifdef __X86__ |
|
2867 |
if (gLoadeePath.Length()>=2 && gLoadeePath[1]==':') |
|
2868 |
{ |
|
2869 |
TInt d = gLoadeePath[0]; |
|
2870 |
if (d=='a' || d=='A') |
|
2871 |
UseFloppy = EDriveA; |
|
2872 |
else if (d=='b' || d=='B') |
|
2873 |
UseFloppy = EDriveB; |
|
2874 |
} |
|
2875 |
#endif |
|
2876 |
RImageArray array; |
|
2877 |
TInt r = array.Add(this); |
|
2878 |
if (r==KErrNone) |
|
2879 |
r = LoadDlls(array); |
|
2880 |
if (r==KErrNone) |
|
2881 |
r = FixupDlls(array); |
|
2882 |
if (r==KErrNone) |
|
2883 |
r = FinaliseDlls(array); |
|
2884 |
CleanupDlls(array); |
|
2885 |
array.Close(); |
|
2886 |
||
2887 |
__IF_DEBUG(Printf("E32Image::ProcessImports returns %d",r)); |
|
2888 |
return r; |
|
2889 |
} |
|
2890 |
||
2891 |
void E32Image::CleanupDlls(RImageArray& aArray) |
|
2892 |
// |
|
2893 |
// Free the space used in fixing up the dlls. |
|
2894 |
// Don't free the entry corresponding to the main loadee. |
|
2895 |
// |
|
2896 |
{ |
|
2897 |
||
2898 |
__IF_DEBUG(Printf("CleanupDlls")); |
|
2899 |
TInt n = aArray.Count(); |
|
2900 |
TInt i; |
|
2901 |
for (i=0; i<n; ++i) |
|
2902 |
{ |
|
2903 |
E32Image* e = aArray[i]; |
|
2904 |
if (e != this) |
|
2905 |
delete e; |
|
2906 |
} |
|
2907 |
} |
|
2908 |
||
2909 |
TInt E32Image::FinaliseDlls(RImageArray& aArray) |
|
2910 |
{ |
|
2911 |
__IF_DEBUG(Printf("E32Image::FinaliseDlls")); |
|
2912 |
TInt i; |
|
2913 |
TInt c = aArray.Count(); |
|
2914 |
TInt r = KErrNone; |
|
2915 |
for(i=0; i<c && r==KErrNone; i++) |
|
2916 |
{ |
|
2917 |
E32Image* e = aArray[i]; |
|
2918 |
if(e!=this && !e->iAlreadyLoaded) |
|
2919 |
{ |
|
2920 |
// transfers ownership of clamp handle to codeseg; nulls handle if successful |
|
2921 |
if(!e->AlwaysLoaded()) |
|
2922 |
r = E32Loader::CodeSegLoaded(*e); |
|
2923 |
if(r==KErrNone && e->iUseCodePaging) |
|
2924 |
{ |
|
2925 |
e->iFileClamp.iCookie[0]=0;// null handle to indicate |
|
2926 |
e->iFileClamp.iCookie[1]=0;// transfer of ownership of clamp handle to codeseg |
|
2927 |
} |
|
2928 |
} |
|
2929 |
} |
|
2930 |
__IF_DEBUG(Printf("E32Image::FinaliseDlls returns %d",r)); |
|
2931 |
return r; |
|
2932 |
} |
|
2933 |
||
2934 |
||
2935 |
TInt E32Image::LoadDlls(RImageArray& aArray) |
|
2936 |
// |
|
2937 |
// Build a matrix of all DLLs referenced by the one we're loading, and |
|
2938 |
// ensure they're all loaded. |
|
2939 |
// |
|
2940 |
{ |
|
2941 |
__IF_DEBUG(Printf("E32Image::LoadDlls")); |
|
2942 |
TInt r=KErrNone; |
|
2943 |
E32ImportSection* importSection=(E32ImportSection *)iImportData; |
|
2944 |
E32ImportBlock* block; |
|
2945 |
if(importSection) |
|
2946 |
block=(E32ImportBlock*)(importSection+1); |
|
2947 |
else |
|
2948 |
block=NULL; |
|
2949 |
const TRomImageHeader* const * pR=NULL; |
|
2950 |
if (iRomImageHeader) |
|
2951 |
pR=iRomImageHeader->iDllRefTable->iEntry; |
|
2952 |
iNextImportPos = 0; |
|
2953 |
||
2954 |
// For each module referenced by this module |
|
2955 |
for (TInt i=0; i<iDepCount; ++i) |
|
2956 |
{ |
|
2957 |
RImageFinder finder; |
|
2958 |
E32ImportBlock* thisBlock = block; |
|
2959 |
E32Image* e = NULL; // will represent referenced module |
|
2960 |
const TRomImageHeader* rih = NULL; |
|
2961 |
RLdrReq req; // new loader request to load referenced module |
|
2962 |
TBuf8<KMaxKernelName> rootname; |
|
2963 |
req.iFileName = (HBufC8*)&rootname; |
|
2964 |
||
2965 |
if (pR) |
|
2966 |
{ |
|
2967 |
// Processing imports for ROM XIP module |
|
2968 |
rih = *pR++; |
|
2969 |
__IF_DEBUG(Printf("Importing from ROM XIP %08x", rih)); |
|
2970 |
e = aArray.Find(rih); |
|
2971 |
} |
|
2972 |
else |
|
2973 |
{ |
|
2974 |
// Processing imports for RAM module |
|
2975 |
__IF_DEBUG(Printf("Import block address %08x",block)); |
|
2976 |
TPtrC8 dllname = (const TText8*)((TUint32)iImportData + block->iOffsetOfDllName); |
|
2977 |
if (dllname.Length() > KMaxKernelName) |
|
2978 |
{ |
|
2979 |
__IF_DEBUG(Printf("Import DLL name too big: %S",&dllname)); |
|
2980 |
RETURN_FAILURE(KErrNotSupported); |
|
2981 |
} |
|
2982 |
TFileNameInfo fni; |
|
2983 |
r = fni.Set(dllname, TFileNameInfo::EAllowUid); |
|
2984 |
if (r!=KErrNone) |
|
2985 |
RETURN_FAILURE(KErrCorrupt); |
|
2986 |
fni.GetName(rootname, TFileNameInfo::EIncludeBaseExt); |
|
2987 |
TUint32* uid=(TUint32*)&req.iRequestedUids; |
|
2988 |
uid[2] = fni.Uid(); |
|
2989 |
req.iRequestedVersion = fni.Version(); |
|
2990 |
if (gLoadeePath.Length() > 0) |
|
2991 |
req.iPath = (HBufC8*)&gLoadeePath; |
|
2992 |
req.iPlatSecCaps = iS.iCaps; |
|
2993 |
req.iFileNameInfo.Set(rootname, 0); |
|
2994 |
req.iImporter = this; |
|
2995 |
r = GetCurrentImportList(block); // get list of required exports from this exporter |
|
2996 |
if (r!=KErrNone) |
|
2997 |
{ |
|
2998 |
return r; |
|
2999 |
} |
|
3000 |
TUint impfmt = iHeader->ImportFormat(); |
|
3001 |
block = (E32ImportBlock*)block->NextBlock(impfmt); |
|
3002 |
||
3003 |
r = finder.Set(req); |
|
3004 |
if (r == KErrNone) |
|
3005 |
r = finder.SearchExisting(aArray); // see what we've already got |
|
3006 |
if (r == KErrNone) |
|
3007 |
{ |
|
3008 |
TBool search = ETrue; |
|
3009 |
if (finder.iExisting) |
|
3010 |
{ |
|
3011 |
// Found an existing DLL - check for an exact version match |
|
3012 |
if (DetailedCompareVersions(finder.iCurrentVersion, finder.iReq->iRequestedVersion) <= EVersion_Exact) |
|
3013 |
search = EFalse; // if exact match, don't need to continue search |
|
3014 |
} |
|
3015 |
if (search) |
|
3016 |
r = finder.Search(); // see what else is available |
|
3017 |
} |
|
3018 |
if (r!=KErrNone) |
|
3019 |
{ |
|
3020 |
finder.Close(); |
|
3021 |
return r; |
|
3022 |
} |
|
3023 |
if (finder.iExisting) |
|
3024 |
e = finder.iExisting; // already have the required module |
|
3025 |
} |
|
3026 |
||
3027 |
// If it's already in the array, go on to the next module |
|
3028 |
if (e) |
|
3029 |
{ |
|
3030 |
__IF_DEBUG(Printf("Already there")); |
|
3031 |
} |
|
3032 |
else |
|
3033 |
{ |
|
3034 |
// Not already in the array |
|
3035 |
__IF_DEBUG(Printf("Not in array, add it")); |
|
3036 |
e = new E32Image; |
|
3037 |
if (!e) |
|
3038 |
{ |
|
3039 |
finder.Close(); |
|
3040 |
return KErrNoMemory; |
|
3041 |
} |
|
3042 |
e->iMain = iMain; |
|
3043 |
e->iClientProcessHandle = iMain->iClientProcessHandle; |
|
3044 |
if (iMain->iAttr & ECodeSegAttKernel) |
|
3045 |
e->iAttr |= ECodeSegAttKernel; |
|
3046 |
if (rih) |
|
3047 |
{ |
|
3048 |
// loading a specified ROM XIP DLL |
|
3049 |
r = e->DoLoadCodeSeg(*rih); |
|
3050 |
} |
|
3051 |
else |
|
3052 |
{ |
|
3053 |
// loading a DLL by name |
|
3054 |
r = e->DoLoadCodeSeg(req, finder); // also closes 'finder' |
|
3055 |
__IF_DEBUG(Printf("%S DoLoadCodeSeg returned %d",req.iFileName,r)); |
|
3056 |
} |
|
3057 |
||
3058 |
// Add the new entry to the array |
|
3059 |
if (r==KErrNone) |
|
3060 |
{ |
|
3061 |
__IF_DEBUG(Printf("Add to the array")); |
|
3062 |
r = aArray.Add(e); |
|
3063 |
} |
|
3064 |
if (r!=KErrNone) |
|
3065 |
{ |
|
3066 |
delete e; |
|
3067 |
return r; |
|
3068 |
} |
|
3069 |
||
3070 |
// Now go nice and recursive, and call LoadDlls on this latest dll, if it |
|
3071 |
// imports anything |
|
3072 |
// This recursive horror *will* terminate because it is only called |
|
3073 |
// on "new" dlls |
|
3074 |
if (e->iDepCount && !e->iAlreadyLoaded && e->iIsDll) |
|
3075 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3076 |
__IF_DEBUG(Printf("****Going recursive****")); |
0 | 3077 |
r = e->LoadDlls(aArray); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3078 |
__IF_DEBUG(Printf("****Returned from recursion****")); |
0 | 3079 |
if (r!=KErrNone) |
3080 |
{ |
|
3081 |
return r; |
|
3082 |
} |
|
3083 |
} |
|
3084 |
||
3085 |
} |
|
3086 |
||
3087 |
// If we added an SMP unsafe dependent, this image is SMP unsafe. |
|
3088 |
// This is done after recursing into LoadDlls, so a single unsafe |
|
3089 |
// dependent anywhere down the tree will poison everything above it. |
|
3090 |
// This isn't sufficient to deal with cycles, though, so the kernel |
|
3091 |
// also has to update the flag in DCodeSeg::FinaliseRecursiveFlags. |
|
3092 |
// It has to be done here first because the kernel doesn't know |
|
3093 |
// about XIP DLLs that don't have a codeseg created. |
|
3094 |
if (!(e->iAttr & ECodeSegAttSMPSafe)) |
|
3095 |
{ |
|
3096 |
__IF_DEBUG(Printf("%S is not SMP safe because it loads %S", &iFileName, &e->iFileName)); |
|
3097 |
iAttr &= ~ECodeSegAttSMPSafe; |
|
3098 |
} |
|
3099 |
||
3100 |
// If exporter is an EXE it must be the same as the client process or newly created process |
|
3101 |
__IF_DEBUG(Printf("Check EXE->EXE")); |
|
3102 |
if (gExeCodeSeg && !e->iIsDll && e->iHandle!=gExeCodeSeg) |
|
3103 |
return KErrNotSupported; |
|
3104 |
||
3105 |
// A globally-visible module may only link to other globally visible modules |
|
3106 |
__IF_DEBUG(Printf("Check Global Attribute")); |
|
3107 |
if ( (iAttr&ECodeSegAttGlobal) && !(e->iAttr&ECodeSegAttGlobal) ) |
|
3108 |
return KErrNotSupported; |
|
3109 |
||
3110 |
// A ram-loaded globally-visible module may only link to ROM XIP modules with no static data |
|
3111 |
__IF_DEBUG(Printf("Check RAM Global")); |
|
3112 |
if ( (iAttr&ECodeSegAttGlobal) && !iRomImageHeader && e->iHandle) |
|
3113 |
return KErrNotSupported; |
|
3114 |
||
3115 |
if (thisBlock) |
|
3116 |
thisBlock->iOffsetOfDllName=(TUint32)e; // For easy access when fixing up imports |
|
3117 |
if (e->iHandle) |
|
3118 |
{ |
|
3119 |
// Record the dependence of this on e |
|
3120 |
r=E32Loader::CodeSegAddDependency(iHandle, e->iHandle); |
|
3121 |
if (r!=KErrNone) |
|
3122 |
{ |
|
3123 |
return r; |
|
3124 |
} |
|
3125 |
} |
|
3126 |
} |
|
3127 |
__IF_DEBUG(Printf("E32Image::LoadDlls OK")); |
|
3128 |
return KErrNone; |
|
3129 |
} |
|
3130 |
||
3131 |
||
3132 |
TInt E32Image::ReadExportDirLoad() |
|
3133 |
{ |
|
3134 |
// Get the exporter's export directory |
|
3135 |
__IF_DEBUG(Printf("ReadExportDirLoad exp_dir=%08x", iExportDirLoad)); |
|
3136 |
if (!iExportDirLoad) |
|
3137 |
{ |
|
3138 |
// already loaded nonglobal DLL - must read the export directory |
|
3139 |
if (iExportDirCount==0 && !(iAttr&ECodeSegAttNmdExpData)) |
|
3140 |
return KErrGeneral; // DLL has no exports, something must be wrong |
|
3141 |
iCopyOfExportDir = (TUint32*)User::Alloc((iExportDirCount+1) * sizeof(TUint32)); |
|
3142 |
if (!iCopyOfExportDir) |
|
3143 |
return KErrNoMemory; |
|
3144 |
__IF_DEBUG(Printf("Reading %d exports", iExportDirCount)); |
|
3145 |
E32Loader::ReadExportDir(iHandle, iCopyOfExportDir); |
|
3146 |
iExportDirLoad = (TUint32)(iCopyOfExportDir+1); |
|
3147 |
} |
|
3148 |
return KErrNone; |
|
3149 |
} |
|
3150 |
||
3151 |
||
3152 |
TInt E32Image::FixupDlls(RImageArray& aArray) |
|
3153 |
// |
|
3154 |
// Go through the array, fixing up the files |
|
3155 |
// |
|
3156 |
{ |
|
3157 |
__IF_DEBUG(Printf("E32Image::FixupDlls")); |
|
3158 |
||
3159 |
// For each E32Image file in the array |
|
3160 |
TInt i; |
|
3161 |
TInt c = aArray.Count(); |
|
3162 |
||
3163 |
for (i=0; i<c; ++i) |
|
3164 |
{ |
|
3165 |
TInt r; |
|
3166 |
||
3167 |
E32Image* imp = aArray[i]; |
|
3168 |
__IF_DEBUG(Printf("Dll number %d %S",i,&imp->iFileName)); |
|
3169 |
||
3170 |
const E32ImportSection* importSection = (const E32ImportSection*)imp->iImportData; |
|
3171 |
if (!importSection) |
|
3172 |
{ |
|
3173 |
__IF_DEBUG(Printf("Has no imports to fixup")); |
|
3174 |
continue; // No imports, skip this dll (true of ALL ROM dlls) |
|
3175 |
} |
|
3176 |
||
3177 |
const E32ImportBlock* block = (const E32ImportBlock*)(importSection + 1); |
|
3178 |
||
3179 |
SFixupImportAddressesInfo info; |
|
3180 |
info.iIat = (TUint32*)(imp->iCodeLoadAddress + imp->iTextSize); |
|
3181 |
info.iCodeLoadAddress = imp->iCodeLoadAddress; |
|
3182 |
||
3183 |
// fix up imports from each dependent DLL, building a table of all the imports for the binary |
|
3184 |
TInt depCount = imp->iDepCount; |
|
3185 |
while (depCount--) |
|
3186 |
{ |
|
3187 |
// declare variables at start of loop body to prevent 'crosses initialization' errors |
|
3188 |
TUint impfmt; |
|
3189 |
||
3190 |
// E32Image::LoadDlls() will have set iOffsetOfDllName of the |
|
3191 |
// import block to point to the E32Image object of the exporter |
|
3192 |
// it's importing |
|
3193 |
E32Image* exp = (E32Image*)(block->iOffsetOfDllName); // LoadDlls() set this to exporter |
|
3194 |
||
3195 |
// Get the exporter's export directory |
|
3196 |
r = exp->ReadExportDirLoad(); |
|
3197 |
if (r != KErrNone) |
|
3198 |
return r; |
|
3199 |
info.iNumImports = block->iNumberOfImports; |
|
3200 |
info.iExporter = exp; |
|
3201 |
||
3202 |
// if demand paging, expand the import fixup buffer for this next exporting DLL |
|
3203 |
if (! imp->iUseCodePaging) |
|
3204 |
info.iFixup64 = 0; |
|
3205 |
else |
|
3206 |
{ |
|
3207 |
info.iFixup64 = imp->ExpandFixups(block->iNumberOfImports); |
|
3208 |
if (!info.iFixup64) |
|
3209 |
return KErrNoMemory; |
|
3210 |
} |
|
3211 |
||
3212 |
// call function in supervisor mode to fix up the import addresses. |
|
3213 |
impfmt = imp->iHeader->ImportFormat(); |
|
3214 |
if (impfmt == KImageImpFmt_ELF) |
|
3215 |
{ |
|
3216 |
info.iImportOffsetList = (TUint32*)(block+1); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3217 |
__IF_DEBUG(Printf("Import format ELF (%08x); info@%08x", impfmt, &info)); |
0 | 3218 |
r = ExecuteInSupervisorMode(&svElfDerivedFixupImportAddresses, &info); |
3219 |
} |
|
3220 |
else |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3221 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3222 |
__IF_DEBUG(Printf("Import format PE (%08x); info@%08x", impfmt, &info)); |
0 | 3223 |
r = ExecuteInSupervisorMode(&svFixupImportAddresses, &info); |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3224 |
} |
0 | 3225 |
|
3226 |
if (r != KErrNone) |
|
3227 |
{ |
|
3228 |
__IF_DEBUG(Printf("svFixupImportAddresses returns %d", r)); |
|
3229 |
return r; |
|
3230 |
} |
|
3231 |
||
3232 |
// Next import block... |
|
3233 |
block = block->NextBlock(impfmt); |
|
3234 |
} // while (depCount--) |
|
3235 |
||
3236 |
if (imp->iUseCodePaging && imp->iFixupCount > 0) |
|
3237 |
{ |
|
3238 |
// convert the <addr,val> pairs to an import fixup tab which can be used when |
|
3239 |
// the code is paged. |
|
3240 |
r = imp->BuildImportFixupTable(); |
|
3241 |
if (r != KErrNone) |
|
3242 |
return r; |
|
3243 |
} |
|
3244 |
} |
|
3245 |
||
3246 |
__IF_DEBUG(Printf("E32Image::FixupDlls OK")); |
|
3247 |
return KErrNone; |
|
3248 |
} |
|
3249 |
||
3250 |
||
3251 |
/** |
|
3252 |
This function is defined because RArray does not natively support |
|
3253 |
sorting 64-bit integers. |
|
3254 |
||
3255 |
It is used by FixupDlls to order the import fixup locations in the image |
|
3256 |
so they can be organized by page. |
|
3257 |
||
3258 |
@param aLeft 64-bit unsigned integer to compare against aRight. |
|
3259 |
@param aRight 64-bit unsigned integer to compare against aLeft. |
|
3260 |
@return -1 if aLeft < aRight; 0 if aLeft == aRight; and |
|
3261 |
+1 if aLeft > aRight. This conforms to the behavior |
|
3262 |
which is expected from a function used by TLinearOrder. |
|
3263 |
*/ |
|
3264 |
static TInt Uint64LinearOrderFunc(const TUint64& aLeft, const TUint64& aRight) |
|
3265 |
{ |
|
3266 |
if (aLeft < aRight) |
|
3267 |
return -1; |
|
3268 |
else if (aLeft > aRight) |
|
3269 |
return 1; |
|
3270 |
else |
|
3271 |
return 0; |
|
3272 |
} |
|
3273 |
||
3274 |
||
3275 |
TUint64* E32Image::ExpandFixups(TInt aNumFixups) |
|
3276 |
{ |
|
3277 |
__IF_DEBUG(Printf("ExpandFixups,%d+%d", iFixupCount,aNumFixups)); |
|
3278 |
TInt newCount = iFixupCount+aNumFixups; |
|
3279 |
TUint64* fixups = (TUint64*) User::ReAlloc(iFixups, sizeof(TUint64) * newCount); |
|
3280 |
if(!fixups) |
|
3281 |
return 0; |
|
3282 |
TUint64* newFixups = fixups+iFixupCount; |
|
3283 |
iFixupCount = newCount; |
|
3284 |
iFixups = fixups; |
|
3285 |
return newFixups; |
|
3286 |
} |
|
3287 |
||
3288 |
||
3289 |
/** |
|
3290 |
Helper function for FixupImports. Takes the set of |
|
3291 |
64-bit <addr,val> fixups, and organizes them into pages. |
|
3292 |
||
3293 |
Each page is stored as fXXX YYYY ZZZZ where YYYY ZZZZ is written |
|
3294 |
to the word at offset XXX. (See PREQ1110 Design Sketch v1.0 S3.1.1.2.3.2.) |
|
3295 |
||
3296 |
On success iImportFixupTableSize is set to the table size in bytes, |
|
3297 |
and iImportFixupTable is a cell containing the table. |
|
3298 |
||
3299 |
@return Symbian OS error code. |
|
3300 |
*/ |
|
3301 |
TInt E32Image::BuildImportFixupTable() |
|
3302 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3303 |
__IF_DEBUG(Printf(">BuildImportFixupTable,%d@%08x,%08x", iFixupCount, iFixups, iCodeLoadAddress)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3304 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3305 |
#ifdef _DEBUG |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3306 |
// Dump the incoming fixup table if loader tracing enabled. Each item is an |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3307 |
// (address, value) pair, where the address and the value are 32 bits each. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3308 |
TInt i; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3309 |
for (i = 0; i < iFixupCount; ++i) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3310 |
{ |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3311 |
TUint64 x = iFixups[i]; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3312 |
__IF_DEBUG(Printf("%04x: %08x %08x", i*sizeof(TUint64), I64HIGH(x), I64LOW(x))); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3313 |
} |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3314 |
#endif // DEBUG |
0 | 3315 |
|
3316 |
// sort the array in address order, to organize by page |
|
3317 |
RArray<TUint64> fixup64ToSort(sizeof(TUint64), iFixups, iFixupCount); |
|
3318 |
// SortUnsigned doesn't work on TUint64 |
|
3319 |
fixup64ToSort.Sort(TLinearOrder<TUint64>(Uint64LinearOrderFunc)); |
|
3320 |
||
3321 |
// now have <address | new-value> pairs, organize into pages. |
|
3322 |
// Each page is stored as fXXX YYYY ZZZZ where YYYY ZZZZ is written |
|
3323 |
// to the word at offset XXX. (See PREQ1110 Design Sketch v1.0 S3.1.1.2.3.2.) |
|
3324 |
||
3325 |
TUint32 pageCount = SizeToPageCount(iCodeSize); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3326 |
iImportFixupTableSize = (pageCount+1)*sizeof(TUint32) + 3*iFixupCount*sizeof(TUint16); |
0 | 3327 |
iImportFixupTable = (TUint32*) User::Alloc(iImportFixupTableSize); |
3328 |
__IF_DEBUG(Printf("iImportFixupTable=0x%08x", iImportFixupTable)); |
|
3329 |
if (iImportFixupTable == 0) |
|
3330 |
return KErrNoMemory; |
|
3331 |
||
3332 |
// byte offsets of pages into the table are written as 32-bit words at |
|
3333 |
// the start of the table |
|
3334 |
||
3335 |
TUint32 lastPage = 0; |
|
3336 |
// byte index of first 48-bit entry in the table, after sentinel index |
|
3337 |
iImportFixupTable[0] = (pageCount + 1) * sizeof(TUint32);; |
|
3338 |
||
3339 |
// location to which 48-bit imports are written |
|
3340 |
TUint16* importOffset = (TUint16*)(iImportFixupTable + pageCount + 1); |
|
3341 |
||
3342 |
// location from where 64-bit <addr,val> pairs are read |
|
3343 |
const TUint64* avEnd = iFixups + iFixupCount; |
|
3344 |
||
3345 |
for (const TUint64* avPtr = iFixups; avPtr < avEnd; ++avPtr) |
|
3346 |
{ |
|
3347 |
TUint64 addr_val = *avPtr; |
|
3348 |
TUint32 addr = I64HIGH(addr_val) - iCodeLoadAddress; |
|
3349 |
TUint32 page = addr >> 12; |
|
3350 |
if (page > lastPage) |
|
3351 |
{ |
|
3352 |
// calculate new start index for current page |
|
3353 |
TUint32 newStart = TUint32(importOffset) - TUint32(iImportFixupTable); |
|
3354 |
||
3355 |
__IF_DEBUG(Printf("page=%d, lastPage=%d, newStart=0x%08x", page, lastPage, newStart)); |
|
3356 |
||
3357 |
// mark intermediate pages as zero-length, starting and ending at |
|
3358 |
// current offset |
|
3359 |
while (++lastPage <= page) |
|
3360 |
iImportFixupTable[lastPage] = newStart; |
|
3361 |
--lastPage; |
|
3362 |
} |
|
3363 |
||
3364 |
TUint16 offsetIntoPage; |
|
3365 |
offsetIntoPage = (addr & KPageOffsetMask); |
|
3366 |
*importOffset++ = offsetIntoPage; |
|
3367 |
||
3368 |
TUint32 val = I64LOW(addr_val); |
|
3369 |
*importOffset++ = val; // low halfword stored first (YYYY) |
|
3370 |
*importOffset++ = val >> 16; // high halfword stored second (ZZZZ) |
|
3371 |
} |
|
3372 |
||
3373 |
// sentinel value marks end of table |
|
3374 |
while (++lastPage <= pageCount) |
|
3375 |
iImportFixupTable[lastPage] = iImportFixupTableSize; |
|
3376 |
||
3377 |
#ifdef _DEBUG |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3378 |
__IF_DEBUG(Printf("processed fixup table (size=%d,pageCount=%d)", iImportFixupTableSize, pageCount)); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3379 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3380 |
// Dump the processed fixup table if loader tracing enabled. The dump is in two |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3381 |
// parts; first, the page indexes (1 word per page), then the entries describing |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3382 |
// the items to be relocated, each of which is a 16-bit offset-within-page and a |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3383 |
// 32-bit value to be stored there. |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3384 |
for (i = 0; i <= (TInt)pageCount; ++i) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3385 |
__IF_DEBUG(Printf("%04x: %08x", i*4, iImportFixupTable[i])); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3386 |
|
0 | 3387 |
const TUint16* table16 = (const TUint16*)iImportFixupTable; |
3388 |
const TInt halfWordsInTable = iImportFixupTableSize / 2; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3389 |
for (i *= 2; i < halfWordsInTable; i += 3) |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3390 |
__IF_DEBUG(Printf("%04x: %04x %04x%04x", i*2, table16[i+0], table16[i+2], table16[i+1])); |
0 | 3391 |
#endif |
3392 |
||
3393 |
User::Free(iFixups); |
|
3394 |
iFixups = 0; |
|
3395 |
return KErrNone; |
|
3396 |
} |
|
3397 |
||
3398 |
||
3399 |
TInt GetModuleInfo(RLdrReq& aReq) |
|
3400 |
// |
|
3401 |
// Read capabilities from file found |
|
3402 |
// |
|
3403 |
{ |
|
3404 |
__IF_DEBUG(Printf("ReadModuleInfo %S",aReq.iFileName)); |
|
3405 |
TFileNameInfo& fi = aReq.iFileNameInfo; |
|
3406 |
RImageFinder finder; |
|
3407 |
TInt r = finder.Set(aReq); |
|
3408 |
if (r == KErrNone) |
|
3409 |
{ |
|
3410 |
finder.iFindExact = ETrue; |
|
3411 |
||
3412 |
r = KErrNotSupported; |
|
3413 |
||
3414 |
// must specify a fully qualified name |
|
3415 |
if (fi.DriveLen() && fi.PathLen()) |
|
3416 |
{ |
|
3417 |
if (fi.VerLen()) |
|
3418 |
aReq.iRequestedVersion = fi.iVersion; |
|
3419 |
else |
|
3420 |
aReq.iRequestedVersion = KModuleVersionWild; |
|
3421 |
r = finder.Search(); |
|
3422 |
if (r == KErrNone) |
|
3423 |
{ |
|
3424 |
RLibrary::TInfo ret_info; |
|
3425 |
memclr(&ret_info,sizeof(ret_info)); |
|
3426 |
ret_info.iModuleVersion = finder.iNew.iModuleVersion; |
|
3427 |
ret_info.iUids = *(const TUidType*)finder.iNew.iUid; |
|
3428 |
*(SSecurityInfo*)&ret_info.iSecurityInfo = finder.iNew.iS; |
|
3429 |
TPckgC<RLibrary::TInfo> ret_pckg(ret_info); |
|
3430 |
r = aReq.iMsg->Write(2, ret_pckg); |
|
3431 |
} |
|
3432 |
} |
|
3433 |
} |
|
3434 |
finder.Close(); |
|
3435 |
return r; |
|
3436 |
} |
|
3437 |
||
3438 |
TInt GetInfoFromHeader(const RLoaderMsg& aMsg) |
|
3439 |
{ |
|
3440 |
TInt r; |
|
3441 |
||
3442 |
// Get size of header supplied by client |
|
3443 |
TInt size; |
|
3444 |
size = aMsg.GetDesLength(0); |
|
3445 |
if(size<0) |
|
3446 |
return size; |
|
3447 |
if(size>RLibrary::KRequiredImageHeaderSize) |
|
3448 |
size = RLibrary::KRequiredImageHeaderSize; |
|
3449 |
if((TUint)size<sizeof(E32ImageHeaderV)) |
|
3450 |
return KErrUnderflow; |
|
3451 |
||
3452 |
// Get header data |
|
3453 |
TUint8* data = new TUint8[size]; |
|
3454 |
if(!data) |
|
3455 |
return KErrNoMemory; |
|
3456 |
TPtr8 ptr(data,size); |
|
3457 |
r = aMsg.Read(0,ptr); |
|
3458 |
if(r==KErrNone) |
|
3459 |
{ |
|
3460 |
// Check header is valid |
|
3461 |
E32ImageHeaderV* header=(E32ImageHeaderV*)data; |
|
3462 |
if(header->TotalSize()>size) |
|
3463 |
r = KErrUnderflow; |
|
3464 |
else |
|
3465 |
{ |
|
3466 |
TUint32 uncompressedSize; |
|
3467 |
r = header->ValidateHeader(-1,uncompressedSize); |
|
3468 |
} |
|
3469 |
if(r==KErrNone) |
|
3470 |
{ |
|
3471 |
// Get info |
|
3472 |
RLibrary::TInfoV2 ret_info; |
|
3473 |
memclr(&ret_info,sizeof(ret_info)); |
|
3474 |
ret_info.iModuleVersion = header->ModuleVersion(); |
|
3475 |
ret_info.iUids = (TUidType&)header->iUid1; |
|
3476 |
header->GetSecurityInfo((SSecurityInfo&)ret_info.iSecurityInfo); |
|
3477 |
ret_info.iHardwareFloatingPoint = (header->iFlags & KImageHWFloatMask) >> KImageHWFloatShift; |
|
3478 |
||
3479 |
ret_info.iDebugAttributes = 0; // default |
|
3480 |
if (header->iFlags & KImageDebuggable) |
|
3481 |
ret_info.iDebugAttributes |= RLibrary::TInfoV2::EDebugAllowed; |
|
3482 |
||
3483 |
TPckg<RLibrary::TInfoV2> ret_pckg(ret_info); |
|
3484 |
TInt max = aMsg.GetDesMaxLength(1); |
|
3485 |
if (ret_pckg.Length() > max) |
|
3486 |
ret_pckg.SetLength(max); |
|
3487 |
r = aMsg.Write(1, ret_pckg); |
|
3488 |
} |
|
3489 |
} |
|
3490 |
||
3491 |
delete[] data; |
|
3492 |
return r; |
|
3493 |
} |
|
3494 |
||
3495 |
#if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
3496 |
void memory_dump(const TAny* a, TUint l) |
|
3497 |
{ |
|
3498 |
TBuf8<80> buf; |
|
3499 |
const TUint8* s = (const TUint8*)a; |
|
3500 |
TInt n=0; |
|
3501 |
while (l) |
|
3502 |
{ |
|
3503 |
buf.Append(' '); |
|
3504 |
buf.AppendNumFixedWidth(*s++, EHex, 2); |
|
3505 |
--l; |
|
3506 |
++n; |
|
3507 |
if (l==0 || n==16) |
|
3508 |
{ |
|
3509 |
RDebug::Printf((const char*)buf.PtrZ()); |
|
3510 |
buf.Zero(); |
|
3511 |
n=0; |
|
3512 |
} |
|
3513 |
} |
|
3514 |
} |
|
3515 |
||
3516 |
void RImageFinder::Dump(const char* aTitle, TInt aR) |
|
3517 |
{ |
|
3518 |
RDebug::Printf(aTitle); |
|
3519 |
RDebug::Printf("r=%d",aR); |
|
3520 |
if (iExisting) |
|
3521 |
{ |
|
3522 |
RDebug::Printf("Existing image found"); |
|
3523 |
RDebug::Printf("Filename=%S Attr=%08x", &iExisting->iFileName, iExisting->iAttr); |
|
3524 |
RDebug::Printf("SID %08x Caps %08x %08x", iExisting->iS.iSecureId, iExisting->iS.iCaps[1], iExisting->iS.iCaps[0]); |
|
3525 |
const TUint32* uid = (const TUint32*)&iExisting->iUids; |
|
3526 |
RDebug::Printf("UIDs %08x %08x %08x VER %08x", uid[0], uid[1], uid[2], iExisting->iModuleVersion); |
|
3527 |
RDebug::Printf("Rom %08x", iExisting->iRomImageHeader); |
|
3528 |
} |
|
3529 |
else if (iNewValid) |
|
3530 |
{ |
|
3531 |
RDebug::Printf("New image found"); |
|
3532 |
RDebug::Printf("Filename=%S Attr=%08x", &iNewFileName, iNew.iAttr); |
|
3533 |
RDebug::Printf("SID %08x Caps %08x %08x", iNew.iS.iSecureId, iNew.iS.iCaps[1], iNew.iS.iCaps[0]); |
|
3534 |
const TUint32* uid = (const TUint32*)iNew.iUid; |
|
3535 |
RDebug::Printf("UIDs %08x %08x %08x VER %08x", uid[0], uid[1], uid[2], iNew.iModuleVersion); |
|
3536 |
RDebug::Printf("Rom %08x", iNew.iRomImageHeader); |
|
3537 |
} |
|
3538 |
else |
|
3539 |
{ |
|
3540 |
RDebug::Printf("No suitable image found"); |
|
3541 |
RDebug::Printf("#NM=%d #UidFail=%d #CapFail=%d #MajVFail=%d #ImpFail=%d", iNameMatches, iUidFail, iCapFail, iMajorVersionFail, iImportFail); |
|
3542 |
} |
|
3543 |
} |
|
3544 |
||
3545 |
void DumpImageHeader(const E32ImageHeader* a) |
|
3546 |
{ |
|
3547 |
RDebug::Printf("E32ImageHeader at %08x :", a); |
|
3548 |
TUint abi = a->ABI(); |
|
3549 |
TUint hdrfmt = a->HeaderFormat(); |
|
3550 |
TUint impfmt = a->ImportFormat(); |
|
3551 |
TUint eptfmt = a->EntryPointFormat(); |
|
3552 |
RDebug::Printf("Header format %d", hdrfmt>>KImageHdrFmtShift); |
|
3553 |
RDebug::Printf("Import format %d", impfmt>>KImageImpFmtShift); |
|
3554 |
RDebug::Printf("EntryPoint format %d", eptfmt>>KImageEptShift); |
|
3555 |
RDebug::Printf("ABI %d", abi>>KImageABIShift); |
|
3556 |
RDebug::Printf("UIDs %08x %08x %08x (%08x)", a->iUid1, a->iUid2, a->iUid3, a->iUidChecksum); |
|
3557 |
RDebug::Printf("Header CRC %08x", a->iHeaderCrc); |
|
3558 |
RDebug::Printf("Signature %08x", a->iSignature); |
|
3559 |
RDebug::Printf("CPU %08x", (TUint)a->CpuIdentifier()); |
|
3560 |
RDebug::Printf("ModuleVersion %08x", a->ModuleVersion()); |
|
3561 |
RDebug::Printf("Compression Type %08x", a->CompressionType()); |
|
3562 |
RDebug::Printf("Tools Version %d.%02d(%d)", a->iToolsVersion.iMajor, a->iToolsVersion.iMinor, a->iToolsVersion.iBuild); |
|
3563 |
RDebug::Printf("Flags %08x", a->iFlags); |
|
3564 |
RDebug::Printf("Code Size %08x", a->iCodeSize); |
|
3565 |
RDebug::Printf("Text Size %08x", a->iTextSize); |
|
3566 |
RDebug::Printf("Data Size %08x", a->iDataSize); |
|
3567 |
RDebug::Printf("BSS Size %08x", a->iBssSize); |
|
3568 |
RDebug::Printf("Stack Size %08x", a->iStackSize); |
|
3569 |
RDebug::Printf("HeapSizeMin %08x", a->iHeapSizeMin); |
|
3570 |
RDebug::Printf("HeapSizeMax %08x", a->iHeapSizeMax); |
|
3571 |
RDebug::Printf("iEntryPoint %08x", a->iEntryPoint); |
|
3572 |
RDebug::Printf("iCodeBase %08x", a->iCodeBase); |
|
3573 |
RDebug::Printf("iDataBase %08x", a->iDataBase); |
|
3574 |
RDebug::Printf("DLL Ref Table Count %d", a->iDllRefTableCount); |
|
3575 |
RDebug::Printf("Export Dir Count %d", a->iExportDirCount); |
|
3576 |
RDebug::Printf("Code Offset %08x", a->iCodeOffset); |
|
3577 |
RDebug::Printf("Data Offset %08x", a->iDataOffset); |
|
3578 |
RDebug::Printf("Code Reloc Offset %08x", a->iCodeRelocOffset); |
|
3579 |
RDebug::Printf("Data Reloc Offset %08x", a->iDataRelocOffset); |
|
3580 |
RDebug::Printf("Import Offset %08x", a->iImportOffset); |
|
3581 |
RDebug::Printf("Export Dir Offset %08x", a->iExportDirOffset); |
|
3582 |
RDebug::Printf("Priority %d", (TUint)a->ProcessPriority()); |
|
3583 |
// KImageHdrFmt_J |
|
3584 |
RDebug::Printf("iUncompressedSize %08x", ((E32ImageHeaderComp*)a)->iUncompressedSize); |
|
3585 |
// KImageHdrFmt_V |
|
3586 |
E32ImageHeaderV* v = (E32ImageHeaderV*)a; |
|
3587 |
RDebug::Printf("SID %08x VID %08x CAP %08x %08x", v->iS.iSecureId, v->iS.iVendorId, v->iS.iCaps[1], v->iS.iCaps[0]); |
|
3588 |
RDebug::Printf("iExportDescType %02x", v->iExportDescType); |
|
3589 |
RDebug::Printf("iExportDescSize %04x", v->iExportDescSize); |
|
3590 |
if (v->iExportDescSize) |
|
3591 |
memory_dump(v->iExportDesc, v->iExportDescSize); |
|
3592 |
} |
|
3593 |
#endif |
|
3594 |