如何使用GCC/LD设置RPATH和RUNPATH? [英] How to set RPATH and RUNPATH with GCC/LD?

查看:291
本文介绍了如何使用GCC/LD设置RPATH和RUNPATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在系统升级后遇到了这个问题:使用GCC -Wl,-rpath=选项的工作方式与以前不同.

I recently encountered this problem after an upgrade of the system: using the GCC -Wl,-rpath= option works differently than before.

我用它来设置一些在我的项目子模块中构建的共享库的搜索路径.当时我认为它比在系统范围内设置LD_LIBRARY_PATH更好(我不想在每次打开计算机时都进行设置).一切正常,这两种方法似乎是等效的.

I used it to set the search paths to some shared libraries that were built in a sub-module of my project. At the time I considered it better than setting LD_LIBRARY_PATH system-wide (I didn't want to set it every time I turned on the computer). Everything worked fine, and these two approaches seemed equivalent.

现在,它表明-rpath的行为已更改.它仍然适用于直接依赖的库,但不适用于通过-rpath=链接同一目录中链接其他库的库.导出LD_LIBRARY_PATH仍然可以像以前一样进行.

Now it seams that the behavior of -rpath has changed. It still works for directly dependent libraries, but not for those that link other libraries from the same directory set via -rpath=. Exporting LD_LIBRARY_PATH still works as before.

我使用readelf检查了编译的输出,并且有所不同.在升级之前(带有GCC 5.4的Linux Mint 18.2),动态部分具有以下内容:

I inspected the output of the compilation using readelf and there is a difference. Before upgrade (Linux Mint 18.2 with GCC 5.4) the Dynamic section had this line:

0x000000000000000f (RPATH)            Library rpath: [submod/lib]

升级后(带有GCC 7.3的Linux Mint 19),该行更改为:

After the upgrade (Linux Mint 19 with GCC 7.3) the line changed to:

0x000000000000001d (RUNPATH)            Library runpath: [submod/lib]

使用RPATH但不使用RUNPATH吗?建议将RPATH替换为RUNPATH(或者说它至少具有不同的用途,因为它的优先级较低),但是它没有给出为什么会影响间接链接的答案.库本身在readelf输出中既没有RPATH也没有RUNPATH.

In use RPATH but not RUNPATH? it is suggested that RPATH was replaced by RUNPATH (or that it at least servers a different purpose as it has a lower priority), but it doesn't give an answer as to why this effects indirect linking. The libraries themselves have neither RPATH, or RUNPATH in readelf output.

所以我的问题是这样的:为什么链接器突然开始以不同的方式解释-rpath=选项,并且有一种方法可以强制执行旧的行为?等效的结果.)

So my question is this: Why has the linker suddenly started to interpret the -rpath= option differently, and is there a way to force the old behavior? (Or do something different that will yield an equivalent result.)

另一个问题是:是否可以告诉链接器的旧版本产生新的输出(即RUNPATH而不是RPATH)?

编辑

这不是如何设置二进制文件的RunPath的副本吗? /a>-我的问题正好相反:我想要RPATH的行为.我已经弄清楚了(感谢评论中的技巧),我将在这里回答我的问题.

This is not a duplicate of How to set RunPath of a binary? -- my question is the opposite: I want the behavior of RPATH. I figured it out (thanks to the tip in the comment), and I will answer my questions here.

推荐答案

有没有办法强制旧的行为?

Is there a way to force the old behavior?

是的.您可以使用此选项-Wl,--disable-new-dtags告诉 new 链接程序使用 old 行为,即RPATH.

Yes. You can use this option -Wl,--disable-new-dtags to tell the new linker to use the old behavior, i.e. RPATH.

是否可以告诉链接器的旧版本产生新的输出(即RUNPATH而不是RPATH)?

Is it possible to tell the old version of linker to produce the new output (i.e. RUNPATH instead of RPATH)?

是的.使用-Wl,--enable-new-dtags告诉 old 链接程序使用 new 行为,即RUNPATH.

Yes. Use -Wl,--enable-new-dtags to tell the old linker to use the new behavior, i.e. RUNPATH.

我用readelf验证了可执行文件,这两个选项似乎可以控制ELF动态部分中的内容.我认为问题是由新版本默认值的更改引起的,尽管有趣的是,ld的手册页会建议它仍然相同:

I verified the executable with readelf and these two options seem to control what will be written in the ELF Dynamic section. I think the problem was caused by a change in the defaults for the new version, although, interestingly, the manual page for ld would suggest that it should still be the same:

-enable-new-dtags
--disable-new-dtags
该链接器可以在ELF中创建新的动态标签.但是较旧的ELF系统可能无法理解它们.如果指定 --enable-new-dtags,将根据需要创建新的动态标签,并 较旧的动态代码将被省略.如果指定--disable-new-dtags,则不会创建新的动态标签. 默认情况下,不会创建新的动态代码.请注意,这些选项是 仅适用于ELF系统.

--enable-new-dtags
--disable-new-dtags
This linker can create the new dynamic tags in ELF. But the older ELF systems may not understand them. If you specify --enable-new-dtags, the new dynamic tags will be created as needed and older dynamic tags will be omitted. If you specify --disable-new-dtags, no new dynamic tags will be created. By default, the new dynamic tags are not created. Note that those options are only available for ELF systems.

这篇关于如何使用GCC/LD设置RPATH和RUNPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆