Ignore:
Timestamp:
Dec 20, 2001, 4:08:56 PM (24 years ago)
Author:
sandervl
Message:

Allow all sections to be accessed when loading an executable/dll with LOAD_LIBRARY_AS_DATAFILE

File:
1 edited

Legend:

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

    r7449 r7660  
    1 /* $Id: winimagepeldr.cpp,v 1.92 2001-11-24 14:58:04 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.93 2001-12-20 15:08:56 sandervl Exp $ */
    22
    33/*
     
    292292    memset(section, 0, nSections*sizeof(Section));
    293293
    294     if(!(dwFlags & FLAG_PELDR_LOADASDATAFILE))
     294    imageSize = 0;
     295    if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
    295296    {
    296         imageSize = 0;
    297         if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL)
     297        dprintf((LOG, "*************************PE SECTIONS START**************************" ));
     298        for (i=0; i<nSections; i++)
    298299        {
    299             dprintf((LOG, "*************************PE SECTIONS START**************************" ));
    300             for (i=0; i<nSections; i++)
     300            dprintf((LOG, "Section:              %-8s", psh[i].Name ));
     301            dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
     302            dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
     303            dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
     304            dprintf((LOG, "Virtual Address End:  %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
     305            dprintf((LOG, "Virtual Size:         %x", psh[i].Misc.VirtualSize ));
     306            dprintf((LOG, "Pointer to raw data:  %x", psh[i].PointerToRawData ));
     307            dprintf((LOG, "Section flags:        %x\n\n", psh[i].Characteristics ));
     308
     309            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
    301310            {
    302                 dprintf((LOG, "Section:              %-8s", psh[i].Name ));
    303                 dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
    304                 dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
    305                 dprintf((LOG, "Virtual Address Start:%x", psh[i].VirtualAddress+oh.ImageBase ));
    306                 dprintf((LOG, "Virtual Address End:  %x", psh[i].VirtualAddress+oh.ImageBase+psh[i].Misc.VirtualSize ));
    307                 dprintf((LOG, "Virtual Size:         %x", psh[i].Misc.VirtualSize ));
    308                 dprintf((LOG, "Pointer to raw data:  %x", psh[i].PointerToRawData ));
    309                 dprintf((LOG, "Section flags:        %x\n\n", psh[i].Characteristics ));
    310 
    311                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_BASERELOC))
    312                 {
    313                     dprintf((LOG, ".reloc" ));
    314                     addSection(SECTION_RELOC, psh[i].PointerToRawData,
     311                dprintf((LOG, ".reloc" ));
     312                addSection(SECTION_RELOC, psh[i].PointerToRawData,
     313                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     314                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     315                continue;
     316            }
     317            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
     318            {
     319                //SvL: Angus.exe has empty export section that's really an
     320                //     uninitialized data section
     321                if(psh[i].SizeOfRawData) {
     322                     dprintf((LOG, ".edata" ));
     323                     addSection(SECTION_EXPORT, psh[i].PointerToRawData,
     324                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     325                                 psh[i].Misc.VirtualSize, psh[i].Characteristics);
     326                     continue;
     327                }
     328            }
     329            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
     330            {
     331                dprintf((LOG, ".rsrc" ));
     332                addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
     333                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     334                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     335                continue;
     336            }
     337            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
     338            {
     339                dprintf((LOG, "TLS section"));
     340                tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
     341                if(tlsDir) {
     342                    addSection(SECTION_TLS, psh[i].PointerToRawData,
    315343                               psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    316344                               psh[i].Misc.VirtualSize, psh[i].Characteristics);
    317                     continue;
    318345                }
    319                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_EXPORT))
    320                 {
    321                     //SvL: Angus.exe has empty export section that's really an
    322                     //     uninitialized data section
    323                     if(psh[i].SizeOfRawData) {
    324                          dprintf((LOG, ".edata" ));
    325                          addSection(SECTION_EXPORT, psh[i].PointerToRawData,
    326                                     psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    327                                     psh[i].Misc.VirtualSize, psh[i].Characteristics);
    328                         continue;
    329                     }
     346                continue;
     347            }
     348            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
     349            {
     350                dprintf((LOG, ".rdebug" ));
     351                addSection(SECTION_DEBUG,  psh[i].PointerToRawData,
     352                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     353                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     354                continue;
     355            }
     356            if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
     357            {
     358                int type = SECTION_IMPORT;
     359                dprintf((LOG, "Import Data Section" ));
     360                if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
     361                   dprintf((LOG, "Also Code Section"));
     362                   type |= SECTION_CODE;
    330363                }
    331                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_RESOURCE))
    332                 {
    333                     dprintf((LOG, ".rsrc" ));
    334                     addSection(SECTION_RESOURCE, psh[i].PointerToRawData,
    335                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    336                                psh[i].Misc.VirtualSize, psh[i].Characteristics);
    337                     continue;
    338                 }
    339                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_TLS))
    340                 {
    341                     dprintf((LOG, "TLS section"));
    342                     tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
    343                     if(tlsDir) {
    344                         addSection(SECTION_TLS, psh[i].PointerToRawData,
    345                                    psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    346                                    psh[i].Misc.VirtualSize, psh[i].Characteristics);
    347                     }
    348                     continue;
    349                 }
    350                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_DEBUG))
    351                 {
    352                     dprintf((LOG, ".rdebug" ));
    353                     addSection(SECTION_DEBUG,  psh[i].PointerToRawData,
    354                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    355                                psh[i].Misc.VirtualSize, psh[i].Characteristics);
    356                     continue;
    357                 }
    358                 if(IsSectionType(win32file, &psh[i], IMAGE_DIRECTORY_ENTRY_IMPORT))
    359                 {
    360                     int type = SECTION_IMPORT;
    361 
    362                     dprintf((LOG, "Import Data Section" ));
    363                     if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE) {
    364                         dprintf((LOG, "Also Code Section"));
    365                         type |= SECTION_CODE;
    366                     }
    367                     addSection(type, psh[i].PointerToRawData,
    368                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    369                                psh[i].Misc.VirtualSize, psh[i].Characteristics);
    370                     continue;
    371                 }
    372 
    373                 //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
    374                 if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
    375                    (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
    376                    !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
    377                   )
    378                 {
    379                     dprintf((LOG, "Code Section"));
    380                     addSection(SECTION_CODE, 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_MEM_WRITE)) { //read only data section
    386                     dprintf((LOG, "Read Only Data Section" ));
    387                     addSection(SECTION_READONLYDATA, 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_CNT_UNINITIALIZED_DATA) {
    393                     dprintf((LOG, "Uninitialized Data Section" ));
    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                 if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
    400                     dprintf((LOG, "Initialized Data Section" ));
    401                     addSection(SECTION_INITDATA, psh[i].PointerToRawData,
    402                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    403                                psh[i].Misc.VirtualSize, psh[i].Characteristics);
    404                     continue;
    405                 }
    406                 if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
    407                     dprintf((LOG, "Other Section, stored as read/write uninit data" ));
    408                     addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
    409                                psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
    410                                psh[i].Misc.VirtualSize, psh[i].Characteristics);
    411                     continue;
    412                 }
    413                 dprintf((LOG, "Unknown section" ));
    414                 goto failure;
    415             }
    416         }
    417     }
    418     else {
    419         if(GetSectionHdrByImageDir(win32file, IMAGE_DIRECTORY_ENTRY_RESOURCE, &sh))
    420         {
    421             dprintf((LOG, "*************************PE SECTIONS START**************************" ));
    422             dprintf((LOG, "Raw data size:        %x", sh.SizeOfRawData ));
    423             dprintf((LOG, "Virtual Address:      %x", sh.VirtualAddress ));
    424             dprintf((LOG, "Virtual Address Start:%x", sh.VirtualAddress+oh.ImageBase ));
    425             dprintf((LOG, "Virtual Address End:  %x", sh.VirtualAddress+oh.ImageBase+sh.Misc.VirtualSize ));
    426             dprintf((LOG, "Virtual Size:         %x", sh.Misc.VirtualSize ));
    427             dprintf((LOG, "Pointer to raw data:  %x", sh.PointerToRawData ));
    428             dprintf((LOG, "Section flags:        %x\n\n", sh.Characteristics ));
    429             addSection(SECTION_RESOURCE, sh.PointerToRawData,
    430                        sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
    431                        sh.Misc.VirtualSize, sh.Characteristics);
     364                addSection(type, psh[i].PointerToRawData,
     365                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     366                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     367                continue;
     368            }
     369            //KSO Sun 1998-08-09: Borland does not alway set the CODE flag for its "CODE" section
     370            if(psh[i].Characteristics & IMAGE_SCN_CNT_CODE ||
     371               (psh[i].Characteristics & IMAGE_SCN_MEM_EXECUTE &&
     372               !(psh[i].Characteristics & (IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_CNT_INITIALIZED_DATA))) //KSO: make sure its not marked as a datasection
     373              )
     374            {
     375                dprintf((LOG, "Code Section"));
     376                addSection(SECTION_CODE, psh[i].PointerToRawData,
     377                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     378                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     379                continue;
     380            }
     381            if(!(psh[i].Characteristics & IMAGE_SCN_MEM_WRITE)) { //read only data section
     382                dprintf((LOG, "Read Only Data Section" ));
     383                addSection(SECTION_READONLYDATA, psh[i].PointerToRawData,
     384                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     385                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     386                continue;
     387            }
     388            if(psh[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) {
     389                dprintf((LOG, "Uninitialized Data Section" ));
     390                addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
     391                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     392                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     393                continue;
     394            }
     395            if(psh[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) {
     396                dprintf((LOG, "Initialized Data Section" ));
     397                addSection(SECTION_INITDATA, psh[i].PointerToRawData,
     398                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     399                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     400                continue;
     401            }
     402            if(psh[i].Characteristics & (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ)) {
     403                dprintf((LOG, "Other Section, stored as read/write uninit data" ));
     404                addSection(SECTION_UNINITDATA, psh[i].PointerToRawData,
     405                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
     406                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
     407                continue;
     408            }
     409            dprintf((LOG, "Unknown section" ));
     410            goto failure;
    432411        }
    433412    }
     
    618597        }
    619598    }
    620 #ifdef COMMIT_ALL
    621     else {
    622         commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
    623     }
    624 #endif
    625599
    626600#ifndef COMMIT_ALL
     
    649623    //Must do this before dlls are loaded for this module. Some apps assume
    650624    //they get TLS index 0 for their main executable
    651     tlsAlloc();
    652     tlsAttachThread();  //setup TLS (main thread)
     625    {
     626      USHORT sel = SetWin32TIB();
     627      tlsAlloc();
     628      tlsAttachThread();  //setup TLS (main thread)
     629      SetFS(sel);
     630    }
    653631
    654632    if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
     
    751729 APIRET   rc;
    752730
     731    dprintf((LOG, "Win32PeLdrImage::commitPage %x %d %d", virtAddress, fWriteAccess, fPageCmd));
    753732    if(virtAddress == 0) {
    754733        return FALSE;
     
    829808    sectionsize = min(sectionsize, range);
    830809
    831     if(fileoffset != -1) {
     810    if(size && fileoffset != -1) {
    832811        rc = DosEnterCritSec();
    833812        if(rc) {
Note: See TracChangeset for help on using the changeset viewer.