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

Last change on this file since 2511 was 2501, checked in by bird, 26 years ago

Temporary backup checkin.

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