toolsandutils/e32tools/inc/pe_file.h
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #if !defined(__PE_FILE_H__)
       
    17 #define __PE_FILE_H__
       
    18 #include <e32rom.h>
       
    19 #include "e32image.h"
       
    20 #include "pe_defs.h"
       
    21 //
       
    22 const TInt KTextSection=0;
       
    23 const TInt KConstSection=1;
       
    24 const TInt KExportSection=2;
       
    25 const TInt KDataSection=3;
       
    26 const TInt KBssSection=4;
       
    27 const TInt KImportSection=5;
       
    28 const TInt KCrtSection=6;
       
    29 const TInt KRelocSection=7;
       
    30 const TInt KNumberOfSections=8;
       
    31 //
       
    32 enum TImportStat {EImpError, EImpSuccess, EImpDone};
       
    33 //
       
    34 class PEFile
       
    35 	{
       
    36 public:
       
    37 	PEFile();
       
    38 	~PEFile();
       
    39 	TBool Init(const TText * const aFileName);
       
    40 	void Close();
       
    41 	//
       
    42 	TImportStat GetNextImport(TText * &aDllName, TUint16 &aOrdinal, TUint32 * &aThunkPtr);
       
    43 	TUint32 GetFixUp(const TUint16 aOrdinal);
       
    44 // 	TText *GetImageName();
       
    45 //	TUint8 *GetExportName(const TUint16 aOrdinal);
       
    46 //	TUint32 WriteDllRefTable(PEFile aPeArray[], TUint aMaxFiles);
       
    47 	void RelocateExportTable();
       
    48 	TUint GetNumberOfExportedFunctions();
       
    49 	TUint GetOrdinalBase();
       
    50 	TBool ExportSectionExists();
       
    51 	TBool ImportSectionExists();
       
    52 	TUint RoundToSectionSize(TUint aSize);
       
    53 	void DumpPeHeaders();
       
    54 	//
       
    55 	TInt Normalise();
       
    56 	void GetRelocs(TUint *aReloc, TUint *aRelocSection, TInt aNumberOfRelocs);
       
    57 	TInt ReadSectionHeaders();
       
    58 	TInt ReadData();
       
    59 	TInt ReadExportDirectory();
       
    60 	char *ReadSectionData(PIMAGE_SECTION_HEADER aHeader);
       
    61 	TInt NumberOfImports() const;
       
    62 	TInt NumberOfImportDlls() const;
       
    63 	TInt NumberOfExports() const;
       
    64 	TInt NumberOfRelocs();
       
    65 public:
       
    66 	static TInt CmpSectionName(PIMAGE_SECTION_HEADER apSectionHeader, char *aName);
       
    67 	TInt VirtualAddressInSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader);
       
    68 	TUint DistanceFromSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader);
       
    69 	TInt FindSectionByVa(TUint aVa, TUint aTryToBeClever=0);
       
    70 	static TInt HasInitialisedData(PIMAGE_SECTION_HEADER aHeader);
       
    71 	
       
    72 //	static TBool DoFixUps(PEFile aPeArray[], TUint aMaxFiles);
       
    73 //	static TBool FindByName(PEFile aPeArray[], TUint aMaxFiles,const TText * const aDllName, PEFile * &aExportPEFile);
       
    74 //	static void WriteDllRefTables(PEFile aPeArray[], TUint aMaxFiles);
       
    75 //	static void RelocateExportTables(PEFile aPeArray[], TUint aMaxFiles);
       
    76 private:
       
    77 	TBool IsInCode(TUint32 anAddr);
       
    78 	TBool IsInData(TUint32 anAddr);
       
    79 	TBool IsInDataReloc(TUint32 anAddr);
       
    80 	TBool IsInExport(TUint32 anAddr);
       
    81 	TBool IsInImport(TUint32 anAddr);
       
    82 	TBool IsValidDOSHeader(PIMAGE_DOS_HEADER pDOSHeader);
       
    83 	TBool IsValidNTHeader(PIMAGE_NT_HEADERS pNTHeader);
       
    84 	TBool IsValidFileHeader(PIMAGE_FILE_HEADER pFileHeader);
       
    85 	void DumpNextSectionInFile(PIMAGE_SECTION_HEADER pSectionHeader);
       
    86 	void CopySectionData(TAny *source, TAny *dest, TUint32 fileLength, TUint32 memLength);
       
    87 	TBool ProcessRelocData(TAny *relocData,TInt dataSize);
       
    88 	PEFile(const PEFile&);
       
    89 	const PEFile & operator = (const PEFile&);
       
    90 public:
       
    91 	static TUint32 iRomMemBase; // where the ROM is being loaded to
       
    92 	static TUint32 iRomLinearBase; // where the ROM will run
       
    93 	TUint32 iMemBase; // where this file is being loaded to
       
    94 	TUint32 iEntryPoint;
       
    95 	TUint32 iImageSize;
       
    96 	TUint32 iCodeSize;
       
    97 	TUint32 iDataSize;
       
    98 	TUint32 iHeapReservedSize;
       
    99 	TUint32 iHeapCommittedSize;
       
   100 	TUint32 iStackReservedSize;
       
   101 	TUint32 iStackCommittedSize;
       
   102 	TUint32 iBssSize;
       
   103 	TUint32 iBssOffset;
       
   104 	TUint32 iSectionAlign;
       
   105 	TUint32 iExpDirectoryOffset;
       
   106 	TUint32 iDataOffset;
       
   107 	TBool	iImageIsDll;
       
   108 private:
       
   109 	IMAGE_NT_HEADERS *iHeader;
       
   110  	PIMAGE_EXPORT_DIRECTORY	iExpDirectory;
       
   111 	PIMAGE_IMPORT_DESCRIPTOR iImpDescriptor; 
       
   112 	TText *iFileName;
       
   113 	TInt32 iFileHandle;
       
   114 	TUint32 iLinkedBase;
       
   115 	TUint32 iStartOfHeaders; // whether DOS header or not
       
   116 	TUint32 iSizeOfHeaders; // Up to and including section headers
       
   117 	TUint32 iNumSections;
       
   118 	TUint32 iRomRunAddr; // where the code will run & rdata be accessed
       
   119 	TUint32 iRamRunAddr; // and where the data & bss will be when it does
       
   120 	TUint32 iRomDelta;
       
   121 	TUint32 iRamDelta;
       
   122 	TBool iHadDataSection;
       
   123 
       
   124 // stuff for relocating image successfully in 2 parts
       
   125 	TUint32 iBssSectionLinkedAddr;
       
   126 	TUint32 iBssSectionAddr;
       
   127 	TUint32 iBssSectionSize;
       
   128 	TUint32 iDataSectionLinkedAddr;
       
   129 	TUint32 iDataSectionAddr;
       
   130 	TUint32 iDataSectionSize;
       
   131 	TUint32 iCodeSectionAddr;
       
   132 	TUint32 iCodeSectionSize;
       
   133 	TUint32 iRDataSectionAddr;
       
   134 	TUint32 iRDataSectionSize;
       
   135 	TUint32 iCRTSectionAddr;
       
   136 	TUint32 iCRTSectionSize;
       
   137 	TUint32 iExportDataDir;
       
   138 
       
   139 // stuff for the pe->e32image translator
       
   140 	PIMAGE_SECTION_HEADER iSectionHeader[KNumberOfSections];
       
   141 	char *iSectionData[KNumberOfSections];
       
   142 	TInt iExportDirSize;
       
   143 	TInt iCpu;
       
   144 	friend class E32ImageFile;
       
   145 	friend class E32ImageFile_PE;
       
   146 	};
       
   147 
       
   148 class E32ImageFile_PE : public E32ImageFile
       
   149 	{
       
   150 public:
       
   151 	E32ImageFile_PE();
       
   152 	virtual ~E32ImageFile_PE();
       
   153 	virtual TBool Translate(const char* aFileName, TUint aDataBase, TBool aAllowDllData, TBool /*aSymLkupEnabled*/);
       
   154 	TBool Translate(PEFile &aPEFile);
       
   155 	TUint ImportAddressTableOffset();
       
   156 	TUint ConstOffset();
       
   157 private:
       
   158 	TInt DoCodeHeader(PEFile &aPeFile);
       
   159 	TInt DoDataHeader(PEFile &aPeFile, TUint aDataBase);
       
   160 	TInt CopyCode(char *aPtr, PEFile &aPeFile);
       
   161 	TInt CopyData(char *aPtr, PEFile &aPeFile);
       
   162 	TInt CopyImportAddrTable(char *aPtr, PEFile &aPeFile);
       
   163 	char *CreateImportSection(const PEFile &aPeFile, TInt &aSize);
       
   164 	void CreateExportSection(char *aPtr, PEFile &aPeFile);
       
   165 
       
   166 	void CreateExportDirectory(char *aPtr, PEFile &aPeFile);
       
   167 	void FixExportDirectory(TUint *aExportDir, PEFile &aPeFile);
       
   168 	void FixRelocs(PEFile &aPeFile, TUint *relocation, TUint *relocsection, TInt aNumberOfRelocs);
       
   169 	char *CreateCodeRelocs(TUint *reloc, TUint *relocsection, TInt nrelocs, TInt &aSize);
       
   170 	char *CreateDataRelocs(TUint *reloc, TUint *relocsection, TInt nrelocs, TInt &aSize);
       
   171 
       
   172 	TUint FixAddress(PEFile &aPeFile, TUint va);
       
   173 	TUint FixImportThunk(PEFile &aPeFile, TUint va);
       
   174 private:
       
   175 	PIMAGE_SECTION_HEADER iPeHeader[KNumberOfSections];
       
   176 	char *iPeData[KNumberOfSections];
       
   177 	TUint iConstOffset;
       
   178 	TUint iCrtOffset;
       
   179 	};
       
   180 
       
   181 #endif