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

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

added forwarder support for PE loader

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