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

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

changes for setting current dir of new process

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