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

Last change on this file since 2912 was 2541, checked in by bird, 26 years ago

applyFixup changes.++

File size: 11.4 KB
Line 
1/* $Id: pe2lx.h,v 1.8 2000-01-27 23:48: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@pmsc.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 #ifndef RING0
69 ULONG testApplyFixups();
70 ULONG writeLxFile(PCSZ pszLXFilename);
71 #endif
72
73 /** @cat public Helper methods */
74 ULONG querySizeOfLxFile();
75 VOID dumpVirtualLxFile();
76
77private:
78 /** @cat conversion methods */
79 ULONG makeObjectTable();
80 ULONG makeObjectPageTable();
81 ULONG makeFixups();
82 ULONG makeExports();
83
84 /** @cat conversion helper(s) */
85 ULONG loadNtHeaders();
86 VOID releaseNtHeaders();
87 ULONG loadBaseRelocations();
88
89 /** @cat init() helper methods - may only be called at init time! */
90 ULONG addObject(ULONG ulRVA, ULONG cbPhysical, ULONG cbVirtual, ULONG flFlags, ULONG offPEFile);
91 ULONG addTIBFixObject();
92 ULONG addStackObject(ULONG cbStack);
93
94 /** @cat PE helper methods */
95
96 /** @cat Fixups Helpers - calls allowed from makeFixups only! */
97 ULONG initFixups();
98 ULONG addPageFixupEntry(BOOL fLast = FALSE);
99 ULONG add32OffsetFixup(WORD offSource, ULONG ulTarget);
100 ULONG add32OrdImportFixup(WORD offSource, ULONG ulModuleOrdinal, ULONG ulFunctionOrdinal);
101 ULONG add32NameImportFixup(WORD offSource, ULONG ulModuleOrdinal, PCSZ pszFnName);
102 ULONG addModule(PCSZ pszModuleName, PULONG pulModuleOrdinal);
103 ULONG addImportFunctionName(PCSZ pszFnName, PULONG poffFnName);
104 VOID finalizeFixups();
105 VOID finalizeImportNames();
106
107 /** @cat Entry table / Resident nametable helpers */
108 ULONG initEntry();
109 ULONG addResName(ULONG ulOrdinal, PCSZ pszName, ULONG cchName);
110 ULONG addEntry(ULONG ulOrdinal, ULONG ulRVA);
111 ULONG addForwarderEntry(ULONG ulOrdinal, PCSZ pszDllName, PCSZ pszFnNameOrOrd);
112 ULONG addLastEntry();
113 VOID finalizeExports();
114
115 /** @cat Misc helpers */
116 ULONG getCountOfPages();
117 ULONG queryObjectAndOffset(ULONG ulRVA, PULONG pulObject, PULONG poffObject);
118 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer);
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 PIMAGE_BASE_RELOCATION pBaseRelocs; /* Baserelocations. */
179 ULONG cbBaseRelocs; /* Size of baserelocations. */
180 ULONG fApplyFixups; /* TRUE: apply fixups, FALSE: do not apply fixups, ~0=check if need to apply fixups */
181 ULONG fDeltaOnly; /* TRUE: apply fixups only as deltas. FALSE: needs to calculate target address... */
182
183 /**
184 * @cat static data.
185 */
186 static struct LieListEntry /* Dll Name lie table. */
187 {
188 PCSZ pszWin32Name; /* Win32 dll name. */
189 PCSZ pszOdin32Name; /* Odin32 dll name. */
190 } paLieList[];
191
192 static struct PeCharacteristicsToLxFlags/* section characteristics to object flags */
193 {
194 unsigned int Characteristics; /* set of section characteristics */
195 ULONG flFlags; /* equivalent object flags */
196 } paSecChars2Flags[];
197};
198
199
200/**
201 * BufferedRVARead - read at RVA with buffering.
202 * @author knut st. osmundsen
203 * @approval knut st. osmundsen
204 */
205class BufferedRVARead
206{
207public:
208 BufferedRVARead(SFN hFile, ULONG cObjects, PCLXOBJECT paObjects);
209#if 0
210 ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
211#else
212 /**
213 * Reads a chunk of data at the spcified RVA.
214 * @returns NO_ERROR on success.
215 * ERROR_INVALID_PARAMETER
216 * <Whatever rc ReadAt returns>
217 * @param ulRVA RVA to read from. Within the filesize.
218 * @param pvBuffer Pointer to output buffer. pvBuffer > 64KB
219 * @param cbBuffer Number of bytes to read. 0 < cbBuffer > 256MB
220 * @status completely
221 * @author knut st. osmundsen
222 */
223 inline ULONG readAtRVA(ULONG ulRVA, PVOID pvBuffer, ULONG cbBuffer)
224 {
225 /*
226 * five cases:
227 * 1) entire area is within the buffer.
228 * 2) start of area is within the buffer.
229 * 3) end of area is within the buffer.
230 * 4) the area is larger than the buffer, covering it.
231 * 5) the area is outside the buffer.
232 *
233 * these are optimal: 1, 2, and 5.
234 * The request is allways process from start to end. This will make case 3 and 4 less effecient.
235 */
236 #ifdef DEBUG
237 if (ulRVA == ~0UL || (ULONG)pvBuffer < 0x10000UL || cbBuffer == 0UL || cbBuffer >= 0x10000000UL)
238 return ERROR_INVALID_PARAMETER;
239 #endif
240
241 do
242 {
243 if (ulRVA >= this->ulRVA && ulRVA < this->ulRVA + sizeof(achBuffer))
244 { /* in buffer */
245 register ULONG cbRead = sizeof(achBuffer) - (ulRVA - this->ulRVA);
246 cbRead = min(cbRead, cbBuffer);
247 memcpy(pvBuffer, &achBuffer[ulRVA - this->ulRVA], (size_t)cbRead);
248 if (cbBuffer == cbRead)
249 return NO_ERROR;
250 cbBuffer -= cbRead;
251 pvBuffer = (PVOID)((ULONG)pvBuffer + cbRead);
252 ulRVA += cbRead;
253 }
254 else
255 { /* not in buffer, then read it into the buffer! */
256 APIRET rc = readToBuffer(ulRVA);
257 if (rc != NO_ERROR)
258 return rc;
259 }
260 } while (cbBuffer != 0UL);
261
262 return NO_ERROR;
263 }
264#endif
265
266 ULONG dupString(ULONG ulRVA, PSZ *ppsz);
267 BufferedRVARead & operator =(BufferedRVARead &SrcObj);
268
269private:
270 ULONG readToBuffer(ULONG ulRVA);
271
272private:
273 SFN hFile; /* Filehandle. */
274 ULONG cObjects; /* Count of objects */
275 PCLXOBJECT paObjects; /* Pointer to a readonly array of objects. */
276 ULONG ulRVA; /* RVA for the buffer start */
277 CHAR achBuffer[BUFFEREDRVAREADER_BUFFERSIZE]; /* Buffer. NOTE! Code assumes that it is a page. */
278};
279
280#endif
Note: See TracBrowser for help on using the repository browser.