Say I have libA.so with a GCC constructor.
My program "program" depends on libA.so, so when I run it, libA.so opens and its constructor runs. Now I also have the libC.so module, which also depends on libA. I run dlopen("libC.so") , which loads libC, and according to my experiments, also executes the libA constructor .
Dependencies are as follows:
- libA has a constructor
- libB also has a constructor
- libC depends on libA and libB
- depends on libA
- libC program links via dlopen ()
Now when I run the program:
- The libA constructor starts before main () starts.
- The libB constructor is started by dlopen ()
Dlopen seems to execute library constructors when they are loaded into memory. This is indicated somewhere, and how does the linker check which libraries are already loaded?
(Why I ask: at some point I got a constructor executed twice in a situation in some not quite clear conditions. Do I correctly believe that this was completely broken and should not happen in a normal situation?)
source share