Ignore:
Timestamp:
Feb 25, 2001, 5:05:40 PM (24 years ago)
Author:
sandervl
Message:

Check executable too when searching for imported modules

File:
1 edited

Legend:

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

    r5128 r5266  
    1 /* $Id: winimagepeldr.cpp,v 1.67 2001-02-14 10:36:45 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.68 2001-02-25 16:05:39 sandervl Exp $ */
    22
    33/*
     
    4545#include "windllpeldr.h"
    4646#include "windlllx.h"
     47#include "winexebase.h"
    4748#include <pefile.h>
    4849#include <unicode.h>
     
    632633    //they get TLS index 0 for their main executable
    633634    tlsAlloc();
    634     tlsAttachThread();  //setup TLS (main thread)
     635    tlsAttachThread();  //setup TLS (main thread)
    635636
    636637    if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS)))
     
    11951196 ULONG *fixup;
    11961197
    1197   fixup   = (ULONG *)(fixupaddr + realBaseAddress);
    1198   orgaddr = *fixup;
     1198    fixup   = (ULONG *)(fixupaddr + realBaseAddress);
     1199    orgaddr = *fixup;
    11991200//  dprintf((LOG, "AddOff32Fixup 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, realBaseAddress + (*fixup - oh.ImageBase)));
    1200   *fixup  = realBaseAddress + (*fixup - oh.ImageBase);
     1201    *fixup  = realBaseAddress + (*fixup - oh.ImageBase);
    12011202}
    12021203//******************************************************************************
     
    12071208 USHORT *fixup;
    12081209
    1209   fixup   = (USHORT *)(fixupaddr + realBaseAddress);
    1210   orgaddr = *fixup;
    1211   if(fHighFixup) {
    1212     *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
     1210    fixup   = (USHORT *)(fixupaddr + realBaseAddress);
     1211    orgaddr = *fixup;
     1212    if(fHighFixup) {
     1213        *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
    12131214//        dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    1214   }
    1215   else {
    1216     *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
     1215    }
     1216    else {
     1217        *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
    12171218//        dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
    1218   }
    1219 }
    1220 //******************************************************************************
    1221 //******************************************************************************
    1222 void Win32PeLdrImage::StoreImportByOrd(Win32DllBase *WinDll, ULONG ordinal, ULONG impaddr)
     1219    }
     1220}
     1221//******************************************************************************
     1222//******************************************************************************
     1223void Win32PeLdrImage::StoreImportByOrd(Win32ImageBase *WinImage, ULONG ordinal, ULONG impaddr)
    12231224{
    12241225 ULONG *import;
    12251226 ULONG  apiaddr;
    12261227
    1227   import  = (ULONG *)impaddr;
    1228   apiaddr = WinDll->getApi(ordinal);
    1229   if(apiaddr == 0)
    1230   {
     1228    import  = (ULONG *)impaddr;
     1229    apiaddr = WinImage->getApi(ordinal);
     1230    if(apiaddr == 0)
     1231    {
    12311232        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
    1232                  WinDll->getName(),
     1233                 WinImage->getModuleName(),
    12331234                 ordinal));
    12341235
    12351236        dprintf((LOG, "--->>> NOT FOUND!" ));
    12361237        *import = (ULONG)MissingApi;
    1237   }
    1238   else *import = apiaddr;
    1239 }
    1240 //******************************************************************************
    1241 //******************************************************************************
    1242 void Win32PeLdrImage::StoreImportByName(Win32DllBase *WinDll, char *impname, ULONG impaddr)
     1238    }
     1239    else *import = apiaddr;
     1240}
     1241//******************************************************************************
     1242//******************************************************************************
     1243void Win32PeLdrImage::StoreImportByName(Win32ImageBase *WinImage, char *impname, ULONG impaddr)
    12431244{
    12441245 ULONG *import;
    12451246 ULONG  apiaddr;
    12461247
    1247   import = (ULONG *)impaddr;
    1248   apiaddr = WinDll->getApi(impname);
    1249   if(apiaddr == 0)
    1250   {
     1248    import = (ULONG *)impaddr;
     1249    apiaddr = WinImage->getApi(impname);
     1250    if(apiaddr == 0)
     1251    {
    12511252        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
    1252                 WinDll->getName(),
     1253                WinImage->getModuleName(),
    12531254                impname));
    12541255
    12551256        dprintf((LOG, "--->>> NOT FOUND!" ));
    12561257        *import = (ULONG)MissingApi;
    1257   }
    1258   else  *import = apiaddr;
     1258    }
     1259    else  *import = apiaddr;
    12591260}
    12601261//******************************************************************************
     
    13331334 ULONG nsize;
    13341335
    1335   if(nameexports == NULL) {
     1336    if(nameexports == NULL) {
    13361337        nameExportSize= 4096;
    13371338        nameexports   = (NameExport *)malloc(nameExportSize);
    13381339        curnameexport = nameexports;
    1339   }
    1340   nsize = (ULONG)curnameexport - (ULONG)nameexports;
    1341   if(nsize + sizeof(NameExport) + strlen(apiname) > nameExportSize) {
     1340    }
     1341    nsize = (ULONG)curnameexport - (ULONG)nameexports;
     1342    if(nsize + sizeof(NameExport) + strlen(apiname) > nameExportSize) {
    13421343        nameExportSize += 4096;
    13431344        char *tmp = (char *)nameexports;
     
    13461347        curnameexport = (NameExport *)((ULONG)nameexports + nsize);
    13471348        free(tmp);
    1348   }
    1349   if(fAbsoluteAddress) {//forwarders use absolute address
    1350     curnameexport->virtaddr = virtaddr;
    1351   }
    1352   else curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
    1353   curnameexport->ordinal  = ordinal;
    1354   *(ULONG *)curnameexport->name = 0;
    1355   strcpy(curnameexport->name, apiname);
    1356 
    1357   curnameexport->nlength = strlen(apiname) + 1;
    1358   if(curnameexport->nlength < sizeof(curnameexport->name))
     1349    }
     1350    if(fAbsoluteAddress) {//forwarders use absolute address
     1351        curnameexport->virtaddr = virtaddr;
     1352    }
     1353    else curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     1354    curnameexport->ordinal  = ordinal;
     1355    *(ULONG *)curnameexport->name = 0;
     1356    strcpy(curnameexport->name, apiname);
     1357
     1358    curnameexport->nlength = strlen(apiname) + 1;
     1359    if(curnameexport->nlength < sizeof(curnameexport->name))
    13591360        curnameexport->nlength = sizeof(curnameexport->name);
    13601361
    1361   curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
     1362    curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
    13621363}
    13631364//******************************************************************************
     
    13651366void Win32PeLdrImage::AddOrdExport(ULONG virtaddr, ULONG ordinal, BOOL fAbsoluteAddress)
    13661367{
    1367   if(ordexports == NULL) {
     1368    if(ordexports == NULL) {
    13681369        ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
    13691370        curordexport = ordexports;
    1370   }
    1371   if(fAbsoluteAddress) {//forwarders use absolute address
    1372     curordexport->virtaddr = virtaddr;
    1373   }
    1374   else  curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
    1375   curordexport->ordinal  = ordinal;
    1376   curordexport++;
     1371    }
     1372    if(fAbsoluteAddress) {//forwarders use absolute address
     1373        curordexport->virtaddr = virtaddr;
     1374    }
     1375    else curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
     1376
     1377    curordexport->ordinal  = ordinal;
     1378    curordexport++;
    13771379}
    13781380//******************************************************************************
     
    15471549 ULONG  ulCurFixup;
    15481550 int    Size;
    1549  Win32DllBase *WinDll;
     1551 Win32DllBase    *WinDll;
     1552 Win32ImageBase  *WinImage;
    15501553 Section *section;
    15511554
     
    16171620  if (fBorland)
    16181621    dprintf((LOG, "Borland-styled PE-File." ));
     1622
    16191623  //Store modules
    16201624  dprintf((LOG, "%d imported Modules: ", cModules ));
     
    16721676    {  //not found, so load it
    16731677        WinDll = loadDll(pszCurModule);
    1674         if(WinDll == NULL) {
    1675             return FALSE;
    1676         }
    16771678    }
    16781679    else {
     
    16801681        dprintf((LOG, "Already found ", pszCurModule));
    16811682    }
    1682     //add the dll we just loaded to dependency list for this image
    1683     addDependency(WinDll);
    1684 
    1685     //Make sure the dependency list is correct (already done
    1686     //in the ctor of Win32DllBase, but for LX dlls the parent is
    1687     //then set to NULL; so change it here again
    1688     WinDll->setUnloadOrder(this);
     1683    if(WinDll == NULL) {
     1684        //TODO: might not be the right order (check executable first and only then dll??)
     1685        if (WinExe != NULL && WinExe->matchModName(pszCurModule)) {
     1686             WinImage = (Win32ImageBase *)WinExe;
     1687        }
     1688        else return FALSE;
     1689    }
     1690    else {
     1691        //add the dll we just loaded to dependency list for this image
     1692        addDependency(WinDll);
     1693
     1694        //Make sure the dependency list is correct (already done
     1695        //in the ctor of Win32DllBase, but for LX dlls the parent is
     1696        //then set to NULL; so change it here again
     1697        WinDll->setUnloadOrder(this);
     1698        WinImage = (Win32ImageBase *)WinDll;
     1699    }
    16891700
    16901701    pulImport  = (PULONG)((ULONG)pulImport + (ULONG)win32file);
     
    16941705    section    = findSectionByOS2Addr(ulCurFixup);
    16951706    if(section == NULL) {
    1696     dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
    1697     return FALSE;
     1707        dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
     1708        return FALSE;
    16981709    }
    16991710    //SvL: Read page from disk
     
    17061717        if (pulImport[j] & IMAGE_ORDINAL_FLAG) { //ordinal
    17071718            dprintf((LOG, "0x%08x Imported function %s @%d", ulCurFixup , pszCurModule, (pulImport[j] & ~IMAGE_ORDINAL_FLAG) ));
    1708             StoreImportByOrd(WinDll, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);
     1719            StoreImportByOrd(WinImage, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);
    17091720        }
    17101721        else {  //name
     
    17191730            //KSO - Aug 6 1998 1:15am:this eases comparing...
    17201731            char *pszFunctionName = (char*)(pulImport[j] + (ULONG)win32file + 2);
    1721             dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup,  pszFunctionName, WinDll->getApi(pszFunctionName)));
    1722             StoreImportByName(WinDll, pszFunctionName, ulCurFixup);
     1732            dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup,  pszFunctionName, WinImage->getApi(pszFunctionName)));
     1733            StoreImportByName(WinImage, pszFunctionName, ulCurFixup);
    17231734        }
    17241735        ulCurFixup += sizeof(IMAGE_THUNK_DATA);
    17251736        j++;
    1726     if((ulCurFixup & 0xfff) == 0) {
     1737        if((ulCurFixup & 0xfff) == 0) {
    17271738            commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
    17281739            DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
    17291740            nrPages++;
    1730     }
     1741        }
    17311742    }
    17321743    //SvL: And restore original protection flags
     
    17481759BOOL Win32PeLdrImage::insideModule(ULONG address)
    17491760{
    1750   if((address >= realBaseAddress) && (address < realBaseAddress + imageSize)) {
    1751     return TRUE;
    1752   }
    1753   return FALSE;
     1761    if((address >= realBaseAddress) && (address < realBaseAddress + imageSize)) {
     1762        return TRUE;
     1763    }
     1764    return FALSE;
    17541765}
    17551766//******************************************************************************
     
    17591770 Section *sect;
    17601771
    1761   sect = findSectionByOS2Addr(address);
    1762   if(sect && (sect->pageflags & PAG_EXECUTE)) {
    1763     return TRUE;
    1764   }
    1765   return FALSE;
     1772    sect = findSectionByOS2Addr(address);
     1773    if(sect && (sect->pageflags & PAG_EXECUTE)) {
     1774        return TRUE;
     1775    }
     1776    return FALSE;
    17661777}
    17671778//******************************************************************************
     
    17691780ULONG Win32PeLdrImage::getImageSize()
    17701781{
    1771   return imageSize;
     1782    return imageSize;
    17721783}
    17731784//******************************************************************************
     
    17811792  ULONG       ulAPIOrdinal;                      /* api requested by ordinal */
    17821793
    1783   apilen = strlen(name) + 1;
    1784   if(apilen < 4)
    1785   {
    1786     *(ULONG *)tmp = 0;
    1787     strcpy(tmp, name);
    1788     apiname = tmp;
    1789     apilen  = 4;
    1790   }
    1791   else  apiname = name;
    1792 
    1793   curexport = nameexports;
    1794   for(i=0; i<nrNameExports; i++)
    1795   {
    1796     if(apilen == curexport->nlength &&
    1797        *(ULONG *)curexport->name == *(ULONG *)apiname)
     1794    apilen = strlen(name) + 1;
     1795    if(apilen < 4)
    17981796    {
    1799         if(strcmp(curexport->name, apiname) == 0)
    1800             return(curexport->virtaddr);
    1801     }
    1802     curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
    1803   }
    1804   return(0);
     1797        *(ULONG *)tmp = 0;
     1798        strcpy(tmp, name);
     1799        apiname = tmp;
     1800        apilen  = 4;
     1801    }
     1802    else  apiname = name;
     1803
     1804    curexport = nameexports;
     1805    for(i=0; i<nrNameExports; i++)
     1806    {
     1807        if(apilen == curexport->nlength &&
     1808           *(ULONG *)curexport->name == *(ULONG *)apiname)
     1809        {
     1810            if(strcmp(curexport->name, apiname) == 0)
     1811                return(curexport->virtaddr);
     1812        }
     1813        curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
     1814    }
     1815    return(0);
    18051816}
    18061817//******************************************************************************
     
    18121823 NameExport *nexport;
    18131824
    1814   curexport = ordexports;
    1815   for(i=0;i<nrOrdExports;i++) {
    1816     if(curexport->ordinal == ordinal)
    1817         return(curexport->virtaddr);
    1818     curexport++;
    1819   }
    1820   //Name exports also contain an ordinal, so check this
    1821   nexport = nameexports;
    1822   for(i=0;i<nrNameExports;i++) {
    1823     if(nexport->ordinal == ordinal)
    1824         return(nexport->virtaddr);
    1825 
    1826     nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
    1827   }
    1828   return(0);
     1825    curexport = ordexports;
     1826    for(i=0;i<nrOrdExports;i++) {
     1827        if(curexport->ordinal == ordinal)
     1828            return(curexport->virtaddr);
     1829        curexport++;
     1830    }
     1831    //Name exports also contain an ordinal, so check this
     1832    nexport = nameexports;
     1833    for(i=0;i<nrNameExports;i++) {
     1834        if(nexport->ordinal == ordinal)
     1835            return(nexport->virtaddr);
     1836
     1837        nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
     1838    }
     1839    return(0);
    18291840}
    18301841//******************************************************************************
     
    18331844ULONG Win32PeLdrImage::getVersion()
    18341845{
    1835   return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion;
     1846    return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion;
    18361847}
    18371848//******************************************************************************
     
    18421853 int r;
    18431854
    1844   dprintf((LOG, "Missing api called!\n"));
    1845   if(fIgnore)
     1855    dprintf((LOG, "Missing api called!\n"));
     1856    if(fIgnore)
     1857        return(0);
     1858
     1859    do {
     1860        r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, "The application has called a non-existing api\n",
     1861                          "Internal Odin Error", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
     1862    }
     1863    while(r == MBID_RETRY); //giggle
     1864
     1865    if( r != MBID_IGNORE )
     1866        ExitProcess(987);
     1867
     1868    fIgnore = TRUE;
    18461869    return(0);
    1847 
    1848   do {
    1849     r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, "The application has called a non-existing api\n",
    1850                   "Internal Odin Error", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
    1851   }
    1852   while(r == MBID_RETRY); //giggle
    1853 
    1854   if( r != MBID_IGNORE )
    1855     ExitProcess(987);
    1856 
    1857   fIgnore = TRUE;
    1858   return(0);
    18591870}
    18601871/******************************************************************************/
Note: See TracChangeset for help on using the changeset viewer.