williamr@2
|
1 |
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
|
williamr@2
|
2 |
// All rights reserved.
|
williamr@2
|
3 |
// This component and the accompanying materials are made available
|
williamr@2
|
4 |
// under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
|
williamr@2
|
5 |
// which accompanies this distribution, and is available
|
williamr@2
|
6 |
// at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
|
williamr@2
|
7 |
//
|
williamr@2
|
8 |
// Initial Contributors:
|
williamr@2
|
9 |
// Nokia Corporation - initial contribution.
|
williamr@2
|
10 |
//
|
williamr@2
|
11 |
// Contributors:
|
williamr@2
|
12 |
//
|
williamr@2
|
13 |
// Description:
|
williamr@2
|
14 |
// f32\inc\f32image.h
|
williamr@2
|
15 |
//
|
williamr@2
|
16 |
//
|
williamr@2
|
17 |
|
williamr@2
|
18 |
|
williamr@2
|
19 |
|
williamr@2
|
20 |
/**
|
williamr@2
|
21 |
@file f32\inc\f32image.h
|
williamr@2
|
22 |
@internalTechnology
|
williamr@2
|
23 |
*/
|
williamr@2
|
24 |
|
williamr@2
|
25 |
#ifndef __F32IMAGE_H__
|
williamr@2
|
26 |
#define __F32IMAGE_H__
|
williamr@2
|
27 |
#include <e32cmn.h>
|
williamr@2
|
28 |
|
williamr@2
|
29 |
/**
|
williamr@2
|
30 |
Value used for E32ImageHeader::iCpuIdentifier.
|
williamr@2
|
31 |
*/
|
williamr@2
|
32 |
enum TCpu
|
williamr@2
|
33 |
{
|
williamr@2
|
34 |
ECpuUnknown=0, ECpuX86=0x1000, ECpuArmV4=0x2000, ECpuArmV5=0x2001, ECpuArmV6=0x2002, ECpuMCore=0x4000
|
williamr@2
|
35 |
};
|
williamr@2
|
36 |
|
williamr@2
|
37 |
/**
|
williamr@2
|
38 |
Ordinal value of the first entry in an executables export directory.
|
williamr@2
|
39 |
@see E32ImageHeader::iExportDirOffset.
|
williamr@2
|
40 |
*/
|
williamr@2
|
41 |
const TInt KOrdinalBase=1;
|
williamr@2
|
42 |
|
williamr@2
|
43 |
/**
|
williamr@2
|
44 |
Value used to initialise E32ImageHeader::iHeaderCrc prior to CRC generation.
|
williamr@2
|
45 |
*/
|
williamr@2
|
46 |
const TUint32 KImageCrcInitialiser = 0xc90fdaa2u;
|
williamr@2
|
47 |
|
williamr@2
|
48 |
|
williamr@2
|
49 |
/**
|
williamr@2
|
50 |
Byte offset from an executable's entrypoint to the code segment ID storage location.
|
williamr@2
|
51 |
*/
|
williamr@2
|
52 |
const TUint KCodeSegIdOffset = 12;
|
williamr@2
|
53 |
|
williamr@2
|
54 |
//
|
williamr@2
|
55 |
// Flags fields for E32ImageHeader::iFlags
|
williamr@2
|
56 |
//
|
williamr@2
|
57 |
|
williamr@2
|
58 |
const TUint KImageDll = 0x00000001u; ///< Flag set if executable is a DLL, clear if an EXE.
|
williamr@2
|
59 |
|
williamr@2
|
60 |
const TUint KImageNoCallEntryPoint = 0x00000002u; ///< Obsolete flag ignored since Symbian OS version 8.1b.
|
williamr@2
|
61 |
|
williamr@2
|
62 |
const TUint KImageFixedAddressExe = 0x00000004u; ///< Executable's data should not move when running on the moving memory model.
|
williamr@2
|
63 |
|
williamr@2
|
64 |
const TUint KImageABIMask = 0x00000018u; ///< Bitmask for ABI value.
|
williamr@2
|
65 |
const TInt KImageABIShift = 3; ///< Bit shift count for ABI value.
|
williamr@2
|
66 |
const TUint KImageABI_GCC98r2 = 0x00000000u; ///< Obsolete ABI for ARM targets.
|
williamr@2
|
67 |
const TUint KImageABI_EABI = 0x00000008u; ///< ARM EABI
|
williamr@2
|
68 |
|
williamr@2
|
69 |
const TUint KImageEptMask = 0x000000e0u; ///< Bitmask for Entrypoint value.
|
williamr@2
|
70 |
const TInt KImageEptShift = 5; ///< Bit shift count for Entrypoint value
|
williamr@2
|
71 |
const TUint KImageEpt_Eka1 = 0x00000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b.
|
williamr@2
|
72 |
const TUint KImageEpt_Eka2 = 0x00000020u; ///< Standard entrypoint for ARM executable.
|
williamr@2
|
73 |
|
williamr@2
|
74 |
const TUint KImageUnpaged = 0x00000100u; ///< Executable image should not be demand paged. Exclusive with KImagePaged,
|
williamr@2
|
75 |
const TUint KImagePaged = 0x00000200u; ///< Executable image should be demand paged. Exclusive with KImageUnpaged,
|
williamr@2
|
76 |
|
williamr@2
|
77 |
const TUint KImageNmdExpData = 0x00000400u; ///< Flag to indicate when named symbol export data present in image
|
williamr@2
|
78 |
|
williamr@2
|
79 |
const TUint KImageDebuggable = 0x00000800u; ///< Flag to indicate image is debuggable
|
williamr@2
|
80 |
|
williamr@2
|
81 |
const TUint KImageHWFloatMask = 0x00f00000u; ///< Bitmask for Floating Point type.
|
williamr@2
|
82 |
const TInt KImageHWFloatShift = 20; ///< Bit shift count for Floating Point type.
|
williamr@2
|
83 |
const TUint KImageHWFloat_None = EFpTypeNone << KImageHWFloatShift; ///< No hardware floating point used.
|
williamr@2
|
84 |
const TUint KImageHWFloat_VFPv2 = EFpTypeVFPv2 << KImageHWFloatShift; ///< ARM VFPv2 floating point used.
|
williamr@2
|
85 |
|
williamr@2
|
86 |
const TUint KImageHdrFmtMask = 0x0f000000u; ///< Bitmask for header format type.
|
williamr@2
|
87 |
const TInt KImageHdrFmtShift = 24; ///< Bit shift count for header format type.
|
williamr@2
|
88 |
const TUint KImageHdrFmt_Original = 0x00000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b.
|
williamr@2
|
89 |
const TUint KImageHdrFmt_J = 0x01000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b.
|
williamr@2
|
90 |
const TUint KImageHdrFmt_V = 0x02000000u; ///< Header has format given by class E32ImageHeaderV.
|
williamr@2
|
91 |
|
williamr@2
|
92 |
const TUint KImageImpFmtMask = 0xf0000000u; ///< Bitmask for import section format type.
|
williamr@2
|
93 |
const TInt KImageImpFmtShift = 28; ///< Bit shift count for import section format type.
|
williamr@2
|
94 |
const TUint KImageImpFmt_PE = 0x00000000u; ///< PE-derived imports.
|
williamr@2
|
95 |
const TUint KImageImpFmt_ELF = 0x10000000u; ///< ELF-derived imports.
|
williamr@2
|
96 |
const TUint KImageImpFmt_PE2 = 0x20000000u; ///< PE-derived imports without redundant copy of import ordinals.
|
williamr@2
|
97 |
|
williamr@2
|
98 |
|
williamr@2
|
99 |
|
williamr@2
|
100 |
|
williamr@2
|
101 |
// forward references...
|
williamr@2
|
102 |
class RFile;
|
williamr@2
|
103 |
class E32RelocSection;
|
williamr@2
|
104 |
|
williamr@2
|
105 |
|
williamr@2
|
106 |
/**
|
williamr@2
|
107 |
Structure for an executable image's header.
|
williamr@2
|
108 |
This is extended by E32ImageHeaderComp and E32ImageHeaderV.
|
williamr@2
|
109 |
All executables since Symbian OS version 8.1b have an header given by class E32ImageHeaderV.
|
williamr@2
|
110 |
|
williamr@2
|
111 |
Summary of an executable image structure...
|
williamr@2
|
112 |
|
williamr@2
|
113 |
- Header, 0..iCodeOffset-1
|
williamr@2
|
114 |
- Code part, iCodeOffset..iCodeOffset+iCodeSize-1
|
williamr@2
|
115 |
- .text section, 0 + iTextSize
|
williamr@2
|
116 |
- Import Address Table (IAT), iText + ?
|
williamr@2
|
117 |
- Export Directory, iExportDirOffset + iExportDirCount*4 (in .text Section)
|
williamr@2
|
118 |
- Rest of data, iCodeOffset+iCodeSize..EOF
|
williamr@2
|
119 |
- .data section, iDataOffset + iDataSize
|
williamr@2
|
120 |
- Import section, iImportOffset + sizeof(E32ImportSection)+?
|
williamr@2
|
121 |
- Code relocation section, iCodeRelocOffset + sizeof(E32RelocSection)+?
|
williamr@2
|
122 |
- Data relocation section, iDataRelocOffset + sizeof(E32RelocSection)+?
|
williamr@2
|
123 |
*/
|
williamr@2
|
124 |
class E32ImageHeader
|
williamr@2
|
125 |
{
|
williamr@2
|
126 |
public:
|
williamr@2
|
127 |
static TInt New(E32ImageHeader*& aHdr, RFile& aFile);
|
williamr@2
|
128 |
static TInt New(E32ImageHeader*& aHdr, TUint8* aFileData, TUint32 aFileSize);
|
williamr@2
|
129 |
TInt ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const;
|
williamr@2
|
130 |
|
williamr@2
|
131 |
inline static TUint ABIFromFlags(TUint aFlags);
|
williamr@2
|
132 |
inline static TUint EptFromFlags(TUint aFlags);
|
williamr@2
|
133 |
inline static TUint HdrFmtFromFlags(TUint aFlags);
|
williamr@2
|
134 |
inline static TUint ImpFmtFromFlags(TUint aFlags);
|
williamr@2
|
135 |
|
williamr@2
|
136 |
inline TUint ABI() const;
|
williamr@2
|
137 |
inline TUint EntryPointFormat() const;
|
williamr@2
|
138 |
inline TUint HeaderFormat() const;
|
williamr@2
|
139 |
inline TUint ImportFormat() const;
|
williamr@2
|
140 |
|
williamr@2
|
141 |
inline TUint32 CompressionType() const;
|
williamr@2
|
142 |
inline TUint32 ModuleVersion() const;
|
williamr@2
|
143 |
inline TInt TotalSize() const;
|
williamr@2
|
144 |
inline TInt UncompressedFileSize() const;
|
williamr@2
|
145 |
inline void GetSecurityInfo(SSecurityInfo& aInfo) const;
|
williamr@2
|
146 |
inline TCpu CpuIdentifier() const;
|
williamr@2
|
147 |
inline TProcessPriority ProcessPriority() const;
|
williamr@2
|
148 |
inline TUint32 ExceptionDescriptor() const;
|
williamr@2
|
149 |
public:
|
williamr@2
|
150 |
TUint32 iUid1; ///< KDynamicLibraryUidValue or KExecutableImageUidValue
|
williamr@2
|
151 |
TUint32 iUid2; ///< Second UID for executable.
|
williamr@2
|
152 |
TUint32 iUid3; ///< Third UID for executable.
|
williamr@2
|
153 |
TUint32 iUidChecksum; ///< Checksum for iUid1, iUid2 and iUid3.
|
williamr@2
|
154 |
TUint iSignature; ///< Contains 'EPOC'.
|
williamr@2
|
155 |
TUint32 iHeaderCrc; ///< CRC-32 of entire header. @see #KImageCrcInitialiser.
|
williamr@2
|
156 |
TUint32 iModuleVersion; ///< Version number for this executable (used in link resolution).
|
williamr@2
|
157 |
TUint32 iCompressionType; ///< Type of compression used for file contents located after the header. (UID or 0 for none).
|
williamr@2
|
158 |
TVersion iToolsVersion; ///< Version number of tools which generated this file.
|
williamr@2
|
159 |
TUint32 iTimeLo; ///< Least significant 32 bits of the time of image creation, in milliseconds since since midnight Jan 1st, 2000.
|
williamr@2
|
160 |
TUint32 iTimeHi; ///< Most significant 32 bits of the time of image creation, in milliseconds since since midnight Jan 1st, 2000.
|
williamr@2
|
161 |
TUint iFlags; ///< Contains various bit-fields of attributes for the image.
|
williamr@2
|
162 |
TInt iCodeSize; ///< Size of executables code. Includes import address table, constant data and export directory.
|
williamr@2
|
163 |
TInt iDataSize; ///< Size of executables initialised data.
|
williamr@2
|
164 |
TInt iHeapSizeMin; ///< Minimum size for an EXEs runtime heap memory.
|
williamr@2
|
165 |
TInt iHeapSizeMax; ///< Maximum size for an EXEs runtime heap memory.
|
williamr@2
|
166 |
TInt iStackSize; ///< Size for stack required by an EXEs initial thread.
|
williamr@2
|
167 |
TInt iBssSize; ///< Size of executables uninitialised data.
|
williamr@2
|
168 |
TUint iEntryPoint; ///< Offset into code of the entry point.
|
williamr@2
|
169 |
TUint iCodeBase; ///< Virtual address that the executables code is linked for.
|
williamr@2
|
170 |
TUint iDataBase; ///< Virtual address that the executables data is linked for.
|
williamr@2
|
171 |
TInt iDllRefTableCount; ///< Number of executable against which this executable is linked. The number of files mention in the import section at iImportOffset.
|
williamr@2
|
172 |
TUint iExportDirOffset; ///< Byte offset into file of the export directory.
|
williamr@2
|
173 |
TInt iExportDirCount; ///< Number of entries in the export directory.
|
williamr@2
|
174 |
TInt iTextSize; ///< Size of just the text section, also doubles as the offset for the Import Address Table w.r.t. the code section.
|
williamr@2
|
175 |
TUint iCodeOffset; ///< Offset into file of the code section. Also doubles the as header size.
|
williamr@2
|
176 |
TUint iDataOffset; ///< Offset into file of the data section.
|
williamr@2
|
177 |
TUint iImportOffset; ///< Offset into file of the import section (E32ImportSection).
|
williamr@2
|
178 |
TUint iCodeRelocOffset; ///< Offset into file of the code relocation section (E32RelocSection).
|
williamr@2
|
179 |
TUint iDataRelocOffset; ///< Offset into file of the data relocation section (E32RelocSection).
|
williamr@2
|
180 |
TUint16 iProcessPriority; ///< Initial runtime process priorty for an EXE. (Value from enum TProcessPriority.)
|
williamr@2
|
181 |
TUint16 iCpuIdentifier; ///< Value from enum TCpu which indicates the CPU architecture for which the image was created
|
williamr@2
|
182 |
};
|
williamr@2
|
183 |
|
williamr@2
|
184 |
|
williamr@2
|
185 |
/**
|
williamr@2
|
186 |
Extends E32ImageHeader.
|
williamr@2
|
187 |
*/
|
williamr@2
|
188 |
class E32ImageHeaderComp : public E32ImageHeader
|
williamr@2
|
189 |
{
|
williamr@2
|
190 |
public:
|
williamr@2
|
191 |
TUint32 iUncompressedSize; ///< Uncompressed size of file data after the header, or zero if file not compressed.
|
williamr@2
|
192 |
};
|
williamr@2
|
193 |
|
williamr@2
|
194 |
|
williamr@2
|
195 |
/**
|
williamr@2
|
196 |
Extends E32ImageHeaderComp.
|
williamr@2
|
197 |
All Symbian OS executable files have a header in this format since OS version 8.1b.
|
williamr@2
|
198 |
*/
|
williamr@2
|
199 |
class E32ImageHeaderV : public E32ImageHeaderComp
|
williamr@2
|
200 |
{
|
williamr@2
|
201 |
public:
|
williamr@2
|
202 |
SSecurityInfo iS; ///< Platform Security information of executable.
|
williamr@2
|
203 |
TUint32 iExceptionDescriptor; ///< Offset in bytes from start of code section to Exception Descriptor, bit 0 set if valid.
|
williamr@2
|
204 |
TUint32 iSpare2; ///< Reserved for future use. Set to zero.
|
williamr@2
|
205 |
TUint16 iExportDescSize; ///< Size of export description stored in iExportDesc.
|
williamr@2
|
206 |
TUint8 iExportDescType; ///< Type of description of holes in export table
|
williamr@2
|
207 |
TUint8 iExportDesc[1]; ///< Description of holes in export table, size given by iExportDescSize..
|
williamr@2
|
208 |
public:
|
williamr@2
|
209 |
TInt ValidateWholeImage(TAny* aBufferStart, TUint aBufferSize) const;
|
williamr@2
|
210 |
TInt ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const;
|
williamr@2
|
211 |
TInt ValidateExportDescription() const;
|
williamr@2
|
212 |
TInt ValidateRelocations(TAny* aBufferStart, TUint aBufferSize, TUint aRelocationInfoOffset, TUint aRelocatedSectionSize, E32RelocSection*& aRelocationSection) const;
|
williamr@2
|
213 |
TInt ValidateImports(TAny* aBufferStart, TUint aBufferSize, TUint& aBiggestImportCount) const;
|
williamr@2
|
214 |
TInt ValidateAndAdjust(TUint32 aFileSize);
|
williamr@2
|
215 |
};
|
williamr@2
|
216 |
|
williamr@2
|
217 |
// export description type E32ImageHeaderV::iExportDescType
|
williamr@2
|
218 |
const TUint KImageHdr_ExpD_NoHoles =0x00; ///< No holes, all exports present.
|
williamr@2
|
219 |
const TUint KImageHdr_ExpD_FullBitmap =0x01; ///< Full bitmap present at E32ImageHeaderV::iExportDesc
|
williamr@2
|
220 |
const TUint KImageHdr_ExpD_SparseBitmap8 =0x02; ///< Sparse bitmap present at E32ImageHeaderV::iExportDesc, granularity 8
|
williamr@2
|
221 |
const TUint KImageHdr_ExpD_Xip =0xff; ///< XIP file
|
williamr@2
|
222 |
|
williamr@2
|
223 |
|
williamr@2
|
224 |
//
|
williamr@2
|
225 |
// inline getters for E32ImageHeader
|
williamr@2
|
226 |
//
|
williamr@2
|
227 |
|
williamr@2
|
228 |
/**
|
williamr@2
|
229 |
Extract ABI type from aFlags.
|
williamr@2
|
230 |
*/
|
williamr@2
|
231 |
inline TUint E32ImageHeader::ABIFromFlags(TUint aFlags)
|
williamr@2
|
232 |
{
|
williamr@2
|
233 |
return aFlags & KImageABIMask;
|
williamr@2
|
234 |
}
|
williamr@2
|
235 |
|
williamr@2
|
236 |
/**
|
williamr@2
|
237 |
Extract ABI type from #iFlags.
|
williamr@2
|
238 |
*/
|
williamr@2
|
239 |
inline TUint E32ImageHeader::ABI() const
|
williamr@2
|
240 |
{
|
williamr@2
|
241 |
return ABIFromFlags(iFlags);
|
williamr@2
|
242 |
}
|
williamr@2
|
243 |
|
williamr@2
|
244 |
/**
|
williamr@2
|
245 |
Extract entrypoint format from aFlags.
|
williamr@2
|
246 |
*/
|
williamr@2
|
247 |
inline TUint E32ImageHeader::EptFromFlags(TUint aFlags)
|
williamr@2
|
248 |
{
|
williamr@2
|
249 |
return aFlags & KImageEptMask;
|
williamr@2
|
250 |
}
|
williamr@2
|
251 |
|
williamr@2
|
252 |
/**
|
williamr@2
|
253 |
Extract entrypoint format from #iFlags.
|
williamr@2
|
254 |
*/
|
williamr@2
|
255 |
inline TUint E32ImageHeader::EntryPointFormat() const
|
williamr@2
|
256 |
{
|
williamr@2
|
257 |
return EptFromFlags(iFlags);
|
williamr@2
|
258 |
}
|
williamr@2
|
259 |
|
williamr@2
|
260 |
/**
|
williamr@2
|
261 |
Extract header format from aFlags.
|
williamr@2
|
262 |
*/
|
williamr@2
|
263 |
inline TUint E32ImageHeader::HdrFmtFromFlags(TUint aFlags)
|
williamr@2
|
264 |
{
|
williamr@2
|
265 |
return aFlags & KImageHdrFmtMask;
|
williamr@2
|
266 |
}
|
williamr@2
|
267 |
|
williamr@2
|
268 |
/**
|
williamr@2
|
269 |
Extract header format from #iFlags.
|
williamr@2
|
270 |
*/
|
williamr@2
|
271 |
inline TUint E32ImageHeader::HeaderFormat() const
|
williamr@2
|
272 |
{
|
williamr@2
|
273 |
return HdrFmtFromFlags(iFlags);
|
williamr@2
|
274 |
}
|
williamr@2
|
275 |
|
williamr@2
|
276 |
/**
|
williamr@2
|
277 |
Extract import format from aFlags.
|
williamr@2
|
278 |
*/
|
williamr@2
|
279 |
inline TUint E32ImageHeader::ImpFmtFromFlags(TUint aFlags)
|
williamr@2
|
280 |
{
|
williamr@2
|
281 |
return aFlags & KImageImpFmtMask;
|
williamr@2
|
282 |
}
|
williamr@2
|
283 |
|
williamr@2
|
284 |
/**
|
williamr@2
|
285 |
Extract import format from #iFlags.
|
williamr@2
|
286 |
*/
|
williamr@2
|
287 |
inline TUint E32ImageHeader::ImportFormat() const
|
williamr@2
|
288 |
{
|
williamr@2
|
289 |
return ImpFmtFromFlags(iFlags);
|
williamr@2
|
290 |
}
|
williamr@2
|
291 |
|
williamr@2
|
292 |
/**
|
williamr@2
|
293 |
Return #iCompressionType.
|
williamr@2
|
294 |
*/
|
williamr@2
|
295 |
inline TUint32 E32ImageHeader::CompressionType() const
|
williamr@2
|
296 |
{
|
williamr@2
|
297 |
return iCompressionType;
|
williamr@2
|
298 |
}
|
williamr@2
|
299 |
|
williamr@2
|
300 |
/**
|
williamr@2
|
301 |
Return #iModuleVersion.
|
williamr@2
|
302 |
*/
|
williamr@2
|
303 |
inline TUint32 E32ImageHeader::ModuleVersion() const
|
williamr@2
|
304 |
{
|
williamr@2
|
305 |
return iModuleVersion;
|
williamr@2
|
306 |
}
|
williamr@2
|
307 |
|
williamr@2
|
308 |
/**
|
williamr@2
|
309 |
Return size of this header.
|
williamr@2
|
310 |
*/
|
williamr@2
|
311 |
inline TInt E32ImageHeader::TotalSize() const
|
williamr@2
|
312 |
{
|
williamr@2
|
313 |
return iCodeOffset;
|
williamr@2
|
314 |
}
|
williamr@2
|
315 |
|
williamr@2
|
316 |
/**
|
williamr@2
|
317 |
Return total size of file after decompression, or -1 if file not compressed.
|
williamr@2
|
318 |
*/
|
williamr@2
|
319 |
inline TInt E32ImageHeader::UncompressedFileSize() const
|
williamr@2
|
320 |
{
|
williamr@2
|
321 |
if(iCompressionType==0)
|
williamr@2
|
322 |
return -1; // not compressed
|
williamr@2
|
323 |
else
|
williamr@2
|
324 |
return ((E32ImageHeaderComp*)this)->iUncompressedSize + TotalSize();
|
williamr@2
|
325 |
}
|
williamr@2
|
326 |
|
williamr@2
|
327 |
/**
|
williamr@2
|
328 |
Return copy of security info, #E32ImageHeaderV::iS.
|
williamr@2
|
329 |
*/
|
williamr@2
|
330 |
inline void E32ImageHeader::GetSecurityInfo(SSecurityInfo& aInfo) const
|
williamr@2
|
331 |
{
|
williamr@2
|
332 |
aInfo = ((E32ImageHeaderV*)this)->iS;
|
williamr@2
|
333 |
}
|
williamr@2
|
334 |
|
williamr@2
|
335 |
/**
|
williamr@2
|
336 |
Return #iCpuIdentifier.
|
williamr@2
|
337 |
*/
|
williamr@2
|
338 |
inline TCpu E32ImageHeader::CpuIdentifier() const
|
williamr@2
|
339 |
{
|
williamr@2
|
340 |
return (TCpu)iCpuIdentifier;
|
williamr@2
|
341 |
}
|
williamr@2
|
342 |
|
williamr@2
|
343 |
/**
|
williamr@2
|
344 |
Return #iProcessPriority.
|
williamr@2
|
345 |
*/
|
williamr@2
|
346 |
inline TProcessPriority E32ImageHeader::ProcessPriority() const
|
williamr@2
|
347 |
{
|
williamr@2
|
348 |
return (TProcessPriority)iProcessPriority;
|
williamr@2
|
349 |
}
|
williamr@2
|
350 |
|
williamr@2
|
351 |
/**
|
williamr@2
|
352 |
Return fffset in bytes from start of code section for the Exception Descriptor.
|
williamr@2
|
353 |
Or zero if not present.
|
williamr@2
|
354 |
*/
|
williamr@2
|
355 |
inline TUint32 E32ImageHeader::ExceptionDescriptor() const
|
williamr@2
|
356 |
{
|
williamr@2
|
357 |
TUint32 xd = ((E32ImageHeaderV*)this)->iExceptionDescriptor;
|
williamr@2
|
358 |
|
williamr@2
|
359 |
if((xd & 1) && (xd != 0xffffffffu))
|
williamr@2
|
360 |
return (xd & ~1);
|
williamr@2
|
361 |
|
williamr@2
|
362 |
return 0;
|
williamr@2
|
363 |
}
|
williamr@2
|
364 |
|
williamr@2
|
365 |
|
williamr@2
|
366 |
/**
|
williamr@2
|
367 |
A block of imports from a single executable.
|
williamr@2
|
368 |
These structures are conatined in a images Import Section (E32ImportSection).
|
williamr@2
|
369 |
*/
|
williamr@2
|
370 |
class E32ImportBlock
|
williamr@2
|
371 |
{
|
williamr@2
|
372 |
public:
|
williamr@2
|
373 |
inline const E32ImportBlock* NextBlock(TUint aImpFmt) const;
|
williamr@2
|
374 |
inline TInt Size(TUint aImpFmt) const;
|
williamr@2
|
375 |
inline const TUint* Imports() const; // import list if present
|
williamr@2
|
376 |
public:
|
williamr@2
|
377 |
TUint32 iOffsetOfDllName; ///< Offset from start of import section for a NUL terminated executable (DLL or EXE) name.
|
williamr@2
|
378 |
TInt iNumberOfImports; ///< Number of imports from this executable.
|
williamr@2
|
379 |
// TUint iImport[iNumberOfImports]; ///< For ELF-derived executes: list of code section offsets. For PE, list of imported ordinals. Omitted in PE2 import format
|
williamr@2
|
380 |
};
|
williamr@2
|
381 |
|
williamr@2
|
382 |
/**
|
williamr@2
|
383 |
Return size of this import block.
|
williamr@2
|
384 |
@param aImpFmt Import format as obtained from image header.
|
williamr@2
|
385 |
*/
|
williamr@2
|
386 |
inline TInt E32ImportBlock::Size(TUint aImpFmt) const
|
williamr@2
|
387 |
{
|
williamr@2
|
388 |
TInt r = sizeof(E32ImportBlock);
|
williamr@2
|
389 |
if(aImpFmt!=KImageImpFmt_PE2)
|
williamr@2
|
390 |
r += iNumberOfImports * sizeof(TUint);
|
williamr@2
|
391 |
return r;
|
williamr@2
|
392 |
}
|
williamr@2
|
393 |
|
williamr@2
|
394 |
/**
|
williamr@2
|
395 |
Return pointer to import block which immediately follows this one.
|
williamr@2
|
396 |
@param aImpFmt Import format as obtained from image header.
|
williamr@2
|
397 |
*/
|
williamr@2
|
398 |
inline const E32ImportBlock* E32ImportBlock::NextBlock(TUint aImpFmt) const
|
williamr@2
|
399 |
{
|
williamr@2
|
400 |
const E32ImportBlock* next = this + 1;
|
williamr@2
|
401 |
if(aImpFmt!=KImageImpFmt_PE2)
|
williamr@2
|
402 |
next = (const E32ImportBlock*)( (TUint8*)next + iNumberOfImports * sizeof(TUint) );
|
williamr@2
|
403 |
return next;
|
williamr@2
|
404 |
}
|
williamr@2
|
405 |
|
williamr@2
|
406 |
/**
|
williamr@2
|
407 |
Return address of first import in this block.
|
williamr@2
|
408 |
For import format KImageImpFmt_ELF, imports are list of code section offsets.
|
williamr@2
|
409 |
For import format KImageImpFmt_PE, imports are a list of imported ordinals.
|
williamr@2
|
410 |
For import format KImageImpFmt_PE2, the import list is not present and should not be accessed.
|
williamr@2
|
411 |
*/
|
williamr@2
|
412 |
inline const TUint* E32ImportBlock::Imports() const
|
williamr@2
|
413 |
{
|
williamr@2
|
414 |
return (const TUint*)(this + 1);
|
williamr@2
|
415 |
}
|
williamr@2
|
416 |
|
williamr@2
|
417 |
|
williamr@2
|
418 |
/**
|
williamr@2
|
419 |
Header for the Import Section in an image, as referenced by E32ImageHeader::iImportOffset.
|
williamr@2
|
420 |
Immediately following this structure are an array of E32ImportBlock structures.
|
williamr@2
|
421 |
The number of these is given by E32ImageHeader::iDllRefTableCount.
|
williamr@2
|
422 |
*/
|
williamr@2
|
423 |
class E32ImportSection
|
williamr@2
|
424 |
{
|
williamr@2
|
425 |
public:
|
williamr@2
|
426 |
TInt iSize; ///< Size of this section excluding 'this' structure
|
williamr@2
|
427 |
// E32ImportBlock iImportBlock[iDllRefTableCount];
|
williamr@2
|
428 |
};
|
williamr@2
|
429 |
|
williamr@2
|
430 |
|
williamr@2
|
431 |
/**
|
williamr@2
|
432 |
A block of relocations for a single page (4kB) of code/data.
|
williamr@2
|
433 |
|
williamr@2
|
434 |
Immediately following this structure are an array of TUint16 values
|
williamr@2
|
435 |
each representing a single value in the page which is to be relocated.
|
williamr@2
|
436 |
The lower 12 bits of each entry is the offset, in bytes, from start of this page.
|
williamr@2
|
437 |
The Upper 4 bits are the relocation type to be applied to the 32-bit value located
|
williamr@2
|
438 |
at that offset.
|
williamr@2
|
439 |
- 1 means relocate relative to code section.
|
williamr@2
|
440 |
- 2 means relocate relative to data section.
|
williamr@2
|
441 |
- 3 means relocate relative to code or data section; calculate which.
|
williamr@2
|
442 |
|
williamr@2
|
443 |
A value of all zeros (0x0000) is ignored. (Used for padding structure to 4 byte alignment).
|
williamr@2
|
444 |
*/
|
williamr@2
|
445 |
class E32RelocBlock
|
williamr@2
|
446 |
{
|
williamr@2
|
447 |
public:
|
williamr@2
|
448 |
TUint32 iPageOffset; ///< Offset, in bytes, for the page being relocated; relative to the section start. Always a multiple of the page size: 4096 bytes.
|
williamr@2
|
449 |
TUint32 iBlockSize; ///< Size, in bytes, for this block structure. Always a multiple of 4.
|
williamr@2
|
450 |
// TUint16 iEntry[]
|
williamr@2
|
451 |
};
|
williamr@2
|
452 |
|
williamr@2
|
453 |
|
williamr@2
|
454 |
/**
|
williamr@2
|
455 |
Header for a Relocation Section in an image, as referenced by E32ImageHeader::iCodeRelocOffset
|
williamr@2
|
456 |
or E32ImageHeader::iDataRelocOffset.
|
williamr@2
|
457 |
|
williamr@2
|
458 |
Immediately following this structure are an array of E32RelocBlock structures.
|
williamr@2
|
459 |
*/
|
williamr@2
|
460 |
class E32RelocSection
|
williamr@2
|
461 |
{
|
williamr@2
|
462 |
public:
|
williamr@2
|
463 |
TInt iSize; ///< Size of this relocation section including 'this' structure. Always a multiple of 4.
|
williamr@2
|
464 |
TInt iNumberOfRelocs; ///< Number of relocations in this section.
|
williamr@2
|
465 |
// E32RelocBlock iRelockBlock[];
|
williamr@2
|
466 |
};
|
williamr@2
|
467 |
|
williamr@2
|
468 |
|
williamr@2
|
469 |
/**
|
williamr@2
|
470 |
Structure contained in the export directory in text section of the stdexe/stddll.
|
williamr@2
|
471 |
It contains information on the names of symbols exported by this stdexe/stddll and
|
williamr@2
|
472 |
pointers to a E32EpocExpSymInfoHdr structure of any stddlls that are dependencies of
|
williamr@2
|
473 |
this stdexe/stddll.
|
williamr@2
|
474 |
|
williamr@2
|
475 |
This is not used for emulator images see E32EmulExpSymInfoHdr below.
|
williamr@2
|
476 |
@see E32EmulExpSymInfoHdr
|
williamr@2
|
477 |
*/
|
williamr@2
|
478 |
class E32EpocExpSymInfoHdr
|
williamr@2
|
479 |
{
|
williamr@2
|
480 |
public:
|
williamr@2
|
481 |
TInt iSize; // size of this Table
|
williamr@2
|
482 |
TInt16 iFlags;
|
williamr@2
|
483 |
TInt16 iSymCount; // number of symbols
|
williamr@2
|
484 |
TInt iSymbolTblOffset; // start of the symbol table - offset from byte 0 of this header
|
williamr@2
|
485 |
TInt iStringTableSz; // size of the string table
|
williamr@2
|
486 |
TInt iStringTableOffset; // start of the string table having names of the symbols - offset from byte 0 of this header
|
williamr@2
|
487 |
TInt iDllCount; // Number of dependent DLLs
|
williamr@2
|
488 |
TInt iDepDllZeroOrdTableOffset; // offset of the DLL dependency table - offset from byte 0 of this header.
|
williamr@2
|
489 |
};
|
williamr@2
|
490 |
|
williamr@2
|
491 |
|
williamr@2
|
492 |
/**
|
williamr@2
|
493 |
Header of the structure contained in the 'KWin32SectionName_NmdExpData'
|
williamr@2
|
494 |
segment of emulator stdexe & stddll images.
|
williamr@2
|
495 |
The segment contains addresses of symbols and NULL
|
williamr@2
|
496 |
terminated ASCII strings of the names of static dependencies.
|
williamr@2
|
497 |
For a stdexe, this segment contains the following:
|
williamr@2
|
498 |
a) symbol count (iSymCount) and static dependency count (iDllCount)
|
williamr@2
|
499 |
b) iSymCount * symbol addresses
|
williamr@2
|
500 |
c) iSymCount * symbol names
|
williamr@2
|
501 |
d) iDllCount * dependency names
|
williamr@2
|
502 |
|
williamr@2
|
503 |
For a stddll, this segment contains the following:
|
williamr@2
|
504 |
a) symbol count (iSymCout) is always 0
|
williamr@2
|
505 |
b) static dependency count (iDllCount)
|
williamr@2
|
506 |
c) iDllCount * dependency names
|
williamr@2
|
507 |
The symbol addresses and names are not required for a stddll as the Windows API,
|
williamr@2
|
508 |
GetProcAddress may be used to get the addresses for symbol names.
|
williamr@2
|
509 |
Since this API works only on DLL handles, we explicitly list them for stdexes.
|
williamr@2
|
510 |
This is used for emulator images only.
|
williamr@2
|
511 |
*/
|
williamr@2
|
512 |
class E32EmulExpSymInfoHdr
|
williamr@2
|
513 |
{
|
williamr@2
|
514 |
public:
|
williamr@2
|
515 |
TInt32 iSymCount; // Number of symbols
|
williamr@2
|
516 |
TInt32 iDllCount; // Number of static dependency DLLs
|
williamr@2
|
517 |
};
|
williamr@2
|
518 |
|
williamr@2
|
519 |
|
williamr@2
|
520 |
|
williamr@2
|
521 |
#ifdef INCLUDE_E32IMAGEHEADER_IMPLEMENTATION
|
williamr@2
|
522 |
|
williamr@2
|
523 |
// include code which implements validation functions...
|
williamr@2
|
524 |
|
williamr@2
|
525 |
#ifndef RETURN_FAILURE
|
williamr@2
|
526 |
#define RETURN_FAILURE(_r) return (_r)
|
williamr@2
|
527 |
#endif
|
williamr@2
|
528 |
|
williamr@2
|
529 |
#ifndef E32IMAGEHEADER_TRACE
|
williamr@2
|
530 |
#define E32IMAGEHEADER_TRACE(_t) ((void)0)
|
williamr@2
|
531 |
#endif
|
williamr@2
|
532 |
|
williamr@2
|
533 |
|
williamr@2
|
534 |
#include <e32uid.h>
|
williamr@2
|
535 |
|
williamr@2
|
536 |
|
williamr@2
|
537 |
/**
|
williamr@2
|
538 |
Validate this image header.
|
williamr@2
|
539 |
|
williamr@2
|
540 |
After successful validation the following are true:
|
williamr@2
|
541 |
- File size is big enough to contain the entire header.
|
williamr@2
|
542 |
- Values #iUidChecksum, #iSignature and #iHeaderCrc are correct.
|
williamr@2
|
543 |
- CPU type (#iCpuIdentifier), ABI type (#iFlags&#KImageABIMask) and
|
williamr@2
|
544 |
entrypoint type (#iFlags&#KImageEptMask) are valid for this system.
|
williamr@2
|
545 |
- Code part of file as specified by #iCodeOffset and #iCodeSize is fully within the file.
|
williamr@2
|
546 |
- Text section size (#iTextSize) is within code part.
|
williamr@2
|
547 |
- Entrypoint value (#iEntryPoint) lies within the code part and is aligned correctly.
|
williamr@2
|
548 |
- Export directory as specified by #iExportDirCount and #iExportDirOffset is fully
|
williamr@2
|
549 |
within code part and is aligned correctly.
|
williamr@2
|
550 |
- Exception description (E32ImageHeaderV::iExceptionDescriptor), if present,
|
williamr@2
|
551 |
lies within the code part.
|
williamr@2
|
552 |
- Data part of file as specified by #iDataOffset and #iDataSize is fully within the file.
|
williamr@2
|
553 |
Or data is not present (#iDataOffset==#iDataSize==0).
|
williamr@2
|
554 |
- Import section (class E32ImportSection at #iImportOffset) is within 'rest of data'
|
williamr@2
|
555 |
and aligned correctly. Data following the E32ImportSection header is NOT validated or
|
williamr@2
|
556 |
checked if it is fully contained within the file.
|
williamr@2
|
557 |
- Code relocations (class E32RelocSection at #iCodeRelocOffset) is within 'rest of data'
|
williamr@2
|
558 |
and aligned correctly. Data following the E32RelocSection header is NOT validated or
|
williamr@2
|
559 |
checked if it is fully contained within the file.
|
williamr@2
|
560 |
- Data relocations (class E32RelocSection at #iDataRelocOffset) is within 'rest of data'
|
williamr@2
|
561 |
and aligned correctly. Data following the E32RelocSection header is NOT validated or
|
williamr@2
|
562 |
checked if it is fully contained within the file.
|
williamr@2
|
563 |
- Export description is validated by E32ImageHeaderV::ValidateExportDescription().
|
williamr@2
|
564 |
- #iUid1 is consistant with #iFlags&#KImageDll. I.e. if flaged as a DLL, #iUid1 is
|
williamr@2
|
565 |
KDynamicLibraryUidValue, otherwise it is KExecutableImageUidValue.
|
williamr@2
|
566 |
- Version number (#iModuleVersion) is valid. (Major and minor versions are <32768).
|
williamr@2
|
567 |
- File compression type (#iCompressionType) is supported.
|
williamr@2
|
568 |
- #iHeapSizeMax>=#iHeapSizeMin
|
williamr@2
|
569 |
- All signed values in header are not negative.
|
williamr@2
|
570 |
|
williamr@2
|
571 |
@param aFileSize Total size of the file from which this header was created.
|
williamr@2
|
572 |
@param[out] aUncompressedSize Returns the total size that the file data would be once decompressed.
|
williamr@2
|
573 |
|
williamr@2
|
574 |
@return KErrNone if no errors detected;
|
williamr@2
|
575 |
KErrCorrupt if errors found;
|
williamr@2
|
576 |
KErrNotSupported if image format not supported on this platform.
|
williamr@2
|
577 |
*/
|
williamr@2
|
578 |
TInt E32ImageHeader::ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const
|
williamr@2
|
579 |
{
|
williamr@2
|
580 |
// check file is big enough for any header...
|
williamr@2
|
581 |
if(TUint(aFileSize)<sizeof(*this))
|
williamr@2
|
582 |
return KErrCorrupt;
|
williamr@2
|
583 |
|
williamr@2
|
584 |
TUint hdrfmt = HeaderFormat();
|
williamr@2
|
585 |
if(hdrfmt==KImageHdrFmt_V)
|
williamr@2
|
586 |
return ((E32ImageHeaderV*)this)->ValidateHeader(aFileSize,aUncompressedSize);
|
williamr@2
|
587 |
|
williamr@2
|
588 |
return KErrNotSupported; // header format unrecognised
|
williamr@2
|
589 |
}
|
williamr@2
|
590 |
|
williamr@2
|
591 |
/**
|
williamr@2
|
592 |
Validate this image header.
|
williamr@2
|
593 |
|
williamr@2
|
594 |
@param aFileSize Total size of the file from which this header was created.
|
williamr@2
|
595 |
@param[out] aUncompressedSize Returns the total size that the file data would be once decompressed.
|
williamr@2
|
596 |
|
williamr@2
|
597 |
@return KErrNone if no errors detected;
|
williamr@2
|
598 |
KErrCorrupt if errors found;
|
williamr@2
|
599 |
KErrNotSupported if image format not supported on this platform.
|
williamr@2
|
600 |
*/
|
williamr@2
|
601 |
TInt E32ImageHeaderV::ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const
|
williamr@2
|
602 |
{
|
williamr@2
|
603 |
const TUint KMaxDesSize = 0x0fffffffu; // maximum size of descriptor
|
williamr@2
|
604 |
if(aFileSize==-1)
|
williamr@2
|
605 |
{
|
williamr@2
|
606 |
// file size unknown, set to maximum valid so rest of validation works...
|
williamr@2
|
607 |
aFileSize = KMaxDesSize;
|
williamr@2
|
608 |
}
|
williamr@2
|
609 |
if(TUint(aFileSize)>KMaxDesSize)
|
williamr@2
|
610 |
RETURN_FAILURE(KErrCorrupt); // file size negative or too big
|
williamr@2
|
611 |
|
williamr@2
|
612 |
aUncompressedSize = 0;
|
williamr@2
|
613 |
|
williamr@2
|
614 |
// check file is big enough to contain this header...
|
williamr@2
|
615 |
if(aFileSize<(TInt)sizeof(*this))
|
williamr@2
|
616 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
617 |
|
williamr@2
|
618 |
// check header format version...
|
williamr@2
|
619 |
if((iFlags&KImageHdrFmtMask)!=KImageHdrFmt_V)
|
williamr@2
|
620 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
621 |
|
williamr@2
|
622 |
// check header size...
|
williamr@2
|
623 |
TUint headerSize = iCodeOffset;
|
williamr@2
|
624 |
if(headerSize>TUint(aFileSize))
|
williamr@2
|
625 |
RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because Loader will fail earlier when reading header from file
|
williamr@2
|
626 |
|
williamr@2
|
627 |
// check iCpuIdentifier...
|
williamr@2
|
628 |
TCpu cpu = (TCpu)iCpuIdentifier;
|
williamr@2
|
629 |
TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6);
|
williamr@2
|
630 |
#if defined(__CPU_ARM)
|
williamr@2
|
631 |
if(!isARM)
|
williamr@2
|
632 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
633 |
#elif defined(__CPU_X86)
|
williamr@2
|
634 |
if(cpu!=ECpuX86)
|
williamr@2
|
635 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
636 |
#endif
|
williamr@2
|
637 |
TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets
|
williamr@2
|
638 |
|
williamr@2
|
639 |
// check iUid1,iUid2,iUid3,iUidChecksum...
|
williamr@2
|
640 |
TUidType uids = *(const TUidType*)&iUid1;
|
williamr@2
|
641 |
TCheckedUid chkuid(uids);
|
williamr@2
|
642 |
const TUint32* pChkUid = (const TUint32*)&chkuid; // need hackery to verify the UID checksum since everything is private
|
williamr@2
|
643 |
if(pChkUid[3]!=iUidChecksum)
|
williamr@2
|
644 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
645 |
|
williamr@2
|
646 |
// check iSignature...
|
williamr@2
|
647 |
if(iSignature!=0x434f5045) // 'EPOC'
|
williamr@2
|
648 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
649 |
|
williamr@2
|
650 |
// check iHeaderCrc...
|
williamr@2
|
651 |
TUint32 supplied_crc = iHeaderCrc;
|
williamr@2
|
652 |
((E32ImageHeaderV*)this)->iHeaderCrc = KImageCrcInitialiser;
|
williamr@2
|
653 |
TUint32 crc = 0;
|
williamr@2
|
654 |
Mem::Crc32(crc, this, headerSize);
|
williamr@2
|
655 |
((E32ImageHeaderV*)this)->iHeaderCrc = supplied_crc;
|
williamr@2
|
656 |
if(crc!=supplied_crc)
|
williamr@2
|
657 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
658 |
|
williamr@2
|
659 |
// check iModuleVersion...
|
williamr@2
|
660 |
TUint32 mv = iModuleVersion;
|
williamr@2
|
661 |
if(mv>=0x80000000u || (mv&0x0000ffffu)>0x8000u)
|
williamr@2
|
662 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
663 |
|
williamr@2
|
664 |
// check iCompressionType and get uncompressed size...
|
williamr@2
|
665 |
TUint compression = iCompressionType;
|
williamr@2
|
666 |
TUint uncompressedSize = aFileSize;
|
williamr@2
|
667 |
if(compression!=KFormatNotCompressed)
|
williamr@2
|
668 |
{
|
williamr@2
|
669 |
if(compression!=KUidCompressionDeflate && compression!=KUidCompressionBytePair)
|
williamr@2
|
670 |
RETURN_FAILURE(KErrNotSupported); // unknown compression method
|
williamr@2
|
671 |
uncompressedSize = headerSize+iUncompressedSize;
|
williamr@2
|
672 |
if(uncompressedSize<headerSize)
|
williamr@2
|
673 |
RETURN_FAILURE(KErrCorrupt); // size overflowed 32 bits
|
williamr@2
|
674 |
}
|
williamr@2
|
675 |
|
williamr@2
|
676 |
// check sizes won't overflow the limit for a descriptor (many Loader uses won't like that).
|
williamr@2
|
677 |
if(uncompressedSize>KMaxDesSize)
|
williamr@2
|
678 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
679 |
|
williamr@2
|
680 |
// check KImageDll in iFlags...
|
williamr@2
|
681 |
if(iFlags&KImageDll)
|
williamr@2
|
682 |
{
|
williamr@2
|
683 |
if(iUid1!=TUint32(KDynamicLibraryUidValue))
|
williamr@2
|
684 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
685 |
}
|
williamr@2
|
686 |
else if(iUid1!=TUint32(KExecutableImageUidValue))
|
williamr@2
|
687 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
688 |
|
williamr@2
|
689 |
// check iFlags for ABI and entry point types...
|
williamr@2
|
690 |
if(isARM)
|
williamr@2
|
691 |
{
|
williamr@2
|
692 |
if((iFlags&KImageEptMask)!=KImageEpt_Eka2)
|
williamr@2
|
693 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
694 |
#if defined(__EABI__)
|
williamr@2
|
695 |
if((iFlags&KImageABIMask)!=KImageABI_EABI)
|
williamr@2
|
696 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
697 |
#elif defined(__GCC32__)
|
williamr@2
|
698 |
if((iFlags&KImageABIMask)!=KImageABI_GCC98r2)
|
williamr@2
|
699 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
700 |
#endif
|
williamr@2
|
701 |
}
|
williamr@2
|
702 |
else
|
williamr@2
|
703 |
{
|
williamr@2
|
704 |
if(iFlags&KImageEptMask)
|
williamr@2
|
705 |
RETURN_FAILURE(KErrNotSupported); // no special entry point type allowed on non-ARM targets
|
williamr@2
|
706 |
if(iFlags&KImageABIMask)
|
williamr@2
|
707 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
708 |
}
|
williamr@2
|
709 |
|
williamr@2
|
710 |
// check iFlags for import format...
|
williamr@2
|
711 |
if((iFlags&KImageImpFmtMask)>KImageImpFmt_PE2)
|
williamr@2
|
712 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
713 |
|
williamr@2
|
714 |
// check iHeapSizeMin...
|
williamr@2
|
715 |
if(iHeapSizeMin<0)
|
williamr@2
|
716 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
717 |
|
williamr@2
|
718 |
// check iHeapSizeMax...
|
williamr@2
|
719 |
if(iHeapSizeMax<iHeapSizeMin)
|
williamr@2
|
720 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
721 |
|
williamr@2
|
722 |
// check iStackSize...
|
williamr@2
|
723 |
if(iStackSize<0)
|
williamr@2
|
724 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
725 |
|
williamr@2
|
726 |
// check iBssSize...
|
williamr@2
|
727 |
if(iBssSize<0)
|
williamr@2
|
728 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
729 |
|
williamr@2
|
730 |
// check iEntryPoint...
|
williamr@2
|
731 |
if(iEntryPoint>=TUint(iCodeSize))
|
williamr@2
|
732 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
733 |
if(iEntryPoint+KCodeSegIdOffset+sizeof(TUint32)>TUint(iCodeSize))
|
williamr@2
|
734 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
735 |
if(iEntryPoint&pointerAlignMask)
|
williamr@2
|
736 |
RETURN_FAILURE(KErrCorrupt); // not aligned
|
williamr@2
|
737 |
|
williamr@2
|
738 |
// check iCodeBase...
|
williamr@2
|
739 |
if(iCodeBase&3)
|
williamr@2
|
740 |
RETURN_FAILURE(KErrCorrupt); // not aligned
|
williamr@2
|
741 |
|
williamr@2
|
742 |
// check iDataBase...
|
williamr@2
|
743 |
if(iDataBase&3)
|
williamr@2
|
744 |
RETURN_FAILURE(KErrCorrupt); // not aligned
|
williamr@2
|
745 |
|
williamr@2
|
746 |
// check iDllRefTableCount...
|
williamr@2
|
747 |
if(iDllRefTableCount<0)
|
williamr@2
|
748 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
749 |
if(iDllRefTableCount)
|
williamr@2
|
750 |
{
|
williamr@2
|
751 |
if(!iImportOffset)
|
williamr@2
|
752 |
RETURN_FAILURE(KErrCorrupt); // we link to DLLs but have no import data
|
williamr@2
|
753 |
}
|
williamr@2
|
754 |
|
williamr@2
|
755 |
// check iCodeOffset and iCodeSize specify region in file...
|
williamr@2
|
756 |
TUint codeStart = iCodeOffset;
|
williamr@2
|
757 |
TUint codeEnd = codeStart+iCodeSize;
|
williamr@2
|
758 |
if(codeEnd<codeStart)
|
williamr@2
|
759 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
760 |
// if(codeStart<headerSize)
|
williamr@2
|
761 |
// RETURN_FAILURE(KErrCorrupt); // can't happen because headerSize is defined as iCodeOffset (codeStart)
|
williamr@2
|
762 |
if(codeEnd>uncompressedSize)
|
williamr@2
|
763 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
764 |
|
williamr@2
|
765 |
// check iDataOffset and iDataSize specify region in file...
|
williamr@2
|
766 |
TUint dataStart = iDataOffset;
|
williamr@2
|
767 |
TUint dataEnd = dataStart+iDataSize;
|
williamr@2
|
768 |
if(dataEnd<dataStart)
|
williamr@2
|
769 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
770 |
if(!dataStart)
|
williamr@2
|
771 |
{
|
williamr@2
|
772 |
// no data...
|
williamr@2
|
773 |
if(dataEnd)
|
williamr@2
|
774 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
775 |
}
|
williamr@2
|
776 |
else
|
williamr@2
|
777 |
{
|
williamr@2
|
778 |
if(dataStart<codeEnd)
|
williamr@2
|
779 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
780 |
if(dataEnd>uncompressedSize)
|
williamr@2
|
781 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
782 |
if((dataStart-codeStart)&pointerAlignMask)
|
williamr@2
|
783 |
RETURN_FAILURE(KErrCorrupt); // data not aligned with respect to code
|
williamr@2
|
784 |
}
|
williamr@2
|
785 |
|
williamr@2
|
786 |
|
williamr@2
|
787 |
// check total data size isn't too bit...
|
williamr@2
|
788 |
TUint totalDataSize = iDataSize+iBssSize;
|
williamr@2
|
789 |
if(totalDataSize>0x7fff0000)
|
williamr@2
|
790 |
RETURN_FAILURE(KErrNoMemory);
|
williamr@2
|
791 |
|
williamr@2
|
792 |
// check iExportDirOffset and iExportDirCount specify region in code part...
|
williamr@2
|
793 |
if(TUint(iExportDirCount)>65535)
|
williamr@2
|
794 |
RETURN_FAILURE(KErrCorrupt); // too many exports
|
williamr@2
|
795 |
if(iExportDirCount)
|
williamr@2
|
796 |
{
|
williamr@2
|
797 |
TUint exportsStart = iExportDirOffset;
|
williamr@2
|
798 |
TUint exportsEnd = exportsStart+iExportDirCount*sizeof(TUint32);
|
williamr@2
|
799 |
if(iFlags&KImageNmdExpData)
|
williamr@2
|
800 |
exportsStart -= sizeof(TUint32); // allow for 0th ordinal
|
williamr@2
|
801 |
if(exportsEnd<exportsStart)
|
williamr@2
|
802 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
803 |
if(exportsStart<codeStart)
|
williamr@2
|
804 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
805 |
if(exportsEnd>codeEnd)
|
williamr@2
|
806 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
807 |
if((exportsStart-codeStart)&pointerAlignMask)
|
williamr@2
|
808 |
RETURN_FAILURE(KErrCorrupt); // not aligned within code section
|
williamr@2
|
809 |
}
|
williamr@2
|
810 |
|
williamr@2
|
811 |
// check iTextSize...
|
williamr@2
|
812 |
if(TUint(iTextSize)>TUint(iCodeSize))
|
williamr@2
|
813 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
814 |
|
williamr@2
|
815 |
// check iImportOffset...
|
williamr@2
|
816 |
TUint start = iImportOffset;
|
williamr@2
|
817 |
if(start)
|
williamr@2
|
818 |
{
|
williamr@2
|
819 |
TUint end = start+sizeof(E32ImportSection); // minimum valid size
|
williamr@2
|
820 |
if(end<start)
|
williamr@2
|
821 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
822 |
if(start<codeEnd)
|
williamr@2
|
823 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
824 |
if(end>uncompressedSize)
|
williamr@2
|
825 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
826 |
if((start-codeEnd)&pointerAlignMask)
|
williamr@2
|
827 |
RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data'
|
williamr@2
|
828 |
}
|
williamr@2
|
829 |
|
williamr@2
|
830 |
// check iCodeRelocOffset...
|
williamr@2
|
831 |
start = iCodeRelocOffset;
|
williamr@2
|
832 |
if(start)
|
williamr@2
|
833 |
{
|
williamr@2
|
834 |
TUint end = start+sizeof(E32RelocSection); // minimum valid size
|
williamr@2
|
835 |
if(end<start)
|
williamr@2
|
836 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
837 |
if(start<codeEnd)
|
williamr@2
|
838 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
839 |
if(end>uncompressedSize)
|
williamr@2
|
840 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
841 |
if((start-codeEnd)&pointerAlignMask)
|
williamr@2
|
842 |
RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data'
|
williamr@2
|
843 |
}
|
williamr@2
|
844 |
|
williamr@2
|
845 |
// check iDataRelocOffset...
|
williamr@2
|
846 |
start = iDataRelocOffset;
|
williamr@2
|
847 |
if(start)
|
williamr@2
|
848 |
{
|
williamr@2
|
849 |
TUint end = start+sizeof(E32RelocSection); // minimum valid size
|
williamr@2
|
850 |
if(end<start)
|
williamr@2
|
851 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
852 |
if(start<codeEnd)
|
williamr@2
|
853 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
854 |
if(end>uncompressedSize)
|
williamr@2
|
855 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
856 |
if((start-codeEnd)&pointerAlignMask)
|
williamr@2
|
857 |
RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data'
|
williamr@2
|
858 |
}
|
williamr@2
|
859 |
|
williamr@2
|
860 |
// check exception descriptor...
|
williamr@2
|
861 |
if(iExceptionDescriptor&1) // if valid...
|
williamr@2
|
862 |
if(iExceptionDescriptor>=TUint(iCodeSize))
|
williamr@2
|
863 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
864 |
|
williamr@2
|
865 |
TInt r = ValidateExportDescription();
|
williamr@2
|
866 |
if(r!=KErrNone)
|
williamr@2
|
867 |
RETURN_FAILURE(r);
|
williamr@2
|
868 |
|
williamr@2
|
869 |
// done...
|
williamr@2
|
870 |
aUncompressedSize = uncompressedSize;
|
williamr@2
|
871 |
return KErrNone;
|
williamr@2
|
872 |
}
|
williamr@2
|
873 |
|
williamr@2
|
874 |
|
williamr@2
|
875 |
/**
|
williamr@2
|
876 |
Valdate that the export description is valid.
|
williamr@2
|
877 |
*/
|
williamr@2
|
878 |
TInt E32ImageHeaderV::ValidateExportDescription() const
|
williamr@2
|
879 |
{
|
williamr@2
|
880 |
TUint headerSize = iCodeOffset;
|
williamr@2
|
881 |
|
williamr@2
|
882 |
// check export description...
|
williamr@2
|
883 |
TUint edSize = iExportDescSize + sizeof(iExportDescSize) + sizeof(iExportDescType);
|
williamr@2
|
884 |
edSize = (edSize+3)&~3;
|
williamr@2
|
885 |
TUint edEnd = _FOFF(E32ImageHeaderV,iExportDescSize)+edSize;
|
williamr@2
|
886 |
if(edEnd!=headerSize)
|
williamr@2
|
887 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
888 |
|
williamr@2
|
889 |
// size of bitmap of exports...
|
williamr@2
|
890 |
TUint bitmapSize = (iExportDirCount+7) >> 3;
|
williamr@2
|
891 |
|
williamr@2
|
892 |
// check export description bitmap...
|
williamr@2
|
893 |
switch(iExportDescType)
|
williamr@2
|
894 |
{
|
williamr@2
|
895 |
case KImageHdr_ExpD_NoHoles:
|
williamr@2
|
896 |
// no bitmap to check...
|
williamr@2
|
897 |
E32IMAGEHEADER_TRACE(("ValidateExportDescription NoHoles"));
|
williamr@2
|
898 |
return KErrNone;
|
williamr@2
|
899 |
|
williamr@2
|
900 |
case KImageHdr_ExpD_FullBitmap:
|
williamr@2
|
901 |
// full bitmap present...
|
williamr@2
|
902 |
E32IMAGEHEADER_TRACE(("ValidateExportDescription FullBitmap"));
|
williamr@2
|
903 |
if(bitmapSize!=iExportDescSize)
|
williamr@2
|
904 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
905 |
return KErrNone;
|
williamr@2
|
906 |
|
williamr@2
|
907 |
case KImageHdr_ExpD_SparseBitmap8:
|
williamr@2
|
908 |
{
|
williamr@2
|
909 |
// sparse bitmap present...
|
williamr@2
|
910 |
E32IMAGEHEADER_TRACE(("ValidateExportDescription SparseBitmap8"));
|
williamr@2
|
911 |
|
williamr@2
|
912 |
// get size of meta-bitmap...
|
williamr@2
|
913 |
TUint metaBitmapSize = (bitmapSize+7) >> 3;
|
williamr@2
|
914 |
if(metaBitmapSize>iExportDescSize)
|
williamr@2
|
915 |
RETURN_FAILURE(KErrCorrupt); // doesn't fit
|
williamr@2
|
916 |
|
williamr@2
|
917 |
TUint totalSize = metaBitmapSize;
|
williamr@2
|
918 |
|
williamr@2
|
919 |
// scan meta-bitmap counting extra bytes which should be present...
|
williamr@2
|
920 |
const TUint8* metaBitmap = iExportDesc;
|
williamr@2
|
921 |
const TUint8* metaBitmapEnd = metaBitmap + metaBitmapSize;
|
williamr@2
|
922 |
while(metaBitmap<metaBitmapEnd)
|
williamr@2
|
923 |
{
|
williamr@2
|
924 |
TUint bits = *metaBitmap++;
|
williamr@2
|
925 |
do
|
williamr@2
|
926 |
{
|
williamr@2
|
927 |
if(bits&1)
|
williamr@2
|
928 |
++totalSize; // another byte is present in bitmap
|
williamr@2
|
929 |
}
|
williamr@2
|
930 |
while(bits>>=1);
|
williamr@2
|
931 |
}
|
williamr@2
|
932 |
|
williamr@2
|
933 |
if(totalSize!=iExportDescSize)
|
williamr@2
|
934 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
935 |
}
|
williamr@2
|
936 |
return KErrNone;
|
williamr@2
|
937 |
|
williamr@2
|
938 |
default:
|
williamr@2
|
939 |
E32IMAGEHEADER_TRACE(("ValidateExportDescription ?"));
|
williamr@2
|
940 |
RETURN_FAILURE(KErrNotSupported);
|
williamr@2
|
941 |
}
|
williamr@2
|
942 |
}
|
williamr@2
|
943 |
|
williamr@2
|
944 |
|
williamr@2
|
945 |
/**
|
williamr@2
|
946 |
Validate a relocation section.
|
williamr@2
|
947 |
|
williamr@2
|
948 |
@param aBufferStart Start of buffer containing the data after the code part in the image file.
|
williamr@2
|
949 |
@param aBufferSize Size of data at aBufferStart.
|
williamr@2
|
950 |
@param aRelocationInfoOffset File offset for relocation section. (#iCodeRelocOffset or #iDataRelocOffset.)
|
williamr@2
|
951 |
@param aRelocatedSectionSize Size of section being relocated. (#iCodeSize or #iDataSize.)
|
williamr@2
|
952 |
@param[out] aRelocationSection Set to the start of the relocation section in the given buffer.
|
williamr@2
|
953 |
|
williamr@2
|
954 |
@return KErrNone if relocation section is valid, else KErrCorrupt.
|
williamr@2
|
955 |
*/
|
williamr@2
|
956 |
TInt E32ImageHeaderV::ValidateRelocations(TAny* aBufferStart, TUint aBufferSize, TUint aRelocationInfoOffset, TUint aRelocatedSectionSize, E32RelocSection*& aRelocationSection) const
|
williamr@2
|
957 |
{
|
williamr@2
|
958 |
aRelocationSection = 0;
|
williamr@2
|
959 |
if(!aRelocationInfoOffset)
|
williamr@2
|
960 |
return KErrNone; // no relocations
|
williamr@2
|
961 |
|
williamr@2
|
962 |
// get alignment requirements...
|
williamr@2
|
963 |
TCpu cpu = (TCpu)iCpuIdentifier;
|
williamr@2
|
964 |
TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6);
|
williamr@2
|
965 |
TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets
|
williamr@2
|
966 |
|
williamr@2
|
967 |
// buffer pointer to read relocation from...
|
williamr@2
|
968 |
TUint8* bufferStart = (TUint8*)aBufferStart;
|
williamr@2
|
969 |
TUint8* bufferEnd = bufferStart+aBufferSize;
|
williamr@2
|
970 |
TUint baseOffset = iCodeOffset+iCodeSize; // file offset for aBufferStart
|
williamr@2
|
971 |
TUint8* sectionStart = (bufferStart+aRelocationInfoOffset-baseOffset);
|
williamr@2
|
972 |
TUint8* p = sectionStart;
|
williamr@2
|
973 |
|
williamr@2
|
974 |
// read section header (ValidateHeader has alread checked this is OK)...
|
williamr@2
|
975 |
E32RelocSection* sectionHeader = (E32RelocSection*)p;
|
williamr@2
|
976 |
TUint size = sectionHeader->iSize;
|
williamr@2
|
977 |
TUint relocsRemaining = sectionHeader->iNumberOfRelocs;
|
williamr@2
|
978 |
E32IMAGEHEADER_TRACE(("E32RelocSection 0x%x %d",size,relocsRemaining));
|
williamr@2
|
979 |
if(size&3)
|
williamr@2
|
980 |
RETURN_FAILURE(KErrCorrupt); // not multiple of word size
|
williamr@2
|
981 |
|
williamr@2
|
982 |
// calculate buffer range for block data...
|
williamr@2
|
983 |
p = (TUint8*)(sectionHeader+1); // start of first block
|
williamr@2
|
984 |
TUint8* sectionEnd = p+size;
|
williamr@2
|
985 |
if(sectionEnd<p)
|
williamr@2
|
986 |
RETURN_FAILURE(KErrCorrupt); // math overflow
|
williamr@2
|
987 |
if(sectionEnd>bufferEnd)
|
williamr@2
|
988 |
RETURN_FAILURE(KErrCorrupt); // overflows buffer
|
williamr@2
|
989 |
|
williamr@2
|
990 |
// process each block...
|
williamr@2
|
991 |
while(p!=sectionEnd)
|
williamr@2
|
992 |
{
|
williamr@2
|
993 |
E32RelocBlock* block = (E32RelocBlock*)p;
|
williamr@2
|
994 |
|
williamr@2
|
995 |
// get address of first entry in this block...
|
williamr@2
|
996 |
TUint16* entryPtr = (TUint16*)(block+1);
|
williamr@2
|
997 |
if((TUint8*)entryPtr<(TUint8*)block || (TUint8*)entryPtr>sectionEnd)
|
williamr@2
|
998 |
RETURN_FAILURE(KErrCorrupt); // overflows relocation section
|
williamr@2
|
999 |
|
williamr@2
|
1000 |
// read block header...
|
williamr@2
|
1001 |
TUint pageOffset = block->iPageOffset;
|
williamr@2
|
1002 |
TUint blockSize = block->iBlockSize;
|
williamr@2
|
1003 |
E32IMAGEHEADER_TRACE(("E32RelocSection block 0x%x 0x%x",pageOffset,blockSize));
|
williamr@2
|
1004 |
if(pageOffset&0xfff)
|
williamr@2
|
1005 |
RETURN_FAILURE(KErrCorrupt); // not page aligned
|
williamr@2
|
1006 |
if(blockSize<sizeof(E32RelocBlock))
|
williamr@2
|
1007 |
RETURN_FAILURE(KErrCorrupt); // blockSize must be at least that of the header just read
|
williamr@2
|
1008 |
if(blockSize&3)
|
williamr@2
|
1009 |
RETURN_FAILURE(KErrCorrupt); // not word aligned
|
williamr@2
|
1010 |
|
williamr@2
|
1011 |
// caculate end of entries in this block...
|
williamr@2
|
1012 |
TUint16* entryEnd = (TUint16*)(p+blockSize);
|
williamr@2
|
1013 |
if(entryEnd<entryPtr)
|
williamr@2
|
1014 |
RETURN_FAILURE(KErrCorrupt); // math overflow
|
williamr@2
|
1015 |
if(entryEnd>(TUint16*)sectionEnd)
|
williamr@2
|
1016 |
RETURN_FAILURE(KErrCorrupt); // overflows relocation section
|
williamr@2
|
1017 |
|
williamr@2
|
1018 |
// process each entry in this block...
|
williamr@2
|
1019 |
while(entryPtr<entryEnd)
|
williamr@2
|
1020 |
{
|
williamr@2
|
1021 |
TUint entry = *entryPtr++;
|
williamr@2
|
1022 |
E32IMAGEHEADER_TRACE(("E32RelocSection entry 0x%04x",entry));
|
williamr@2
|
1023 |
if(!entry)
|
williamr@2
|
1024 |
continue;
|
williamr@2
|
1025 |
|
williamr@2
|
1026 |
// check relocation type...
|
williamr@2
|
1027 |
TUint entryType = entry&0xf000;
|
williamr@2
|
1028 |
if(entryType!=KTextRelocType && entryType!=KDataRelocType && entryType!=KInferredRelocType)
|
williamr@2
|
1029 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
1030 |
|
williamr@2
|
1031 |
// check relocation is within section being relocated...
|
williamr@2
|
1032 |
TUint offset = pageOffset+(entry&0x0fff);
|
williamr@2
|
1033 |
if(offset>=aRelocatedSectionSize || offset+4>aRelocatedSectionSize)
|
williamr@2
|
1034 |
RETURN_FAILURE(KErrCorrupt); // not within section
|
williamr@2
|
1035 |
if(offset&pointerAlignMask)
|
williamr@2
|
1036 |
RETURN_FAILURE(KErrCorrupt); // not aligned correctly
|
williamr@2
|
1037 |
|
williamr@2
|
1038 |
// count each relocation processed...
|
williamr@2
|
1039 |
--relocsRemaining;
|
williamr@2
|
1040 |
}
|
williamr@2
|
1041 |
|
williamr@2
|
1042 |
// next sub block...
|
williamr@2
|
1043 |
p = (TUint8*)entryEnd;
|
williamr@2
|
1044 |
}
|
williamr@2
|
1045 |
|
williamr@2
|
1046 |
// check number of relocations in section header is correct...
|
williamr@2
|
1047 |
E32IMAGEHEADER_TRACE(("E32RelocSection relocsRemaining=%d",relocsRemaining));
|
williamr@2
|
1048 |
if(relocsRemaining)
|
williamr@2
|
1049 |
RETURN_FAILURE(KErrCorrupt); // incorrect number of entries
|
williamr@2
|
1050 |
|
williamr@2
|
1051 |
aRelocationSection = sectionHeader;
|
williamr@2
|
1052 |
return KErrNone;
|
williamr@2
|
1053 |
}
|
williamr@2
|
1054 |
|
williamr@2
|
1055 |
|
williamr@2
|
1056 |
/**
|
williamr@2
|
1057 |
Validate an import section.
|
williamr@2
|
1058 |
|
williamr@2
|
1059 |
For PE format imports, this also verifies that the Import Address Table fits within the code
|
williamr@2
|
1060 |
part of the image.
|
williamr@2
|
1061 |
|
williamr@2
|
1062 |
@param aBufferStart Start of buffer containing the data after the code part in the image file.
|
williamr@2
|
1063 |
@param aBufferSize Size of data at aBufferStart.
|
williamr@2
|
1064 |
@param[out] aBiggestImportCount Largest number of imports the image has from any single dependency.
|
williamr@2
|
1065 |
|
williamr@2
|
1066 |
@return KErrNone if section is valid (or absent), else KErrCorrupt.
|
williamr@2
|
1067 |
*/
|
williamr@2
|
1068 |
TInt E32ImageHeaderV::ValidateImports(TAny* aBufferStart, TUint aBufferSize, TUint& aBiggestImportCount) const
|
williamr@2
|
1069 |
{
|
williamr@2
|
1070 |
if(!iImportOffset)
|
williamr@2
|
1071 |
{
|
williamr@2
|
1072 |
aBiggestImportCount = 0;
|
williamr@2
|
1073 |
return KErrNone; // no imports
|
williamr@2
|
1074 |
}
|
williamr@2
|
1075 |
|
williamr@2
|
1076 |
// get alignment requirements...
|
williamr@2
|
1077 |
TCpu cpu = (TCpu)iCpuIdentifier;
|
williamr@2
|
1078 |
TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6);
|
williamr@2
|
1079 |
TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets
|
williamr@2
|
1080 |
|
williamr@2
|
1081 |
// buffer pointer to read imports from...
|
williamr@2
|
1082 |
TUint8* bufferStart = (TUint8*)aBufferStart;
|
williamr@2
|
1083 |
TUint8* bufferEnd = bufferStart+aBufferSize;
|
williamr@2
|
1084 |
TUint baseOffset = iCodeOffset+iCodeSize; // file offset for aBufferStart
|
williamr@2
|
1085 |
TUint8* sectionStart = (bufferStart+iImportOffset-baseOffset);
|
williamr@2
|
1086 |
TUint8* p = sectionStart;
|
williamr@2
|
1087 |
|
williamr@2
|
1088 |
// read section header (ValidateHeader has alread checked this is OK)...
|
williamr@2
|
1089 |
E32ImportSection* sectionHeader = (E32ImportSection*)p;
|
williamr@2
|
1090 |
TUint size = sectionHeader->iSize;
|
williamr@2
|
1091 |
E32IMAGEHEADER_TRACE(("E32ImportSection 0x%x",size));
|
williamr@2
|
1092 |
|
williamr@2
|
1093 |
// check section lies within buffer...
|
williamr@2
|
1094 |
p = (TUint8*)(sectionHeader+1); // start of first import block
|
williamr@2
|
1095 |
TUint8* sectionEnd = sectionStart+size;
|
williamr@2
|
1096 |
if(sectionEnd<p)
|
williamr@2
|
1097 |
RETURN_FAILURE(KErrCorrupt); // math overflow or not big enough to contain header
|
williamr@2
|
1098 |
if(sectionEnd>bufferEnd)
|
williamr@2
|
1099 |
RETURN_FAILURE(KErrCorrupt); // overflows buffer
|
williamr@2
|
1100 |
|
williamr@2
|
1101 |
// process each import block...
|
williamr@2
|
1102 |
TUint numDeps = iDllRefTableCount;
|
williamr@2
|
1103 |
TUint biggestImportCount = 0;
|
williamr@2
|
1104 |
TUint totalImports = 0;
|
williamr@2
|
1105 |
TUint importFormat = iFlags&KImageImpFmtMask;
|
williamr@2
|
1106 |
while(numDeps--)
|
williamr@2
|
1107 |
{
|
williamr@2
|
1108 |
// get block header...
|
williamr@2
|
1109 |
E32ImportBlock* block = (E32ImportBlock*)p;
|
williamr@2
|
1110 |
p = (TUint8*)(block+1);
|
williamr@2
|
1111 |
if(p<(TUint8*)block || p>sectionEnd)
|
williamr@2
|
1112 |
RETURN_FAILURE(KErrCorrupt); // overflows buffer
|
williamr@2
|
1113 |
|
williamr@2
|
1114 |
E32IMAGEHEADER_TRACE(("E32ImportBlock 0x%x %d",block->iOffsetOfDllName,block->iNumberOfImports));
|
williamr@2
|
1115 |
|
williamr@2
|
1116 |
// check import dll name is within section...
|
williamr@2
|
1117 |
TUint8* name = sectionStart+block->iOffsetOfDllName;
|
williamr@2
|
1118 |
if(name<sectionStart || name>=sectionEnd)
|
williamr@2
|
1119 |
RETURN_FAILURE(KErrCorrupt); // not within import section
|
williamr@2
|
1120 |
while(*name++ && name<sectionEnd)
|
williamr@2
|
1121 |
{}
|
williamr@2
|
1122 |
if(name[-1])
|
williamr@2
|
1123 |
RETURN_FAILURE(KErrCorrupt); // name overflows section
|
williamr@2
|
1124 |
E32IMAGEHEADER_TRACE(("E32ImportBlock %s",sectionStart+block->iOffsetOfDllName));
|
williamr@2
|
1125 |
|
williamr@2
|
1126 |
// process import count...
|
williamr@2
|
1127 |
TUint numberOfImports = block->iNumberOfImports;
|
williamr@2
|
1128 |
if(numberOfImports>=0x80000000u/sizeof(TUint32))
|
williamr@2
|
1129 |
RETURN_FAILURE(KErrCorrupt); // size doesn't fit into a signed integer
|
williamr@2
|
1130 |
if(numberOfImports>biggestImportCount)
|
williamr@2
|
1131 |
biggestImportCount = numberOfImports;
|
williamr@2
|
1132 |
totalImports += numberOfImports;
|
williamr@2
|
1133 |
|
williamr@2
|
1134 |
// process import data...
|
williamr@2
|
1135 |
|
williamr@2
|
1136 |
// PE2 doesn't have any more data...
|
williamr@2
|
1137 |
if(importFormat==KImageImpFmt_PE2)
|
williamr@2
|
1138 |
continue;
|
williamr@2
|
1139 |
|
williamr@2
|
1140 |
// get import data range...
|
williamr@2
|
1141 |
TUint32* imports = (TUint32*)p;
|
williamr@2
|
1142 |
TUint32* importsEnd = imports+numberOfImports;
|
williamr@2
|
1143 |
if(importsEnd<imports)
|
williamr@2
|
1144 |
RETURN_FAILURE(KErrCorrupt); // math overflow. Fuzzer can't trigger this because needs aBufferStart to be in to be >0x80000000
|
williamr@2
|
1145 |
if(importsEnd>(TUint32*)sectionEnd)
|
williamr@2
|
1146 |
RETURN_FAILURE(KErrCorrupt); // overflows buffer
|
williamr@2
|
1147 |
|
williamr@2
|
1148 |
// move pointer on to next block...
|
williamr@2
|
1149 |
p = (TUint8*)importsEnd;
|
williamr@2
|
1150 |
|
williamr@2
|
1151 |
if(importFormat==KImageImpFmt_ELF)
|
williamr@2
|
1152 |
{
|
williamr@2
|
1153 |
// check imports are in code section...
|
williamr@2
|
1154 |
TUint32 limit = iCodeSize-sizeof(TUint32);
|
williamr@2
|
1155 |
while(imports<importsEnd)
|
williamr@2
|
1156 |
{
|
williamr@2
|
1157 |
TUint32 i = *imports++;
|
williamr@2
|
1158 |
if(i>limit)
|
williamr@2
|
1159 |
RETURN_FAILURE(KErrCorrupt);
|
williamr@2
|
1160 |
if(i&pointerAlignMask)
|
williamr@2
|
1161 |
RETURN_FAILURE(KErrCorrupt); // not word aligned
|
williamr@2
|
1162 |
}
|
williamr@2
|
1163 |
}
|
williamr@2
|
1164 |
else if(importFormat==KImageImpFmt_PE)
|
williamr@2
|
1165 |
{
|
williamr@2
|
1166 |
// import data is not used, so don't bother checking it
|
williamr@2
|
1167 |
}
|
williamr@2
|
1168 |
else
|
williamr@2
|
1169 |
{
|
williamr@2
|
1170 |
RETURN_FAILURE(KErrCorrupt); // bad import format, Fuzzer can't trigger this because import format checked by header validation
|
williamr@2
|
1171 |
}
|
williamr@2
|
1172 |
|
williamr@2
|
1173 |
// next block...
|
williamr@2
|
1174 |
p = (TUint8*)block->NextBlock(importFormat);
|
williamr@2
|
1175 |
}
|
williamr@2
|
1176 |
|
williamr@2
|
1177 |
// done processing imports; for PE derived files now check import address table (IAT)...
|
williamr@2
|
1178 |
if(importFormat==KImageImpFmt_PE || importFormat==KImageImpFmt_PE2)
|
williamr@2
|
1179 |
{
|
williamr@2
|
1180 |
if(totalImports>=0x80000000u/sizeof(TUint32))
|
williamr@2
|
1181 |
RETURN_FAILURE(KErrCorrupt); // size doesn't fit into a signed integer
|
williamr@2
|
1182 |
TUint importAddressTable = iTextSize; // offset for IAT
|
williamr@2
|
1183 |
if(importAddressTable&pointerAlignMask)
|
williamr@2
|
1184 |
RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because PE imports are for X86 which doesn't have alignment restrictions
|
williamr@2
|
1185 |
TUint importAddressTableEnd = importAddressTable+sizeof(TUint32)*totalImports;
|
williamr@2
|
1186 |
if(importAddressTableEnd<importAddressTable || importAddressTableEnd>TUint(iCodeSize))
|
williamr@2
|
1187 |
RETURN_FAILURE(KErrCorrupt); // import address table overflows code part of file
|
williamr@2
|
1188 |
E32IMAGEHEADER_TRACE(("E32ImportSection IAT offsets 0x%x..0x%x",importAddressTable,importAddressTableEnd));
|
williamr@2
|
1189 |
}
|
williamr@2
|
1190 |
|
williamr@2
|
1191 |
aBiggestImportCount = biggestImportCount;
|
williamr@2
|
1192 |
return KErrNone;
|
williamr@2
|
1193 |
}
|
williamr@2
|
1194 |
|
williamr@2
|
1195 |
|
williamr@2
|
1196 |
|
williamr@2
|
1197 |
|
williamr@2
|
1198 |
/**
|
williamr@2
|
1199 |
Validate a whole executable image.
|
williamr@2
|
1200 |
|
williamr@2
|
1201 |
This runs all of the other validation methods in turn.
|
williamr@2
|
1202 |
|
williamr@2
|
1203 |
@param aBufferStart Start of buffer containing the data after the header part of an image file.
|
williamr@2
|
1204 |
@param aBufferSize Size of data at aBufferStart.
|
williamr@2
|
1205 |
|
williamr@2
|
1206 |
@return KErrNone if image is valid, else KErrCorrupt or KErrNotSupported.
|
williamr@2
|
1207 |
*/
|
williamr@2
|
1208 |
TInt E32ImageHeaderV::ValidateWholeImage(TAny* aBufferStart, TUint aBufferSize) const
|
williamr@2
|
1209 |
{
|
williamr@2
|
1210 |
TUint32 dummyUncompressedSize;
|
williamr@2
|
1211 |
TInt r = ValidateHeader(TotalSize()+aBufferSize,dummyUncompressedSize);
|
williamr@2
|
1212 |
if(r!=KErrNone)
|
williamr@2
|
1213 |
return r;
|
williamr@2
|
1214 |
|
williamr@2
|
1215 |
TInt endOfCodeOffset = iCodeSize;
|
williamr@2
|
1216 |
void* restOfFileData = ((TUint8*)aBufferStart)+endOfCodeOffset;
|
williamr@2
|
1217 |
TInt restOfFileSize = aBufferSize-endOfCodeOffset;
|
williamr@2
|
1218 |
|
williamr@2
|
1219 |
E32RelocSection* dummy;
|
williamr@2
|
1220 |
r = ValidateRelocations(restOfFileData,restOfFileSize,iCodeRelocOffset,iCodeSize,dummy);
|
williamr@2
|
1221 |
if(r!=KErrNone)
|
williamr@2
|
1222 |
return r;
|
williamr@2
|
1223 |
r = ValidateRelocations(restOfFileData,restOfFileSize,iDataRelocOffset,iDataSize,dummy);
|
williamr@2
|
1224 |
if(r!=KErrNone)
|
williamr@2
|
1225 |
return r;
|
williamr@2
|
1226 |
|
williamr@2
|
1227 |
TUint biggestImportCount;
|
williamr@2
|
1228 |
r = ValidateImports(restOfFileData,restOfFileSize,biggestImportCount);
|
williamr@2
|
1229 |
if(r!=KErrNone)
|
williamr@2
|
1230 |
return r;
|
williamr@2
|
1231 |
|
williamr@2
|
1232 |
return r;
|
williamr@2
|
1233 |
}
|
williamr@2
|
1234 |
|
williamr@2
|
1235 |
|
williamr@2
|
1236 |
#endif // INCLUDE_E32IMAGEHEADER_IMPLEMENTATION
|
williamr@2
|
1237 |
|
williamr@2
|
1238 |
|
williamr@2
|
1239 |
#endif // __F32IMAGE_H__
|