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