1 | /*
|
---|
2 | * Win32 PE loader Image base class
|
---|
3 | *
|
---|
4 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
5 | * Copyright 1998 Knut St. Osmundsen
|
---|
6 | *
|
---|
7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
8 | *
|
---|
9 | * TODO: Check psh[i].Characteristics for more than only the code section
|
---|
10 | * TODO: Make resource section readonly when GDI32 is fixed
|
---|
11 | * TODO: Loading of forwarder dlls before handling imports might not be correct
|
---|
12 | * (circular dependencies; have to check what NT does)
|
---|
13 | * TODO: Two LoadLibrary calls in two threads at the same time won't be handled properly (rare but possible)
|
---|
14 | *
|
---|
15 | * NOTE: FLAG_PELDR_LOADASDATAFILE is a special flag to only load the resource directory
|
---|
16 | * of a PE image. Processing imports, sections etc is not done.
|
---|
17 | * This is useful for GetVersionSize/Resource in case it wants to
|
---|
18 | * get version info of an image that is not loaded.
|
---|
19 | * So an instance of this type can't be used for anything but resource lookup!
|
---|
20 | *
|
---|
21 | *
|
---|
22 | */
|
---|
23 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
24 | #define INCL_DOSMODULEMGR
|
---|
25 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
26 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
27 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
28 | #define INCL_WIN
|
---|
29 | #define INCL_BASE
|
---|
30 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
31 |
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | //use a different logfile
|
---|
38 | #define PRIVATE_LOGGING
|
---|
39 | #include <misc.h>
|
---|
40 | #include <win32api.h>
|
---|
41 | #include "winimagebase.h"
|
---|
42 | #include "winimagepeldr.h"
|
---|
43 | #include "windllpeldr.h"
|
---|
44 | #include "windlllx.h"
|
---|
45 | #include "winexebase.h"
|
---|
46 | #include <pefile.h>
|
---|
47 | #include <unicode.h>
|
---|
48 | #include "oslibmisc.h"
|
---|
49 | #include "initterm.h"
|
---|
50 | #include <win\virtual.h>
|
---|
51 | #include "oslibdos.h"
|
---|
52 | #include "mmap.h"
|
---|
53 | #include <wprocess.h>
|
---|
54 |
|
---|
55 |
|
---|
56 | //Define COMMIT_ALL to let the pe loader commit all sections of the image
|
---|
57 | //This is very useful during debugging as you'll get lots of exceptions
|
---|
58 | //otherwise.
|
---|
59 | //#ifdef DEBUG
|
---|
60 | #define COMMIT_ALL
|
---|
61 | //#endif
|
---|
62 |
|
---|
63 | char szErrorTitle[] = "Odin";
|
---|
64 | char szMemErrorMsg[] = "Memory allocation failure";
|
---|
65 | char szFileErrorMsg[] = "File IO error";
|
---|
66 | char szPEErrorMsg[] = "Not a valid win32 exe. (perhaps 16 bits windows)";
|
---|
67 | char szCPUErrorMsg[] = "Executable doesn't run on x86 machines";
|
---|
68 | char szExeErrorMsg[] = "File isn't an executable";
|
---|
69 | char szInteralErrorMsg[]= "Internal Error";
|
---|
70 | char szErrorModule[128] = "";
|
---|
71 |
|
---|
72 | #ifdef DEBUG
|
---|
73 | static FILE *_privateLogFile = NULL;
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | ULONG MissingApi();
|
---|
77 |
|
---|
78 | //******************************************************************************
|
---|
79 | //******************************************************************************
|
---|
80 | void OpenPrivateLogFilePE()
|
---|
81 | {
|
---|
82 | #ifdef DEBUG
|
---|
83 | char logname[CCHMAXPATH];
|
---|
84 |
|
---|
85 | sprintf(logname, "pe_%d.log", loadNr);
|
---|
86 | _privateLogFile = fopen(logname, "w");
|
---|
87 | if(_privateLogFile == NULL) {
|
---|
88 | sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
|
---|
89 | _privateLogFile = fopen(logname, "w");
|
---|
90 | }
|
---|
91 | dprintfGlobal(("PE LOGFILE : %s", logname));
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 | //******************************************************************************
|
---|
95 | //******************************************************************************
|
---|
96 | void ClosePrivateLogFilePE()
|
---|
97 | {
|
---|
98 | #ifdef DEBUG
|
---|
99 | if(_privateLogFile) {
|
---|
100 | fclose(_privateLogFile);
|
---|
101 | _privateLogFile = NULL;
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 | }
|
---|
105 | //******************************************************************************
|
---|
106 | //******************************************************************************
|
---|
107 | Win32PeLdrImage::Win32PeLdrImage(char *pszFileName, BOOL isExe) :
|
---|
108 | Win32ImageBase(-1),
|
---|
109 | nrsections(0), imageSize(0), dwFlags(0), section(NULL),
|
---|
110 | imageVirtBase(-1), realBaseAddress(0), imageVirtEnd(0),
|
---|
111 | nrNameExports(0), nrOrdExports(0),
|
---|
112 | nameexports(NULL), ordexports(NULL),
|
---|
113 | curnameexport(NULL), curordexport(NULL),
|
---|
114 | memmap(NULL), pFixups(NULL), dwFixupSize(0)
|
---|
115 | {
|
---|
116 | HFILE dllfile;
|
---|
117 |
|
---|
118 | strcpy(szFileName, pszFileName);
|
---|
119 | strupr(szFileName);
|
---|
120 | if(isExe) {
|
---|
121 | if(!strchr(szFileName, '.')) {
|
---|
122 | strcat(szFileName,".EXE");
|
---|
123 | }
|
---|
124 | dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
125 | if(dllfile == NULL) {
|
---|
126 | if(!strstr(szFileName, ".EXE")) {
|
---|
127 | strcat(szFileName,".EXE");
|
---|
128 | }
|
---|
129 | dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
130 | if(dllfile == NULL) {
|
---|
131 | OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else OSLibDosClose(dllfile);
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | findDll(szFileName, szModule, sizeof(szModule));
|
---|
138 | strcpy(szFileName, szModule);
|
---|
139 | }
|
---|
140 | strcpy(szModule, OSLibStripPath(szFileName));
|
---|
141 | strupr(szModule);
|
---|
142 | }
|
---|
143 | //******************************************************************************
|
---|
144 | //******************************************************************************
|
---|
145 | Win32PeLdrImage::~Win32PeLdrImage()
|
---|
146 | {
|
---|
147 | if(memmap)
|
---|
148 | delete memmap;
|
---|
149 |
|
---|
150 | if(hFile) {
|
---|
151 | OSLibDosClose(hFile);
|
---|
152 | hFile = 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if(realBaseAddress)
|
---|
156 | DosFreeMem((PVOID)realBaseAddress);
|
---|
157 |
|
---|
158 | if(nameexports)
|
---|
159 | free(nameexports);
|
---|
160 |
|
---|
161 | if(ordexports)
|
---|
162 | free(ordexports);
|
---|
163 |
|
---|
164 | if(section)
|
---|
165 | free(section);
|
---|
166 | }
|
---|
167 | //******************************************************************************
|
---|
168 | //******************************************************************************
|
---|
169 | BOOL Win32PeLdrImage::init(ULONG reservedMem)
|
---|
170 | {
|
---|
171 | LPVOID win32file = NULL;
|
---|
172 | ULONG filesize, ulRead, ulNewPos;
|
---|
173 | PIMAGE_SECTION_HEADER psh;
|
---|
174 | IMAGE_SECTION_HEADER sh;
|
---|
175 | IMAGE_TLS_DIRECTORY *tlsDir = NULL;
|
---|
176 | int nSections, i;
|
---|
177 | char szFullPath[CCHMAXPATH] = "";
|
---|
178 | IMAGE_DOS_HEADER doshdr;
|
---|
179 | ULONG signature;
|
---|
180 |
|
---|
181 | hFile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
---|
182 |
|
---|
183 | //default error:
|
---|
184 | strcpy(szErrorModule, OSLibStripPath(szFileName));
|
---|
185 | if(hFile == NULL) {
|
---|
186 | goto failure;
|
---|
187 | }
|
---|
188 | //read dos header
|
---|
189 | if(DosRead(hFile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
|
---|
190 | goto failure;
|
---|
191 | }
|
---|
192 | if(OSLibDosSetFilePtr(hFile, doshdr.e_lfanew, OSLIB_SETPTR_FILE_BEGIN) == -1) {
|
---|
193 | goto failure;
|
---|
194 | }
|
---|
195 | //read signature dword
|
---|
196 | if(DosRead(hFile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
|
---|
197 | goto failure;
|
---|
198 | }
|
---|
199 | //read pe header
|
---|
200 | if(DosRead(hFile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
|
---|
201 | goto failure;
|
---|
202 | }
|
---|
203 | //read optional header
|
---|
204 | if(DosRead(hFile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
|
---|
205 | goto failure;
|
---|
206 | }
|
---|
207 | if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
|
---|
208 | dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
|
---|
209 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
210 | goto failure;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if(oh.SizeOfImage == 0) {//just in case
|
---|
214 | oh.SizeOfImage = OSLibDosGetFileSize(hFile, NULL);
|
---|
215 | }
|
---|
216 |
|
---|
217 | imageSize = oh.SizeOfImage;
|
---|
218 | //Allocate memory to hold the entire image
|
---|
219 | if(allocSections(reservedMem) == FALSE) {
|
---|
220 | dprintf((LOG, "Failed to allocate image memory for %s at %x, rc %d", szFileName, oh.ImageBase, errorState));
|
---|
221 | goto failure;
|
---|
222 | }
|
---|
223 |
|
---|
224 | memmap = new Win32MemMap(this, realBaseAddress, imageSize);
|
---|
225 | if(memmap == NULL || !memmap->Init()) {
|
---|
226 | goto failure;
|
---|
227 | }
|
---|
228 | win32file = memmap->mapViewOfFile(0, 0, 2);
|
---|
229 |
|
---|
230 | if(DosQueryPathInfo(szFileName, FIL_QUERYFULLNAME, szFullPath, sizeof(szFullPath)) == 0) {
|
---|
231 | setFullPath(szFullPath);
|
---|
232 | }
|
---|
233 |
|
---|
234 | if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
|
---|
235 | dprintf((LOG, "Not a valid PE file!"));
|
---|
236 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
237 | goto failure;
|
---|
238 | }
|
---|
239 | if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
|
---|
240 | dprintf((LOG, "Doesn't run on x86 processors!"));
|
---|
241 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
242 | goto failure;
|
---|
243 | }
|
---|
244 | //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
|
---|
245 | if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
|
---|
246 | dprintf((LOG, "Can't convert system files"));
|
---|
247 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
248 | goto failure;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
---|
252 | dprintf((LOG, "No fixups, might not run!"));
|
---|
253 | }
|
---|
254 |
|
---|
255 | dprintf((LOG, "PE file : %s", szFileName));
|
---|
256 | dprintf((LOG, "PE Optional header: "));
|
---|
257 | dprintf((LOG, "Preferred address : %d", oh.ImageBase ));
|
---|
258 | dprintf((LOG, "Base Of Code : %d", oh.BaseOfCode ));
|
---|
259 | dprintf((LOG, "CodeSize : %d", oh.SizeOfCode ));
|
---|
260 | dprintf((LOG, "Base Of Data : %d", oh.BaseOfData ));
|
---|
261 | dprintf((LOG, "Data Size (uninit): %d", oh.SizeOfUninitializedData ));
|
---|
262 | dprintf((LOG, "Data Size (init) : %d", oh.SizeOfInitializedData ));
|
---|
263 | dprintf((LOG, "Entry Point : %d", oh.AddressOfEntryPoint ));
|
---|
264 | dprintf((LOG, "Section Alignment : %d", oh.SectionAlignment ));
|
---|
265 | dprintf((LOG, "Stack Reserve size: %d", oh.SizeOfStackReserve ));
|
---|
266 | dprintf((LOG, "Stack Commit size : %d", oh.SizeOfStackCommit ));
|
---|
267 | dprintf((LOG, "SizeOfHeapReserve : %d", oh.SizeOfHeapReserve ));
|
---|
268 | dprintf((LOG, "SizeOfHeapCommit : %d", oh.SizeOfHeapCommit ));
|
---|
269 | dprintf((LOG, "FileAlignment : %d", oh.FileAlignment ));
|
---|
270 | dprintf((LOG, "Subsystem : %d", oh.Subsystem ));
|
---|
271 | dprintf((LOG, "Image Size : %d", oh.SizeOfImage ));
|
---|
272 | dprintf((LOG, "Header Size : %d", oh.SizeOfHeaders ));
|
---|
273 | dprintf((LOG, "MajorImageVersion : %d", oh.MajorImageVersion ));
|
---|
274 | dprintf((LOG, "MinorImageVersion : %d", oh.MinorImageVersion ));
|
---|
275 |
|
---|
276 | //get header page
|
---|
277 | commitPage(realBaseAddress, FALSE);
|
---|
278 |
|
---|
279 | nSections = NR_SECTIONS(win32file);
|
---|
280 | section = (Section *)malloc(nSections*sizeof(Section));
|
---|
281 | if(section == NULL) {
|
---|
282 | DebugInt3();
|
---|
283 | goto failure;
|
---|
284 | }
|
---|
285 | memset(section, 0, nSections*sizeof(Section));
|
---|
286 |
|
---|
287 | if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
|
---|
288 | {
|
---|
289 | imageSize = 0;
|
---|
290 | if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
|
---|
291 | {
|
---|
292 | dprintf((LOG, "*************************PE SECTIONS START**************************" ));
|
---|
293 | for (i=0; i<nSections; i++)
|
---|
294 | {
|
---|
295 | dprintf((LOG, "Raw data size: %x", psh[i].SizeOfRawData ));
|
---|
296 | dprintf((LOG, "Virtual Address: %x", psh[i].VirtualAddress ));
|
---|
297 | dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
|
---|
298 | dprintf((LOG, "Virtual Address End: %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
|
---|
299 | dprintf((LOG, "Virtual Size: %x", psh[i].Misc.VirtualSize ));
|
---|
300 | dprintf((LOG, "Pointer to raw data: %x", psh[i].PointerToRawData ));
|
---|
301 | dprintf((LOG, "Section flags: %x\n\n", psh[i].Characteristics ));
|
---|
302 |
|
---|
303 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
|
---|
304 | {
|
---|
305 | dprintf((LOG, ".reloc" ));
|
---|
306 | addSection(SECTION_RELOC, psh[i].PointerToRawData,
|
---|
307 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
308 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
309 | continue;
|
---|
310 | }
|
---|
311 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
|
---|
312 | {
|
---|
313 | //SvL: Angus.exe has empty export section that's really an
|
---|
314 | // uninitialized data section
|
---|
315 | if(psh[i].SizeOfRawData) {
|
---|
316 | dprintf((LOG, ".edata" ));
|
---|
317 | addSection(SECTION_EXPORT, psh[i].PointerToRawData,
|
---|
318 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
319 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
320 | continue;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
|
---|
324 | {
|
---|
325 | dprintf((LOG, ".rsrc" ));
|
---|
326 | addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
|
---|
327 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
328 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
329 | continue;
|
---|
330 | }
|
---|
331 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
|
---|
332 | {
|
---|
333 | dprintf((LOG, "TLS section"));
|
---|
334 | tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
|
---|
335 | if(tlsDir) {
|
---|
336 | addSection(SECTION_TLS, psh[i].PointerToRawData,
|
---|
337 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
338 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
339 | }
|
---|
340 | continue;
|
---|
341 | }
|
---|
342 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
|
---|
343 | {
|
---|
344 | dprintf((LOG, ".rdebug" ));
|
---|
345 | addSection(SECTION_DEBUG, psh[i].PointerToRawData,
|
---|
346 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
347 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
348 | continue;
|
---|
349 | }
|
---|
350 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
|
---|
351 | {
|
---|
352 | int type = SECTION_IMPORT;
|
---|
353 |
|
---|
354 | dprintf((LOG, "Import Data Section" ));
|
---|
355 | if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
|
---|
356 | dprintf((LOG, "Also Code Section"));
|
---|
357 | type |= SECTION_CODE;
|
---|
358 | }
|
---|
359 | addSection(type, psh[i].PointerToRawData,
|
---|
360 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
361 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
362 | continue;
|
---|
363 | }
|
---|
364 |
|
---|
365 | //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
|
---|
366 | if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
|
---|
367 | (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
|
---|
368 | !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
|
---|
369 | )
|
---|
370 | {
|
---|
371 | dprintf((LOG, "Code Section"));
|
---|
372 | addSection(SECTION_CODE, psh[i].PointerToRawData,
|
---|
373 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
374 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
375 | continue;
|
---|
376 | }
|
---|
377 | if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
|
---|
378 | dprintf((LOG, "Read Only Data Section" ));
|
---|
379 | addSection(SECTION_READONLYDATA, psh[i].PointerToRawData,
|
---|
380 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
381 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
382 | continue;
|
---|
383 | }
|
---|
384 | if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
|
---|
385 | dprintf((LOG, "Uninitialized Data Section" ));
|
---|
386 | addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
|
---|
387 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
388 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
389 | continue;
|
---|
390 | }
|
---|
391 | if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
|
---|
392 | dprintf((LOG, "Initialized Data Section" ));
|
---|
393 | addSection(SECTION_INITDATA, psh[i].PointerToRawData,
|
---|
394 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
395 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
396 | continue;
|
---|
397 | }
|
---|
398 | if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
|
---|
399 | dprintf((LOG, "Other Section, stored as read/write uninit data" ));
|
---|
400 | addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
|
---|
401 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
---|
402 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
---|
403 | continue;
|
---|
404 | }
|
---|
405 | dprintf((LOG, "Unknown section" ));
|
---|
406 | goto failure;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 | else {
|
---|
411 | if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh))
|
---|
412 | {
|
---|
413 | dprintf((LOG, "*************************PE SECTIONS START**************************" ));
|
---|
414 | dprintf((LOG, "Raw data size: %x", sh.SizeOfRawData ));
|
---|
415 | dprintf((LOG, "Virtual Address: %x", sh.VirtualAddress ));
|
---|
416 | dprintf((LOG, "Virtual Address Start:%x", sh.VirtualAddress+oh.ImageBase ));
|
---|
417 | dprintf((LOG, "Virtual Address End: %x", sh.VirtualAddress+oh.ImageBase+sh.Misc.VirtualSize ));
|
---|
418 | dprintf((LOG, "Virtual Size: %x", sh.Misc.VirtualSize ));
|
---|
419 | dprintf((LOG, "Pointer to raw data: %x", sh.PointerToRawData ));
|
---|
420 | dprintf((LOG, "Section flags: %x\n\n", sh.Characteristics ));
|
---|
421 | addSection(SECTION_RESOURCE, sh.PointerToRawData,
|
---|
422 | sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
|
---|
423 | sh.Misc.VirtualSize, sh.Characteristics);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | dprintf((LOG, "*************************PE SECTIONS END **************************" ));
|
---|
427 |
|
---|
428 | imageSize += imageVirtBase - oh.ImageBase;
|
---|
429 | dprintf((LOG, "Total size of Image %x", imageSize ));
|
---|
430 | dprintf((LOG, "imageVirtBase %x", imageVirtBase ));
|
---|
431 | dprintf((LOG, "imageVirtEnd %x", imageVirtEnd ));
|
---|
432 |
|
---|
433 | //In case there are any gaps between sections, adjust size
|
---|
434 | if(imageSize != imageVirtEnd - oh.ImageBase)
|
---|
435 | {
|
---|
436 | dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
|
---|
437 | imageSize = imageVirtEnd - oh.ImageBase;
|
---|
438 | }
|
---|
439 | if(imageSize < oh.SizeOfImage) {
|
---|
440 | imageSize = oh.SizeOfImage;
|
---|
441 | }
|
---|
442 |
|
---|
443 | dprintf((LOG, "OS/2 base address %x", realBaseAddress ));
|
---|
444 | if(oh.AddressOfEntryPoint) {
|
---|
445 | entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
|
---|
446 | }
|
---|
447 | else {
|
---|
448 | dprintf((LOG, "EntryPoint == NULL" ));
|
---|
449 | entryPoint = NULL;
|
---|
450 | }
|
---|
451 |
|
---|
452 | //set memory protection flags
|
---|
453 | if(setMemFlags() == FALSE) {
|
---|
454 | dprintf((LOG, "Failed to set memory protection" ));
|
---|
455 | goto failure;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if(realBaseAddress != oh.ImageBase && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
|
---|
459 | pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
---|
460 | dwFixupSize = ImageDirectorySize(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
---|
461 | commitPage((ULONG)pFixups, FALSE);
|
---|
462 | }
|
---|
463 |
|
---|
464 | if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
|
---|
465 | {
|
---|
466 | if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS))
|
---|
467 | {
|
---|
468 | Section *sect;
|
---|
469 | BOOL fTLSFixups = FALSE;
|
---|
470 |
|
---|
471 | sect = findSectionByAddr(tlsDir->StartAddressOfRawData);
|
---|
472 | //There might be fixups for the TLS structure, so search the sections
|
---|
473 | //by the OS/2 virtual address too
|
---|
474 | if(sect == NULL) {
|
---|
475 | sect = findSectionByOS2Addr(tlsDir->StartAddressOfRawData);
|
---|
476 | fTLSFixups = TRUE;
|
---|
477 | }
|
---|
478 |
|
---|
479 | dprintf((LOG, "TLS Directory" ));
|
---|
480 | dprintf((LOG, "TLS Address of Index %x", tlsDir->AddressOfIndex ));
|
---|
481 | dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
|
---|
482 | dprintf((LOG, "TLS SizeOfZeroFill %x", tlsDir->SizeOfZeroFill ));
|
---|
483 | dprintf((LOG, "TLS Characteristics %x", tlsDir->Characteristics ));
|
---|
484 | if(sect == NULL) {
|
---|
485 | dprintf((LOG, "Couldn't find TLS section!!" ));
|
---|
486 | goto failure;
|
---|
487 | }
|
---|
488 | setTLSAddress((char *)sect->realvirtaddr);
|
---|
489 | setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
|
---|
490 | setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
|
---|
491 |
|
---|
492 | fTLSFixups = FALSE;
|
---|
493 | sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
|
---|
494 | //There might be fixups for the TLS structure, so search the sections
|
---|
495 | //by the OS/2 virtual address too
|
---|
496 | if(sect == NULL) {
|
---|
497 | sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
|
---|
498 | fTLSFixups = TRUE;
|
---|
499 | }
|
---|
500 | if(sect == NULL) {
|
---|
501 | dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
|
---|
502 | goto failure;
|
---|
503 | }
|
---|
504 | if(fTLSFixups) {
|
---|
505 | setTLSIndexAddr((LPDWORD)tlsDir->AddressOfIndex); //no fixup required
|
---|
506 | }
|
---|
507 | else {//need to add a manual fixup
|
---|
508 | setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
|
---|
509 | }
|
---|
510 |
|
---|
511 | if((ULONG)tlsDir->AddressOfCallBacks != 0)
|
---|
512 | {
|
---|
513 | fTLSFixups = FALSE;
|
---|
514 |
|
---|
515 | sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
|
---|
516 | //There might be fixups for the TLS structure, so search the sections
|
---|
517 | //by the OS/2 virtual address too
|
---|
518 | if(sect == NULL) {
|
---|
519 | sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
|
---|
520 | fTLSFixups = TRUE;
|
---|
521 | }
|
---|
522 | if(sect == NULL) {
|
---|
523 | dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
|
---|
524 | goto failure;
|
---|
525 | }
|
---|
526 | if(fTLSFixups) {
|
---|
527 | setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)tlsDir->AddressOfCallBacks); //no fixup required
|
---|
528 | }
|
---|
529 | else {//need to add a manual fixup
|
---|
530 | setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
|
---|
531 | }
|
---|
532 | //modify tls callback pointers for new image base address
|
---|
533 | int i = 0;
|
---|
534 | while(tlsCallBackAddr[i])
|
---|
535 | {
|
---|
536 | fTLSFixups = FALSE;
|
---|
537 |
|
---|
538 | sect = findSectionByAddr((ULONG)tlsCallBackAddr[i]);
|
---|
539 | //There might be fixups for the TLS structure, so search the sections
|
---|
540 | //by the OS/2 virtual address too
|
---|
541 | if(sect == NULL) {
|
---|
542 | sect = findSectionByOS2Addr((ULONG)tlsCallBackAddr[i]);
|
---|
543 | fTLSFixups = TRUE;
|
---|
544 | }
|
---|
545 | if(sect == NULL) {
|
---|
546 | dprintf((LOG, "Couldn't find TLS callback section!!" ));
|
---|
547 | goto failure;
|
---|
548 | }
|
---|
549 | if(fTLSFixups) {
|
---|
550 | tlsCallBackAddr[i] = tlsCallBackAddr[i];
|
---|
551 | }
|
---|
552 | else tlsCallBackAddr[i] = (PIMAGE_TLS_CALLBACK)(realBaseAddress + ((ULONG)tlsCallBackAddr[i] - oh.ImageBase));
|
---|
553 | i++;
|
---|
554 | }
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | #ifdef DEBUG
|
---|
559 | dprintf((LOG, "Image directories: "));
|
---|
560 | for (i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; i++)
|
---|
561 | {
|
---|
562 | char *pszName;
|
---|
563 |
|
---|
564 | if(oh.DataDirectory[i].VirtualAddress && oh.DataDirectory[i].Size) {
|
---|
565 | switch (i)
|
---|
566 | {
|
---|
567 | case IMAGE_DIRECTORY_ENTRY_EXPORT: pszName = "Export Directory (IMAGE_DIRECTORY_ENTRY_EXPORT)"; break;
|
---|
568 | case IMAGE_DIRECTORY_ENTRY_IMPORT: pszName = "Import Directory (IMAGE_DIRECTORY_ENTRY_IMPORT)"; break;
|
---|
569 | case IMAGE_DIRECTORY_ENTRY_RESOURCE: pszName = "Resource Directory (IMAGE_DIRECTORY_ENTRY_RESOURCE)"; break;
|
---|
570 | case IMAGE_DIRECTORY_ENTRY_EXCEPTION: pszName = "Exception Directory (IMAGE_DIRECTORY_ENTRY_EXCEPTION)"; break;
|
---|
571 | case IMAGE_DIRECTORY_ENTRY_SECURITY: pszName = "Security Directory (IMAGE_DIRECTORY_ENTRY_SECURITY)"; break;
|
---|
572 | case IMAGE_DIRECTORY_ENTRY_BASERELOC: pszName = "Base Relocation Table (IMAGE_DIRECTORY_ENTRY_BASERELOC)"; break;
|
---|
573 | case IMAGE_DIRECTORY_ENTRY_DEBUG: pszName = "Debug Directory (IMAGE_DIRECTORY_ENTRY_DEBUG)"; break;
|
---|
574 | case IMAGE_DIRECTORY_ENTRY_COPYRIGHT: pszName = "Description String (IMAGE_DIRECTORY_ENTRY_COPYRIGHT)"; break;
|
---|
575 | case IMAGE_DIRECTORY_ENTRY_GLOBALPTR: pszName = "Machine Value (MIPS GP) (IMAGE_DIRECTORY_ENTRY_GLOBALPTR)"; break;
|
---|
576 | case IMAGE_DIRECTORY_ENTRY_TLS: pszName = "TLS Directory (IMAGE_DIRECTORY_ENTRY_TLS)"; break;
|
---|
577 | case IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: pszName = "Load Configuration Directory (IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG)"; break;
|
---|
578 | case IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT:pszName = "Bound Import Directory in headers (IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT)"; break;
|
---|
579 | case IMAGE_DIRECTORY_ENTRY_IAT: pszName = "Import Address Table (IMAGE_DIRECTORY_ENTRY_IAT)"; break;
|
---|
580 | default:
|
---|
581 | pszName = "unknown";
|
---|
582 | }
|
---|
583 | dprintf((LOG, "directory %s", pszName));
|
---|
584 | dprintf((LOG, " Address 0x%08x", oh.DataDirectory[i].VirtualAddress));
|
---|
585 | dprintf((LOG, " Size 0x%08x", oh.DataDirectory[i].Size));
|
---|
586 | }
|
---|
587 | }
|
---|
588 | dprintf((LOG, "\n\n"));
|
---|
589 | #endif
|
---|
590 |
|
---|
591 | #ifdef COMMIT_ALL
|
---|
592 | for (i=0; i<nSections; i++) {
|
---|
593 | commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
|
---|
594 | }
|
---|
595 | #else
|
---|
596 | for (i=0; i<nSections; i++) {
|
---|
597 | switch(section[i].type)
|
---|
598 | {
|
---|
599 | case SECTION_IMPORT:
|
---|
600 | case SECTION_RELOC:
|
---|
601 | case SECTION_EXPORT:
|
---|
602 | commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
|
---|
603 | break;
|
---|
604 | }
|
---|
605 | }
|
---|
606 | #endif
|
---|
607 | if(processExports((char *)win32file) == FALSE) {
|
---|
608 | dprintf((LOG, "Failed to process exported apis" ));
|
---|
609 | goto failure;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | #ifdef COMMIT_ALL
|
---|
613 | else {
|
---|
614 | commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
|
---|
615 | }
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | #ifndef COMMIT_ALL
|
---|
619 | if(entryPoint) {
|
---|
620 | //commit code at entrypoint, since we going to call it anyway
|
---|
621 | commitPage((ULONG)entryPoint, FALSE);
|
---|
622 | }
|
---|
623 | #endif
|
---|
624 |
|
---|
625 | //SvL: Use pointer to image header as module handle now. Some apps needs this
|
---|
626 | hinstance = (HINSTANCE)realBaseAddress;
|
---|
627 |
|
---|
628 | //SvL: Set instance handle in process database structure
|
---|
629 | SetPDBInstance(hinstance);
|
---|
630 |
|
---|
631 | //PH: get pResRootDir pointer correct first, since processImports may
|
---|
632 | // implicitly call functions depending on it.
|
---|
633 | if(oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress && oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size)
|
---|
634 | {
|
---|
635 | //get offset in resource object of directory entry
|
---|
636 | pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)(oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress + realBaseAddress);
|
---|
637 | ulRVAResourceSection = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
|
---|
638 | }
|
---|
639 |
|
---|
640 | //Allocate TLS index for this module
|
---|
641 | //Must do this before dlls are loaded for this module. Some apps assume
|
---|
642 | //they get TLS index 0 for their main executable
|
---|
643 | tlsAlloc();
|
---|
644 | tlsAttachThread(); //setup TLS (main thread)
|
---|
645 |
|
---|
646 | if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
|
---|
647 | {
|
---|
648 | if(processImports((char *)win32file) == FALSE) {
|
---|
649 | dprintf((LOG, "Failed to process imports!" ));
|
---|
650 | goto failure;
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | return(TRUE);
|
---|
655 |
|
---|
656 | failure:
|
---|
657 | if(memmap) {
|
---|
658 | delete memmap;
|
---|
659 | memmap = NULL;
|
---|
660 | }
|
---|
661 | if(hFile) {
|
---|
662 | OSLibDosClose(hFile);
|
---|
663 | hFile = 0;
|
---|
664 | }
|
---|
665 | errorState = ERROR_INTERNAL;
|
---|
666 | return FALSE;
|
---|
667 | }
|
---|
668 | //******************************************************************************
|
---|
669 | // commitPage:
|
---|
670 | // commits image page(s) when an access violation exception is received
|
---|
671 | // (usually called from exception.cpp; also from other methods in this file)
|
---|
672 | //
|
---|
673 | // Parameters:
|
---|
674 | // virtAddress - address of exception (rounded down to page boundary)
|
---|
675 | // fWriteAccess - type of access violation (read or write)
|
---|
676 | // fPageCmd - SINGLE_PAGE -> commit single page
|
---|
677 | // SECTION_PAGES -> commit default nr of pages
|
---|
678 | // COMPLETE_SECTION -> commit entire section
|
---|
679 | //
|
---|
680 | // Remarks:
|
---|
681 | // DosEnterCritSec/DosExitCritSec is used to make sure the other threads in
|
---|
682 | // the application can't touch the pages before they are loaded from disk and
|
---|
683 | // fixups are applied.
|
---|
684 | //
|
---|
685 | // TODO:
|
---|
686 | // SECTION_PAGES: - don't load pages starting at access violation address, but
|
---|
687 | // a region surrounding it (e.g. -32k -> + 32k)
|
---|
688 | // this will prevent many pagefaults when the app uses
|
---|
689 | // pages with a lower addr.
|
---|
690 | //
|
---|
691 | //******************************************************************************
|
---|
692 |
|
---|
693 | #define DOSREAD_IDEAL_SIZE 61440
|
---|
694 | static inline APIRET _Optlink fastDosRead(HFILE hFile,
|
---|
695 | PVOID pAddress,
|
---|
696 | ULONG ulSize,
|
---|
697 | PULONG pulBytesRead)
|
---|
698 | {
|
---|
699 | /* we better break the DosRead into multiple calls */
|
---|
700 | PBYTE p = (PBYTE)pAddress;
|
---|
701 | ULONG ulReadBytes;
|
---|
702 | APIRET rc;
|
---|
703 |
|
---|
704 | *pulBytesRead = ulSize;
|
---|
705 |
|
---|
706 | do
|
---|
707 | {
|
---|
708 | rc = DosRead(hFile,
|
---|
709 | p,
|
---|
710 | min(DOSREAD_IDEAL_SIZE, ulSize),
|
---|
711 | &ulReadBytes);
|
---|
712 | if (rc != NO_ERROR)
|
---|
713 | {
|
---|
714 | /* in case of errors bail out */
|
---|
715 | *pulBytesRead = 0;
|
---|
716 | return rc;
|
---|
717 | }
|
---|
718 |
|
---|
719 | ulSize -= ulReadBytes;
|
---|
720 | p += ulReadBytes;
|
---|
721 | }
|
---|
722 | while (ulSize > 0);
|
---|
723 |
|
---|
724 | return NO_ERROR;
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | BOOL Win32PeLdrImage::commitPage(ULONG virtAddress, BOOL fWriteAccess, int fPageCmd)
|
---|
729 | {
|
---|
730 | Section *section;
|
---|
731 | ULONG offset, size, sectionsize, protflags, fileoffset, range, attr;
|
---|
732 | ULONG ulNewPos, ulRead, orgVirtAddress = virtAddress;
|
---|
733 | APIRET rc;
|
---|
734 |
|
---|
735 | //Round down to nearest page boundary
|
---|
736 | virtAddress = virtAddress & ~0xFFF;
|
---|
737 |
|
---|
738 | section = findSectionByOS2Addr(virtAddress);
|
---|
739 | if(section == NULL) {
|
---|
740 | section = findSectionByOS2Addr(orgVirtAddress);
|
---|
741 | if(section) {
|
---|
742 | virtAddress = orgVirtAddress;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | if(section == NULL) {
|
---|
746 | size = 4096;
|
---|
747 | sectionsize = 4096;
|
---|
748 | protflags = PAG_READ|PAG_WRITE; //readonly?
|
---|
749 | section = findPreviousSectionByOS2Addr(virtAddress);
|
---|
750 | if(section == NULL) {//access to header
|
---|
751 | offset = 0;
|
---|
752 | fileoffset = virtAddress - realBaseAddress;
|
---|
753 | }
|
---|
754 | else {
|
---|
755 | offset = virtAddress - (section->realvirtaddr + section->virtualsize);
|
---|
756 | fileoffset = section->rawoffset + section->rawsize + offset;
|
---|
757 | }
|
---|
758 | }
|
---|
759 | else {
|
---|
760 | protflags = section->pageflags;
|
---|
761 | offset = virtAddress - section->realvirtaddr;
|
---|
762 | sectionsize = section->virtualsize - offset;
|
---|
763 |
|
---|
764 | if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
|
---|
765 | //unintialized data (set to 0)
|
---|
766 | size = 0;
|
---|
767 | fileoffset = -1;
|
---|
768 | }
|
---|
769 | else {
|
---|
770 | size = section->rawsize-offset;
|
---|
771 | fileoffset = section->rawoffset + offset;
|
---|
772 | }
|
---|
773 | if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
|
---|
774 | dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
|
---|
775 | return FALSE;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | //Check range of pages with the same attributes starting at virtAddress
|
---|
779 | //(some pages might already have been loaded)
|
---|
780 | range = sectionsize;
|
---|
781 | rc = DosQueryMem((PVOID)virtAddress, &range, &attr);
|
---|
782 | if(rc) {
|
---|
783 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
|
---|
784 | return FALSE;
|
---|
785 | }
|
---|
786 | if(attr & PAG_COMMIT) {
|
---|
787 | dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
|
---|
788 | return FALSE;
|
---|
789 | }
|
---|
790 |
|
---|
791 | if(fPageCmd == SINGLE_PAGE) {
|
---|
792 | size = min(size, PAGE_SIZE);
|
---|
793 | sectionsize = min(sectionsize, PAGE_SIZE);
|
---|
794 | }
|
---|
795 | else
|
---|
796 | if(fPageCmd == SECTION_PAGES) {
|
---|
797 | size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
|
---|
798 | sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
|
---|
799 | }
|
---|
800 | //else complete section
|
---|
801 |
|
---|
802 | size = min(size, range);
|
---|
803 | sectionsize = min(sectionsize, range);
|
---|
804 |
|
---|
805 | if(fileoffset != -1) {
|
---|
806 | rc = DosEnterCritSec();
|
---|
807 | if(rc) {
|
---|
808 | dprintf((LOG, "DosEnterCritSec failed with rc %d", rc));
|
---|
809 | goto fail;
|
---|
810 | }
|
---|
811 | rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
812 | if(rc) {
|
---|
813 | DosExitCritSec();
|
---|
814 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
---|
815 | goto fail;
|
---|
816 | }
|
---|
817 |
|
---|
818 | if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
|
---|
819 | DosExitCritSec();
|
---|
820 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
|
---|
821 | goto fail;
|
---|
822 | }
|
---|
823 |
|
---|
824 | // 2001-05-31 PH
|
---|
825 | // ensure DosRead() does not have to read more
|
---|
826 | // than 65535 bytes, otherwise split into two requests!
|
---|
827 | rc = fastDosRead(hFile, (PVOID)virtAddress, size, &ulRead);
|
---|
828 | if(rc) {
|
---|
829 | DosExitCritSec();
|
---|
830 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x %x %x %x (rc=%d)!", virtAddress, size, ulRead, fileoffset, rc));
|
---|
831 | goto fail;
|
---|
832 | }
|
---|
833 | if(ulRead != size) {
|
---|
834 | DosExitCritSec();
|
---|
835 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
|
---|
836 | goto fail;
|
---|
837 | }
|
---|
838 | setFixups(virtAddress, sectionsize);
|
---|
839 |
|
---|
840 | rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
|
---|
841 | DosExitCritSec();
|
---|
842 | if(rc) {
|
---|
843 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
---|
844 | goto fail;
|
---|
845 | }
|
---|
846 | }
|
---|
847 | else {
|
---|
848 | rc = DosEnterCritSec();
|
---|
849 | if(rc) {
|
---|
850 | dprintf((LOG, "DosEnterCritSec failed with rc %d", rc));
|
---|
851 | goto fail;
|
---|
852 | }
|
---|
853 |
|
---|
854 | rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
855 | if(rc) {
|
---|
856 | DosExitCritSec();
|
---|
857 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
---|
858 | goto fail;
|
---|
859 | }
|
---|
860 | setFixups(virtAddress, sectionsize);
|
---|
861 |
|
---|
862 | rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
|
---|
863 | DosExitCritSec();
|
---|
864 | if(rc) {
|
---|
865 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
---|
866 | goto fail;
|
---|
867 | }
|
---|
868 | }
|
---|
869 | return TRUE;
|
---|
870 |
|
---|
871 | fail:
|
---|
872 | return FALSE;
|
---|
873 | }
|
---|
874 | //******************************************************************************
|
---|
875 | //******************************************************************************
|
---|
876 | void Win32PeLdrImage::addSection(ULONG type, ULONG rawoffset, ULONG rawsize, ULONG virtaddress, ULONG virtsize, ULONG flags)
|
---|
877 | {
|
---|
878 | virtsize = max(rawsize, virtsize);
|
---|
879 |
|
---|
880 | section[nrsections].rawoffset = rawoffset;
|
---|
881 | section[nrsections].type = type;
|
---|
882 | section[nrsections].rawsize = rawsize;
|
---|
883 | section[nrsections].virtaddr = virtaddress;
|
---|
884 | section[nrsections].flags = flags;
|
---|
885 |
|
---|
886 | virtsize = ((virtsize - 1) & ~0xFFF) + PAGE_SIZE;
|
---|
887 | imageSize += virtsize;
|
---|
888 | section[nrsections].virtualsize = virtsize;
|
---|
889 |
|
---|
890 | if(virtaddress < imageVirtBase)
|
---|
891 | imageVirtBase = virtaddress;
|
---|
892 | if(virtaddress + virtsize > imageVirtEnd)
|
---|
893 | imageVirtEnd = virtaddress + virtsize;
|
---|
894 |
|
---|
895 | nrsections++;
|
---|
896 | }
|
---|
897 | //******************************************************************************
|
---|
898 | //******************************************************************************
|
---|
899 | BOOL Win32PeLdrImage::allocSections(ULONG reservedMem)
|
---|
900 | {
|
---|
901 | APIRET rc;
|
---|
902 | ULONG baseAddress;
|
---|
903 |
|
---|
904 | realBaseAddress = 0;
|
---|
905 |
|
---|
906 | //Allocated in by pe.exe
|
---|
907 | if(reservedMem && reservedMem == oh.ImageBase) {
|
---|
908 | realBaseAddress = oh.ImageBase;
|
---|
909 | return TRUE;
|
---|
910 | }
|
---|
911 |
|
---|
912 | //SvL: We don't care where the image is loaded for resource lookup
|
---|
913 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
|
---|
914 | return allocFixedMem(reservedMem);
|
---|
915 | }
|
---|
916 | rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
|
---|
917 | if(rc) {
|
---|
918 | dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
|
---|
919 | errorState = rc;
|
---|
920 | return(FALSE);
|
---|
921 | }
|
---|
922 | realBaseAddress = baseAddress;
|
---|
923 | return(TRUE);
|
---|
924 | }
|
---|
925 | //******************************************************************************
|
---|
926 | //******************************************************************************
|
---|
927 | Section *Win32PeLdrImage::findSection(ULONG type)
|
---|
928 | {
|
---|
929 | for(int i=0;i<nrsections;i++) {
|
---|
930 | if(section[i].type == type) {
|
---|
931 | return §ion[i];
|
---|
932 | }
|
---|
933 | }
|
---|
934 | return NULL;
|
---|
935 | }
|
---|
936 | //******************************************************************************
|
---|
937 | //******************************************************************************
|
---|
938 | Section *Win32PeLdrImage::findSectionByAddr(ULONG addr)
|
---|
939 | {
|
---|
940 | for(int i=0;i<nrsections;i++) {
|
---|
941 | if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
|
---|
942 | return §ion[i];
|
---|
943 | }
|
---|
944 | }
|
---|
945 | return NULL;
|
---|
946 | }
|
---|
947 | //******************************************************************************
|
---|
948 | //******************************************************************************
|
---|
949 | Section *Win32PeLdrImage::findSectionByOS2Addr(ULONG addr)
|
---|
950 | {
|
---|
951 | for(int i=0;i<nrsections;i++) {
|
---|
952 | if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
|
---|
953 | return §ion[i];
|
---|
954 | }
|
---|
955 | }
|
---|
956 | return NULL;
|
---|
957 | }
|
---|
958 | //******************************************************************************
|
---|
959 | //******************************************************************************
|
---|
960 | Section *Win32PeLdrImage::findPreviousSectionByOS2Addr(ULONG addr)
|
---|
961 | {
|
---|
962 | ULONG lowestAddr = 0xffffffff;
|
---|
963 | ULONG index = -1;
|
---|
964 |
|
---|
965 | for(int i=0;i<nrsections;i++) {
|
---|
966 | if(section[i].realvirtaddr > addr) {
|
---|
967 | if(section[i].realvirtaddr < lowestAddr) {
|
---|
968 | lowestAddr = section[i].realvirtaddr;
|
---|
969 | index = i;
|
---|
970 | }
|
---|
971 | }
|
---|
972 | }
|
---|
973 | if(index == -1)
|
---|
974 | return NULL;
|
---|
975 |
|
---|
976 | return §ion[index];
|
---|
977 | }
|
---|
978 | //******************************************************************************
|
---|
979 | #define FALLOC_SIZE (1024*1024)
|
---|
980 | //NOTE: Needs testing (while loop)
|
---|
981 | //TODO: Free unused (parts of) reservedMem
|
---|
982 | //******************************************************************************
|
---|
983 | BOOL Win32PeLdrImage::allocFixedMem(ULONG reservedMem)
|
---|
984 | {
|
---|
985 | ULONG address = 0;
|
---|
986 | ULONG *memallocs;
|
---|
987 | ULONG alloccnt = 0;
|
---|
988 | ULONG diff, i, baseAddress;
|
---|
989 | APIRET rc;
|
---|
990 | BOOL allocFlags = flAllocMem;
|
---|
991 |
|
---|
992 | //Reserve enough space to store 4096 pointers to 1MB memory chunks
|
---|
993 | memallocs = (ULONG *)malloc(4096*sizeof(ULONG *));
|
---|
994 | if(memallocs == NULL) {
|
---|
995 | dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
|
---|
996 | return FALSE;
|
---|
997 | }
|
---|
998 |
|
---|
999 | if(oh.ImageBase < 512*1024*1024) {
|
---|
1000 | allocFlags = 0;
|
---|
1001 | }
|
---|
1002 | while(TRUE) {
|
---|
1003 | rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
|
---|
1004 | if(rc) break;
|
---|
1005 |
|
---|
1006 | dprintf((LOG, "DosAllocMem returned %x", address ));
|
---|
1007 | if(address + FALLOC_SIZE >= oh.ImageBase) {
|
---|
1008 | if(address > oh.ImageBase) {//we've passed it!
|
---|
1009 | DosFreeMem((PVOID)address);
|
---|
1010 | break;
|
---|
1011 | }
|
---|
1012 | //found the right address
|
---|
1013 | DosFreeMem((PVOID)address);
|
---|
1014 |
|
---|
1015 | diff = oh.ImageBase - address;
|
---|
1016 | if(diff) {
|
---|
1017 | rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
|
---|
1018 | if(rc) break;
|
---|
1019 | }
|
---|
1020 | rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
|
---|
1021 | if(rc) break;
|
---|
1022 |
|
---|
1023 | if(diff) DosFreeMem((PVOID)address);
|
---|
1024 |
|
---|
1025 | realBaseAddress = baseAddress;
|
---|
1026 | break;
|
---|
1027 | }
|
---|
1028 | memallocs[alloccnt++] = address;
|
---|
1029 | }
|
---|
1030 | for(i=0;i<alloccnt;i++) {
|
---|
1031 | DosFreeMem((PVOID)memallocs[i]);
|
---|
1032 | }
|
---|
1033 | free(memallocs);
|
---|
1034 |
|
---|
1035 | if(realBaseAddress == 0) //Let me guess.. MS Office app?
|
---|
1036 | return(FALSE);
|
---|
1037 |
|
---|
1038 | return(TRUE);
|
---|
1039 | }
|
---|
1040 | //******************************************************************************
|
---|
1041 | //******************************************************************************
|
---|
1042 | BOOL Win32PeLdrImage::setMemFlags()
|
---|
1043 | {
|
---|
1044 | int i;
|
---|
1045 | WINIMAGE_LOOKUP *imgLookup;
|
---|
1046 |
|
---|
1047 | imgLookup = WINIMAGE_LOOKUPADDR(realBaseAddress);
|
---|
1048 | imgLookup->magic1 = MAGIC_WINIMAGE;
|
---|
1049 | imgLookup->image = this;
|
---|
1050 | imgLookup->magic2 = MAGIC_WINIMAGE;
|
---|
1051 |
|
---|
1052 | // Process all the image sections
|
---|
1053 | for(i=0;i<nrsections;i++) {
|
---|
1054 | section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | for(i=0;i<nrsections;i++) {
|
---|
1058 | switch(section[i].type)
|
---|
1059 | {
|
---|
1060 | case SECTION_CODE:
|
---|
1061 | case (SECTION_CODE | SECTION_IMPORT):
|
---|
1062 | section[i].pageflags = PAG_EXECUTE | PAG_READ;
|
---|
1063 | if(section[i].flags & IMAGE_SCN_MEM_WRITE)
|
---|
1064 | section[i].pageflags |= PAG_WRITE;
|
---|
1065 | break;
|
---|
1066 | case SECTION_INITDATA:
|
---|
1067 | case SECTION_UNINITDATA:
|
---|
1068 | case SECTION_IMPORT:
|
---|
1069 | case SECTION_TLS:
|
---|
1070 | section[i].pageflags = PAG_WRITE | PAG_READ;
|
---|
1071 | break;
|
---|
1072 |
|
---|
1073 | case SECTION_RESOURCE:
|
---|
1074 | //TODO: GDI32 changes some bitmap structures to avoid problems in Open32
|
---|
1075 | // -> causes crashes if resource section is readonly
|
---|
1076 | // -> make it readonly again when gdi32 has been rewritten
|
---|
1077 | section[i].pageflags = PAG_WRITE | PAG_READ;
|
---|
1078 | break;
|
---|
1079 |
|
---|
1080 | case SECTION_READONLYDATA:
|
---|
1081 | case SECTION_EXPORT:
|
---|
1082 | default:
|
---|
1083 | section[i].pageflags = PAG_READ;
|
---|
1084 | break;
|
---|
1085 | }
|
---|
1086 | if(section[i].flags & (IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
|
---|
1087 | //SvL: sometimes i.e. import/export sections also contain data
|
---|
1088 | // must make them read/write
|
---|
1089 | section[i].pageflags = PAG_WRITE;
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | return(TRUE);
|
---|
1093 | }
|
---|
1094 | //******************************************************************************
|
---|
1095 | //******************************************************************************
|
---|
1096 | BOOL Win32PeLdrImage::setFixups(ULONG virtAddress, ULONG size)
|
---|
1097 | {
|
---|
1098 | int i, j;
|
---|
1099 | char *page;
|
---|
1100 | ULONG count, newpage;
|
---|
1101 | Section *section;
|
---|
1102 | PIMAGE_BASE_RELOCATION prel = pFixups;
|
---|
1103 |
|
---|
1104 | if(realBaseAddress == oh.ImageBase || fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
---|
1105 | return(TRUE);
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | virtAddress -= realBaseAddress;
|
---|
1109 | //round size to next page boundary
|
---|
1110 | size = (size-1) & ~0xFFF;
|
---|
1111 | size += PAGE_SIZE;
|
---|
1112 |
|
---|
1113 | if(prel) {
|
---|
1114 | j = 1;
|
---|
1115 | while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
|
---|
1116 | prel->VirtualAddress && prel->VirtualAddress < virtAddress)
|
---|
1117 | {
|
---|
1118 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
---|
1119 | }
|
---|
1120 | while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
|
---|
1121 | prel->VirtualAddress && prel->VirtualAddress < virtAddress + size)
|
---|
1122 | {
|
---|
1123 | page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
|
---|
1124 | count = (prel->SizeOfBlock - 8)/2;
|
---|
1125 | j++;
|
---|
1126 | for(i=0;i<count;i++) {
|
---|
1127 | int type = prel->TypeOffset[i] >> 12;
|
---|
1128 | int offset = prel->TypeOffset[i] & 0xFFF;
|
---|
1129 | int fixupsize = 0;
|
---|
1130 |
|
---|
1131 | switch(type)
|
---|
1132 | {
|
---|
1133 | case IMAGE_REL_BASED_HIGHLOW:
|
---|
1134 | fixupsize = 4;
|
---|
1135 | break;
|
---|
1136 | case IMAGE_REL_BASED_HIGH:
|
---|
1137 | case IMAGE_REL_BASED_LOW:
|
---|
1138 | fixupsize = 2;
|
---|
1139 | break;
|
---|
1140 | }
|
---|
1141 | //If the fixup crosses the final page boundary,
|
---|
1142 | //then we have to load another page
|
---|
1143 | if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
|
---|
1144 | {
|
---|
1145 | newpage = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
|
---|
1146 | newpage &= ~0xFFF;
|
---|
1147 |
|
---|
1148 | section = findSectionByOS2Addr(newpage);
|
---|
1149 | if(section == NULL) {
|
---|
1150 | //should never happen
|
---|
1151 | dprintf((LOG, "::setFixups -> section == NULL!!"));
|
---|
1152 | return FALSE;
|
---|
1153 | }
|
---|
1154 | //SvL: Read page from disk
|
---|
1155 | commitPage(newpage, FALSE, SINGLE_PAGE);
|
---|
1156 |
|
---|
1157 | //SvL: Enable write access (TODO: may need to prevent other threads from being active)
|
---|
1158 | DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | switch(type)
|
---|
1162 | {
|
---|
1163 | case IMAGE_REL_BASED_ABSOLUTE:
|
---|
1164 | break; //skip
|
---|
1165 | case IMAGE_REL_BASED_HIGHLOW:
|
---|
1166 | AddOff32Fixup(prel->VirtualAddress + offset);
|
---|
1167 | break;
|
---|
1168 | case IMAGE_REL_BASED_HIGH:
|
---|
1169 | AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
|
---|
1170 | break;
|
---|
1171 | case IMAGE_REL_BASED_LOW:
|
---|
1172 | AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
|
---|
1173 | break;
|
---|
1174 | case IMAGE_REL_BASED_HIGHADJ:
|
---|
1175 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
---|
1176 | default:
|
---|
1177 | break;
|
---|
1178 | }
|
---|
1179 | if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
|
---|
1180 | {
|
---|
1181 | //SvL: Restore original page protection flags (TODO: may need to prevent other threads from being active)
|
---|
1182 | DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
---|
1186 | }//while
|
---|
1187 | }
|
---|
1188 | else {
|
---|
1189 | dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
|
---|
1190 | return(FALSE);
|
---|
1191 | }
|
---|
1192 | return(TRUE);
|
---|
1193 | }
|
---|
1194 | //******************************************************************************
|
---|
1195 | //******************************************************************************
|
---|
1196 | BOOL Win32PeLdrImage::setFixups(PIMAGE_BASE_RELOCATION prel)
|
---|
1197 | {
|
---|
1198 | int i, j;
|
---|
1199 | char *page;
|
---|
1200 | ULONG count;
|
---|
1201 |
|
---|
1202 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
---|
1203 | return(TRUE);
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | if(prel) {
|
---|
1207 | j = 1;
|
---|
1208 | while(prel->VirtualAddress) {
|
---|
1209 | page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
|
---|
1210 | count = (prel->SizeOfBlock - 8)/2;
|
---|
1211 | dprintf((LOG, "Page %d Address %x Count %d", j, prel->VirtualAddress, count ));
|
---|
1212 | j++;
|
---|
1213 | for(i=0;i<count;i++) {
|
---|
1214 | int type = prel->TypeOffset[i] >> 12;
|
---|
1215 | int offset = prel->TypeOffset[i] & 0xFFF;
|
---|
1216 | switch(type) {
|
---|
1217 | case IMAGE_REL_BASED_ABSOLUTE:
|
---|
1218 | //// dprintf((LOG, "absolute fixup; unused" ));
|
---|
1219 | break; //skip
|
---|
1220 | case IMAGE_REL_BASED_HIGHLOW:
|
---|
1221 | //// dprintf((LOG, "address ", offset << " type ", type ));
|
---|
1222 | AddOff32Fixup(prel->VirtualAddress + offset);
|
---|
1223 | break;
|
---|
1224 | case IMAGE_REL_BASED_HIGH:
|
---|
1225 | AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
|
---|
1226 | break;
|
---|
1227 | case IMAGE_REL_BASED_LOW:
|
---|
1228 | AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
|
---|
1229 | break;
|
---|
1230 | case IMAGE_REL_BASED_HIGHADJ:
|
---|
1231 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
---|
1232 | default:
|
---|
1233 | dprintf((LOG, "Unknown/unsupported fixup type!" ));
|
---|
1234 | break;
|
---|
1235 | }
|
---|
1236 | }
|
---|
1237 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
---|
1238 | }//while
|
---|
1239 | }
|
---|
1240 | else {
|
---|
1241 | dprintf((LOG, "No internal fixups found!" ));
|
---|
1242 | errorState = ERROR_INTERNAL;
|
---|
1243 | return(FALSE);
|
---|
1244 | }
|
---|
1245 | return(TRUE);
|
---|
1246 | }
|
---|
1247 | //******************************************************************************
|
---|
1248 | //******************************************************************************
|
---|
1249 | void Win32PeLdrImage::AddOff32Fixup(ULONG fixupaddr)
|
---|
1250 | {
|
---|
1251 | ULONG orgaddr;
|
---|
1252 | ULONG *fixup;
|
---|
1253 |
|
---|
1254 | fixup = (ULONG *)(fixupaddr + realBaseAddress);
|
---|
1255 | orgaddr = *fixup;
|
---|
1256 | // dprintf((LOG, "AddOff32Fixup 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, realBaseAddress + (*fixup - oh.ImageBase)));
|
---|
1257 | *fixup = realBaseAddress + (*fixup - oh.ImageBase);
|
---|
1258 | }
|
---|
1259 | //******************************************************************************
|
---|
1260 | //******************************************************************************
|
---|
1261 | void Win32PeLdrImage::AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup)
|
---|
1262 | {
|
---|
1263 | ULONG orgaddr;
|
---|
1264 | USHORT *fixup;
|
---|
1265 |
|
---|
1266 | fixup = (USHORT *)(fixupaddr + realBaseAddress);
|
---|
1267 | orgaddr = *fixup;
|
---|
1268 | if(fHighFixup) {
|
---|
1269 | *fixup += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
|
---|
1270 | // dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
|
---|
1271 | }
|
---|
1272 | else {
|
---|
1273 | *fixup += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
|
---|
1274 | // dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 | //******************************************************************************
|
---|
1278 | //******************************************************************************
|
---|
1279 | void Win32PeLdrImage::StoreImportByOrd(Win32ImageBase *WinImage, ULONG ordinal, ULONG impaddr)
|
---|
1280 | {
|
---|
1281 | ULONG *import;
|
---|
1282 | ULONG apiaddr;
|
---|
1283 |
|
---|
1284 | import = (ULONG *)impaddr;
|
---|
1285 | apiaddr = WinImage->getApi(ordinal);
|
---|
1286 | if(apiaddr == 0)
|
---|
1287 | {
|
---|
1288 | dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
|
---|
1289 | WinImage->getModuleName(),
|
---|
1290 | ordinal));
|
---|
1291 |
|
---|
1292 | dprintf((LOG, "--->>> NOT FOUND!" ));
|
---|
1293 | *import = (ULONG)MissingApi;
|
---|
1294 | }
|
---|
1295 | else *import = apiaddr;
|
---|
1296 | }
|
---|
1297 | //******************************************************************************
|
---|
1298 | //******************************************************************************
|
---|
1299 | void Win32PeLdrImage::StoreImportByName(Win32ImageBase *WinImage, char *impname, ULONG impaddr)
|
---|
1300 | {
|
---|
1301 | ULONG *import;
|
---|
1302 | ULONG apiaddr;
|
---|
1303 |
|
---|
1304 | import = (ULONG *)impaddr;
|
---|
1305 | apiaddr = WinImage->getApi(impname);
|
---|
1306 | if(apiaddr == 0)
|
---|
1307 | {
|
---|
1308 | dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
|
---|
1309 | WinImage->getModuleName(),
|
---|
1310 | impname));
|
---|
1311 |
|
---|
1312 | dprintf((LOG, "--->>> NOT FOUND!" ));
|
---|
1313 | *import = (ULONG)MissingApi;
|
---|
1314 | }
|
---|
1315 | else *import = apiaddr;
|
---|
1316 | }
|
---|
1317 | //******************************************************************************
|
---|
1318 | //******************************************************************************
|
---|
1319 | BOOL Win32PeLdrImage::processExports(char *win32file)
|
---|
1320 | {
|
---|
1321 | IMAGE_SECTION_HEADER sh;
|
---|
1322 | PIMAGE_EXPORT_DIRECTORY ped;
|
---|
1323 | ULONG *ptrNames, *ptrAddress;
|
---|
1324 | USHORT *ptrOrd;
|
---|
1325 | BOOL fForwarder;
|
---|
1326 | int i;
|
---|
1327 |
|
---|
1328 | /* get section header and pointer to data directory for .edata section */
|
---|
1329 | if((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
|
---|
1330 | (win32file, IMAGE_DIRECTORY_ENTRY_EXPORT)) != NULL &&
|
---|
1331 | GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_EXPORT, &sh) )
|
---|
1332 | {
|
---|
1333 |
|
---|
1334 | dprintf((LOG, "Exported Functions: " ));
|
---|
1335 | ptrOrd = (USHORT *)((ULONG)ped->AddressOfNameOrdinals +
|
---|
1336 | (ULONG)win32file);
|
---|
1337 | ptrNames = (ULONG *)((ULONG)ped->AddressOfNames +
|
---|
1338 | (ULONG)win32file);
|
---|
1339 | ptrAddress = (ULONG *)((ULONG)ped->AddressOfFunctions +
|
---|
1340 | (ULONG)win32file);
|
---|
1341 | nrOrdExports = ped->NumberOfFunctions;
|
---|
1342 | nrNameExports = ped->NumberOfNames;
|
---|
1343 |
|
---|
1344 | int ord, RVAExport;
|
---|
1345 | char *name;
|
---|
1346 | for(i=0;i<ped->NumberOfNames;i++)
|
---|
1347 | {
|
---|
1348 | fForwarder = FALSE;
|
---|
1349 | ord = ptrOrd[i] + ped->Base;
|
---|
1350 | name = (char *)((ULONG)ptrNames[i] + (ULONG)win32file);
|
---|
1351 | RVAExport = ptrAddress[ptrOrd[i]];
|
---|
1352 |
|
---|
1353 | /* forwarder? ulRVA within export directory. */
|
---|
1354 | if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
|
---|
1355 | RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
---|
1356 | + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
---|
1357 | {
|
---|
1358 | fForwarder = AddForwarder(oh.ImageBase + RVAExport, name, ord);
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | if(!fForwarder)
|
---|
1362 | {
|
---|
1363 | //points to code (virtual address relative to oh.ImageBase
|
---|
1364 | AddNameExport(oh.ImageBase + RVAExport, name, ord);
|
---|
1365 | dprintf((LOG, "address 0x%x %s @%d (0x%08x)", RVAExport, name, ord, realBaseAddress + RVAExport));
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++)
|
---|
1370 | {
|
---|
1371 | fForwarder = FALSE;
|
---|
1372 | ord = ped->Base + i; //Correct??
|
---|
1373 | RVAExport = ptrAddress[i];
|
---|
1374 | /* forwarder? ulRVA within export directory. */
|
---|
1375 | if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
|
---|
1376 | RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
---|
1377 | + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
---|
1378 | {
|
---|
1379 | fForwarder = AddForwarder(oh.ImageBase + RVAExport, NULL, ord);
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | if(!fForwarder && RVAExport)
|
---|
1383 | {
|
---|
1384 | //points to code (virtual address relative to oh.ImageBase
|
---|
1385 | dprintf((LOG, "ord %d at 0x%08x (0x%08x)", ord, RVAExport, realBaseAddress + RVAExport));
|
---|
1386 | AddOrdExport(oh.ImageBase + RVAExport, ord);
|
---|
1387 | }
|
---|
1388 | }
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | return(TRUE);
|
---|
1392 | }
|
---|
1393 | //******************************************************************************
|
---|
1394 | //******************************************************************************
|
---|
1395 | void Win32PeLdrImage::AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal, BOOL fAbsoluteAddress)
|
---|
1396 | {
|
---|
1397 | ULONG nsize;
|
---|
1398 | int iApiNameLength = strlen(apiname);
|
---|
1399 |
|
---|
1400 | if(nameexports == NULL)
|
---|
1401 | {
|
---|
1402 | nameExportSize= 4096;
|
---|
1403 | nameexports = (NameExport *)malloc(nameExportSize);
|
---|
1404 | curnameexport = nameexports;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | nsize = (ULONG)curnameexport - (ULONG)nameexports;
|
---|
1408 | if(nsize + sizeof(NameExport) + iApiNameLength > nameExportSize)
|
---|
1409 | {
|
---|
1410 | nameExportSize += 4096;
|
---|
1411 | char *tmp = (char *)nameexports;
|
---|
1412 | nameexports = (NameExport *)malloc(nameExportSize);
|
---|
1413 | memcpy(nameexports, tmp, nsize);
|
---|
1414 | curnameexport = (NameExport *)((ULONG)nameexports + nsize);
|
---|
1415 | free(tmp);
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | if(fAbsoluteAddress) //forwarders use absolute address
|
---|
1419 | curnameexport->virtaddr = virtaddr;
|
---|
1420 | else
|
---|
1421 | curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
|
---|
1422 |
|
---|
1423 | curnameexport->ordinal = ordinal;
|
---|
1424 | *(ULONG *)curnameexport->name = 0;
|
---|
1425 | strcpy(curnameexport->name, apiname);
|
---|
1426 |
|
---|
1427 | curnameexport->nlength = iApiNameLength + 1;
|
---|
1428 | if(curnameexport->nlength < sizeof(curnameexport->name))
|
---|
1429 | curnameexport->nlength = sizeof(curnameexport->name);
|
---|
1430 |
|
---|
1431 | curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
|
---|
1432 | }
|
---|
1433 | //******************************************************************************
|
---|
1434 | //******************************************************************************
|
---|
1435 | void Win32PeLdrImage::AddOrdExport(ULONG virtaddr,
|
---|
1436 | ULONG ordinal,
|
---|
1437 | BOOL fAbsoluteAddress)
|
---|
1438 | {
|
---|
1439 | if(ordexports == NULL)
|
---|
1440 | {
|
---|
1441 | ordexports = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
|
---|
1442 | curordexport = ordexports;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | if(fAbsoluteAddress) //forwarders use absolute address
|
---|
1446 | curordexport->virtaddr = virtaddr;
|
---|
1447 | else
|
---|
1448 | curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
|
---|
1449 |
|
---|
1450 | curordexport->ordinal = ordinal;
|
---|
1451 | curordexport++;
|
---|
1452 | }
|
---|
1453 | //******************************************************************************
|
---|
1454 | //******************************************************************************
|
---|
1455 | BOOL Win32PeLdrImage::AddForwarder(ULONG virtaddr,
|
---|
1456 | char *apiname,
|
---|
1457 | ULONG ordinal)
|
---|
1458 | {
|
---|
1459 | char *forward = (char *)(realBaseAddress + (virtaddr - oh.ImageBase));
|
---|
1460 | char *forwarddll, *forwardapi, *forwardapi0;
|
---|
1461 | Win32DllBase *WinDll;
|
---|
1462 | DWORD exportaddr;
|
---|
1463 | int forwardord;
|
---|
1464 | int iForwardApiLength; /* save strlen result */
|
---|
1465 |
|
---|
1466 | // 2001-06-01 PH
|
---|
1467 | // we do exactly know which character we replace: the "." is zeroed
|
---|
1468 | // so we can cheaply restore the name at the end of the method.
|
---|
1469 | // forwarddll = strdup(forward);
|
---|
1470 | // if(forwarddll == NULL)
|
---|
1471 | // {
|
---|
1472 | // return FALSE;
|
---|
1473 | // }
|
---|
1474 | forwarddll = forward;
|
---|
1475 |
|
---|
1476 | forwardapi = strchr(forwarddll, '.');
|
---|
1477 | if(forwardapi == NULL)
|
---|
1478 | {
|
---|
1479 | goto fail;
|
---|
1480 | }
|
---|
1481 | *forwardapi0 = 0;
|
---|
1482 | forwardapi=forwardapi0++;
|
---|
1483 | iForwardApiLength = strlen(forwardapi);
|
---|
1484 |
|
---|
1485 | if(strlen(forwarddll) == 0 || iForwardApiLength == 0)
|
---|
1486 | {
|
---|
1487 | goto fail;
|
---|
1488 | }
|
---|
1489 |
|
---|
1490 | WinDll = Win32DllBase::findModule(forwarddll);
|
---|
1491 | if(WinDll == NULL)
|
---|
1492 | {
|
---|
1493 | WinDll = loadDll(forwarddll);
|
---|
1494 | if(WinDll == NULL)
|
---|
1495 | {
|
---|
1496 | dprintf((LOG, "ERROR: couldn't find forwarder %s.%s", forwarddll, forwardapi));
|
---|
1497 | goto fail;
|
---|
1498 | }
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | //check if name or ordinal forwarder
|
---|
1502 | if(*forwardapi >= '0' && *forwardapi <= '9')
|
---|
1503 | {
|
---|
1504 | forwardord = atoi(forwardapi);
|
---|
1505 | }
|
---|
1506 | else
|
---|
1507 | forwardord = 0;
|
---|
1508 |
|
---|
1509 | if(forwardord != 0 || (iForwardApiLength == 1 && *forwardapi == '0'))
|
---|
1510 | exportaddr = WinDll->getApi(forwardord);
|
---|
1511 | else
|
---|
1512 | exportaddr = WinDll->getApi(forwardapi);
|
---|
1513 |
|
---|
1514 | if(apiname)
|
---|
1515 | {
|
---|
1516 | dprintf((LOG, "address 0x%x %s @%d (0x%08x) forwarder %s.%s", virtaddr - oh.ImageBase, apiname, ordinal, virtaddr, forwarddll, forwardapi));
|
---|
1517 | AddNameExport(exportaddr, apiname, ordinal, TRUE);
|
---|
1518 | }
|
---|
1519 | else
|
---|
1520 | {
|
---|
1521 | dprintf((LOG, "address 0x%x @%d (0x%08x) forwarder %s.%s", virtaddr - oh.ImageBase, ordinal, virtaddr, forwarddll, forwardapi));
|
---|
1522 | AddOrdExport(exportaddr, ordinal, TRUE);
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | // free(forwarddll);
|
---|
1526 | *forwardapi0 = '.';
|
---|
1527 | return TRUE;
|
---|
1528 |
|
---|
1529 | fail:
|
---|
1530 | // free(forwarddll);
|
---|
1531 | *forwardapi0 = '.';
|
---|
1532 | return FALSE;
|
---|
1533 | }
|
---|
1534 | //******************************************************************************
|
---|
1535 | //******************************************************************************
|
---|
1536 | Win32DllBase *Win32PeLdrImage::loadDll(char *pszCurModule)
|
---|
1537 | {
|
---|
1538 | Win32DllBase *WinDll = NULL;
|
---|
1539 | char modname[CCHMAXPATH];
|
---|
1540 |
|
---|
1541 | strcpy(modname, pszCurModule);
|
---|
1542 |
|
---|
1543 | //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
|
---|
1544 | Win32DllBase::renameDll(modname);
|
---|
1545 |
|
---|
1546 | char szModName2[CCHMAXPATH];
|
---|
1547 | strcpy(szModName2, modname);
|
---|
1548 | if (!Win32ImageBase::findDll(szModName2, modname, sizeof(modname)))
|
---|
1549 | {
|
---|
1550 | dprintf((LOG, "Module %s not found!", modname));
|
---|
1551 | sprintf(szErrorModule, "%s", modname);
|
---|
1552 | errorState = 2;
|
---|
1553 | return NULL;
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | if(isPEImage(modname, NULL) != ERROR_SUCCESS_W)
|
---|
1557 | {//LX image, so let OS/2 do all the work for us
|
---|
1558 | APIRET rc;
|
---|
1559 | char szModuleFailure[CCHMAXPATH] = "";
|
---|
1560 | ULONG hInstanceNewDll;
|
---|
1561 | Win32LxDll *lxdll;
|
---|
1562 |
|
---|
1563 | char *dot = strchr(modname, '.');
|
---|
1564 | if(dot == NULL) {
|
---|
1565 | strcat(modname, DLL_EXTENSION);
|
---|
1566 | }
|
---|
1567 | rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
|
---|
1568 | if(rc) {
|
---|
1569 | dprintf((LOG, "DosLoadModule returned %X for %s", rc, szModuleFailure));
|
---|
1570 | sprintf(szErrorModule, "%s", szModuleFailure);
|
---|
1571 | errorState = rc;
|
---|
1572 | return NULL;
|
---|
1573 | }
|
---|
1574 | lxdll = Win32LxDll::findModuleByOS2Handle(hInstanceNewDll);
|
---|
1575 | if(lxdll == NULL) {//shouldn't happen!
|
---|
1576 | dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
|
---|
1577 | errorState = ERROR_INTERNAL;
|
---|
1578 | return NULL;
|
---|
1579 | }
|
---|
1580 | lxdll->setDllHandleOS2(hInstanceNewDll);
|
---|
1581 | if(lxdll->AddRef() == -1) {//-1 -> load failed (attachProcess)
|
---|
1582 | dprintf((LOG, "Dll %s refused to be loaded; aborting", modname));
|
---|
1583 | delete lxdll;
|
---|
1584 | errorState = ERROR_INTERNAL;
|
---|
1585 | return NULL;
|
---|
1586 | }
|
---|
1587 | WinDll = (Win32DllBase*)lxdll;
|
---|
1588 | }
|
---|
1589 | else {
|
---|
1590 | Win32PeLdrDll *pedll;
|
---|
1591 |
|
---|
1592 | pedll = new Win32PeLdrDll(modname, this);
|
---|
1593 | if(pedll == NULL) {
|
---|
1594 | dprintf((LOG, "pedll: Error allocating memory" ));
|
---|
1595 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
1596 | errorState = ERROR_INTERNAL;
|
---|
1597 | return NULL;
|
---|
1598 | }
|
---|
1599 | dprintf((LOG, "**********************************************************************" ));
|
---|
1600 | dprintf((LOG, "********************** Loading Module *********************" ));
|
---|
1601 | dprintf((LOG, "**********************************************************************" ));
|
---|
1602 | if(pedll->init(0) == FALSE) {
|
---|
1603 | dprintf((LOG, "Internal WinDll error ", pedll->getError() ));
|
---|
1604 | delete pedll;
|
---|
1605 | return NULL;
|
---|
1606 | }
|
---|
1607 | #ifdef DEBUG
|
---|
1608 | pedll->AddRef(getModuleName());
|
---|
1609 | #else
|
---|
1610 | pedll->AddRef();
|
---|
1611 | #endif
|
---|
1612 | if(pedll->attachProcess() == FALSE) {
|
---|
1613 | dprintf((LOG, "attachProcess failed!" ));
|
---|
1614 | delete pedll;
|
---|
1615 | errorState = ERROR_INTERNAL;
|
---|
1616 | return NULL;
|
---|
1617 | }
|
---|
1618 | WinDll = (Win32DllBase*)pedll;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | dprintf((LOG, "**********************************************************************" ));
|
---|
1622 | dprintf((LOG, "********************** Finished Loading Module %s ", modname ));
|
---|
1623 | dprintf((LOG, "**********************************************************************" ));
|
---|
1624 |
|
---|
1625 |
|
---|
1626 | return WinDll;
|
---|
1627 | }
|
---|
1628 | //******************************************************************************
|
---|
1629 | /** All initial processing of imports is done here
|
---|
1630 | * Should now detect most Borland styled files including the GifCon32.exe and
|
---|
1631 | * loader32 from SoftIce. (Stupid Borland!!!)
|
---|
1632 | *
|
---|
1633 | * knut [Jul 22 1998 2:44am]
|
---|
1634 | **/
|
---|
1635 | //******************************************************************************
|
---|
1636 | BOOL Win32PeLdrImage::processImports(char *win32file)
|
---|
1637 | {
|
---|
1638 | PIMAGE_IMPORT_DESCRIPTOR pID;
|
---|
1639 | IMAGE_SECTION_HEADER shID;
|
---|
1640 | IMAGE_SECTION_HEADER shExtra = {0};
|
---|
1641 | PIMAGE_OPTIONAL_HEADER pOH;
|
---|
1642 | int i,j, nrPages;
|
---|
1643 | BOOL fBorland = 0;
|
---|
1644 | int cModules;
|
---|
1645 | char *pszModules;
|
---|
1646 | char *pszCurModule;
|
---|
1647 | char *pszTmp;
|
---|
1648 | ULONG *pulImport;
|
---|
1649 | ULONG ulCurFixup;
|
---|
1650 | int Size;
|
---|
1651 | Win32DllBase *WinDll;
|
---|
1652 | Win32ImageBase *WinImage = NULL;
|
---|
1653 | Section *section;
|
---|
1654 |
|
---|
1655 | /* "algorithm:"
|
---|
1656 | * 1) get module names and store them
|
---|
1657 | * a) check dwRVAModuleName is within .idata seg - if not find section
|
---|
1658 | * 2) iterate thru functions of each module
|
---|
1659 | * a) check OriginalFirstThunk is not 0 and that it points to a RVA.
|
---|
1660 | * b) if not a) borland-styled PE-file - ARG!!!
|
---|
1661 | * check FirstThunk
|
---|
1662 | * c) check OriginalFirstThunk/FirstThunk ok RVAs and find right section
|
---|
1663 | * d) store ordinal/name import
|
---|
1664 | * 3) finished
|
---|
1665 | */
|
---|
1666 |
|
---|
1667 | /* 1) get module names */
|
---|
1668 | pID = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT);
|
---|
1669 | if (pID == NULL)
|
---|
1670 | return TRUE;
|
---|
1671 | if (!GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT, &shID))
|
---|
1672 | return TRUE;
|
---|
1673 |
|
---|
1674 | //calc size of module list
|
---|
1675 | i = Size = cModules = 0;
|
---|
1676 | while (pID[i].Name != 0)
|
---|
1677 | {
|
---|
1678 | //test RVA inside ID-Section
|
---|
1679 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
|
---|
1680 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
---|
1681 | }
|
---|
1682 | else {
|
---|
1683 | //is the "Extra"-section already found or do we have to find it?
|
---|
1684 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData)) {
|
---|
1685 | if (!GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name))
|
---|
1686 | return FALSE;
|
---|
1687 | }
|
---|
1688 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
---|
1689 | }
|
---|
1690 | Size += strlen(pszTmp) + 1;
|
---|
1691 | i++;
|
---|
1692 | cModules++;
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | pszModules = (char*)malloc(Size);
|
---|
1696 | assert(pszModules != NULL);
|
---|
1697 | j = 0;
|
---|
1698 | for (i = 0; i < cModules; i++)
|
---|
1699 | {
|
---|
1700 | //test RVA inside ID-Section
|
---|
1701 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
|
---|
1702 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
---|
1703 | }
|
---|
1704 | else {
|
---|
1705 | fBorland = TRUE;
|
---|
1706 | //is the "Extra"-section already found or do we have to find it?
|
---|
1707 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
---|
1708 | {
|
---|
1709 | if (GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name)) {
|
---|
1710 | free(pszModules);
|
---|
1711 | return FALSE;
|
---|
1712 | }
|
---|
1713 | }
|
---|
1714 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | strcpy(pszModules+j, pszTmp);
|
---|
1718 | j += strlen(pszTmp) + 1;
|
---|
1719 | }
|
---|
1720 | if (fBorland)
|
---|
1721 | dprintf((LOG, "Borland-styled PE-File." ));
|
---|
1722 |
|
---|
1723 | //Store modules
|
---|
1724 | dprintf((LOG, "%d imported Modules: ", cModules ));
|
---|
1725 |
|
---|
1726 | /* 2) functions */
|
---|
1727 | pszCurModule = pszModules;
|
---|
1728 | pOH = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(win32file);
|
---|
1729 | for (i = 0; i < cModules; i++)
|
---|
1730 | {
|
---|
1731 | dprintf((LOG, "Module %s", pszCurModule ));
|
---|
1732 | if(pID[i].ForwarderChain) {
|
---|
1733 | dprintf((LOG, "ForwarderChain: %x", pID[i].ForwarderChain));
|
---|
1734 | }
|
---|
1735 | // a) check that OriginalFirstThunk not is 0 and look for Borland-styled PE
|
---|
1736 | if (i == 0)
|
---|
1737 | {
|
---|
1738 | //heavy borland-style test - assume array of thunks is within that style does not change
|
---|
1739 | if((ULONG)pID[i].u.OriginalFirstThunk == 0 ||
|
---|
1740 | (ULONG)pID[i].u.OriginalFirstThunk < shID.VirtualAddress ||
|
---|
1741 | (ULONG)pID[i].u.OriginalFirstThunk >= shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData) ||
|
---|
1742 | (ULONG)pID[i].u.OriginalFirstThunk >= pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress &&
|
---|
1743 | (ULONG)pID[i].u.OriginalFirstThunk < sizeof(*pID)*cModules + pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
|
---|
1744 | {
|
---|
1745 | fBorland = TRUE;
|
---|
1746 | }
|
---|
1747 | }
|
---|
1748 | //light borland-style test
|
---|
1749 | if (pID[i].u.OriginalFirstThunk == 0 || fBorland) {
|
---|
1750 | pulImport = (ULONG*)pID[i].FirstThunk;
|
---|
1751 | }
|
---|
1752 | else pulImport = (ULONG*)pID[i].u.OriginalFirstThunk;
|
---|
1753 |
|
---|
1754 | // b) check if RVA ok
|
---|
1755 | if (!(pulImport > 0 && (ULONG)pulImport < pOH->SizeOfImage)) {
|
---|
1756 | dprintf((LOG, "Invalid RVA %x", pulImport ));
|
---|
1757 | break;
|
---|
1758 | }
|
---|
1759 | // check section
|
---|
1760 | if ((ULONG)pulImport < shExtra.VirtualAddress || (ULONG)pulImport >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
---|
1761 | {
|
---|
1762 | if (!GetSectionHdrByRVA(win32file, &shExtra, (ULONG)pulImport))
|
---|
1763 | {
|
---|
1764 | dprintf((LOG, "warning: could not find section for Thunk RVA %x", pulImport ));
|
---|
1765 | break;
|
---|
1766 | }
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | //SvL: Load dll if needed
|
---|
1770 | dprintf((LOG, "**********************************************************************" ));
|
---|
1771 | dprintf((LOG, "************** Import Module %s ", pszCurModule ));
|
---|
1772 | dprintf((LOG, "**********************************************************************" ));
|
---|
1773 | WinDll = Win32DllBase::findModule(pszCurModule);
|
---|
1774 |
|
---|
1775 | if(WinDll == NULL)
|
---|
1776 | { //not found, so load it
|
---|
1777 | if (WinExe != NULL && WinExe->matchModName(pszCurModule)) {
|
---|
1778 | WinImage = (Win32ImageBase *)WinExe;
|
---|
1779 | }
|
---|
1780 | else {
|
---|
1781 | WinDll = loadDll(pszCurModule);
|
---|
1782 | if(WinDll == NULL) {
|
---|
1783 | return FALSE;
|
---|
1784 | }
|
---|
1785 | }
|
---|
1786 | }
|
---|
1787 | else {
|
---|
1788 | WinDll->AddRef();
|
---|
1789 | dprintf((LOG, "Already found ", pszCurModule));
|
---|
1790 | }
|
---|
1791 | if(WinDll != NULL) {
|
---|
1792 | //add the dll we just loaded to dependency list for this image
|
---|
1793 | addDependency(WinDll);
|
---|
1794 |
|
---|
1795 | //Make sure the dependency list is correct (already done
|
---|
1796 | //in the ctor of Win32DllBase, but for LX dlls the parent is
|
---|
1797 | //then set to NULL; so change it here again
|
---|
1798 | WinDll->setUnloadOrder(this);
|
---|
1799 | WinImage = (Win32ImageBase *)WinDll;
|
---|
1800 | }
|
---|
1801 | else
|
---|
1802 | if(WinImage == NULL) {
|
---|
1803 | dprintf((LOG, "Unable to load dll %s", pszCurModule ));
|
---|
1804 | return FALSE;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | pulImport = (PULONG)((ULONG)pulImport + (ULONG)win32file);
|
---|
1808 | j = 0;
|
---|
1809 | ulCurFixup = (ULONG)pID[i].FirstThunk + (ULONG)win32file;
|
---|
1810 |
|
---|
1811 | section = findSectionByOS2Addr(ulCurFixup);
|
---|
1812 | if(section == NULL) {
|
---|
1813 | dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
|
---|
1814 | return FALSE;
|
---|
1815 | }
|
---|
1816 | //SvL: Read page from disk
|
---|
1817 | commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
|
---|
1818 | //SvL: Enable write access
|
---|
1819 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
|
---|
1820 | nrPages = 1;
|
---|
1821 |
|
---|
1822 | while (pulImport[j] != 0) {
|
---|
1823 | if (pulImport[j] & IMAGE_ORDINAL_FLAG) { //ordinal
|
---|
1824 | dprintf((LOG, "0x%08x Imported function %s @%d", ulCurFixup , pszCurModule, (pulImport[j] & ~IMAGE_ORDINAL_FLAG) ));
|
---|
1825 | StoreImportByOrd(WinImage, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);
|
---|
1826 | }
|
---|
1827 | else { //name
|
---|
1828 | //check
|
---|
1829 | if (pulImport[j] < shExtra.VirtualAddress || pulImport[j] >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData)) {
|
---|
1830 | if (!GetSectionHdrByRVA(win32file, &shExtra, pulImport[j]))
|
---|
1831 | {
|
---|
1832 | dprintf((LOG, "warning: could not find section for Import Name RVA ", pulImport[j] ));
|
---|
1833 | break;
|
---|
1834 | }
|
---|
1835 | }
|
---|
1836 | //KSO - Aug 6 1998 1:15am:this eases comparing...
|
---|
1837 | char *pszFunctionName = (char*)(pulImport[j] + (ULONG)win32file + 2);
|
---|
1838 | dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup, pszFunctionName, WinImage->getApi(pszFunctionName)));
|
---|
1839 | StoreImportByName(WinImage, pszFunctionName, ulCurFixup);
|
---|
1840 | }
|
---|
1841 | ulCurFixup += sizeof(IMAGE_THUNK_DATA);
|
---|
1842 | j++;
|
---|
1843 | if((ulCurFixup & 0xfff) == 0) {
|
---|
1844 | commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
|
---|
1845 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
|
---|
1846 | nrPages++;
|
---|
1847 | }
|
---|
1848 | }
|
---|
1849 | //SvL: And restore original protection flags
|
---|
1850 | ulCurFixup = (ULONG)pID[i].FirstThunk + pOH->ImageBase;
|
---|
1851 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE*nrPages, section->pageflags);
|
---|
1852 |
|
---|
1853 | dprintf((LOG, "**********************************************************************" ));
|
---|
1854 | dprintf((LOG, "************** End Import Module %s ", pszCurModule ));
|
---|
1855 | dprintf((LOG, "**********************************************************************" ));
|
---|
1856 |
|
---|
1857 | pszCurModule += strlen(pszCurModule) + 1;
|
---|
1858 | }//for (i = 0; i < cModules; i++)
|
---|
1859 |
|
---|
1860 | free(pszModules);
|
---|
1861 |
|
---|
1862 | return TRUE;
|
---|
1863 | }
|
---|
1864 | //******************************************************************************
|
---|
1865 | //******************************************************************************
|
---|
1866 | BOOL Win32PeLdrImage::insideModule(ULONG address)
|
---|
1867 | {
|
---|
1868 | if((address >= realBaseAddress) && (address < realBaseAddress + imageSize)) {
|
---|
1869 | return TRUE;
|
---|
1870 | }
|
---|
1871 | return FALSE;
|
---|
1872 | }
|
---|
1873 | //******************************************************************************
|
---|
1874 | //******************************************************************************
|
---|
1875 | BOOL Win32PeLdrImage::insideModuleCode(ULONG address)
|
---|
1876 | {
|
---|
1877 | Section *sect;
|
---|
1878 |
|
---|
1879 | sect = findSectionByOS2Addr(address);
|
---|
1880 | if(sect && (sect->pageflags & PAG_EXECUTE)) {
|
---|
1881 | return TRUE;
|
---|
1882 | }
|
---|
1883 | return FALSE;
|
---|
1884 | }
|
---|
1885 | //******************************************************************************
|
---|
1886 | //******************************************************************************
|
---|
1887 | ULONG Win32PeLdrImage::getImageSize()
|
---|
1888 | {
|
---|
1889 | return imageSize;
|
---|
1890 | }
|
---|
1891 | //******************************************************************************
|
---|
1892 | //******************************************************************************
|
---|
1893 | ULONG Win32PeLdrImage::getApi(char *name)
|
---|
1894 | {
|
---|
1895 | ULONG apiaddr, i, apilen;
|
---|
1896 | char *apiname;
|
---|
1897 | char tmp[4];
|
---|
1898 | NameExport *curexport;
|
---|
1899 | ULONG ulAPIOrdinal; /* api requested by ordinal */
|
---|
1900 |
|
---|
1901 | apilen = strlen(name) + 1;
|
---|
1902 | if(apilen < 4)
|
---|
1903 | {
|
---|
1904 | *(ULONG *)tmp = 0;
|
---|
1905 | strcpy(tmp, name);
|
---|
1906 | apiname = tmp;
|
---|
1907 | apilen = 4;
|
---|
1908 | }
|
---|
1909 | else apiname = name;
|
---|
1910 |
|
---|
1911 | curexport = nameexports;
|
---|
1912 | for(i=0; i<nrNameExports; i++)
|
---|
1913 | {
|
---|
1914 | if(apilen == curexport->nlength &&
|
---|
1915 | *(ULONG *)curexport->name == *(ULONG *)apiname)
|
---|
1916 | {
|
---|
1917 | if(strcmp(curexport->name, apiname) == 0)
|
---|
1918 | {
|
---|
1919 | return(curexport->virtaddr);
|
---|
1920 | }
|
---|
1921 | }
|
---|
1922 | curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
|
---|
1923 | }
|
---|
1924 | return(0);
|
---|
1925 | }
|
---|
1926 | //******************************************************************************
|
---|
1927 | //******************************************************************************
|
---|
1928 | ULONG Win32PeLdrImage::getApi(int ordinal)
|
---|
1929 | {
|
---|
1930 | ULONG apiaddr, i;
|
---|
1931 | OrdExport *curexport;
|
---|
1932 | NameExport *nexport;
|
---|
1933 |
|
---|
1934 | curexport = ordexports;
|
---|
1935 | for(i=0;i<nrOrdExports;i++)
|
---|
1936 | {
|
---|
1937 | if(curexport->ordinal == ordinal)
|
---|
1938 | return(curexport->virtaddr);
|
---|
1939 | curexport++;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | //Name exports also contain an ordinal, so check this
|
---|
1943 | nexport = nameexports;
|
---|
1944 | for(i=0;i<nrNameExports;i++)
|
---|
1945 | {
|
---|
1946 | if(nexport->ordinal == ordinal)
|
---|
1947 | return(nexport->virtaddr);
|
---|
1948 |
|
---|
1949 | nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
|
---|
1950 | }
|
---|
1951 | return(0);
|
---|
1952 | }
|
---|
1953 | //******************************************************************************
|
---|
1954 | //Returns required OS version for this image
|
---|
1955 | //******************************************************************************
|
---|
1956 | ULONG Win32PeLdrImage::getVersion()
|
---|
1957 | {
|
---|
1958 | return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion;
|
---|
1959 | }
|
---|
1960 | //******************************************************************************
|
---|
1961 | //******************************************************************************
|
---|
1962 | ULONG MissingApi()
|
---|
1963 | {
|
---|
1964 | static BOOL fIgnore = FALSE;
|
---|
1965 | int r;
|
---|
1966 |
|
---|
1967 | dprintf((LOG, "Missing api called!\n"));
|
---|
1968 | if(fIgnore)
|
---|
1969 | return(0);
|
---|
1970 |
|
---|
1971 | do {
|
---|
1972 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, "The application has called a non-existing api\n",
|
---|
1973 | "Internal Odin Error", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
---|
1974 | }
|
---|
1975 | while(r == MBID_RETRY); //giggle
|
---|
1976 |
|
---|
1977 | if( r != MBID_IGNORE )
|
---|
1978 | ExitProcess(987);
|
---|
1979 |
|
---|
1980 | fIgnore = TRUE;
|
---|
1981 | return(0);
|
---|
1982 | }
|
---|
1983 | /******************************************************************************/
|
---|
1984 | /******************************************************************************/
|
---|