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