c - 有没有办法告诉链接器在加载共享对象时忽略 DT_SONAME?

标签 c linux linker

man ld 说:

-soname=name When creating an ELF shared object, set the internal DT_SONAME field to the specified name. When an executable is linked with a shared object which has a DT_SONAME field, then when the executable is run the dynamic linker will attempt to load the shared object specified by the DT_SONAME field rather than the using the file name given to the linker.

这个语句的后半部分可以被覆盖吗?换句话说,在运行可执行文件时,是否可以强制链接器忽略外部共享库的 DT_SONAME 而只使用文件名?

最佳答案

In other words, can when running an executable can you force the linker to ignore the DT_SONAME of external shared libraries and instead use file names only?

没有。

首先,链接器不参与可执行文件的运行,加载器(也称为动态链接器)参与。加载器是一个完全独立的程序,通常不共享任何代码,与链接器完全不同。

其次,链接器完成后,用于链接可执行文件的文件名不会记录在任何地方(仅记录DT_SONAME)。因此,即使加载器想要使用原始文件名,它也无法找出原来的文件名。

也就是说,您可以更改记录DT_SONAME 的可执行文件的.dynstr 部分中的字符串。如果字符串是(比方说)foobar.so.1,而您将其二进制编辑为 barfoo.so,那么加载程序将查找 barfoo。 so(使用其通常的共享库搜索规则)而不是 foobar.so.1。这里唯一的限制是新名称不能比旧名称长(否则您会破坏其他字符串)。

也可以将 .dynstr 部分替换为具有更长字符串的新部分(并将 foobar.so.1 替换为 /full/path/to/libsomething.so),但这种转换很难正确执行。

关于c - 有没有办法告诉链接器在加载共享对象时忽略 DT_SONAME?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701156/

相关文章:

php - 使用/发送 POST 数据时的 libcurl C 问题(不是 C++)

c - 矩阵未在声明中填零

linux - Linux下/sys/devices下创建文件

linux - Bash for 循环 - 列出/etc/init.d 中的文件,然后对结果运行命令

c++ - 什么会导致 "can' t 链接到共享库上的主可执行文件?

c++ - mongodb cpp 驱动程序(ubuntu 服务器 x64)中缺少 lboost_thread-mt

c - 外部结构数组错误: array type has incomplete element type

c - 结构成员如何存储在小端机器上?

linux - 如何复制文件/目录而不更改其属性

C++ - LNK2019 未解析的外部符号 - 似乎找不到我缺少的东西