开发者

How to load a shared library without loading its dependencies?

Say I have a library libfoo.so.1, which depends (according to ldd) on libbar.so.1. However, libbar.so.1 is not available at the moment. My app needs to call a function in libfoo.so.1 which doesn't require libbar.so.1 at all.

Is there a way to load libfoo.so.1, resolve the function symbol and then call it without having libbar.so.1 to satisfy the dependency? It's a case of "I know what I'm doing, just let me do it already". I tried the RTLD_LAZY flag, but it still tries to load the libbar.so.1 library before not loading the symbols.


EDIT

Here's the exact situation.

We have 3 players:

  • libbar.so.1, a shared library located in a path not in LD_LIBRARY_PATH or ldconfig, and whose dependencies are all resolved
  • libfoo.so.1, a shared library located in a different directory than libbar, but which depends on libbar. At runtime, libfoo will know where to locate libbar.
  • App, a binary application which needs to load libfoo at some point during runtime.

App doesn't know where to find libbar, but knows that libfoo knows. What I'm trying to accomplish is having an init function in libfoo which would simply change App's current working directory to where libbar is located to finally resolve all the dependencies and make everyone happy.开发者_如何学C

libfoo will eventually need to call stuff in libbar, just not in this init function. I don't think creating a stub would work since the symbols would eventually need to resolve to the real functions.


Well, variables are still resolved even with RTLD_LAZY, so in general you do need all the libraries to be linked. Seems like you should create a stub libbar.so.1 that has no functionality and can be found by the linker.


Just a thought, have you thought of interpositioning dependency - simply create a identical function with the same signature, parameters etc and let the linker resolve this function and ignore libbar.so.1? Since you did not mention this, I thought I'd suggest this.

Hope this helps, Best regards, Tom.


Another thought: Would extracting (use ar(1)) the necessary function(s) from libfoo.so.1, either into a .o or into another .so file, and then linking against that extract help? I'm assuming the reference to libbar.so.1 is in a libfoo function that is not called (even indirectly) from your program.


What's the actual requirement here? Merely linking a library doesn't do much, and is usually benign. Do you lack the library? Just create a stub library of the same name. You want to control or preempt the use of symbols in the library? Put them in another library (with the right version tags!) and LD_PRELOAD it.

I guess the meta-question here is that I don't see what value being able to preempt the dependency linkage has. It's just a helper function.


Use dlopen to load the library and dlsym to get the function you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜