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