I have a gcc-compiled application linked against dynamic libraries. Is there a way to impose the order in which libraries are loaded? (In my case one library constructor uses resources set up by other library constructor).
Thanks.
I have a gcc-compiled application linked against dynamic libraries. Is there a way to impose the order in which libraries are loaded? (In my case one library constructor uses resources set up by other library constructor).
Thanks.
You can use dlopen
and load the libraries yourself: this way, you can have a finer grain control over the loading/unloading process. See here.
Of course, this isn't a "gcc" based solution and it requires reworking your application... Maybe you could explain the "problem" you are facing in a bit more details?
You can disregard my solution if it doesn't fit your needs. Cheers!
gcc
isn't in-charge of loading the libraries, either ld.so
does it automatically when your program loads, or you do it manually as @jldupont suggests.
And ld.so
might deliberately randomise the order to prevent return-to-stdlib attacks.
So either:
-l<dependentlib>
in the link command. You can test this by creating a trival program that links only with that shared library - if it builds and runs, then the library contains all necessary dependent libs. This might help if ld.so loads the libraries in dependency order - which I think it has to do.