Created
January 6, 2015 03:30
-
-
Save byteandahalf/7b4203c65a262c596a6d to your computer and use it in GitHub Desktop.
Dynamically hook vtable functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <dlfcn.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stddef.h> | |
uintptr_t DynamicHookVirtual(void** vtable, char* mangled, int end, void* myfunc) { | |
int index = -1; | |
// TODO: Find the size of the vtable automagically? | |
for(int i = 0; i < end; i++) { | |
void* currentfunc = vtable[i]; | |
Dl_info info; | |
int status = dladdr(currentfunc, &info); | |
const char* sym = info.dli_sname; | |
if(!strcmp(sym, mangled)) { | |
index = i; | |
break; | |
} | |
} | |
if(index == -1) return 0x00000000; | |
uintptr_t real = (uintptr_t) vtable[index]; | |
vtable[index] = myfunc; | |
return real; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would just keep reading endlessly.