Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-28-2010, 11:33 AM   #1
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
LD_PRELOAD not working


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

#ifndef _GNU_SOURCE
#define _GNU_SOURCE

#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:
$ gcc -shared -o printhack.o -ldl
I'm trying it out with:
#include <stdio.h>

int main(void){
  printf("hello world\n");
  return 0;
I'm trying to run it with:
$ LD_PRELOAD=$PWD/ ./test
However, the program still calls the original version. Anyone know what the problem could be? Thanks!
Old 07-28-2010, 02:13 PM   #2
Sergei Steshenko
Senior Member
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Set LD_DEBUG to 'all' and see the output.
Old 07-28-2010, 02:48 PM   #3
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

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

You'll need to overload puts as well.
Old 07-28-2010, 06:09 PM   #4
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Differences between LD_LIBRARY_PATH and LD_PRELOAD Nerox Linux - General 1 05-12-2012 07:51 PM
Declaring LD_PRELOAD globally across system wide mzeeshanbhatti Linux - Software 8 08-03-2009 12:04 AM
Should we trust on LD_PRELOAD (library preload) ? barunparichha Programming 2 07-13-2006 09:42 PM
segmentation fault while defining LD_PRELOAD javeree Linux - Software 0 01-10-2005 03:48 PM
Differences between LD_LIBRARY_PATH and LD_PRELOAD Nerox Linux - Newbie 1 10-01-2004 08:03 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration