Changeset 5266 for trunk/src/kernel32/winimagepeldr.cpp
- Timestamp:
- Feb 25, 2001, 5:05:40 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/winimagepeldr.cpp
r5128 r5266 1 /* $Id: winimagepeldr.cpp,v 1.6 7 2001-02-14 10:36:45sandervl Exp $ */1 /* $Id: winimagepeldr.cpp,v 1.68 2001-02-25 16:05:39 sandervl Exp $ */ 2 2 3 3 /* … … 45 45 #include "windllpeldr.h" 46 46 #include "windlllx.h" 47 #include "winexebase.h" 47 48 #include <pefile.h> 48 49 #include <unicode.h> … … 632 633 //they get TLS index 0 for their main executable 633 634 tlsAlloc(); 634 tlsAttachThread(); 635 tlsAttachThread(); //setup TLS (main thread) 635 636 636 637 if(!(dwFlags & (FLAG_PELDR_LOADASDATAFILE | FLAG_PELDR_SKIPIMPORTS))) … … 1195 1196 ULONG *fixup; 1196 1197 1197 fixup = (ULONG *)(fixupaddr + realBaseAddress);1198 orgaddr = *fixup;1198 fixup = (ULONG *)(fixupaddr + realBaseAddress); 1199 orgaddr = *fixup; 1199 1200 // 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); 1201 1202 } 1202 1203 //****************************************************************************** … … 1207 1208 USHORT *fixup; 1208 1209 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); 1213 1214 // 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); 1217 1218 // dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup)); 1218 }1219 } 1220 //****************************************************************************** 1221 //****************************************************************************** 1222 void Win32PeLdrImage::StoreImportByOrd(Win32 DllBase *WinDll, ULONG ordinal, ULONG impaddr)1219 } 1220 } 1221 //****************************************************************************** 1222 //****************************************************************************** 1223 void Win32PeLdrImage::StoreImportByOrd(Win32ImageBase *WinImage, ULONG ordinal, ULONG impaddr) 1223 1224 { 1224 1225 ULONG *import; 1225 1226 ULONG apiaddr; 1226 1227 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 { 1231 1232 dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n", 1232 Win Dll->getName(),1233 WinImage->getModuleName(), 1233 1234 ordinal)); 1234 1235 1235 1236 dprintf((LOG, "--->>> NOT FOUND!" )); 1236 1237 *import = (ULONG)MissingApi; 1237 }1238 else*import = apiaddr;1239 } 1240 //****************************************************************************** 1241 //****************************************************************************** 1242 void Win32PeLdrImage::StoreImportByName(Win32 DllBase *WinDll, char *impname, ULONG impaddr)1238 } 1239 else *import = apiaddr; 1240 } 1241 //****************************************************************************** 1242 //****************************************************************************** 1243 void Win32PeLdrImage::StoreImportByName(Win32ImageBase *WinImage, char *impname, ULONG impaddr) 1243 1244 { 1244 1245 ULONG *import; 1245 1246 ULONG apiaddr; 1246 1247 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 { 1251 1252 dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n", 1252 Win Dll->getName(),1253 WinImage->getModuleName(), 1253 1254 impname)); 1254 1255 1255 1256 dprintf((LOG, "--->>> NOT FOUND!" )); 1256 1257 *import = (ULONG)MissingApi; 1257 }1258 else *import = apiaddr;1258 } 1259 else *import = apiaddr; 1259 1260 } 1260 1261 //****************************************************************************** … … 1333 1334 ULONG nsize; 1334 1335 1335 if(nameexports == NULL) {1336 if(nameexports == NULL) { 1336 1337 nameExportSize= 4096; 1337 1338 nameexports = (NameExport *)malloc(nameExportSize); 1338 1339 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) { 1342 1343 nameExportSize += 4096; 1343 1344 char *tmp = (char *)nameexports; … … 1346 1347 curnameexport = (NameExport *)((ULONG)nameexports + nsize); 1347 1348 free(tmp); 1348 }1349 if(fAbsoluteAddress) {//forwarders use absolute address1350 curnameexport->virtaddr = virtaddr;1351 }1352 elsecurnameexport->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)) 1359 1360 curnameexport->nlength = sizeof(curnameexport->name); 1360 1361 1361 curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);1362 curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength); 1362 1363 } 1363 1364 //****************************************************************************** … … 1365 1366 void Win32PeLdrImage::AddOrdExport(ULONG virtaddr, ULONG ordinal, BOOL fAbsoluteAddress) 1366 1367 { 1367 if(ordexports == NULL) {1368 if(ordexports == NULL) { 1368 1369 ordexports = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport)); 1369 1370 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++; 1377 1379 } 1378 1380 //****************************************************************************** … … 1547 1549 ULONG ulCurFixup; 1548 1550 int Size; 1549 Win32DllBase *WinDll; 1551 Win32DllBase *WinDll; 1552 Win32ImageBase *WinImage; 1550 1553 Section *section; 1551 1554 … … 1617 1620 if (fBorland) 1618 1621 dprintf((LOG, "Borland-styled PE-File." )); 1622 1619 1623 //Store modules 1620 1624 dprintf((LOG, "%d imported Modules: ", cModules )); … … 1672 1676 { //not found, so load it 1673 1677 WinDll = loadDll(pszCurModule); 1674 if(WinDll == NULL) {1675 return FALSE;1676 }1677 1678 } 1678 1679 else { … … 1680 1681 dprintf((LOG, "Already found ", pszCurModule)); 1681 1682 } 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 } 1689 1700 1690 1701 pulImport = (PULONG)((ULONG)pulImport + (ULONG)win32file); … … 1694 1705 section = findSectionByOS2Addr(ulCurFixup); 1695 1706 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; 1698 1709 } 1699 1710 //SvL: Read page from disk … … 1706 1717 if (pulImport[j] & IMAGE_ORDINAL_FLAG) { //ordinal 1707 1718 dprintf((LOG, "0x%08x Imported function %s @%d", ulCurFixup , pszCurModule, (pulImport[j] & ~IMAGE_ORDINAL_FLAG) )); 1708 StoreImportByOrd(Win Dll, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup);1719 StoreImportByOrd(WinImage, pulImport[j] & ~IMAGE_ORDINAL_FLAG, ulCurFixup); 1709 1720 } 1710 1721 else { //name … … 1719 1730 //KSO - Aug 6 1998 1:15am:this eases comparing... 1720 1731 char *pszFunctionName = (char*)(pulImport[j] + (ULONG)win32file + 2); 1721 dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup, pszFunctionName, Win Dll->getApi(pszFunctionName)));1722 StoreImportByName(Win Dll, pszFunctionName, ulCurFixup);1732 dprintf((LOG, "0x%08x Imported function %s (0x%08x)", ulCurFixup, pszFunctionName, WinImage->getApi(pszFunctionName))); 1733 StoreImportByName(WinImage, pszFunctionName, ulCurFixup); 1723 1734 } 1724 1735 ulCurFixup += sizeof(IMAGE_THUNK_DATA); 1725 1736 j++; 1726 if((ulCurFixup & 0xfff) == 0) {1737 if((ulCurFixup & 0xfff) == 0) { 1727 1738 commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE); 1728 1739 DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE); 1729 1740 nrPages++; 1730 }1741 } 1731 1742 } 1732 1743 //SvL: And restore original protection flags … … 1748 1759 BOOL Win32PeLdrImage::insideModule(ULONG address) 1749 1760 { 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; 1754 1765 } 1755 1766 //****************************************************************************** … … 1759 1770 Section *sect; 1760 1771 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; 1766 1777 } 1767 1778 //****************************************************************************** … … 1769 1780 ULONG Win32PeLdrImage::getImageSize() 1770 1781 { 1771 return imageSize;1782 return imageSize; 1772 1783 } 1773 1784 //****************************************************************************** … … 1781 1792 ULONG ulAPIOrdinal; /* api requested by ordinal */ 1782 1793 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) 1798 1796 { 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); 1805 1816 } 1806 1817 //****************************************************************************** … … 1812 1823 NameExport *nexport; 1813 1824 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 this1821 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); 1829 1840 } 1830 1841 //****************************************************************************** … … 1833 1844 ULONG Win32PeLdrImage::getVersion() 1834 1845 { 1835 return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion;1846 return (oh.MajorOperatingSystemVersion << 16) | oh.MinorOperatingSystemVersion; 1836 1847 } 1837 1848 //****************************************************************************** … … 1842 1853 int r; 1843 1854 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; 1846 1869 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); //giggle1853 1854 if( r != MBID_IGNORE )1855 ExitProcess(987);1856 1857 fIgnore = TRUE;1858 return(0);1859 1870 } 1860 1871 /******************************************************************************/
Note:
See TracChangeset
for help on using the changeset viewer.