| 1 | /* $Id: winimagepeldr.cpp,v 1.89 2001-07-03 06:53:39 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 PE loader Image base class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 1998 Knut St. Osmundsen
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | * TODO: Check psh[i].Characteristics for more than only the code section
|
|---|
| 12 | * TODO: Make resource section readonly when GDI32 is fixed
|
|---|
| 13 | * TODO: Loading of forwarder dlls before handling imports might not be correct
|
|---|
| 14 | * (circular dependencies; have to check what NT does)
|
|---|
| 15 | * TODO: Two LoadLibrary calls in two threads at the same time won't be handled properly (rare but possible)
|
|---|
| 16 | *
|
|---|
| 17 | * NOTE: FLAG_PELDR_LOADASDATAFILE is a special flag to only load the resource directory
|
|---|
| 18 | * of a PE image. Processing imports, sections etc is not done.
|
|---|
| 19 | * This is useful for GetVersionSize/Resource in case it wants to
|
|---|
| 20 | * get version info of an image that is not loaded.
|
|---|
| 21 | * So an instance of this type can't be used for anything but resource lookup!
|
|---|
| 22 | *
|
|---|
| 23 | *
|
|---|
| 24 | */
|
|---|
| 25 | #define INCL_DOSFILEMGR /* File Manager values */
|
|---|
| 26 | #define INCL_DOSMODULEMGR
|
|---|
| 27 | #define INCL_DOSERRORS /* DOS Error values */
|
|---|
| 28 | #define INCL_DOSPROCESS /* DOS Process values */
|
|---|
| 29 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
|---|
| 30 | #define INCL_WIN
|
|---|
| 31 | #define INCL_BASE
|
|---|
| 32 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 33 |
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <string.h>
|
|---|
| 36 | #include <stdlib.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | //use a different logfile
|
|---|
| 40 | #define PRIVATE_LOGGING
|
|---|
| 41 | #include <misc.h>
|
|---|
| 42 | #include <win32api.h>
|
|---|
| 43 | #include "winimagebase.h"
|
|---|
| 44 | #include "winimagepeldr.h"
|
|---|
| 45 | #include "windllpeldr.h"
|
|---|
| 46 | #include "windlllx.h"
|
|---|
| 47 | #include "winexebase.h"
|
|---|
| 48 | #include <pefile.h>
|
|---|
| 49 | #include <unicode.h>
|
|---|
| 50 | #include "oslibmisc.h"
|
|---|
| 51 | #include "initterm.h"
|
|---|
| 52 | #include <win\virtual.h>
|
|---|
| 53 | #include "oslibdos.h"
|
|---|
| 54 | #include "mmap.h"
|
|---|
| 55 | #include <wprocess.h>
|
|---|
| 56 |
|
|---|
| 57 | //Define COMMIT_ALL to let the pe loader commit all sections of the image
|
|---|
| 58 | //This is very useful during debugging as you'll get lots of exceptions
|
|---|
| 59 | //otherwise.
|
|---|
| 60 | //#ifdef DEBUG
|
|---|
| 61 | #define COMMIT_ALL
|
|---|
| 62 | //#endif
|
|---|
| 63 |
|
|---|
| 64 | char szErrorTitle[] = "Odin";
|
|---|
| 65 | char szMemErrorMsg[] = "Memory allocation failure";
|
|---|
| 66 | char szFileErrorMsg[] = "File IO error";
|
|---|
| 67 | char szPEErrorMsg[] = "Not a valid win32 exe. (perhaps 16 bits windows)";
|
|---|
| 68 | char szCPUErrorMsg[] = "Executable doesn't run on x86 machines";
|
|---|
| 69 | char szExeErrorMsg[] = "File isn't an executable";
|
|---|
| 70 | char szInteralErrorMsg[]= "Internal Error";
|
|---|
| 71 | char szErrorModule[128] = "";
|
|---|
| 72 |
|
|---|
| 73 | #ifdef DEBUG
|
|---|
| 74 | static FILE *_privateLogFile = NULL;
|
|---|
| 75 | #endif
|
|---|
| 76 |
|
|---|
| 77 | ULONG MissingApi();
|
|---|
| 78 |
|
|---|
| 79 | //******************************************************************************
|
|---|
| 80 | //******************************************************************************
|
|---|
| 81 | void OpenPrivateLogFilePE()
|
|---|
| 82 | {
|
|---|
| 83 | #ifdef DEBUG
|
|---|
| 84 | char logname[CCHMAXPATH];
|
|---|
| 85 |
|
|---|
| 86 | sprintf(logname, "pe_%d.log", loadNr);
|
|---|
| 87 | _privateLogFile = fopen(logname, "w");
|
|---|
| 88 | if(_privateLogFile == NULL) {
|
|---|
| 89 | sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
|
|---|
| 90 | _privateLogFile = fopen(logname, "w");
|
|---|
| 91 | }
|
|---|
| 92 | dprintfGlobal(("PE LOGFILE : %s", logname));
|
|---|
| 93 | #endif
|
|---|
| 94 | }
|
|---|
| 95 | //******************************************************************************
|
|---|
| 96 | //******************************************************************************
|
|---|
| 97 | void ClosePrivateLogFilePE()
|
|---|
| 98 | {
|
|---|
| 99 | #ifdef DEBUG
|
|---|
| 100 | if(_privateLogFile) {
|
|---|
| 101 | fclose(_privateLogFile);
|
|---|
| 102 | _privateLogFile = NULL;
|
|---|
| 103 | }
|
|---|
| 104 | #endif
|
|---|
| 105 | }
|
|---|
| 106 | //******************************************************************************
|
|---|
| 107 | //******************************************************************************
|
|---|
| 108 | Win32PeLdrImage::Win32PeLdrImage(char *pszFileName, BOOL isExe) :
|
|---|
| 109 | Win32ImageBase(-1),
|
|---|
| 110 | nrsections(0), imageSize(0), dwFlags(0), section(NULL),
|
|---|
| 111 | imageVirtBase(-1), realBaseAddress(0), imageVirtEnd(0),
|
|---|
| 112 | nrNameExports(0), nrOrdExports(0), nameexports(NULL), ordexports(NULL),
|
|---|
| 113 | memmap(NULL), pFixups(NULL), dwFixupSize(0), curnameexport(NULL), curordexport(NULL),
|
|---|
| 114 | nrOrdExportsRegistered(0)
|
|---|
| 115 | {
|
|---|
| 116 | HFILE dllfile;
|
|---|
| 117 |
|
|---|
| 118 | strcpy(szFileName, pszFileName);
|
|---|
| 119 | strupr(szFileName);
|
|---|
| 120 | if(isExe) {
|
|---|
| 121 | if(!strchr(szFileName, '.')) {
|
|---|
| 122 | strcat(szFileName,".EXE");
|
|---|
| 123 | }
|
|---|
| 124 | dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
|---|
| 125 | if(dllfile == NULL) {
|
|---|
| 126 | if(!strstr(szFileName, ".EXE")) {
|
|---|
| 127 | strcat(szFileName,".EXE");
|
|---|
| 128 | }
|
|---|
| 129 | dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
|---|
| 130 | if(dllfile == NULL) {
|
|---|
| 131 | OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | else OSLibDosClose(dllfile);
|
|---|
| 135 | }
|
|---|
| 136 | else {
|
|---|
| 137 | findDll(szFileName, szModule, sizeof(szModule));
|
|---|
| 138 | strcpy(szFileName, szModule);
|
|---|
| 139 | }
|
|---|
| 140 | strcpy(szModule, OSLibStripPath(szFileName));
|
|---|
| 141 | strupr(szModule);
|
|---|
| 142 | }
|
|---|
| 143 | //******************************************************************************
|
|---|
| 144 | //******************************************************************************
|
|---|
| 145 | Win32PeLdrImage::~Win32PeLdrImage()
|
|---|
| 146 | {
|
|---|
| 147 | if(memmap)
|
|---|
| 148 | delete memmap;
|
|---|
| 149 |
|
|---|
| 150 | if(hFile) {
|
|---|
| 151 | OSLibDosClose(hFile);
|
|---|
| 152 | hFile = 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | if(realBaseAddress)
|
|---|
| 156 | DosFreeMem((PVOID)realBaseAddress);
|
|---|
| 157 |
|
|---|
| 158 | if(nameexports)
|
|---|
| 159 | free(nameexports);
|
|---|
| 160 |
|
|---|
| 161 | if(ordexports)
|
|---|
| 162 | free(ordexports);
|
|---|
| 163 |
|
|---|
| 164 | if(section)
|
|---|
| 165 | free(section);
|
|---|
| 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, NULL);
|
|---|
| 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 for %s at %x, rc %d", szFileName, oh.ImageBase, errorState));;
|
|---|
| 221 | goto failure;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | memmap = new Win32MemMap(this, realBaseAddress, imageSize);
|
|---|
| 225 | if(memmap == NULL || !memmap->Init()) {
|
|---|
| 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 | section = (Section *)malloc(nSections*sizeof(Section));
|
|---|
| 281 | if(section == NULL) {
|
|---|
| 282 | DebugInt3();
|
|---|
| 283 | goto failure;
|
|---|
| 284 | }
|
|---|
| 285 | memset(section, 0, nSections*sizeof(Section));
|
|---|
| 286 |
|
|---|
| 287 | if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
|
|---|
| 288 | {
|
|---|
| 289 | imageSize = 0;
|
|---|
| 290 | if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
|
|---|
| 291 | {
|
|---|
| 292 | dprintf((LOG, "*************************PE SECTIONS START**************************" ));
|
|---|
| 293 | for (i=0; i<nSections; i++)
|
|---|
| 294 | {
|
|---|
| 295 | dprintf((LOG, "Raw data size: %x", psh[i].SizeOfRawData ));
|
|---|
| 296 | dprintf((LOG, "Virtual Address: %x", psh[i].VirtualAddress ));
|
|---|
| 297 | dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
|
|---|
| 298 | dprintf((LOG, "Virtual Address End: %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
|
|---|
| 299 | dprintf((LOG, "Virtual Size: %x", psh[i].Misc.VirtualSize ));
|
|---|
| 300 | dprintf((LOG, "Pointer to raw data: %x", psh[i].PointerToRawData ));
|
|---|
| 301 | dprintf((LOG, "Section flags: %x\n\n", psh[i].Characteristics ));
|
|---|
| 302 |
|
|---|
| 303 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
|
|---|
| 304 | {
|
|---|
| 305 | dprintf((LOG, ".reloc" ));
|
|---|
| 306 | addSection(SECTION_RELOC, psh[i].PointerToRawData,
|
|---|
| 307 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 308 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 309 | continue;
|
|---|
| 310 | }
|
|---|
| 311 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
|
|---|
| 312 | {
|
|---|
| 313 | //SvL: Angus.exe has empty export section that's really an
|
|---|
| 314 | // uninitialized data section
|
|---|
| 315 | if(psh[i].SizeOfRawData) {
|
|---|
| 316 | dprintf((LOG, ".edata" ));
|
|---|
| 317 | addSection(SECTION_EXPORT, psh[i].PointerToRawData,
|
|---|
| 318 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 319 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 320 | continue;
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
|
|---|
| 324 | {
|
|---|
| 325 | dprintf((LOG, ".rsrc" ));
|
|---|
| 326 | addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
|
|---|
| 327 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 328 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 329 | continue;
|
|---|
| 330 | }
|
|---|
| 331 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
|
|---|
| 332 | {
|
|---|
| 333 | dprintf((LOG, "TLS section"));
|
|---|
| 334 | tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
|
|---|
| 335 | if(tlsDir) {
|
|---|
| 336 | addSection(SECTION_TLS, psh[i].PointerToRawData,
|
|---|
| 337 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 338 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 339 | }
|
|---|
| 340 | continue;
|
|---|
| 341 | }
|
|---|
| 342 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
|
|---|
| 343 | {
|
|---|
| 344 | dprintf((LOG, ".rdebug" ));
|
|---|
| 345 | addSection(SECTION_DEBUG, psh[i].PointerToRawData,
|
|---|
| 346 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 347 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 348 | continue;
|
|---|
| 349 | }
|
|---|
| 350 | if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
|
|---|
| 351 | {
|
|---|
| 352 | int type = SECTION_IMPORT;
|
|---|
| 353 |
|
|---|
| 354 | dprintf((LOG, "Import Data Section" ));
|
|---|
| 355 | if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
|
|---|
| 356 | dprintf((LOG, "Also Code Section"));
|
|---|
| 357 | type |= SECTION_CODE;
|
|---|
| 358 | }
|
|---|
| 359 | addSection(type, psh[i].PointerToRawData,
|
|---|
| 360 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 361 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 362 | continue;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
|
|---|
| 366 | if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
|
|---|
| 367 | (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
|
|---|
| 368 | !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
|
|---|
| 369 | )
|
|---|
| 370 | {
|
|---|
| 371 | dprintf((LOG, "Code Section"));
|
|---|
| 372 | addSection(SECTION_CODE, psh[i].PointerToRawData,
|
|---|
| 373 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 374 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 375 | continue;
|
|---|
| 376 | }
|
|---|
| 377 | if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
|
|---|
| 378 | dprintf((LOG, "Read Only Data Section" ));
|
|---|
| 379 | addSection(SECTION_READONLYDATA, psh[i].PointerToRawData,
|
|---|
| 380 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 381 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 382 | continue;
|
|---|
| 383 | }
|
|---|
| 384 | if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
|
|---|
| 385 | dprintf((LOG, "Uninitialized Data Section" ));
|
|---|
| 386 | addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
|
|---|
| 387 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 388 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 389 | continue;
|
|---|
| 390 | }
|
|---|
| 391 | if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
|
|---|
| 392 | dprintf((LOG, "Initialized Data Section" ));
|
|---|
| 393 | addSection(SECTION_INITDATA, psh[i].PointerToRawData,
|
|---|
| 394 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 395 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 396 | continue;
|
|---|
| 397 | }
|
|---|
| 398 | if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
|
|---|
| 399 | dprintf((LOG, "Other Section, stored as read/write uninit data" ));
|
|---|
| 400 | addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
|
|---|
| 401 | psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
|
|---|
| 402 | psh[i].Misc.VirtualSize, psh[i].Characteristics);
|
|---|
| 403 | continue;
|
|---|
| 404 | }
|
|---|
| 405 | dprintf((LOG, "Unknown section" ));
|
|---|
| 406 | goto failure;
|
|---|
| 407 | }
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 | else {
|
|---|
| 411 | if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh))
|
|---|
| 412 | {
|
|---|
| 413 | dprintf((LOG, "*************************PE SECTIONS START**************************" ));
|
|---|
| 414 | dprintf((LOG, "Raw data size: %x", sh.SizeOfRawData ));
|
|---|
| 415 | dprintf((LOG, "Virtual Address: %x", sh.VirtualAddress ));
|
|---|
| 416 | dprintf((LOG, "Virtual Address Start:%x", sh.VirtualAddress+oh.ImageBase ));
|
|---|
| 417 | dprintf((LOG, "Virtual Address End: %x", sh.VirtualAddress+oh.ImageBase+sh.Misc.VirtualSize ));
|
|---|
| 418 | dprintf((LOG, "Virtual Size: %x", sh.Misc.VirtualSize ));
|
|---|
| 419 | dprintf((LOG, "Pointer to raw data: %x", sh.PointerToRawData ));
|
|---|
| 420 | dprintf((LOG, "Section flags: %x\n\n", sh.Characteristics ));
|
|---|
| 421 | addSection(SECTION_RESOURCE, sh.PointerToRawData,
|
|---|
| 422 | sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
|
|---|
| 423 | sh.Misc.VirtualSize, sh.Characteristics);
|
|---|
| 424 | }
|
|---|
| 425 | }
|
|---|
| 426 | dprintf((LOG, "*************************PE SECTIONS END **************************" ));
|
|---|
| 427 |
|
|---|
| 428 | imageSize += imageVirtBase - oh.ImageBase;
|
|---|
| 429 | dprintf((LOG, "Total size of Image %x", imageSize ));
|
|---|
| 430 | dprintf((LOG, "imageVirtBase %x", imageVirtBase ));
|
|---|
| 431 | dprintf((LOG, "imageVirtEnd %x", imageVirtEnd ));
|
|---|
| 432 |
|
|---|
| 433 | //In case there are any gaps between sections, adjust size
|
|---|
| 434 | if(imageSize != imageVirtEnd - oh.ImageBase)
|
|---|
| 435 | {
|
|---|
| 436 | dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
|
|---|
| 437 | imageSize = imageVirtEnd - oh.ImageBase;
|
|---|
| 438 | }
|
|---|
| 439 | if(imageSize < oh.SizeOfImage) {
|
|---|
| 440 | imageSize = oh.SizeOfImage;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | dprintf((LOG, "OS/2 base address %x", realBaseAddress ));
|
|---|
| 444 | if(oh.AddressOfEntryPoint) {
|
|---|
| 445 | entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
|
|---|
| 446 | }
|
|---|
| 447 | else {
|
|---|
| 448 | dprintf((LOG, "EntryPoint == NULL" ));
|
|---|
| 449 | entryPoint = NULL;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | //set memory protection flags
|
|---|
| 453 | if(setMemFlags() == FALSE) {
|
|---|
| 454 | dprintf((LOG, "Failed to set memory protection" ));
|
|---|
| 455 | goto failure;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | if(realBaseAddress != oh.ImageBase && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
|
|---|
| 459 | pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
|---|
| 460 | dwFixupSize = ImageDirectorySize(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
|
|---|
| 461 | commitPage((ULONG)pFixups, FALSE);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
|
|---|
| 465 | {
|
|---|
| 466 | if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS))
|
|---|
| 467 | {
|
|---|
| 468 | Section *sect;
|
|---|
| 469 | BOOL fTLSFixups = FALSE;
|
|---|
| 470 |
|
|---|
| 471 | sect = findSectionByAddr(tlsDir->StartAddressOfRawData);
|
|---|
| 472 | //There might be fixups for the TLS structure, so search the sections
|
|---|
| 473 | //by the OS/2 virtual address too
|
|---|
| 474 | if(sect == NULL) {
|
|---|
| 475 | sect = findSectionByOS2Addr(tlsDir->StartAddressOfRawData);
|
|---|
| 476 | fTLSFixups = TRUE;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | dprintf((LOG, "TLS Directory" ));
|
|---|
| 480 | dprintf((LOG, "TLS Address of Index %x", tlsDir->AddressOfIndex ));
|
|---|
| 481 | dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
|
|---|
| 482 | dprintf((LOG, "TLS SizeOfZeroFill %x", tlsDir->SizeOfZeroFill ));
|
|---|
| 483 | dprintf((LOG, "TLS Characteristics %x", tlsDir->Characteristics ));
|
|---|
| 484 | if(sect == NULL) {
|
|---|
| 485 | dprintf((LOG, "Couldn't find TLS section!!" ));
|
|---|
| 486 | goto failure;
|
|---|
| 487 | }
|
|---|
| 488 | setTLSAddress((char *)sect->realvirtaddr);
|
|---|
| 489 | setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
|
|---|
| 490 | setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
|
|---|
| 491 |
|
|---|
| 492 | fTLSFixups = FALSE;
|
|---|
| 493 | sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
|
|---|
| 494 | //There might be fixups for the TLS structure, so search the sections
|
|---|
| 495 | //by the OS/2 virtual address too
|
|---|
| 496 | if(sect == NULL) {
|
|---|
| 497 | sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
|
|---|
| 498 | fTLSFixups = TRUE;
|
|---|
| 499 | }
|
|---|
| 500 | if(sect == NULL) {
|
|---|
| 501 | dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
|
|---|
| 502 | goto failure;
|
|---|
| 503 | }
|
|---|
| 504 | if(fTLSFixups) {
|
|---|
| 505 | setTLSIndexAddr((LPDWORD)tlsDir->AddressOfIndex); //no fixup required
|
|---|
| 506 | }
|
|---|
| 507 | else {//need to add a manual fixup
|
|---|
| 508 | setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | if((ULONG)tlsDir->AddressOfCallBacks != 0)
|
|---|
| 512 | {
|
|---|
| 513 | fTLSFixups = FALSE;
|
|---|
| 514 |
|
|---|
| 515 | sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
|
|---|
| 516 | //There might be fixups for the TLS structure, so search the sections
|
|---|
| 517 | //by the OS/2 virtual address too
|
|---|
| 518 | if(sect == NULL) {
|
|---|
| 519 | sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
|
|---|
| 520 | fTLSFixups = TRUE;
|
|---|
| 521 | }
|
|---|
| 522 | if(sect == NULL) {
|
|---|
| 523 | dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
|
|---|
| 524 | goto failure;
|
|---|
| 525 | }
|
|---|
| 526 | if(fTLSFixups) {
|
|---|
| 527 | setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)tlsDir->AddressOfCallBacks); //no fixup required
|
|---|
| 528 | }
|
|---|
| 529 | else {//need to add a manual fixup
|
|---|
| 530 | setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
|
|---|
| 531 | }
|
|---|
| 532 | //modify tls callback pointers for new image base address
|
|---|
| 533 | int i = 0;
|
|---|
| 534 | while(tlsCallBackAddr[i])
|
|---|
| 535 | {
|
|---|
| 536 | fTLSFixups = FALSE;
|
|---|
| 537 |
|
|---|
| 538 | sect = findSectionByAddr((ULONG)tlsCallBackAddr[i]);
|
|---|
| 539 | //There might be fixups for the TLS structure, so search the sections
|
|---|
| 540 | //by the OS/2 virtual address too
|
|---|
| 541 | if(sect == NULL) {
|
|---|
| 542 | sect = findSectionByOS2Addr((ULONG)tlsCallBackAddr[i]);
|
|---|
| 543 | fTLSFixups = TRUE;
|
|---|
| 544 | }
|
|---|
| 545 | if(sect == NULL) {
|
|---|
| 546 | dprintf((LOG, "Couldn't find TLS callback section!!" ));
|
|---|
| 547 | goto failure;
|
|---|
| 548 | }
|
|---|
| 549 | if(fTLSFixups) {
|
|---|
| 550 | tlsCallBackAddr[i] = tlsCallBackAddr[i];
|
|---|
| 551 | }
|
|---|
| 552 | else tlsCallBackAddr[i] = (PIMAGE_TLS_CALLBACK)(realBaseAddress + ((ULONG)tlsCallBackAddr[i] - oh.ImageBase));
|
|---|
| 553 | i++;
|
|---|
| 554 | }
|
|---|
| 555 | }
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | #ifdef DEBUG
|
|---|
| 559 | dprintf((LOG, "Image directories: "));
|
|---|
| 560 | for (i = 0; i < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; i++)
|
|---|
| 561 | {
|
|---|
| 562 | char *pszName;
|
|---|
| 563 |
|
|---|
| 564 | if(oh.DataDirectory[i].VirtualAddress && oh.DataDirectory[i].Size) {
|
|---|
| 565 | switch (i)
|
|---|
| 566 | {
|
|---|
| 567 | case IMAGE_DIRECTORY_ENTRY_EXPORT: pszName = "Export Directory (IMAGE_DIRECTORY_ENTRY_EXPORT)"; break;
|
|---|
| 568 | case IMAGE_DIRECTORY_ENTRY_IMPORT: pszName = "Import Directory (IMAGE_DIRECTORY_ENTRY_IMPORT)"; break;
|
|---|
| 569 | case IMAGE_DIRECTORY_ENTRY_RESOURCE: pszName = "Resource Directory (IMAGE_DIRECTORY_ENTRY_RESOURCE)"; break;
|
|---|
| 570 | case IMAGE_DIRECTORY_ENTRY_EXCEPTION: pszName = "Exception Directory (IMAGE_DIRECTORY_ENTRY_EXCEPTION)"; break;
|
|---|
| 571 | case IMAGE_DIRECTORY_ENTRY_SECURITY: pszName = "Security Directory (IMAGE_DIRECTORY_ENTRY_SECURITY)"; break;
|
|---|
| 572 | case IMAGE_DIRECTORY_ENTRY_BASERELOC: pszName = "Base Relocation Table (IMAGE_DIRECTORY_ENTRY_BASERELOC)"; break;
|
|---|
| 573 | case IMAGE_DIRECTORY_ENTRY_DEBUG: pszName = "Debug Directory (IMAGE_DIRECTORY_ENTRY_DEBUG)"; break;
|
|---|
| 574 | case IMAGE_DIRECTORY_ENTRY_COPYRIGHT: pszName = "Description String (IMAGE_DIRECTORY_ENTRY_COPYRIGHT)"; break;
|
|---|
| 575 | case IMAGE_DIRECTORY_ENTRY_GLOBALPTR: pszName = "Machine Value (MIPS GP) (IMAGE_DIRECTORY_ENTRY_GLOBALPTR)"; break;
|
|---|
| 576 | case IMAGE_DIRECTORY_ENTRY_TLS: pszName = "TLS Directory (IMAGE_DIRECTORY_ENTRY_TLS)"; break;
|
|---|
| 577 | case IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: pszName = "Load Configuration Directory (IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG)"; break;
|
|---|
| 578 | case IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT:pszName = "Bound Import Directory in headers (IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT)"; break;
|
|---|
| 579 | case IMAGE_DIRECTORY_ENTRY_IAT: pszName = "Import Address Table (IMAGE_DIRECTORY_ENTRY_IAT)"; break;
|
|---|
| 580 | default:
|
|---|
| 581 | pszName = "unknown";
|
|---|
| 582 | }
|
|---|
| 583 | dprintf((LOG, "directory %s", pszName));
|
|---|
| 584 | dprintf((LOG, " Address 0x%08x", oh.DataDirectory[i].VirtualAddress));
|
|---|
| 585 | dprintf((LOG, " Size 0x%08x", oh.DataDirectory[i].Size));
|
|---|
| 586 | }
|
|---|
| 587 | }
|
|---|
| 588 | dprintf((LOG, "\n\n"));
|
|---|
| 589 | #endif
|
|---|
| 590 |
|
|---|
| 591 | #ifdef COMMIT_ALL
|
|---|
| 592 | for (i=0; i<nSections; i++) {
|
|---|
| 593 | commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
|
|---|
| 594 | }
|
|---|
| 595 | #else
|
|---|
| 596 | for (i=0; i<nSections; i++) {
|
|---|
| 597 | switch(section[i].type)
|
|---|
| 598 | {
|
|---|
| 599 | case SECTION_IMPORT:
|
|---|
| 600 | case SECTION_RELOC:
|
|---|
| 601 | case SECTION_EXPORT:
|
|---|
| 602 | commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
|
|---|
| 603 | break;
|
|---|
| 604 | }
|
|---|
| 605 | }
|
|---|
| 606 | #endif
|
|---|
| 607 | if(processExports((char *)win32file) == FALSE) {
|
|---|
| 608 | dprintf((LOG, "Failed to process exported apis" ));
|
|---|
| 609 | goto failure;
|
|---|
| 610 | }
|
|---|
| 611 | }
|
|---|
| 612 | #ifdef COMMIT_ALL
|
|---|
| 613 | else {
|
|---|
| 614 | commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
|
|---|
| 615 | }
|
|---|
| 616 | #endif
|
|---|
| 617 |
|
|---|
| 618 | #ifndef COMMIT_ALL
|
|---|
| 619 | if(entryPoint) {
|
|---|
| 620 | //commit code at entrypoint, since we going to call it anyway
|
|---|
| 621 | commitPage((ULONG)entryPoint, FALSE);
|
|---|
| 622 | }
|
|---|
| 623 | #endif
|
|---|
| 624 |
|
|---|
| 625 | //SvL: Use pointer to image header as module handle now. Some apps needs this
|
|---|
| 626 | hinstance = (HINSTANCE)realBaseAddress;
|
|---|
| 627 |
|
|---|
| 628 | //SvL: Set instance handle in process database structure
|
|---|
| 629 | SetPDBInstance(hinstance);
|
|---|
| 630 |
|
|---|
| 631 | //PH: get pResRootDir pointer correct first, since processImports may
|
|---|
| 632 | // implicitly call functions depending on it.
|
|---|
| 633 | if(oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress && oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size)
|
|---|
| 634 | {
|
|---|
| 635 | //get offset in resource object of directory entry
|
|---|
| 636 | pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)(oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress + realBaseAddress);
|
|---|
| 637 | ulRVAResourceSection = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | //Allocate TLS index for this module
|
|---|
| 641 | //Must do this before dlls are loaded for this module. Some apps assume
|
|---|
| 642 | //they get TLS index 0 for their main executable
|
|---|
| 643 | tlsAlloc();
|
|---|
| 644 | tlsAttachThread(); //setup TLS (main thread)
|
|---|
| 645 |
|
|---|
| 646 | if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
|
|---|
| 647 | {
|
|---|
| 648 | if(processImports((char *)win32file) == FALSE) {
|
|---|
| 649 | dprintf((LOG, "Failed to process imports!" ));
|
|---|
| 650 | goto failure;
|
|---|
| 651 | }
|
|---|
| 652 | }
|
|---|
| 653 | return(TRUE);
|
|---|
| 654 |
|
|---|
| 655 | failure:
|
|---|
| 656 | if(memmap) {
|
|---|
| 657 | delete memmap;
|
|---|
| 658 | memmap = NULL;
|
|---|
| 659 | }
|
|---|
| 660 | if(hFile) {
|
|---|
| 661 | OSLibDosClose(hFile);
|
|---|
| 662 | hFile = 0;
|
|---|
| 663 | }
|
|---|
| 664 | errorState = ERROR_INTERNAL;
|
|---|
| 665 | return FALSE;
|
|---|
| 666 | }
|
|---|
| 667 | //******************************************************************************
|
|---|
| 668 | //******************************************************************************
|
|---|
| 669 | #define DOSREAD_IDEAL_SIZE 61440
|
|---|
| 670 | static inline APIRET _Optlink fastDosRead(HFILE hFile,
|
|---|
| 671 | PVOID pAddress,
|
|---|
| 672 | ULONG ulSize,
|
|---|
| 673 | PULONG pulBytesRead)
|
|---|
| 674 | {
|
|---|
| 675 | /* we better break the DosRead into multiple calls */
|
|---|
| 676 | PBYTE p = (PBYTE)pAddress;
|
|---|
| 677 | ULONG ulReadBytes;
|
|---|
| 678 | APIRET rc;
|
|---|
| 679 |
|
|---|
| 680 | *pulBytesRead = ulSize;
|
|---|
| 681 |
|
|---|
| 682 | do
|
|---|
| 683 | {
|
|---|
| 684 | rc = DosRead(hFile,
|
|---|
| 685 | p,
|
|---|
| 686 | min(DOSREAD_IDEAL_SIZE, ulSize),
|
|---|
| 687 | &ulReadBytes);
|
|---|
| 688 | if (rc != NO_ERROR)
|
|---|
| 689 | {
|
|---|
| 690 | /* in case of errors bail out */
|
|---|
| 691 | *pulBytesRead = 0;
|
|---|
| 692 | return rc;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | ulSize -= ulReadBytes;
|
|---|
| 696 | p += ulReadBytes;
|
|---|
| 697 | }
|
|---|
| 698 | while (ulSize > 0);
|
|---|
| 699 |
|
|---|
| 700 | return NO_ERROR;
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | //******************************************************************************
|
|---|
| 704 | // commitPage:
|
|---|
| 705 | // commits image page(s) when an access violation exception is received
|
|---|
| 706 | // (usually called from exception.cpp; also from other methods in this file)
|
|---|
| 707 | //
|
|---|
| 708 | // Parameters:
|
|---|
| 709 | // virtAddress - address of exception (rounded down to page boundary)
|
|---|
| 710 | // fWriteAccess - type of access violation (read or write)
|
|---|
| 711 | // fPageCmd - SINGLE_PAGE -> commit single page
|
|---|
| 712 | // SECTION_PAGES -> commit default nr of pages
|
|---|
| 713 | // COMPLETE_SECTION -> commit entire section
|
|---|
| 714 | //
|
|---|
| 715 | // Remarks:
|
|---|
| 716 | // DosEnterCritSec/DosExitCritSec is used to make sure the other threads in
|
|---|
| 717 | // the application can't touch the pages before they are loaded from disk and
|
|---|
| 718 | // fixups are applied.
|
|---|
| 719 | //
|
|---|
| 720 | // TODO:
|
|---|
| 721 | // SECTION_PAGES: - don't load pages starting at access violation address, but
|
|---|
| 722 | // a region surrounding it (e.g. -32k -> + 32k)
|
|---|
| 723 | // this will prevent many pagefaults when the app uses
|
|---|
| 724 | // pages with a lower addr.
|
|---|
| 725 | //
|
|---|
| 726 | //******************************************************************************
|
|---|
| 727 | BOOL Win32PeLdrImage::commitPage(ULONG virtAddress, BOOL fWriteAccess, int fPageCmd)
|
|---|
| 728 | {
|
|---|
| 729 | Section *section;
|
|---|
| 730 | ULONG offset, size, sectionsize, protflags, fileoffset, range, attr;
|
|---|
| 731 | ULONG ulNewPos, ulRead, orgVirtAddress = virtAddress;
|
|---|
| 732 | APIRET rc;
|
|---|
| 733 |
|
|---|
| 734 | if(virtAddress == 0) {
|
|---|
| 735 | return FALSE;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | //Round down to nearest page boundary
|
|---|
| 739 | virtAddress = virtAddress & ~0xFFF;
|
|---|
| 740 |
|
|---|
| 741 | section = findSectionByOS2Addr(virtAddress);
|
|---|
| 742 | if(section == NULL) {
|
|---|
| 743 | section = findSectionByOS2Addr(orgVirtAddress);
|
|---|
| 744 | if(section) {
|
|---|
| 745 | virtAddress = orgVirtAddress;
|
|---|
| 746 | }
|
|---|
| 747 | }
|
|---|
| 748 | if(section == NULL) {
|
|---|
| 749 | size = 4096;
|
|---|
| 750 | sectionsize = 4096;
|
|---|
| 751 | protflags = PAG_READ|PAG_WRITE; //readonly?
|
|---|
| 752 | section = findPreviousSectionByOS2Addr(virtAddress);
|
|---|
| 753 | if(section == NULL) {//access to header
|
|---|
| 754 | offset = 0;
|
|---|
| 755 | fileoffset = virtAddress - realBaseAddress;
|
|---|
| 756 | }
|
|---|
| 757 | else {
|
|---|
| 758 | offset = virtAddress - (section->realvirtaddr + section->virtualsize);
|
|---|
| 759 | fileoffset = section->rawoffset + section->rawsize + offset;
|
|---|
| 760 | }
|
|---|
| 761 | }
|
|---|
| 762 | else {
|
|---|
| 763 | protflags = section->pageflags;
|
|---|
| 764 | offset = virtAddress - section->realvirtaddr;
|
|---|
| 765 | sectionsize = section->virtualsize - offset;
|
|---|
| 766 |
|
|---|
| 767 | if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
|
|---|
| 768 | //unintialized data (set to 0)
|
|---|
| 769 | size = 0;
|
|---|
| 770 | fileoffset = -1;
|
|---|
| 771 | }
|
|---|
| 772 | else {
|
|---|
| 773 | size = section->rawsize-offset;
|
|---|
| 774 | fileoffset = section->rawoffset + offset;
|
|---|
| 775 | }
|
|---|
| 776 | if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
|
|---|
| 777 | dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
|
|---|
| 778 | return FALSE;
|
|---|
| 779 | }
|
|---|
| 780 | }
|
|---|
| 781 | if(fPageCmd == COMPLETE_SECTION && section->type == SECTION_DEBUG) {//ignore
|
|---|
| 782 | return TRUE;
|
|---|
| 783 | }
|
|---|
| 784 | //Check range of pages with the same attributes starting at virtAddress
|
|---|
| 785 | //(some pages might already have been loaded)
|
|---|
| 786 | range = sectionsize;
|
|---|
| 787 | rc = DosQueryMem((PVOID)virtAddress, &range, &attr);
|
|---|
| 788 | if(rc) {
|
|---|
| 789 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
|
|---|
| 790 | return FALSE;
|
|---|
| 791 | }
|
|---|
| 792 | if(attr & PAG_COMMIT) {
|
|---|
| 793 | dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
|
|---|
| 794 | return FALSE;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | if(fPageCmd == SINGLE_PAGE) {
|
|---|
| 798 | size = min(size, PAGE_SIZE);
|
|---|
| 799 | sectionsize = min(sectionsize, PAGE_SIZE);
|
|---|
| 800 | }
|
|---|
| 801 | else
|
|---|
| 802 | if(fPageCmd == SECTION_PAGES) {
|
|---|
| 803 | size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
|
|---|
| 804 | sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
|
|---|
| 805 | }
|
|---|
| 806 | //else complete section
|
|---|
| 807 |
|
|---|
| 808 | size = min(size, range);
|
|---|
| 809 | sectionsize = min(sectionsize, range);
|
|---|
| 810 |
|
|---|
| 811 | if(fileoffset != -1) {
|
|---|
| 812 | rc = DosEnterCritSec();
|
|---|
| 813 | if(rc) {
|
|---|
| 814 | dprintf((LOG, "DosEnterCritSec failed with rc %d", rc));
|
|---|
| 815 | goto fail;
|
|---|
| 816 | }
|
|---|
| 817 | rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
|---|
| 818 | if(rc) {
|
|---|
| 819 | DosExitCritSec();
|
|---|
| 820 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
|---|
| 821 | goto fail;
|
|---|
| 822 | }
|
|---|
| 823 |
|
|---|
| 824 | if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
|
|---|
| 825 | DosExitCritSec();
|
|---|
| 826 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
|
|---|
| 827 | goto fail;
|
|---|
| 828 | }
|
|---|
| 829 | #if 1
|
|---|
| 830 | // 2001-05-31 PH
|
|---|
| 831 | // ensure DosRead() does not have to read more
|
|---|
| 832 | // than 65535 bytes, otherwise split into two requests!
|
|---|
| 833 | rc = fastDosRead(hFile, (PVOID)virtAddress, size, &ulRead);
|
|---|
| 834 | #else
|
|---|
| 835 | rc = DosRead(hFile, (PVOID)virtAddress, size, &ulRead);
|
|---|
| 836 | #endif
|
|---|
| 837 | if(rc) {
|
|---|
| 838 | DosExitCritSec();
|
|---|
| 839 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x %x %x %x (rc=%d)!", virtAddress, size, ulRead, fileoffset, rc));
|
|---|
| 840 | goto fail;
|
|---|
| 841 | }
|
|---|
| 842 | if(ulRead != size) {
|
|---|
| 843 | DosExitCritSec();
|
|---|
| 844 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
|
|---|
| 845 | goto fail;
|
|---|
| 846 | }
|
|---|
| 847 | setFixups(virtAddress, sectionsize);
|
|---|
| 848 |
|
|---|
| 849 | rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
|
|---|
| 850 | DosExitCritSec();
|
|---|
| 851 | if(rc) {
|
|---|
| 852 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
|---|
| 853 | goto fail;
|
|---|
| 854 | }
|
|---|
| 855 | }
|
|---|
| 856 | else {
|
|---|
| 857 | rc = DosEnterCritSec();
|
|---|
| 858 | if(rc) {
|
|---|
| 859 | dprintf((LOG, "DosEnterCritSec failed with rc %d", rc));
|
|---|
| 860 | goto fail;
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
|---|
| 864 | if(rc) {
|
|---|
| 865 | DosExitCritSec();
|
|---|
| 866 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
|---|
| 867 | goto fail;
|
|---|
| 868 | }
|
|---|
| 869 | setFixups(virtAddress, sectionsize);
|
|---|
| 870 |
|
|---|
| 871 | rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
|
|---|
| 872 | DosExitCritSec();
|
|---|
| 873 | if(rc) {
|
|---|
| 874 | dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
|
|---|
| 875 | goto fail;
|
|---|
| 876 | }
|
|---|
| 877 | }
|
|---|
| 878 | return TRUE;
|
|---|
| 879 |
|
|---|
| 880 | fail:
|
|---|
| 881 | return FALSE;
|
|---|
| 882 | }
|
|---|
| 883 | //******************************************************************************
|
|---|
| 884 | //******************************************************************************
|
|---|
| 885 | void Win32PeLdrImage::addSection(ULONG type, ULONG rawoffset, ULONG rawsize, ULONG virtaddress, ULONG virtsize, ULONG flags)
|
|---|
| 886 | {
|
|---|
| 887 | virtsize = max(rawsize, virtsize);
|
|---|
| 888 |
|
|---|
| 889 | section[nrsections].rawoffset = rawoffset;
|
|---|
| 890 | section[nrsections].type = type;
|
|---|
| 891 | section[nrsections].rawsize = rawsize;
|
|---|
| 892 | section[nrsections].virtaddr = virtaddress;
|
|---|
| 893 | section[nrsections].flags = flags;
|
|---|
| 894 |
|
|---|
| 895 | virtsize = ((virtsize - 1) & ~0xFFF) + PAGE_SIZE;
|
|---|
| 896 | imageSize += virtsize;
|
|---|
| 897 | section[nrsections].virtualsize = virtsize;
|
|---|
| 898 |
|
|---|
| 899 | if(virtaddress < imageVirtBase)
|
|---|
| 900 | imageVirtBase = virtaddress;
|
|---|
| 901 | if(virtaddress + virtsize > imageVirtEnd)
|
|---|
| 902 | imageVirtEnd = virtaddress + virtsize;
|
|---|
| 903 |
|
|---|
| 904 | nrsections++;
|
|---|
| 905 | }
|
|---|
| 906 | //******************************************************************************
|
|---|
| 907 | //******************************************************************************
|
|---|
| 908 | BOOL Win32PeLdrImage::allocSections(ULONG reservedMem)
|
|---|
| 909 | {
|
|---|
| 910 | APIRET rc;
|
|---|
| 911 | ULONG baseAddress;
|
|---|
| 912 |
|
|---|
| 913 | realBaseAddress = 0;
|
|---|
| 914 |
|
|---|
| 915 | //Allocated in by pe.exe
|
|---|
| 916 | if(reservedMem && reservedMem == oh.ImageBase) {
|
|---|
| 917 | realBaseAddress = oh.ImageBase;
|
|---|
| 918 | return TRUE;
|
|---|
| 919 | }
|
|---|
| 920 |
|
|---|
| 921 | //SvL: We don't care where the image is loaded for resource lookup
|
|---|
| 922 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
|
|---|
| 923 | return allocFixedMem(reservedMem);
|
|---|
| 924 | }
|
|---|
| 925 | rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
|
|---|
| 926 | if(rc) {
|
|---|
| 927 | dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
|
|---|
| 928 | errorState = rc;
|
|---|
| 929 | return(FALSE);
|
|---|
| 930 | }
|
|---|
| 931 | realBaseAddress = baseAddress;
|
|---|
| 932 | return(TRUE);
|
|---|
| 933 | }
|
|---|
| 934 | //******************************************************************************
|
|---|
| 935 | //******************************************************************************
|
|---|
| 936 | Section *Win32PeLdrImage::findSection(ULONG type)
|
|---|
| 937 | {
|
|---|
| 938 | for(int i=0;i<nrsections;i++) {
|
|---|
| 939 | if(section[i].type == type) {
|
|---|
| 940 | return §ion[i];
|
|---|
| 941 | }
|
|---|
| 942 | }
|
|---|
| 943 | return NULL;
|
|---|
| 944 | }
|
|---|
| 945 | //******************************************************************************
|
|---|
| 946 | //******************************************************************************
|
|---|
| 947 | Section *Win32PeLdrImage::findSectionByAddr(ULONG addr)
|
|---|
| 948 | {
|
|---|
| 949 | for(int i=0;i<nrsections;i++) {
|
|---|
| 950 | if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
|
|---|
| 951 | return §ion[i];
|
|---|
| 952 | }
|
|---|
| 953 | }
|
|---|
| 954 | return NULL;
|
|---|
| 955 | }
|
|---|
| 956 | //******************************************************************************
|
|---|
| 957 | //******************************************************************************
|
|---|
| 958 | Section *Win32PeLdrImage::findSectionByOS2Addr(ULONG addr)
|
|---|
| 959 | {
|
|---|
| 960 | for(int i=0;i<nrsections;i++) {
|
|---|
| 961 | if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
|
|---|
| 962 | return §ion[i];
|
|---|
| 963 | }
|
|---|
| 964 | }
|
|---|
| 965 | return NULL;
|
|---|
| 966 | }
|
|---|
| 967 | //******************************************************************************
|
|---|
| 968 | //******************************************************************************
|
|---|
| 969 | Section *Win32PeLdrImage::findPreviousSectionByOS2Addr(ULONG addr)
|
|---|
| 970 | {
|
|---|
| 971 | ULONG lowestAddr = 0xffffffff;
|
|---|
| 972 | ULONG index = -1;
|
|---|
| 973 |
|
|---|
| 974 | for(int i=0;i<nrsections;i++) {
|
|---|
| 975 | if(section[i].realvirtaddr > addr) {
|
|---|
| 976 | if(section[i].realvirtaddr < lowestAddr) {
|
|---|
| 977 | lowestAddr = section[i].realvirtaddr;
|
|---|
| 978 | index = i;
|
|---|
| 979 | }
|
|---|
| 980 | }
|
|---|
| 981 | }
|
|---|
| 982 | if(index == -1)
|
|---|
| 983 | return NULL;
|
|---|
| 984 |
|
|---|
| 985 | return §ion[index];
|
|---|
| 986 | }
|
|---|
| 987 | //******************************************************************************
|
|---|
| 988 | #define FALLOC_SIZE (1024*1024)
|
|---|
| 989 | //NOTE: Needs testing (while loop)
|
|---|
| 990 | //TODO: Free unused (parts of) reservedMem
|
|---|
| 991 | //******************************************************************************
|
|---|
| 992 | BOOL Win32PeLdrImage::allocFixedMem(ULONG reservedMem)
|
|---|
| 993 | {
|
|---|
| 994 | ULONG address = 0;
|
|---|
| 995 | ULONG *memallocs;
|
|---|
| 996 | ULONG alloccnt = 0;
|
|---|
| 997 | ULONG diff, i, baseAddress;
|
|---|
| 998 | APIRET rc;
|
|---|
| 999 | BOOL allocFlags = flAllocMem;
|
|---|
| 1000 |
|
|---|
| 1001 | //Reserve enough space to store 4096 pointers to 1MB memory chunks
|
|---|
| 1002 | memallocs = (ULONG *)malloc(4096*sizeof(ULONG *));
|
|---|
| 1003 | if(memallocs == NULL) {
|
|---|
| 1004 | dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
|
|---|
| 1005 | return FALSE;
|
|---|
| 1006 | }
|
|---|
| 1007 |
|
|---|
| 1008 | if(oh.ImageBase < 512*1024*1024) {
|
|---|
| 1009 | allocFlags = 0;
|
|---|
| 1010 | }
|
|---|
| 1011 | while(TRUE) {
|
|---|
| 1012 | rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
|
|---|
| 1013 | if(rc) break;
|
|---|
| 1014 |
|
|---|
| 1015 | dprintf((LOG, "DosAllocMem returned %x", address ));
|
|---|
| 1016 | if(address + FALLOC_SIZE >= oh.ImageBase) {
|
|---|
| 1017 | if(address > oh.ImageBase) {//we've passed it!
|
|---|
| 1018 | DosFreeMem((PVOID)address);
|
|---|
| 1019 | break;
|
|---|
| 1020 | }
|
|---|
| 1021 | //found the right address
|
|---|
| 1022 | DosFreeMem((PVOID)address);
|
|---|
| 1023 |
|
|---|
| 1024 | diff = oh.ImageBase - address;
|
|---|
| 1025 | if(diff) {
|
|---|
| 1026 | rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
|
|---|
| 1027 | if(rc) break;
|
|---|
| 1028 | }
|
|---|
| 1029 | rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
|
|---|
| 1030 | if(rc) break;
|
|---|
| 1031 |
|
|---|
| 1032 | if(diff) DosFreeMem((PVOID)address);
|
|---|
| 1033 |
|
|---|
| 1034 | realBaseAddress = baseAddress;
|
|---|
| 1035 | break;
|
|---|
| 1036 | }
|
|---|
| 1037 | memallocs[alloccnt++] = address;
|
|---|
| 1038 | }
|
|---|
| 1039 | for(i=0;i<alloccnt;i++) {
|
|---|
| 1040 | DosFreeMem((PVOID)memallocs[i]);
|
|---|
| 1041 | }
|
|---|
| 1042 | free(memallocs);
|
|---|
| 1043 |
|
|---|
| 1044 | if(realBaseAddress == 0) //Let me guess.. MS Office app?
|
|---|
| 1045 | return(FALSE);
|
|---|
| 1046 |
|
|---|
| 1047 | return(TRUE);
|
|---|
| 1048 | }
|
|---|
| 1049 | //******************************************************************************
|
|---|
| 1050 | //******************************************************************************
|
|---|
| 1051 | BOOL Win32PeLdrImage::setMemFlags()
|
|---|
| 1052 | {
|
|---|
| 1053 | int i;
|
|---|
| 1054 | WINIMAGE_LOOKUP *imgLookup;
|
|---|
| 1055 |
|
|---|
| 1056 | imgLookup = WINIMAGE_LOOKUPADDR(realBaseAddress);
|
|---|
| 1057 | imgLookup->magic1 = MAGIC_WINIMAGE;
|
|---|
| 1058 | imgLookup->image = this;
|
|---|
| 1059 | imgLookup->magic2 = MAGIC_WINIMAGE;
|
|---|
| 1060 |
|
|---|
| 1061 | // Process all the image sections
|
|---|
| 1062 | for(i=0;i<nrsections;i++) {
|
|---|
| 1063 | section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
|
|---|
| 1064 | }
|
|---|
| 1065 |
|
|---|
| 1066 | for(i=0;i<nrsections;i++) {
|
|---|
| 1067 | switch(section[i].type)
|
|---|
| 1068 | {
|
|---|
| 1069 | case SECTION_CODE:
|
|---|
| 1070 | case (SECTION_CODE | SECTION_IMPORT):
|
|---|
| 1071 | section[i].pageflags = PAG_EXECUTE | PAG_READ;
|
|---|
| 1072 | if(section[i].flags & IMAGE_SCN_MEM_WRITE)
|
|---|
| 1073 | section[i].pageflags |= PAG_WRITE;
|
|---|
| 1074 | break;
|
|---|
| 1075 | case SECTION_INITDATA:
|
|---|
| 1076 | case SECTION_UNINITDATA:
|
|---|
| 1077 | case SECTION_IMPORT:
|
|---|
| 1078 | case SECTION_TLS:
|
|---|
| 1079 | section[i].pageflags = PAG_WRITE | PAG_READ;
|
|---|
| 1080 | break;
|
|---|
| 1081 |
|
|---|
| 1082 | case SECTION_RESOURCE:
|
|---|
| 1083 | //TODO: GDI32 changes some bitmap structures to avoid problems in Open32
|
|---|
| 1084 | // -> causes crashes if resource section is readonly
|
|---|
| 1085 | // -> make it readonly again when gdi32 has been rewritten
|
|---|
| 1086 | section[i].pageflags = PAG_WRITE | PAG_READ;
|
|---|
| 1087 | break;
|
|---|
| 1088 |
|
|---|
| 1089 | case SECTION_READONLYDATA:
|
|---|
| 1090 | case SECTION_EXPORT:
|
|---|
| 1091 | default:
|
|---|
| 1092 | section[i].pageflags = PAG_READ;
|
|---|
| 1093 | break;
|
|---|
| 1094 | }
|
|---|
| 1095 | if(section[i].flags & (IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
|
|---|
| 1096 | //SvL: sometimes i.e. import/export sections also contain data
|
|---|
| 1097 | // must make them read/write
|
|---|
| 1098 | section[i].pageflags = PAG_WRITE;
|
|---|
| 1099 | }
|
|---|
| 1100 | }
|
|---|
| 1101 | return(TRUE);
|
|---|
| 1102 | }
|
|---|
| 1103 | //******************************************************************************
|
|---|
| 1104 | //******************************************************************************
|
|---|
| 1105 | BOOL Win32PeLdrImage::setFixups(ULONG virtAddress, ULONG size)
|
|---|
| 1106 | {
|
|---|
| 1107 | int i, j;
|
|---|
| 1108 | char *page;
|
|---|
| 1109 | ULONG count, newpage;
|
|---|
| 1110 | Section *section;
|
|---|
| 1111 | PIMAGE_BASE_RELOCATION prel = pFixups;
|
|---|
| 1112 |
|
|---|
| 1113 | if(realBaseAddress == oh.ImageBase || fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
|---|
| 1114 | return(TRUE);
|
|---|
| 1115 | }
|
|---|
| 1116 |
|
|---|
| 1117 | virtAddress -= realBaseAddress;
|
|---|
| 1118 | //round size to next page boundary
|
|---|
| 1119 | size = (size-1) & ~0xFFF;
|
|---|
| 1120 | size += PAGE_SIZE;
|
|---|
| 1121 |
|
|---|
| 1122 | if(prel) {
|
|---|
| 1123 | j = 1;
|
|---|
| 1124 | while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
|
|---|
| 1125 | prel->VirtualAddress && prel->VirtualAddress < virtAddress)
|
|---|
| 1126 | {
|
|---|
| 1127 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
|---|
| 1128 | }
|
|---|
| 1129 | while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
|
|---|
| 1130 | prel->VirtualAddress && prel->VirtualAddress < virtAddress + size)
|
|---|
| 1131 | {
|
|---|
| 1132 | page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
|
|---|
| 1133 | count = (prel->SizeOfBlock - 8)/2;
|
|---|
| 1134 | j++;
|
|---|
| 1135 | for(i=0;i<count;i++) {
|
|---|
| 1136 | int type = prel->TypeOffset[i] >> 12;
|
|---|
| 1137 | int offset = prel->TypeOffset[i] & 0xFFF;
|
|---|
| 1138 | int fixupsize = 0;
|
|---|
| 1139 |
|
|---|
| 1140 | switch(type)
|
|---|
| 1141 | {
|
|---|
| 1142 | case IMAGE_REL_BASED_HIGHLOW:
|
|---|
| 1143 | fixupsize = 4;
|
|---|
| 1144 | break;
|
|---|
| 1145 | case IMAGE_REL_BASED_HIGH:
|
|---|
| 1146 | case IMAGE_REL_BASED_LOW:
|
|---|
| 1147 | fixupsize = 2;
|
|---|
| 1148 | break;
|
|---|
| 1149 | }
|
|---|
| 1150 | //If the fixup crosses the final page boundary,
|
|---|
| 1151 | //then we have to load another page
|
|---|
| 1152 | if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
|
|---|
| 1153 | {
|
|---|
| 1154 | newpage = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
|
|---|
| 1155 | newpage &= ~0xFFF;
|
|---|
| 1156 |
|
|---|
| 1157 | section = findSectionByOS2Addr(newpage);
|
|---|
| 1158 | if(section == NULL) {
|
|---|
| 1159 | //should never happen
|
|---|
| 1160 | dprintf((LOG, "::setFixups -> section == NULL!!"));
|
|---|
| 1161 | return FALSE;
|
|---|
| 1162 | }
|
|---|
| 1163 | //SvL: Read page from disk
|
|---|
| 1164 | commitPage(newpage, FALSE, SINGLE_PAGE);
|
|---|
| 1165 |
|
|---|
| 1166 | //SvL: Enable write access (TODO: may need to prevent other threads from being active)
|
|---|
| 1167 | DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
|
|---|
| 1168 | }
|
|---|
| 1169 |
|
|---|
| 1170 | switch(type)
|
|---|
| 1171 | {
|
|---|
| 1172 | case IMAGE_REL_BASED_ABSOLUTE:
|
|---|
| 1173 | break; //skip
|
|---|
| 1174 | case IMAGE_REL_BASED_HIGHLOW:
|
|---|
| 1175 | AddOff32Fixup(prel->VirtualAddress + offset);
|
|---|
| 1176 | break;
|
|---|
| 1177 | case IMAGE_REL_BASED_HIGH:
|
|---|
| 1178 | AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
|
|---|
| 1179 | break;
|
|---|
| 1180 | case IMAGE_REL_BASED_LOW:
|
|---|
| 1181 | AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
|
|---|
| 1182 | break;
|
|---|
| 1183 | case IMAGE_REL_BASED_HIGHADJ:
|
|---|
| 1184 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
|---|
| 1185 | default:
|
|---|
| 1186 | break;
|
|---|
| 1187 | }
|
|---|
| 1188 | if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
|
|---|
| 1189 | {
|
|---|
| 1190 | //SvL: Restore original page protection flags (TODO: may need to prevent other threads from being active)
|
|---|
| 1191 | DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
|
|---|
| 1192 | }
|
|---|
| 1193 | }
|
|---|
| 1194 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
|---|
| 1195 | }//while
|
|---|
| 1196 | }
|
|---|
| 1197 | else {
|
|---|
| 1198 | dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
|
|---|
| 1199 | return(FALSE);
|
|---|
| 1200 | }
|
|---|
| 1201 | return(TRUE);
|
|---|
| 1202 | }
|
|---|
| 1203 | //******************************************************************************
|
|---|
| 1204 | //******************************************************************************
|
|---|
| 1205 | BOOL Win32PeLdrImage::setFixups(PIMAGE_BASE_RELOCATION prel)
|
|---|
| 1206 | {
|
|---|
| 1207 | int i, j;
|
|---|
| 1208 | char *page;
|
|---|
| 1209 | ULONG count;
|
|---|
| 1210 |
|
|---|
| 1211 | if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
|
|---|
| 1212 | return(TRUE);
|
|---|
| 1213 | }
|
|---|
| 1214 |
|
|---|
| 1215 | if(prel) {
|
|---|
| 1216 | j = 1;
|
|---|
| 1217 | while(prel->VirtualAddress) {
|
|---|
| 1218 | page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
|
|---|
| 1219 | count = (prel->SizeOfBlock - 8)/2;
|
|---|
| 1220 | dprintf((LOG, "Page %d Address %x Count %d", j, prel->VirtualAddress, count ));
|
|---|
| 1221 | j++;
|
|---|
| 1222 | for(i=0;i<count;i++) {
|
|---|
| 1223 | int type = prel->TypeOffset[i] >> 12;
|
|---|
| 1224 | int offset = prel->TypeOffset[i] & 0xFFF;
|
|---|
| 1225 | switch(type) {
|
|---|
| 1226 | case IMAGE_REL_BASED_ABSOLUTE:
|
|---|
| 1227 | //// dprintf((LOG, "absolute fixup; unused" ));
|
|---|
| 1228 | break; //skip
|
|---|
| 1229 | case IMAGE_REL_BASED_HIGHLOW:
|
|---|
| 1230 | //// dprintf((LOG, "address ", offset << " type ", type ));
|
|---|
| 1231 | AddOff32Fixup(prel->VirtualAddress + offset);
|
|---|
| 1232 | break;
|
|---|
| 1233 | case IMAGE_REL_BASED_HIGH:
|
|---|
| 1234 | AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
|
|---|
| 1235 | break;
|
|---|
| 1236 | case IMAGE_REL_BASED_LOW:
|
|---|
| 1237 | AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
|
|---|
| 1238 | break;
|
|---|
| 1239 | case IMAGE_REL_BASED_HIGHADJ:
|
|---|
| 1240 | case IMAGE_REL_BASED_MIPS_JMPADDR:
|
|---|
| 1241 | default:
|
|---|
| 1242 | dprintf((LOG, "Unknown/unsupported fixup type!" ));
|
|---|
| 1243 | break;
|
|---|
| 1244 | }
|
|---|
| 1245 | }
|
|---|
| 1246 | prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
|
|---|
| 1247 | }//while
|
|---|
| 1248 | }
|
|---|
| 1249 | else {
|
|---|
| 1250 | dprintf((LOG, "No internal fixups found!" ));
|
|---|
| 1251 | errorState = ERROR_INTERNAL;
|
|---|
| 1252 | return(FALSE);
|
|---|
| 1253 | }
|
|---|
| 1254 | return(TRUE);
|
|---|
| 1255 | }
|
|---|
| 1256 | //******************************************************************************
|
|---|
| 1257 | //******************************************************************************
|
|---|
| 1258 | void Win32PeLdrImage::AddOff32Fixup(ULONG fixupaddr)
|
|---|
| 1259 | {
|
|---|
| 1260 | ULONG orgaddr;
|
|---|
| 1261 | ULONG *fixup;
|
|---|
| 1262 |
|
|---|
| 1263 | fixup = (ULONG *)(fixupaddr + realBaseAddress);
|
|---|
| 1264 | orgaddr = *fixup;
|
|---|
| 1265 | // dprintf((LOG, "AddOff32Fixup 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, realBaseAddress + (*fixup - oh.ImageBase)));
|
|---|
| 1266 | *fixup = realBaseAddress + (*fixup - oh.ImageBase);
|
|---|
| 1267 | }
|
|---|
| 1268 | //******************************************************************************
|
|---|
| 1269 | //******************************************************************************
|
|---|
| 1270 | void Win32PeLdrImage::AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup)
|
|---|
| 1271 | {
|
|---|
| 1272 | ULONG orgaddr;
|
|---|
| 1273 | USHORT *fixup;
|
|---|
| 1274 |
|
|---|
| 1275 | fixup = (USHORT *)(fixupaddr + realBaseAddress);
|
|---|
| 1276 | orgaddr = *fixup;
|
|---|
| 1277 | if(fHighFixup) {
|
|---|
| 1278 | *fixup += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
|
|---|
| 1279 | // dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
|
|---|
| 1280 | }
|
|---|
| 1281 | else {
|
|---|
| 1282 | *fixup += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
|
|---|
| 1283 | // dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
|
|---|
| 1284 | }
|
|---|
| 1285 | }
|
|---|
| 1286 | //******************************************************************************
|
|---|
| 1287 | //******************************************************************************
|
|---|
| 1288 | void Win32PeLdrImage::StoreImportByOrd(Win32ImageBase *WinImage, ULONG ordinal, ULONG impaddr)
|
|---|
| 1289 | {
|
|---|
| 1290 | ULONG *import;
|
|---|
| 1291 | ULONG apiaddr;
|
|---|
| 1292 |
|
|---|
| 1293 | import = (ULONG *)impaddr;
|
|---|
| 1294 | apiaddr = WinImage->getApi(ordinal);
|
|---|
| 1295 | if(apiaddr == 0)
|
|---|
| 1296 | {
|
|---|
| 1297 | dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
|
|---|
| 1298 | WinImage->getModuleName(),
|
|---|
| 1299 | ordinal));
|
|---|
| 1300 |
|
|---|
| 1301 | dprintf((LOG, "--->>> NOT FOUND!" ));
|
|---|
| 1302 | *import = (ULONG)MissingApi;
|
|---|
| 1303 | }
|
|---|
| 1304 | else *import = apiaddr;
|
|---|
| 1305 | }
|
|---|
| 1306 | //******************************************************************************
|
|---|
| 1307 | //******************************************************************************
|
|---|
| 1308 | void Win32PeLdrImage::StoreImportByName(Win32ImageBase *WinImage, char *impname, ULONG impaddr)
|
|---|
| 1309 | {
|
|---|
| 1310 | ULONG *import;
|
|---|
| 1311 | ULONG apiaddr;
|
|---|
| 1312 |
|
|---|
| 1313 | import = (ULONG *)impaddr;
|
|---|
| 1314 | apiaddr = WinImage->getApi(impname);
|
|---|
| 1315 | if(apiaddr == 0)
|
|---|
| 1316 | {
|
|---|
| 1317 | dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
|
|---|
| 1318 | WinImage->getModuleName(),
|
|---|
| 1319 | impname));
|
|---|
| 1320 |
|
|---|
| 1321 | dprintf((LOG, "--->>> NOT FOUND!" ));
|
|---|
| 1322 | *import = (ULONG)MissingApi;
|
|---|
| 1323 | }
|
|---|
| 1324 | else *import = apiaddr;
|
|---|
| 1325 | }
|
|---|
| 1326 | //******************************************************************************
|
|---|
| 1327 | //******************************************************************************
|
|---|
| 1328 | BOOL Win32PeLdrImage::processExports(char *win32file)
|
|---|
| 1329 | {
|
|---|
| 1330 | IMAGE_SECTION_HEADER sh;
|
|---|
| 1331 | PIMAGE_EXPORT_DIRECTORY ped;
|
|---|
| 1332 | ULONG *ptrNames, *ptrAddress;
|
|---|
| 1333 | USHORT *ptrOrd;
|
|---|
| 1334 | BOOL fForwarder;
|
|---|
| 1335 | int i;
|
|---|
| 1336 |
|
|---|
| 1337 | /* get section header and pointer to data directory for .edata section */
|
|---|
| 1338 | if((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
|
|---|
| 1339 | (win32file, IMAGE_DIRECTORY_ENTRY_EXPORT)) != NULL &&
|
|---|
| 1340 | GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_EXPORT, &sh) ) {
|
|---|
| 1341 |
|
|---|
| 1342 | dprintf((LOG, "Exported Functions: " ));
|
|---|
| 1343 | ptrOrd = (USHORT *)((ULONG)ped->AddressOfNameOrdinals +
|
|---|
| 1344 | (ULONG)win32file);
|
|---|
| 1345 | ptrNames = (ULONG *)((ULONG)ped->AddressOfNames +
|
|---|
| 1346 | (ULONG)win32file);
|
|---|
| 1347 | ptrAddress = (ULONG *)((ULONG)ped->AddressOfFunctions +
|
|---|
| 1348 | (ULONG)win32file);
|
|---|
| 1349 | nrOrdExports = ped->NumberOfFunctions;
|
|---|
| 1350 | nrNameExports = ped->NumberOfNames;
|
|---|
| 1351 |
|
|---|
| 1352 | int ord, RVAExport;
|
|---|
| 1353 | char *name;
|
|---|
| 1354 | for(i=0;i<ped->NumberOfNames;i++)
|
|---|
| 1355 | {
|
|---|
| 1356 | fForwarder = FALSE;
|
|---|
| 1357 | ord = ptrOrd[i] + ped->Base;
|
|---|
| 1358 | name = (char *)((ULONG)ptrNames[i] + (ULONG)win32file);
|
|---|
| 1359 | RVAExport = ptrAddress[ptrOrd[i]];
|
|---|
| 1360 |
|
|---|
| 1361 | /* forwarder? ulRVA within export directory. */
|
|---|
| 1362 | if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
|
|---|
| 1363 | RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
|---|
| 1364 | + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
|---|
| 1365 | {
|
|---|
| 1366 | fForwarder = AddForwarder(oh.ImageBase + RVAExport, name, ord);
|
|---|
| 1367 | }
|
|---|
| 1368 | if(!fForwarder) {
|
|---|
| 1369 | //points to code (virtual address relative to oh.ImageBase
|
|---|
| 1370 | AddNameExport(oh.ImageBase + RVAExport, name, ord);
|
|---|
| 1371 | dprintf((LOG, "address 0x%x %s @%d (0x%08x)", RVAExport, name, ord, realBaseAddress + RVAExport));
|
|---|
| 1372 | }
|
|---|
| 1373 | }
|
|---|
| 1374 | for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++)
|
|---|
| 1375 | {
|
|---|
| 1376 | fForwarder = FALSE;
|
|---|
| 1377 | ord = ped->Base + i; //Correct??
|
|---|
| 1378 | RVAExport = ptrAddress[i];
|
|---|
| 1379 | /* forwarder? ulRVA within export directory. */
|
|---|
| 1380 | if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
|
|---|
| 1381 | RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
|
|---|
| 1382 | + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
|
|---|
| 1383 | {
|
|---|
| 1384 | fForwarder = AddForwarder(oh.ImageBase + RVAExport, NULL, ord);
|
|---|
| 1385 | }
|
|---|
| 1386 | if(!fForwarder && RVAExport) {
|
|---|
| 1387 | //points to code (virtual address relative to oh.ImageBase
|
|---|
| 1388 | dprintf((LOG, "ord %d at 0x%08x (0x%08x)", ord, RVAExport, realBaseAddress + RVAExport));
|
|---|
| 1389 | AddOrdExport(oh.ImageBase + RVAExport, ord);
|
|---|
| 1390 | }
|
|---|
| 1391 | }
|
|---|
| 1392 | }
|
|---|
| 1393 |
|
|---|
| 1394 | return(TRUE);
|
|---|
| 1395 | }
|
|---|
| 1396 | //******************************************************************************
|
|---|
| 1397 | //******************************************************************************
|
|---|
| 1398 | void Win32PeLdrImage::AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal, BOOL fAbsoluteAddress)
|
|---|
| 1399 | {
|
|---|
| 1400 | ULONG nsize;
|
|---|
| 1401 | int iApiNameLength = strlen(apiname);
|
|---|
| 1402 |
|
|---|
| 1403 | if(nameexports == NULL) {
|
|---|
| 1404 | // think of a maximum of bytes per export name,
|
|---|
| 1405 | // verify if this is true for MFC-DLLs, etc.
|
|---|
| 1406 | nameExportSize = nrNameExports * (sizeof(NameExport) + 32);
|
|---|
| 1407 |
|
|---|
| 1408 | nameexports = (NameExport *)malloc(nameExportSize);
|
|---|
| 1409 | curnameexport = nameexports;
|
|---|
| 1410 | }
|
|---|
| 1411 | nsize = (ULONG)curnameexport - (ULONG)nameexports;
|
|---|
| 1412 | if(nsize + sizeof(NameExport) + iApiNameLength > nameExportSize) {
|
|---|
| 1413 | nameExportSize += 4096;
|
|---|
| 1414 | char *tmp = (char *)nameexports;
|
|---|
| 1415 | nameexports = (NameExport *)malloc(nameExportSize);
|
|---|
| 1416 | memcpy(nameexports, tmp, nsize);
|
|---|
| 1417 | curnameexport = (NameExport *)((ULONG)nameexports + nsize);
|
|---|
| 1418 | free(tmp);
|
|---|
| 1419 | }
|
|---|
| 1420 | if(fAbsoluteAddress) {//forwarders use absolute address
|
|---|
| 1421 | curnameexport->virtaddr = virtaddr;
|
|---|
| 1422 | }
|
|---|
| 1423 | else curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
|
|---|
| 1424 | curnameexport->ordinal = ordinal;
|
|---|
| 1425 | *(ULONG *)curnameexport->name = 0;
|
|---|
| 1426 |
|
|---|
| 1427 | curnameexport->nlength = iApiNameLength + 1;
|
|---|
| 1428 | memcpy(curnameexport->name, apiname, curnameexport->nlength);
|
|---|
| 1429 |
|
|---|
| 1430 | if(curnameexport->nlength < sizeof(curnameexport->name))
|
|---|
| 1431 | curnameexport->nlength = sizeof(curnameexport->name);
|
|---|
| 1432 |
|
|---|
| 1433 | curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
|
|---|
| 1434 | }
|
|---|
| 1435 | //******************************************************************************
|
|---|
| 1436 | //******************************************************************************
|
|---|
| 1437 | void Win32PeLdrImage::AddOrdExport(ULONG virtaddr, ULONG ordinal, BOOL fAbsoluteAddress)
|
|---|
| 1438 | {
|
|---|
| 1439 | if(ordexports == NULL) {
|
|---|
| 1440 | ordexports = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
|
|---|
| 1441 | curordexport = ordexports;
|
|---|
| 1442 | }
|
|---|
| 1443 | if(fAbsoluteAddress) {//forwarders use absolute address
|
|---|
| 1444 | curordexport->virtaddr = virtaddr;
|
|---|
| 1445 | }
|
|---|
| 1446 | else curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
|
|---|
| 1447 |
|
|---|
| 1448 | curordexport->ordinal = ordinal;
|
|---|
| 1449 | curordexport++;
|
|---|
| 1450 | nrOrdExportsRegistered++;
|
|---|
| 1451 | }
|
|---|
| 1452 | //******************************************************************************
|
|---|
| 1453 | //******************************************************************************
|
|---|
| 1454 | BOOL Win32PeLdrImage::AddForwarder(ULONG virtaddr, char *apiname, ULONG ordinal)
|
|---|
| 1455 | {
|
|---|
| 1456 | char *forward = (char *)(realBaseAddress + (virtaddr - oh.ImageBase));
|
|---|
| 1457 | char *forwarddll, *forwardapi;
|
|---|
| 1458 | Win32DllBase *WinDll;
|
|---|
| 1459 | DWORD exportaddr;
|
|---|
| 1460 | int forwardord;
|
|---|
| 1461 | int iForwardDllLength = strlen(forward);
|
|---|
| 1462 | int iForwardApiLength;
|
|---|
| 1463 |
|
|---|
| 1464 | if(iForwardDllLength == 0)
|
|---|
| 1465 | return FALSE;
|
|---|
| 1466 |
|
|---|
| 1467 | forwarddll = (char*)alloca(iForwardDllLength);
|
|---|
| 1468 | if(forwarddll == NULL) {
|
|---|
| 1469 | DebugInt3();
|
|---|
| 1470 | return FALSE;
|
|---|
| 1471 | }
|
|---|
| 1472 | memcpy(forwarddll, forward, iForwardDllLength + 1);
|
|---|
| 1473 |
|
|---|
| 1474 | forwardapi = strchr(forwarddll, '.');
|
|---|
| 1475 | if(forwardapi == NULL) {
|
|---|
| 1476 | return FALSE;
|
|---|
| 1477 | }
|
|---|
| 1478 | *forwardapi++ = 0;
|
|---|
| 1479 | iForwardApiLength = strlen(forwardapi);
|
|---|
| 1480 | if(iForwardApiLength == 0) {
|
|---|
| 1481 | return FALSE;
|
|---|
| 1482 | }
|
|---|
| 1483 | WinDll = Win32DllBase::findModule(forwarddll);
|
|---|
| 1484 | if(WinDll == NULL) {
|
|---|
| 1485 | WinDll = loadDll(forwarddll);
|
|---|
| 1486 | if(WinDll == NULL) {
|
|---|
| 1487 | dprintf((LOG, "ERROR: couldn't find forwarder %s.%s", forwarddll, forwardapi));
|
|---|
| 1488 | return FALSE;
|
|---|
| 1489 | }
|
|---|
| 1490 | }
|
|---|
| 1491 | //check if name or ordinal forwarder
|
|---|
| 1492 | forwardord = 0;
|
|---|
| 1493 | if(*forwardapi >= '0' && *forwardapi <= '9') {
|
|---|
| 1494 | forwardord = atoi(forwardapi);
|
|---|
| 1495 | }
|
|---|
| 1496 | if(forwardord != 0 || (iForwardApiLength == 1 && *forwardapi == '0')) {
|
|---|
| 1497 | exportaddr = WinDll->getApi(forwardord);
|
|---|
| 1498 | }
|
|---|
| 1499 | else exportaddr = WinDll->getApi(forwardapi);
|
|---|
| 1500 |
|
|---|
| 1501 | if(apiname) {
|
|---|
| 1502 | dprintf((LOG, "address 0x%x %s @%d (0x%08x) forwarder %s.%s", virtaddr - oh.ImageBase, apiname, ordinal, virtaddr, forwarddll, forwardapi));
|
|---|
| 1503 | AddNameExport(exportaddr, apiname, ordinal, TRUE);
|
|---|
| 1504 | }
|
|---|
| 1505 | else {
|
|---|
| 1506 | dprintf((LOG, "address 0x%x @%d (0x%08x) forwarder %s.%s", virtaddr - oh.ImageBase, ordinal, virtaddr, forwarddll, forwardapi));
|
|---|
| 1507 | AddOrdExport(exportaddr, ordinal, TRUE);
|
|---|
| 1508 | }
|
|---|
| 1509 | return TRUE;
|
|---|
| 1510 | }
|
|---|
| 1511 | //******************************************************************************
|
|---|
| 1512 | //******************************************************************************
|
|---|
| 1513 | Win32DllBase *Win32PeLdrImage::loadDll(char *pszCurModule)
|
|---|
| 1514 | {
|
|---|
| 1515 | Win32DllBase *WinDll = NULL;
|
|---|
| 1516 | char modname[CCHMAXPATH];
|
|---|
| 1517 |
|
|---|
| 1518 | strcpy(modname, pszCurModule);
|
|---|
| 1519 |
|
|---|
| 1520 | //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
|
|---|
| 1521 | Win32DllBase::renameDll(modname);
|
|---|
| 1522 |
|
|---|
| 1523 | char szModName2[CCHMAXPATH];
|
|---|
| 1524 | strcpy(szModName2, modname);
|
|---|
| 1525 | if (!Win32ImageBase::findDll(szModName2, modname, sizeof(modname)))
|
|---|
| 1526 | {
|
|---|
| 1527 | dprintf((LOG, "Module %s not found!", modname));
|
|---|
| 1528 | sprintf(szErrorModule, "%s", modname);
|
|---|
| 1529 | errorState = 2;
|
|---|
| 1530 | return NULL;
|
|---|
| 1531 | }
|
|---|
| 1532 |
|
|---|
| 1533 | if(isPEImage(modname, NULL, NULL) != ERROR_SUCCESS_W)
|
|---|
| 1534 | {//LX image, so let OS/2 do all the work for us
|
|---|
| 1535 | APIRET rc;
|
|---|
| 1536 | char szModuleFailure[CCHMAXPATH] = "";
|
|---|
| 1537 | ULONG hInstanceNewDll;
|
|---|
| 1538 | Win32LxDll *lxdll;
|
|---|
| 1539 |
|
|---|
| 1540 | char *dot = strchr(modname, '.');
|
|---|
| 1541 | if(dot == NULL) {
|
|---|
| 1542 | strcat(modname, DLL_EXTENSION);
|
|---|
| 1543 | }
|
|---|
| 1544 | rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
|
|---|
| 1545 | if(rc) {
|
|---|
| 1546 | dprintf((LOG, "DosLoadModule returned %X for %s", rc, szModuleFailure));
|
|---|
| 1547 | sprintf(szErrorModule, "%s", szModuleFailure);
|
|---|
| 1548 | errorState = rc;
|
|---|
| 1549 | return NULL;
|
|---|
| 1550 | }
|
|---|
| 1551 | lxdll = Win32LxDll::findModuleByOS2Handle(hInstanceNewDll);
|
|---|
| 1552 | if(lxdll == NULL) {//shouldn't happen!
|
|---|
| 1553 | dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
|
|---|
| 1554 | errorState = ERROR_INTERNAL;
|
|---|
| 1555 | return NULL;
|
|---|
| 1556 | }
|
|---|
| 1557 | lxdll->setDllHandleOS2(hInstanceNewDll);
|
|---|
| 1558 | if(lxdll->AddRef() == -1) {//-1 -> load failed (attachProcess)
|
|---|
| 1559 | dprintf((LOG, "Dll %s refused to be loaded; aborting", modname));
|
|---|
| 1560 | delete lxdll;
|
|---|
| 1561 | errorState = ERROR_INTERNAL;
|
|---|
| 1562 | return NULL;
|
|---|
| 1563 | }
|
|---|
| 1564 | WinDll = (Win32DllBase*)lxdll;
|
|---|
| 1565 | }
|
|---|
| 1566 | else {
|
|---|
| 1567 | Win32PeLdrDll *pedll;
|
|---|
| 1568 |
|
|---|
| 1569 | pedll = new Win32PeLdrDll(modname, this);
|
|---|
| 1570 | if(pedll == NULL) {
|
|---|
| 1571 | dprintf((LOG, "pedll: Error allocating memory" ));
|
|---|
| 1572 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 1573 | errorState = ERROR_INTERNAL;
|
|---|
| 1574 | return NULL;
|
|---|
| 1575 | }
|
|---|
| 1576 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1577 | dprintf((LOG, "********************** Loading Module *********************" ));
|
|---|
| 1578 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1579 | if(pedll->init(0) == FALSE) {
|
|---|
| 1580 | dprintf((LOG, "Internal WinDll error ", pedll->getError() ));
|
|---|
| 1581 | delete pedll;
|
|---|
| 1582 | return NULL;
|
|---|
| 1583 | }
|
|---|
| 1584 | #ifdef DEBUG
|
|---|
| 1585 | pedll->AddRef(getModuleName());
|
|---|
| 1586 | #else
|
|---|
| 1587 | pedll->AddRef();
|
|---|
| 1588 | #endif
|
|---|
| 1589 | if(pedll->attachProcess() == FALSE) {
|
|---|
| 1590 | dprintf((LOG, "attachProcess failed!" ));
|
|---|
| 1591 | delete pedll;
|
|---|
| 1592 | errorState = ERROR_INTERNAL;
|
|---|
| 1593 | return NULL;
|
|---|
| 1594 | }
|
|---|
| 1595 | WinDll = (Win32DllBase*)pedll;
|
|---|
| 1596 | }
|
|---|
| 1597 |
|
|---|
| 1598 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1599 | dprintf((LOG, "********************** Finished Loading Module %s ", modname ));
|
|---|
| 1600 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1601 |
|
|---|
| 1602 | return WinDll;
|
|---|
| 1603 | }
|
|---|
| 1604 |
|
|---|
| 1605 | //******************************************************************************
|
|---|
| 1606 | /** All initial processing of imports is done here
|
|---|
| 1607 | * Should now detect most Borland styled files including the GifCon32.exe and
|
|---|
| 1608 | * loader32 from SoftIce. (Stupid Borland!!!)
|
|---|
| 1609 | *
|
|---|
| 1610 | * knut [Jul 22 1998 2:44am]
|
|---|
| 1611 | **/
|
|---|
| 1612 | //******************************************************************************
|
|---|
| 1613 | BOOL Win32PeLdrImage::processImports(char *win32file)
|
|---|
| 1614 | {
|
|---|
| 1615 | PIMAGE_IMPORT_DESCRIPTOR pID;
|
|---|
| 1616 | IMAGE_SECTION_HEADER shID;
|
|---|
| 1617 | IMAGE_SECTION_HEADER shExtra = {0};
|
|---|
| 1618 | PIMAGE_OPTIONAL_HEADER pOH;
|
|---|
| 1619 | int i,j, nrPages;
|
|---|
| 1620 | BOOL fBorland = 0;
|
|---|
| 1621 | int cModules;
|
|---|
| 1622 | char *pszModules;
|
|---|
| 1623 | char *pszCurModule;
|
|---|
| 1624 | char *pszTmp;
|
|---|
| 1625 | ULONG *pulImport;
|
|---|
| 1626 | ULONG ulCurFixup;
|
|---|
| 1627 | int Size;
|
|---|
| 1628 | Win32DllBase *WinDll;
|
|---|
| 1629 | Win32ImageBase *WinImage = NULL;
|
|---|
| 1630 | Section *section;
|
|---|
| 1631 |
|
|---|
| 1632 | /* "algorithm:"
|
|---|
| 1633 | * 1) get module names and store them
|
|---|
| 1634 | * a) check dwRVAModuleName is within .idata seg - if not find section
|
|---|
| 1635 | * 2) iterate thru functions of each module
|
|---|
| 1636 | * a) check OriginalFirstThunk is not 0 and that it points to a RVA.
|
|---|
| 1637 | * b) if not a) borland-styled PE-file - ARG!!!
|
|---|
| 1638 | * check FirstThunk
|
|---|
| 1639 | * c) check OriginalFirstThunk/FirstThunk ok RVAs and find right section
|
|---|
| 1640 | * d) store ordinal/name import
|
|---|
| 1641 | * 3) finished
|
|---|
| 1642 | */
|
|---|
| 1643 |
|
|---|
| 1644 | /* 1) get module names */
|
|---|
| 1645 | pID = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT);
|
|---|
| 1646 | if (pID == NULL)
|
|---|
| 1647 | return TRUE;
|
|---|
| 1648 | if (!GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT, &shID))
|
|---|
| 1649 | return TRUE;
|
|---|
| 1650 |
|
|---|
| 1651 | //calc size of module list
|
|---|
| 1652 | i = Size = cModules = 0;
|
|---|
| 1653 | while (pID[i].Name != 0)
|
|---|
| 1654 | {
|
|---|
| 1655 | //test RVA inside ID-Section
|
|---|
| 1656 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
|
|---|
| 1657 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
|---|
| 1658 | }
|
|---|
| 1659 | else {
|
|---|
| 1660 | //is the "Extra"-section already found or do we have to find it?
|
|---|
| 1661 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData)) {
|
|---|
| 1662 | if (!GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name))
|
|---|
| 1663 | return FALSE;
|
|---|
| 1664 | }
|
|---|
| 1665 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
|---|
| 1666 | }
|
|---|
| 1667 | Size += strlen(pszTmp) + 1;
|
|---|
| 1668 | i++;
|
|---|
| 1669 | cModules++;
|
|---|
| 1670 | }
|
|---|
| 1671 |
|
|---|
| 1672 | pszModules = (char*)alloca(Size);
|
|---|
| 1673 | if(pszModules == NULL) {
|
|---|
| 1674 | DebugInt3();
|
|---|
| 1675 | return FALSE;
|
|---|
| 1676 | }
|
|---|
| 1677 | j = 0;
|
|---|
| 1678 | for (i = 0; i < cModules; i++)
|
|---|
| 1679 | {
|
|---|
| 1680 | //test RVA inside ID-Section
|
|---|
| 1681 | if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
|
|---|
| 1682 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
|---|
| 1683 | }
|
|---|
| 1684 | else {
|
|---|
| 1685 | fBorland = TRUE;
|
|---|
| 1686 | //is the "Extra"-section already found or do we have to find it?
|
|---|
| 1687 | if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 1688 | {
|
|---|
| 1689 | if (GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name)) {
|
|---|
| 1690 | return FALSE;
|
|---|
| 1691 | }
|
|---|
| 1692 | }
|
|---|
| 1693 | pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
|
|---|
| 1694 | }
|
|---|
| 1695 |
|
|---|
| 1696 | int iTmpLength = strlen(pszTmp) + 1;
|
|---|
| 1697 | memcpy(pszModules+j, pszTmp, iTmpLength);
|
|---|
| 1698 | j += iTmpLength;
|
|---|
| 1699 | }
|
|---|
| 1700 | if (fBorland)
|
|---|
| 1701 | dprintf((LOG, "Borland-styled PE-File." ));
|
|---|
| 1702 |
|
|---|
| 1703 | //Store modules
|
|---|
| 1704 | dprintf((LOG, "%d imported Modules: ", cModules ));
|
|---|
| 1705 |
|
|---|
| 1706 | /* 2) functions */
|
|---|
| 1707 | pszCurModule = pszModules;
|
|---|
| 1708 | pOH = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(win32file);
|
|---|
| 1709 | for (i = 0; i < cModules; i++)
|
|---|
| 1710 | {
|
|---|
| 1711 | dprintf((LOG, "Module %s", pszCurModule ));
|
|---|
| 1712 | if(pID[i].ForwarderChain) {
|
|---|
| 1713 | dprintf((LOG, "ForwarderChain: %x", pID[i].ForwarderChain));
|
|---|
| 1714 | }
|
|---|
| 1715 | // a) check that OriginalFirstThunk not is 0 and look for Borland-styled PE
|
|---|
| 1716 | if (i == 0)
|
|---|
| 1717 | {
|
|---|
| 1718 | //heavy borland-style test - assume array of thunks is within that style does not change
|
|---|
| 1719 | if((ULONG)pID[i].u.OriginalFirstThunk == 0 ||
|
|---|
| 1720 | (ULONG)pID[i].u.OriginalFirstThunk < shID.VirtualAddress ||
|
|---|
| 1721 | (ULONG)pID[i].u.OriginalFirstThunk >= shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData) ||
|
|---|
| 1722 | (ULONG)pID[i].u.OriginalFirstThunk >= pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress &&
|
|---|
| 1723 | (ULONG)pID[i].u.OriginalFirstThunk < sizeof(*pID)*cModules + pOH->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
|
|---|
| 1724 | {
|
|---|
| 1725 | fBorland = TRUE;
|
|---|
| 1726 | }
|
|---|
| 1727 | }
|
|---|
| 1728 | //light borland-style test
|
|---|
| 1729 | if (pID[i].u.OriginalFirstThunk == 0 || fBorland) {
|
|---|
| 1730 | pulImport = (ULONG*)pID[i].FirstThunk;
|
|---|
| 1731 | }
|
|---|
| 1732 | else pulImport = (ULONG*)pID[i].u.OriginalFirstThunk;
|
|---|
| 1733 |
|
|---|
| 1734 | // b) check if RVA ok
|
|---|
| 1735 | if (!(pulImport > 0 && (ULONG)pulImport < pOH->SizeOfImage)) {
|
|---|
| 1736 | dprintf((LOG, "Invalid RVA %x", pulImport ));
|
|---|
| 1737 | break;
|
|---|
| 1738 | }
|
|---|
| 1739 | // check section
|
|---|
| 1740 | if ((ULONG)pulImport < shExtra.VirtualAddress || (ULONG)pulImport >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 1741 | {
|
|---|
| 1742 | if (!GetSectionHdrByRVA(win32file, &shExtra, (ULONG)pulImport))
|
|---|
| 1743 | {
|
|---|
| 1744 | dprintf((LOG, "warning: could not find section for Thunk RVA %x", pulImport ));
|
|---|
| 1745 | break;
|
|---|
| 1746 | }
|
|---|
| 1747 | }
|
|---|
| 1748 |
|
|---|
| 1749 | //Load dll if needed
|
|---|
| 1750 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1751 | dprintf((LOG, "************** Import Module %s ", pszCurModule ));
|
|---|
| 1752 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1753 | WinDll = Win32DllBase::findModule(pszCurModule);
|
|---|
| 1754 |
|
|---|
| 1755 | if(WinDll == NULL)
|
|---|
| 1756 | { //not found, so load it
|
|---|
| 1757 | if (WinExe != NULL && WinExe->matchModName(pszCurModule)) {
|
|---|
| 1758 | WinImage = (Win32ImageBase *)WinExe;
|
|---|
| 1759 | }
|
|---|
| 1760 | else {
|
|---|
| 1761 | WinDll = loadDll(pszCurModule);
|
|---|
| 1762 | if(WinDll == NULL) {
|
|---|
| 1763 | return FALSE;
|
|---|
| 1764 | }
|
|---|
| 1765 | }
|
|---|
| 1766 | }
|
|---|
| 1767 | else {
|
|---|
| 1768 | WinDll->AddRef();
|
|---|
| 1769 | dprintf((LOG, "Already found ", pszCurModule));
|
|---|
| 1770 | }
|
|---|
| 1771 | if(WinDll != NULL) {
|
|---|
| 1772 | //add the dll we just loaded to dependency list for this image
|
|---|
| 1773 | addDependency(WinDll);
|
|---|
| 1774 |
|
|---|
| 1775 | //Make sure the dependency list is correct (already done
|
|---|
| 1776 | //in the ctor of Win32DllBase, but for LX dlls the parent is
|
|---|
| 1777 | //then set to NULL; so change it here again
|
|---|
| 1778 | WinDll->setUnloadOrder(this);
|
|---|
| 1779 | WinImage = (Win32ImageBase *)WinDll;
|
|---|
| 1780 | }
|
|---|
| 1781 | else
|
|---|
| 1782 | if(WinImage == NULL) {
|
|---|
| 1783 | dprintf((LOG, "Unable to load dll %s", pszCurModule ));
|
|---|
| 1784 | return FALSE;
|
|---|
| 1785 | }
|
|---|
| 1786 |
|
|---|
| 1787 | pulImport = (PULONG)((ULONG)pulImport + (ULONG)win32file);
|
|---|
| 1788 | j = 0;
|
|---|
| 1789 | ulCurFixup = (ULONG)pID[i].FirstThunk + (ULONG)win32file;
|
|---|
| 1790 |
|
|---|
| 1791 | section = findSectionByOS2Addr(ulCurFixup);
|
|---|
| 1792 | if(section == NULL) {
|
|---|
| 1793 | dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
|
|---|
| 1794 | return FALSE;
|
|---|
| 1795 | }
|
|---|
| 1796 | //Read page from disk
|
|---|
| 1797 | commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
|
|---|
| 1798 | //Enable write access
|
|---|
| 1799 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
|
|---|
| 1800 | nrPages = 1;
|
|---|
| 1801 |
|
|---|
| 1802 | while (pulImport[j] != 0) {
|
|---|
| 1803 | if (pulImport[j] & IMAGE_ORDINAL_FLAG) { //ordinal
|
|---|
| 1804 | dprintf((LOG, "0x%08x Imported function %s @%d", ulCurFixup , pszCurModule, (pulImport[j] & ~IMAGE_ORDINAL_FLAG) ));
|
|---|
| 1805 | StoreImportByOrd(WinImage, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);
|
|---|
| 1806 | }
|
|---|
| 1807 | else { //name
|
|---|
| 1808 | //check
|
|---|
| 1809 | if (pulImport[j] < shExtra.VirtualAddress || pulImport[j] >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
|
|---|
| 1810 | {
|
|---|
| 1811 | if (!GetSectionHdrByRVA(win32file, &shExtra, pulImport[j]))
|
|---|
| 1812 | {
|
|---|
| 1813 | dprintf((LOG, "warning: could not find section for Import Name RVA ", pulImport[j] ));
|
|---|
| 1814 | break;
|
|---|
| 1815 | }
|
|---|
| 1816 | }
|
|---|
| 1817 | //KSO - Aug 6 1998 1:15am:this eases comparing...
|
|---|
| 1818 | char *pszFunctionName = (char*)(pulImport[j] + (ULONG)win32file + 2);
|
|---|
| 1819 | dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup, pszFunctionName, WinImage->getApi(pszFunctionName)));
|
|---|
| 1820 | StoreImportByName(WinImage, pszFunctionName, ulCurFixup);
|
|---|
| 1821 | }
|
|---|
| 1822 | ulCurFixup += sizeof(IMAGE_THUNK_DATA);
|
|---|
| 1823 | j++;
|
|---|
| 1824 | if((ulCurFixup & 0xfff) == 0) {
|
|---|
| 1825 | commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
|
|---|
| 1826 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
|
|---|
| 1827 | nrPages++;
|
|---|
| 1828 | }
|
|---|
| 1829 | }
|
|---|
| 1830 | //SvL: And restore original protection flags
|
|---|
| 1831 | ulCurFixup = (ULONG)pID[i].FirstThunk + pOH->ImageBase;
|
|---|
| 1832 | DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE*nrPages, section->pageflags);
|
|---|
| 1833 |
|
|---|
| 1834 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1835 | dprintf((LOG, "************** End Import Module %s ", pszCurModule ));
|
|---|
| 1836 | dprintf((LOG, "**********************************************************************" ));
|
|---|
| 1837 |
|
|---|
| 1838 | pszCurModule += strlen(pszCurModule) + 1;
|
|---|
| 1839 | }//for (i = 0; i < cModules; i++)
|
|---|
| 1840 | return TRUE;
|
|---|
| 1841 | }
|
|---|
| 1842 | //******************************************************************************
|
|---|
| 1843 | //******************************************************************************
|
|---|
| 1844 | BOOL Win32PeLdrImage::insideModule(ULONG address)
|
|---|
| 1845 | {
|
|---|
| 1846 | if((address >= realBaseAddress) && (address < realBaseAddress + imageSize)) {
|
|---|
| 1847 | return TRUE;
|
|---|
| 1848 | }
|
|---|
| 1849 | return FALSE;
|
|---|
| 1850 | }
|
|---|
| 1851 | //******************************************************************************
|
|---|
| 1852 | //******************************************************************************
|
|---|
| 1853 | BOOL Win32PeLdrImage::insideModuleCode(ULONG address)
|
|---|
| 1854 | {
|
|---|
| 1855 | Section *sect;
|
|---|
| 1856 |
|
|---|
| 1857 | sect = findSectionByOS2Addr(address);
|
|---|
| 1858 | if(sect && (sect->pageflags & PAG_EXECUTE)) {
|
|---|
| 1859 | return TRUE;
|
|---|
| 1860 | }
|
|---|
| 1861 | return FALSE;
|
|---|
| 1862 | }
|
|---|
| 1863 | //******************************************************************************
|
|---|
| 1864 | //******************************************************************************
|
|---|
| 1865 | ULONG Win32PeLdrImage::getImageSize()
|
|---|
| 1866 | {
|
|---|
| 1867 | return imageSize;
|
|---|
| 1868 | }
|
|---|
| 1869 | //******************************************************************************
|
|---|
| 1870 | //******************************************************************************
|
|---|
| 1871 | ULONG Win32PeLdrImage::getApi(char *name)
|
|---|
| 1872 | {
|
|---|
| 1873 | ULONG apiaddr, i, apilen;
|
|---|
| 1874 | char *apiname;
|
|---|
| 1875 | char tmp[4];
|
|---|
| 1876 | NameExport *curexport;
|
|---|
| 1877 | ULONG ulAPIOrdinal; /* api requested by ordinal */
|
|---|
| 1878 |
|
|---|
| 1879 | apilen = strlen(name) + 1;
|
|---|
| 1880 | if(apilen < 4)
|
|---|
| 1881 | {
|
|---|
| 1882 | *(ULONG *)tmp = 0;
|
|---|
| 1883 | strcpy(tmp, name);
|
|---|
| 1884 | apiname = tmp;
|
|---|
| 1885 | apilen = 4;
|
|---|
| 1886 | }
|
|---|
| 1887 | else apiname = name;
|
|---|
| 1888 |
|
|---|
| 1889 | curexport = nameexports;
|
|---|
| 1890 | for(i=0; i<nrNameExports; i++)
|
|---|
| 1891 | {
|
|---|
| 1892 | if(apilen == curexport->nlength &&
|
|---|
| 1893 | *(ULONG *)curexport->name == *(ULONG *)apiname)
|
|---|
| 1894 | {
|
|---|
| 1895 | if(strcmp(curexport->name, apiname) == 0)
|
|---|
| 1896 | return(curexport->virtaddr);
|
|---|
| 1897 | }
|
|---|
| 1898 | curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
|
|---|
| 1899 | }
|
|---|
| 1900 | return(0);
|
|---|
| 1901 | }
|
|---|
| 1902 | //******************************************************************************
|
|---|
| 1903 | //******************************************************************************
|
|---|
| 1904 | ULONG Win32PeLdrImage::getApi(int ordinal)
|
|---|
| 1905 | {
|
|---|
| 1906 | ULONG apiaddr, i;
|
|---|
| 1907 | OrdExport *curexport;
|
|---|
| 1908 | NameExport *nexport;
|
|---|
| 1909 |
|
|---|
| 1910 | curexport = ordexports;
|
|---|
| 1911 |
|
|---|
| 1912 | /* accelerated resolving of ordinal exports
|
|---|
| 1913 | * is based on the assumption the ordinal export
|
|---|
| 1914 | * table is always sorted ascending.
|
|---|
| 1915 | *
|
|---|
| 1916 | * When the step size is too small, we continue
|
|---|
| 1917 | * with the linear search.
|
|---|
| 1918 | */
|
|---|
| 1919 |
|
|---|
| 1920 | // start in the middle of the tree
|
|---|
| 1921 | i = nrOrdExportsRegistered >> 1;
|
|---|
| 1922 | int iStep = i;
|
|---|
| 1923 |
|
|---|
| 1924 | for(;;)
|
|---|
| 1925 | {
|
|---|
| 1926 | int iThisExport = curexport[i].ordinal;
|
|---|
| 1927 |
|
|---|
| 1928 | iStep >>= 1; // next step will be narrower
|
|---|
| 1929 |
|
|---|
| 1930 | if (iThisExport < ordinal)
|
|---|
| 1931 | i += min(iStep, (ordinal-iThisExport)); // move farther down the list
|
|---|
| 1932 | else
|
|---|
| 1933 | if (iThisExport == ordinal) // found the export?
|
|---|
| 1934 | return curexport[i].virtaddr;
|
|---|
| 1935 | else
|
|---|
| 1936 | i -= min(iStep, (iThisExport-ordinal)); // move farther up the list
|
|---|
| 1937 |
|
|---|
| 1938 | // if we're in the direct neighbourhood search linearly
|
|---|
| 1939 | if (iStep <= 1)
|
|---|
| 1940 | {
|
|---|
| 1941 | // decide if we're to search backward or forward
|
|---|
| 1942 | if (ordinal > curexport[i].ordinal)
|
|---|
| 1943 | {
|
|---|
| 1944 | // As a certain number of exports are 0 at the end
|
|---|
| 1945 | // of the array, this case will hit fairly often.
|
|---|
| 1946 | // the last comparison will send the loop off into the
|
|---|
| 1947 | // wrong direction!
|
|---|
| 1948 | #ifdef DEBUG
|
|---|
| 1949 | if (curexport[i].ordinal == 0)
|
|---|
| 1950 | {
|
|---|
| 1951 | DebugInt3();
|
|---|
| 1952 | }
|
|---|
| 1953 | #endif
|
|---|
| 1954 |
|
|---|
| 1955 | for (;i<nrOrdExports;i++) // scan forward
|
|---|
| 1956 | {
|
|---|
| 1957 | iThisExport = curexport[i].ordinal;
|
|---|
| 1958 | if(iThisExport == ordinal)
|
|---|
| 1959 | return(curexport[i].virtaddr);
|
|---|
| 1960 | else
|
|---|
| 1961 | if (iThisExport > ordinal)
|
|---|
| 1962 | {
|
|---|
| 1963 | // Oops, cannot find the ordinal in the sorted list
|
|---|
| 1964 | break;
|
|---|
| 1965 | }
|
|---|
| 1966 | }
|
|---|
| 1967 | }
|
|---|
| 1968 | else
|
|---|
| 1969 | {
|
|---|
| 1970 | for (;i>=0;i--) // scan backward
|
|---|
| 1971 | {
|
|---|
| 1972 | iThisExport = curexport[i].ordinal;
|
|---|
| 1973 | if(curexport[i].ordinal == ordinal)
|
|---|
| 1974 | return(curexport[i].virtaddr);
|
|---|
| 1975 | else
|
|---|
| 1976 | if (iThisExport < ordinal)
|
|---|
| 1977 | // Oops, cannot find the ordinal in the sorted list
|
|---|
| 1978 | break;
|
|---|
| 1979 | }
|
|---|
| 1980 | }
|
|---|
| 1981 |
|
|---|
| 1982 | // not found yet.
|
|---|
| 1983 | break;
|
|---|
| 1984 | }
|
|---|
| 1985 | }
|
|---|
| 1986 |
|
|---|
| 1987 | //Name exports also contain an ordinal, so check this
|
|---|
| 1988 | nexport = nameexports;
|
|---|
| 1989 | for(i=0;i<nrNameExports;i++) {
|
|---|
| 1990 | if(nexport->ordinal == ordinal)
|
|---|
| 1991 | return(nexport->virtaddr);
|
|---|
| 1992 |
|
|---|
| 1993 | nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
|
|---|
| 1994 | }
|
|---|
| 1995 | return(0);
|
|---|
| 1996 | }
|
|---|
| 1997 | //******************************************************************************
|
|---|
| 1998 | //Returns required OS version for this image
|
|---|
| 1999 | //******************************************************************************
|
|---|
| 2000 | ULONG Win32PeLdrImage::getVersion()
|
|---|
| 2001 | {
|
|---|
| 2002 | return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion;
|
|---|
| 2003 | }
|
|---|
| 2004 | //******************************************************************************
|
|---|
| 2005 | //******************************************************************************
|
|---|
| 2006 | ULONG MissingApi()
|
|---|
| 2007 | {
|
|---|
| 2008 | static BOOL fIgnore = FALSE;
|
|---|
| 2009 | int r;
|
|---|
| 2010 |
|
|---|
| 2011 | dprintf((LOG, "Missing api called!\n"));
|
|---|
| 2012 | if(fIgnore)
|
|---|
| 2013 | return(0);
|
|---|
| 2014 |
|
|---|
| 2015 | do {
|
|---|
| 2016 | r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, "The application has called a non-existing api\n",
|
|---|
| 2017 | "Internal Odin Error", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
|
|---|
| 2018 | }
|
|---|
| 2019 | while(r == MBID_RETRY); //giggle
|
|---|
| 2020 |
|
|---|
| 2021 | if( r != MBID_IGNORE )
|
|---|
| 2022 | ExitProcess(987);
|
|---|
| 2023 |
|
|---|
| 2024 | fIgnore = TRUE;
|
|---|
| 2025 | return(0);
|
|---|
| 2026 | }
|
|---|
| 2027 | /******************************************************************************/
|
|---|
| 2028 | /******************************************************************************/
|
|---|