为什么 ld 不在 link 时间从 DSO 本身搜索 rpaths

Why doesn't ld search rpaths from a DSO itself at link time

我有 libA.so、libB.so 和一个可执行文件 'foo'。 'foo' 需要 libB.so 而它本身需要 libA.so。在 linking foo 期间使用 libB 显式 links 因为它直接使用它的符号。 'foo' 不直接使用 libA 中的符号。当 linking 'foo' 时,ld 想要检查它是否可以解析 libA 中 libB 的符号引用,但它找不到 libA。我可以使用 -Wl,rpath-link= 让它找到 libA,或者我可以让 linker 使用 -Wl,--allow-shlib-undefined.

忽略 libA

问题是我不必设置这些选项中的任何一个,因为 libB.so 包含一个 rpath 告诉 linker 在哪里可以找到 libA.so 和 linker 在运行时使用这个 rpath 成功找到了 libA。那么为什么它不在 link 时间使用它呢?在这种情况下,强制 foo 的构建配置知道 libA 在哪里似乎完全没有必要?

I shouldn't have to set either of these options because libB.so contains an rpath that tells the linker where to find libA.so and the linker uses this rpath at runtime

您混淆了静态 linker ld 和运行时 linker(又名加载器)ld.so.

在 Linux,它们分别来自 binutils 和 GLIBC。它们是完全不同的程序,由不同的人员维护。

ld 可以实现 ld.so 当前 版本使用的搜索路径,但这是

  • 大量代码,需要从头开始编写并且
  • 一旦 ld.so 搜索机制改变就会中断

更新:

isn't the search of the rpath executed by the dynamic linker 'the standard'

没有定义这个的标准(据我所知)。

此外,在 Linux 和 Solaris 上,ld.so 使用的搜索路径可能包含 $ORIGIN$PLATFORM 等动态标记,它们是 在(静态)link 时间未知