6 extern "C" char * cplus_demangle(const char *, int);
7 #define DMGL_PARAMS (1 << 0)
15 static bool FindVFunc(void *handle, void **vtable, const char* vtable_end, const char *class_function, const char *params, VOffset *offsets);
17 int main(int argc, char **argv)
28 fprintf(stderr, "Usage: <binary> <vtable start symbol> <function> <optional: params> <optional: vtable end symbol>\n");
32 handle = dlopen(argv[1], RTLD_NOW);
36 fprintf(stderr, "Failed to open server image, error [%s]\n", dlerror());
40 pVtable = (void **)dlsym(handle, argv[2]);
44 fprintf(stderr, "Invalid vtable symbol \"%s\"\n", argv[2]);
58 int len = strlen(params)-1;
60 if (params[len] == '"')
73 ret = FindVFunc(handle, pVtable, argv[5], argv[3], params, &offsets);
77 ret = FindVFunc(handle, pVtable, "_ZTI", argv[3], params, &offsets);
84 printf("%s%s - Win: %i Linux : %i\n", argv[3], params, offsets.windows_offset, offsets.linux_offset);
88 printf("%s - Win: %i Linux : %i\n", argv[3], offsets.windows_offset, offsets.linux_offset);
95 fprintf(stderr, "Failed to find function!");
100 static bool FindVFunc(void *handle, void **vtable, const char* vtable_end, const char *class_function, const char *params, VOffset *offsets)
104 int linux_offset = -1;
105 int windows_offset = -1;
109 int start_offset = -1;
111 for (int i=0; i< 1000; i++)
113 void *FuncPtr = vtable[i];
115 int status = dladdr(FuncPtr, &d);
122 if (i > 1 && strncmp(d.dli_sname, vtable_end, strlen(vtable_end)) == 0)
127 char *name = cplus_demangle(d.dli_sname, DMGL_PARAMS);
131 printf("Demangling failed\n");
135 char *foundfunction = strpbrk(name, ":");
137 if (foundfunction == NULL)
139 //printf("Couldnt split function\n");
144 //Skip both ':' chars
148 if (strncmp(foundfunction, class_function, strlen(class_function)) == 0 && foundfunction[strlen(class_function)] == '(')
150 //printf("Symbol: %s Demangled: %s Offset: %i\n", d.dli_sname, name, i);
152 //We have a pointer to a function, but it may be overloaded.
155 if (start_offset == -1)
160 char *foundparams = strpbrk(foundfunction, "(");
162 if (foundparams == NULL)
169 if(params == NULL || strcmp(foundparams, params) == 0)
171 //This is actually our function - So this is the linux offset
173 location = overloads;
180 windows_offset = (overloads-location) + start_offset;
182 offsets->linux_offset = linux_offset - 2;
183 offsets->windows_offset = windows_offset - 2;