Ignore:
Timestamp:
Apr 15, 2000, 11:17:06 PM (25 years ago)
Author:
bird
Message:

Corrected call to findDll.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/winimagepeldr.cpp

    r3375 r3393  
    1 /* $Id: winimagepeldr.cpp,v 1.39 2000-04-14 22:35:28 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.40 2000-04-15 21:17:06 bird Exp $ */
    22
    33/*
     
    7777 char logname[CCHMAXPATH];
    7878
    79         sprintf(logname, "pe_%d.log", loadNr);
    80         _privateLogFile = fopen(logname, "w");
    81         if(_privateLogFile == NULL) {
    82                 sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
    83                 _privateLogFile = fopen(logname, "w");
    84         }
    85         dprintfGlobal(("PE LOGFILE : %s", logname));
     79    sprintf(logname, "pe_%d.log", loadNr);
     80        _privateLogFile = fopen(logname, "w");
     81    if(_privateLogFile == NULL) {
     82        sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
     83            _privateLogFile = fopen(logname, "w");
     84    }
     85    dprintfGlobal(("PE LOGFILE : %s", logname));
    8686#endif
    8787}
     
    9191{
    9292#ifdef DEBUG
    93         if(_privateLogFile) {
    94                 fclose(_privateLogFile);
    95                 _privateLogFile = NULL;
    96         }
     93    if(_privateLogFile) {
     94        fclose(_privateLogFile);
     95        _privateLogFile = NULL;
     96    }
    9797#endif
    9898}
     
    113113  strupr(szFileName);
    114114  if(isExe) {
    115         if(!strchr(szFileName, '.')) {
    116                 strcat(szFileName,".EXE");
    117         }
    118         dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
    119         if(dllfile == NULL) {
    120                 if(!strstr(szFileName, ".EXE")) {
    121                         strcat(szFileName,".EXE");
    122                 }
    123                 dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
    124                 if(dllfile == NULL) {
    125                         OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
    126                 }
    127         }
    128         else    OSLibDosClose(dllfile);
     115    if(!strchr(szFileName, '.')) {
     116        strcat(szFileName,".EXE");
     117    }
     118    dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
     119    if(dllfile == NULL) {
     120        if(!strstr(szFileName, ".EXE")) {
     121            strcat(szFileName,".EXE");
     122        }
     123        dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
     124        if(dllfile == NULL) {
     125            OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
     126        }
     127    }
     128    else    OSLibDosClose(dllfile);
    129129  }
    130130  else {
    131         findDll(szFileName, szModule, sizeof(szModule));
    132         strcpy(szFileName, szModule);
     131    if (findDll(szFileName, szModule, sizeof(szModule)))
     132        strcpy(szFileName, szModule);
    133133  }
    134134  strcpy(szModule, OSLibStripPath(szFileName));
     
    136136  char *dot = strstr(szModule, ".");
    137137  while(dot) {
    138         char *newdot = strstr(dot+1, ".");
    139         if(newdot == NULL)      break;
    140         dot = newdot;
     138    char *newdot = strstr(dot+1, ".");
     139    if(newdot == NULL)  break;
     140    dot = newdot;
    141141  }
    142142  if(dot)
    143         *dot = 0;
     143    *dot = 0;
    144144}
    145145//******************************************************************************
     
    148148{
    149149  if(memmap)
    150         delete memmap;
     150    delete memmap;
    151151
    152152  if(hFile) {
    153         OSLibDosClose(hFile);
    154         hFile = 0;
     153    OSLibDosClose(hFile);
     154    hFile = 0;
    155155  }
    156156
    157157  if(realBaseAddress)
    158         DosFreeMem((PVOID)realBaseAddress);
     158        DosFreeMem((PVOID)realBaseAddress);
    159159
    160160  if(nameexports)
    161         free(nameexports);
     161        free(nameexports);
    162162
    163163  if(ordexports)
    164         free(ordexports);
     164        free(ordexports);
    165165}
    166166//******************************************************************************
     
    183183  strcpy(szErrorModule, OSLibStripPath(szFileName));
    184184  if(hFile == NULL) {
    185         goto failure;
     185        goto failure;
    186186  }
    187187  //read dos header
    188188  if(DosRead(hFile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
    189         goto failure;
     189        goto failure;
    190190  }
    191191  if(OSLibDosSetFilePtr(hFile, doshdr.e_lfanew, OSLIB_SETPTR_FILE_BEGIN) == -1) {
    192         goto failure;
     192        goto failure;
    193193  }
    194194  //read signature dword
    195195  if(DosRead(hFile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
    196         goto failure;
     196        goto failure;
    197197  }
    198198  //read pe header
    199199  if(DosRead(hFile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
    200         goto failure;
     200        goto failure;
    201201  }
    202202  //read optional header
    203203  if(DosRead(hFile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
    204         goto failure;
     204        goto failure;
    205205  }
    206206  if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
    207         dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
     207        dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
    208208        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    209         goto failure;
     209        goto failure;
    210210  }
    211211
    212212  if(oh.SizeOfImage == 0) {//just in case
    213         oh.SizeOfImage = OSLibDosGetFileSize(hFile);
     213    oh.SizeOfImage = OSLibDosGetFileSize(hFile);
    214214  }
    215215
     
    217217  //Allocate memory to hold the entire image
    218218  if(allocSections(reservedMem) == FALSE) {
    219         dprintf((LOG, "Failed to allocate image memory, rc %d", errorState));;
    220         goto failure;
     219        dprintf((LOG, "Failed to allocate image memory, rc %d", errorState));;
     220        goto failure;
    221221  }
    222222
    223223  memmap = new Win32MemMap(this, realBaseAddress, imageSize);
    224224  if(memmap == NULL || !memmap->Init(0)) {
    225         goto failure;
     225        goto failure;
    226226  }
    227227  win32file = memmap->mapViewOfFile(0, 0, 2);
    228228
    229229  if(DosQueryPathInfo(szFileName, FIL_QUERYFULLNAME, szFullPath, sizeof(szFullPath)) == 0) {
    230         setFullPath(szFullPath);
     230    setFullPath(szFullPath);
    231231  }
    232232
    233233  if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
    234         dprintf((LOG, "Not a valid PE file!"));
     234        dprintf((LOG, "Not a valid PE file!"));
    235235        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    236         goto failure;
     236        goto failure;
    237237  }
    238238  if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
    239         dprintf((LOG, "Doesn't run on x86 processors!"));
     239        dprintf((LOG, "Doesn't run on x86 processors!"));
    240240        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    241         goto failure;
     241        goto failure;
    242242  }
    243243  //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
    244244  if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
    245         dprintf((LOG, "Can't convert system files"));
     245        dprintf((LOG, "Can't convert system files"));
    246246        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    247         goto failure;
     247        goto failure;
    248248  }
    249249
    250250  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    251         dprintf((LOG, "No fixups, might not run!"));
     251        dprintf((LOG, "No fixups, might not run!"));
    252252  }
    253253
     
    283283   if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL) {
    284284    dprintf((LOG, "*************************PE SECTIONS START**************************" ));
    285     for (i=0; i<nSections; i++) { 
     285    for (i=0; i<nSections; i++) {
    286286        dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
    287287        dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
     
    312312            continue;
    313313        }
    314         if(strcmp(psh[i].Name, ".tls") == 0)
    315         {
    316                 tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
    317                 if(tlsDir) {
    318                         addSection(SECTION_TLS, psh[i].PointerToRawData,
    319                                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    320                                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    321                 }
    322                 continue;
    323         }
     314    if(strcmp(psh[i].Name, ".tls") == 0)
     315    {
     316        tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
     317        if(tlsDir) {
     318                addSection(SECTION_TLS, psh[i].PointerToRawData,
     319                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     320                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     321        }
     322        continue;
     323    }
    324324
    325325        if(strcmp(psh[i].Name, ".debug") == 0) {
     
    330330            continue;
    331331        }
    332         if(IsImportSection(win32file, &psh[i]))
    333         {
     332    if(IsImportSection(win32file, &psh[i]))
     333        {
    334334          int type = SECTION_IMPORT;
    335335            dprintf((LOG, "Import Data Section" ));
     
    385385        }
    386386        dprintf((LOG, "Unknown section" ));
    387         goto failure;
     387        goto failure;
    388388     }
    389389   }
    390390  }
    391391  else {
    392         if(GetSectionHdrByName (win32file, &sh, ".rsrc"))
     392    if(GetSectionHdrByName (win32file, &sh, ".rsrc"))
    393393        {
    394394            addSection(SECTION_RESOURCE, sh.PointerToRawData,
    395395                       sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
    396396                       sh.Misc.VirtualSize, sh.Characteristics);
    397         }
     397    }
    398398  }
    399399  dprintf((LOG, "*************************PE SECTIONS END **************************" ));
     
    405405  //In case there are any gaps between sections, adjust size
    406406  if(imageSize != imageVirtEnd - oh.ImageBase) {
    407         dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
    408         imageSize = imageVirtEnd - oh.ImageBase;
     407        dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
     408        imageSize = imageVirtEnd - oh.ImageBase;
    409409  }
    410410  if(imageSize < oh.SizeOfImage) {
    411         imageSize = oh.SizeOfImage;
     411    imageSize = oh.SizeOfImage;
    412412  }
    413413
    414414  dprintf((LOG, "OS/2 base address %x", realBaseAddress ));
    415415  if(oh.AddressOfEntryPoint) {
    416         entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
     416    entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
    417417  }
    418418  else {
    419         dprintf((LOG, "EntryPoint == NULL" ));
    420         entryPoint = NULL;
     419    dprintf((LOG, "EntryPoint == NULL" ));
     420    entryPoint = NULL;
    421421  }
    422422
    423423  //set memory protection flags
    424424  if(setMemFlags() == FALSE) {
    425         dprintf((LOG, "Failed to set memory protection" ));
    426         goto failure;
     425        dprintf((LOG, "Failed to set memory protection" ));
     426        goto failure;
    427427  }
    428428
     
    432432    Section *sect = findSection(SECTION_TLS);
    433433
    434         if(sect == NULL) {
    435                 dprintf((LOG, "Couldn't find TLS section!!" ));
    436                 goto failure;
    437         }
    438         dprintf((LOG, "TLS Directory" ));
    439         dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
    440         dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
    441         dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
    442         dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
    443         setTLSAddress((char *)sect->realvirtaddr);
    444         setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
    445         setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
    446 
    447         sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
    448         if(sect == NULL) {
    449                 dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
    450                 goto failure;
    451         }
    452         setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
    453 
    454         if((ULONG)tlsDir->AddressOfCallBacks != 0) {
    455                 sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
    456                 if(sect == NULL) {
    457                         dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
    458                         goto failure;
    459                 }
    460                 setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
    461         }
     434    if(sect == NULL) {
     435        dprintf((LOG, "Couldn't find TLS section!!" ));
     436            goto failure;
     437    }
     438    dprintf((LOG, "TLS Directory" ));
     439    dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
     440    dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
     441    dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
     442    dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
     443    setTLSAddress((char *)sect->realvirtaddr);
     444    setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
     445    setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
     446
     447    sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
     448    if(sect == NULL) {
     449        dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
     450            goto failure;
     451    }
     452    setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
     453
     454    if((ULONG)tlsDir->AddressOfCallBacks != 0) {
     455        sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
     456        if(sect == NULL) {
     457            dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
     458                goto failure;
     459        }
     460        setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
     461    }
    462462   }
    463463
    464464   if(realBaseAddress != oh.ImageBase) {
    465         pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
    466         commitPage((ULONG)pFixups, FALSE);
     465    pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
     466    commitPage((ULONG)pFixups, FALSE);
    467467   }
    468468#ifdef COMMIT_ALL
    469469   for (i=0; i<nSections; i++) {
    470         commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
     470    commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
    471471   }
    472472#else
    473473   for (i=0; i<nSections; i++) {
    474         switch(section[i].type)
    475         {
    476         case SECTION_IMPORT:
    477         case SECTION_RELOC:
    478         case SECTION_EXPORT:
    479                 commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
    480                 break;
    481         }
     474    switch(section[i].type)
     475    {
     476    case SECTION_IMPORT:
     477    case SECTION_RELOC:
     478    case SECTION_EXPORT:
     479        commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
     480        break;
     481    }
    482482   }
    483483#endif
    484484   if(processExports((char *)win32file) == FALSE) {
    485         dprintf((LOG, "Failed to process exported apis" ));
    486         goto failure;
     485        dprintf((LOG, "Failed to process exported apis" ));
     486        goto failure;
    487487   }
    488488  }
    489489#ifdef COMMIT_ALL
    490490  else {
    491         commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
     491    commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
    492492  }
    493493#endif
     
    502502  //    implicitly call functions depending on it.
    503503  if(GetSectionHdrByName (win32file, &sh, ".rsrc")) {
    504         //get offset in resource object of directory entry
    505         pResDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
     504        //get offset in resource object of directory entry
     505    pResDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
    506506        ulRVAResourceSection = sh.VirtualAddress;
    507507  }
     
    510510  {
    511511   if(processImports((char *)win32file) == FALSE) {
    512         dprintf((LOG, "Failed to process imports!" ));
    513         goto failure;
     512        dprintf((LOG, "Failed to process imports!" ));
     513        goto failure;
    514514   }
    515515  }
     
    518518failure:
    519519  if(memmap) {
    520         delete memmap;
    521         memmap = NULL;
     520    delete memmap;
     521    memmap = NULL;
    522522  }
    523523  if(hFile) {
    524         OSLibDosClose(hFile);
    525         hFile = 0;
     524    OSLibDosClose(hFile);
     525    hFile = 0;
    526526  }
    527527  errorState = ERROR_INTERNAL;
     
    541541  //Round down to nearest page boundary
    542542  virtAddress = virtAddress & ~0xFFF;
    543  
     543
    544544  section = findSectionByOS2Addr(virtAddress);
    545545  if(section == NULL) {
    546         size        = 4096;
    547         sectionsize = 4096;
    548         protflags   = PAG_READ|PAG_WRITE; //readonly?
    549         section = findPreviousSectionByOS2Addr(virtAddress);
    550         if(section == NULL) {//access to header
    551                 offset     = 0;
    552                 fileoffset = virtAddress - realBaseAddress;
    553         }
    554         else {
    555                 offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
    556                 fileoffset = section->rawoffset + section->rawsize + offset;
    557         }
     546    size        = 4096;
     547    sectionsize = 4096;
     548    protflags   = PAG_READ|PAG_WRITE; //readonly?
     549    section = findPreviousSectionByOS2Addr(virtAddress);
     550    if(section == NULL) {//access to header
     551        offset     = 0;
     552        fileoffset = virtAddress - realBaseAddress;
     553    }
     554    else {
     555        offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
     556        fileoffset = section->rawoffset + section->rawsize + offset;
     557    }
    558558  }
    559559  else {
    560         protflags   = section->pageflags;
    561         offset      = virtAddress - section->realvirtaddr;
    562         sectionsize = section->virtualsize - offset;
    563 
    564         if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
    565                 //unintialized data (set to 0)
    566                 size = 0;
    567                 fileoffset = -1;
    568         }
    569         else {
    570                 size = section->rawsize-offset;
    571                 fileoffset = section->rawoffset + offset;
    572         }
    573         if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
    574                 dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
    575                 return FALSE;
    576         }
     560    protflags   = section->pageflags;
     561    offset      = virtAddress - section->realvirtaddr;
     562    sectionsize = section->virtualsize - offset;
     563
     564    if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
     565        //unintialized data (set to 0)
     566        size = 0;
     567        fileoffset = -1;
     568    }
     569    else {
     570        size = section->rawsize-offset;
     571        fileoffset = section->rawoffset + offset;
     572    }
     573    if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
     574        dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
     575        return FALSE;
     576    }
    577577  }
    578578  //Check range of pages with the same attributes starting at virtAddress
     
    581581  rc = DosQueryMem((PVOID)virtAddress, &range, &attr);
    582582  if(rc) {
    583         dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
    584         return FALSE;
     583    dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
     584    return FALSE;
    585585  }
    586586  if(attr & PAG_COMMIT) {
    587         dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
    588         return FALSE;
     587    dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
     588    return FALSE;
    589589  }
    590590
    591591  if(fPageCmd == SINGLE_PAGE) {
    592         size = min(size, PAGE_SIZE);
    593         sectionsize = min(sectionsize, PAGE_SIZE);
     592    size = min(size, PAGE_SIZE);
     593    sectionsize = min(sectionsize, PAGE_SIZE);
    594594  }
    595595  else
    596596  if(fPageCmd == SECTION_PAGES) {
    597         size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
    598         sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
     597    size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
     598    sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
    599599  }
    600600  size = min(size, range);
     
    602602
    603603  if(fileoffset != -1) {
    604         rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
    605         if(rc) {
    606                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    607                 return FALSE;
    608         }
    609 
    610         if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
    611                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
    612                 return FALSE;
    613         }
    614         if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
    615                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
    616                 return FALSE;
    617         }
    618         if(ulRead != size) {
    619                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
    620                 return FALSE;
    621         }
    622         if(realBaseAddress != oh.ImageBase) {
    623                 setFixups(virtAddress, sectionsize);
    624         }
    625 
    626         rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
    627         if(rc) {
    628                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    629                 return FALSE;
    630         }
     604    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
     605    if(rc) {
     606        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     607            return FALSE;
     608    }
     609
     610    if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
     611        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
     612            return FALSE;
     613    }
     614    if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
     615        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
     616            return FALSE;
     617    }
     618    if(ulRead != size) {
     619        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
     620            return FALSE;
     621    }
     622    if(realBaseAddress != oh.ImageBase) {
     623        setFixups(virtAddress, sectionsize);
     624    }
     625
     626    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
     627    if(rc) {
     628        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     629        return FALSE;
     630    }
    631631  }
    632632  else {
    633         rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
    634         if(rc) {
    635                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    636                 return FALSE;
    637         }
    638         if(realBaseAddress != oh.ImageBase) {
    639                 setFixups(virtAddress, sectionsize);
    640         }
    641         rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
    642         if(rc) {
    643                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    644                 return FALSE;
    645         }
     633    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
     634    if(rc) {
     635        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     636            return FALSE;
     637    }
     638    if(realBaseAddress != oh.ImageBase) {
     639        setFixups(virtAddress, sectionsize);
     640    }
     641    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
     642    if(rc) {
     643        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     644        return FALSE;
     645    }
    646646  }
    647647  return TRUE;
     
    664664
    665665  if(virtaddress < imageVirtBase)
    666         imageVirtBase = virtaddress;
     666        imageVirtBase = virtaddress;
    667667  if(virtaddress + virtsize > imageVirtEnd)
    668         imageVirtEnd  = virtaddress + virtsize;
     668        imageVirtEnd  = virtaddress + virtsize;
    669669
    670670  nrsections++;
     
    679679  //SvL: We don't care where the image is loaded for resource lookup
    680680  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED && loadType == REAL_LOAD) {
    681         return allocFixedMem(reservedMem);
     681        return allocFixedMem(reservedMem);
    682682  }
    683683  rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
    684684  if(rc) {
    685         dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
    686         errorState = rc;
    687         return(FALSE);
     685    dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
     686        errorState = rc;
     687        return(FALSE);
    688688  }
    689689  realBaseAddress = baseAddress;
     
    695695{
    696696  for(int i=0;i<nrsections;i++) {
    697         if(section[i].type == type) {
    698                 return &section[i];
    699         }
     697    if(section[i].type == type) {
     698        return &section[i];
     699    }
    700700  }
    701701  return NULL;
     
    706706{
    707707  for(int i=0;i<nrsections;i++) {
    708         if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
    709                 return &section[i];
    710         }
     708    if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
     709        return &section[i];
     710    }
    711711  }
    712712  return NULL;
     
    717717{
    718718  for(int i=0;i<nrsections;i++) {
    719         if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
    720                 return &section[i];
    721         }
     719    if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
     720        return &section[i];
     721    }
    722722  }
    723723  return NULL;
     
    731731
    732732  for(int i=0;i<nrsections;i++) {
    733         if(section[i].realvirtaddr > addr) {
    734                 if(section[i].realvirtaddr < lowestAddr) {
    735                         lowestAddr = section[i].realvirtaddr;
    736                         index = i;
    737                 }
    738         }
     733    if(section[i].realvirtaddr > addr) {
     734        if(section[i].realvirtaddr < lowestAddr) {
     735            lowestAddr = section[i].realvirtaddr;
     736            index = i;
     737        }
     738    }
    739739  }
    740740  if(index == -1)
    741         return NULL;
     741    return NULL;
    742742
    743743  return &section[index];
     
    761761  //Allocated in peldr.dll
    762762  if(reservedMem && reservedMem == oh.ImageBase) {
    763         realBaseAddress = oh.ImageBase;
    764         return TRUE;
     763    realBaseAddress = oh.ImageBase;
     764    return TRUE;
    765765  }
    766766
     
    768768  memallocs = (ULONG *)malloc(4096*sizeof(ULONG *));
    769769  if(memallocs == NULL) {
    770         dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
    771         return FALSE;
     770    dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
     771    return FALSE;
    772772  }
    773773
    774774  if(oh.ImageBase < 512*1024*1024) {
    775         allocFlags = 0;
     775    allocFlags = 0;
    776776  }
    777777  while(TRUE) {
    778         rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
    779         if(rc) break;
    780 
    781         dprintf((LOG, "DosAllocMem returned %x", address ));
    782         if(address + FALLOC_SIZE >= oh.ImageBase) {
    783                 if(address > oh.ImageBase) {//we've passed it!
    784                         DosFreeMem((PVOID)address);
    785                         break;
    786                 }
    787                 //found the right address
    788                 DosFreeMem((PVOID)address);
    789 
    790                 diff = oh.ImageBase - address;
    791                 if(diff) {
    792                         rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
    793                         if(rc) break;
    794                 }
    795                 rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
    796                 if(rc) break;
    797 
    798                 if(diff) DosFreeMem((PVOID)address);
    799 
    800                 realBaseAddress = baseAddress;
    801                 break;
    802         }
    803         memallocs[alloccnt++] = address;
     778        rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
     779        if(rc) break;
     780
     781        dprintf((LOG, "DosAllocMem returned %x", address ));
     782        if(address + FALLOC_SIZE >= oh.ImageBase) {
     783            if(address > oh.ImageBase) {//we've passed it!
     784                    DosFreeMem((PVOID)address);
     785                    break;
     786            }
     787            //found the right address
     788            DosFreeMem((PVOID)address);
     789
     790            diff = oh.ImageBase - address;
     791            if(diff) {
     792                    rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
     793                    if(rc) break;
     794            }
     795            rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
     796            if(rc) break;
     797
     798            if(diff) DosFreeMem((PVOID)address);
     799
     800            realBaseAddress = baseAddress;
     801            break;
     802        }
     803    memallocs[alloccnt++] = address;
    804804  }
    805805  for(i=0;i<alloccnt;i++) {
    806         DosFreeMem((PVOID)memallocs[i]);
     806        DosFreeMem((PVOID)memallocs[i]);
    807807  }
    808808  free(memallocs);
    809809
    810810  if(realBaseAddress == 0) //Let me guess.. MS Office app?
    811         return(FALSE);
     811        return(FALSE);
    812812
    813813  return(TRUE);
     
    827827  // Process all the image sections
    828828  for(i=0;i<nrsections;i++) {
    829         section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
     829        section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
    830830  }
    831831
    832832  for(i=0;i<nrsections;i++) {
    833         switch(section[i].type)
    834         {
     833        switch(section[i].type)
     834    {
    835835        case SECTION_CODE:
    836836        case (SECTION_CODE | SECTION_IMPORT):
    837                 section[i].pageflags = PAG_EXECUTE | PAG_READ;
    838                 if(section[i].flags & IMAGE_SCN_MEM_WRITE)
    839                         section[i].pageflags |= PAG_WRITE;
    840                 break;
     837                section[i].pageflags = PAG_EXECUTE | PAG_READ;
     838        if(section[i].flags & IMAGE_SCN_MEM_WRITE)
     839            section[i].pageflags |= PAG_WRITE;
     840                break;
    841841        case SECTION_INITDATA:
    842842        case SECTION_UNINITDATA:
    843843        case SECTION_IMPORT: //TODO: read only?
    844                 section[i].pageflags = PAG_WRITE | PAG_READ;
    845                 break;
     844                section[i].pageflags = PAG_WRITE | PAG_READ;
     845                break;
    846846        case SECTION_READONLYDATA:
    847847        case SECTION_RESOURCE:
    848         case SECTION_TLS:
     848    case SECTION_TLS:
    849849        default:
    850                 section[i].pageflags = PAG_READ;
    851                 break;
    852         }
     850                section[i].pageflags = PAG_READ;
     851                break;
     852        }
    853853  }
    854854  return(TRUE);
     
    865865
    866866  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    867         return(TRUE);
     867        return(TRUE);
    868868  }
    869869
     
    872872  size  = (size-1) & ~0xFFF;
    873873  size += PAGE_SIZE;
    874  
     874
    875875  if(prel) {
    876         j = 1;
    877         while(prel->VirtualAddress && prel->VirtualAddress < virtAddress) {
    878                 prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
    879         }
    880         while(prel->VirtualAddress && prel->VirtualAddress < virtAddress + size) {
    881                 page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
    882                 count  = (prel->SizeOfBlock - 8)/2;
    883                 j++;
    884                 for(i=0;i<count;i++) {
    885                         int type   = prel->TypeOffset[i] >> 12;
    886                         int offset = prel->TypeOffset[i] & 0xFFF;
    887                         int fixupsize = 0;
    888 
    889                         switch(type)
    890                         {
    891                         case IMAGE_REL_BASED_HIGHLOW:
    892                                 fixupsize = 4;
    893                                 break;
    894                         case IMAGE_REL_BASED_HIGH:
    895                         case IMAGE_REL_BASED_LOW:
    896                                 fixupsize = 2;
    897                                 break;
    898                         }
    899                         //If the fixup crosses the final page boundary,
    900                         //then we have to load another page
    901                         if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
    902                         {
    903                                 newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
    904                                 newpage &= ~0xFFF;
    905 
    906                                 section  = findSectionByOS2Addr(newpage);
    907                                 if(section == NULL) {
    908                                         //should never happen
    909                                         dprintf((LOG, "::setFixups -> section == NULL!!"));
    910                                         return FALSE;
    911                                 }
    912                                 //SvL: Read page from disk
    913                                 commitPage(newpage, FALSE, SINGLE_PAGE);
    914 
    915                                 //SvL: Enable write access
    916                                 DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
    917                         }
    918 
    919                         switch(type)
    920                         {
    921                         case IMAGE_REL_BASED_ABSOLUTE:
    922                                 break;  //skip
    923                         case IMAGE_REL_BASED_HIGHLOW:
    924                                 AddOff32Fixup(prel->VirtualAddress + offset);
    925                                 break;
    926                         case IMAGE_REL_BASED_HIGH:
    927                                 AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
    928                                 break;
    929                         case IMAGE_REL_BASED_LOW:
    930                                 AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
    931                                 break;
    932                         case IMAGE_REL_BASED_HIGHADJ:
    933                         case IMAGE_REL_BASED_MIPS_JMPADDR:
    934                         default:
    935                                 break;
    936                         }
    937                         if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
    938                         {
    939                                 //SvL: Restore original page protection flags
    940                                 DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
    941                         }
    942                 }
    943                 prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
    944         }//while
     876    j = 1;
     877    while(prel->VirtualAddress && prel->VirtualAddress < virtAddress) {
     878            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
     879    }
     880        while(prel->VirtualAddress && prel->VirtualAddress < virtAddress + size) {
     881            page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
     882            count  = (prel->SizeOfBlock - 8)/2;
     883            j++;
     884            for(i=0;i<count;i++) {
     885                int type   = prel->TypeOffset[i] >> 12;
     886                int offset = prel->TypeOffset[i] & 0xFFF;
     887            int fixupsize = 0;
     888
     889                switch(type)
     890            {
     891                case IMAGE_REL_BASED_HIGHLOW:
     892                fixupsize = 4;
     893                break;
     894                    case IMAGE_REL_BASED_HIGH:
     895                    case IMAGE_REL_BASED_LOW:
     896                fixupsize = 2;
     897                break;
     898            }
     899            //If the fixup crosses the final page boundary,
     900            //then we have to load another page
     901            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
     902            {
     903                newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
     904                newpage &= ~0xFFF;
     905
     906                    section  = findSectionByOS2Addr(newpage);
     907                    if(section == NULL) {
     908                    //should never happen
     909                    dprintf((LOG, "::setFixups -> section == NULL!!"));
     910                    return FALSE;
     911                    }
     912                //SvL: Read page from disk
     913                commitPage(newpage, FALSE, SINGLE_PAGE);
     914
     915                    //SvL: Enable write access
     916                    DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
     917            }
     918
     919                switch(type)
     920            {
     921                case IMAGE_REL_BASED_ABSOLUTE:
     922                    break;  //skip
     923                case IMAGE_REL_BASED_HIGHLOW:
     924                        AddOff32Fixup(prel->VirtualAddress + offset);
     925                        break;
     926                    case IMAGE_REL_BASED_HIGH:
     927                AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
     928                break;
     929                    case IMAGE_REL_BASED_LOW:
     930                AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
     931                break;
     932                    case IMAGE_REL_BASED_HIGHADJ:
     933                    case IMAGE_REL_BASED_MIPS_JMPADDR:
     934                default:
     935                        break;
     936                }
     937            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
     938            {
     939                    //SvL: Restore original page protection flags
     940                    DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
     941            }
     942            }
     943            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
     944        }//while
    945945  }
    946946  else {
    947         dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
    948         return(FALSE);
     947    dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
     948        return(FALSE);
    949949  }
    950950  return(TRUE);
     
    959959
    960960  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    961         return(TRUE);
     961        return(TRUE);
    962962  }
    963963
     
    981981                break;
    982982            case IMAGE_REL_BASED_HIGH:
    983                 AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
    984                 break;
     983        AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
     984        break;
    985985            case IMAGE_REL_BASED_LOW:
    986                 AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
    987                 break;
     986        AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
     987        break;
    988988            case IMAGE_REL_BASED_HIGHADJ:
    989989            case IMAGE_REL_BASED_MIPS_JMPADDR:
     
    10251025  orgaddr = *fixup;
    10261026  if(fHighFixup) {
    1027         *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
     1027    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
    10281028//        dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    10291029  }
    10301030  else {
    1031         *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
     1031    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
    10321032//        dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    10331033  }
     
    10441044  if(apiaddr == 0)
    10451045  {
    1046         dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
     1046        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
    10471047                 WinDll->getName(),
    10481048                 ordinal));
    10491049
    1050         dprintf((LOG, "--->>> NOT FOUND!" ));
    1051         *import = (ULONG)MissingApi;
     1050        dprintf((LOG, "--->>> NOT FOUND!" ));
     1051        *import = (ULONG)MissingApi;
    10521052  }
    10531053  else  *import = apiaddr;
     
    10641064  if(apiaddr == 0)
    10651065  {
    1066         dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
     1066        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
    10671067                WinDll->getName(),
    10681068                impname));
    10691069
    1070         dprintf((LOG, "--->>> NOT FOUND!" ));
    1071         *import = (ULONG)MissingApi;
     1070        dprintf((LOG, "--->>> NOT FOUND!" ));
     1071        *import = (ULONG)MissingApi;
    10721072  }
    10731073  else  *import = apiaddr;
     
    11491149
    11501150  if(nameexports == NULL) {
    1151         nameExportSize= 4096;
    1152         nameexports   = (NameExport *)malloc(nameExportSize);
    1153         curnameexport = nameexports;
     1151        nameExportSize= 4096;
     1152        nameexports   = (NameExport *)malloc(nameExportSize);
     1153        curnameexport = nameexports;
    11541154  }
    11551155  nsize = (ULONG)curnameexport - (ULONG)nameexports;
    11561156  if(nsize + sizeof(NameExport) + strlen(apiname) > nameExportSize) {
    1157         nameExportSize += 4096;
    1158         char *tmp = (char *)nameexports;
    1159         nameexports = (NameExport *)malloc(nameExportSize);
    1160         memcpy(nameexports, tmp, nsize);
    1161         curnameexport = (NameExport *)((ULONG)nameexports + nsize);
    1162         free(tmp);
     1157        nameExportSize += 4096;
     1158        char *tmp = (char *)nameexports;
     1159        nameexports = (NameExport *)malloc(nameExportSize);
     1160        memcpy(nameexports, tmp, nsize);
     1161        curnameexport = (NameExport *)((ULONG)nameexports + nsize);
     1162        free(tmp);
    11631163  }
    11641164  curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     
    11691169  curnameexport->nlength = strlen(apiname) + 1;
    11701170  if(curnameexport->nlength < sizeof(curnameexport->name))
    1171         curnameexport->nlength = sizeof(curnameexport->name);
     1171        curnameexport->nlength = sizeof(curnameexport->name);
    11721172
    11731173  curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
     
    11781178{
    11791179  if(ordexports == NULL) {
    1180         ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
    1181         curordexport = ordexports;
     1180        ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
     1181        curordexport = ordexports;
    11821182  }
    11831183  curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     
    13291329    if(WinDll == NULL)
    13301330    {  //not found, so load it
    1331         char modname[CCHMAXPATH];
    1332 
    1333         strcpy(modname, pszCurModule);
    1334         //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
    1335         Win32DllBase::renameDll(modname);
    1336 
    1337         if(isPEImage(modname) == FALSE)
    1338         {//LX image, so let OS/2 do all the work for us
    1339                 APIRET rc;
    1340                 char   szModuleFailure[CCHMAXPATH] = "";
    1341                 ULONG  hInstanceNewDll;
    1342 
    1343                 char *dot = strchr(modname, '.');
    1344                 if(dot) {
    1345                         *dot = 0;
    1346                 }
    1347                 strcat(modname, ".DLL");
    1348                 rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
    1349                 if(rc) {
    1350                         dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
    1351                         sprintf(szErrorModule, "%s.DLL", szModuleFailure);
    1352                         errorState = rc;
    1353                         return(FALSE);
    1354                 }
    1355                 WinDll = (Win32PeLdrDll *)Win32DllBase::findModule(hInstanceNewDll);
    1356                 if(WinDll == NULL) {//shouldn't happen!
    1357                         dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
    1358                         errorState = ERROR_INTERNAL;
    1359                         return(FALSE);
    1360                 }
    1361                 //Mark this dll as loaded by DosLoadModule
    1362                 WinDll->setLoadLibrary();
    1363                 WinDll->AddRef();
    1364         }
    1365         else {
    1366                 WinDll = new Win32PeLdrDll(modname, this);
    1367 
    1368                 if(WinDll == NULL) {
    1369                     dprintf((LOG, "WinDll: Error allocating memory" ));
    1370                     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    1371                     errorState = ERROR_INTERNAL;
    1372                     return(FALSE);
    1373                 }
    1374                 dprintf((LOG, "**********************************************************************" ));
    1375                 dprintf((LOG, "**********************     Loading Module        *********************" ));
    1376                 dprintf((LOG, "**********************************************************************" ));
    1377                 if(WinDll->init(0) == FALSE) {
    1378                     dprintf((LOG, "Internal WinDll error ", WinDll->getError() ));
    1379                     return(FALSE);
    1380                 }
     1331    char modname[CCHMAXPATH];
     1332
     1333    strcpy(modname, pszCurModule);
     1334    //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
     1335    Win32DllBase::renameDll(modname);
     1336
     1337    if(isPEImage(modname) == FALSE)
     1338    {//LX image, so let OS/2 do all the work for us
     1339        APIRET rc;
     1340        char   szModuleFailure[CCHMAXPATH] = "";
     1341        ULONG  hInstanceNewDll;
     1342
     1343        char *dot = strchr(modname, '.');
     1344        if(dot) {
     1345            *dot = 0;
     1346        }
     1347        strcat(modname, ".DLL");
     1348        rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
     1349        if(rc) {
     1350            dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
     1351            sprintf(szErrorModule, "%s.DLL", szModuleFailure);
     1352            errorState = rc;
     1353            return(FALSE);
     1354        }
     1355        WinDll = (Win32PeLdrDll *)Win32DllBase::findModule(hInstanceNewDll);
     1356        if(WinDll == NULL) {//shouldn't happen!
     1357            dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
     1358            errorState = ERROR_INTERNAL;
     1359            return(FALSE);
     1360        }
     1361        //Mark this dll as loaded by DosLoadModule
     1362        WinDll->setLoadLibrary();
     1363        WinDll->AddRef();
     1364    }
     1365    else {
     1366            WinDll = new Win32PeLdrDll(modname, this);
     1367
     1368            if(WinDll == NULL) {
     1369                dprintf((LOG, "WinDll: Error allocating memory" ));
     1370                WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     1371                errorState = ERROR_INTERNAL;
     1372                return(FALSE);
     1373            }
     1374            dprintf((LOG, "**********************************************************************" ));
     1375            dprintf((LOG, "**********************     Loading Module        *********************" ));
     1376            dprintf((LOG, "**********************************************************************" ));
     1377            if(WinDll->init(0) == FALSE) {
     1378                dprintf((LOG, "Internal WinDll error ", WinDll->getError() ));
     1379                return(FALSE);
     1380            }
    13811381#ifdef DEBUG
    1382                 WinDll->AddRef(getModuleName());
     1382            WinDll->AddRef(getModuleName());
    13831383#else
    1384                 WinDll->AddRef();
     1384            WinDll->AddRef();
    13851385#endif
    1386                 if(WinDll->attachProcess() == FALSE) {
    1387                     dprintf((LOG, "attachProcess failed!" ));
    1388                     delete WinDll;
    1389                     errorState = ERROR_INTERNAL;
    1390                     return(FALSE);
    1391                 }
    1392         }
    1393 
    1394         dprintf((LOG, "**********************************************************************" ));
    1395         dprintf((LOG, "**********************  Finished Loading Module  *********************" ));
     1386            if(WinDll->attachProcess() == FALSE) {
     1387                dprintf((LOG, "attachProcess failed!" ));
     1388            delete WinDll;
     1389                errorState = ERROR_INTERNAL;
     1390                return(FALSE);
     1391            }
     1392    }
     1393
     1394    dprintf((LOG, "**********************************************************************" ));
     1395    dprintf((LOG, "**********************  Finished Loading Module  *********************" ));
    13961396        dprintf((LOG, "**********************************************************************" ));
    13971397    }
    13981398    else {
    1399         if(WinDll->isLxDll() && !WinDll->isLoaded()) {
    1400                 //can happen with i.e. wininet
    1401                 //wininet depends on wsock32; when the app loads wsock32 afterwards
    1402                 //with LoadLibrary or as a child of another dll, we need to make
     1399    if(WinDll->isLxDll() && !WinDll->isLoaded()) {
     1400        //can happen with i.e. wininet
     1401        //wininet depends on wsock32; when the app loads wsock32 afterwards
     1402        //with LoadLibrary or as a child of another dll, we need to make
    14031403                //sure it's loaded once with DosLoadModule
    1404                 WinDll->loadLibrary();
    1405         }
    1406         WinDll->AddRef();
    1407        
    1408         dprintf((LOG, "Already found ", pszCurModule));
     1404        WinDll->loadLibrary();
     1405    }
     1406    WinDll->AddRef();
     1407
     1408    dprintf((LOG, "Already found ", pszCurModule));
    14091409    }
    14101410    //add the dll we just loaded to dependency list for this image
     
    14221422    section    = findSectionByOS2Addr(ulCurFixup);
    14231423    if(section == NULL) {
    1424         dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
    1425         return FALSE;
     1424    dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
     1425    return FALSE;
    14261426    }
    14271427    //SvL: Read page from disk
     
    14521452        ulCurFixup += sizeof(IMAGE_THUNK_DATA);
    14531453        j++;
    1454         if((ulCurFixup & 0xfff) == 0) {
    1455                 commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
    1456                 DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
    1457                 nrPages++;
    1458         }
     1454    if((ulCurFixup & 0xfff) == 0) {
     1455            commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
     1456            DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
     1457            nrPages++;
     1458    }
    14591459    }
    14601460    //SvL: And restore original protection flags
     
    14851485  if(apilen < 4)
    14861486  {
    1487         *(ULONG *)tmp = 0;
    1488         strcpy(tmp, name);
    1489         apiname = tmp;
    1490         apilen  = 4;
    1491   }
    1492   else  apiname = name;
     1487    *(ULONG *)tmp = 0;
     1488    strcpy(tmp, name);
     1489    apiname = tmp;
     1490    apilen  = 4;
     1491  }
     1492  else  apiname = name;
    14931493
    14941494  curexport = nameexports;
     
    14981498       *(ULONG *)curexport->name == *(ULONG *)apiname)
    14991499    {
    1500         if(strcmp(curexport->name, apiname) == 0)
    1501                 return(curexport->virtaddr);
     1500        if(strcmp(curexport->name, apiname) == 0)
     1501            return(curexport->virtaddr);
    15021502    }
    15031503    curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
     
    15151515  curexport = ordexports;
    15161516  for(i=0;i<nrOrdExports;i++) {
    1517         if(curexport->ordinal == ordinal)
    1518                 return(curexport->virtaddr);
    1519         curexport++;
     1517    if(curexport->ordinal == ordinal)
     1518        return(curexport->virtaddr);
     1519    curexport++;
    15201520  }
    15211521  //Name exports also contain an ordinal, so check this
    15221522  nexport = nameexports;
    15231523  for(i=0;i<nrNameExports;i++) {
    1524         if(nexport->ordinal == ordinal)
    1525                 return(nexport->virtaddr);
    1526 
    1527         nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
     1524    if(nexport->ordinal == ordinal)
     1525        return(nexport->virtaddr);
     1526
     1527    nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
    15281528  }
    15291529  return(0);
     
    15541554
    15551555  if( r != MBID_IGNORE )
    1556         ExitProcess(987);
     1556    ExitProcess(987);
    15571557
    15581558  fIgnore = TRUE;
Note: See TracChangeset for help on using the changeset viewer.