LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   LD_PRELOAD not working (https://www.linuxquestions.org/questions/programming-9/ld_preload-not-working-822662/)

kamransoomro84 07-28-2010 11:33 AM

LD_PRELOAD not working
 
Hi,

I'm trying to create my own version of the printf function. I'm using the following code:

Code:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <dlfcn.h>
#include <stdio.h>

int printf(const char *format, ...){

    static int (*real_printf) (const char *format, ...) = NULL;

    if (!real_printf){
            real_printf = dlsym(RTLD_NEXT, "printf");
    }

    return real_printf("my printf\n");
}

I compiled it with:
Code:

$ gcc -shared -o libprinthack.so printhack.o -ldl
I'm trying it out with:
Code:


#include <stdio.h>

int main(void){
  printf("hello world\n");
  return 0;
}

I'm trying to run it with:
Code:

$ LD_PRELOAD=$PWD/libprinthack.so ./test
However, the program still calls the original version. Anyone know what the problem could be? Thanks!

Sergei Steshenko 07-28-2010 02:13 PM

Set LD_DEBUG to 'all' and see the output.

orgcandman 07-28-2010 02:48 PM

Yeah, gcc is being [un]helpful and changing your printf() to a puts().

You'll need to overload puts as well.

tuxdev 07-28-2010 06:09 PM

Or you could build the hosting app with -fno-builtin-printf, but that kind of defeats most of the point of LD_PRELOAD..


All times are GMT -5. The time now is 03:19 AM.