redefinition - How to Bypass a Standard C++ Function While Maintaining Its Functionality -
i looking way able redefine set of posix functions end redefinition call original function. idea trying create layer can restrict os api's can called depending on "profile" active. "profile" determines set of functions allowed , not specified should not used.
for example, if in 1 profile not allowed use strcpy, able either cause compile time error (via static_assert) or print screen saying "strcpy not allowed in profile" such below:
my_string.h
#include <string.h> char *strcpy(char *restrict s1, const char *restrict s2) { #if defined(profile_pass_through) printf("strcpy not allowed in profile\n"); return strcpy(s1, s2); #elif defined(profile_error) static_assesrt(0, "strcpy not allowed in profile\n"); return 0; #else return strcpy(s1, s2); #endif }
so way within main.cpp can use my_string.h
#define profile_pass_through #include "my_string.h" int main() { char temp1[10]; char temp2[10]; sprintf(temp2, "testing"); if (0 = strcpy(temp1, temp2)) { printf("temp1 %s\n", temp1); } return 0; }
now realize code have written above not compile due redefinition of strcpy, there way allow sort of functionality without playing around macros or creating own standard c , c++ libraries?
you can write preprocessor changes calls standard routine calls own routine. such preprocessor might complicated, depending whether need recognize full c++ grammar distinguish calls using name spaces , on or can away more casual recognition of calls.
you can link own library, producing relocatable object module resolved names stripped. library contain routines standard names, such
strcpy
, execute whatever code desire , call other names, suchmystrcpy
. object module produced linked second library , standard library. second library contains routines names, suchmystrcpy
, call original library namesstrcpy
. details doing of course dependent on linker. goal have chain this: original code callsstrcpy
. resolved version ofstrcpy
in first library. version callsmystrcpy
.mystrcpy
calls standard librarystrcpy
.you can compile assembly , edit names in assembly routines called instead of standard library routines.
on systems, can use
dlsym
, other functions defined in<dlfcn.h>
load dynamic library contains standard implementations , call them via pointers returneddlsym
instead of usual names in source code.the gcc linker has
--wrap
switch resolves callsfoo
routine__wrap_foo
, resolves calls__real_foo
(which use in implementation) realfoo
.
see intercepting arbitrary functions on windows, unix, , macintosh os x platforms.
Comments
Post a Comment