Ignore:
Timestamp:
Oct 16, 2000, 9:15:16 PM (25 years ago)
Author:
sandervl
Message:

TLS fixes

File:
1 edited

Legend:

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

    r4481 r4489  
    1 /* $Id: winimagepeldr.cpp,v 1.61 2000-10-11 07:22:28 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.62 2000-10-16 19:15:16 sandervl Exp $ */
    22
    33/*
     
    8080 char logname[CCHMAXPATH];
    8181
    82         sprintf(logname, "pe_%d.log", loadNr);
    83         _privateLogFile = fopen(logname, "w");
    84         if(_privateLogFile == NULL) {
    85                 sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
    86                 _privateLogFile = fopen(logname, "w");
    87         }
    88         dprintfGlobal(("PE LOGFILE : %s", logname));
     82    sprintf(logname, "pe_%d.log", loadNr);
     83        _privateLogFile = fopen(logname, "w");
     84    if(_privateLogFile == NULL) {
     85        sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
     86            _privateLogFile = fopen(logname, "w");
     87    }
     88    dprintfGlobal(("PE LOGFILE : %s", logname));
    8989#endif
    9090}
     
    9494{
    9595#ifdef DEBUG
    96         if(_privateLogFile) {
    97                 fclose(_privateLogFile);
    98                 _privateLogFile = NULL;
    99         }
     96    if(_privateLogFile) {
     97        fclose(_privateLogFile);
     98        _privateLogFile = NULL;
     99    }
    100100#endif
    101101}
     
    114114  strupr(szFileName);
    115115  if(isExe) {
    116         if(!strchr(szFileName, '.')) {
    117                 strcat(szFileName,".EXE");
    118         }
    119         dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
    120         if(dllfile == NULL) {
    121                 if(!strstr(szFileName, ".EXE")) {
    122                         strcat(szFileName,".EXE");
    123                 }
    124                 dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
    125                 if(dllfile == NULL) {
    126                         OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
    127                 }
    128         }
    129         else    OSLibDosClose(dllfile);
     116    if(!strchr(szFileName, '.')) {
     117        strcat(szFileName,".EXE");
     118    }
     119    dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
     120    if(dllfile == NULL) {
     121        if(!strstr(szFileName, ".EXE")) {
     122            strcat(szFileName,".EXE");
     123        }
     124        dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
     125        if(dllfile == NULL) {
     126            OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
     127        }
     128    }
     129    else    OSLibDosClose(dllfile);
    130130  }
    131131  else {
    132         findDll(szFileName, szModule, sizeof(szModule));
    133         strcpy(szFileName, szModule);
     132    findDll(szFileName, szModule, sizeof(szModule));
     133    strcpy(szFileName, szModule);
    134134  }
    135135  strcpy(szModule, OSLibStripPath(szFileName));
     
    137137  char *dot = strstr(szModule, ".");
    138138  while(dot) {
    139         char *newdot = strstr(dot+1, ".");
    140         if(newdot == NULL)      break;
    141         dot = newdot;
     139    char *newdot = strstr(dot+1, ".");
     140    if(newdot == NULL)  break;
     141    dot = newdot;
    142142  }
    143143  if(dot)
    144         *dot = 0;
     144    *dot = 0;
    145145}
    146146//******************************************************************************
     
    149149{
    150150  if(memmap)
    151         delete memmap;
     151    delete memmap;
    152152
    153153  if(hFile) {
    154         OSLibDosClose(hFile);
    155         hFile = 0;
     154    OSLibDosClose(hFile);
     155    hFile = 0;
    156156  }
    157157
    158158  if(realBaseAddress)
    159         DosFreeMem((PVOID)realBaseAddress);
     159        DosFreeMem((PVOID)realBaseAddress);
    160160
    161161  if(nameexports)
    162         free(nameexports);
     162        free(nameexports);
    163163
    164164  if(ordexports)
    165         free(ordexports);
     165        free(ordexports);
    166166}
    167167//******************************************************************************
     
    184184  strcpy(szErrorModule, OSLibStripPath(szFileName));
    185185  if(hFile == NULL) {
    186         goto failure;
     186        goto failure;
    187187  }
    188188  //read dos header
    189189  if(DosRead(hFile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
    190         goto failure;
     190        goto failure;
    191191  }
    192192  if(OSLibDosSetFilePtr(hFile, doshdr.e_lfanew, OSLIB_SETPTR_FILE_BEGIN) == -1) {
    193         goto failure;
     193        goto failure;
    194194  }
    195195  //read signature dword
    196196  if(DosRead(hFile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
    197         goto failure;
     197        goto failure;
    198198  }
    199199  //read pe header
    200200  if(DosRead(hFile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
    201         goto failure;
     201        goto failure;
    202202  }
    203203  //read optional header
    204204  if(DosRead(hFile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
    205         goto failure;
     205        goto failure;
    206206  }
    207207  if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
    208         dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
     208        dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
    209209        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    210         goto failure;
     210        goto failure;
    211211  }
    212212
    213213  if(oh.SizeOfImage == 0) {//just in case
    214         oh.SizeOfImage = OSLibDosGetFileSize(hFile, NULL);
     214    oh.SizeOfImage = OSLibDosGetFileSize(hFile, NULL);
    215215  }
    216216
     
    218218  //Allocate memory to hold the entire image
    219219  if(allocSections(reservedMem) == FALSE) {
    220         dprintf((LOG, "Failed to allocate image memory for %s at %x, rc %d", szFileName, oh.ImageBase, errorState));;
    221         goto failure;
     220        dprintf((LOG, "Failed to allocate image memory for %s at %x, rc %d", szFileName, oh.ImageBase, errorState));;
     221        goto failure;
    222222  }
    223223
    224224  memmap = new Win32MemMap(this, realBaseAddress, imageSize);
    225225  if(memmap == NULL || !memmap->Init(0)) {
    226         goto failure;
     226        goto failure;
    227227  }
    228228  win32file = memmap->mapViewOfFile(0, 0, 2);
    229229
    230230  if(DosQueryPathInfo(szFileName, FIL_QUERYFULLNAME, szFullPath, sizeof(szFullPath)) == 0) {
    231         setFullPath(szFullPath);
     231    setFullPath(szFullPath);
    232232  }
    233233
    234234  if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
    235         dprintf((LOG, "Not a valid PE file!"));
     235        dprintf((LOG, "Not a valid PE file!"));
    236236        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    237         goto failure;
     237        goto failure;
    238238  }
    239239  if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
    240         dprintf((LOG, "Doesn't run on x86 processors!"));
     240        dprintf((LOG, "Doesn't run on x86 processors!"));
    241241        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    242         goto failure;
     242        goto failure;
    243243  }
    244244  //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
    245245  if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
    246         dprintf((LOG, "Can't convert system files"));
     246        dprintf((LOG, "Can't convert system files"));
    247247        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    248         goto failure;
     248        goto failure;
    249249  }
    250250
    251251  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    252         dprintf((LOG, "No fixups, might not run!"));
     252        dprintf((LOG, "No fixups, might not run!"));
    253253  }
    254254
     
    281281  if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
    282282  {
    283    imageSize = 0;
    284    if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL) {
    285     dprintf((LOG, "*************************PE SECTIONS START**************************" ));
    286     for (i=0; i<nSections; i++)
    287     {
    288         dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
    289         dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
    290         dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
    291         dprintf((LOG, "Virtual Address End:  %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
    292         dprintf((LOG, "Virtual Size:         %x", psh[i].Misc.VirtualSize ));
    293         dprintf((LOG, "Pointer to raw data:  %x", psh[i].PointerToRawData ));
    294         dprintf((LOG, "Section flags:        %x\n\n", psh[i].Characteristics ));
    295 
    296         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
     283    imageSize = 0;
     284    if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
     285    {
     286        dprintf((LOG, "*************************PE SECTIONS START**************************" ));
     287        for (i=0; i<nSections; i++)
    297288        {
    298             dprintf((LOG, ".reloc" ));
    299             addSection(SECTION_RELOC, psh[i].PointerToRawData,
    300                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    301                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    302             continue;
    303         }
    304         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
    305         {
    306             //SvL: Angus.exe has empty export section that's really an
    307             //     uninitialized data section
    308             if(psh[i].SizeOfRawData) {
    309                  dprintf((LOG, ".edata" ));
    310                  addSection(SECTION_EXPORT, psh[i].PointerToRawData,
    311                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    312                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
    313                  continue;
    314             }
    315         }
    316         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
    317         {
    318             dprintf((LOG, ".rsrc" ));
    319             addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
    320                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    321                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    322             continue;
    323         }
    324         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
    325         {
    326                 dprintf((LOG, "TLS section"));
    327                 tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
    328                 if(tlsDir) {
    329                         addSection(SECTION_TLS, psh[i].PointerToRawData,
    330                                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    331                                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    332                 }
    333                 continue;
    334         }
    335         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG)) 
    336         {
    337             dprintf((LOG, ".rdebug" ));
    338             addSection(SECTION_DEBUG,  psh[i].PointerToRawData,
    339                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    340                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    341             continue;
    342         }
    343         if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
    344         {
    345             int type = SECTION_IMPORT;
    346 
    347             dprintf((LOG, "Import Data Section" ));
    348             if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
    349                 dprintf((LOG, "Also Code Section"));
    350                 type |= SECTION_CODE;
    351             }
    352             addSection(type, psh[i].PointerToRawData,
    353                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    354                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    355             continue;
    356         }
    357 
    358         //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
    359         if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
    360            (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
    361            !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
    362             )
    363         {
    364             dprintf((LOG, "Code Section"));
    365             addSection(SECTION_CODE, psh[i].PointerToRawData,
    366                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    367                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    368             continue;
    369         }
    370         if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
    371             dprintf((LOG, "Read Only Data Section" ));
    372             addSection(SECTION_READONLYDATA, psh[i].PointerToRawData,
    373                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    374                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    375             continue;
    376         }
    377         if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
    378             dprintf((LOG, "Uninitialized Data Section" ));
    379             addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
    380                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    381                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    382             continue;
    383         }
    384         if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
    385             dprintf((LOG, "Initialized Data Section" ));
    386             addSection(SECTION_INITDATA, psh[i].PointerToRawData,
    387                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    388                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    389             continue;
    390         }
    391         if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
    392             dprintf((LOG, "Other Section, stored as read/write uninit data" ));
    393             addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
    394                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    395                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    396             continue;
    397         }
    398         dprintf((LOG, "Unknown section" ));
    399         goto failure;
    400      }
    401    }
     289            dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
     290            dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
     291            dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
     292            dprintf((LOG, "Virtual Address End:  %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
     293            dprintf((LOG, "Virtual Size:         %x", psh[i].Misc.VirtualSize ));
     294            dprintf((LOG, "Pointer to raw data:  %x", psh[i].PointerToRawData ));
     295            dprintf((LOG, "Section flags:        %x\n\n", psh[i].Characteristics ));
     296
     297            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
     298            {
     299                dprintf((LOG, ".reloc" ));
     300                addSection(SECTION_RELOC, psh[i].PointerToRawData,
     301                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     302                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     303                continue;
     304            }
     305            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
     306            {
     307                //SvL: Angus.exe has empty export section that's really an
     308                //     uninitialized data section
     309                if(psh[i].SizeOfRawData) {
     310                     dprintf((LOG, ".edata" ));
     311                     addSection(SECTION_EXPORT, psh[i].PointerToRawData,
     312                          psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     313                          psh[i].Misc.VirtualSize, psh[i].Characteristics);
     314                     continue;
     315                }
     316            }
     317            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
     318            {
     319                dprintf((LOG, ".rsrc" ));
     320                addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
     321                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     322                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     323                continue;
     324            }
     325            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
     326            {
     327                dprintf((LOG, "TLS section"));
     328                tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
     329                if(tlsDir) {
     330                        addSection(SECTION_TLS, psh[i].PointerToRawData,
     331                                   psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     332                                   psh[i].Misc.VirtualSize, psh[i].Characteristics);
     333                }
     334                continue;
     335            }
     336            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
     337            {
     338                dprintf((LOG, ".rdebug" ));
     339                addSection(SECTION_DEBUG,  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(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
     345            {
     346                int type = SECTION_IMPORT;
     347
     348                dprintf((LOG, "Import Data Section" ));
     349                if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
     350                    dprintf((LOG, "Also Code Section"));
     351                    type |= SECTION_CODE;
     352                }
     353                addSection(type, psh[i].PointerToRawData,
     354                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     355                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     356                continue;
     357            }
     358
     359            //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
     360            if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
     361               (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
     362               !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
     363                )
     364            {
     365                dprintf((LOG, "Code Section"));
     366                addSection(SECTION_CODE, psh[i].PointerToRawData,
     367                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     368                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     369                continue;
     370            }
     371            if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
     372                dprintf((LOG, "Read Only Data Section" ));
     373                addSection(SECTION_READONLYDATA, psh[i].PointerToRawData,
     374                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     375                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     376                continue;
     377            }
     378            if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
     379                dprintf((LOG, "Uninitialized Data Section" ));
     380                addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
     381                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     382                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     383                continue;
     384            }
     385            if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
     386                dprintf((LOG, "Initialized Data Section" ));
     387                addSection(SECTION_INITDATA, psh[i].PointerToRawData,
     388                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     389                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     390                continue;
     391            }
     392            if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
     393                dprintf((LOG, "Other Section, stored as read/write uninit data" ));
     394                addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
     395                       psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     396                       psh[i].Misc.VirtualSize, psh[i].Characteristics);
     397                continue;
     398            }
     399            dprintf((LOG, "Unknown section" ));
     400            goto failure;
     401         }
     402    }
    402403  }
    403404  else {
    404         if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh))
     405        if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh))
    405406        {
    406407            addSection(SECTION_RESOURCE, sh.PointerToRawData,
    407408                       sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
    408409                       sh.Misc.VirtualSize, sh.Characteristics);
    409         }
     410        }
    410411  }
    411412  dprintf((LOG, "*************************PE SECTIONS END **************************" ));
     
    417418
    418419  //In case there are any gaps between sections, adjust size
    419   if(imageSize != imageVirtEnd - oh.ImageBase) {
    420         dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
    421         imageSize = imageVirtEnd - oh.ImageBase;
     420  if(imageSize != imageVirtEnd - oh.ImageBase)
     421  {
     422    dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
     423    imageSize = imageVirtEnd - oh.ImageBase;
    422424  }
    423425  if(imageSize < oh.SizeOfImage) {
    424         imageSize = oh.SizeOfImage;
     426    imageSize = oh.SizeOfImage;
    425427  }
    426428
    427429  dprintf((LOG, "OS/2 base address %x", realBaseAddress ));
    428430  if(oh.AddressOfEntryPoint) {
    429         entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
     431    entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
    430432  }
    431433  else {
    432         dprintf((LOG, "EntryPoint == NULL" ));
    433         entryPoint = NULL;
     434    dprintf((LOG, "EntryPoint == NULL" ));
     435    entryPoint = NULL;
    434436  }
    435437
    436438  //set memory protection flags
    437439  if(setMemFlags() == FALSE) {
    438         dprintf((LOG, "Failed to set memory protection" ));
    439         goto failure;
     440    dprintf((LOG, "Failed to set memory protection" ));
     441    goto failure;
     442  }
     443
     444  if(realBaseAddress != oh.ImageBase) {
     445    pFixups     = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
     446    dwFixupSize = ImageDirectorySize(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
     447    commitPage((ULONG)pFixups, FALSE);
    440448  }
    441449
    442450  if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
    443451  {
    444    if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS))
    445    {
    446         Section *sect = findSectionByAddr(tlsDir->StartAddressOfRawData);
    447 
    448         dprintf((LOG, "TLS Directory" ));
    449         dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
    450         dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
    451         dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
    452         dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
    453         if(sect == NULL) {
    454                 dprintf((LOG, "Couldn't find TLS section!!" ));
    455                 goto failure;
    456         }
    457         setTLSAddress((char *)sect->realvirtaddr);
    458         setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
    459         setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
    460 
    461         sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
    462         if(sect == NULL) {
    463                 dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
    464                 goto failure;
    465         }
    466         setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
    467 
    468         if((ULONG)tlsDir->AddressOfCallBacks != 0) {
    469                 sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
    470                 if(sect == NULL) {
    471                         dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
    472                         goto failure;
    473                 }
    474                 setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
    475                 //modify tls callback pointers for new image base address
    476                 int i = 0;
    477                 while(tlsCallBackAddr[i]) {
    478                         tlsCallBackAddr[i] = (PIMAGE_TLS_CALLBACK)(realBaseAddress + ((ULONG)*tlsCallBackAddr - oh.ImageBase));
    479                         i++;
     452    if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS))
     453    {
     454        Section *sect;
     455        BOOL     fTLSFixups = FALSE;
     456
     457        sect = findSectionByAddr(tlsDir->StartAddressOfRawData);
     458        //There might be fixups for the TLS structure, so search the sections
     459        //by the OS/2 virtual address too
     460        if(sect == NULL) {
     461            sect = findSectionByOS2Addr(tlsDir->StartAddressOfRawData);
     462            fTLSFixups = TRUE;
     463        }
     464
     465        dprintf((LOG, "TLS Directory" ));
     466        dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
     467        dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
     468        dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
     469        dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
     470        if(sect == NULL) {
     471            dprintf((LOG, "Couldn't find TLS section!!" ));
     472            goto failure;
     473        }
     474        setTLSAddress((char *)sect->realvirtaddr);
     475        setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
     476        setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
     477
     478        fTLSFixups = FALSE;
     479        sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
     480        //There might be fixups for the TLS structure, so search the sections
     481        //by the OS/2 virtual address too
     482        if(sect == NULL) {
     483            sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
     484            fTLSFixups = TRUE;
     485        }
     486        if(sect == NULL) {
     487            dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
     488            goto failure;
     489        }
     490        if(fTLSFixups) {
     491            setTLSIndexAddr((LPDWORD)tlsDir->AddressOfIndex);  //no fixup required
     492        }
     493        else {//need to add a manual fixup
     494            setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
     495        }
     496
     497        if((ULONG)tlsDir->AddressOfCallBacks != 0)
     498        {
     499            fTLSFixups = FALSE;
     500
     501            sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
     502            //There might be fixups for the TLS structure, so search the sections
     503            //by the OS/2 virtual address too
     504            if(sect == NULL) {
     505                sect = findSectionByOS2Addr((ULONG)tlsDir->AddressOfIndex);
     506                fTLSFixups = TRUE;
     507            }
     508            if(sect == NULL) {
     509                dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
     510                goto failure;
     511            }
     512            if(fTLSFixups) {
     513                setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)tlsDir->AddressOfCallBacks); //no fixup required
     514            }
     515            else {//need to add a manual fixup
     516                setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
     517            }
     518            //modify tls callback pointers for new image base address
     519            int i = 0;
     520            while(tlsCallBackAddr[i])
     521            {
     522                fTLSFixups = FALSE;
     523
     524                sect = findSectionByAddr((ULONG)tlsCallBackAddr[i]);
     525                //There might be fixups for the TLS structure, so search the sections
     526                //by the OS/2 virtual address too
     527                if(sect == NULL) {
     528                    sect = findSectionByOS2Addr((ULONG)tlsCallBackAddr[i]);
     529                    fTLSFixups = TRUE;
    480530                }
    481         }
     531                if(sect == NULL) {
     532                    dprintf((LOG, "Couldn't find TLS callback section!!" ));
     533                    goto failure;
     534                }
     535                if(fTLSFixups) {
     536                        tlsCallBackAddr[i] = tlsCallBackAddr[i];
     537                }
     538                else    tlsCallBackAddr[i] = (PIMAGE_TLS_CALLBACK)(realBaseAddress + ((ULONG)tlsCallBackAddr[i] - oh.ImageBase));
     539                i++;
     540            }
     541        }
    482542   }
    483543
     
    488548        char *pszName;
    489549
    490         if(oh.DataDirectory[i].VirtualAddress && oh.DataDirectory[i].Size) {
    491                 switch (i)
    492                 {
    493                 case IMAGE_DIRECTORY_ENTRY_EXPORT:      pszName = "Export Directory (IMAGE_DIRECTORY_ENTRY_EXPORT)"; break;
    494                 case IMAGE_DIRECTORY_ENTRY_IMPORT:      pszName = "Import Directory (IMAGE_DIRECTORY_ENTRY_IMPORT)"; break;
    495                 case IMAGE_DIRECTORY_ENTRY_RESOURCE:    pszName = "Resource Directory (IMAGE_DIRECTORY_ENTRY_RESOURCE)"; break;
    496                 case IMAGE_DIRECTORY_ENTRY_EXCEPTION:   pszName = "Exception Directory (IMAGE_DIRECTORY_ENTRY_EXCEPTION)"; break;
    497                 case IMAGE_DIRECTORY_ENTRY_SECURITY:    pszName = "Security Directory (IMAGE_DIRECTORY_ENTRY_SECURITY)"; break;
    498                 case IMAGE_DIRECTORY_ENTRY_BASERELOC:   pszName = "Base Relocation Table (IMAGE_DIRECTORY_ENTRY_BASERELOC)"; break;
    499                 case IMAGE_DIRECTORY_ENTRY_DEBUG:       pszName = "Debug Directory (IMAGE_DIRECTORY_ENTRY_DEBUG)"; break;
    500                 case IMAGE_DIRECTORY_ENTRY_COPYRIGHT:   pszName = "Description String (IMAGE_DIRECTORY_ENTRY_COPYRIGHT)"; break;
    501                 case IMAGE_DIRECTORY_ENTRY_GLOBALPTR:   pszName = "Machine Value (MIPS GP) (IMAGE_DIRECTORY_ENTRY_GLOBALPTR)"; break;
    502                 case IMAGE_DIRECTORY_ENTRY_TLS:         pszName = "TLS Directory (IMAGE_DIRECTORY_ENTRY_TLS)"; break;
    503                 case IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: pszName = "Load Configuration Directory (IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG)"; break;
    504                 case IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT:pszName = "Bound Import Directory in headers (IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT)"; break;
    505                 case IMAGE_DIRECTORY_ENTRY_IAT:         pszName = "Import Address Table (IMAGE_DIRECTORY_ENTRY_IAT)"; break;
    506                 default:
    507                     pszName = "unknown";
    508                 }
    509                 dprintf((LOG, "directory %s", pszName));
    510                 dprintf((LOG, "          Address    0x%08x", oh.DataDirectory[i].VirtualAddress));
    511                 dprintf((LOG, "          Size       0x%08x", oh.DataDirectory[i].Size));
    512         }
    513    }
    514    dprintf((LOG, "\n\n"));
     550        if(oh.DataDirectory[i].VirtualAddress && oh.DataDirectory[i].Size) {
     551            switch (i)
     552            {
     553            case IMAGE_DIRECTORY_ENTRY_EXPORT:      pszName = "Export Directory (IMAGE_DIRECTORY_ENTRY_EXPORT)"; break;
     554            case IMAGE_DIRECTORY_ENTRY_IMPORT:      pszName = "Import Directory (IMAGE_DIRECTORY_ENTRY_IMPORT)"; break;
     555            case IMAGE_DIRECTORY_ENTRY_RESOURCE:    pszName = "Resource Directory (IMAGE_DIRECTORY_ENTRY_RESOURCE)"; break;
     556            case IMAGE_DIRECTORY_ENTRY_EXCEPTION:   pszName = "Exception Directory (IMAGE_DIRECTORY_ENTRY_EXCEPTION)"; break;
     557            case IMAGE_DIRECTORY_ENTRY_SECURITY:    pszName = "Security Directory (IMAGE_DIRECTORY_ENTRY_SECURITY)"; break;
     558            case IMAGE_DIRECTORY_ENTRY_BASERELOC:   pszName = "Base Relocation Table (IMAGE_DIRECTORY_ENTRY_BASERELOC)"; break;
     559            case IMAGE_DIRECTORY_ENTRY_DEBUG:       pszName = "Debug Directory (IMAGE_DIRECTORY_ENTRY_DEBUG)"; break;
     560            case IMAGE_DIRECTORY_ENTRY_COPYRIGHT:   pszName = "Description String (IMAGE_DIRECTORY_ENTRY_COPYRIGHT)"; break;
     561            case IMAGE_DIRECTORY_ENTRY_GLOBALPTR:   pszName = "Machine Value (MIPS GP) (IMAGE_DIRECTORY_ENTRY_GLOBALPTR)"; break;
     562            case IMAGE_DIRECTORY_ENTRY_TLS:         pszName = "TLS Directory (IMAGE_DIRECTORY_ENTRY_TLS)"; break;
     563            case IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: pszName = "Load Configuration Directory (IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG)"; break;
     564            case IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT:pszName = "Bound Import Directory in headers (IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT)"; break;
     565            case IMAGE_DIRECTORY_ENTRY_IAT:         pszName = "Import Address Table (IMAGE_DIRECTORY_ENTRY_IAT)"; break;
     566            default:
     567                pszName = "unknown";
     568            }
     569            dprintf((LOG, "directory %s", pszName));
     570            dprintf((LOG, "          Address    0x%08x", oh.DataDirectory[i].VirtualAddress));
     571            dprintf((LOG, "          Size       0x%08x", oh.DataDirectory[i].Size));
     572        }
     573    }
     574    dprintf((LOG, "\n\n"));
    515575#endif
    516576
    517    if(realBaseAddress != oh.ImageBase) {
    518         pFixups     = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
    519         dwFixupSize = ImageDirectorySize(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
    520         commitPage((ULONG)pFixups, FALSE);
    521    }
    522577#ifdef COMMIT_ALL
    523    for (i=0; i<nSections; i++) {
    524         commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
    525    }
     578    for (i=0; i<nSections; i++) {
     579        commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
     580    }
    526581#else
    527    for (i=0; i<nSections; i++) {
    528         switch(section[i].type)
    529         {
    530         case SECTION_IMPORT:
    531         case SECTION_RELOC:
    532         case SECTION_EXPORT:
    533                 commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
    534                 break;
    535         }
    536    }
     582    for (i=0; i<nSections; i++) {
     583        switch(section[i].type)
     584        {
     585        case SECTION_IMPORT:
     586        case SECTION_RELOC:
     587        case SECTION_EXPORT:
     588            commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
     589            break;
     590        }
     591    }
    537592#endif
    538    if(processExports((char *)win32file) == FALSE) {
    539         dprintf((LOG, "Failed to process exported apis" ));
    540         goto failure;
    541    }
     593    if(processExports((char *)win32file) == FALSE) {
     594        dprintf((LOG, "Failed to process exported apis" ));
     595        goto failure;
     596    }
    542597  }
    543598#ifdef COMMIT_ALL
    544599  else {
    545         commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
     600        commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
    546601  }
    547602#endif
     
    556611  //    implicitly call functions depending on it.
    557612  if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh)) {
    558         //get offset in resource object of directory entry
    559         pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
    560         ulRVAResourceSection = sh.VirtualAddress;
     613    //get offset in resource object of directory entry
     614    pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
     615    ulRVAResourceSection = sh.VirtualAddress;
    561616  }
    562617
    563618  if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
    564619  {
    565         if(processImports((char *)win32file) == FALSE) {
    566              dprintf((LOG, "Failed to process imports!" ));
    567              goto failure;
    568         }
     620    if(processImports((char *)win32file) == FALSE) {
     621        dprintf((LOG, "Failed to process imports!" ));
     622        goto failure;
     623    }
    569624  }
    570625  return(TRUE);
     
    572627failure:
    573628  if(memmap) {
    574         delete memmap;
    575         memmap = NULL;
     629    delete memmap;
     630    memmap = NULL;
    576631  }
    577632  if(hFile) {
    578         OSLibDosClose(hFile);
    579         hFile = 0;
     633    OSLibDosClose(hFile);
     634    hFile = 0;
    580635  }
    581636  errorState = ERROR_INTERNAL;
     
    595650  //Round down to nearest page boundary
    596651  virtAddress = virtAddress & ~0xFFF;
    597  
     652
    598653  section = findSectionByOS2Addr(virtAddress);
    599654  if(section == NULL) {
    600         size        = 4096;
    601         sectionsize = 4096;
    602         protflags   = PAG_READ|PAG_WRITE; //readonly?
    603         section = findPreviousSectionByOS2Addr(virtAddress);
    604         if(section == NULL) {//access to header
    605                 offset     = 0;
    606                 fileoffset = virtAddress - realBaseAddress;
    607         }
    608         else {
    609                 offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
    610                 fileoffset = section->rawoffset + section->rawsize + offset;
    611         }
     655    size        = 4096;
     656    sectionsize = 4096;
     657    protflags   = PAG_READ|PAG_WRITE; //readonly?
     658    section = findPreviousSectionByOS2Addr(virtAddress);
     659    if(section == NULL) {//access to header
     660        offset     = 0;
     661        fileoffset = virtAddress - realBaseAddress;
     662    }
     663    else {
     664        offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
     665        fileoffset = section->rawoffset + section->rawsize + offset;
     666    }
    612667  }
    613668  else {
    614         protflags   = section->pageflags;
    615         offset      = virtAddress - section->realvirtaddr;
    616         sectionsize = section->virtualsize - offset;
    617 
    618         if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
    619                 //unintialized data (set to 0)
    620                 size = 0;
    621                 fileoffset = -1;
    622         }
    623         else {
    624                 size = section->rawsize-offset;
    625                 fileoffset = section->rawoffset + offset;
    626         }
    627         if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
    628                 dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
    629                 return FALSE;
    630         }
     669    protflags   = section->pageflags;
     670    offset      = virtAddress - section->realvirtaddr;
     671    sectionsize = section->virtualsize - offset;
     672
     673    if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
     674        //unintialized data (set to 0)
     675        size = 0;
     676        fileoffset = -1;
     677    }
     678    else {
     679        size = section->rawsize-offset;
     680        fileoffset = section->rawoffset + offset;
     681    }
     682    if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
     683        dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
     684        return FALSE;
     685    }
    631686  }
    632687  //Check range of pages with the same attributes starting at virtAddress
     
    635690  rc = DosQueryMem((PVOID)virtAddress, &range, &attr);
    636691  if(rc) {
    637         dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
    638         return FALSE;
     692    dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
     693    return FALSE;
    639694  }
    640695  if(attr & PAG_COMMIT) {
    641         dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
    642         return FALSE;
     696    dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
     697    return FALSE;
    643698  }
    644699
    645700  if(fPageCmd == SINGLE_PAGE) {
    646         size = min(size, PAGE_SIZE);
    647         sectionsize = min(sectionsize, PAGE_SIZE);
     701    size = min(size, PAGE_SIZE);
     702    sectionsize = min(sectionsize, PAGE_SIZE);
    648703  }
    649704  else
    650705  if(fPageCmd == SECTION_PAGES) {
    651         size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
    652         sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
     706    size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
     707    sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
    653708  }
    654709  size = min(size, range);
     
    656711
    657712  if(fileoffset != -1) {
    658         rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
    659         if(rc) {
    660                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    661                 return FALSE;
    662         }
    663 
    664         if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
    665                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
    666                 return FALSE;
    667         }
    668         if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
    669                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
    670                 return FALSE;
    671         }
    672         if(ulRead != size) {
    673                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
    674                 return FALSE;
    675         }
    676         setFixups(virtAddress, sectionsize);
    677 
    678         rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
    679         if(rc) {
    680                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    681                 return FALSE;
    682         }
     713    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
     714    if(rc) {
     715        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     716            return FALSE;
     717    }
     718
     719    if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
     720        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
     721            return FALSE;
     722    }
     723    if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
     724        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
     725            return FALSE;
     726    }
     727    if(ulRead != size) {
     728        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
     729            return FALSE;
     730    }
     731    setFixups(virtAddress, sectionsize);
     732
     733    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
     734    if(rc) {
     735        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     736        return FALSE;
     737    }
    683738  }
    684739  else {
    685         rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
    686         if(rc) {
    687                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    688                 return FALSE;
    689         }
    690         setFixups(virtAddress, sectionsize);
    691 
    692         rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
    693         if(rc) {
    694                 dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
    695                 return FALSE;
    696         }
     740    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
     741    if(rc) {
     742        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     743            return FALSE;
     744    }
     745    setFixups(virtAddress, sectionsize);
     746
     747    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
     748    if(rc) {
     749        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
     750        return FALSE;
     751    }
    697752  }
    698753  return TRUE;
     
    715770
    716771  if(virtaddress < imageVirtBase)
    717         imageVirtBase = virtaddress;
     772        imageVirtBase = virtaddress;
    718773  if(virtaddress + virtsize > imageVirtEnd)
    719         imageVirtEnd  = virtaddress + virtsize;
     774        imageVirtEnd  = virtaddress + virtsize;
    720775
    721776  nrsections++;
     
    732787  //Allocated in peldr.dll
    733788  if(reservedMem && reservedMem == oh.ImageBase) {
    734         realBaseAddress = oh.ImageBase;
    735         return TRUE;
     789    realBaseAddress = oh.ImageBase;
     790    return TRUE;
    736791  }
    737792
    738793  //SvL: We don't care where the image is loaded for resource lookup
    739794  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
    740         return allocFixedMem(reservedMem);
     795        return allocFixedMem(reservedMem);
    741796  }
    742797  rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
    743798  if(rc) {
    744         dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
    745         errorState = rc;
    746         return(FALSE);
     799    dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
     800        errorState = rc;
     801        return(FALSE);
    747802  }
    748803  realBaseAddress = baseAddress;
     
    754809{
    755810  for(int i=0;i<nrsections;i++) {
    756         if(section[i].type == type) {
    757                 return &section[i];
    758         }
     811    if(section[i].type == type) {
     812        return &section[i];
     813    }
    759814  }
    760815  return NULL;
     
    765820{
    766821  for(int i=0;i<nrsections;i++) {
    767         if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
    768                 return &section[i];
    769         }
     822    if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
     823        return &section[i];
     824    }
    770825  }
    771826  return NULL;
     
    776831{
    777832  for(int i=0;i<nrsections;i++) {
    778         if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
    779                 return &section[i];
    780         }
     833    if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
     834        return &section[i];
     835    }
    781836  }
    782837  return NULL;
     
    790845
    791846  for(int i=0;i<nrsections;i++) {
    792         if(section[i].realvirtaddr > addr) {
    793                 if(section[i].realvirtaddr < lowestAddr) {
    794                         lowestAddr = section[i].realvirtaddr;
    795                         index = i;
    796                 }
    797         }
     847    if(section[i].realvirtaddr > addr) {
     848        if(section[i].realvirtaddr < lowestAddr) {
     849            lowestAddr = section[i].realvirtaddr;
     850            index = i;
     851        }
     852    }
    798853  }
    799854  if(index == -1)
    800         return NULL;
     855    return NULL;
    801856
    802857  return &section[index];
     
    819874  memallocs = (ULONG *)malloc(4096*sizeof(ULONG *));
    820875  if(memallocs == NULL) {
    821         dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
    822         return FALSE;
     876    dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
     877    return FALSE;
    823878  }
    824879
    825880  if(oh.ImageBase < 512*1024*1024) {
    826         allocFlags = 0;
     881    allocFlags = 0;
    827882  }
    828883  while(TRUE) {
    829         rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
    830         if(rc) break;
    831 
    832         dprintf((LOG, "DosAllocMem returned %x", address ));
    833         if(address + FALLOC_SIZE >= oh.ImageBase) {
    834                 if(address > oh.ImageBase) {//we've passed it!
    835                         DosFreeMem((PVOID)address);
    836                         break;
    837                 }
    838                 //found the right address
    839                 DosFreeMem((PVOID)address);
    840 
    841                 diff = oh.ImageBase - address;
    842                 if(diff) {
    843                         rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
    844                         if(rc) break;
    845                 }
    846                 rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
    847                 if(rc) break;
    848 
    849                 if(diff) DosFreeMem((PVOID)address);
    850 
    851                 realBaseAddress = baseAddress;
    852                 break;
    853         }
    854         memallocs[alloccnt++] = address;
     884        rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
     885        if(rc) break;
     886
     887        dprintf((LOG, "DosAllocMem returned %x", address ));
     888        if(address + FALLOC_SIZE >= oh.ImageBase) {
     889            if(address > oh.ImageBase) {//we've passed it!
     890                    DosFreeMem((PVOID)address);
     891                    break;
     892            }
     893            //found the right address
     894            DosFreeMem((PVOID)address);
     895
     896            diff = oh.ImageBase - address;
     897            if(diff) {
     898                    rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
     899                    if(rc) break;
     900            }
     901            rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
     902            if(rc) break;
     903
     904            if(diff) DosFreeMem((PVOID)address);
     905
     906            realBaseAddress = baseAddress;
     907            break;
     908        }
     909    memallocs[alloccnt++] = address;
    855910  }
    856911  for(i=0;i<alloccnt;i++) {
    857         DosFreeMem((PVOID)memallocs[i]);
     912        DosFreeMem((PVOID)memallocs[i]);
    858913  }
    859914  free(memallocs);
    860915
    861916  if(realBaseAddress == 0) //Let me guess.. MS Office app?
    862         return(FALSE);
     917        return(FALSE);
    863918
    864919  return(TRUE);
     
    878933  // Process all the image sections
    879934  for(i=0;i<nrsections;i++) {
    880         section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
     935        section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
    881936  }
    882937
    883938  for(i=0;i<nrsections;i++) {
    884         switch(section[i].type)
    885         {
     939        switch(section[i].type)
     940    {
    886941        case SECTION_CODE:
    887942        case (SECTION_CODE | SECTION_IMPORT):
    888                 section[i].pageflags = PAG_EXECUTE | PAG_READ;
    889                 if(section[i].flags & IMAGE_SCN_MEM_WRITE)
    890                         section[i].pageflags |= PAG_WRITE;
    891                 break;
     943                section[i].pageflags = PAG_EXECUTE | PAG_READ;
     944        if(section[i].flags & IMAGE_SCN_MEM_WRITE)
     945            section[i].pageflags |= PAG_WRITE;
     946                break;
    892947        case SECTION_INITDATA:
    893948        case SECTION_UNINITDATA:
    894949        case SECTION_IMPORT:
    895         case SECTION_TLS:
    896                 section[i].pageflags = PAG_WRITE | PAG_READ;
    897                 break;
     950    case SECTION_TLS:
     951                section[i].pageflags = PAG_WRITE | PAG_READ;
     952                break;
    898953
    899954        case SECTION_RESOURCE:
    900                 //TODO: GDI32 changes some bitmap structures to avoid problems in Open32
     955        //TODO: GDI32 changes some bitmap structures to avoid problems in Open32
    901956                //      -> causes crashes if resource section is readonly
    902957                //      -> make it readonly again when gdi32 has been rewritten
    903                 section[i].pageflags = PAG_WRITE | PAG_READ;
    904                 break;
     958                section[i].pageflags = PAG_WRITE | PAG_READ;
     959        break;
    905960
    906961        case SECTION_READONLYDATA:
    907962        case SECTION_EXPORT:
    908963        default:
    909                 section[i].pageflags = PAG_READ;
    910                 break;
    911         }
    912         if(section[i].flags & (IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
    913                 //SvL: sometimes i.e. import/export sections also contain data
     964                section[i].pageflags = PAG_READ;
     965                break;
     966        }
     967    if(section[i].flags & (IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
     968        //SvL: sometimes i.e. import/export sections also contain data
    914969                //     must make them read/write
    915                 section[i].pageflags = PAG_WRITE;
     970                section[i].pageflags = PAG_WRITE;
    916971        }
    917972
     
    930985
    931986  if(realBaseAddress == oh.ImageBase || fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    932         return(TRUE);
     987        return(TRUE);
    933988  }
    934989
     
    937992  size  = (size-1) & ~0xFFF;
    938993  size += PAGE_SIZE;
    939  
     994
    940995  if(prel) {
    941         j = 1;
    942         while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
    943               prel->VirtualAddress && prel->VirtualAddress < virtAddress) 
     996    j = 1;
     997    while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
     998              prel->VirtualAddress && prel->VirtualAddress < virtAddress)
    944999        {
    945                 prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
    946         }
    947         while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
    948               prel->VirtualAddress && prel->VirtualAddress < virtAddress + size) 
     1000            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
     1001    }
     1002    while(((ULONG)prel < (ULONG)pFixups+dwFixupSize) &&
     1003              prel->VirtualAddress && prel->VirtualAddress < virtAddress + size)
    9491004        {
    950                 page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
    951                 count  = (prel->SizeOfBlock - 8)/2;
    952                 j++;
    953                 for(i=0;i<count;i++) {
    954                         int type   = prel->TypeOffset[i] >> 12;
    955                         int offset = prel->TypeOffset[i] & 0xFFF;
    956                         int fixupsize = 0;
    957 
    958                         switch(type)
    959                         {
    960                         case IMAGE_REL_BASED_HIGHLOW:
    961                                 fixupsize = 4;
    962                                 break;
    963                         case IMAGE_REL_BASED_HIGH:
    964                         case IMAGE_REL_BASED_LOW:
    965                                 fixupsize = 2;
    966                                 break;
    967                         }
    968                         //If the fixup crosses the final page boundary,
    969                         //then we have to load another page
    970                         if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
    971                         {
    972                                 newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
    973                                 newpage &= ~0xFFF;
    974 
    975                                 section  = findSectionByOS2Addr(newpage);
    976                                 if(section == NULL) {
    977                                         //should never happen
    978                                         dprintf((LOG, "::setFixups -> section == NULL!!"));
    979                                         return FALSE;
    980                                 }
    981                                 //SvL: Read page from disk
    982                                 commitPage(newpage, FALSE, SINGLE_PAGE);
    983 
    984                                 //SvL: Enable write access
    985                                 DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
    986                         }
    987 
    988                         switch(type)
    989                         {
    990                         case IMAGE_REL_BASED_ABSOLUTE:
    991                                 break;  //skip
    992                         case IMAGE_REL_BASED_HIGHLOW:
    993                                 AddOff32Fixup(prel->VirtualAddress + offset);
    994                                 break;
    995                         case IMAGE_REL_BASED_HIGH:
    996                                 AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
    997                                 break;
    998                         case IMAGE_REL_BASED_LOW:
    999                                 AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
    1000                                 break;
    1001                         case IMAGE_REL_BASED_HIGHADJ:
    1002                         case IMAGE_REL_BASED_MIPS_JMPADDR:
    1003                         default:
    1004                                 break;
    1005                         }
    1006                         if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
    1007                         {
    1008                                 //SvL: Restore original page protection flags
    1009                                 DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
    1010                         }
    1011                 }
    1012                 prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
    1013         }//while
     1005            page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
     1006            count  = (prel->SizeOfBlock - 8)/2;
     1007            j++;
     1008            for(i=0;i<count;i++) {
     1009                int type   = prel->TypeOffset[i] >> 12;
     1010                int offset = prel->TypeOffset[i] & 0xFFF;
     1011            int fixupsize = 0;
     1012
     1013                switch(type)
     1014            {
     1015                case IMAGE_REL_BASED_HIGHLOW:
     1016                fixupsize = 4;
     1017                break;
     1018                    case IMAGE_REL_BASED_HIGH:
     1019                    case IMAGE_REL_BASED_LOW:
     1020                fixupsize = 2;
     1021                break;
     1022            }
     1023            //If the fixup crosses the final page boundary,
     1024            //then we have to load another page
     1025            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
     1026            {
     1027                newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
     1028                newpage &= ~0xFFF;
     1029
     1030                    section  = findSectionByOS2Addr(newpage);
     1031                    if(section == NULL) {
     1032                    //should never happen
     1033                    dprintf((LOG, "::setFixups -> section == NULL!!"));
     1034                    return FALSE;
     1035                    }
     1036                //SvL: Read page from disk
     1037                commitPage(newpage, FALSE, SINGLE_PAGE);
     1038
     1039                    //SvL: Enable write access
     1040                    DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
     1041            }
     1042
     1043                switch(type)
     1044            {
     1045                case IMAGE_REL_BASED_ABSOLUTE:
     1046                    break;  //skip
     1047                case IMAGE_REL_BASED_HIGHLOW:
     1048                        AddOff32Fixup(prel->VirtualAddress + offset);
     1049                        break;
     1050                    case IMAGE_REL_BASED_HIGH:
     1051                AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
     1052                break;
     1053                    case IMAGE_REL_BASED_LOW:
     1054                AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
     1055                break;
     1056                    case IMAGE_REL_BASED_HIGHADJ:
     1057                    case IMAGE_REL_BASED_MIPS_JMPADDR:
     1058                default:
     1059                        break;
     1060                }
     1061            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
     1062            {
     1063                    //SvL: Restore original page protection flags
     1064                    DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
     1065            }
     1066            }
     1067            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
     1068        }//while
    10141069  }
    10151070  else {
    1016         dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
    1017         return(FALSE);
     1071    dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
     1072        return(FALSE);
    10181073  }
    10191074  return(TRUE);
     
    10281083
    10291084  if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
    1030         return(TRUE);
     1085        return(TRUE);
    10311086  }
    10321087
     
    10501105                break;
    10511106            case IMAGE_REL_BASED_HIGH:
    1052                 AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
    1053                 break;
     1107        AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
     1108        break;
    10541109            case IMAGE_REL_BASED_LOW:
    1055                 AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
    1056                 break;
     1110        AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
     1111        break;
    10571112            case IMAGE_REL_BASED_HIGHADJ:
    10581113            case IMAGE_REL_BASED_MIPS_JMPADDR:
     
    10941149  orgaddr = *fixup;
    10951150  if(fHighFixup) {
    1096         *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
     1151    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
    10971152//        dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    10981153  }
    10991154  else {
    1100         *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
     1155    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
    11011156//        dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    11021157  }
     
    11131168  if(apiaddr == 0)
    11141169  {
    1115         dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
     1170        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
    11161171                 WinDll->getName(),
    11171172                 ordinal));
    11181173
    1119         dprintf((LOG, "--->>> NOT FOUND!" ));
    1120         *import = (ULONG)MissingApi;
     1174        dprintf((LOG, "--->>> NOT FOUND!" ));
     1175        *import = (ULONG)MissingApi;
    11211176  }
    11221177  else  *import = apiaddr;
     
    11331188  if(apiaddr == 0)
    11341189  {
    1135         dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
     1190        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
    11361191                WinDll->getName(),
    11371192                impname));
    11381193
    1139         dprintf((LOG, "--->>> NOT FOUND!" ));
    1140         *import = (ULONG)MissingApi;
     1194        dprintf((LOG, "--->>> NOT FOUND!" ));
     1195        *import = (ULONG)MissingApi;
    11411196  }
    11421197  else  *import = apiaddr;
     
    11701225    int   ord, RVAExport;
    11711226    char *name;
    1172     for(i=0;i<ped->NumberOfNames;i++) 
     1227    for(i=0;i<ped->NumberOfNames;i++)
    11731228    {
    1174         fForwarder = FALSE;
     1229    fForwarder = FALSE;
    11751230        ord        = ptrOrd[i] + ped->Base;
    11761231        name       = (char *)((ULONG)ptrNames[i] + (ULONG)win32file);
     
    11801235        if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
    11811236           RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
    1182                        + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size) 
     1237                       + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
    11831238        {
    11841239            fForwarder = AddForwarder(oh.ImageBase + RVAExport, name, ord);
    11851240        }
    1186         if(!fForwarder) {
     1241    if(!fForwarder) {
    11871242            //points to code (virtual address relative to oh.ImageBase
    11881243            AddNameExport(oh.ImageBase + RVAExport, name, ord);
     
    11901245        }
    11911246    }
    1192     for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++) 
     1247    for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++)
    11931248    {
    1194         fForwarder = FALSE;
     1249    fForwarder = FALSE;
    11951250        ord = ped->Base + i;  //Correct??
    11961251        RVAExport = ptrAddress[i];
     
    11981253        if(RVAExport > oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress &&
    11991254           RVAExport < oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress
    1200                        + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size) 
     1255                       + oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size)
    12011256        {
    12021257            fForwarder = AddForwarder(oh.ImageBase + RVAExport, NULL, ord);
    12031258        }
    1204         if(!fForwarder && RVAExport) {
     1259    if(!fForwarder && RVAExport) {
    12051260            //points to code (virtual address relative to oh.ImageBase
    12061261            dprintf((LOG, "ord %d at 0x%08x (0x%08x)", ord, RVAExport, realBaseAddress + RVAExport));
     
    12181273
    12191274  if(nameexports == NULL) {
    1220         nameExportSize= 4096;
    1221         nameexports   = (NameExport *)malloc(nameExportSize);
    1222         curnameexport = nameexports;
     1275        nameExportSize= 4096;
     1276        nameexports   = (NameExport *)malloc(nameExportSize);
     1277        curnameexport = nameexports;
    12231278  }
    12241279  nsize = (ULONG)curnameexport - (ULONG)nameexports;
    12251280  if(nsize + sizeof(NameExport) + strlen(apiname) > nameExportSize) {
    1226         nameExportSize += 4096;
    1227         char *tmp = (char *)nameexports;
    1228         nameexports = (NameExport *)malloc(nameExportSize);
    1229         memcpy(nameexports, tmp, nsize);
    1230         curnameexport = (NameExport *)((ULONG)nameexports + nsize);
    1231         free(tmp);
     1281        nameExportSize += 4096;
     1282        char *tmp = (char *)nameexports;
     1283        nameexports = (NameExport *)malloc(nameExportSize);
     1284        memcpy(nameexports, tmp, nsize);
     1285        curnameexport = (NameExport *)((ULONG)nameexports + nsize);
     1286        free(tmp);
    12321287  }
    12331288  if(fAbsoluteAddress) {//forwarders use absolute address
    1234         curnameexport->virtaddr = virtaddr;
    1235   }
    1236   else  curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     1289    curnameexport->virtaddr = virtaddr;
     1290  }
     1291  else  curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
    12371292  curnameexport->ordinal  = ordinal;
    12381293  *(ULONG *)curnameexport->name = 0;
     
    12411296  curnameexport->nlength = strlen(apiname) + 1;
    12421297  if(curnameexport->nlength < sizeof(curnameexport->name))
    1243         curnameexport->nlength = sizeof(curnameexport->name);
     1298        curnameexport->nlength = sizeof(curnameexport->name);
    12441299
    12451300  curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
     
    12501305{
    12511306  if(ordexports == NULL) {
    1252         ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
    1253         curordexport = ordexports;
     1307        ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
     1308        curordexport = ordexports;
    12541309  }
    12551310  if(fAbsoluteAddress) {//forwarders use absolute address
    1256         curordexport->virtaddr = virtaddr;
    1257   }
    1258   else  curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     1311    curordexport->virtaddr = virtaddr;
     1312  }
     1313  else  curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
    12591314  curordexport->ordinal  = ordinal;
    12601315  curordexport++;
     
    12691324 DWORD         exportaddr;
    12701325 int           forwardord;
    1271  
     1326
    12721327  forwarddll = strdup(forward);
    12731328  if(forwarddll == NULL) {
    1274         return FALSE;
     1329    return FALSE;
    12751330  }
    12761331  forwardapi = strchr(forwarddll, '.');
    12771332  if(forwardapi == NULL) {
    1278         goto fail;
     1333    goto fail;
    12791334  }
    12801335  *forwardapi++ = 0;
    12811336  if(strlen(forwarddll) == 0 || strlen(forwardapi) == 0) {
    1282         goto fail;
     1337    goto fail;
    12831338  }
    12841339  WinDll = Win32DllBase::findModule(forwarddll);
    12851340  if(WinDll == NULL) {
    1286         WinDll = loadDll(forwarddll);
    1287         if(WinDll == NULL) {
    1288                 dprintf((LOG, "ERROR: couldn't find forwarder %s.%s", forwarddll, forwardapi));
    1289                 goto fail;
    1290         }
     1341    WinDll = loadDll(forwarddll);
     1342    if(WinDll == NULL) {
     1343        dprintf((LOG, "ERROR: couldn't find forwarder %s.%s", forwarddll, forwardapi));
     1344        goto fail;
     1345    }
    12911346  }
    12921347  //check if name or ordinal forwarder
    12931348  forwardord = 0;
    12941349  if(*forwardapi >= '0' && *forwardapi <= '9') {
    1295         forwardord = atoi(forwardapi);
     1350    forwardord = atoi(forwardapi);
    12961351  }
    12971352  if(forwardord != 0 || (strlen(forwardapi) == 1 && *forwardapi == '0')) {
    1298         exportaddr = WinDll->getApi(forwardord);
    1299   }
    1300   else  exportaddr = WinDll->getApi(forwardapi);
     1353    exportaddr = WinDll->getApi(forwardord);
     1354  }
     1355  else  exportaddr = WinDll->getApi(forwardapi);
    13011356
    13021357  if(apiname) {
    13031358        dprintf((LOG, "address 0x%x %s @%d (0x%08x) forwarder %s.%s", virtaddr - oh.ImageBase, apiname, ordinal, virtaddr, forwarddll, forwardapi));
    1304         AddNameExport(exportaddr, apiname, ordinal, TRUE);
     1359    AddNameExport(exportaddr, apiname, ordinal, TRUE);
    13051360  }
    13061361  else {
     
    13221377 char modname[CCHMAXPATH];
    13231378
    1324         strcpy(modname, pszCurModule);
    1325         //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
    1326         Win32DllBase::renameDll(modname);
    1327 
    1328         if(isPEImage(modname) != ERROR_SUCCESS_W)
    1329         {//LX image, so let OS/2 do all the work for us
    1330                 APIRET rc;
    1331                 char   szModuleFailure[CCHMAXPATH] = "";
    1332                 ULONG  hInstanceNewDll;
     1379    strcpy(modname, pszCurModule);
     1380    //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
     1381    Win32DllBase::renameDll(modname);
     1382
     1383    if(isPEImage(modname) != ERROR_SUCCESS_W)
     1384    {//LX image, so let OS/2 do all the work for us
     1385        APIRET rc;
     1386        char   szModuleFailure[CCHMAXPATH] = "";
     1387        ULONG  hInstanceNewDll;
    13331388                Win32LxDll *lxdll;
    13341389
    1335                 char *dot = strchr(modname, '.');
    1336                 if(dot) {
    1337                         *dot = 0;
    1338                 }
    1339                 strcat(modname, ".DLL");
    1340                 rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
    1341                 if(rc) {
    1342                         dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
    1343                         sprintf(szErrorModule, "%s.DLL", szModuleFailure);
    1344                         errorState = rc;
    1345                         return NULL;
    1346                 }
    1347                 lxdll = Win32LxDll::findModuleByOS2Handle(hInstanceNewDll);
    1348                 if(lxdll == NULL) {//shouldn't happen!
    1349                         dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
    1350                         errorState = ERROR_INTERNAL;
    1351                         return NULL;
    1352                 }
    1353                 lxdll->setDllHandleOS2(hInstanceNewDll);
    1354                 if(lxdll->AddRef() == -1) {//-1 -> load failed (attachProcess)
    1355                         dprintf((LOG, "Dll %s refused to be loaded; aborting", modname));
    1356                         delete lxdll;
    1357                         errorState = ERROR_INTERNAL;
    1358                         return NULL;
    1359                 }
     1390        char *dot = strchr(modname, '.');
     1391        if(dot) {
     1392            *dot = 0;
     1393        }
     1394        strcat(modname, ".DLL");
     1395        rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
     1396        if(rc) {
     1397            dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
     1398            sprintf(szErrorModule, "%s.DLL", szModuleFailure);
     1399            errorState = rc;
     1400            return NULL;
     1401        }
     1402        lxdll = Win32LxDll::findModuleByOS2Handle(hInstanceNewDll);
     1403        if(lxdll == NULL) {//shouldn't happen!
     1404            dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
     1405            errorState = ERROR_INTERNAL;
     1406            return NULL;
     1407        }
     1408        lxdll->setDllHandleOS2(hInstanceNewDll);
     1409        if(lxdll->AddRef() == -1) {//-1 -> load failed (attachProcess)
     1410            dprintf((LOG, "Dll %s refused to be loaded; aborting", modname));
     1411            delete lxdll;
     1412            errorState = ERROR_INTERNAL;
     1413            return NULL;
     1414        }
    13601415                WinDll = (Win32DllBase*)lxdll;
    1361         }
    1362         else {
     1416    }
     1417    else {
    13631418         Win32PeLdrDll *pedll;
    13641419
    1365                 pedll = new Win32PeLdrDll(modname, this);
    1366                 if(pedll == NULL) {
    1367                     dprintf((LOG, "pedll: Error allocating memory" ));
    1368                     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    1369                     errorState = ERROR_INTERNAL;
    1370                     return NULL;
    1371                 }
    1372                 dprintf((LOG, "**********************************************************************" ));
    1373                 dprintf((LOG, "**********************     Loading Module        *********************" ));
    1374                 dprintf((LOG, "**********************************************************************" ));
    1375                 if(pedll->init(0) == FALSE) {
    1376                     dprintf((LOG, "Internal WinDll error ", pedll->getError() ));
    1377                     delete pedll;
    1378                     return NULL;
    1379                 }
     1420            pedll = new Win32PeLdrDll(modname, this);
     1421            if(pedll == NULL) {
     1422                dprintf((LOG, "pedll: Error allocating memory" ));
     1423                WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     1424                errorState = ERROR_INTERNAL;
     1425            return NULL;
     1426            }
     1427            dprintf((LOG, "**********************************************************************" ));
     1428            dprintf((LOG, "**********************     Loading Module        *********************" ));
     1429            dprintf((LOG, "**********************************************************************" ));
     1430            if(pedll->init(0) == FALSE) {
     1431                dprintf((LOG, "Internal WinDll error ", pedll->getError() ));
     1432            delete pedll;
     1433            return NULL;
     1434            }
    13801435#ifdef DEBUG
    1381                 pedll->AddRef(getModuleName());
     1436            pedll->AddRef(getModuleName());
    13821437#else
    1383                 pedll->AddRef();
     1438            pedll->AddRef();
    13841439#endif
    1385                 if(pedll->attachProcess() == FALSE) {
    1386                     dprintf((LOG, "attachProcess failed!" ));
    1387                     delete pedll;
    1388                     errorState = ERROR_INTERNAL;
    1389                     return NULL;
    1390                 }
    1391                 WinDll = (Win32DllBase*)pedll;
    1392         }
    1393 
    1394         dprintf((LOG, "**********************************************************************" ));
    1395         dprintf((LOG, "**********************  Finished Loading Module %s ", modname ));
     1440            if(pedll->attachProcess() == FALSE) {
     1441                dprintf((LOG, "attachProcess failed!" ));
     1442            delete pedll;
     1443                errorState = ERROR_INTERNAL;
     1444            return NULL;
     1445            }
     1446        WinDll = (Win32DllBase*)pedll;
     1447    }
     1448
     1449    dprintf((LOG, "**********************************************************************" ));
     1450    dprintf((LOG, "**********************  Finished Loading Module %s ", modname ));
    13961451        dprintf((LOG, "**********************************************************************" ));
    13971452
    1398         return WinDll;
     1453    return WinDll;
    13991454}
    14001455//******************************************************************************
     
    15451600    if(WinDll == NULL)
    15461601    {  //not found, so load it
    1547         WinDll = loadDll(pszCurModule);
    1548         if(WinDll == NULL) {
    1549                 return FALSE;
    1550         }
     1602    WinDll = loadDll(pszCurModule);
     1603    if(WinDll == NULL) {
     1604        return FALSE;
     1605    }
    15511606    }
    15521607    else {
    1553         WinDll->AddRef();
    1554        
    1555         dprintf((LOG, "Already found ", pszCurModule));
     1608    WinDll->AddRef();
     1609
     1610    dprintf((LOG, "Already found ", pszCurModule));
    15561611    }
    15571612    //add the dll we just loaded to dependency list for this image
     
    15691624    section    = findSectionByOS2Addr(ulCurFixup);
    15701625    if(section == NULL) {
    1571         dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
    1572         return FALSE;
     1626    dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
     1627    return FALSE;
    15731628    }
    15741629    //SvL: Read page from disk
     
    15991654        ulCurFixup += sizeof(IMAGE_THUNK_DATA);
    16001655        j++;
    1601         if((ulCurFixup & 0xfff) == 0) {
    1602                 commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
    1603                 DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
    1604                 nrPages++;
    1605         }
     1656    if((ulCurFixup & 0xfff) == 0) {
     1657            commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
     1658            DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
     1659            nrPages++;
     1660    }
    16061661    }
    16071662    //SvL: And restore original protection flags
     
    16241679{
    16251680  if((address >= realBaseAddress) && (address < realBaseAddress + imageSize)) {
    1626         return TRUE;
     1681    return TRUE;
    16271682  }
    16281683  return FALSE;
     
    16361691  sect = findSectionByOS2Addr(address);
    16371692  if(sect && (sect->pageflags & PAG_EXECUTE)) {
    1638         return TRUE;
     1693    return TRUE;
    16391694  }
    16401695  return FALSE;
     
    16591714  if(apilen < 4)
    16601715  {
    1661         *(ULONG *)tmp = 0;
    1662         strcpy(tmp, name);
    1663         apiname = tmp;
    1664         apilen  = 4;
    1665   }
    1666   else  apiname = name;
     1716    *(ULONG *)tmp = 0;
     1717    strcpy(tmp, name);
     1718    apiname = tmp;
     1719    apilen  = 4;
     1720  }
     1721  else  apiname = name;
    16671722
    16681723  curexport = nameexports;
     
    16721727       *(ULONG *)curexport->name == *(ULONG *)apiname)
    16731728    {
    1674         if(strcmp(curexport->name, apiname) == 0)
    1675                 return(curexport->virtaddr);
     1729        if(strcmp(curexport->name, apiname) == 0)
     1730            return(curexport->virtaddr);
    16761731    }
    16771732    curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
     
    16891744  curexport = ordexports;
    16901745  for(i=0;i<nrOrdExports;i++) {
    1691         if(curexport->ordinal == ordinal)
    1692                 return(curexport->virtaddr);
    1693         curexport++;
     1746    if(curexport->ordinal == ordinal)
     1747        return(curexport->virtaddr);
     1748    curexport++;
    16941749  }
    16951750  //Name exports also contain an ordinal, so check this
    16961751  nexport = nameexports;
    16971752  for(i=0;i<nrNameExports;i++) {
    1698         if(nexport->ordinal == ordinal)
    1699                 return(nexport->virtaddr);
    1700 
    1701         nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
     1753    if(nexport->ordinal == ordinal)
     1754        return(nexport->virtaddr);
     1755
     1756    nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
    17021757  }
    17031758  return(0);
     
    17281783
    17291784  if( r != MBID_IGNORE )
    1730         ExitProcess(987);
     1785    ExitProcess(987);
    17311786
    17321787  fIgnore = TRUE;
Note: See TracChangeset for help on using the changeset viewer.