Library Wrapper

This is a discussion on Library Wrapper within the C Programming forums, part of the General Programming Boards category; I am not sure if it is even possible to do what I want to in C, but I thought ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Library Wrapper

    I am not sure if it is even possible to do what I want to in C, but I thought I'd ask just in case.

    Basically I want to modify the behavior of a library function without having to rename all calls to the function or directly edit the library.

    Say there is a library that contains a function: void foo(). I would like to somehow write a function with the same interface, void foo(), which does something and then somehow calls the original foo function from the library.

    For anyone familiar with CLOS, I want the equivalent of an :around method.

    I was thinking there might be a way for me to write my own library that links to the original library, but somehow renames the original foo() function so it won't collide with mine.

    Does anyone know if something like this is possible in C?

    Thanks,
    James

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    This is possible on some systems, under certain conditions. Platform details?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    I'm working on an old-ish sparc machine running Solaris 5.9 (though I can also use a linux machine if that would work better)

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cusavior View Post
    I'm working on an old-ish sparc machine running Solaris 5.9 (though I can also use a linux machine if that would work better)
    On Solaris you can use the LD_PRELOAD environment variable to hook a shared object during the dynamic linking. This object can override any symbols in any other shared libraries referenced by that program.

    The problem is calling the original function. If you were trying to override malloc() for instance, you need some way of calling the ACTUAL malloc(), but you can't do it through the name malloc() because you've overridden it, so you'd just be calling yourself.

    Most standard C libraries expose "internal" names for some of their standard functions specifically to allow this kind of thing. Poking around on my SunOS 5.7 box, I see this:

    Code:
    [6]     |         0|       4|OBJT |GLOB |0    |3      |_libc_malloc
    So presumably, if you wanted to hook malloc(), you'd write a function called malloc(), do your thing, and eventually call the real _libc_malloc(). You'd compile this module as a .so file, then list it in the LD_PRELOAD variable. Any program you run will now be hooked to your wrapper function.

    If you are trying to hook a function in a library which doesn't expose an internal name, it is going to be more difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21