Skip to content

Commit f5c9db8

Browse files
committed
Simplfy some casting code, updated comments about 32/64 bit.
1 parent 6c4ab35 commit f5c9db8

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

MemoryModule.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ AlignValueUp(size_t value, size_t alignment) {
102102
return (value + alignment - 1) & ~(alignment - 1);
103103
}
104104

105+
static inline void*
106+
OffsetPointer(void* data, ptrdiff_t offset) {
107+
return (void*) ((uintptr_t) data + offset);
108+
}
109+
105110
static inline void
106111
OutputLastError(const char *msg)
107112
{
@@ -153,9 +158,11 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade
153158
}
154159

155160
// Always use position from file to support alignments smaller
156-
// than page size.
161+
// than page size (allocation above will align to page size).
157162
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);
159166
memset(dest, 0, section_size);
160167
}
161168

@@ -178,10 +185,12 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade
178185
}
179186

180187
// Always use position from file to support alignments smaller
181-
// than page size.
188+
// than page size (allocation above will align to page size).
182189
dest = codeBase + section->VirtualAddress;
183190
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);
185194
}
186195

187196
return TRUE;
@@ -261,7 +270,9 @@ FinalizeSections(PMEMORYMODULE module)
261270
int i;
262271
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(module->headers);
263272
#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);
265276
#else
266277
static const uintptr_t imageOffset = 0;
267278
#endif
@@ -345,7 +356,7 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
345356
for (; relocation->VirtualAddress > 0; ) {
346357
DWORD i;
347358
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);
349360
for (i=0; i<((relocation->SizeOfBlock-IMAGE_SIZEOF_BASE_RELOCATION) / 2); i++, relInfo++) {
350361
// the upper 4 bits define the type of relocation
351362
int type = *relInfo >> 12;
@@ -382,7 +393,7 @@ PerformBaseRelocation(PMEMORYMODULE module, ptrdiff_t delta)
382393
}
383394

384395
// advance to next relocation block
385-
relocation = (PIMAGE_BASE_RELOCATION) (((char *) relocation) + relocation->SizeOfBlock);
396+
relocation = (PIMAGE_BASE_RELOCATION) OffsetPointer(relocation, relocation->SizeOfBlock);
386397
}
387398
return TRUE;
388399
}
@@ -861,7 +872,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry(
861872
int cmp;
862873
PIMAGE_RESOURCE_DIR_STRING_U resourceString;
863874
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);
865876
cmp = _wcsnicmp(searchKey, resourceString->NameString, resourceString->Length);
866877
if (cmp == 0) {
867878
// Handle partial match
@@ -993,7 +1004,7 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO
9931004
data = (PIMAGE_RESOURCE_DIR_STRING_U) MemoryLoadResource(module, resource);
9941005
id = id & 0x0f;
9951006
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));
9971008
}
9981009
if (data->Length == 0) {
9991010
SetLastError(ERROR_RESOURCE_NAME_NOT_FOUND);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy