@@ -102,6 +102,11 @@ AlignValueUp(size_t value, size_t alignment) {
102
102
return (value + alignment - 1 ) & ~(alignment - 1 );
103
103
}
104
104
105
+ static inline void *
106
+ OffsetPointer (void * data , ptrdiff_t offset ) {
107
+ return (void * ) ((uintptr_t ) data + offset );
108
+ }
109
+
105
110
static inline void
106
111
OutputLastError (const char * msg )
107
112
{
@@ -153,9 +158,11 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade
153
158
}
154
159
155
160
// Always use position from file to support alignments smaller
156
- // than page size.
161
+ // than page size (allocation above will align to page size) .
157
162
dest = codeBase + section -> VirtualAddress ;
158
- section -> Misc .PhysicalAddress = (DWORD ) (uintptr_t ) dest ;
163
+ // NOTE: On 64bit systems we truncate to 32bit here but expand
164
+ // again later when "PhysicalAddress" is used.
165
+ section -> Misc .PhysicalAddress = (DWORD ) ((uintptr_t ) dest & 0xffffffff );
159
166
memset (dest , 0 , section_size );
160
167
}
161
168
@@ -178,10 +185,12 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade
178
185
}
179
186
180
187
// Always use position from file to support alignments smaller
181
- // than page size.
188
+ // than page size (allocation above will align to page size) .
182
189
dest = codeBase + section -> VirtualAddress ;
183
190
memcpy (dest , data + section -> PointerToRawData , section -> SizeOfRawData );
184
- section -> Misc .PhysicalAddress = (DWORD ) (uintptr_t ) dest ;
191
+ // NOTE: On 64bit systems we truncate to 32bit here but expand
192
+ // again later when "PhysicalAddress" is used.
193
+ section -> Misc .PhysicalAddress = (DWORD ) ((uintptr_t ) dest & 0xffffffff );
185
194
}
186
195
187
196
return TRUE;
@@ -261,7 +270,9 @@ FinalizeSections(PMEMORYMODULE module)
261
270
int i ;
262
271
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION (module -> headers );
263
272
#ifdef _WIN64
264
- uintptr_t imageOffset = (module -> headers -> OptionalHeader .ImageBase & 0xffffffff00000000 );
273
+ // "PhysicalAddress" might have been truncated to 32bit above, expand to
274
+ // 64bits again.
275
+ uintptr_t imageOffset = ((uintptr_t ) module -> headers -> OptionalHeader .ImageBase & 0xffffffff00000000 );
265
276
#else
266
277
static const uintptr_t imageOffset = 0 ;
267
278
#endif
@@ -345,7 +356,7 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
345
356
for (; relocation -> VirtualAddress > 0 ; ) {
346
357
DWORD i ;
347
358
unsigned char * dest = codeBase + relocation -> VirtualAddress ;
348
- unsigned short * relInfo = (unsigned short * )(( unsigned char * ) relocation + IMAGE_SIZEOF_BASE_RELOCATION );
359
+ unsigned short * relInfo = (unsigned short * ) OffsetPointer ( relocation , IMAGE_SIZEOF_BASE_RELOCATION );
349
360
for (i = 0 ; i < ((relocation -> SizeOfBlock - IMAGE_SIZEOF_BASE_RELOCATION ) / 2 ); i ++ , relInfo ++ ) {
350
361
// the upper 4 bits define the type of relocation
351
362
int type = * relInfo >> 12 ;
@@ -382,7 +393,7 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
382
393
}
383
394
384
395
// advance to next relocation block
385
- relocation = (PIMAGE_BASE_RELOCATION ) ((( char * ) relocation ) + relocation -> SizeOfBlock );
396
+ relocation = (PIMAGE_BASE_RELOCATION ) OffsetPointer ( relocation , relocation -> SizeOfBlock );
386
397
}
387
398
return TRUE;
388
399
}
@@ -861,7 +872,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry(
861
872
int cmp ;
862
873
PIMAGE_RESOURCE_DIR_STRING_U resourceString ;
863
874
middle = (start + end ) >> 1 ;
864
- resourceString = (PIMAGE_RESOURCE_DIR_STRING_U ) ((( char * ) root ) + ( entries [middle ].Name & 0x7FFFFFFF ) );
875
+ resourceString = (PIMAGE_RESOURCE_DIR_STRING_U ) OffsetPointer ( root , entries [middle ].Name & 0x7FFFFFFF );
865
876
cmp = _wcsnicmp (searchKey , resourceString -> NameString , resourceString -> Length );
866
877
if (cmp == 0 ) {
867
878
// Handle partial match
@@ -993,7 +1004,7 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO
993
1004
data = (PIMAGE_RESOURCE_DIR_STRING_U ) MemoryLoadResource (module , resource );
994
1005
id = id & 0x0f ;
995
1006
while (id -- ) {
996
- data = (PIMAGE_RESOURCE_DIR_STRING_U ) ((( char * ) data ) + (data -> Length + 1 ) * sizeof (WCHAR ));
1007
+ data = (PIMAGE_RESOURCE_DIR_STRING_U ) OffsetPointer ( data , (data -> Length + 1 ) * sizeof (WCHAR ));
997
1008
}
998
1009
if (data -> Length == 0 ) {
999
1010
SetLastError (ERROR_RESOURCE_NAME_NOT_FOUND );
0 commit comments