![]() |
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? |
Re: How to detect dynamic library load/unload?
Where did you read that ? I thought it was
Code:
static void init (void) Code:
static void fini (void) BTW. all of you can use the code tags now, they work =) |
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? |
Re: How to detect dynamic library load/unload?
Did you try "man dlopen"???
botman |
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. |
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); |
Re: How to detect dynamic library load/unload?
why not use the constructor/destructor in a global class ?
|
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. |
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 22:39. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.