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?

  1. 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.

  2. 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, such mystrcpy. object module produced linked second library , standard library. second library contains routines names, such mystrcpy, call original library names strcpy. details doing of course dependent on linker. goal have chain this: original code calls strcpy. resolved version of strcpy in first library. version calls mystrcpy. mystrcpy calls standard library strcpy.

  3. you can compile assembly , edit names in assembly routines called instead of standard library routines.

  4. on systems, can use dlsym , other functions defined in <dlfcn.h> load dynamic library contains standard implementations , call them via pointers returned dlsym instead of usual names in source code.

  5. the gcc linker has --wrap switch resolves calls foo routine __wrap_foo , resolves calls __real_foo (which use in implementation) real foo.

see intercepting arbitrary functions on windows, unix, , macintosh os x platforms.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -