sl@0: // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // f32\inc\f32image.h sl@0: // sl@0: // WARNING: This file contains some APIs which are internal and are subject sl@0: // to change without notice. Such APIs should therefore not be used sl@0: // outside the Kernel and Hardware Services package. sl@0: // sl@0: sl@0: /** sl@0: @file f32\inc\f32image.h sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #ifndef __F32IMAGE_H__ sl@0: #define __F32IMAGE_H__ sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /** sl@0: Value used for E32ImageHeader::iCpuIdentifier. sl@0: */ sl@0: enum TCpu sl@0: { sl@0: ECpuUnknown=0, ECpuX86=0x1000, ECpuArmV4=0x2000, ECpuArmV5=0x2001, ECpuArmV6=0x2002, ECpuMCore=0x4000 sl@0: }; sl@0: sl@0: /** sl@0: Ordinal value of the first entry in an executables export directory. sl@0: @see E32ImageHeader::iExportDirOffset. sl@0: */ sl@0: const TInt KOrdinalBase=1; sl@0: sl@0: /** sl@0: Value used to initialise E32ImageHeader::iHeaderCrc prior to CRC generation. sl@0: */ sl@0: const TUint32 KImageCrcInitialiser = 0xc90fdaa2u; sl@0: sl@0: sl@0: /** sl@0: Byte offset from an executable's entrypoint to the code segment ID storage location. sl@0: */ sl@0: const TUint KCodeSegIdOffset = 12; sl@0: sl@0: // sl@0: // Flags fields for E32ImageHeader::iFlags sl@0: // sl@0: sl@0: const TUint KImageDll = 0x00000001u; ///< Flag set if executable is a DLL, clear if an EXE. sl@0: sl@0: const TUint KImageNoCallEntryPoint = 0x00000002u; ///< Obsolete flag ignored since Symbian OS version 8.1b. sl@0: sl@0: const TUint KImageFixedAddressExe = 0x00000004u; ///< Executable's data should not move when running on the moving memory model. sl@0: sl@0: const TUint KImageABIMask = 0x00000018u; ///< Bitmask for ABI value. sl@0: const TInt KImageABIShift = 3; ///< Bit shift count for ABI value. sl@0: const TUint KImageABI_GCC98r2 = 0x00000000u; ///< Obsolete ABI for ARM targets. sl@0: const TUint KImageABI_EABI = 0x00000008u; ///< ARM EABI sl@0: sl@0: const TUint KImageEptMask = 0x000000e0u; ///< Bitmask for Entrypoint value. sl@0: const TInt KImageEptShift = 5; ///< Bit shift count for Entrypoint value sl@0: const TUint KImageEpt_Eka1 = 0x00000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b. sl@0: const TUint KImageEpt_Eka2 = 0x00000020u; ///< Standard entrypoint for ARM executable. sl@0: sl@0: const TUint KImageCodeUnpaged = 0x00000100u; ///< Executable image should not be demand paged. Exclusive with KImageCodePaged, sl@0: const TUint KImageCodePaged = 0x00000200u; ///< Executable image should be demand paged. Exclusive with KImageCodeUnpaged, sl@0: sl@0: const TUint KImageNmdExpData = 0x00000400u; ///< Flag to indicate when named symbol export data present in image sl@0: sl@0: const TUint KImageDebuggable = 0x00000800u; ///< Flag to indicate image is debuggable sl@0: sl@0: const TUint KImageDataUnpaged = 0x00001000u; ///< Flag to indicate the image should not be data paged. Exclusive with KImageDataPaged. sl@0: const TUint KImageDataPaged = 0x00002000u; ///< Flag to indicate the image should be data paged. Exclusive with KImageDataUnpaged. sl@0: const TUint KImageDataPagingMask = KImageDataUnpaged | KImageDataPaged; ///< Mask for data paging flags. sl@0: sl@0: const TUint KImageSMPSafe = 0x00004000u; ///< Flag to indicate image is SMP safe sl@0: sl@0: const TUint KImageHWFloatMask = 0x00f00000u; ///< Bitmask for Floating Point type. sl@0: const TInt KImageHWFloatShift = 20; ///< Bit shift count for Floating Point type. sl@0: const TUint KImageHWFloat_None = EFpTypeNone << KImageHWFloatShift; ///< No hardware floating point used. sl@0: const TUint KImageHWFloat_VFPv2 = EFpTypeVFPv2 << KImageHWFloatShift; ///< ARM VFPv2 floating point used. sl@0: const TUint KImageHWFloat_VFPv3 = EFpTypeVFPv3 << KImageHWFloatShift; ///< ARM VFPv3 floating point used. This includes Advanced SIMD (NEON). sl@0: const TUint KImageHWFloat_VFPv3D16 = EFpTypeVFPv3D16 << KImageHWFloatShift; ///< ARM VFPv3-D16 floating point used. This does not include Advanced SIMD (NEON). sl@0: sl@0: const TUint KImageHdrFmtMask = 0x0f000000u; ///< Bitmask for header format type. sl@0: const TInt KImageHdrFmtShift = 24; ///< Bit shift count for header format type. sl@0: const TUint KImageHdrFmt_Original = 0x00000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b. sl@0: const TUint KImageHdrFmt_J = 0x01000000u; ///< @removed Obsolete format not used since Symbian OS version 8.1b. sl@0: const TUint KImageHdrFmt_V = 0x02000000u; ///< Header has format given by class E32ImageHeaderV. sl@0: sl@0: const TUint KImageImpFmtMask = 0xf0000000u; ///< Bitmask for import section format type. sl@0: const TInt KImageImpFmtShift = 28; ///< Bit shift count for import section format type. sl@0: const TUint KImageImpFmt_PE = 0x00000000u; ///< PE-derived imports. sl@0: const TUint KImageImpFmt_ELF = 0x10000000u; ///< ELF-derived imports. sl@0: const TUint KImageImpFmt_PE2 = 0x20000000u; ///< PE-derived imports without redundant copy of import ordinals. sl@0: sl@0: sl@0: sl@0: sl@0: // forward references... sl@0: class RFile; sl@0: class E32RelocSection; sl@0: sl@0: sl@0: /** sl@0: Structure for an executable image's header. sl@0: This is extended by E32ImageHeaderComp and E32ImageHeaderV. sl@0: All executables since Symbian OS version 8.1b have an header given by class E32ImageHeaderV. sl@0: sl@0: Summary of an executable image structure... sl@0: sl@0: - Header, 0..iCodeOffset-1 sl@0: - Code part, iCodeOffset..iCodeOffset+iCodeSize-1 sl@0: - .text section, 0 + iTextSize sl@0: - Import Address Table (IAT), iText + ? sl@0: - Export Directory, iExportDirOffset + iExportDirCount*4 (in .text Section) sl@0: - Rest of data, iCodeOffset+iCodeSize..EOF sl@0: - .data section, iDataOffset + iDataSize sl@0: - Import section, iImportOffset + sizeof(E32ImportSection)+? sl@0: - Code relocation section, iCodeRelocOffset + sizeof(E32RelocSection)+? sl@0: - Data relocation section, iDataRelocOffset + sizeof(E32RelocSection)+? sl@0: */ sl@0: class E32ImageHeader sl@0: { sl@0: public: sl@0: static TInt New(E32ImageHeader*& aHdr, RFile& aFile); sl@0: static TInt New(E32ImageHeader*& aHdr, TUint8* aFileData, TUint32 aFileSize); sl@0: TInt ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const; sl@0: sl@0: inline static TUint ABIFromFlags(TUint aFlags); sl@0: inline static TUint EptFromFlags(TUint aFlags); sl@0: inline static TUint HdrFmtFromFlags(TUint aFlags); sl@0: inline static TUint ImpFmtFromFlags(TUint aFlags); sl@0: sl@0: inline TUint ABI() const; sl@0: inline TUint EntryPointFormat() const; sl@0: inline TUint HeaderFormat() const; sl@0: inline TUint ImportFormat() const; sl@0: sl@0: inline TUint32 CompressionType() const; sl@0: inline TUint32 ModuleVersion() const; sl@0: inline TInt TotalSize() const; sl@0: inline TInt UncompressedFileSize() const; sl@0: inline void GetSecurityInfo(SSecurityInfo& aInfo) const; sl@0: inline TCpu CpuIdentifier() const; sl@0: inline TProcessPriority ProcessPriority() const; sl@0: inline TUint32 ExceptionDescriptor() const; sl@0: public: sl@0: TUint32 iUid1; ///< KDynamicLibraryUidValue or KExecutableImageUidValue sl@0: TUint32 iUid2; ///< Second UID for executable. sl@0: TUint32 iUid3; ///< Third UID for executable. sl@0: TUint32 iUidChecksum; ///< Checksum for iUid1, iUid2 and iUid3. sl@0: TUint iSignature; ///< Contains 'EPOC'. sl@0: TUint32 iHeaderCrc; ///< CRC-32 of entire header. @see #KImageCrcInitialiser. sl@0: TUint32 iModuleVersion; ///< Version number for this executable (used in link resolution). sl@0: TUint32 iCompressionType; ///< Type of compression used for file contents located after the header. (UID or 0 for none). sl@0: TVersion iToolsVersion; ///< Version number of tools which generated this file. sl@0: TUint32 iTimeLo; ///< Least significant 32 bits of the time of image creation, in milliseconds since since midnight Jan 1st, 2000. sl@0: TUint32 iTimeHi; ///< Most significant 32 bits of the time of image creation, in milliseconds since since midnight Jan 1st, 2000. sl@0: TUint iFlags; ///< Contains various bit-fields of attributes for the image. sl@0: TInt iCodeSize; ///< Size of executables code. Includes import address table, constant data and export directory. sl@0: TInt iDataSize; ///< Size of executables initialised data. sl@0: TInt iHeapSizeMin; ///< Minimum size for an EXEs runtime heap memory. sl@0: TInt iHeapSizeMax; ///< Maximum size for an EXEs runtime heap memory. sl@0: TInt iStackSize; ///< Size for stack required by an EXEs initial thread. sl@0: TInt iBssSize; ///< Size of executables uninitialised data. sl@0: TUint iEntryPoint; ///< Offset into code of the entry point. sl@0: TUint iCodeBase; ///< Virtual address that the executables code is linked for. sl@0: TUint iDataBase; ///< Virtual address that the executables data is linked for. sl@0: TInt iDllRefTableCount; ///< Number of executable against which this executable is linked. The number of files mention in the import section at iImportOffset. sl@0: TUint iExportDirOffset; ///< Byte offset into file of the export directory. sl@0: TInt iExportDirCount; ///< Number of entries in the export directory. sl@0: TInt iTextSize; ///< Size of just the text section, also doubles as the offset for the Import Address Table w.r.t. the code section. sl@0: TUint iCodeOffset; ///< Offset into file of the code section. Also doubles the as header size. sl@0: TUint iDataOffset; ///< Offset into file of the data section. sl@0: TUint iImportOffset; ///< Offset into file of the import section (E32ImportSection). sl@0: TUint iCodeRelocOffset; ///< Offset into file of the code relocation section (E32RelocSection). sl@0: TUint iDataRelocOffset; ///< Offset into file of the data relocation section (E32RelocSection). sl@0: TUint16 iProcessPriority; ///< Initial runtime process priorty for an EXE. (Value from enum TProcessPriority.) sl@0: TUint16 iCpuIdentifier; ///< Value from enum TCpu which indicates the CPU architecture for which the image was created sl@0: }; sl@0: sl@0: sl@0: /** sl@0: Extends E32ImageHeader. sl@0: */ sl@0: class E32ImageHeaderComp : public E32ImageHeader sl@0: { sl@0: public: sl@0: TUint32 iUncompressedSize; ///< Uncompressed size of file data after the header, or zero if file not compressed. sl@0: }; sl@0: sl@0: sl@0: /** sl@0: Extends E32ImageHeaderComp. sl@0: All Symbian OS executable files have a header in this format since OS version 8.1b. sl@0: */ sl@0: class E32ImageHeaderV : public E32ImageHeaderComp sl@0: { sl@0: public: sl@0: SSecurityInfo iS; ///< Platform Security information of executable. sl@0: TUint32 iExceptionDescriptor; ///< Offset in bytes from start of code section to Exception Descriptor, bit 0 set if valid. sl@0: TUint32 iSpare2; ///< Reserved for future use. Set to zero. sl@0: TUint16 iExportDescSize; ///< Size of export description stored in iExportDesc. sl@0: TUint8 iExportDescType; ///< Type of description of holes in export table sl@0: TUint8 iExportDesc[1]; ///< Description of holes in export table, size given by iExportDescSize.. sl@0: public: sl@0: TInt ValidateWholeImage(TAny* aBufferStart, TUint aBufferSize) const; sl@0: TInt ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const; sl@0: TInt ValidateExportDescription() const; sl@0: TInt ValidateRelocations(TAny* aBufferStart, TUint aBufferSize, TUint aRelocationInfoOffset, TUint aRelocatedSectionSize, E32RelocSection*& aRelocationSection) const; sl@0: TInt ValidateImports(TAny* aBufferStart, TUint aBufferSize, TUint& aBiggestImportCount) const; sl@0: TInt ValidateAndAdjust(TUint32 aFileSize); sl@0: }; sl@0: sl@0: // export description type E32ImageHeaderV::iExportDescType sl@0: const TUint KImageHdr_ExpD_NoHoles =0x00; ///< No holes, all exports present. sl@0: const TUint KImageHdr_ExpD_FullBitmap =0x01; ///< Full bitmap present at E32ImageHeaderV::iExportDesc sl@0: const TUint KImageHdr_ExpD_SparseBitmap8 =0x02; ///< Sparse bitmap present at E32ImageHeaderV::iExportDesc, granularity 8 sl@0: const TUint KImageHdr_ExpD_Xip =0xff; ///< XIP file sl@0: sl@0: sl@0: // sl@0: // inline getters for E32ImageHeader sl@0: // sl@0: sl@0: /** sl@0: Extract ABI type from aFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::ABIFromFlags(TUint aFlags) sl@0: { sl@0: return aFlags & KImageABIMask; sl@0: } sl@0: sl@0: /** sl@0: Extract ABI type from #iFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::ABI() const sl@0: { sl@0: return ABIFromFlags(iFlags); sl@0: } sl@0: sl@0: /** sl@0: Extract entrypoint format from aFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::EptFromFlags(TUint aFlags) sl@0: { sl@0: return aFlags & KImageEptMask; sl@0: } sl@0: sl@0: /** sl@0: Extract entrypoint format from #iFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::EntryPointFormat() const sl@0: { sl@0: return EptFromFlags(iFlags); sl@0: } sl@0: sl@0: /** sl@0: Extract header format from aFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::HdrFmtFromFlags(TUint aFlags) sl@0: { sl@0: return aFlags & KImageHdrFmtMask; sl@0: } sl@0: sl@0: /** sl@0: Extract header format from #iFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::HeaderFormat() const sl@0: { sl@0: return HdrFmtFromFlags(iFlags); sl@0: } sl@0: sl@0: /** sl@0: Extract import format from aFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::ImpFmtFromFlags(TUint aFlags) sl@0: { sl@0: return aFlags & KImageImpFmtMask; sl@0: } sl@0: sl@0: /** sl@0: Extract import format from #iFlags. sl@0: */ sl@0: inline TUint E32ImageHeader::ImportFormat() const sl@0: { sl@0: return ImpFmtFromFlags(iFlags); sl@0: } sl@0: sl@0: /** sl@0: Return #iCompressionType. sl@0: */ sl@0: inline TUint32 E32ImageHeader::CompressionType() const sl@0: { sl@0: return iCompressionType; sl@0: } sl@0: sl@0: /** sl@0: Return #iModuleVersion. sl@0: */ sl@0: inline TUint32 E32ImageHeader::ModuleVersion() const sl@0: { sl@0: return iModuleVersion; sl@0: } sl@0: sl@0: /** sl@0: Return size of this header. sl@0: */ sl@0: inline TInt E32ImageHeader::TotalSize() const sl@0: { sl@0: return iCodeOffset; sl@0: } sl@0: sl@0: /** sl@0: Return total size of file after decompression, or -1 if file not compressed. sl@0: */ sl@0: inline TInt E32ImageHeader::UncompressedFileSize() const sl@0: { sl@0: if(iCompressionType==0) sl@0: return -1; // not compressed sl@0: else sl@0: return ((E32ImageHeaderComp*)this)->iUncompressedSize + TotalSize(); sl@0: } sl@0: sl@0: /** sl@0: Return copy of security info, #E32ImageHeaderV::iS. sl@0: */ sl@0: inline void E32ImageHeader::GetSecurityInfo(SSecurityInfo& aInfo) const sl@0: { sl@0: aInfo = ((E32ImageHeaderV*)this)->iS; sl@0: } sl@0: sl@0: /** sl@0: Return #iCpuIdentifier. sl@0: */ sl@0: inline TCpu E32ImageHeader::CpuIdentifier() const sl@0: { sl@0: return (TCpu)iCpuIdentifier; sl@0: } sl@0: sl@0: /** sl@0: Return #iProcessPriority. sl@0: */ sl@0: inline TProcessPriority E32ImageHeader::ProcessPriority() const sl@0: { sl@0: return (TProcessPriority)iProcessPriority; sl@0: } sl@0: sl@0: /** sl@0: Return fffset in bytes from start of code section for the Exception Descriptor. sl@0: Or zero if not present. sl@0: */ sl@0: inline TUint32 E32ImageHeader::ExceptionDescriptor() const sl@0: { sl@0: TUint32 xd = ((E32ImageHeaderV*)this)->iExceptionDescriptor; sl@0: sl@0: if((xd & 1) && (xd != 0xffffffffu)) sl@0: return (xd & ~1); sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: /** sl@0: A block of imports from a single executable. sl@0: These structures are conatined in a images Import Section (E32ImportSection). sl@0: */ sl@0: class E32ImportBlock sl@0: { sl@0: public: sl@0: inline const E32ImportBlock* NextBlock(TUint aImpFmt) const; sl@0: inline TInt Size(TUint aImpFmt) const; sl@0: inline const TUint* Imports() const; // import list if present sl@0: public: sl@0: TUint32 iOffsetOfDllName; ///< Offset from start of import section for a NUL terminated executable (DLL or EXE) name. sl@0: TInt iNumberOfImports; ///< Number of imports from this executable. sl@0: // TUint iImport[iNumberOfImports]; ///< For ELF-derived executes: list of code section offsets. For PE, list of imported ordinals. Omitted in PE2 import format sl@0: }; sl@0: sl@0: /** sl@0: Return size of this import block. sl@0: @param aImpFmt Import format as obtained from image header. sl@0: */ sl@0: inline TInt E32ImportBlock::Size(TUint aImpFmt) const sl@0: { sl@0: TInt r = sizeof(E32ImportBlock); sl@0: if(aImpFmt!=KImageImpFmt_PE2) sl@0: r += iNumberOfImports * sizeof(TUint); sl@0: return r; sl@0: } sl@0: sl@0: /** sl@0: Return pointer to import block which immediately follows this one. sl@0: @param aImpFmt Import format as obtained from image header. sl@0: */ sl@0: inline const E32ImportBlock* E32ImportBlock::NextBlock(TUint aImpFmt) const sl@0: { sl@0: const E32ImportBlock* next = this + 1; sl@0: if(aImpFmt!=KImageImpFmt_PE2) sl@0: next = (const E32ImportBlock*)( (TUint8*)next + iNumberOfImports * sizeof(TUint) ); sl@0: return next; sl@0: } sl@0: sl@0: /** sl@0: Return address of first import in this block. sl@0: For import format KImageImpFmt_ELF, imports are list of code section offsets. sl@0: For import format KImageImpFmt_PE, imports are a list of imported ordinals. sl@0: For import format KImageImpFmt_PE2, the import list is not present and should not be accessed. sl@0: */ sl@0: inline const TUint* E32ImportBlock::Imports() const sl@0: { sl@0: return (const TUint*)(this + 1); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Header for the Import Section in an image, as referenced by E32ImageHeader::iImportOffset. sl@0: Immediately following this structure are an array of E32ImportBlock structures. sl@0: The number of these is given by E32ImageHeader::iDllRefTableCount. sl@0: */ sl@0: class E32ImportSection sl@0: { sl@0: public: sl@0: TInt iSize; ///< Size of this section excluding 'this' structure sl@0: // E32ImportBlock iImportBlock[iDllRefTableCount]; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: A block of relocations for a single page (4kB) of code/data. sl@0: sl@0: Immediately following this structure are an array of TUint16 values sl@0: each representing a single value in the page which is to be relocated. sl@0: The lower 12 bits of each entry is the offset, in bytes, from start of this page. sl@0: The Upper 4 bits are the relocation type to be applied to the 32-bit value located sl@0: at that offset. sl@0: - 1 means relocate relative to code section. sl@0: - 2 means relocate relative to data section. sl@0: - 3 means relocate relative to code or data section; calculate which. sl@0: sl@0: A value of all zeros (0x0000) is ignored. (Used for padding structure to 4 byte alignment). sl@0: */ sl@0: class E32RelocBlock sl@0: { sl@0: public: sl@0: TUint32 iPageOffset; ///< Offset, in bytes, for the page being relocated; relative to the section start. Always a multiple of the page size: 4096 bytes. sl@0: TUint32 iBlockSize; ///< Size, in bytes, for this block structure. Always a multiple of 4. sl@0: // TUint16 iEntry[] sl@0: }; sl@0: sl@0: sl@0: /** sl@0: Header for a Relocation Section in an image, as referenced by E32ImageHeader::iCodeRelocOffset sl@0: or E32ImageHeader::iDataRelocOffset. sl@0: sl@0: Immediately following this structure are an array of E32RelocBlock structures. sl@0: */ sl@0: class E32RelocSection sl@0: { sl@0: public: sl@0: TInt iSize; ///< Size of this relocation section including 'this' structure. Always a multiple of 4. sl@0: TInt iNumberOfRelocs; ///< Number of relocations in this section. sl@0: // E32RelocBlock iRelockBlock[]; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: Structure contained in the export directory in text section of the stdexe/stddll. sl@0: It contains information on the names of symbols exported by this stdexe/stddll and sl@0: pointers to a E32EpocExpSymInfoHdr structure of any stddlls that are dependencies of sl@0: this stdexe/stddll. sl@0: sl@0: This is not used for emulator images see E32EmulExpSymInfoHdr below. sl@0: @see E32EmulExpSymInfoHdr sl@0: */ sl@0: class E32EpocExpSymInfoHdr sl@0: { sl@0: public: sl@0: TInt iSize; // size of this Table sl@0: TInt16 iFlags; sl@0: TInt16 iSymCount; // number of symbols sl@0: TInt iSymbolTblOffset; // start of the symbol table - offset from byte 0 of this header sl@0: TInt iStringTableSz; // size of the string table sl@0: TInt iStringTableOffset; // start of the string table having names of the symbols - offset from byte 0 of this header sl@0: TInt iDllCount; // Number of dependent DLLs sl@0: TInt iDepDllZeroOrdTableOffset; // offset of the DLL dependency table - offset from byte 0 of this header. sl@0: }; sl@0: sl@0: sl@0: /** sl@0: Header of the structure contained in the 'KWin32SectionName_NmdExpData' sl@0: segment of emulator stdexe & stddll images. sl@0: The segment contains addresses of symbols and NULL sl@0: terminated ASCII strings of the names of static dependencies. sl@0: For a stdexe, this segment contains the following: sl@0: a) symbol count (iSymCount) and static dependency count (iDllCount) sl@0: b) iSymCount * symbol addresses sl@0: c) iSymCount * symbol names sl@0: d) iDllCount * dependency names sl@0: sl@0: For a stddll, this segment contains the following: sl@0: a) symbol count (iSymCout) is always 0 sl@0: b) static dependency count (iDllCount) sl@0: c) iDllCount * dependency names sl@0: The symbol addresses and names are not required for a stddll as the Windows API, sl@0: GetProcAddress may be used to get the addresses for symbol names. sl@0: Since this API works only on DLL handles, we explicitly list them for stdexes. sl@0: This is used for emulator images only. sl@0: */ sl@0: class E32EmulExpSymInfoHdr sl@0: { sl@0: public: sl@0: TInt32 iSymCount; // Number of symbols sl@0: TInt32 iDllCount; // Number of static dependency DLLs sl@0: }; sl@0: sl@0: sl@0: sl@0: #ifdef INCLUDE_E32IMAGEHEADER_IMPLEMENTATION sl@0: sl@0: // include code which implements validation functions... sl@0: sl@0: #ifndef RETURN_FAILURE sl@0: #define RETURN_FAILURE(_r) return (_r) sl@0: #endif sl@0: sl@0: #ifndef E32IMAGEHEADER_TRACE sl@0: #define E32IMAGEHEADER_TRACE(_t) ((void)0) sl@0: #endif sl@0: sl@0: sl@0: #include sl@0: sl@0: sl@0: /** sl@0: Validate this image header. sl@0: sl@0: After successful validation the following are true: sl@0: - File size is big enough to contain the entire header. sl@0: - Values #iUidChecksum, #iSignature and #iHeaderCrc are correct. sl@0: - CPU type (#iCpuIdentifier), ABI type (#iFlags&#KImageABIMask) and sl@0: entrypoint type (#iFlags&#KImageEptMask) are valid for this system. sl@0: - Code part of file as specified by #iCodeOffset and #iCodeSize is fully within the file. sl@0: - Text section size (#iTextSize) is within code part. sl@0: - Entrypoint value (#iEntryPoint) lies within the code part and is aligned correctly. sl@0: - Export directory as specified by #iExportDirCount and #iExportDirOffset is fully sl@0: within code part and is aligned correctly. sl@0: - Exception description (E32ImageHeaderV::iExceptionDescriptor), if present, sl@0: lies within the code part. sl@0: - Data part of file as specified by #iDataOffset and #iDataSize is fully within the file. sl@0: Or data is not present (#iDataOffset==#iDataSize==0). sl@0: - Import section (class E32ImportSection at #iImportOffset) is within 'rest of data' sl@0: and aligned correctly. Data following the E32ImportSection header is NOT validated or sl@0: checked if it is fully contained within the file. sl@0: - Code relocations (class E32RelocSection at #iCodeRelocOffset) is within 'rest of data' sl@0: and aligned correctly. Data following the E32RelocSection header is NOT validated or sl@0: checked if it is fully contained within the file. sl@0: - Data relocations (class E32RelocSection at #iDataRelocOffset) is within 'rest of data' sl@0: and aligned correctly. Data following the E32RelocSection header is NOT validated or sl@0: checked if it is fully contained within the file. sl@0: - Export description is validated by E32ImageHeaderV::ValidateExportDescription(). sl@0: - #iUid1 is consistant with #iFlags&#KImageDll. I.e. if flaged as a DLL, #iUid1 is sl@0: KDynamicLibraryUidValue, otherwise it is KExecutableImageUidValue. sl@0: - Version number (#iModuleVersion) is valid. (Major and minor versions are <32768). sl@0: - File compression type (#iCompressionType) is supported. sl@0: - #iHeapSizeMax>=#iHeapSizeMin sl@0: - All signed values in header are not negative. sl@0: sl@0: @param aFileSize Total size of the file from which this header was created. sl@0: @param[out] aUncompressedSize Returns the total size that the file data would be once decompressed. sl@0: sl@0: @return KErrNone if no errors detected; sl@0: KErrCorrupt if errors found; sl@0: KErrNotSupported if image format not supported on this platform. sl@0: */ sl@0: TInt E32ImageHeader::ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const sl@0: { sl@0: // check file is big enough for any header... sl@0: if(TUint(aFileSize)ValidateHeader(aFileSize,aUncompressedSize); sl@0: sl@0: return KErrNotSupported; // header format unrecognised sl@0: } sl@0: sl@0: /** sl@0: Validate this image header. sl@0: sl@0: @param aFileSize Total size of the file from which this header was created. sl@0: @param[out] aUncompressedSize Returns the total size that the file data would be once decompressed. sl@0: sl@0: @return KErrNone if no errors detected; sl@0: KErrCorrupt if errors found; sl@0: KErrNotSupported if image format not supported on this platform. sl@0: */ sl@0: TInt E32ImageHeaderV::ValidateHeader(TInt aFileSize, TUint32& aUncompressedSize) const sl@0: { sl@0: const TUint KMaxDesSize = 0x0fffffffu; // maximum size of descriptor sl@0: if(aFileSize==-1) sl@0: { sl@0: // file size unknown, set to maximum valid so rest of validation works... sl@0: aFileSize = KMaxDesSize; sl@0: } sl@0: if(TUint(aFileSize)>KMaxDesSize) sl@0: RETURN_FAILURE(KErrCorrupt); // file size negative or too big sl@0: sl@0: aUncompressedSize = 0; sl@0: sl@0: // check file is big enough to contain this header... sl@0: if(aFileSize<(TInt)sizeof(*this)) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check header format version... sl@0: if((iFlags&KImageHdrFmtMask)!=KImageHdrFmt_V) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: sl@0: // check header size... sl@0: TUint headerSize = iCodeOffset; sl@0: if(headerSize>TUint(aFileSize)) sl@0: RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because Loader will fail earlier when reading header from file sl@0: sl@0: // check iCpuIdentifier... sl@0: TCpu cpu = (TCpu)iCpuIdentifier; sl@0: TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6); sl@0: #if defined(__CPU_ARM) sl@0: if(!isARM) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: #elif defined(__CPU_X86) sl@0: if(cpu!=ECpuX86) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: #endif sl@0: TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets sl@0: sl@0: // check iUid1,iUid2,iUid3,iUidChecksum... sl@0: TUidType uids = *(const TUidType*)&iUid1; sl@0: TCheckedUid chkuid(uids); sl@0: const TUint32* pChkUid = (const TUint32*)&chkuid; // need hackery to verify the UID checksum since everything is private sl@0: if(pChkUid[3]!=iUidChecksum) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iSignature... sl@0: if(iSignature!=0x434f5045) // 'EPOC' sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iHeaderCrc... sl@0: TUint32 supplied_crc = iHeaderCrc; sl@0: ((E32ImageHeaderV*)this)->iHeaderCrc = KImageCrcInitialiser; sl@0: TUint32 crc = 0; sl@0: Mem::Crc32(crc, this, headerSize); sl@0: ((E32ImageHeaderV*)this)->iHeaderCrc = supplied_crc; sl@0: if(crc!=supplied_crc) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iModuleVersion... sl@0: TUint32 mv = iModuleVersion; sl@0: if(mv>=0x80000000u || (mv&0x0000ffffu)>0x8000u) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: sl@0: // check iCompressionType and get uncompressed size... sl@0: TUint compression = iCompressionType; sl@0: TUint uncompressedSize = aFileSize; sl@0: if(compression!=KFormatNotCompressed) sl@0: { sl@0: if(compression!=KUidCompressionDeflate && compression!=KUidCompressionBytePair) sl@0: RETURN_FAILURE(KErrNotSupported); // unknown compression method sl@0: uncompressedSize = headerSize+iUncompressedSize; sl@0: if(uncompressedSizeKMaxDesSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check KImageDll in iFlags... sl@0: if(iFlags&KImageDll) sl@0: { sl@0: if(iUid1!=TUint32(KDynamicLibraryUidValue)) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: } sl@0: else if(iUid1!=TUint32(KExecutableImageUidValue)) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: sl@0: // check iFlags for ABI and entry point types... sl@0: if(isARM) sl@0: { sl@0: if((iFlags&KImageEptMask)!=KImageEpt_Eka2) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: #if defined(__EABI__) sl@0: if((iFlags&KImageABIMask)!=KImageABI_EABI) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: #elif defined(__GCC32__) sl@0: if((iFlags&KImageABIMask)!=KImageABI_GCC98r2) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: #endif sl@0: } sl@0: else sl@0: { sl@0: if(iFlags&KImageEptMask) sl@0: RETURN_FAILURE(KErrNotSupported); // no special entry point type allowed on non-ARM targets sl@0: if(iFlags&KImageABIMask) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: } sl@0: sl@0: // check iFlags for import format... sl@0: if((iFlags&KImageImpFmtMask)>KImageImpFmt_PE2) sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: sl@0: // check iHeapSizeMin... sl@0: if(iHeapSizeMin<0) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iHeapSizeMax... sl@0: if(iHeapSizeMax=TUint(iCodeSize)) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if(iEntryPoint+KCodeSegIdOffset+sizeof(TUint32)>TUint(iCodeSize)) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if(iEntryPoint&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned sl@0: sl@0: // check iCodeBase... sl@0: if(iCodeBase&3) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned sl@0: sl@0: // check iDataBase... sl@0: if(iDataBase&3) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned sl@0: sl@0: // check iDllRefTableCount... sl@0: if(iDllRefTableCount<0) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if(iDllRefTableCount) sl@0: { sl@0: if(!iImportOffset) sl@0: RETURN_FAILURE(KErrCorrupt); // we link to DLLs but have no import data sl@0: } sl@0: sl@0: // check iCodeOffset and iCodeSize specify region in file... sl@0: TUint codeStart = iCodeOffset; sl@0: TUint codeEnd = codeStart+iCodeSize; sl@0: if(codeEnduncompressedSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iDataOffset and iDataSize specify region in file... sl@0: TUint dataStart = iDataOffset; sl@0: TUint dataEnd = dataStart+iDataSize; sl@0: if(dataEnduncompressedSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if((dataStart-codeStart)&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // data not aligned with respect to code sl@0: } sl@0: sl@0: sl@0: // check total data size isn't too bit... sl@0: TUint totalDataSize = iDataSize+iBssSize; sl@0: if(totalDataSize>0x7fff0000) sl@0: RETURN_FAILURE(KErrNoMemory); sl@0: sl@0: // check iExportDirOffset and iExportDirCount specify region in code part... sl@0: if(TUint(iExportDirCount)>65535) sl@0: RETURN_FAILURE(KErrCorrupt); // too many exports sl@0: if(iExportDirCount) sl@0: { sl@0: TUint exportsStart = iExportDirOffset; sl@0: TUint exportsEnd = exportsStart+iExportDirCount*sizeof(TUint32); sl@0: if(iFlags&KImageNmdExpData) sl@0: exportsStart -= sizeof(TUint32); // allow for 0th ordinal sl@0: if(exportsEndcodeEnd) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if((exportsStart-codeStart)&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned within code section sl@0: } sl@0: sl@0: // check iTextSize... sl@0: if(TUint(iTextSize)>TUint(iCodeSize)) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // check iImportOffset... sl@0: TUint start = iImportOffset; sl@0: if(start) sl@0: { sl@0: TUint end = start+sizeof(E32ImportSection); // minimum valid size sl@0: if(enduncompressedSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if((start-codeEnd)&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data' sl@0: } sl@0: sl@0: // check iCodeRelocOffset... sl@0: start = iCodeRelocOffset; sl@0: if(start) sl@0: { sl@0: TUint end = start+sizeof(E32RelocSection); // minimum valid size sl@0: if(enduncompressedSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if((start-codeEnd)&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data' sl@0: } sl@0: sl@0: // check iDataRelocOffset... sl@0: start = iDataRelocOffset; sl@0: if(start) sl@0: { sl@0: TUint end = start+sizeof(E32RelocSection); // minimum valid size sl@0: if(enduncompressedSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if((start-codeEnd)&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned within 'rest of data' sl@0: } sl@0: sl@0: // check exception descriptor... sl@0: if(iExceptionDescriptor&1) // if valid... sl@0: if(iExceptionDescriptor>=TUint(iCodeSize)) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: TInt r = ValidateExportDescription(); sl@0: if(r!=KErrNone) sl@0: RETURN_FAILURE(r); sl@0: sl@0: // done... sl@0: aUncompressedSize = uncompressedSize; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Valdate that the export description is valid. sl@0: */ sl@0: TInt E32ImageHeaderV::ValidateExportDescription() const sl@0: { sl@0: TUint headerSize = iCodeOffset; sl@0: sl@0: // check export description... sl@0: TUint edSize = iExportDescSize + sizeof(iExportDescSize) + sizeof(iExportDescType); sl@0: edSize = (edSize+3)&~3; sl@0: TUint edEnd = _FOFF(E32ImageHeaderV,iExportDescSize)+edSize; sl@0: if(edEnd!=headerSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: sl@0: // size of bitmap of exports... sl@0: TUint bitmapSize = (iExportDirCount+7) >> 3; sl@0: sl@0: // check export description bitmap... sl@0: switch(iExportDescType) sl@0: { sl@0: case KImageHdr_ExpD_NoHoles: sl@0: // no bitmap to check... sl@0: E32IMAGEHEADER_TRACE(("ValidateExportDescription NoHoles")); sl@0: return KErrNone; sl@0: sl@0: case KImageHdr_ExpD_FullBitmap: sl@0: // full bitmap present... sl@0: E32IMAGEHEADER_TRACE(("ValidateExportDescription FullBitmap")); sl@0: if(bitmapSize!=iExportDescSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: return KErrNone; sl@0: sl@0: case KImageHdr_ExpD_SparseBitmap8: sl@0: { sl@0: // sparse bitmap present... sl@0: E32IMAGEHEADER_TRACE(("ValidateExportDescription SparseBitmap8")); sl@0: sl@0: // get size of meta-bitmap... sl@0: TUint metaBitmapSize = (bitmapSize+7) >> 3; sl@0: if(metaBitmapSize>iExportDescSize) sl@0: RETURN_FAILURE(KErrCorrupt); // doesn't fit sl@0: sl@0: TUint totalSize = metaBitmapSize; sl@0: sl@0: // scan meta-bitmap counting extra bytes which should be present... sl@0: const TUint8* metaBitmap = iExportDesc; sl@0: const TUint8* metaBitmapEnd = metaBitmap + metaBitmapSize; sl@0: while(metaBitmap>=1); sl@0: } sl@0: sl@0: if(totalSize!=iExportDescSize) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: } sl@0: return KErrNone; sl@0: sl@0: default: sl@0: E32IMAGEHEADER_TRACE(("ValidateExportDescription ?")); sl@0: RETURN_FAILURE(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: sl@0: /** sl@0: Validate a relocation section. sl@0: sl@0: @param aBufferStart Start of buffer containing the data after the code part in the image file. sl@0: @param aBufferSize Size of data at aBufferStart. sl@0: @param aRelocationInfoOffset File offset for relocation section. (#iCodeRelocOffset or #iDataRelocOffset.) sl@0: @param aRelocatedSectionSize Size of section being relocated. (#iCodeSize or #iDataSize.) sl@0: @param[out] aRelocationSection Set to the start of the relocation section in the given buffer. sl@0: sl@0: @return KErrNone if relocation section is valid, else KErrCorrupt. sl@0: */ sl@0: TInt E32ImageHeaderV::ValidateRelocations(TAny* aBufferStart, TUint aBufferSize, TUint aRelocationInfoOffset, TUint aRelocatedSectionSize, E32RelocSection*& aRelocationSection) const sl@0: { sl@0: aRelocationSection = 0; sl@0: if(!aRelocationInfoOffset) sl@0: return KErrNone; // no relocations sl@0: sl@0: // get alignment requirements... sl@0: TCpu cpu = (TCpu)iCpuIdentifier; sl@0: TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6); sl@0: TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets sl@0: sl@0: // buffer pointer to read relocation from... sl@0: TUint8* bufferStart = (TUint8*)aBufferStart; sl@0: TUint8* bufferEnd = bufferStart+aBufferSize; sl@0: TUint baseOffset = iCodeOffset+iCodeSize; // file offset for aBufferStart sl@0: TUint8* sectionStart = (bufferStart+aRelocationInfoOffset-baseOffset); sl@0: TUint8* p = sectionStart; sl@0: sl@0: // read section header (ValidateHeader has alread checked this is OK)... sl@0: E32RelocSection* sectionHeader = (E32RelocSection*)p; sl@0: TUint size = sectionHeader->iSize; sl@0: TUint relocsRemaining = sectionHeader->iNumberOfRelocs; sl@0: E32IMAGEHEADER_TRACE(("E32RelocSection 0x%x %d",size,relocsRemaining)); sl@0: if(size&3) sl@0: RETURN_FAILURE(KErrCorrupt); // not multiple of word size sl@0: sl@0: // calculate buffer range for block data... sl@0: p = (TUint8*)(sectionHeader+1); // start of first block sl@0: TUint8* sectionEnd = p+size; sl@0: if(sectionEndbufferEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows buffer sl@0: sl@0: // process each block... sl@0: while(p!=sectionEnd) sl@0: { sl@0: E32RelocBlock* block = (E32RelocBlock*)p; sl@0: sl@0: // get address of first entry in this block... sl@0: TUint16* entryPtr = (TUint16*)(block+1); sl@0: if((TUint8*)entryPtr<(TUint8*)block || (TUint8*)entryPtr>sectionEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows relocation section sl@0: sl@0: // read block header... sl@0: TUint pageOffset = block->iPageOffset; sl@0: TUint blockSize = block->iBlockSize; sl@0: E32IMAGEHEADER_TRACE(("E32RelocSection block 0x%x 0x%x",pageOffset,blockSize)); sl@0: if(pageOffset&0xfff) sl@0: RETURN_FAILURE(KErrCorrupt); // not page aligned sl@0: if(blockSize(TUint16*)sectionEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows relocation section sl@0: sl@0: // process each entry in this block... sl@0: while(entryPtr=aRelocatedSectionSize || offset+4>aRelocatedSectionSize) sl@0: RETURN_FAILURE(KErrCorrupt); // not within section sl@0: if(offset&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not aligned correctly sl@0: sl@0: // count each relocation processed... sl@0: --relocsRemaining; sl@0: } sl@0: sl@0: // next sub block... sl@0: p = (TUint8*)entryEnd; sl@0: } sl@0: sl@0: // check number of relocations in section header is correct... sl@0: E32IMAGEHEADER_TRACE(("E32RelocSection relocsRemaining=%d",relocsRemaining)); sl@0: if(relocsRemaining) sl@0: RETURN_FAILURE(KErrCorrupt); // incorrect number of entries sl@0: sl@0: aRelocationSection = sectionHeader; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Validate an import section. sl@0: sl@0: For PE format imports, this also verifies that the Import Address Table fits within the code sl@0: part of the image. sl@0: sl@0: @param aBufferStart Start of buffer containing the data after the code part in the image file. sl@0: @param aBufferSize Size of data at aBufferStart. sl@0: @param[out] aBiggestImportCount Largest number of imports the image has from any single dependency. sl@0: sl@0: @return KErrNone if section is valid (or absent), else KErrCorrupt. sl@0: */ sl@0: TInt E32ImageHeaderV::ValidateImports(TAny* aBufferStart, TUint aBufferSize, TUint& aBiggestImportCount) const sl@0: { sl@0: if(!iImportOffset) sl@0: { sl@0: aBiggestImportCount = 0; sl@0: return KErrNone; // no imports sl@0: } sl@0: sl@0: // get alignment requirements... sl@0: TCpu cpu = (TCpu)iCpuIdentifier; sl@0: TBool isARM = (cpu==ECpuArmV4 || cpu==ECpuArmV5 || cpu==ECpuArmV6); sl@0: TUint32 pointerAlignMask = isARM ? 3 : 0; // mask of bits which must be zero for aligned pointers/offsets sl@0: sl@0: // buffer pointer to read imports from... sl@0: TUint8* bufferStart = (TUint8*)aBufferStart; sl@0: TUint8* bufferEnd = bufferStart+aBufferSize; sl@0: TUint baseOffset = iCodeOffset+iCodeSize; // file offset for aBufferStart sl@0: TUint8* sectionStart = (bufferStart+iImportOffset-baseOffset); sl@0: TUint8* p = sectionStart; sl@0: sl@0: // read section header (ValidateHeader has alread checked this is OK)... sl@0: E32ImportSection* sectionHeader = (E32ImportSection*)p; sl@0: TUint size = sectionHeader->iSize; sl@0: E32IMAGEHEADER_TRACE(("E32ImportSection 0x%x",size)); sl@0: sl@0: // check section lies within buffer... sl@0: p = (TUint8*)(sectionHeader+1); // start of first import block sl@0: TUint8* sectionEnd = sectionStart+size; sl@0: if(sectionEndbufferEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows buffer sl@0: sl@0: // process each import block... sl@0: TUint numDeps = iDllRefTableCount; sl@0: TUint biggestImportCount = 0; sl@0: TUint totalImports = 0; sl@0: TUint importFormat = iFlags&KImageImpFmtMask; sl@0: while(numDeps--) sl@0: { sl@0: // get block header... sl@0: E32ImportBlock* block = (E32ImportBlock*)p; sl@0: p = (TUint8*)(block+1); sl@0: if(p<(TUint8*)block || p>sectionEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows buffer sl@0: sl@0: E32IMAGEHEADER_TRACE(("E32ImportBlock 0x%x %d",block->iOffsetOfDllName,block->iNumberOfImports)); sl@0: sl@0: // check import dll name is within section... sl@0: TUint8* name = sectionStart+block->iOffsetOfDllName; sl@0: if(name=sectionEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // not within import section sl@0: while(*name++ && nameiOffsetOfDllName)); sl@0: sl@0: // process import count... sl@0: TUint numberOfImports = block->iNumberOfImports; sl@0: if(numberOfImports>=0x80000000u/sizeof(TUint32)) sl@0: RETURN_FAILURE(KErrCorrupt); // size doesn't fit into a signed integer sl@0: if(numberOfImports>biggestImportCount) sl@0: biggestImportCount = numberOfImports; sl@0: totalImports += numberOfImports; sl@0: sl@0: // process import data... sl@0: sl@0: // PE2 doesn't have any more data... sl@0: if(importFormat==KImageImpFmt_PE2) sl@0: continue; sl@0: sl@0: // get import data range... sl@0: TUint32* imports = (TUint32*)p; sl@0: TUint32* importsEnd = imports+numberOfImports; sl@0: if(importsEnd0x80000000 sl@0: if(importsEnd>(TUint32*)sectionEnd) sl@0: RETURN_FAILURE(KErrCorrupt); // overflows buffer sl@0: sl@0: // move pointer on to next block... sl@0: p = (TUint8*)importsEnd; sl@0: sl@0: if(importFormat==KImageImpFmt_ELF) sl@0: { sl@0: // check imports are in code section... sl@0: TUint32 limit = iCodeSize-sizeof(TUint32); sl@0: while(importslimit) sl@0: RETURN_FAILURE(KErrCorrupt); sl@0: if(i&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // not word aligned sl@0: } sl@0: } sl@0: else if(importFormat==KImageImpFmt_PE) sl@0: { sl@0: // import data is not used, so don't bother checking it sl@0: } sl@0: else sl@0: { sl@0: RETURN_FAILURE(KErrCorrupt); // bad import format, Fuzzer can't trigger this because import format checked by header validation sl@0: } sl@0: sl@0: // next block... sl@0: p = (TUint8*)block->NextBlock(importFormat); sl@0: } sl@0: sl@0: // done processing imports; for PE derived files now check import address table (IAT)... sl@0: if(importFormat==KImageImpFmt_PE || importFormat==KImageImpFmt_PE2) sl@0: { sl@0: if(totalImports>=0x80000000u/sizeof(TUint32)) sl@0: RETURN_FAILURE(KErrCorrupt); // size doesn't fit into a signed integer sl@0: TUint importAddressTable = iTextSize; // offset for IAT sl@0: if(importAddressTable&pointerAlignMask) sl@0: RETURN_FAILURE(KErrCorrupt); // Fuzzer can't trigger this because PE imports are for X86 which doesn't have alignment restrictions sl@0: TUint importAddressTableEnd = importAddressTable+sizeof(TUint32)*totalImports; sl@0: if(importAddressTableEndTUint(iCodeSize)) sl@0: RETURN_FAILURE(KErrCorrupt); // import address table overflows code part of file sl@0: E32IMAGEHEADER_TRACE(("E32ImportSection IAT offsets 0x%x..0x%x",importAddressTable,importAddressTableEnd)); sl@0: } sl@0: sl@0: aBiggestImportCount = biggestImportCount; sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: Validate a whole executable image. sl@0: sl@0: This runs all of the other validation methods in turn. sl@0: sl@0: @param aBufferStart Start of buffer containing the data after the header part of an image file. sl@0: @param aBufferSize Size of data at aBufferStart. sl@0: sl@0: @return KErrNone if image is valid, else KErrCorrupt or KErrNotSupported. sl@0: */ sl@0: TInt E32ImageHeaderV::ValidateWholeImage(TAny* aBufferStart, TUint aBufferSize) const sl@0: { sl@0: TUint32 dummyUncompressedSize; sl@0: TInt r = ValidateHeader(TotalSize()+aBufferSize,dummyUncompressedSize); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: sl@0: TInt endOfCodeOffset = iCodeSize; sl@0: void* restOfFileData = ((TUint8*)aBufferStart)+endOfCodeOffset; sl@0: TInt restOfFileSize = aBufferSize-endOfCodeOffset; sl@0: sl@0: E32RelocSection* dummy; sl@0: r = ValidateRelocations(restOfFileData,restOfFileSize,iCodeRelocOffset,iCodeSize,dummy); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: r = ValidateRelocations(restOfFileData,restOfFileSize,iDataRelocOffset,iDataSize,dummy); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: sl@0: TUint biggestImportCount; sl@0: r = ValidateImports(restOfFileData,restOfFileSize,biggestImportCount); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: sl@0: return r; sl@0: } sl@0: sl@0: sl@0: #endif // INCLUDE_E32IMAGEHEADER_IMPLEMENTATION sl@0: sl@0: sl@0: #endif // __F32IMAGE_H__ sl@0: sl@0: