.so 依赖图中的符号查找顺序
Order of symbol lookup in .so dependency graph
假设我在其中一个 .so 中引用了一个共享对象加载时依赖关系图和符号 foo
。还假设这个符号 foo
在其他几个共享对象中定义。我的问题是:将找到哪个定义,什么是查找顺序以及定义的位置(在什么标准中,手册页)?
示例:
考虑一个依赖图
https://i.imgur.com/jdhD3V0.png
其中以 ldd 顺序列出的库,例如
ldd a.so
b.so
d.so
让我们假设 foo
在 c.so 和 d.so 中定义,并首先在 f.so 中引用。我的实验表明 d.so 的实现将由链接器执行。看起来库是按 bfs 顺序搜索的。这是正确的吗?这是否与库加载顺序一致?我在任何文档中都找不到它,必须确保它不是实现定义的。
谢谢!
动态链接在ELF specfication. (Note that there are some really old PDFs and Postscript files floating around, but those are generally very outdated.) Symbol lookup is described in section Shared Object Dependencies:
中指定
When resolving symbolic references, the dynamic linker examines the symbol tables with a breadth-first search. That is, it first looks at the symbol table of the executable program itself, then at the symbol tables of the DT_NEEDED
entries (in order), and then at the second level DT_NEEDED
entries, and so on.
(有多种扩展可以改变这种行为。ELF 规范本身定义了 DF_SYMBOLIC
flag。)
这意味着无法回答你的问题,因为你的图表没有显示主要的可执行文件,并且不清楚搜索多个依赖项的顺序(从上到下或从下到上)。
查找顺序是否与对象加载顺序匹配是实现定义的,因为根据 ELF 规范,仅加载对象(不执行其初始化函数)并不具有可观察的效果。
Initialization order(执行初始化函数的顺序)比符号查找顺序受到的限制更少,因为 DT_NEEDED
条目的顺序对此无关紧要。所以理论上,一个实现有可能在 b.so
之前加载一个初始化 d.so
,但是来自 b.so
的符号插入 d.so
的符号,因为它在符号搜索中排在第一位顺序(由于 DT_NEEDED
条目的排序方式)。
假设我在其中一个 .so 中引用了一个共享对象加载时依赖关系图和符号 foo
。还假设这个符号 foo
在其他几个共享对象中定义。我的问题是:将找到哪个定义,什么是查找顺序以及定义的位置(在什么标准中,手册页)?
示例: 考虑一个依赖图 https://i.imgur.com/jdhD3V0.png
其中以 ldd 顺序列出的库,例如
ldd a.so
b.so
d.so
让我们假设 foo
在 c.so 和 d.so 中定义,并首先在 f.so 中引用。我的实验表明 d.so 的实现将由链接器执行。看起来库是按 bfs 顺序搜索的。这是正确的吗?这是否与库加载顺序一致?我在任何文档中都找不到它,必须确保它不是实现定义的。
谢谢!
动态链接在ELF specfication. (Note that there are some really old PDFs and Postscript files floating around, but those are generally very outdated.) Symbol lookup is described in section Shared Object Dependencies:
中指定When resolving symbolic references, the dynamic linker examines the symbol tables with a breadth-first search. That is, it first looks at the symbol table of the executable program itself, then at the symbol tables of the
DT_NEEDED
entries (in order), and then at the second levelDT_NEEDED
entries, and so on.
(有多种扩展可以改变这种行为。ELF 规范本身定义了 DF_SYMBOLIC
flag。)
这意味着无法回答你的问题,因为你的图表没有显示主要的可执行文件,并且不清楚搜索多个依赖项的顺序(从上到下或从下到上)。
查找顺序是否与对象加载顺序匹配是实现定义的,因为根据 ELF 规范,仅加载对象(不执行其初始化函数)并不具有可观察的效果。
Initialization order(执行初始化函数的顺序)比符号查找顺序受到的限制更少,因为 DT_NEEDED
条目的顺序对此无关紧要。所以理论上,一个实现有可能在 b.so
之前加载一个初始化 d.so
,但是来自 b.so
的符号插入 d.so
的符号,因为它在符号搜索中排在第一位顺序(由于 DT_NEEDED
条目的排序方式)。