对于具有相同文件的两个调用,dlopen是否会产生相同的句柄? [英] Will dlopen yield the same handle for two calls with the same file?
问题描述
如果在同一应用程序运行中两次在同一个lib/文件上使用dlopen,在两种情况下它会产生相同的句柄吗?是否有任何保证(简短的实验表明至少在我的盒子上可以做到)?
If I use dlopen on the same lib/file two times in the same application-run, will it yield the same handle in both cases? Is there any guarantee for this (a short experiment showed that it at least does on my box)?
我目前正在玩一个小的插件系统(出于好奇),如果对此观察到的行为有某种保证,我可以将此地址用作插件的键,以防止重复加载. /p>
I'm currently playing around with a little plugin-system (out of curiosity) and if there would be some kind of guarantee for this observed behaviour I could use this address as a key for a plugin to prevent duplicated loads.
推荐答案
是. dlopen(3) linux手册页说:
Yes. The dlopen(3) linux man page says:
If the same library is loaded again with dlopen(), the same file
handle is returned. The dl library maintains reference counts for
library handles, so a dynamic library is not deallocated until
dlclose() has been called on it as many times as dlopen()
has succeeded on it.
顺便说一句,在Linux系统上,您可以dlopen很多(成千上万)共享库,例如我的例子
BTW, on Linux systems, you can dlopen a lot (many dozens of thousands) of shared libraries, as my example manydl.c demonstrates. The main limitation is address space.
So practically, not bothering about dlclose
-ing stuff is possible.
(除非您的dlopen版本共享库具有怪异的或消耗资源的构造函数或析构函数)
2017年12月添加:
请注意,相关的是传递给dlopen
的确切路径字符串.因此,如果使用"./foo.so"
和"././foo.so"
(或"../foosymlink.so"
,其中foosymlink.so
是与foo.so
的符号链接),则dlopen句柄是不同的,并且在某些情况下,该共享库的两个实例的行为异常可能发生.
Notice that what is relevant is the exact path string passed to dlopen
. So if you use "./foo.so"
and "././foo.so"
(or "../foosymlink.so"
where foosymlink.so
is a symlink to foo.so
) the dlopen-ed handles are different, and in some cases weird behavior of the two instances of that shared library might happen.
于2019年6月添加:
另请阅读Drepper的 如何编写共享库 纸(它也很好地解释了如何使用它们!).
Read also Drepper's How to write shared libraries paper (it explains also well how to use them!).
这篇关于对于具有相同文件的两个调用,dlopen是否会产生相同的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!