dlopen:是否可以“手动”捕获未解析的符号?当它们发生时解决它们?

发布于 2024-11-30 03:23:43 字数 347 浏览 4 评论 0原文

是否可以在发生未解析的符号引用时捕获它们,以便调用函数来尝试根据需要解析符号?或者是否可以在运行时向动态符号表添加新符号,而无需创建库文件并对其进行 dlopen?我使用 GNU/Linux,使用 GCC。 (向其他 Unix 系统的可移植性会很好,但这不是一个关键问题。)

提前致谢!

编辑:我应该更详细地说明我想要做什么。我想为编程语言编写一个解释器,预计它支持编译(dlopen'ed)和解释模块。我希望链接器解析从已编译模块到其他地方定义的函数的调用,以避免每次调用时查找该函数,但对解释代码的调用将无法解析。我想捕获这些调用,以便我可以在需要时调用适当的解释函数(或者如果该函数不存在则发出错误信号)。

Is it possible to trap unresolved symbol references when they happen, so that a function is called to try to resolve the symbol as needed? Or is it possible to add new symbols to the dynamic symbol table at runtime without creating a library file and dlopen'ing it? I am on GNU/Linux, using GCC. (Portability to other Unixes would be nice, but is not a key concern.)

Thanks in advance!

Edit: I should have given more detail about what I am trying to do. I want to write an interpreter for a programming language, which is expected to support both compiled (dlopen'ed) and interpreted modules. I wanted calls from a compiled module to functions defined elsewhere to be resolved by the linker, to avoid a lookup for the function at every call, but calls to interpreted code would be left unresolved. I wanted to trap those calls, so that I could call the appropriate interpreted function when needed (or signal an error if the function does not exist).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-12-07 03:23:43

我大胆猜测,您要解决的问题是您 dlopen 并开始使用可加载模块,然后由于无法解析的符号而突然崩溃。如果是这样,这是延迟绑定的结果,您可以通过在环境中导出LD_BIND_NOW=1(或任何值,只要已设置)来禁用它。这将确保在 dlopen 返回之前可以解析所有符号,如果有任何符号不能解析,则 dlopen 操作将失败,让您可以优雅地处理这种情况。

I'm making a wild guess that the problem you're trying to address is the case where you dlopen and start using a loadable module, then suddenly crash due to unresolved symbols. If so, this is a result of lazy binding, and you can disable it by exporting LD_BIND_NOW=1 (or any value, as long as it's set) in the environment. This will ensure that all symbols can be resolved before dlopen returns, and if any can't, the dlopen operation will fail, letting you handle the situation gracefully.

庆幸我还是我 2024-12-07 03:23:43

如果您知道缺少哪些符号,则可以仅使用它们编写一个库,并在应用程序执行之前对其进行 LD_PRELOAD。

如果您没有缺少的符号列表,您可以通过在二进制文件上使用“nm”或“objdump”来发现它们,并在此基础上编写一个脚本,该脚本将使用以下命令构建库在应用程序执行之前丢失符号,然后也进行 LD_PRELOAD。

此外,您可以使用 gdb 将新的“代码”注入应用程序,使函数指向您需要的内容。

最后,您还可以重写一些 ld.so 函数来检测丢失的符号,并对它们执行一些操作。

但无论如何,如果您可以解释您想要完成的任务,那么提供适当的解决方案会更容易。

If you know what symbols are missing, you could write a library just with them, and LD_PRELOAD it prior to the application execution.

If you don't have the list of the symbols that are missing, you could discover them by using either 'nm' or 'objdump' on the binary, and, with base on that, write a script which will build the library with the missing symbols prior to the application execution, and then LD_PRELOAD it as well.

Also, you could use gdb to inject new 'code' into applications, making the functions point to what you need.

Finally, you could also override some of the ld.so functions to detect the missing symbols, and do something about them.

But in any case, if you could explain what you are trying to accomplish, it would be easier to provide a proper solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文