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

Last change on this file since 1330 was 1270, checked in by bird, 26 years ago

New modules. Pe2Lx rewrite.

File size: 13.1 KB
Line 
1/* $Id: pe2lx.h,v 1.4 1999-10-14 01:17:55 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 * RING3 wrappings for RING0 constructs.
21 */
22#ifdef RING3
23 typedef HFILE SFN; /* System File Number. (OS2Krnl.h) */
24 typedef PVOID PMTE; /* Pointer to Module Table Entry. (OS2Krnl.h) */
25#endif
26
27/*
28 * Misc
29 */
30#define PAGESIZE 0x1000
31
32/*
33 * BufferedRVAReader config
34 */
35#define BUFFEREDRVAREADER_BUFFERSIZE PAGESIZE /* reader code assumes this size... */
36
37
38/*
39 * Error definitions (used in addition to them in bseerr.h)
40 */
41#define ERROR_FAILED_TO_ADD_OBJECT 0x42000000UL
42#define ERROR_INITMETHOD_NOT_INITTIME 0x42000001UL
43#define ERROR_INTERNAL_PROCESSING_ERROR 0x42000002UL
44
45
46/*
47 * Output macros.
48 * Macros: option infolevel
49 * printIPE -W1 Error
50 * printErr -W1 Error
51 * printWar -W2 Warning
52 * printInf -W3 Info
53 * printInfA -W4 InfoAll
54 */
55#define printIPE(a) (Pe2Lx::ulInfoLevel >= Pe2Lx::Error ? \
56 Pe2Lx::printf("\nerror(%d:"__FUNCTION__"): !Internal Processing Error!\n\t", __LINE__), \
57 Pe2Lx::printf a, \
58 Pe2Lx::printf("\n") \
59 : (void)0,(void)0,(void)0 )
60#define printErr(a) (Pe2Lx::ulInfoLevel >= Pe2Lx::Error ? \
61 Pe2Lx::printf("error(%d:"__FUNCTION__"): ", __LINE__), \
62 Pe2Lx::printf a \
63 : (void)0,(void)0 )
64#define printWar(a) (Pe2Lx::ulInfoLevel >= Pe2Lx::Warning ? \
65 Pe2Lx::printf("warning("__FUNCTION__"): "), \
66 Pe2Lx::printf a \
67 : (void)0,(void)0 )
68#define printInf(a) (Pe2Lx::ulInfoLevel >= Pe2Lx::Info ? \
69 Pe2Lx::printf a : (void)0 )
70#define printInfA(a)(Pe2Lx::ulInfoLevel >= Pe2Lx::InfoAll ? \
71 Pe2Lx::printf a : (void)0 )
72
73
74
75/*******************************************************************************
76* Structures and Typedefs *
77*******************************************************************************/
78/**
79 * LXObject is use to store objects in the virtual LX file.
80 */
81typedef struct LXObject
82{
83 ULONG ulRVA; /* Object relative virtual address */
84 ULONG cbPhysical; /* Physical size */
85 ULONG cbVirtual; /* Virtual size */
86 ULONG flFlags; /* LX flags! */
87 struct
88 {
89 ULONG offTIBFix : 30; /* TIBfix offset (from the object start) */
90 ULONG fTIBFixObject : 1;/* Set: The object contains the tibfix. Clear: not tib fix object. */
91 ULONG fStackObject : 1; /* Set: The object is the stack object. Clear: not the stack object. */
92 } Misc;
93 ULONG offPEFile; /* section offset into real PE file. */
94 ULONG offLXFile; /* object offset into virtual LX file. */
95} LXOBJECT, *PLXOBJECT;
96typedef const LXOBJECT *PCLXOBJECT;
97
98
99
100/**
101 * Pe2Lx class. Reads a PE executable image and creates a virtual LX file which
102 * can be read from and (RING3) dumped to file.
103 *
104 * @author knut st. osmundsen
105 * @approval knut st. osmundsen
106 */
107class Pe2Lx
108{
109public:
110 /** @cat Constructor/Destructor */
111 Pe2Lx(SFN hFile);
112 ~Pe2Lx();
113
114 /** @cat Public Main methods */
115 ULONG init(PCSZ pszFilename);
116 ULONG read(ULONG offLXFile, PVOID pvBuffer, ULONG cbToRead, ULONG flFlags, PMTE pMTE);
117 #ifndef RING0
118 ULONG writeLxFile(PCSZ pszLXFilename);
119 #endif
120
121 /** @cat public Helper methods */
122 BOOL queryIsModuleName(PCSZ pszFilename);
123 ULONG querySizeOfLxFile();
124 VOID dumpVirtualLxFile();
125
126
127private:
128 /** @cat conversion methods */
129 ULONG makeObjectTable();
130 ULONG makeObjectPageTable();
131 ULONG makeFixups();
132 ULONG makeExports();
133
134 /** @cat conversion helper(s) */
135 ULONG loadNtHeaders();
136 VOID releaseNtHeaders();
137
138 /** @cat init() helper methods - may only be called at init time! */
139 ULONG addObject(ULONG ulRVA, ULONG cbPhysical, ULONG cbVirtual, ULONG flFlags, ULONG offPEFile);
140 ULONG addTIBFixObject();
141 ULONG addStackObject(ULONG cbStack);
142
143 /** @cat PE helper methods */
144
145 /** @cat Fixups Helpers - calls allowed from makeFixups only! */
146 ULONG initFixups();
147 ULONG addPageFixupEntry(BOOL fLast = FALSE);
148 ULONG add32OffsetFixup(WORD offSource, ULONG ulTarget);
149 ULONG add32OrdImportFixup(WORD offSource, ULONG ulModuleOrdinal, ULONG ulFunctionOrdinal);
150 ULONG add32NameImportFixup(WORD offSource, ULONG ulModuleOrdinal, PCSZ pszFnName);
151 ULONG addModule(PCSZ pszModuleName, PULONG pulModuleOrdinal);
152 ULONG addImportFunctionName(PCSZ pszFnName, PULONG poffFnName);
153 VOID finalizeFixups();
154 VOID finalizeImportNames();
155
156 /** @cat Entry table / Resident nametable helpers */
157 ULONG initEntry();
158 ULONG addResName(ULONG ulOrdinal, PCSZ pszName, ULONG cchName);
159 ULONG addEntry(ULONG ulOrdinal, ULONG ulRVA);
160 ULONG addForwarderEntry(ULONG ulOrdinal, PCSZ pszDllName, PCSZ pszFnNameOrOrd);
161 ULONG addLastEntry();
162 VOID finalizeExports();
163
164 /** @cat Misc helpers */
165 ULONG getCountOfPages();
166 ULONG queryObjectAndOffset(ULONG ulRVA, PULONG pulObject, PULONG poffObject);
167
168 /** @cat static helpers */
169 static PCSZ queryOdin32ModuleName(PCSZ pszWin32ModuleName);
170
171 /** @cat static dump methods */
172 static VOID dumpNtHeaders(PIMAGE_NT_HEADERS pNtHdrs);
173 static VOID dumpSectionHeader(PIMAGE_SECTION_HEADER pSection);
174
175 /** @cat static print method */
176public:
177 static VOID printf(PCSZ pszFormat, ...);
178
179private:
180 /** @cat private data members - allways present. */
181 #ifdef DEBUG
182 BOOL fInitTime; /* init time indicator (debug) */
183 #endif
184 SFN hFile; /* filehandle */
185 PSZ pszFilename; /* fullpath */
186 PSZ pszModuleName; /* filename without extention. */
187 BOOL fAllInOneObject; /* The All-in-object fix will be or is applied. */
188 PLXOBJECT paObjects; /* Pointer to object array. */
189 USHORT cObjects; /* Count of elements in the object array. */
190 USHORT cObjectsAllocated; /* Size of the object array. */
191
192 /**
193 * @cat LX structures
194 */
195 struct e32_exe LXHdr; /* Lxheader */
196
197 struct o32_obj *paObjTab; /* Pointer to object table - if null check cObjects > 0 and generate it using makeObjectTable */
198 struct o32_map *paObjPageTab; /* Pointer to object page table - if null check cObjects > 0 and generate it using makeObjectPageTable */
199
200 PCHAR pachResNameTable; /* Pointer to resident name table. */
201 ULONG offCurResName; /* Offset of the next entry in the resident name table. */
202 ULONG cchRNTAllocated; /* Count of char allocated for the resident name table. */
203
204 struct b32_bundle *pEntryBundles; /* Pointer to entry bundles. (exports) */
205 ULONG offCurEntryBundle; /* Offset of the next bundle. */
206 ULONG offLastEntryBundle; /* Offset of the last entry bundle. */
207 ULONG ulLastOrdinal; /* Ordinal number of last entry which was added. */
208 ULONG cbEBAllocated; /* Count of bytes allocate for entry bundles. */
209 BOOL fForwarders; /* Set if forwarders are present. */
210
211 PULONG paulFixupPageTable; /* Pointer to fixup pagetable. If null generate it using makeFixups. */
212 ULONG cFixupPTEntries; /* Number of entries in the fixup page table. */
213 ULONG cFPTEAllocated; /* Number of page table entries allocated. */
214
215 PVOID pFixupRecords; /* Pointer to fixup records. If null generate it using makeFixups. */
216 ULONG offCurFixupRec; /* Offset of next fixup. */
217 ULONG cbFRAllocated; /* Count of bytes allocated for Fixup records. */
218
219 PVOID pvCrossPageFixup; /* Pointer to cross page fixup. */
220 ULONG cbCrossPageFixup; /* Fixup size in bytes. */
221
222 PCHAR pachImpModuleNames; /* Pointer to list of module names. */
223 ULONG offCurImpModuleName; /* Offset of next modulename. */
224 ULONG cchIMNAllocated; /* Count of chars allocated for pachImpModuleNames. */
225
226 PCHAR pachImpFunctionNames; /* Pointer to list of function names. */
227 ULONG offCurImpFunctionName; /* Offset of next functionname. */
228 ULONG cchIFNAllocated; /* Count of chars allocated for pachImpFunctionNames. */
229
230 /**
231 * @cat PE structues
232 */
233 ULONG offNtHeaders; /* Fileoffset of the PE\0\0 signature. */
234 PIMAGE_NT_HEADERS pNtHdrs; /* Pointer to NT-Headers. If null load it using loadNtHeaders. */
235 ULONG ulImageBase; /* Image base address. */
236
237 /**
238 * @cat static data.
239 */
240 static struct LieListEntry /* Dll Name lie table. */
241 {
242 PCSZ pszWin32Name; /* Win32 dll name. */
243 PCSZ pszOdin32Name; /* Odin32 dll name. */
244 } paLieList[];
245
246 static struct PeCharacteristicsToLxFlags/* section characteristics to object flags */
247 {
248 unsigned int Characteristics; /* set of section characteristics */
249 ULONG flFlags; /* equivalent object flags */
250 } paSecChars2Flags[];
251
252public:
253 static ULONG ulInfoLevel; /* Current output message detail level. */
254 enum {Quiet, Error, Warning, Info, InfoAll}; /* Output message detail levels. */
255};
256
257
258/**
259 * BufferedRVARead - read at RVA with buffering.
260 * @author knut st. osmundsen
261 * @approval knut st. osmundsen
262 */
263class BufferedRVARead
264{
265public:
266 BufferedRVARead(SFN hFile, ULONG cObjects, PCLXOBJECT paObjects);
267#if 0
268 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
269#else
270 /**
271 * Reads a chunk of data at the spcified RVA.
272 * @returns NO_ERROR on success.
273 * ERROR_INVALID_PARAMETER
274 * <Whatever rc ReadAt returns>
275 * @param ulRVA RVA to read from. Within the filesize.
276 * @param pvBuffer Pointer to output buffer. pvBuffer > 64KB
277 * @param cbBuffer Number of bytes to read. 0 < cbBuffer > 256MB
278 * @status completely
279 * @author knut st. osmundsen
280 */
281 inline ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
282 {
283 /*
284 * five cases:
285 * 1) entire area is within the buffer.
286 * 2) start of area is within the buffer.
287 * 3) end of area is within the buffer.
288 * 4) the area is larger than the buffer, covering it.
289 * 5) the area is outside the buffer.
290 *
291 * these are optimal: 1, 2, and 5.
292 * The request is allways process from start to end. This will make case 3 and 4 less effecient.
293 */
294 #ifdef DEBUG
295 if (ulRVA == ~0UL || (ULONG)pvBuffer < 0x10000UL || cbBuffer == 0UL || cbBuffer >= 0x10000000UL)
296 return ERROR_INVALID_PARAMETER;
297 #endif
298
299 do
300 {
301 if (ulRVA >= this->ulRVA && ulRVA < this->ulRVA + sizeof(achBuffer))
302 { /* in buffer */
303 register ULONG cbRead = sizeof(achBuffer) - (ulRVA - this->ulRVA);
304 cbRead = min(cbRead, cbBuffer);
305 memcpy(pvBuffer, &achBuffer[ulRVA - this->ulRVA], (size_t)cbRead);
306 if (cbBuffer == cbRead)
307 return NO_ERROR;
308 cbBuffer -= cbRead;
309 pvBuffer = (PVOID)((ULONG)pvBuffer + cbRead);
310 ulRVA += cbRead;
311 }
312 else
313 { /* not in buffer, then read it into the buffer! */
314 APIRET rc = readToBuffer(ulRVA);
315 if (rc != NO_ERROR)
316 return rc;
317 }
318 } while (cbBuffer != 0UL);
319
320 return NO_ERROR;
321 }
322#endif
323
324 ULONG dupString(ULONG ulRVA, PSZ *ppsz);
325 BufferedRVARead & operator =(BufferedRVARead &SrcObj);
326
327private:
328 ULONG readToBuffer(ULONG ulRVA);
329
330private:
331 SFN hFile; /* Filehandle. */
332 ULONG cObjects; /* Count of objects */
333 PCLXOBJECT paObjects; /* Pointer to a readonly array of objects. */
334 ULONG ulRVA; /* RVA for the buffer start */
335 CHAR achBuffer[BUFFEREDRVAREADER_BUFFERSIZE]; /* Buffer. NOTE! Code assumes that it is a page. */
336};
337
338#endif
Note: See TracBrowser for help on using the repository browser.