@@ -46,6 +46,7 @@ typedef struct {
4646typedef BOOL (WINAPI * DllEntryProc )(HINSTANCE hinstDLL , DWORD fdwReason , LPVOID lpReserved );
4747
4848#define GET_HEADER_DICTIONARY (module , idx ) &(module)->headers->OptionalHeader.DataDirectory[idx]
49+ #define CALCULATE_ADDRESS (base , offset ) (((DWORD)(base)) + (offset))
4950
5051#ifdef DEBUG_OUTPUT
5152static void
@@ -79,7 +80,7 @@ CopySections(const unsigned char *data, PIMAGE_NT_HEADERS old_headers, PMEMORYMO
7980 size = old_headers -> OptionalHeader .SectionAlignment ;
8081 if (size > 0 )
8182 {
82- dest = (unsigned char * )VirtualAlloc (codeBase + section -> VirtualAddress ,
83+ dest = (unsigned char * )VirtualAlloc (( unsigned char * ) CALCULATE_ADDRESS ( codeBase , section -> VirtualAddress ) ,
8384 size ,
8485 MEM_COMMIT ,
8586 PAGE_READWRITE );
@@ -93,11 +94,11 @@ CopySections(const unsigned char *data, PIMAGE_NT_HEADERS old_headers, PMEMORYMO
9394 }
9495
9596 // commit memory block and copy data from dll
96- dest = (unsigned char * )VirtualAlloc (codeBase + section -> VirtualAddress ,
97+ dest = (unsigned char * )VirtualAlloc (( unsigned char * ) CALCULATE_ADDRESS ( codeBase , section -> VirtualAddress ) ,
9798 section -> SizeOfRawData ,
9899 MEM_COMMIT ,
99100 PAGE_READWRITE );
100- memcpy (dest , data + section -> PointerToRawData , section -> SizeOfRawData );
101+ memcpy (dest , ( unsigned char * ) CALCULATE_ADDRESS ( data , section -> PointerToRawData ) , section -> SizeOfRawData );
101102 section -> Misc .PhysicalAddress = (DWORD )dest ;
102103 }
103104}
@@ -172,10 +173,10 @@ PerformBaseRelocation(PMEMORYMODULE module, DWORD delta)
172173 PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_BASERELOC );
173174 if (directory -> Size > 0 )
174175 {
175- PIMAGE_BASE_RELOCATION relocation = (PIMAGE_BASE_RELOCATION )(codeBase + directory -> VirtualAddress );
176+ PIMAGE_BASE_RELOCATION relocation = (PIMAGE_BASE_RELOCATION )CALCULATE_ADDRESS (codeBase , directory -> VirtualAddress );
176177 for (; relocation -> VirtualAddress > 0 ; )
177178 {
178- unsigned char * dest = (unsigned char * )(codeBase + relocation -> VirtualAddress );
179+ unsigned char * dest = (unsigned char * )CALCULATE_ADDRESS (codeBase , relocation -> VirtualAddress );
179180 unsigned short * relInfo = (unsigned short * )((unsigned char * )relocation + IMAGE_SIZEOF_BASE_RELOCATION );
180181 for (i = 0 ; i < ((relocation -> SizeOfBlock - IMAGE_SIZEOF_BASE_RELOCATION ) / 2 ); i ++ , relInfo ++ )
181182 {
@@ -195,7 +196,7 @@ PerformBaseRelocation(PMEMORYMODULE module, DWORD delta)
195196
196197 case IMAGE_REL_BASED_HIGHLOW :
197198 // change complete 32 bit address
198- patchAddrHL = (DWORD * )(dest + offset );
199+ patchAddrHL = (DWORD * )CALCULATE_ADDRESS (dest , offset );
199200 * patchAddrHL += delta ;
200201 break ;
201202
@@ -206,7 +207,7 @@ PerformBaseRelocation(PMEMORYMODULE module, DWORD delta)
206207 }
207208
208209 // advance to next relocation block
209- relocation = (PIMAGE_BASE_RELOCATION )((( DWORD ) relocation ) + relocation -> SizeOfBlock );
210+ relocation = (PIMAGE_BASE_RELOCATION )CALCULATE_ADDRESS ( relocation , relocation -> SizeOfBlock );
210211 }
211212 }
212213}
@@ -220,11 +221,11 @@ BuildImportTable(PMEMORYMODULE module)
220221 PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY (module , IMAGE_DIRECTORY_ENTRY_IMPORT );
221222 if (directory -> Size > 0 )
222223 {
223- PIMAGE_IMPORT_DESCRIPTOR importDesc = (PIMAGE_IMPORT_DESCRIPTOR )(codeBase + directory -> VirtualAddress );
224+ PIMAGE_IMPORT_DESCRIPTOR importDesc = (PIMAGE_IMPORT_DESCRIPTOR )CALCULATE_ADDRESS (codeBase , directory -> VirtualAddress );
224225 for (; !IsBadReadPtr (importDesc , sizeof (IMAGE_IMPORT_DESCRIPTOR )) && importDesc -> Name ; importDesc ++ )
225226 {
226227 DWORD * thunkRef , * funcRef ;
227- HMODULE handle = LoadLibrary ((LPCSTR )(codeBase + importDesc -> Name ));
228+ HMODULE handle = LoadLibrary ((LPCSTR )CALCULATE_ADDRESS (codeBase , importDesc -> Name ));
228229 if (handle == INVALID_HANDLE_VALUE )
229230 {
230231#if DEBUG_OUTPUT
@@ -244,19 +245,19 @@ BuildImportTable(PMEMORYMODULE module)
244245 module -> modules [module -> numModules ++ ] = handle ;
245246 if (importDesc -> OriginalFirstThunk )
246247 {
247- thunkRef = (DWORD * )(codeBase + importDesc -> OriginalFirstThunk );
248- funcRef = (DWORD * )(codeBase + importDesc -> FirstThunk );
248+ thunkRef = (DWORD * )CALCULATE_ADDRESS (codeBase , importDesc -> OriginalFirstThunk );
249+ funcRef = (DWORD * )CALCULATE_ADDRESS (codeBase , importDesc -> FirstThunk );
249250 } else {
250251 // no hint table
251- thunkRef = (DWORD * )(codeBase + importDesc -> FirstThunk );
252- funcRef = (DWORD * )(codeBase + importDesc -> FirstThunk );
252+ thunkRef = (DWORD * )CALCULATE_ADDRESS (codeBase , importDesc -> FirstThunk );
253+ funcRef = (DWORD * )CALCULATE_ADDRESS (codeBase , importDesc -> FirstThunk );
253254 }
254255 for (; * thunkRef ; thunkRef ++ , funcRef ++ )
255256 {
256257 if IMAGE_SNAP_BY_ORDINAL (* thunkRef )
257258 * funcRef = (DWORD )GetProcAddress (handle , (LPCSTR )IMAGE_ORDINAL (* thunkRef ));
258259 else {
259- PIMAGE_IMPORT_BY_NAME thunkData = (PIMAGE_IMPORT_BY_NAME )(codeBase + * thunkRef );
260+ PIMAGE_IMPORT_BY_NAME thunkData = (PIMAGE_IMPORT_BY_NAME )CALCULATE_ADDRESS (codeBase , * thunkRef );
260261 * funcRef = (DWORD )GetProcAddress (handle , (LPCSTR )& thunkData -> Name );
261262 }
262263 if (* funcRef == 0 )
@@ -368,7 +369,7 @@ HMEMORYMODULE MemoryLoadLibrary(const void *data)
368369 // get entry point of loaded library
369370 if (result -> headers -> OptionalHeader .AddressOfEntryPoint != 0 )
370371 {
371- DllEntry = (DllEntryProc )(code + result -> headers -> OptionalHeader .AddressOfEntryPoint );
372+ DllEntry = (DllEntryProc )CALCULATE_ADDRESS (code , result -> headers -> OptionalHeader .AddressOfEntryPoint );
372373 if (DllEntry == 0 )
373374 {
374375#if DEBUG_OUTPUT
@@ -409,16 +410,16 @@ FARPROC MemoryGetProcAddress(HMEMORYMODULE module, const char *name)
409410 // no export table found
410411 return NULL ;
411412
412- exports = (PIMAGE_EXPORT_DIRECTORY )(codeBase + directory -> VirtualAddress );
413+ exports = (PIMAGE_EXPORT_DIRECTORY )CALCULATE_ADDRESS (codeBase , directory -> VirtualAddress );
413414 if (exports -> NumberOfNames == 0 || exports -> NumberOfFunctions == 0 )
414415 // DLL doesn't export anything
415416 return NULL ;
416417
417418 // search function name in list of exported names
418- nameRef = (DWORD * )(codeBase + exports -> AddressOfNames );
419- ordinal = (WORD * )(codeBase + exports -> AddressOfNameOrdinals );
419+ nameRef = (DWORD * )CALCULATE_ADDRESS (codeBase , exports -> AddressOfNames );
420+ ordinal = (WORD * )CALCULATE_ADDRESS (codeBase , exports -> AddressOfNameOrdinals );
420421 for (i = 0 ; i < exports -> NumberOfNames ; i ++ , nameRef ++ , ordinal ++ )
421- if (stricmp (name , (const char * )(codeBase + * nameRef )) == 0 )
422+ if (stricmp (name , (const char * )CALCULATE_ADDRESS (codeBase , * nameRef )) == 0 )
422423 {
423424 idx = * ordinal ;
424425 break ;
@@ -433,7 +434,7 @@ FARPROC MemoryGetProcAddress(HMEMORYMODULE module, const char *name)
433434 return NULL ;
434435
435436 // AddressOfFunctions contains the RVAs to the "real" functions
436- return (FARPROC )(codeBase + * (DWORD * )(codeBase + exports -> AddressOfFunctions + (idx * 4 )));
437+ return (FARPROC )CALCULATE_ADDRESS (codeBase , * (DWORD * )CALCULATE_ADDRESS (codeBase , exports -> AddressOfFunctions + (idx * 4 )));
437438}
438439
439440void MemoryFreeLibrary (HMEMORYMODULE mod )
@@ -446,7 +447,7 @@ void MemoryFreeLibrary(HMEMORYMODULE mod)
446447 if (module -> initialized != 0 )
447448 {
448449 // notify library about detaching from process
449- DllEntryProc DllEntry = (DllEntryProc )(module -> codeBase + module -> headers -> OptionalHeader .AddressOfEntryPoint );
450+ DllEntryProc DllEntry = (DllEntryProc )CALCULATE_ADDRESS (module -> codeBase , module -> headers -> OptionalHeader .AddressOfEntryPoint );
450451 (* DllEntry )((HINSTANCE )module -> codeBase , DLL_PROCESS_DETACH , 0 );
451452 module -> initialized = 0 ;
452453 }
0 commit comments