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