.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   How to detect dynamic library load/unload? (http://forums.bots-united.com/showthread.php?t=2807)

botmeister 18-10-2004 22:21

How to detect dynamic library load/unload?
 
I want to get my linux loadable library to run a function whenever it is loaded and unloaded.

I know how to to this in windows using the entry/exit function

BOOL WINAPI DllMain
( HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)

Under linux g++ I read that I should use these function macros

__init and __exit

I created two functions for my library based on examples I saw,

static void __init StartUp(void)
static void __exit ShutDown(void)

But these functions do not get called when dlopen is called or when the libary is unloaded.

Can anyone help me with this?

Pierre-Marie Baty 18-10-2004 23:02

Re: How to detect dynamic library load/unload?
 
Where did you read that ? I thought it was

Code:

static void init (void)
and

Code:

static void fini (void)
??

BTW. all of you can use the code tags now, they work =)

botmeister 19-10-2004 09:29

Re: How to detect dynamic library load/unload?
 
I read about the __init and __exit macros from some website. You have to include <linux/init.h> for it to work, or so goes the story.

I tried init and fini as suggested but no luck.

Is there a compiler/linker option for this?

botman 19-10-2004 14:49

Re: How to detect dynamic library load/unload?
 
Did you try "man dlopen"???

botman

botmeister 19-10-2004 19:47

Re: How to detect dynamic library load/unload?
 
No, but :RTFM:was the obvious thing to do :| thanks for the suggestion.

FYI I found this, looks like init and fini are now obsolete and the last para is what I'll have to dig into:

The obsolete symbols _init and _fini
The linker recognizes special symbols _init and _fini. If a dynamic
library exports a routine named _init, then that code is executed after
the loading, before dlopen() returns. If the dynamic library exports a
routine named _fini, then that routine is called just before the
library is unloaded. In case you need to avoid linking against the
system startup files, this can be done by giving gcc the "-nostart-
files" parameter on the command line.

Using these routines, or the gcc -nostartupfiles or -nostdlib options,
is not recommended. Their use may result in undesired behavior, since
the constructor/destructor routines will not be executed (unless spe-
cial measures are taken).

Instead, libraries should export routines using the __attribute__((con-
structor)) and __attribute__((destructor)) function attributes. See
the gcc info pages for information on these. Constructor routines are
executed before dlopen returns, and destructor routines are executed
before dlclose returns.

Pierre-Marie Baty 19-10-2004 20:23

Re: How to detect dynamic library load/unload?
 
Google told me that

The C prototypes for these functions are:
Code:

  void __attribute__ ((constructor)) my_init (void);

  void __attribute__ ((destructor)) my_fini (void);

put the function names you want, but type the attributes verbatim. Tell me if it works because I'm interested in that issue too :)

Whistler 23-10-2004 07:53

Re: How to detect dynamic library load/unload?
 
why not use the constructor/destructor in a global class ?

botmeister 24-10-2004 10:37

Re: How to detect dynamic library load/unload?
 
why not use the constructor/destructor in a global class ?

I suppose that would work in some cases. It is the order of events that can be important.

botmeister 24-10-2004 10:41

Re: How to detect dynamic library load/unload?
 
Tell me if it works because I'm interested in that issue too

It looks like the functions are being called, but my code is crashing. It's late, so I don't have time to check what's going wrong, but it looks like I'm calling things that are not yet initialized which is causing the crash.

The order in which things happen are very important, and I'll have to figure this all out when I get the time.


All times are GMT +2. The time now is 02:50.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.