RTLD_NEXT on cygwin environment

Recently my friend made proxy for me so I can open some restricted websites at office, unfortunately I can’t find free proxy chaining tool for windows. So I decided to port tool from linux, this tool (not surprisingly) called proxychains.

However I got another problem as we expected :p , this problem came from RTLD_NEXT, this is one of two dlsym() handler. So what is dlsym and RTLD_NEXT? Have you ever asked “How to override malloc or fprintf”, with dlsym() with RTLD_NEXT you can create wrapper or override function from standard lib. dlsym() is function to load symbol from external library and RTLD_NEXT allows you to search symbol in your app first instead from standard library.

(Big) But RTLD_NEXT is not POSIX but GCC extension, you need to define _GNU_SOURCE in your code, (Another Big) But like another GCC Extension this one also problematic.

You can not use it in CYGWIN environment, this extension not available on CYGWIN

BTW

If you wanna know how to use dlsym() with RTLD_NEXT, I found it in stack overflow :

#define _GNU_SOURCE
#include<dlfcn.h>
#include<stdio.h>
void* malloc(size_t sz){
 void*(*libc_malloc)(size_t)= dlsym(RTLD_NEXT,"malloc");
 printf("malloc\n");
 return libc_malloc(sz);
}
void free(void*p){
 void(*libc_free)(void*)= dlsym(RTLD_NEXT,"free");
 printf("free\n");
 libc_free(p);
}
int main(){
 free(malloc(10));
 return 0;
}

Update:

I just remembered about wrap option in ld  🙂 :

--wrap= symbol

         Rename undefined references to symbol in order to  allow
         wrapper code to be linked into the output object without
         having to modify source code.
all  undefined  references  to  symbol  are  modified to
         reference   __wrap_symbol,   and   all   references   to
         __real_symbol are modified to reference symbol. The user
         is  expected  to  provide  an  object   containing   the
         __wrap_symbol  function.  This wrapper function can call
         __real_symbol in order to reference the actual  function
         being wrapped.
void * __wrap_malloc(size_t c)
           {
                   (void) printf("malloc called with %zu0, c);
                   return (__real_malloc(c));
           }

4 thoughts on “RTLD_NEXT on cygwin environment

Leave a comment