LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2009, 02:10 AM   #1
philfine
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Rep: Reputation: 0
dlopen, different handler for same loads


Hello everyone,

I am looking into dynamic loading using dlopen to actually load the same .so file but having different data section (globals & static vars) regions for each of the loads.

From what I can understand from dlopen manpage:
"If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it. The _init routine, if present, is only called once. But a subsequent call with RTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY."

How can I avoid this behaviour of dlopen. Is there any other function such as dlopen that is not as restrictive ?
Will I have to implement my own dlopen if I want to achieve that ?

Please comment.
Thanks in advance for you help
 
Old 08-27-2009, 08:42 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Quote:
Originally Posted by philfine View Post
How can I avoid this behaviour of dlopen. Is there any other function such as dlopen that is not as restrictive ?
This is indeed the normal (and only) behaviour of dynamic loading. I can't recall any other library offering such a functionality.

Quote:
Originally Posted by philfine View Post
Will I have to implement my own dlopen if I want to achieve that ?
You could work around it by e.g. programmatically making individual uniquely renamed copies of the original *.so and linking those dynamically. This way each instance will keep its own static data.

Last edited by fantas; 08-27-2009 at 09:10 AM. Reason: typo
 
Old 08-29-2009, 06:25 AM   #3
philfine
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Hi fantas and thanks for the reply.

Quote:
Originally Posted by fantas View Post
This is indeed the normal (and only) behaviour of dynamic loading. I can't recall any other library offering such a functionality.

You could work around it by e.g. programmatically making individual uniquely renamed copies of the original *.so and linking those dynamically. This way each instance will keep its own static data.
Sure, although not a very efficient implementation, and in my case, out of question.
Do you think it would be possible using dlinfo to access internal structures that dlopen uses and just change the name of the .so (in those structures) such that in the next load it actually reloads again ?

Once again, thanks a lot.
 
Old 08-29-2009, 03:29 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I ran into this problem a few years ago when developing a plug-in system for an application of mine. Essentially the application would load the specified plug-ins (with dlopen) and pass them parameters specified in a configuration file (via a dlsymmed function.) For example, one might load a plug-in to filter IP addresses of incoming socket connections, and it's perfectly reasonable to want to load it more than once using different parameters. The problem I had was that the static data contained in the library was exactly the same no matter how many times it was loaded; there was always only one instance associated with the process that loaded it.

I'm not sure if you have an initialization function you manually call (e.g. to pass parameters,) but if you do, consider having the process that loads it pass a unique reference for each load instance. For example, the solution to my problem was to add an argument to the initialization function (e.g. load_plugin(int instance, const char *options)) and reference that in subsequent calls to functions within the library (e.g. check_address(int instance, uint32_t address).) This, of course, requires that you have control over the library's API. Internally, the libraries use a hash table to locate the context data for the specific load instance. Even though the library is loaded only once, enumerating the load instances allows you to "fake" multiple instances internally.

Loading a library only once is one of the most useful things about shared libraries. In fact, on many systems the library is only loaded once system-wide; only the data is process-unique. Imagine if your program linked to 5 libraries that were linked to libm; you wouldn't want libm loaded 5 times when your program runs.
Kevin Barry

PS The functions shown in red would be a part of the library's API.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SIGSEGV handler (segmentation fauld handler) myp Programming 8 03-08-2011 02:17 PM
dlopen calls mathimca05 Linux - Newbie 1 02-05-2008 06:33 AM
dlopen() does not loads the dependent libraries linuxravin Programming 9 07-25-2006 06:52 AM
<0>Kernel panic: Aiee, killing interrupt handler! In interrupt handler - not syncing mrb Linux - Newbie 2 01-09-2005 09:47 AM
installed dropline, root loads kde3.2, user loads drop pgrimes Linux - Software 7 06-28-2004 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration