4949
5050#include "MemoryModule.h"
5151
52+ typedef BOOL (WINAPI * DllEntryProc )(HINSTANCE hinstDLL , DWORD fdwReason , LPVOID lpReserved );
53+ typedef int (WINAPI * ExeEntryProc )(void );
54+
5255typedef struct {
5356 PIMAGE_NT_HEADERS headers ;
5457 unsigned char * codeBase ;
5558 HCUSTOMMODULE * modules ;
5659 int numModules ;
5760 int initialized ;
61+ int isDLL ;
62+ int isRelocated ;
5863 CustomLoadLibraryFunc loadLibrary ;
5964 CustomGetProcAddressFunc getProcAddress ;
6065 CustomFreeLibraryFunc freeLibrary ;
6166 void * userdata ;
67+ ExeEntryProc exeEntry ;
6268} MEMORYMODULE , * PMEMORYMODULE ;
6369
64- typedef BOOL (WINAPI * DllEntryProc )(HINSTANCE hinstDLL , DWORD fdwReason , LPVOID lpReserved );
65-
6670#define GET_HEADER_DICTIONARY (module , idx ) &(module)->headers->OptionalHeader.DataDirectory[idx]
6771
6872#ifdef DEBUG_OUTPUT
@@ -202,11 +206,12 @@ ExecuteTLS(PMEMORYMODULE module)
202206 }
203207}
204208
205- static void
209+ static int
206210PerformBaseRelocation (PMEMORYMODULE module , SIZE_T delta )
207211{
208212 DWORD i ;
209213 unsigned char * codeBase = module -> codeBase ;
214+ int result = 0 ;
210215
211216 PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_BASERELOC );
212217 if (directory -> Size > 0 ) {
@@ -254,7 +259,9 @@ PerformBaseRelocation(PMEMORYMODULE module, SIZE_T delta)
254259 // advance to next relocation block
255260 relocation = (PIMAGE_BASE_RELOCATION ) (((char * ) relocation ) + relocation -> SizeOfBlock );
256261 }
262+ result = 1 ;
257263 }
264+ return result ;
258265}
259266
260267static int
@@ -355,7 +362,6 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
355362 PIMAGE_NT_HEADERS old_header ;
356363 unsigned char * code , * headers ;
357364 SIZE_T locationDelta ;
358- DllEntryProc DllEntry ;
359365 BOOL successfull ;
360366
361367 dos_header = (PIMAGE_DOS_HEADER )data ;
@@ -410,6 +416,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
410416 result -> numModules = 0 ;
411417 result -> modules = NULL ;
412418 result -> initialized = 0 ;
419+ result -> isDLL = (old_header -> FileHeader .Characteristics & IMAGE_FILE_DLL ) != 0 ;
413420 result -> loadLibrary = loadLibrary ;
414421 result -> getProcAddress = getProcAddress ;
415422 result -> freeLibrary = freeLibrary ;
@@ -434,7 +441,9 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
434441 // adjust base address of imported data
435442 locationDelta = (SIZE_T )(code - old_header -> OptionalHeader .ImageBase );
436443 if (locationDelta != 0 ) {
437- PerformBaseRelocation (result , locationDelta );
444+ result -> isRelocated = PerformBaseRelocation (result , locationDelta );
445+ } else {
446+ result -> isRelocated = 1 ;
438447 }
439448
440449 // load required dlls and adjust function table of imports
@@ -451,14 +460,20 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
451460
452461 // get entry point of loaded library
453462 if (result -> headers -> OptionalHeader .AddressOfEntryPoint != 0 ) {
454- DllEntry = (DllEntryProc ) (code + result -> headers -> OptionalHeader .AddressOfEntryPoint );
455- // notify library about attaching to process
456- successfull = (* DllEntry )((HINSTANCE )code , DLL_PROCESS_ATTACH , 0 );
457- if (!successfull ) {
458- SetLastError (ERROR_DLL_INIT_FAILED );
459- goto error ;
463+ if (result -> isDLL ) {
464+ DllEntryProc DllEntry = (DllEntryProc ) (code + result -> headers -> OptionalHeader .AddressOfEntryPoint );
465+ // notify library about attaching to process
466+ successfull = (* DllEntry )((HINSTANCE )code , DLL_PROCESS_ATTACH , 0 );
467+ if (!successfull ) {
468+ SetLastError (ERROR_DLL_INIT_FAILED );
469+ goto error ;
470+ }
471+ result -> initialized = 1 ;
472+ } else {
473+ result -> exeEntry = (ExeEntryProc ) (code + result -> headers -> OptionalHeader .AddressOfEntryPoint );
460474 }
461- result -> initialized = 1 ;
475+ } else {
476+ result -> exeEntry = NULL ;
462477 }
463478
464479 return (HMEMORYMODULE )result ;
@@ -549,6 +564,17 @@ void MemoryFreeLibrary(HMEMORYMODULE mod)
549564 }
550565}
551566
567+ int MemoryCallEntryPoint (HMEMORYMODULE mod )
568+ {
569+ PMEMORYMODULE module = (PMEMORYMODULE )mod ;
570+
571+ if (module == NULL || module -> isDLL || module -> exeEntry == NULL || !module -> isRelocated ) {
572+ return -1 ;
573+ }
574+
575+ return module -> exeEntry ();
576+ }
577+
552578#define DEFAULT_LANGUAGE MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)
553579
554580HMEMORYRSRC MemoryFindResource (HMEMORYMODULE module , LPCTSTR name , LPCTSTR type )
0 commit comments