replace a dynamic shared library in run time

点点圈 提交于 2019-12-02 23:08:25

From "man dlclose":

The function dlclose() decrements the reference count on the dynamic
library handle handle.  If the reference count drops to zero and
no other loaded libraries use symbols in it, then the dynamic library
is unloaded.

I am guessing that you are running afoul of the "no other loaded libraries use symbols in it" part.

To debug, run your program with LD_DEBUG=bindings, and look for messages like:

binding file <some.so> [0] to libdynamicTest.so.1 [0]: normal symbol `<symbol>'

Update:

You have several bugs:

  1. You are linking test_agent against libdynamic.so.1 directly:
    cc -o test_agent -L. ...-ldl build/test_agent/hello.o libdynamic.so.1

    Once you've done this, you can no longer expect this library to be ever unloaded.

  2. By doing this:

    *((int *)handle) = 0;

    you are actually corrupting the state of the dynamic loader, and that causes subsequent dlsym to give you bogus address, which causes your SIGSEGV when you try to use it.

Once you fix problem #2, your program will no longer crash, though it will still not unload the library. To actually get the library to unload, you need to also fix problem #1.

If you fix problem #1 first, then problem #2 will no longer corrupt the dynamic loader. It will corrupt heap instead, and you can trivially observe that with Valgrind.

According to man 8 ld.so:

BUGS
       Currently ld.so has no means of unloading and searching for com‐
       patible or newer version of libraries.

I'm not 100% sure this is related, but it sounds like it may be.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!