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