rpath - runtime search path

本文详细介绍了RPath在二进制文件中如何指导库和依赖项定位的方法,并深入探讨了GNU ld.so的工作原理及配置选项。文章还解释了在运行时如何搜索共享库的复杂算法。

RPath is a very interesting topic which can instruct the binary to find the right location of libraries and dependencies.

GNU ld.so has some rules in regarding how to use the rpath to find the right dependencies (basically it has attribute or environment which direct the runtime to look for directories for things within).

 

You may find some explaination from this wikipedia link: http://en.wikipedia.org/wiki/Rpath 

GNU ld.so

The dynamic linker of the GNU C Library and its derivative Embedded GLIBC implement a rather complicated algorithm for searching for shared libraries. The basic search order is:[1]

  1. The (colon-separated) paths in the DT_RPATH dynamic section attribute of the binary if present and DT_RUNPATH attribute does not exist.
  2. The (colon-separated) paths in the environment variable LD_LIBRARY_PATH, unless the executable is a setuid/setgidbinary, in which case it is ignored. LD_LIBRARY_PATH can be overridden by calling the dynamic linker with the option --library-path (e.g. /lib/ld-linux.so.2 --library-path $HOME/mylibs myprogram).
  3. The (colon-separated) paths in the DT_RUNPATH dynamic section attribute of the binary if present.
  4. Lookup based on the ldconfig cache file (often located at /etc/ld.so.cache) which contains a compiled list of candidate libraries previously found in the augmented library path (set by /etc/ld.so.conf). If, however, the binary was linked with the-z nodeflib linker option, libraries in the default library paths are skipped.
  5. In the trusted default path /lib, and then /usr/lib. If the binary was linked with the -z nodeflib linker option, this step is skipped.

Notes:

  • The option --inhibit-rpath LIST of the dynamic linker instructs it to ignore DT_RPATH and DT_RUNPATH attributes of the object names in LIST.
  • Libraries specified by the environment variable LD_PRELOAD and then those listed in /etc/ld.so.preload are loaded before the search begins. A preload can thus be used to replace some (or all) of the requested library's normal functionalities, or it can simply be used to supply a library that would otherwise not be found.
  • Static libraries are searched and linked into the ELF file at link time and are not linked at run time.

[edit]The role of GNU ld

The GNU Linker (GNU ld) implements a feature which it calls "new-dtags": [2]

If the new-dtags feature is enabled in the linker (at run time using --enable-new-dtags), GNU ld, besides setting the DT_RPATHattribute, also sets the DT_RUNPATH attribute to the same string. At run time, if the dynamic linker finds a DT_RUNPATH attribute, it ignores the value of the DT_RPATH attribute, with the effect that LD_LIBRARY_PATH is checked next and the paths in theDT_RUNPATH attribute are only searched after it.

This means that in such configurations, the paths in LD_LIBRARY_PATH are searched before those given at link time using -rpath if--enable-new-dtags was active.

Instead of specifying the -rpath to the linker, the environment variable LD_RUN_PATH can be set to the same effect.

CMake Warning at /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:166 (add_library): Cannot generate a safe runtime search path for target mrp_interfaces__rosidl_generator_py because files in some directories may conflict with libraries in implicit directories: runtime library [libpython3.10.so] in /usr/lib/aarch64-linux-gnu may be hidden by files in: /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/usr/lib64/aarch64-linux-gnu Some of these libraries may not be found correctly. Call Stack (most recent call first): /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include) /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions) CMakeLists.txt:17 (rosidl_generate_interfaces) CMake Warning at /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:226 (add_library): Cannot generate a safe runtime search path for target mrp_interfaces__rosidl_typesupport_fastrtps_c__pyext because files in some directories may conflict with libraries in implicit directories: runtime library [libpython3.10.so] in /usr/lib/aarch64-linux-gnu may be hidden by files in: /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/usr/lib64/aarch64-linux-gnu Some of these libraries may not be found correctly. Call Stack (most recent call first): /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include) /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions) CMakeLists.txt:17 (rosidl_generate_interfaces) CMake Warning at /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:226 (add_library): Cannot generate a safe runtime search path for target mrp_interfaces__rosidl_typesupport_introspection_c__pyext because files in some directories may conflict with libraries in implicit directories: runtime library [libpython3.10.so] in /usr/lib/aarch64-linux-gnu may be hidden by files in: /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/usr/lib64/aarch64-linux-gnu Some of these libraries may not be found correctly. Call Stack (most recent call first): /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include) /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions) CMakeLists.txt:17 (rosidl_generate_interfaces) CMake Warning at /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:226 (add_library): Cannot generate a safe runtime search path for target mrp_interfaces__rosidl_typesupport_c__pyext because files in some directories may conflict with libraries in implicit directories: runtime library [libpython3.10.so] in /usr/lib/aarch64-linux-gnu may be hidden by files in: /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/usr/lib64/aarch64-linux-gnu Some of these libraries may not be found correctly. Call Stack (most recent call first): /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include) /home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions) CMakeLists.txt:17 (rosidl_generate_interfaces) //home/yebh/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/aarch64-none-linux-gnu/rootfs/usr/bin/python3: error while loading shared libraries: libm.so.6: cannot open shared object file: No such file or directory make[2]: *** [mrp_interfaces__py/CMakeFiles/mrp_interfaces__py.dir/build.make:199:rosidl_generator_py/mrp_interfaces/_mrp_interfaces_s.ep.rosidl_typesupport_fastrtps_c.c] 错误 127 make[1]: *** [CMakeFiles/Makefile2:593:mrp_interfaces__py/CMakeFiles/mrp_interfaces__py.dir/all] 错误 2 make: *** [Makefile:149:all] 错误 2 --- Failed <<< mrp_interfaces [5.54s, exited with code 2] Summary: 0 packages finished [5.84s] 1 package failed: mrp_interfaces 1 package had stderr output: mrp_interfaces
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值