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

Last change on this file since 9955 was 9955, checked in by bird, 22 years ago

Added isCustomDllExcluded()

File size: 12.9 KB
Line 
1/* $Id: pe2lx.h,v 1.15 2003-03-31 01:23:14 bird 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 #ifndef RING0
142 static BOOL isCustomDllExcluded(const char *pszModuleName);
143 #endif
144
145 /** @cat static dump methods */
146 static VOID dumpNtHeaders(PIMAGE_NT_HEADERS pNtHdrs);
147 static VOID dumpSectionHeader(PIMAGE_SECTION_HEADER pSection);
148
149
150private:
151 /** @cat private data members - allways present. */
152 BOOL fAllInOneObject; /* The All-in-object fix will be or is applied. */
153 PLXOBJECT paObjects; /* Pointer to object array. */
154 USHORT cObjects; /* Count of elements in the object array. */
155 USHORT cObjectsAllocated; /* Size of the object array. */
156
157 /**
158 * @cat LX structures
159 */
160 struct e32_exe LXHdr; /* Lxheader */
161
162 struct o32_obj *paObjTab; /* Pointer to object table - if null check cObjects > 0 and generate it using makeObjectTable */
163 struct o32_map *paObjPageTab; /* Pointer to object page table - if null check cObjects > 0 and generate it using makeObjectPageTable */
164
165 PCHAR pachResNameTable; /* Pointer to resident name table. */
166 ULONG offCurResName; /* Offset of the next entry in the resident name table. */
167 ULONG cchRNTAllocated; /* Count of char allocated for the resident name table. */
168
169 struct b32_bundle *pEntryBundles; /* Pointer to entry bundles. (exports) */
170 ULONG offCurEntryBundle; /* Offset of the next bundle. */
171 ULONG offLastEntryBundle; /* Offset of the last entry bundle. */
172 ULONG ulLastOrdinal; /* Ordinal number of last entry which was added. */
173 ULONG cbEBAllocated; /* Count of bytes allocate for entry bundles. */
174 BOOL fForwarders; /* Set if forwarders are present. */
175
176 PULONG paulFixupPageTable; /* Pointer to fixup pagetable. If null generate it using makeFixups. */
177 ULONG cFixupPTEntries; /* Number of entries in the fixup page table. */
178 ULONG cFPTEAllocated; /* Number of page table entries allocated. */
179
180 PVOID pFixupRecords; /* Pointer to fixup records. If null generate it using makeFixups. */
181 ULONG offCurFixupRec; /* Offset of next fixup. */
182 ULONG cbFRAllocated; /* Count of bytes allocated for Fixup records. */
183
184 PVOID pvCrossPageFixup; /* Pointer to cross page fixup. */
185 ULONG cbCrossPageFixup; /* Fixup size in bytes. */
186
187 PCHAR pachImpModuleNames; /* Pointer to list of module names. */
188 ULONG offCurImpModuleName; /* Offset of next modulename. */
189 ULONG cchIMNAllocated; /* Count of chars allocated for pachImpModuleNames. */
190
191 PCHAR pachImpFunctionNames; /* Pointer to list of function names. */
192 ULONG offCurImpFunctionName; /* Offset of next functionname. */
193 ULONG cchIFNAllocated; /* Count of chars allocated for pachImpFunctionNames. */
194
195 /**
196 * @cat PE structues
197 */
198 ULONG offNtHeaders; /* Fileoffset of the PE\0\0 signature. */
199 PIMAGE_NT_HEADERS pNtHdrs; /* Pointer to NT-Headers. If null load it using loadNtHeaders. */
200 ULONG ulImageBase; /* Image base address. */
201 PIMAGE_BASE_RELOCATION pBaseRelocs; /* Baserelocations. */
202 ULONG cbBaseRelocs; /* Size of baserelocations. */
203 ULONG fApplyFixups; /* TRUE: apply fixups, FALSE: do not apply fixups, ~0=check if need to apply fixups */
204 ULONG fDeltaOnly; /* TRUE: apply fixups only as deltas. FALSE: needs to calculate target address... */
205
206 /**
207 * @cat static data.
208 */
209 static struct LieListEntry /* Dll Name lie table. */
210 {
211 PCSZ pszWin32Name; /* Win32 dll name. */
212 PCSZ pszOdin32Name; /* Odin32 dll name. */
213 } paLieList[];
214
215 static struct PeCharacteristicsToLxFlags/* section characteristics to object flags */
216 {
217 unsigned int Characteristics; /* set of section characteristics */
218 ULONG flFlags; /* equivalent object flags */
219 } paSecChars2Flags[];
220
221 static LONG cLoadedModules; /* Count of existing objects. Updated by constructor and destructor. */
222 static const char * pszOdin32Path; /* Odin32 base path (include a slash). */
223 static ULONG cchOdin32Path; /* Odin32 base path length. */
224 static SFN sfnKernel32; /* Odin32 Kernel32 filehandle. */
225};
226
227
228/**
229 * BufferedRVARead - read at RVA with buffering.
230 * @author knut st. osmundsen
231 * @approval knut st. osmundsen
232 */
233class BufferedRVARead
234{
235public:
236 BufferedRVARead(SFN hFile, ULONG cObjects, PCLXOBJECT paObjects);
237#if 0
238 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
239#else
240 /**
241 * Reads a chunk of data at the spcified RVA.
242 * @returns NO_ERROR on success.
243 * ERROR_INVALID_PARAMETER
244 * <Whatever rc ReadAt returns>
245 * @param ulRVA RVA to read from. Within the filesize.
246 * @param pvBuffer Pointer to output buffer. pvBuffer > 64KB
247 * @param cbBuffer Number of bytes to read. 0 < cbBuffer > 256MB
248 * @status completely
249 * @author knut st. osmundsen
250 */
251 inline ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
252 {
253 /*
254 * five cases:
255 * 1) entire area is within the buffer.
256 * 2) start of area is within the buffer.
257 * 3) end of area is within the buffer.
258 * 4) the area is larger than the buffer, covering it.
259 * 5) the area is outside the buffer.
260 *
261 * these are optimal: 1, 2, and 5.
262 * The request is allways process from start to end. This will make case 3 and 4 less effecient.
263 */
264 #ifdef DEBUG
265 if (ulRVA == ~0UL || (ULONG)pvBuffer < 0x10000UL || cbBuffer == 0UL || cbBuffer >= 0x10000000UL)
266 return ERROR_INVALID_PARAMETER;
267 #endif
268
269 do
270 {
271 if (ulRVA >= this->ulRVA && ulRVA < this->ulRVA + sizeof(achBuffer))
272 { /* in buffer */
273 register ULONG cbRead = sizeof(achBuffer) - (ulRVA - this->ulRVA);
274 cbRead = min(cbRead, cbBuffer);
275 memcpy(pvBuffer, &achBuffer[ulRVA - this->ulRVA], (size_t)cbRead);
276 if (cbBuffer == cbRead)
277 return NO_ERROR;
278 cbBuffer -= cbRead;
279 pvBuffer = (PVOID)((ULONG)pvBuffer + cbRead);
280 ulRVA += cbRead;
281 }
282 else
283 { /* not in buffer, then read it into the buffer! */
284 APIRET rc = readToBuffer(ulRVA);
285 if (rc != NO_ERROR)
286 return rc;
287 }
288 } while (cbBuffer != 0UL);
289
290 return NO_ERROR;
291 }
292#endif
293
294 ULONG dupString(ULONG ulRVA, PSZ *ppsz);
295 BufferedRVARead & operator =(BufferedRVARead &SrcObj);
296
297private:
298 ULONG readToBuffer(ULONG ulRVA);
299
300private:
301 SFN hFile; /* Filehandle. */
302 ULONG cObjects; /* Count of objects */
303 PCLXOBJECT paObjects; /* Pointer to a readonly array of objects. */
304 ULONG ulRVA; /* RVA for the buffer start */
305 CHAR achBuffer[BUFFEREDRVAREADER_BUFFERSIZE]; /* Buffer. NOTE! Code assumes that it is a page. */
306};
307
308#endif
Note: See TracBrowser for help on using the repository browser.