linux/gcc:C/C++ 内部的 ldd 功能程序

发布于 2024-08-06 11:21:20 字数 425 浏览 3 评论 0原文

有没有一种简单有效的方法可以知道给定的动态链接的ELF缺少运行所需的.so,所有这些都来自C/C++程序的内部

我需要一个与 ldd 功能有些相似的程序,而不是尝试执行 ELF 来找出系统中的(满足/未满足)依赖项。也许通过某个库询问 ld-linux.so 实用程序? (我是linux这一部分的新手=)

注意:阅读ldd的源代码对我的意图没有多大帮助:看起来ldd实际上是在分叉另一个进程并执行该程序。

如果在不执行程序的情况下无法知道程序是否具有未满足的依赖项,是否有某种方法至少可以在我的程序中快速列出该 ELF 所需的 .so?

预先感谢 =)

Is there a simple and efficient way to know that a given dynamically linked ELF is missing a required .so for it to run, all from the inside of a C/C++ program?

I need a program with somewhat similar functionality as ldd, without trying to execute the ELF to find out the (met/unmet) dependencies in the system. Perhaps asking the ld-linux.so utility via some library? (I'm a newbie in this part of linux =)

NOTE: reading the source code of ldd was not very helpful for my intentions: it seems that ldd is in fact forking another process and executing the program.

If it's not possible to know that a program has unmet dependencies without executing it, is there some way to, at least, quickly list the .so's required for that ELF all from within my program?

Thanks in advance =)

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

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

发布评论

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

评论(3

无戏配角 2024-08-13 11:21:20

根据 ld.so(8),设置环境变量 LD_TRACE_LOADED_OBJECTS 到非空字符串将给出类似 ldd 的结果(而不是正常执行二进制文件或库)。

setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
FILE *ldd = popen("/lib/libz.so");

As per ld.so(8), setting the environment variable LD_TRACE_LOADED_OBJECTS to a non-empty string will give ldd-like results (instead of executing the binary or library normally).

setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
FILE *ldd = popen("/lib/libz.so");
迷鸟归林 2024-08-13 11:21:20

您尝试过dlopen功能吗?您可以使用它来加载动态库(或者,对于您的情况,检查是否可以加载库)。

获得所需库的列表更加困难,请查看 readelf 源

Have you tried dlopen function? you can use this to load a dynamic library (or, for your case, to ckeck if a library can be loaded).

Having a list of needed libraries is more difficult, take a look to handle_dynamic function on readelf source

荒芜了季节 2024-08-13 11:21:20

使用 ptrace() 跟踪所有 open() 调用以查找程序所依赖的所有内容怎么样(但是,输出包括文件,而不仅仅是库)。或者也许通过文件名“/lib”中的前缀过滤输出会有所帮助。

What about using ptrace() to trace all open() calls to find all what the program depends on (however, the output includes files,not only libraries).Or maybe filtering the output by the prefix in file name "/lib" helps.

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