0

I am trying to understand few things about using dylib and still studying, so please excuse me if I ask stupid question.

  1. Is it possible for lib loaded via dlopen() to unload itself?

Ex. In code below, can we just execute the hello() code then at the end unload it.

 __attribute__((constructor))
 static void hello()
{
   // get handle to this dylib some how ???
   // some how unload itself ????
}

I have seen on few links, that it is possible to unload a libray from itself. But it is for windows. Lib unload itself.

2 . Can another process unload the library injected in some xyz process?

RLT
  • 4,219
  • 4
  • 37
  • 91

2 Answers2

1

For Windows, please see my answer here regarding FreeLibraryAndExitThread function for properly doing it. I assume there are similar functions available in other systems.
As I undestand it, calling FreeLibrary in this case is a wrong way to do that - from MSDN: "If they were to call FreeLibrary and ExitThread separately, a race condition would exist. The library could be unloaded before ExitThread is called." As a remark, ExitThread does some bookkeeping besides just returning from the thread function.

Community
  • 1
  • 1
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
1

It may be possible for a DLL to unload itself but it may cause the process that legitimately loaded the DLL to crash if that proces later calls a function in the DLL.

The link you refer to describes DLL injection which is where the code of a DLL is loaded as part of the address space of a process with a view to intercepting calls the process makes to functions in other "legitimate" DLLs . It is usually done in a "secretive" manner. i.e. the process whose calls are intercepted and rerouted is unaware of the DLL injection.

So the link refers to advanced stuff which is used in rare circumstances.

You are also wondering what to do if a DLL you load throws an exception. You should be able to catch it, but it might not be necessary to unload the DLL. There are OS specific calls on all platforms to unload a shared library. So, yes, you can do it. However it would be unusual to unload a DLL just because it threw an exception. It normally works fine but would cause a problem if another thread in your process is still using code in that DLL (this you would have to be aware of obviously).

The short answer is, yes, you can unload the DLL if you explicitly loaded it. Just think carefully about when is the best time to unload it.

Windows:

LoadLibrary()
FreeLibrary()
GetProcAddress()

Unix/Linux:

dlopen()
dlclose()
dlsym()

PS:I use the one term, DLL in my answer to refer to shared libraries on Windows and Linux.

ScrollerBlaster
  • 1,578
  • 2
  • 17
  • 21
  • I am actually trying to unload the injected dylib. I cannot use dlclose() and dlsym() without having an handle to it. Thus I want to do something like what has been done in link I mentioned. But for OS X. – RLT Jan 05 '12 at 11:45
  • Bit confused now. Is it for the process to unload the injected dynlib ? Or for another process to do it or for the dynlib to do it. I thought you also wanted the dynlib to unload itself? I suggest revisiting the question to make it more explicit. – ScrollerBlaster Jan 05 '12 at 12:01
  • 1
    Still seems very ambitious. Not only will you have to get the handle, and I dont know if that is possible, but also presumably the injected code did some rerouting of calls to its own functions which would have to be undone in order to restore everything to a stable state. – ScrollerBlaster Jan 05 '12 at 13:07
  • Yes it will be undone in __attribute__((destructor)) static void destroy() {} – RLT Jan 05 '12 at 13:57
  • Please see my answer regarding FreeLibraryAndExitThread. Using FreeLibrary would lead to a race condition where ExitThread is not called. – Roland Pihlakas Nov 29 '13 at 07:22