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