dylib如何卸载自身?
我试图了解有关使用 dylib 的一些事情并且仍在学习,所以如果我问愚蠢的问题,请原谅。
- 通过 dlopen() 加载的 lib 是否可以自行卸载?
前任。在下面的代码中,我们可以只执行 hello() 代码,然后在最后卸载它吗?
__attribute__((constructor))
static void hello()
{
// get handle to this dylib some how ???
// some how unload itself ????
}
我在几个链接上看到,可以从自身卸载库。但它是针对 Windows 的。 Lib 自行卸载。
2.另一个进程可以卸载某个 xyz 进程中注入的库吗?
I am trying to understand few things about using dylib and still studying, so please excuse me if I ask stupid question.
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DLL 可能会自行卸载,但如果合法加载 DLL 的进程稍后调用 DLL 中的函数,则可能会导致该进程崩溃。
您引用的链接描述了 DLL 注入,即 DLL 的代码作为进程地址空间的一部分加载,以拦截进程对其他“合法”中的函数进行的调用DLL。它通常以“秘密”的方式进行。即,其调用被拦截并重新路由的进程不知道 DLL 注入。
因此,该链接指的是在极少数情况下使用的高级内容。
您还想知道如果您加载的 DLL 抛出异常该怎么办。您应该能够捕获它,但可能不需要卸载 DLL。所有平台上都有操作系统特定的调用来卸载共享库。所以,是的,你可以做到。然而,仅仅因为 DLL 抛出异常而卸载它是不寻常的。它通常工作正常,但如果进程中的另一个线程仍在使用该 DLL 中的代码,则会导致问题(显然您必须意识到这一点)。
简短的回答是,是的,如果您显式加载了 DLL,则可以卸载它。只要仔细考虑什么时候是卸载的最佳时机。
Windows:
Unix/Linux:
PS:我在答案中使用“DLL”这一术语来指代 Windows 和 Linux 上的共享库。
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:
Unix/Linux:
PS:I use the one term, DLL in my answer to refer to shared libraries on Windows and Linux.
对于 Windows,请参阅我的回答,了解有关 FreeLibraryAndExitThread 函数的正确操作。我认为其他系统中也有类似的功能。
据我了解,在这种情况下调用 FreeLibrary 是一种错误的方法 - 来自 MSDN:“如果他们分别调用 FreeLibrary 和 ExitThread,就会存在竞争条件。库可以在调用 ExitThread 之前卸载。”需要注意的是,ExitThread 除了从线程函数返回之外还执行一些簿记操作。
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.