共享库如何知道它所在的位置? [英] How can a shared library know where it resides?

查看:56
本文介绍了共享库如何知道它所在的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 linux 机器开发一个共享库,它是相对于带有 rpath 的主可执行文件动态加载的.现在,库本身尝试相对于其位置动态加载其他库,但没有 rpath(我使用 scandir 在某个文件夹中搜索共享库 - 我还不知道它们的名称).

I'm developing a shared library for linux machines, which is dynamically loaded relative to the main executable with rpath. Now, the library itself tries to load other libraries dynamically relative to its location but without rpath (I use scandir to search for shared libraries in a certain folder - I don't know their names yet).

这仅适用于将工作目录设置为共享库位置的情况,否则我会按预期查看不同的目录.

This works only, if the working directory is set to the shared libraries location, as otherwise I look in a different directory as intended.

共享库是否有任何实用、可靠的方法来确定其驻留位置?

我知道,我可以使用/proc/self/maps 或类似的东西来获取加载的文件,但这只有效,只要库知道它自己的名字.

I know, I could use /proc/self/maps or something like that, to get the loaded files, but this works only, as long the library knows its own name.

另一个想法是使用dlinfo(),但是要使用它,共享库需要知道自己的句柄.

Another idea is so use dlinfo(), but to use it, the shared library need to know its own handle.

推荐答案

共享库有什么实用、可靠的方法吗确定它所在的位置?

Is there any practical, reliable way for the shared library to determine where it resides?

我会使用 dlinfo/proc/self/maps(proc 可能并不总是被挂载,尤其是在容器中).

I'd use dlinfo and /proc/self/maps (proc may not always be mounted, especially in containers).

我知道,我可以使用/proc/self/maps 或类似的东西,获取加载的文件,但这仅适用于库知道自己的名字.

I know, I could use /proc/self/maps or something like that, to get the loaded files, but this works only, as long the library knows its own name.

不是真的,您可以使用指向库中某些代码的指针(最好指向某些内部标签,以避免与 PLT/GOT 混淆)并将结果与​​从 /proc/self/maps/proc/self/maps 获得的内存范围进行比较代码>(或dlinfo).

Not really, you can take a pointer to some code inside library (preferably to some internal label, to avoid messing with PLT/GOT) and compare result against memory range obtained from /proc/self/maps (or dlinfo).

这篇关于共享库如何知道它所在的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆