如何设置共享库的动态链接器路径?

发布于 2024-10-08 10:20:45 字数 443 浏览 0 评论 0原文

我想编译一个带有 .interp 段的共享库。

#include <stdio.h>

int foo(int argc, char** argv) {

    printf("Hello, world!\n");
    return 0;

}

我正在使用以下命令。

gcc -c -o test.o test.c
ld --dynamic-linker=blah -shared -o test.so test.o

我最终没有 INTERP 段,就好像我从未通过 --dynamic-linker=blah 选项一样。使用readelf -l test.so 检查。构建可执行文件时,链接器会正确处理选项并将 INTERP 段放入程序头中。如何使其也适用于共享库?

I want to compile a shared library with an .interp segment.

#include <stdio.h>

int foo(int argc, char** argv) {

    printf("Hello, world!\n");
    return 0;

}

I'm using the following commands.

gcc -c -o test.o test.c
ld --dynamic-linker=blah -shared -o test.so test.o

I end up without an INTERP segment, as if I never passed the --dynamic-linker=blah option. Check with readelf -l test.so. When building an executable, the linker processes the option correctly and puts an INTERP segment in the program header. How to do I make it work for shared libraries too?

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

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

发布评论

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

评论(3

青瓷清茶倾城歌 2024-10-15 10:20:45

如果使用 -sharedld 不包含 .interp 部分,正如 @MichaelDillon 已经说过的那样。不过,您可以自己提供此部分。

const char interp_section[] __attribute__((section(".interp"))) = "/path/to/dynamic/linker";

上面的行将使用 GCC 属性

如果您尝试构建一个本身也可执行的共享对象,请查看此问题。它对该过程有更全面的描述。

ld doesn't include a .interp section if -shared is used, as @MichaelDillon already said. You can however provide this section yourself.

const char interp_section[] __attribute__((section(".interp"))) = "/path/to/dynamic/linker";

The line above will save the string "/path/to/dynamic/linker" in the .interp section using GCC attributes.

If you're trying to build a shared object that's also executable by itself, check this question out. It has a more comprehensive description of the process.

千紇 2024-10-15 10:20:45

INTERP 段仅进入需要首先加载 ELF 解释器 (ld.so) 的二进制文件。共享库没有 INTERP 段,因为在加载共享库之前 ELF 解释器已经加载。

The INTERP segment only goes into binaries which need to load the ELF interpreter (ld.so) in the first place. A shared library has no INTERP segment because the ELF interpreter is already loaded before the shared library is loaded.

嗳卜坏 2024-10-15 10:20:45

在大多数 Linux 系统中,ldconfig 在每次系统启动时运行,它会查找 /etc/ld.so.conf 中的定义以查找具有共享库的目录。在文件 /etc/ld.so.cache 中有共享库 sonames 和库完整路径的映射。考虑阅读这篇文章:http://grahamwideman.wordpress.com/2009/02/09/the-linux-loader-and-how-it-finds-libraries/#comment-164

In most linux systems the ldconfig is run at every system boot and it looks definitions in /etc/ld.so.conf for looking in directories that have shared libraries. In the file /etc/ld.so.cache there are mappings for shared libraries sonames and the library full path. Consider reading this article: http://grahamwideman.wordpress.com/2009/02/09/the-linux-loader-and-how-it-finds-libraries/#comment-164

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