Changeset 2572 for trunk/src/peldr/pe.cpp
- Timestamp:
- Jan 30, 2000, 3:48:52 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/peldr/pe.cpp
r1832 r2572 1 /* $Id: pe.cpp,v 1.1 2 1999-11-24 19:33:04sandervl Exp $ */1 /* $Id: pe.cpp,v 1.13 2000-01-30 14:48:51 sandervl Exp $ */ 2 2 3 3 /* … … 63 63 WIN32CTOR CreateWin32Exe = 0; 64 64 65 ULONG reservedMemory = 0; 66 67 void AllocateExeMem(char *filename); 68 69 //****************************************************************************** 70 //****************************************************************************** 65 71 int main(int argc, char *argv[]) 66 72 { … … 71 77 HMODULE hmodPMWin = 0, hmodKernel32 = 0; 72 78 79 if(argc >= 2) { 80 strcpy(exeName, argv[1]); 81 strupr(exeName); 82 AllocateExeMem(exeName); 83 } 84 73 85 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin); 74 86 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize); … … 97 109 strcpy(exeName, argv[1]); 98 110 strupr(exeName); 99 if(CreateWin32Exe(exeName, ReserveMem()) == FALSE) {111 if(CreateWin32Exe(exeName, reservedMemory) == FALSE) { 100 112 goto fail; 101 113 } … … 117 129 } 118 130 //****************************************************************************** 119 //****************************************************************************** 131 //SvL: Reserve memory for win32 exes without fixups 132 // This is done before any Odin or PMWIN dll is loaded, so we'll get 133 // a very low virtual address. (which is exactly what we want) 134 //****************************************************************************** 135 void AllocateExeMem(char *filename) 136 { 137 HFILE dllfile = 0; 138 char szFileName[CCHMAXPATH], *tmp; 139 ULONG action, ulRead, signature; 140 APIRET rc; 141 IMAGE_DOS_HEADER doshdr; 142 IMAGE_OPTIONAL_HEADER oh; 143 IMAGE_FILE_HEADER fh; 144 ULONG address = 0; 145 ULONG *memallocs; 146 ULONG alloccnt = 0; 147 ULONG diff, i, baseAddress; 148 ULONG ulSysinfo, flAllocMem = 0; 149 150 strcpy(szFileName, filename); 151 tmp = szFileName; 152 while(*tmp != ' ' && *tmp != 0) 153 tmp++; 154 *tmp = 0; 155 156 if(!strchr(szFileName, '.')) { 157 strcat(szFileName,".EXE"); 158 } 159 rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL); 160 if(rc != 0) { 161 if(!strstr(szFileName, ".EXE")) { 162 strcat(szFileName,".EXE"); 163 } 164 } 165 else DosClose(dllfile); 166 167 rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL); 168 if(rc) { 169 goto end; //oops 170 } 171 172 //read dos header 173 if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) { 174 goto end; 175 } 176 if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) { 177 goto end; 178 } 179 //read signature dword 180 if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) { 181 goto end; 182 } 183 //read pe header 184 if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) { 185 goto end; 186 } 187 //read optional header 188 if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) { 189 goto end; 190 } 191 if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) { 192 goto end; 193 } 194 if(!(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)) { 195 goto end; //no need to allocate anything now 196 } 197 198 // check for high memory support 199 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo)); 200 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB 201 { 202 flAllocMem = PAG_ANY; // high memory support. Let's use it! 203 } 204 205 //Reserve enough space to store 4096 pointers to 1MB memory chunks 206 memallocs = (ULONG *)alloca(4096*sizeof(ULONG *)); 207 if(memallocs == NULL) { 208 goto end; //oops 209 } 210 211 if(oh.ImageBase < 512*1024*1024) { 212 flAllocMem = 0; 213 } 214 else { 215 if(flAllocMem == 0) { 216 goto end; //no support for > 512 MB 217 } 218 } 219 while(TRUE) { 220 rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem); 221 if(rc) break; 222 223 if(address + FALLOC_SIZE >= oh.ImageBase) { 224 if(address > oh.ImageBase) {//we've passed it! 225 DosFreeMem((PVOID)address); 226 break; 227 } 228 //found the right address 229 DosFreeMem((PVOID)address); 230 231 diff = oh.ImageBase - address; 232 if(diff) { 233 rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem); 234 if(rc) break; 235 } 236 rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem); 237 if(rc) break; 238 239 if(diff) DosFreeMem((PVOID)address); 240 241 reservedMemory = baseAddress; 242 break; 243 } 244 memallocs[alloccnt++] = address; 245 } 246 for(i=0;i<alloccnt;i++) { 247 DosFreeMem((PVOID)memallocs[i]); 248 } 249 end: 250 if(dllfile) DosClose(dllfile); 251 return; 252 } 253 //****************************************************************************** 254 //******************************************************************************
Note:
See TracChangeset
for help on using the changeset viewer.