Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Wrappers for shared library functions?
@ 1997-03-05  5:00 john hall
  1997-03-06  5:00 ` john hall
  1997-03-11  5:00 ` Craig Neth USG
  0 siblings, 2 replies; 5+ messages in thread
From: john hall @ 1997-03-05  5:00 UTC (permalink / raw)



I would be very grateful for any advice on how to create "wrappers" for
one or two functions in large shared libraries, such that a calling 
program will invoke the wrapper function which in turn calls the original
function to do the majority of the "work". 

The replumbing should ideally be entirely transparent both to the 
calling program and to the shared library functions at run time.

The wrapper could be used for monitoring usage and possibly "tweaking"
the parameters supplied by the calling program or the results returned
by a library function.

It seems possible that by creating a library of wrappers dependent
on the "real" shared library and with a bit of renaming that the 
wrappers could be interposed between the calling program and the "real"
functions.

However I am unclear on how to provide the calling program
with access to all the "real" shared library functions without
having to write wrappers for all (maybe lots) of them - and then, 
what about other symbols exported from the "real" library such as 
external variables which may be referenced in a main program?

Thanks for any advice on this.

john hall
jhall@netcomuk.co.uk



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Wrappers for shared library functions?
  1997-03-05  5:00 Wrappers for shared library functions? john hall
@ 1997-03-06  5:00 ` john hall
  1997-03-06  5:00   ` Curt Smith
  1997-03-11  5:00 ` Craig Neth USG
  1 sibling, 1 reply; 5+ messages in thread
From: john hall @ 1997-03-06  5:00 UTC (permalink / raw)



I (john hall) wrote:
> 
> I would be very grateful for any advice on how to create "wrappers" for
> one or two functions in large shared libraries,

Apologies for adding to my post,.......

I should have stated that this is for OSF/1 /DU v2.x, v3.x and v4.x.

I do not have sources for either the calling applications or the
libraries (e.g. libm.so) - hence the need for "transparency".

I've tried renaming an existing library file and creating a replacement
dummy library which has the renamed real one as a dependency. However, 
the loader is unable to resolve symbols contained in the renamed real
library. It appears that the name of the shared library is also stored
in the library file itself. 

As a consequence, although the renamed shared library file (e.g. 
libRenamedReal.so) is found by ld when creating the "dummy" libReal.so,
the dependencies of the dummy library (revealed through odump -Dl)
include the name "libReal.so" instead of "libRenamedReal.so". 

Hence the loader appears never to try and load libRenamedReal.so 
when loading the dummy libReal.so, even though ld had to check against
symbols it contains when creating the dummy. Humph.

Archived newsgroup articles on this topic suggest that a library
preload technique can be used on Solaris, but I have not been able to
locate any equivalent on DU.

regards
john hall



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Wrappers for shared library functions?
  1997-03-06  5:00 ` john hall
@ 1997-03-06  5:00   ` Curt Smith
  0 siblings, 0 replies; 5+ messages in thread
From: Curt Smith @ 1997-03-06  5:00 UTC (permalink / raw)



john hall (jhall@netcomuk.co.uk) wrote:
: I (john hall) wrote:
: > 
: > I would be very grateful for any advice on how to create "wrappers" for
: > one or two functions in large shared libraries,
: 
: As a consequence, although the renamed shared library file (e.g. 
: libRenamedReal.so) is found by ld when creating the "dummy" libReal.so,
: the dependencies of the dummy library (revealed through odump -Dl)
: include the name "libReal.so" instead of "libRenamedReal.so". 

Sure enough... I just did:

cp /usr/shlib/libm.so /tmp
vi /tmp/libm.so

And there's libc.so in a list of other (guess) dependancies.  Given that
the start point of the "l" in libc.so must be left in place, and there's
strings immediately following, your ownly choice of hacking libm.so or
libxxx.so for that mater is to make the dependancy on libc.so to be 
libC.so.  Where you just change the characters of the dependant library.

Therefore do:

- Make your wrapper library named:	libm.so  (or what ever you're wrapping)
- Make the real library named:		libM.so
- Edit libM.so to make the dependancies
  on libm.so to be libM.so.

Good luck.


Curt Smith
curts@compgen.com




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Wrappers for shared library functions?
  1997-03-05  5:00 Wrappers for shared library functions? john hall
  1997-03-06  5:00 ` john hall
@ 1997-03-11  5:00 ` Craig Neth USG
  1997-03-13  5:00   ` john hall
  1 sibling, 1 reply; 5+ messages in thread
From: Craig Neth USG @ 1997-03-11  5:00 UTC (permalink / raw)
  To: john hall


john hall wrote:
> 
> I would be very grateful for any advice on how to create "wrappers" for
> one or two functions in large shared libraries, such that a calling
> program will invoke the wrapper function which in turn calls the original
> function to do the majority of the "work".

One way to do this would be to write the wrappers to use
dlopen()/dlsym()
to open the library that contains the symbol in question and then have
the
wrapper call the function.  

For example, suppose I wanted to provide a 'sqrt()' wrapper that printed
a message to stdout every time sqrt() was called:


hook.c:
-----------------------------------------------
#include <stdio.h>
#include <dlfcn.h>

double sqrt(double x)
{
  void *handle;
  double (*sqrt_sym)();
  double result;

  handle = dlopen("/usr/shlib/libm.so", RTLD_LAZY);
  sqrt_sym = dlsym(handle, "sqrt");
  printf ("calling sqrt\n");
  result = (*sqrt_sym) (x);
  dlclose(handle);
  return (result);
}

Build this function in a .so file, say hook.so:

 cc -shared -o hook.so hook.o  /* this works on V4.0 or greater. 
Pre-V4.0, use 'ld' */

Now link your program against hook.so:

 cc -o sqrt sqrt.c hook.so -lm

Now, at this point, you haven't hooked anything yet.   You need one
more thing:

 setenv _RLD_LIST  hook.so:DEFAULT

This tells the loader to always load your library before any others.
(If hook.so is not in your LD_LIBRARY_PATH you'll need to add that
before running
this example).

What happens is that because the hook library is loaded first, the calls
in sqrt.c to sqrt() will get resolved to the one in the hook library.    
The hooked sqrt() implementation will find the 'real' sqrt() function in
libm and
call it directly.

You need to be careful doing things like this.    Some functions in the
standard
libraries are implemented as a 'family' of routines that share static
data.   If
you preempt one of the routines and change it's implementation, but
don't also
preempt the other symbols in the family, then you could have all sorts
of nasty
problems. (malloc() is one example of such a 'family' of routines)  
Also, 
there are no guarantees that you will catch *all* calls to a given entry
point 
this way - for example, internal calls in a library may or may not call
the
function in such a way that you can preempt it.   

Craig

-- 
--------------------------------------------------------------------------------
Craig Neth                         ! neth@zk3.dec.com
DIGITAL Equipment Corporation      ! 
110 Spit Brook Road ZKO3-3/W17     !
Nashua, NH 03062                   ! Opinions expressed herein are my
own.
================================================================================



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Wrappers for shared library functions?
  1997-03-11  5:00 ` Craig Neth USG
@ 1997-03-13  5:00   ` john hall
  0 siblings, 0 replies; 5+ messages in thread
From: john hall @ 1997-03-13  5:00 UTC (permalink / raw)



Craig Neth USG wrote:
> 
> .................
> Now, at this point, you haven't hooked anything yet.   You need one
> more thing:
> 
>  setenv _RLD_LIST  hook.so:DEFAULT
> ....................

Thanks Craig. Using this technique everything works as hoped for,
..... except my __ini_xxx() functions in the wrapper are not called 
"automatically" when the library is loaded :-(

This isn't a disaster as I can revamp the wrapper library, but what
is the rationale behind this - is it a benefit, a bug or a "feature"? I
didn't get any hint of this behaviour from the man pages (ld or loader).

regards
john hall
jhall@netcomuk.co.uk




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1997-03-13  5:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-03-05  5:00 Wrappers for shared library functions? john hall
1997-03-06  5:00 ` john hall
1997-03-06  5:00   ` Curt Smith
1997-03-11  5:00 ` Craig Neth USG
1997-03-13  5:00   ` john hall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox