Ignore:
Timestamp:
Apr 9, 2003, 12:39:41 PM (22 years ago)
Author:
sandervl
Message:

Unmap memory view when deleting PE dll object

File:
1 edited

Legend:

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

    r9963 r9997  
    1 /* $Id: winimagepeldr.cpp,v 1.106 2003-03-31 11:54:28 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.107 2003-04-09 10:39:40 sandervl Exp $ */
    22
    33/*
     
    112112    nrNameExports(0), nrOrdExports(0), nameexports(NULL), ordexports(NULL),
    113113    memmap(NULL), pFixups(NULL), dwFixupSize(0), curnameexport(NULL), curordexport(NULL),
    114     nrOrdExportsRegistered(0)
     114    nrOrdExportsRegistered(0), peview(NULL)
    115115{
    116116    HFILE  dllfile;
     
    148148{
    149149    if(memmap)
     150    {
     151        if(peview) {
     152            memmap->unmapViewOfFile(peview);
     153            peview = NULL;
     154        }
    150155        memmap->Release();
     156        memmap = NULL;
     157    }
    151158
    152159    if(hFile) {
     
    171178DWORD Win32PeLdrImage::init(ULONG reservedMem, ULONG ulPEOffset)
    172179{
    173  LPVOID win32file = NULL;
    174180 ULONG  filesize, ulRead, ulNewPos;
    175181 PIMAGE_SECTION_HEADER psh;
     
    252258        goto failure;
    253259    }
    254     win32file = memmap->mapViewOfFile(0, 0, 2);
     260    peview = memmap->mapViewOfFile(0, 0, 2);
    255261
    256262    if(DosQueryPathInfo(szFileName, FIL_QUERYFULLNAME, szFullPath, sizeof(szFullPath)) == 0) {
     
    303309    commitPage(realBaseAddress, FALSE);
    304310
    305     nSections = NR_SECTIONS(win32file);
     311    nSections = NR_SECTIONS(peview);
    306312    section = (Section *)malloc(nSections*sizeof(Section));
    307313    if(section == NULL) {
     
    313319
    314320    imageSize = 0;
    315     if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
     321    if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (peview)) != NULL)
    316322    {
    317323        dprintf((LOG, "*************************PE SECTIONS START**************************" ));
     
    327333            dprintf((LOG, "Section flags:        %x\n\n", psh[i].Characteristics ));
    328334
    329             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
     335            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
    330336            {
    331337                dprintf((LOG, ".reloc" ));
     
    335341                continue;
    336342            }
    337             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
     343            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
    338344            {
    339345                //SvL: Angus.exe has empty export section that's really an
     
    347353                }
    348354            }
    349             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
     355            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
    350356            {
    351357                dprintf((LOG, ".rsrc" ));
     
    355361                continue;
    356362            }
    357             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
     363            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
    358364            {
    359365                dprintf((LOG, "TLS section"));
    360                 tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
     366                tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(peview, IMAGE_DIRECTORY_ENTRY_TLS);
    361367                if(tlsDir) {
    362368                    addSection(SECTION_TLS, psh[i].PointerToRawData,
     
    366372                continue;
    367373            }
    368             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
     374            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
    369375            {
    370376                dprintf((LOG, ".rdebug" ));
     
    374380                continue;
    375381            }
    376             if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
     382            if(IsSectionType(peview, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
    377383            {
    378384                int type = SECTION_IMPORT;
     
    465471
    466472    if(realBaseAddress != oh.ImageBase && !(dwFlags & FLAG_PELDR_LOADASDATAFILE)) {
    467         pFixups     = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
    468         dwFixupSize = ImageDirectorySize(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
     473        pFixups     = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(peview, IMAGE_DIRECTORY_ENTRY_BASERELOC);
     474        dwFixupSize = ImageDirectorySize(peview, IMAGE_DIRECTORY_ENTRY_BASERELOC);
    469475        commitPage((ULONG)pFixups, FALSE);
    470476    }
     
    475481    if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
    476482    {
    477         if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS))
     483        if(tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(peview, IMAGE_DIRECTORY_ENTRY_TLS))
    478484        {
    479485            Section *sect;
     
    620626        }
    621627#endif
    622         if(processExports((char *)win32file) == FALSE) {
     628        if(processExports() == FALSE) {
    623629            dprintf((LOG, "Failed to process exported apis" ));
    624630            lasterror = LDRERROR_EXPORTS;
     
    655661    if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
    656662    {
    657         if(processImports((char *)win32file) == FALSE) {
     663        if(processImports() == FALSE) {
    658664            dprintf((LOG, "Failed to process imports!" ));
    659665            lasterror = LDRERROR_IMPORTS;
     
    13811387//******************************************************************************
    13821388//******************************************************************************
    1383 BOOL Win32PeLdrImage::processExports(char *win32file)
     1389BOOL Win32PeLdrImage::processExports()
    13841390{
    13851391 IMAGE_SECTION_HEADER    sh;
     
    13921398  /* get section header and pointer to data directory for .edata section */
    13931399  if((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
    1394      (win32file, IMAGE_DIRECTORY_ENTRY_EXPORT)) != NULL &&
    1395      GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_EXPORT, &sh) ) {
    1396 
    1397         dprintf((LOG, "Exported Functions: " ));
     1400     (peview, IMAGE_DIRECTORY_ENTRY_EXPORT)) != NULL &&
     1401     GetSectionHdrByImageDir(peview, IMAGE_DIRECTORY_ENTRY_EXPORT, &sh) )
     1402  {
     1403
     1404    dprintf((LOG, "Exported Functions: " ));
    13981405    ptrOrd     = (USHORT *)((ULONG)ped->AddressOfNameOrdinals +
    1399                             (ULONG)win32file);
     1406                            (ULONG)peview);
    14001407    ptrNames   = (ULONG *)((ULONG)ped->AddressOfNames +
    1401                             (ULONG)win32file);
     1408                            (ULONG)peview);
    14021409    ptrAddress = (ULONG *)((ULONG)ped->AddressOfFunctions +
    1403                             (ULONG)win32file);
     1410                            (ULONG)peview);
    14041411    nrOrdExports  = ped->NumberOfFunctions;
    14051412    nrNameExports = ped->NumberOfNames;
     
    14091416    for(i=0;i<ped->NumberOfNames;i++)
    14101417    {
    1411     fForwarder = FALSE;
     1418        fForwarder = FALSE;
    14121419        ord        = ptrOrd[i] + ped->Base;
    1413         name       = (char *)((ULONG)ptrNames[i] + (ULONG)win32file);
     1420        name       = (char *)((ULONG)ptrNames[i] + (ULONG)peview);
    14141421        RVAExport  = ptrAddress[ptrOrd[i]];
    14151422
     
    14211428            fForwarder = AddForwarder(oh.ImageBase + RVAExport, name, ord);
    14221429        }
    1423     if(!fForwarder) {
     1430        if(!fForwarder) {
    14241431            //points to code (virtual address relative to oh.ImageBase
    14251432            AddNameExport(oh.ImageBase + RVAExport, name, ord);
     
    14291436    for(i=0;i<max(ped->NumberOfNames,ped->NumberOfFunctions);i++)
    14301437    {
    1431     fForwarder = FALSE;
     1438        fForwarder = FALSE;
    14321439        ord = ped->Base + i;  //Correct??
    14331440        RVAExport = ptrAddress[i];
     
    14391446            fForwarder = AddForwarder(oh.ImageBase + RVAExport, NULL, ord);
    14401447        }
    1441     if(!fForwarder && RVAExport) {
     1448        if(!fForwarder && RVAExport) {
    14421449            //points to code (virtual address relative to oh.ImageBase
    14431450            dprintf((LOG, "ord %d at 0x%08x (0x%08x)", ord, RVAExport, realBaseAddress + RVAExport));
     
    16751682 **/
    16761683//******************************************************************************
    1677 BOOL Win32PeLdrImage::processImports(char *win32file)
     1684BOOL Win32PeLdrImage::processImports()
    16781685{
    16791686    PIMAGE_IMPORT_DESCRIPTOR pID;
     
    17071714
    17081715    /* 1) get module names */
    1709     pID = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT);
     1716    pID = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryOffset(peview, IMAGE_DIRECTORY_ENTRY_IMPORT);
    17101717    if (pID == NULL)
    17111718        return TRUE;
    1712     if (!GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_IMPORT, &shID))
     1719    if (!GetSectionHdrByImageDir(peview, IMAGE_DIRECTORY_ENTRY_IMPORT, &shID))
    17131720        return TRUE;
    17141721
     
    17191726        //test RVA inside ID-Section
    17201727        if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
    1721             pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
     1728            pszTmp = (char*)(pID[i].Name + (ULONG)peview);
    17221729        }
    17231730        else {
    17241731            //is the "Extra"-section already found or do we have to find it?
    17251732            if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData)) {
    1726                 if (!GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name))
     1733                if (!GetSectionHdrByRVA(peview, &shExtra, pID[i].Name))
    17271734                    return FALSE;
    17281735            }
    1729             pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
     1736            pszTmp = (char*)(pID[i].Name + (ULONG)peview);
    17301737        }
    17311738        Size += strlen(pszTmp) + 1;
     
    17441751        //test RVA inside ID-Section
    17451752        if (pID[i].Name >= shID.VirtualAddress && pID[i].Name < shID.VirtualAddress + max(shID.Misc.VirtualSize, shID.SizeOfRawData)) {
    1746             pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
     1753            pszTmp = (char*)(pID[i].Name + (ULONG)peview);
    17471754        }
    17481755        else {
     
    17511758            if (pID[i].Name < shExtra.VirtualAddress || pID[i].Name >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
    17521759            {
    1753                 if (GetSectionHdrByRVA(win32file, &shExtra, pID[i].Name)) {
     1760                if (GetSectionHdrByRVA(peview, &shExtra, pID[i].Name)) {
    17541761                    return FALSE;
    17551762                }
    17561763            }
    1757             pszTmp = (char*)(pID[i].Name + (ULONG)win32file);
     1764            pszTmp = (char*)(pID[i].Name + (ULONG)peview);
    17581765        }
    17591766
     
    17701777    /* 2) functions */
    17711778    pszCurModule = pszModules;
    1772     pOH = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(win32file);
     1779    pOH = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF(peview);
    17731780    for (i = 0; i < cModules; i++)
    17741781    {
     
    18041811        if ((ULONG)pulImport < shExtra.VirtualAddress || (ULONG)pulImport >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
    18051812        {
    1806             if (!GetSectionHdrByRVA(win32file, &shExtra, (ULONG)pulImport))
     1813            if (!GetSectionHdrByRVA(peview, &shExtra, (ULONG)pulImport))
    18071814            {
    18081815                dprintf((LOG, "warning: could not find section for Thunk RVA %x", pulImport ));
     
    18491856        }
    18501857
    1851         pulImport  = (PULONG)((ULONG)pulImport + (ULONG)win32file);
     1858        pulImport  = (PULONG)((ULONG)pulImport + (ULONG)peview);
    18521859        j          = 0;
    1853         ulCurFixup = (ULONG)pID[i].FirstThunk + (ULONG)win32file;
     1860        ulCurFixup = (ULONG)pID[i].FirstThunk + (ULONG)peview;
    18541861
    18551862        section    = findSectionByOS2Addr(ulCurFixup);
     
    18731880                if (pulImport[j] < shExtra.VirtualAddress || pulImport[j] >= shExtra.VirtualAddress + max(shExtra.Misc.VirtualSize, shExtra.SizeOfRawData))
    18741881                {
    1875                     if (!GetSectionHdrByRVA(win32file, &shExtra, pulImport[j]))
     1882                    if (!GetSectionHdrByRVA(peview, &shExtra, pulImport[j]))
    18761883                    {
    18771884                        dprintf((LOG, "warning: could not find section for Import Name RVA ", pulImport[j] ));
     
    18801887                }
    18811888                //KSO - Aug 6 1998 1:15am:this eases comparing...
    1882                 char *pszFunctionName = (char*)(pulImport[j] + (ULONG)win32file + 2);
     1889                char *pszFunctionName = (char*)(pulImport[j] + (ULONG)peview + 2);
    18831890                dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup,  pszFunctionName, WinImage->getApi(pszFunctionName)));
    18841891                StoreImportByName(WinImage, pszFunctionName, ulCurFixup);
Note: See TracChangeset for help on using the changeset viewer.