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

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

TLS + resource lookup (name -1) fixes

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