From: kerry@yahoo.com (Kerry Kopp)
Subject: Re: Library Interposition on Tru64?
Date: 20 Aug 2001 12:17:44 -0700 [thread overview]
Message-ID: <72ee5672.0108201117.1a78e1b6@posting.google.com> (raw)
In-Reply-To: 72ee5672.0108151842.5cdac734@posting.google.com
kerry@yahoo.com (Kerry Kopp) wrote in message news:<72ee5672.0108151842.5cdac734@posting.google.com>...
> Dan Riley <dsr@mail.lns.cornell.edu> wrote in message news:<shd75xw8rh.fsf@lnxcu9.lns.cornell.edu>...
>
>
> > However, I don't see an obvious equivalent to RTLD_NEXT.
>
> Hmmmm, crud. I've seen some docs about iterating over the various
> symbols (on Solaris, of course), so I'll try and figure out if that's
> possible. (Although I'm hoping that someone out there knows how to do
> it... :) )
Allllrighty, I figured it out, sort of. It's not as auto-magic as
RTLD_NEXT, but hey, it works. :)
/*
* Example of a library interposer on DEC/Tru64: interpose on
malloc().
* (Tested on 4.0F, Rev 1229)
* Build and use this interposer as following:
* 1) compile the shared lib
* % cc -o malloc_interposer.so -shared malloc_interposer.c
* 2) tell the interposer the nameof the real library that has the
function
* that we are interposing. For malloc(), this is libc.so. (This is
done
* as an environment variable so that it can be changed at runtime)
* Note: you don't need a full path, as the loader will resolve it
for you.
* % setenv INTERPOSE_TARGET_LIB libc.so
* 3) Tell the loader to look in our library first, then the default
libs.
* (This is the equivalent to LD_PRELOAD on Solaris)
* % setenv _RLD_LIST $cwd/malloc_interposer.so:DEFAULT
* 4) Run the app as normal. See the pretty printouts.
* 5) Turn off the interposition if needed
* % unsetenv _RLD_LIST
*
* In theory, maybe we should dlclose the shared lib that we are
opening, but,
* well, this is an example, right? :)
* Created from Solaris interposition examples, some research, and a
little help
* from Dan Riley on Usenet, 8/16/2001, Kerry Kopp kerry@yahoo.com
*/
#include <stdio.h> /* For printf() */
#include <dlfcn.h> /* For dlopen(), dlsym() */
#include <stdlib.h> /* For getenv() */
/* The environment var that has the real library we want */
#define LIB_PATH_ENV_VAR "INTERPOSE_TARGET_LIB"
void *malloc(size_t size)
{
static void * (*func)();
char * libPath = NULL;
void * handle = NULL;
libPath = getenv(LIB_PATH_ENV_VAR);
if ( !libPath ) {
printf("LibPath is null, bailing\n");
return NULL;
}
handle = dlopen(libPath, RTLD_NOW);
if ( !handle ) {
printf("Handle is null, bailing.\n");
return NULL;
}
if(!func) {
func = (void *(*)()) dlsym(handle, "malloc");
}
printf("malloc(%d) was called\n", size);
return(func(size));
}
prev parent reply other threads:[~2001-08-20 19:17 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-15 8:29 Library Interposition on Tru64? Kerry Kopp
2001-08-15 17:08 ` Dan Riley
2001-08-16 2:42 ` Kerry Kopp
2001-08-20 19:17 ` Kerry Kopp [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox