1.1 --- a/tools/gdc-psyfork/MemoryUtils.cpp Tue Jul 03 07:51:12 2012 -0400
1.2 +++ b/tools/gdc-psyfork/MemoryUtils.cpp Tue Jul 03 10:27:27 2012 -0400
1.3 @@ -67,20 +67,27 @@
1.4 #endif
1.5 }
1.6
1.7 -void *MemoryUtils::FindPatternInFile(int fd, const char *pattern, size_t len)
1.8 +void *MemoryUtils::FindPatternInFile(int fd, const char *pattern, size_t len, int &matches, bool &atFuncStart)
1.9 {
1.10 size_t size;
1.11 size = lseek (fd , 0 , SEEK_END);
1.12 +
1.13 + matches = 0;
1.14 + // TODO: fix this
1.15 + atFuncStart = true;
1.16
1.17 lseek(fd, 0, SEEK_SET);
1.18
1.19 void *map;
1.20 - char *ptr, *end;
1.21 + char *ptr, *start, *end;
1.22 bool found = true;
1.23 map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
1.24
1.25 ptr = (char *)map;
1.26 + start = ptr;
1.27 end = ptr + size;
1.28 +
1.29 + void *firstMatch = NULL;
1.30
1.31 while (ptr < end)
1.32 {
1.33 @@ -95,19 +102,49 @@
1.34 }
1.35
1.36 if (found)
1.37 - return ptr;
1.38 + {
1.39 + if( !firstMatch )
1.40 + {
1.41 + firstMatch = ptr;
1.42 + if( ptr - start >= 1 )
1.43 + {
1.44 + unsigned char oneBack = *(unsigned char*)((intptr_t)ptr - 1);
1.45 + if( oneBack != 0xC3 && oneBack != 0xCB && oneBack != 0xCC )
1.46 + {
1.47 + if( ptr - start >= 3 )
1.48 + {
1.49 + unsigned char threeBack = *(unsigned char*)((intptr_t)ptr - 3);
1.50 + if( threeBack != 0xCA && threeBack != 0xC2 )
1.51 + {
1.52 + atFuncStart = false;
1.53 + }
1.54 + }
1.55 + else
1.56 + {
1.57 + atFuncStart = false;
1.58 + }
1.59 + }
1.60 + }
1.61 + }
1.62 +
1.63 + ++matches;
1.64 + }
1.65
1.66 - ptr++;
1.67 + ++ptr;
1.68 }
1.69
1.70 - return NULL;
1.71 + return firstMatch;
1.72 }
1.73
1.74 -void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
1.75 +void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len, int &matches, bool &atFuncStart)
1.76 {
1.77 DynLibInfo lib;
1.78 bool found;
1.79 char *ptr, *end;
1.80 +
1.81 + matches = 0;
1.82 + // TODO: fix this
1.83 + atFuncStart = true;
1.84
1.85 memset(&lib, 0, sizeof(DynLibInfo));
1.86
1.87 @@ -115,7 +152,8 @@
1.88 {
1.89 return NULL;
1.90 }
1.91 -
1.92 +
1.93 + void *firstMatch = NULL;
1.94 ptr = reinterpret_cast<char *>(lib.baseAddress);
1.95 end = ptr + lib.memorySize - 1;
1.96
1.97 @@ -132,12 +170,17 @@
1.98 }
1.99
1.100 if (found)
1.101 - return ptr;
1.102 + {
1.103 + if( !firstMatch )
1.104 + firstMatch = ptr;
1.105 +
1.106 + ++matches;
1.107 + }
1.108
1.109 - ptr++;
1.110 + ++ptr;
1.111 }
1.112
1.113 - return NULL;
1.114 + return firstMatch;
1.115 }
1.116
1.117 void *MemoryUtils::ResolveSymbol(void *handle, const char *symbol)