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/