Changeset 695 for trunk/src


Ignore:
Timestamp:
Aug 25, 1999, 5:27:20 PM (26 years ago)
Author:
sandervl
Message:

Readonly memory mapped files work now + PE loader uses those apis

Location:
trunk/src/kernel32
Files:
2 edited

Legend:

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

    r690 r695  
    1 /* $Id: mmap.cpp,v 1.11 1999-08-25 14:27:07 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.12 1999-08-25 15:27:19 sandervl Exp $ */
    22
    33/*
     
    150150
    151151  dprintf(("Win32MemMap::commitPage %x (faultaddr %x), nr of pages %d", pageAddr, lpPageFaultAddr, nrpages));
    152   if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, newProt) == FALSE) {
    153         goto fail;
    154   }
    155152  if(hMemFile != -1) {
     153        if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
     154                goto fail;
     155        }
    156156        offset = pageAddr - (ULONG)pMapping;
    157157        size   = nrpages*PAGE_SIZE;
     
    171171                goto fail;
    172172        }
     173        if(mProtFlags & PAGE_READONLY) {
     174//DosSetMem returns flags with EXECUTE bit set, even though the initial allocation is without this bit set!
     175//Also returns access denied when trying to set it back to READONLY
     176                VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt);
     177//              if(VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt) == FALSE) {
     178//                      goto fail;
     179//              }
     180        }
     181  }
     182  else {
     183        if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, newProt) == FALSE) {
     184                goto fail;
     185        }
    173186  }
    174187
     
    224237
    225238  if(fMapped == FALSE) {//if not mapped, reserve/commit entire view
    226         pMapping = VirtualAlloc(0, mSize, fAlloc, mProtFlags);
     239        pMapping = VirtualAlloc(0, mSize, fAlloc, memFlags);
    227240        if(pMapping == NULL) {
    228241                dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
     
    255268
    256269//  mapMutex.enter();
     270  dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
    257271  if(fMapped == FALSE)
    258272        goto parmfail;
  • trunk/src/kernel32/winimage.cpp

    r651 r695  
    1 /* $Id: winimage.cpp,v 1.15 1999-08-23 17:02:35 sandervl Exp $ */
     1/* $Id: winimage.cpp,v 1.16 1999-08-25 15:27:20 sandervl Exp $ */
    22
    33/*
     
    3636#include "os2util.h"
    3737#include "initterm.h"
     38#include <win\virtual.h>
    3839
    3940char szErrorTitle[]     = "Win32 for OS/2";
     
    192193BOOL Win32Image::init(ULONG reservedMem)
    193194{
    194  HFILE  win32handle;
    195  ULONG  ulAction       = 0;      /* Action taken by DosOpen */
    196  ULONG  ulLocal        = 0;      /* File pointer position after DosSetFilePtr */
    197  APIRET rc             = NO_ERROR;            /* Return code */
     195 HANDLE fImgMapping = 0;
    198196 char   szErrorMsg[64];
    199197 LPVOID win32file     = NULL;
     
    203201 int    nSections, i;
    204202
    205   rc = DosOpen(szFileName,                     /* File path name */
    206            &win32handle,                   /* File handle */
    207                &ulAction,                      /* Action taken */
    208                0L,                             /* File primary allocation */
    209                0L,                     /* File attribute */
    210                OPEN_ACTION_FAIL_IF_NEW |
    211                OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type */
    212                OPEN_FLAGS_NOINHERIT |
    213                OPEN_SHARE_DENYNONE  |
    214                OPEN_ACCESS_READONLY,           /* Open mode of the file */
    215                0L);                            /* No extended attribute */
    216 
    217   if (rc != NO_ERROR) {
     203  fImgMapping = VIRTUAL_MapFileA(szFileName, &win32file);
     204
     205  if (fImgMapping == -1) {
    218206        sprintf(szErrorMsg, "Unable to open %32s\n", szFileName);
    219207        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    220         errorState = ERROR_INTERNAL;
    221         return(FALSE);
    222   }
    223 
    224   /* Move the file pointer back to the beginning of the file */
    225   DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
    226   DosSetFilePtr(win32handle, 0L, FILE_END, &filesize);
    227   DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
    228 
    229   win32file = malloc(filesize);
    230   if(win32file == NULL) {
    231         fout << "Error allocating memory" << endl;
    232         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    233         DosClose(win32handle);                /* Close the file */
    234         errorState = ERROR_INTERNAL;
    235         return(FALSE);
    236   }
    237   rc = DosRead(win32handle, win32file, filesize, &ulRead);
    238   if(rc != NO_ERROR) {
    239         fout << "DosRead returned " << rc << endl;
    240         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szFileErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    241         DosClose(win32handle);                /* Close the file */
    242         errorState = ERROR_INTERNAL;
    243         return(FALSE);
     208        goto failure;
    244209  }
    245210
     
    247212        fout << "Not a valid PE file (probably a 16 bits windows exe/dll)!" << endl;
    248213        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    249         DosClose(win32handle);                /* Close the file */
    250         errorState = ERROR_INTERNAL;
    251         return(FALSE);
     214        goto failure;
    252215  }
    253216
     
    255218        fout << "Not a valid PE file!" << endl;
    256219        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    257         DosClose(win32handle);                /* Close the file */
    258         errorState = ERROR_INTERNAL;
    259         return(FALSE);
     220        goto failure;
    260221  }
    261222  if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
    262223        fout << "You need a REAL CPU to run this code" << endl;
    263224        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    264         DosClose(win32handle);                /* Close the file */
    265         errorState = ERROR_INTERNAL;
    266         return(FALSE);
     225        goto failure;
    267226  }
    268227  //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
     
    270229        fout << "Can't convert system files" << endl;
    271230        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    272         DosClose(win32handle);                /* Close the file */
    273         errorState = ERROR_INTERNAL;
    274         return(FALSE);
     231        goto failure;
    275232  }
    276233
     
    405362        }
    406363        fout << "Unknown section" << endl;
    407         errorState = ERROR_INTERNAL;
    408         return(FALSE);
    409         }
     364        goto failure;
     365     }
    410366  }
    411367  fout << "*************************PE SECTIONS END **************************" << endl;
     
    422378  if(allocSections(reservedMem) == FALSE) {
    423379        fout << "Failed to allocate image memory, rc " << errorState << endl;
    424         return(FALSE);
     380        goto failure;
    425381  }
    426382  fout << "OS/2 base address " << realBaseAddress << endl;
    427383  if(storeSections((char *)win32file) == FALSE) {
    428384        fout << "Failed to store sections, rc " << errorState << endl;
    429         return(FALSE);
     385        goto failure;
    430386  }
    431387  if(oh.AddressOfEntryPoint) {
     
    442398        if(sect == NULL) {
    443399                fout << "Couldn't find TLS section!!" << endl;
    444                 return(FALSE);
     400                goto failure;
    445401        }
    446402        setTLSAddress((char *)(sect->realvirtaddr + (tlsDir->StartAddressOfRawData - sect->virtaddr)));
     
    451407        if(sect == NULL) {
    452408                fout << "Couldn't find TLS AddressOfIndex section!!" << endl;
    453                 return(FALSE);
     409                goto failure;
    454410        }
    455411        setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
     
    458414        if(sect == NULL) {
    459415                fout << "Couldn't find TLS AddressOfCallBacks section!!" << endl;
    460                 return(FALSE);
     416                goto failure;
    461417        }
    462418        setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
     
    466422        if(setFixups((PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC)) == FALSE) {
    467423                fout << "Failed to set fixups" << endl;
    468                 return(FALSE);
     424                goto failure;
    469425        }
    470426  }
     
    472428  if(processImports((char *)win32file) == FALSE) {
    473429        fout << "Failed to process imports!" << endl;
    474         return(FALSE);
     430        goto failure;
    475431  }
    476432
     
    478434        if(processExports((char *)win32file) == FALSE) {
    479435                fout << "Failed to process exported apis" << endl;
    480                 return(FALSE);
     436                goto failure;
    481437        }
    482438  }
     
    494450  if(setMemFlags() == FALSE) {
    495451        fout << "Failed to set memory protection" << endl;
    496         return(FALSE);
    497   }
    498 
    499   //Now it's safe to free win32file
    500   free(win32file);
    501   DosClose(win32handle);                /* Close the file */
     452        goto failure;
     453  }
     454
     455  CloseHandle(fImgMapping);
    502456  return(TRUE);
     457failure:
     458  if(fImgMapping) CloseHandle(fImgMapping);
     459  errorState = ERROR_INTERNAL;
     460  return FALSE;
    503461}
    504462//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.