13

Is there a way for a shared library to be "notified" when it is loaded?

In other words, let's say I use dlopen on a shared library, is there a function that is automatically called (if present) on the shared library (e.g. main?)

jldupont
  • 93,734
  • 56
  • 203
  • 318

3 Answers3

22

Libraries should export initialization and cleanup routines using the gcc __attribute__((constructor)) and __attribute__((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen returns (or before main() is started if the library is loaded at load time). Destructor routines are executed before dlclose returns (or after exit() or completion of main() if the library is loaded at load time). The C prototypes for these functions are:

 void __attribute__ ((constructor))  my_init(void);  
 void __attribute__  ((destructor)) my_fini(void);

Taken from http://tldp.org/HOWTO/Program-Library-HOWTO/index.html

THat is, you just tack on __attribute__ ((constructor)) to the functions you want to be called when the shared library is loaded. The above docuemtn also notes that the older _ini and _fini functions are considered obsolete.

nos
  • 223,662
  • 58
  • 417
  • 506
17

Yes. When a library is opened, all static construction takes place... so, if you use C++, you can do:

// mylibrary.cpp
namespace
{
    class dynamic_library_load_unload_handler
    {
         public:
              dynamic_library_load_unload_handler(){
                    // Code to execute when the library is loaded
              }
              ~dynamic_library_load_unload_handler(){
                    // Code to execute when the library is unloaded
              }
    } dynamic_library_load_unload_handler_hook;
}

Unlike the __attribute__ ((constructor)) solutions given, this will be portable. Note, though, that if you have multiple objects like this, there is no guarantee with respect to the construction/destruction order.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • Cool! Is there a way to get argc and argv in the ctor? Linux (ELF) specific solution: https://gcc.gnu.org/ml/gcc/2002-10/msg01296.html – TrueY Sep 01 '15 at 09:29
  • @TrueY, I'm not familiar with a portable solution, but let me know if you find one. That being said, from an API standpoint, I would suggest that libraries let the applications determine what commandline switches configure what... rather than attempt to read argc/argv... consider providing utility routines that can load configuration from a file and/or from the commandline that libraries can optionally use (but also provide direct methods for the application to configure the library's behavior). – Michael Aaron Safyan Sep 02 '15 at 03:51
  • Interesting. And in plain C you could only have a constructor this way (global variable initialized by a function call), since there's no notion of "static deinitialization". – Ruslan Jan 22 '16 at 06:26
0

At least on Linux, and probably on at least some other Unix systems, if the library is dynamically opened a global function named _init, if it exists, will be called by the dynamic linker.

Jack Lloyd
  • 8,215
  • 2
  • 37
  • 47
  • ... what is the prototype for this _init function? – jldupont Oct 21 '09 at 17:54
  • 3
    @Jack: just found out through http://tldp.org/HOWTO/Program-Library-HOWTO/miscellaneous.html#INIT-AND-CLEANUP that the _init and _fini special functions are marked as OBSOLETE/DANGEROUS... – jldupont Oct 21 '09 at 17:58