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