This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: How to override printf/scanf calls


Hi Eduardo,
	Thanks for the immediate reply, I have looked at dlsym and used it
to overload other functions, I particularly am facing issues when trying to
overload printf. My sample code is as follows:

printf.c:
#include <dlfcn.h>
#include <unistd.h>

static int (*libc_printf) (const char *fmt, ...);

int
printf (const char *fmt, ...)
{
        libc_printf = dlsym (RTLD_NEXT, "printf");
        if (libc_printf == NULL) {
                write (2, "overload failed.\n", 17);
                return -1;
        }

        write (2, "overloaded printf.\n", 19);

        return libc_printf (fmt);
}

This compile as follows:
gcc -Wall -Wextra -fPIC -D_GNU_SOURCE -shared -o libprintf.so printf.c -ldl

and I then execute my application as

LD_PRELOAD=libprintf.so ./test

The error I get is:
ERROR: ld.so: object 'libprintf.so' from LD_PRELOAD cannot be preloaded:
ignored.

I am not sure what I am doing wrong here.

Regards,

Bharath

From: Eduardo [mailto:erocha.ssa@gmail.com] 
Sent: Thursday, October 08, 2009 12:36 AM
To: Bharath Ramesh
Cc: libc-help@sourceware.org
Subject: Re: How to override printf/scanf calls

Have a look at dlsym (see man page). Particularly, look for option
RTLD_NEXT.
Eduardo
On Oct 7, 2009 11:16 PM, "Bharath Ramesh" <bramesh@vt.edu> wrote:

I know that I can override most system calls in libc writing the function
with the right signature and then creating a shard library out of it and use
the LD_PRELOAD mechanism when I run any application. This method doesn't not
work for libc calls like printf/scanf. Writing a shared library with any of
this calls has issues as ld cant preload this shared library. The only other
option I have at the moment is to download glibc source and rebuild glibc
with appropriate changes. This makes the solution not portable, I was
wondering if there was any other way by which I could override libc calls
like printf/scanf?

Regards,

Bharath

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]