source: trunk/src/kernel32/winimagepeldr.cpp@ 4474

Last change on this file since 4474 was 4474, checked in by sandervl, 25 years ago

pe loader fixes, add system32\drivers dir during installation, add build date + time during kernel32 load

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