Created
September 17, 2014 18:48
-
-
Save phausler/91a262e0334609439003 to your computer and use it in GitHub Desktop.
portable function wrappers
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
#if defined(__APPLE__) // apple ld specific | |
// wrapped ala -Wl,-alias,___wrap_printf,_printf | |
#include <dlfcn.h> | |
#define WRAPFN(f) __wrap_##f | |
#define WRAPFN_DECL(f) WRAPFN(f) | |
#define REALFN(f) __real_##f | |
#define REALFN_DECL(f) (*REALFN(f)) | |
#define REALFN_DEF(f) typeof(&WRAPFN(f)) REALFN(f) = NULL | |
#define REALFN_CALL(f) ({ \ | |
if (REALFN(f) == NULL) { \ | |
REALFN(f) = (typeof(&WRAPFN(f)))dlsym(RTLD_NEXT, #f); \ | |
} \ | |
REALFN(f); \ | |
}) | |
#elif defined(__linux__) || defined(__bsd__) // gnu ld specific | |
// wrapped ala -Wl,--wrap,printf | |
#define WRAPFN(f) __wrap_##f | |
#define WRAPFN_DECL(f) WRAPFN(f) | |
#define REALFN(f) __real_##f | |
#define REALFN_DECL(f) REALFN(f) | |
#define REALFN_DEF(f) | |
#define REALFN_CALL(f) REALFN(f) | |
#endif | |
/* | |
example usage | |
#include <stdio.h> | |
extern int WRAPFN_DECL(printf)(const char *__restrict fmt, ...); | |
extern int REALFN_DECL(printf)(const char *__restrict fmt, ...); | |
REALFN_DEF(printf); | |
int WRAPFN(printf)(const char * __restrict fmt, ...) | |
{ | |
return REALFN_CALL(printf)("test\n"); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment