source: trunk/src/win32k/include/pe2lx.h@ 8266

Last change on this file since 8266 was 6853, checked in by sandervl, 24 years ago

header updates

File size: 12.8 KB
Line 
1/* $Id: pe2lx.h,v 1.14 2001-09-28 07:43:38 sandervl Exp $
2 *
3 * Pe2Lx class declarations. Ring 0 and Ring 3
4 *
5 * Copyright (c) 1998-2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10#ifndef _PE2LX_H_
11#define _PE2LX_H_
12
13
14/*******************************************************************************
15* Defined Constants And Macros *
16*******************************************************************************/
17/*
18 * BufferedRVAReader config
19 */
20#define BUFFEREDRVAREADER_BUFFERSIZE PAGESIZE /* reader code assumes this size... */
21
22
23
24/*******************************************************************************
25* Structures and Typedefs *
26*******************************************************************************/
27/**
28 * LXObject is use to store objects in the virtual LX file.
29 */
30typedef struct LXObject
31{
32 ULONG ulRVA; /* Object relative virtual address */
33 ULONG cbPhysical; /* Physical size */
34 ULONG cbVirtual; /* Virtual size */
35 ULONG flFlags; /* LX flags! */
36 struct
37 {
38 ULONG offTIBFix : 30; /* TIBfix offset (from the object start) */
39 ULONG fTIBFixObject : 1;/* Set: The object contains the tibfix. Clear: not tib fix object. */
40 ULONG fStackObject : 1; /* Set: The object is the stack object. Clear: not the stack object. */
41 } Misc;
42 ULONG offPEFile; /* section offset into real PE file. */
43 ULONG offLXFile; /* object offset into virtual LX file. */
44} LXOBJECT, *PLXOBJECT;
45typedef const LXOBJECT *PCLXOBJECT;
46
47
48
49/**
50 * Pe2Lx class. Reads a PE executable image and creates a virtual LX file which
51 * can be read from and (RING3) dumped to file.
52 *
53 * @author knut st. osmundsen
54 * @approval knut st. osmundsen
55 */
56class Pe2Lx : public ModuleBase
57{
58public:
59 /** @cat Constructor/Destructor */
60 Pe2Lx(SFN hFile);
61 ~Pe2Lx();
62
63 /** @cat Public Main methods */
64 ULONG init(PCSZ pszFilename);
65 ULONG read(ULONG offLXFile, PVOID pvBuffer, ULONG cbToRead, ULONG flFlags, PMTE pMTE);
66 ULONG applyFixups(PMTE pMTE, ULONG iObject, ULONG iPageTable, PVOID pvPage,
67 ULONG ulPageAddress, PVOID pvPTDA); /*(ldrEnum32bitRelRecs)*/
68 ULONG applyFixupsDelta(PVOID pvPage, ULONG ulDelta, ULONG ulRVAPage);
69
70 ULONG openPath(PCHAR pachFilename, USHORT cchFilename, ldrlv_t *pLdrLv, PULONG pful, ULONG lLibPath); /* (ldrOpenPath) */
71 static ULONG openPath2(PCHAR pachFilename, ULONG cchFilename, ldrlv_t *pLdrLv, PULONG pful, ULONG lLibPath, BOOL fOdin32PathValid);
72 #ifndef RING0
73 ULONG testApplyFixups();
74 ULONG writeFile(PCSZ pszLXFilename);
75 #endif
76
77 /** @cat public query methods */
78 BOOL isExe();
79 BOOL isDll();
80 static SFN getKernel32SFN() { return sfnKernel32; }
81 static VOID setKernel32SFN(SFN sfn) { sfnKernel32 = sfn; }
82 static VOID invalidateOdin32Path();
83 static LONG getLoadedModuleCount() { return cLoadedModules;}
84
85 /** @cat public Helper methods */
86 ULONG querySizeOfLxFile();
87 VOID dumpVirtualLxFile();
88
89private:
90 /** @cat conversion methods */
91 ULONG makeObjectTable();
92 ULONG makeObjectPageTable();
93 ULONG makeFixups();
94 ULONG makeExports();
95
96 /** @cat conversion helper(s) */
97 ULONG loadNtHeaders();
98 VOID releaseNtHeaders();
99 ULONG loadBaseRelocations();
100
101 /** @cat init() helper methods - may only be called at init time! */
102 ULONG addObject(ULONG ulRVA, ULONG cbPhysical, ULONG cbVirtual, ULONG flFlags, ULONG offPEFile);
103 ULONG addTIBFixObject();
104 ULONG addStackObject(ULONG cbStack);
105
106 /** @cat PE helper methods */
107
108 /** @cat Fixups Helpers - calls allowed from makeFixups only! */
109 ULONG initFixups();
110 ULONG addPageFixupEntry(BOOL fLast = FALSE);
111 ULONG add32OffsetFixup(WORD offSource, ULONG ulTarget);
112 #ifndef RING0
113 ULONG add32OrdImportFixup(WORD offSource, ULONG ulModuleOrdinal, ULONG ulFunctionOrdinal, PSZ pszModuleName);
114 ULONG add32NameImportFixup(WORD offSource, ULONG ulModuleOrdinal, PCSZ pszFnName, PSZ pszModuleName);
115 #else
116 ULONG add32OrdImportFixup(WORD offSource, ULONG ulModuleOrdinal, ULONG ulFunctionOrdinal);
117 ULONG add32NameImportFixup(WORD offSource, ULONG ulModuleOrdinal, PCSZ pszFnName);
118 #endif
119 ULONG addModule(PCSZ pszModuleName, PULONG pulModuleOrdinal);
120 ULONG addImportFunctionName(PCSZ pszFnName, PULONG poffFnName);
121 VOID finalizeFixups();
122 VOID finalizeImportNames();
123
124 /** @cat Entry table / Resident nametable helpers */
125 ULONG initEntry();
126 ULONG addResName(ULONG ulOrdinal, PCSZ pszName, ULONG cchName);
127 ULONG addEntry(ULONG ulOrdinal, ULONG ulRVA);
128 ULONG addForwarderEntry(ULONG ulOrdinal, PCSZ pszDllName, PCSZ pszFnNameOrOrd);
129 ULONG addLastEntry();
130 VOID finalizeExports();
131
132 /** @cat Misc helpers */
133 ULONG getCountOfPages();
134 ULONG queryObjectAndOffset(ULONG ulRVA, PULONG pulObject, PULONG poffObject);
135 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer);
136
137 /** @cat static helpers */
138 static PCSZ queryOdin32ModuleName(PCSZ pszWin32ModuleName);
139 static BOOL initOdin32Path();
140 static BOOL setOdin32Path(const char *psz);
141
142 /** @cat static dump methods */
143 static VOID dumpNtHeaders(PIMAGE_NT_HEADERS pNtHdrs);
144 static VOID dumpSectionHeader(PIMAGE_SECTION_HEADER pSection);
145
146private:
147 /** @cat private data members - allways present. */
148 BOOL fAllInOneObject; /* The All-in-object fix will be or is applied. */
149 PLXOBJECT paObjects; /* Pointer to object array. */
150 USHORT cObjects; /* Count of elements in the object array. */
151 USHORT cObjectsAllocated; /* Size of the object array. */
152
153 /**
154 * @cat LX structures
155 */
156 struct e32_exe LXHdr; /* Lxheader */
157
158 struct o32_obj *paObjTab; /* Pointer to object table - if null check cObjects > 0 and generate it using makeObjectTable */
159 struct o32_map *paObjPageTab; /* Pointer to object page table - if null check cObjects > 0 and generate it using makeObjectPageTable */
160
161 PCHAR pachResNameTable; /* Pointer to resident name table. */
162 ULONG offCurResName; /* Offset of the next entry in the resident name table. */
163 ULONG cchRNTAllocated; /* Count of char allocated for the resident name table. */
164
165 struct b32_bundle *pEntryBundles; /* Pointer to entry bundles. (exports) */
166 ULONG offCurEntryBundle; /* Offset of the next bundle. */
167 ULONG offLastEntryBundle; /* Offset of the last entry bundle. */
168 ULONG ulLastOrdinal; /* Ordinal number of last entry which was added. */
169 ULONG cbEBAllocated; /* Count of bytes allocate for entry bundles. */
170 BOOL fForwarders; /* Set if forwarders are present. */
171
172 PULONG paulFixupPageTable; /* Pointer to fixup pagetable. If null generate it using makeFixups. */
173 ULONG cFixupPTEntries; /* Number of entries in the fixup page table. */
174 ULONG cFPTEAllocated; /* Number of page table entries allocated. */
175
176 PVOID pFixupRecords; /* Pointer to fixup records. If null generate it using makeFixups. */
177 ULONG offCurFixupRec; /* Offset of next fixup. */
178 ULONG cbFRAllocated; /* Count of bytes allocated for Fixup records. */
179
180 PVOID pvCrossPageFixup; /* Pointer to cross page fixup. */
181 ULONG cbCrossPageFixup; /* Fixup size in bytes. */
182
183 PCHAR pachImpModuleNames; /* Pointer to list of module names. */
184 ULONG offCurImpModuleName; /* Offset of next modulename. */
185 ULONG cchIMNAllocated; /* Count of chars allocated for pachImpModuleNames. */
186
187 PCHAR pachImpFunctionNames; /* Pointer to list of function names. */
188 ULONG offCurImpFunctionName; /* Offset of next functionname. */
189 ULONG cchIFNAllocated; /* Count of chars allocated for pachImpFunctionNames. */
190
191 /**
192 * @cat PE structues
193 */
194 ULONG offNtHeaders; /* Fileoffset of the PE\0\0 signature. */
195 PIMAGE_NT_HEADERS pNtHdrs; /* Pointer to NT-Headers. If null load it using loadNtHeaders. */
196 ULONG ulImageBase; /* Image base address. */
197 PIMAGE_BASE_RELOCATION pBaseRelocs; /* Baserelocations. */
198 ULONG cbBaseRelocs; /* Size of baserelocations. */
199 ULONG fApplyFixups; /* TRUE: apply fixups, FALSE: do not apply fixups, ~0=check if need to apply fixups */
200 ULONG fDeltaOnly; /* TRUE: apply fixups only as deltas. FALSE: needs to calculate target address... */
201
202 /**
203 * @cat static data.
204 */
205 static struct LieListEntry /* Dll Name lie table. */
206 {
207 PCSZ pszWin32Name; /* Win32 dll name. */
208 PCSZ pszOdin32Name; /* Odin32 dll name. */
209 } paLieList[];
210
211 static struct PeCharacteristicsToLxFlags/* section characteristics to object flags */
212 {
213 unsigned int Characteristics; /* set of section characteristics */
214 ULONG flFlags; /* equivalent object flags */
215 } paSecChars2Flags[];
216
217 static LONG cLoadedModules; /* Count of existing objects. Updated by constructor and destructor. */
218 static const char * pszOdin32Path; /* Odin32 base path (include a slash). */
219 static ULONG cchOdin32Path; /* Odin32 base path length. */
220 static SFN sfnKernel32; /* Odin32 Kernel32 filehandle. */
221};
222
223
224/**
225 * BufferedRVARead - read at RVA with buffering.
226 * @author knut st. osmundsen
227 * @approval knut st. osmundsen
228 */
229class BufferedRVARead
230{
231public:
232 BufferedRVARead(SFN hFile, ULONG cObjects, PCLXOBJECT paObjects);
233#if 0
234 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
235#else
236 /**
237 * Reads a chunk of data at the spcified RVA.
238 * @returns NO_ERROR on success.
239 * ERROR_INVALID_PARAMETER
240 * <Whatever rc ReadAt returns>
241 * @param ulRVA RVA to read from. Within the filesize.
242 * @param pvBuffer Pointer to output buffer. pvBuffer > 64KB
243 * @param cbBuffer Number of bytes to read. 0 < cbBuffer > 256MB
244 * @status completely
245 * @author knut st. osmundsen
246 */
247 inline ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
248 {
249 /*
250 * five cases:
251 * 1) entire area is within the buffer.
252 * 2) start of area is within the buffer.
253 * 3) end of area is within the buffer.
254 * 4) the area is larger than the buffer, covering it.
255 * 5) the area is outside the buffer.
256 *
257 * these are optimal: 1, 2, and 5.
258 * The request is allways process from start to end. This will make case 3 and 4 less effecient.
259 */
260 #ifdef DEBUG
261 if (ulRVA == ~0UL || (ULONG)pvBuffer < 0x10000UL || cbBuffer == 0UL || cbBuffer >= 0x10000000UL)
262 return ERROR_INVALID_PARAMETER;
263 #endif
264
265 do
266 {
267 if (ulRVA >= this->ulRVA && ulRVA < this->ulRVA + sizeof(achBuffer))
268 { /* in buffer */
269 register ULONG cbRead = sizeof(achBuffer) - (ulRVA - this->ulRVA);
270 cbRead = min(cbRead, cbBuffer);
271 memcpy(pvBuffer, &achBuffer[ulRVA - this->ulRVA], (size_t)cbRead);
272 if (cbBuffer == cbRead)
273 return NO_ERROR;
274 cbBuffer -= cbRead;
275 pvBuffer = (PVOID)((ULONG)pvBuffer + cbRead);
276 ulRVA += cbRead;
277 }
278 else
279 { /* not in buffer, then read it into the buffer! */
280 APIRET rc = readToBuffer(ulRVA);
281 if (rc != NO_ERROR)
282 return rc;
283 }
284 } while (cbBuffer != 0UL);
285
286 return NO_ERROR;
287 }
288#endif
289
290 ULONG dupString(ULONG ulRVA, PSZ *ppsz);
291 BufferedRVARead & operator =(BufferedRVARead &SrcObj);
292
293private:
294 ULONG readToBuffer(ULONG ulRVA);
295
296private:
297 SFN hFile; /* Filehandle. */
298 ULONG cObjects; /* Count of objects */
299 PCLXOBJECT paObjects; /* Pointer to a readonly array of objects. */
300 ULONG ulRVA; /* RVA for the buffer start */
301 CHAR achBuffer[BUFFEREDRVAREADER_BUFFERSIZE]; /* Buffer. NOTE! Code assumes that it is a page. */
302};
303
304#endif
Note: See TracBrowser for help on using the repository browser.