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