Skip to content

Instantly share code, notes, and snippets.

@aragaer
Created July 8, 2011 15:22
Show Gist options
  • Save aragaer/1072069 to your computer and use it in GitHub Desktop.
Save aragaer/1072069 to your computer and use it in GitHub Desktop.
#define HOOK_CALL(lib, name, size) \
static void (*_ ## name ## _hook)(void); \
void name (void) { \
void *args = __builtin_apply_args(); \
DBG("Calling %s", #name); \
if (!_ ## name ## _hook) { \
void *orig_p, *orig; \
void *hook = get_lib_hook(lib); \
orig = dlsym(RTLD_NEXT, #name); \
if (!orig) { \
void *orig_lib = dlopen(lib_hook_list[lib].filename, RTLD_LAZY | RTLD_GLOBAL);\
if (!orig_lib) { \
fprintf(stderr, "Can't open library %s: %s\n", lib_hook_list[lib].filename, dlerror());\
exit(-1); \
} \
orig = dlsym(orig_lib, #name); \
if (!orig) { \
fprintf(stderr, "Symbol not found "#name":%s\n", dlerror());\
exit(-1); \
} \
} \
if (hook) { \
orig_p = dlsym(hook, "_" #name); \
if (orig_p) { \
*(void **) orig_p = orig; \
_ ## name ## _hook = dlsym(hook, #name);\
} \
} \
if (!_ ## name ## _hook) { \
DBG("Can't hook %s, using original", #name); \
_ ## name ## _hook = orig; \
} \
} \
__builtin_return(__builtin_apply(_ ## name ## _hook, args, size));\
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment