Multiple Loads of a shared library

I am trying to write a daemon that accesses non-reentrant libraries that are dynamically loaded.  The job of these libraries is to go fetch and process data from various sources, then return the homogenized results when done.  I'm multi-threading the application to hopefully speed up data requests by reducing wait time.  The problem I'm having is trying to load the same reader library multiple times in the same process.  When I use the dlopen on the library twice, it returns exactly the same handle the second time as the previous.  I've attempted to use mmap on the file, but then I can't call dlsym.  This makes sense, but that implies that I need to parse for various executable file formats (aout, coff, elf, etc.).  Getting the file format isn't necessarily a problem, but is there a nice library created that I can just pass the memory that I've mapped to get the function pointers I need from the library?  Or is there a more elegant solution?  One idea I've been given is to create a temporary copy of the file, but that just seems kludgy.  I'd really like a nice elegant solution.

Thanks.  I know this isn't your typical 2 banana problem, so I've given the most the thing would allow me to submit it as.
ChefInnocentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
beside I cannot follow your thought why a shared lib cannot be used twice or more, how about following:

 1. copy your shared lib to different locations, let's say directory a b c d ...
 2. write a shell wrapper like:
    #! /bin/sh
    LD_LIBRARY_PATH="/path/to/$1:${LD_LIBRARY_PATH}"
    export LD_LIBRARY_PATH
    /path/to/your-script
 3. call above wrapper like:
    wrapper a
    wrapper b
    wrapper c
    ...
0
BernhardBrueckCommented:
bad news first:
Simple mapping the library multiple time into memory won't work. Even if you get the entrance points of the functions inside the library you can't simple call them. The library itself may call functions outside (e.g. C-libs) which need to be resolved as well. When the libs are not compiled as reentrant
you  would even end with multible c-libs etc.

better solution:
use processes and communicate through files or shared mem

tmp = create new tmpfile
if fork() {                         // child ?
            dlopen('broken_lib');
            init_lib;
     use_library;
     write result to tmp;
     exit;                          // end child
}
watifor child                     // parent
read result from tmp

Hope that help,
     Bernhard Brueck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mglxxxCommented:
I agree with BernhardBrueck. His suggestion looks
much simpler than what you are trying to do and
will definitely work.
Even if you could simply 'parse' the library for
the entry points and jump to them, you'd also need
to find any static initialisers and call them
before using the library.
0
ChefInnocentAuthor Commented:
Although I have decided to attack the problem differently, it does appear that the answer BernhardBrueck gave does work.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.