Changeset 281 for trunk/src/pe2lx/LX.CPP
- Timestamp:
- Jul 7, 1999, 10:11:58 AM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/pe2lx/LX.CPP
r97 r281 1 /* $Id: LX.CPP,v 1. 3 1999-06-10 17:08:51 phallerExp $ */1 /* $Id: LX.CPP,v 1.4 1999-07-07 08:11:10 sandervl Exp $ */ 2 2 3 3 /* … … 59 59 //Register module in kernel32 (resource managment) 60 60 // EXE: 61 //push [esp+4] ;instance handle 61 //push [esp+8] ;reserved (to use same startup code as dll) 62 //push [esp+8] ;instance handle 62 63 //push internal pe2lx version 63 64 //push version resource id 64 65 //push name_lookup_tableid 65 66 //push orgres_lookup_tableid 67 //push tib_object_address 68 //push tib_object_size 69 //push tib_index_address 70 //push tib_callback_address 71 //push original_entrypoint 66 72 //mov ecx, KERNEL32:RegisterResourceInfo (stdcall) 67 73 //call ecx 68 //69 //call original_entrypoint70 74 //ret 71 75 // 72 76 // DLL: 73 //cmp [esp+8], 1 74 //je detach 75 //push [esp+4] ;instance handle 77 //push [esp+8] ;attach or detach 78 //push [esp+8] ;instance handle 76 79 //push internal pe2lx version 77 80 //push version resource id 78 81 //push name_lookup_tableid 79 82 //push orgres_lookup_tableid 83 //push tib_object_address 84 //push tib_index_address 85 //push tib_callback_address 80 86 //mov ecx, KERNEL32:RegisterDll (stdcall) 87 //push original_entrypoint 81 88 //call ecx 82 ///DLL entrypoint:83 /// BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)84 //push 085 //mov ecx, [esp+12]86 //xor ecx, 1 //0 -> 1, 1 -> 087 //push ecx //push entry type (1 = attach process, 0 = detach process)88 //push [esp+12] //push module handle89 //call testje90 89 //ret 91 //detach: 92 //push [esp+4] //instance handle 93 //call KERNEL32:DllExitList 94 //ret 95 96 #define EXE_OFF_ENTRYFIX (1+19+2+10) 97 #define EXE_OFF_TIBCALL (EXE_OFF_ENTRYFIX-1) 98 #define EXE_OFF_NAMETABLE (3+2+10) 90 99 91 //SvL: 18-7-'98: offsets for internal pe2lx version & version resource id 100 #define EXE_OFF_PE2LXVER (5) 101 #define EXE_OFF_VERRESID (10) 102 #define EXE_TIBSIZE sizeof(szEXETIBFix) 103 #define EXE_OFF_KERIMP (EXE_OFF_NAMETABLE+10) 104 105 #define DLL_OFF_ENTRYFIX (15+28+10) 106 //#define DLL_OFF_ENTRYFIX (16+28+10) 107 #define DLL_OFF_TIBCALL (DLL_OFF_ENTRYFIX-1) 108 //SvL: 18-7-'98: offsets for internal pe2lx version & version resource id 109 #define DLL_OFF_PE2LXVER (12) 110 #define DLL_OFF_VERRESID (17) 111 #define DLL_OFF_NAMETABLE (12+10) 112 //#define DLL_OFF_NAMETABLE 12+1+10 113 #define DLL_TIBSIZE sizeof(szDLLTIBFix) 114 #define DLL_OFF_KERIMP (DLL_OFF_NAMETABLE+10) 115 #define DLL_OFF_KERIMP2 (sizeof(szDLLTIBFix)-5) 92 #define EXE_OFF_PE2LXVER (9) 93 #define EXE_OFF_VERRESID (14) 94 #define EXE_OFF_NAMETABLE (19) 95 #define EXE_OFF_ORGRESTABLE (24) 96 #define EXE_OFF_TIBOBJ_ADDR (29) 97 #define EXE_OFF_TIBINIT_SIZE (34) 98 #define EXE_OFF_TIBOBJ_SIZE (39) 99 #define EXE_OFF_TIB_INDEX (44) 100 #define EXE_OFF_TIB_CALLBACK_ADDR (49) 101 #define EXE_OFF_ENTRYPOINT (54) 102 #define EXE_OFF_ENTRYFIX (59) 103 #define EXE_TIB_SIZE sizeof(szEXETIBFix) 116 104 117 105 #define SIZE_TIBCALL 5 118 106 119 #define EXTRA_FIXUPS 2 //extra kernel32 imports 107 #define EXTRA_FIXUPS 1 //extra kernel32 import 108 #define EXTRA_TLS_FIXUPS 3 //3 data fixups for TLS support (start, index & callback) 120 109 121 110 static unsigned char szEXETIBFix[] = { 122 //push [esp+4] 123 0xFF, 0x74, 0x24, 0x04, 111 //push [esp+8] 112 0xFF, 0x74, 0x24, 0x08, 113 //push [esp+4] 114 0xFF, 0x74, 0x24, 0x08, 124 115 //SvL: 18-7-'98: push internal pe2lx version 125 116 0x68, 0x00, 0x00, 0x00, 0x00, 126 117 //SvL: 18-7-'98: push version resource id (-1 = no version resource present) 127 118 0x68, 0xFF, 0xFF, 0xFF, 0xFF, 128 //push [table id1] (kernel32 proc depends on 0x77777777 & 0x88888888!!)119 //push [table id1] (kernel32 proc depends on 0x77777777 & 0x88888888!!) 129 120 //Svl: So does void LXHeader::SaveConvertedNames()! 130 121 0x68, 0x77, 0x77, 0x77, 0x77, 131 //push [table id1]122 //push [table id1] 132 123 0x68, 0x88, 0x88, 0x88, 0x88, 133 //mov ecx, KERNEL32:RegisterResourceInfo (stdcall) 124 //push tib_object_address 125 0x68, 0x00, 0x00, 0x00, 0x00, 126 //push tib_initialized_size 127 0x68, 0x00, 0x00, 0x00, 0x00, 128 //push tib_object_size 129 0x68, 0x00, 0x00, 0x00, 0x00, 130 //push tib_index_address 131 0x68, 0x00, 0x00, 0x00, 0x00, 132 //push tib_callback_address 133 0x68, 0x00, 0x00, 0x00, 0x00, 134 //push original_entrypoint 135 0x68, 0x00, 0x00, 0x00, 0x00, 136 //mov ecx, KERNEL32:RegisterResourceInfo (stdcall) 134 137 0xB9, 0x99, 0x99, 0x99, 0x99, 135 //call ecx138 //call ecx 136 139 0xFF, 0xD1, 137 //call entrypoint138 0xE8, 0x8C, 0xFF, 0xFF, 0xFF,139 //ret140 0xC3};141 142 //WARNING: Kernel32.dll depends on this layout (offsets of dllexitlist &143 // entry point call are hardcoded! (both calculated in RegisterDll)144 static unsigned char szDLLTIBFix[] = {145 //cmp [esp+8], 1146 // 0xCC, 0x80, 0x7C, 0x24, 0x08, 0x01,147 0x80, 0x7C, 0x24, 0x08, 0x01,148 //je detach149 0x74, 0x33,150 //push [esp+4]151 0xFF, 0x74, 0x24, 0x04,152 //SvL: 18-7-'98: push internal pe2lx version153 0x68, 0x00, 0x00, 0x00, 0x00,154 //SvL: 18-7-'98: push version resource id (-1 = no version resource present)155 0x68, 0xFF, 0xFF, 0xFF, 0xFF,156 //push [table id1] (kernel32 proc depends on 0x77777777 & 0x88888888!!)157 //Svl: So does void LXHeader::SaveConvertedNames()!158 0x68, 0x77, 0x77, 0x77, 0x77,159 //push [table id1]160 0x68, 0x88, 0x88, 0x88, 0x88,161 //mov ecx, KERNEL32:RegisterDll (stdcall)162 0xB9, 0x99, 0x99, 0x99, 0x99,163 //call ecx164 0xFF, 0xD1,165 //push 0166 0x6A, 0x00,167 0x8B, 0x4C, 0x24, 0x0C,168 0x83, 0xF1, 0x01,169 0x51, 0xFF, 0x74, 0x24, 0x0C,170 //call entrypoint171 0xE8, 0x80, 0xFF, 0xFF, 0xFF,172 //ret173 0xC3,174 //detach:175 //push [esp+4]176 0xFF, 0x74, 0x24, 0x04,177 //call KERNEL32:DllExitList (stdcall)178 0xE8, 0x00, 0x00, 0x00, 0x00,179 140 //ret 180 141 0xC3}; … … 191 152 cvtname(NULL), curcvtname(0), nrcvtnames(0), datapage(NULL), 192 153 cvtnametableid(0), orgrestableid(0), orgrestable(NULL), nrorgres(0), 193 kernel32Object(-1), TIBOffKerImport(0), fConsole(FALSE), 154 kernel32Object(-1), fConsole(FALSE), 155 tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0), tlsCallBackAddr(0), 194 156 VersionResourceId(-1), impnamesize(0), impmodulesize(0) 195 157 { … … 214 176 ZERO(ResSection); 215 177 memset(&PESection[0], 0, sizeof(PESection)); 216 217 szTIBFix = &szEXETIBFix[0];218 TIBSize = EXE_TIBSIZE;219 TIBOffEntry = EXE_OFF_ENTRYFIX;220 TIBOffCall = EXE_OFF_TIBCALL;221 TIBOffName = EXE_OFF_NAMETABLE;222 //SvL: 18-7-'98: Internal pe2lx version and version resource id offsets223 TIBOffPe2lxVer = EXE_OFF_PE2LXVER;224 TIBOffVerResId = EXE_OFF_VERRESID;225 178 } 226 179 //****************************************************************************** … … 252 205 this->IsEXE = IsEXE; 253 206 if(IsEXE) { 254 szTIBFix = &szEXETIBFix[0]; 255 TIBSize = EXE_TIBSIZE; 256 TIBOffEntry = EXE_OFF_ENTRYFIX; 257 TIBOffCall = EXE_OFF_TIBCALL; 258 TIBOffName = EXE_OFF_NAMETABLE; 259 TIBOffKerImport = EXE_OFF_KERIMP; 260 //SvL: 18-7-'98: Internal pe2lx version and version resource id offsets 261 TIBOffPe2lxVer = EXE_OFF_PE2LXVER; 262 TIBOffVerResId = EXE_OFF_VERRESID; 263 LXHdr.e32_mflags |= E32MODEXE; 207 LXHdr.e32_mflags |= E32MODEXE; 264 208 } 265 209 else {//Assuming per process initialization/termination... 266 szTIBFix = &szDLLTIBFix[0];267 TIBSize = DLL_TIBSIZE;268 TIBOffEntry = DLL_OFF_ENTRYFIX;269 TIBOffCall = DLL_OFF_TIBCALL;270 TIBOffName = DLL_OFF_NAMETABLE;271 TIBOffKerImport = DLL_OFF_KERIMP;272 //SvL: 18-7-'98: Internal pe2lx version and version resource id offsets273 TIBOffPe2lxVer = DLL_OFF_PE2LXVER;274 TIBOffVerResId = DLL_OFF_VERRESID;275 210 LXHdr.e32_mflags |= E32LIBINIT | E32LIBTERM | E32MODDLL; 276 211 } … … 437 372 rawsize = CombinedData.rawsize; 438 373 439 datapage[0].size = TIBSize;374 datapage[0].size = EXE_TIB_SIZE; 440 375 datapage[0].flags = 0; 441 376 idx = 1; … … 616 551 void LXHeader::SetNrOff32Fixups(int nr) 617 552 { 618 intfixuprec = (intfixup *)malloc(nr*sizeof(intfixup)); 619 memset(intfixuprec, 0, nr*sizeof(intfixup)); 620 } 621 //****************************************************************************** 622 //****************************************************************************** 623 void LXHeader::AddOff32Fixup(int address) 553 intfixuprec = (intfixup *)malloc((nr+EXTRA_TLS_FIXUPS)*sizeof(intfixup)); 554 memset(intfixuprec, 0, (nr+EXTRA_TLS_FIXUPS)*sizeof(intfixup)); 555 } 556 //****************************************************************************** 557 //SvL: TLS Section support 558 //Add three interal fixups. One for start of TLS section, one for DWORD that 559 //receives the TLS index and one for the array of TLS callbacks 560 //Also one for the entrypoint address. 561 //****************************************************************************** 562 void LXHeader::AddExtraFixups() 563 { 564 *(ULONG *)&szEXETIBFix[EXE_OFF_TIBOBJ_ADDR] = tlsAddress; 565 *(ULONG *)&szEXETIBFix[EXE_OFF_TIBINIT_SIZE] = tlsInitSize; 566 *(ULONG *)&szEXETIBFix[EXE_OFF_TIBOBJ_SIZE] = tlsTotalSize; 567 *(ULONG *)&szEXETIBFix[EXE_OFF_TIB_INDEX] = tlsIndexAddr; 568 *(ULONG *)&szEXETIBFix[EXE_OFF_TIB_CALLBACK_ADDR] = tlsCallBackAddr; 569 *(ULONG *)&szEXETIBFix[EXE_OFF_ENTRYPOINT] = EntryAddress; 570 571 if(LXHdr.e32_mflags & E32NOINTFIX) 572 return; //no internal fixups, so we don't have to add them either 573 574 AddOff32Fixup(PESection[0].address + EXE_OFF_ENTRYPOINT, TRUE); 575 576 if(tlsAddress == 0) 577 return; //no TLS section in this image 578 579 AddOff32Fixup(PESection[0].address + EXE_OFF_TIBOBJ_ADDR, TRUE); 580 AddOff32Fixup(PESection[0].address + EXE_OFF_TIB_INDEX, TRUE); 581 AddOff32Fixup(PESection[0].address + EXE_OFF_TIB_CALLBACK_ADDR, TRUE); 582 583 return; 584 } 585 //****************************************************************************** 586 //SvL: if fLookatStartupCode is set to true, we'll read the szEXETIBFix 587 // fixup addresses instead of those in the section 588 // (section 0 is main code section which doesn't include szEXETIBFix) 589 //****************************************************************************** 590 void LXHeader::AddOff32Fixup(int address, BOOL fLookatStartupCode) 624 591 { 625 592 static intfixup crossedpage; … … 654 621 if(type == 0) { // +1 for tibfix page 655 622 assert(PESection[type].type & SECTION_CODE); 656 targetaddr = *(int *)(PESection[type].rawdata + (address - (PESection[type].address + (PESection[type].nrinvalidpages+1)*PAGE_SIZE))); 623 if(fLookatStartupCode) 624 { 625 targetaddr = *(int *)(&szEXETIBFix[0] + (address & 0xFF)); 626 } 627 else { 628 targetaddr = *(int *)(PESection[type].rawdata + (address - (PESection[type].address + (PESection[type].nrinvalidpages+1)*PAGE_SIZE))); 629 } 657 630 } 658 631 else targetaddr = *(int *)(PESection[type].rawdata + (address - (PESection[type].address + PESection[type].nrinvalidpages*PAGE_SIZE))); … … 836 809 dest += size-4; 837 810 } 811 #if 0 812 else 813 if(strcmp(dest, "OLE32") == 0) {//SvL: Name conflict with Lotus OLE32 dll 814 strcpy(dest, "OLE32OS2"); 815 dest[-1] = 8; //change module name length 816 dest += 8; 817 } 818 #endif 838 819 else { 839 820 if(strcmp(dest, "KERNEL32") == 0) { … … 1018 999 if(PESection[i].type == SECTION_COMBINEDDATA) { 1019 1000 if(j == PESection[i].nrinvalidpages) { 1020 objpage[idx].o32_pagesize = (USHORT)( TIBSize);1001 objpage[idx].o32_pagesize = (USHORT)(EXE_TIB_SIZE); 1021 1002 objpage[idx].o32_pageflags = 0; 1022 pagedataoffset += TIBSize;1003 pagedataoffset += EXE_TIB_SIZE; 1023 1004 LXHdr.e32_mpages++; 1024 //Modify entry point address, and write original to TIBFix code1025 *(LONG *)&szTIBFix[TIBOffEntry] = LXHdr.e32_eip + PAGE_SIZE - TIBOffCall - SIZE_TIBCALL;1026 1005 LXHdr.e32_eip = PAGE_SIZE*PESection[i].nrinvalidpages; 1027 1006 objtable.o32_size = objtable.o32_mapsize*PAGE_SIZE; … … 1044 1023 else //take care of TIBFix code! 1045 1024 if(fFirstCode == FALSE && PESection[i].type & SECTION_CODE && j == PESection[i].nrinvalidpages) { 1046 objpage[idx].o32_pagesize = (USHORT) TIBSize;1025 objpage[idx].o32_pagesize = (USHORT)EXE_TIB_SIZE; 1047 1026 objpage[idx].o32_pageflags = 0; 1048 pagedataoffset += TIBSize;1027 pagedataoffset += EXE_TIB_SIZE; 1049 1028 LXHdr.e32_mpages++; 1050 //Modify entry point address, and write original to TIBFix code1051 *(LONG *)&szTIBFix[TIBOffEntry] = LXHdr.e32_eip + PAGE_SIZE - TIBOffCall - SIZE_TIBCALL;1052 1029 1053 1030 LXHdr.e32_eip = PAGE_SIZE*PESection[i].nrinvalidpages; … … 1197 1174 kerord->nr_stype = 0x07; //32 bits offset 1198 1175 kerord->nr_flags = 0x01 | 0x40; //import by ordinal + mod offset = 16 bits 1199 kerord->r32_soff = TIBOffKerImport;1176 kerord->r32_soff = EXE_OFF_ENTRYFIX; 1200 1177 kerord->r32_objmod = kernel32Object + 1; 1201 1178 if(IsEXE) … … 1204 1181 1205 1182 currealrec = (realintfixup *)((int)currealrec + sizeof(realnamefixup)); 1206 if(!IsEXE) {1207 kerord = (realordfixup *)currealrec;1208 kerord->nr_stype = 0x08; //32 bits self referencing offset1209 kerord->nr_flags = 0x01 | 0x40; //import by ordinal + mod offset = 16 bits1210 kerord->r32_soff = DLL_OFF_KERIMP2;1211 kerord->r32_objmod = kernel32Object + 1;1212 kerord->ord = ORD_KERNEL32DLLEXITLIST;1213 currealrec = (realintfixup *)((int)currealrec + sizeof(realnamefixup));1214 }1215 1183 } 1216 1184 //SvL: 16-9-'97, for multiple import pages … … 1233 1201 1234 1202 //Add extra kernel32 imports 1235 if(IsEXE) 1236 nrimpfixups++; 1237 else nrimpfixups += EXTRA_FIXUPS; 1203 nrimpfixups += EXTRA_FIXUPS; 1238 1204 1239 1205 //Write fixup page table … … 1258 1224 //Write Import Procedure Name array 1259 1225 if(impnamesize) { 1260 DosWrite(win32handle, impnames, impnamesize, &ulWrite);1226 DosWrite(win32handle, impnames, impnamesize, &ulWrite); 1261 1227 } 1262 1228 … … 1270 1236 //Put our special TIB fix to code section first! (SvL: 30-7-'97) 1271 1237 if(PESection[i].type & SECTION_CODE && fFirstCode == FALSE) { 1272 DosWrite(win32handle, sz TIBFix, TIBSize, &ulWrite);1238 DosWrite(win32handle, szEXETIBFix, EXE_TIB_SIZE, &ulWrite); 1273 1239 fFirstCode = TRUE; 1274 1240 } … … 1596 1562 1597 1563 //fill in parameter for RegisterResourceInfo 1598 *(LONG *)&sz TIBFix[TIBOffName] = cvtnametableid;1564 *(LONG *)&szEXETIBFix[EXE_OFF_NAMETABLE] = cvtnametableid; 1599 1565 1600 1566 StoreResource(cvtnametableid, RT_RCDATA, (int)curcvtname - (int)cvtname, (char *)cvtname); … … 1606 1572 1607 1573 //fill in parameter for RegisterResourceInfo 1608 *(LONG *)&sz TIBFix[TIBOffName+5] = (fConsole << 24) | orgrestableid;1574 *(LONG *)&szEXETIBFix[EXE_OFF_ORGRESTABLE] = (fConsole << 24) | orgrestableid; 1609 1575 1610 1576 StoreResource(orgrestableid, RT_RCDATA, (nrorgres+1)*sizeof(ULONG), (char *)orgrestable); 1611 1577 } 1612 1578 else { 1613 *(LONG *)&sz TIBFix[TIBOffName+5] = (fConsole << 24) | 0x888888;1579 *(LONG *)&szEXETIBFix[EXE_OFF_ORGRESTABLE] = (fConsole << 24) | 0x888888; 1614 1580 } 1615 1581 //SvL: 18-7-'98: Store internal pe2lx version and version resource id 1616 *(LONG *)&sz TIBFix[TIBOffPe2lxVer] = PE2LX_VERSION;1617 *(LONG *)&sz TIBFix[TIBOffVerResId] = VersionResourceId;1582 *(LONG *)&szEXETIBFix[EXE_OFF_PE2LXVER] = PE2LX_VERSION; 1583 *(LONG *)&szEXETIBFix[EXE_OFF_VERRESID] = VersionResourceId; 1618 1584 } 1619 1585 /******************************************************************************/
Note:
See TracChangeset
for help on using the changeset viewer.