views:

86

answers:

2

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.

+1  A: 

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!

jldupont
+1  A: 

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:

  1. Load the libraries yourself.
  2. Or remove the dependencies between the library load scripts.
  3. Make the libraries contain the dependencies themselves (might work, might not) That is when you get to the point of linking each shared library, make sure it includes -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.
Douglas Leeder
How do I "make the libraries contain the dependencies themselves" ?
Jack
@Jack - edited my answer - basically when linking the library make sure all dependent libs are linked in.
Douglas Leeder