首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

运行期加载时共享库路径搜索优先级实验

编程知识
2024年08月01日 19:44

目录

前言

《共享库链接和加载时的路径搜索优先级》中提到,共享库在运行期加载时的路径搜索优先级为:
RPATH>LD_LIBRARY_PATH>RUNPATH>/etc/ld.so.conf/etc/ld.so.conf.d/*>默认库路径

本实验分两步验证上述结论
①单独测试每种方法指定的路径的可行性
②查看链接器输出的详细信息,验证上述优先级顺序

实验环境

操作系统:Ubuntu 20.04
编译器:g++-11.4.0
make:GNU Make 4.2.1

目录说明

项目的目录结构如下:

.
├── lib
├── obj
├── libhello.cpp
├── main.cpp
├── run_cmp_all.sh
├── run_default.sh
├── run_ld_library_path.sh
├── run_ld_so_cache.sh
├── run_none.sh
├── run_rpath.sh
├── run_runpath.sh
└── makefile

其中:

  • lib为存放共享库的文件夹
  • obj为存放可重定位目标文件的文件夹
  • libhello.cpp为共享库源码,它将被编译为libhello.so.1.1.0(soname为libhello.so.1
  • mian.cpp为主函数,其中调用了hello函数
  • run_cmp_all.sh为对比测试上述路径优先级的脚本
  • run_default.sh为单独测试默认路径可行性的脚本
  • run_ld_library_path.sh为单独测试LD_LIBRARY_PATH可行性的脚本
  • run_ld_so_cache.sh为单独测试ld.so.cache可行性的脚本
  • run_none.sh为不配置任何路径的脚本,用于演示搜索不到动态库文件,程序无法运行的情况
  • run_rpath.sh为单独测试RPATH可行性的脚本
  • run_runpath.sh为单独测试RUNPATH可行性的脚本
  • makefile为自动化构建脚本

本文主要讨论运行期的加载过程,因此不对编译期的过程做过多解释,内容详见makefile文件

在附录中,我将提供本次实验涉及到的代码。

单独测试

不配置路径

不做任何路径的配置并且不在默认路径下放置libhello.so.1文件,查看程序是否可以运行成功。

直接运行脚本即可完成上述操作:

sh ./run_none.sh

输出:

./hello: error while loading shared libraries: libhello.so.1: cannot open shared object file: No such file or directory

可以看到由于我们没有配置任何额外的搜索路径,并且没有在默认搜索路径下放置libhello.so.1文件,动态链接器就找不到相应的共享库文件,就会导致加载失败,程序无法运行。

默认路径

libhello.so.1.1.0拷贝至默认搜索路径/usr/lib,并在/usr/lib下创建一个软链接(libhello.so.1)指向它
运行可执行文件。

直接运行脚本即可完成上述操作:

sh ./run_default.sh

输出:

Hello from the 1.1.0 library!

没有报错。

ld.so.cache

创建路径/opt/hellolib_runtime
libhello.so.1.1.0拷贝至/opt/hellolib_runtime,并在/opt/hellolib_runtime下创建一个软链接(libhello.so.1)指向它
/etc/ld.so.conf.d中新增一个配置文件libhello.conf,并向其中写入动态库文件所在的路径/opt/hellolib_runtime
使用ldconfig命令更新ld.so.cache
运行可执行文件

直接运行脚本即可完成上述操作:

sh ./run_ld_so_cache.sh

输出:

Hello from the 1.1.0 library!

没有报错。

RUNPATH

在编译期添加-Wl,--enable-new-dtags,-rpath,/opt/hellolib_runtime指定RUNPATH
libhello.so.1.1.0拷贝至/opt/hellolib_runtime,并在/opt/hellolib_runtime下创建一个软链接(libhello.so.1)指向它
运行可执行文件

直接运行脚本即可完成上述操作:

sh ./run_runpath.sh

输出:

Hello from the 1.1.0 library!

没有报错。

LD_LIBRARY_PATH

创建路径/opt/hellolib_runtime
libhello.so.1.1.0拷贝至/opt/hellolib_runtime,并在/opt/hellolib_runtime下创建一个软链接(libhello.so.1)指向它
向环境变量LD_LIBRARY_PATH中添加/opt/hellolib_runtime
运行可执行文件

直接运行脚本即可完成上述操作:

sh ./run_ld_library_path.sh

输出:

Hello from the 1.1.0 library!

没有报错。

RPATH

在编译期添加-Wl,--disable-new-dtags,-rpath,/opt/hellolib_runtime指定RPATH
libhello.so.1.1.0拷贝至/opt/hellolib_runtime,并在/opt/hellolib_runtime下创建一个软链接(libhello.so.1)指向它
运行可执行文件

直接运行脚本即可完成上述操作:

sh ./run_rpath.sh

输出:

Hello from the 1.1.0 library!

没有报错。

优先级测试

在此模块我们要测试共享库在运行期加载时的路径搜索优先级(没有测RUNPATH,有兴趣的朋友可以自行测试一下),关键步骤如下:
创建路径/opt/hellolib_runtime,/opt/hellolib_runtime1,/opt/hellolib_runtime2
在编译期添加-Wl,--disable-new-dtags,-rpath,/opt/hellolib_runtime指定RPATH
向环境变量LD_LIBRARY_PATH中添加/opt/hellolib_runtime1
/etc/ld.so.conf.d中新增一个配置文件libhello.conf,向其中写入路径/opt/hellolib_runtime2,并使用ldconfig命令更新ld.so.cache
libhello.so.1.1.0拷贝至默认搜索路径/usr/lib,并在/usr/lib下创建一个软链接(libhello.so.1)指向它
运行可执行文件

直接运行脚本即可完成上述操作:

sh ./run_cmp_all.sh

在脚本中,我设置了环境变量LD_DEBUG=libs,启用了动态链接器的调试输出(针对库的加载过程),因此终端输出了以下信息(仅截取关键部分):

 	460445:     find library=libhello.so.1 [0]; searching
    460445:      search path=/opt/hellolib_runtime/tls/haswell/x86_64:/opt/hellolib_runtime/tls/haswell:/opt/hellolib_runtime/tls/x86_64:/opt/hellolib_runtime/tls:/opt/hellolib_runtime/haswell/x86_64:/opt/hellolib_runtime/haswell:/opt/hellolib_runtime/x86_64:/opt/hellolib_runtime            (RPATH from file ./hello_rpath)
...
    460445:       trying file=/opt/hellolib_runtime/libhello.so.1
    460445:      search path=/opt/hellolib_runtime1/tls/haswell/x86_64:/opt/hellolib_runtime1/tls/haswell:/opt/hellolib_runtime1/tls/x86_64:/opt/hellolib_runtime1/tls:/opt/hellolib_runtime1/haswell/x86_64:/opt/hellolib_runtime1/haswell:/opt/hellolib_runtime1/x86_64:/opt/hellolib_runtime1:tls/haswell/x86_64:tls/haswell:tls/x86_64:tls:haswell/x86_64:haswell:x86_64:               (LD_LIBRARY_PATH)
...
    460445:       trying file=/opt/hellolib_runtime1/libhello.so.1
...
    460445:      search cache=/etc/ld.so.cache
    460445:      search path=/lib/x86_64-linux-gnu/tls/haswell/x86_64:/lib/x86_64-linux-gnu/tls/haswell:/lib/x86_64-linux-gnu/tls/x86_64:/lib/x86_64-linux-gnu/tls:/lib/x86_64-linux-gnu/haswell/x86_64:/lib/x86_64-linux-gnu/haswell:/lib/x86_64-linux-gnu/x86_64:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls/haswell/x86_64:/usr/lib/x86_64-linux-gnu/tls/haswell:/usr/lib/x86_64-linux-gnu/tls/x86_64:/usr/lib/x86_64-linux-gnu/tls:/usr/lib/x86_64-linux-gnu/haswell/x86_64:/usr/lib/x86_64-linux-gnu/haswell:/usr/lib/x86_64-linux-gnu/x86_64:/usr/lib/x86_64-linux-gnu:/lib/tls/haswell/x86_64:/lib/tls/haswell:/lib/tls/x86_64:/lib/tls:/lib/haswell/x86_64:/lib/haswell:/lib/x86_64:/lib:/usr/lib/tls/haswell/x86_64:/usr/lib/tls/haswell:/usr/lib/tls/x86_64:/usr/lib/tls:/usr/lib/haswell/x86_64:/usr/lib/haswell:/usr/lib/x86_64:/usr/lib            (system search path)
...
    460445:       trying file=/lib/libhello.so.1

可以看出动态链接器依次搜索了:RPATH指定的路径,LD_LIBRARY_PATH记录的路径,ld.so.cache中记录的路径,默认路径。

因此验证了上述的路径搜索优先级的顺序。

附录

库文件源码

//file: libhello.cpp
#include <iostream>
void hello()
{
    std::cout << "Hello from the 1.1.0 library!" << std::endl;
}

主程序源码

//file: main.cpp
extern void hello();
int main()
{
    hello();
    return 0;
}

makefile

# file: makefile
CXX = g++
CXXFLAGS = -fPIC
LDFLAGS = -shared
DEBUG_MODE ?= 0
 
ifeq ($(DEBUG_MODE),1)
    DEBUG_OPTS = -v -Wl,--verbose
endif
 
all: lib/libhello.so.1.1.0 obj/main.o
 
lib/libhello.so.1.1.0: libhello.cpp
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -Wl,-soname,libhello.so.1
 
obj/main.o: main.cpp
	$(CXX) -c -o $@ $^
 
# 仅使用-L
hello: obj/main.o
	mkdir -p /opt/hellolib_compile
	cp ./lib/libhello.so.1.1.0 /opt/hellolib_compile
	ln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.so
	$(CXX) $(DEBUG_OPTS) -o $@ $^ -L/opt/hellolib_compile -lhello

hello_rpath: obj/main.o
	mkdir -p /opt/hellolib_compile
	cp ./lib/libhello.so.1.1.0 /opt/hellolib_compile
	ln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.so
	mkdir -p /opt/hellolib_runtime
	cp ./lib/libhello.so.1.1.0 /opt/hellolib_runtime
	ln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1
	$(CXX) $(DEBUG_OPTS) -o $@ $^ -Wl,--disable-new-dtags,-rpath,/opt/hellolib_runtime -L/opt/hellolib_compile -lhello

hello_runpath: obj/main.o
	mkdir -p /opt/hellolib_compile
	cp ./lib/libhello.so.1.1.0 /opt/hellolib_compile
	ln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.so
	mkdir -p /opt/hellolib_runtime
	cp ./lib/libhello.so.1.1.0 /opt/hellolib_runtime
	ln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1
	$(CXX) $(DEBUG_OPTS) -o $@ $^ -Wl,--enable-new-dtags,-rpath,/opt/hellolib_runtime -L/opt/hellolib_compile -lhello
 
clean:
	rm -f ./lib/* ./obj/* ./hello*
	rm -f /usr/lib/libhello*
	rm -rf /opt/hellolib_compile*
	rm -rf /opt/hellolib_runtime*

.PHONY: clean hello hello_rpath hello_runpath

脚本

run_none

#!/bin/bash

make
make hello

LD_DEBUG=libs ./hello

make clean

run_default

#!/bin/bash

make
make hello

cp ./lib/libhello.so.1.1.0 /usr/lib
ln -sf /usr/lib/libhello.so.1.1.0 /usr/lib/libhello.so.1

LD_DEBUG=libs ./hello

rm -f /usr/lib/libhello.so*
make clean

run_ld_so_cache

#!/bin/bash

make
make hello

mkdir -p /opt/hellolib_runtime
cp ./lib/libhello.so.1.1.0 /opt/hellolib_runtime
ln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1

echo "/opt/hellolib_runtime" > /etc/ld.so.conf.d/libhello.conf
ldconfig

LD_DEBUG=libs ./hello

rm -rf /opt/hellolib_runtime*
rm -f /etc/ld.so.conf.d/libhello.conf
ldconfig
make clean

run_runpath

#!/bin/bash

make
make hello_runpath

LD_DEBUG=libs ./hello_runpath

make clean

run_ld_library_path

#!/bin/bash

make
make hello

mkdir -p /opt/hellolib_runtime
cp ./lib/libhello.so.1.1.0 /opt/hellolib_runtime
ln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1

export LD_LIBRARY_PATH=/opt/hellolib_runtime:$LD_LIBRARY_PATH

LD_DEBUG=libs ./hello

rm -rf /opt/hellolib_runtime*
make clean

run_rpath

#!/bin/bash

make
make hello_rpath

LD_DEBUG=libs ./hello_rpath

make clean

run_cmp_all

#!/bin/bash

make
make hello_rpath
rm -rf /opt/hellolib_runtime/*

mkdir -p /opt/hellolib_runtime1
export LD_LIBRARY_PATH=/opt/hellolib_runtime1:$LD_LIBRARY_PATH

mkdir -p /opt/hellolib_runtime2
echo "/opt/hellolib_runtime2" > /etc/ld.so.conf.d/libhello.conf
ldconfig

cp ./lib/libhello.so.1.1.0 /usr/lib
ln -sf /usr/lib/libhello.so.1.1.0 /usr/lib/libhello.so.1

LD_DEBUG=libs ./hello_rpath

rm -f /usr/lib/libhello.so*
rm -f /etc/ld.so.conf.d/libhello.conf
ldconfig
rm -rf /opt/hellolib_runtime*
make clean
From:https://www.cnblogs.com/paw5zx/p/18337485
本文地址: http://www.shuzixingkong.net/article/678
0评论
提交 加载更多评论
其他文章 LangChain的LCEL和Runnable你搞懂了吗
LangChain的LCEL估计行业内的朋友都听过,但是LCEL里的RunnablePassthrough、RunnableParallel、RunnableBranch、RunnableLambda又是什么意思?什么场景下用?
LangChain的LCEL和Runnable你搞懂了吗 LangChain的LCEL和Runnable你搞懂了吗 LangChain的LCEL和Runnable你搞懂了吗
Jenkins 配置即代码(Configuration as Code)详解
1、概述 在《Centos7下安装配置最新版本Jenkins(2.452.3)》这篇博文中讲解了如何安装Jenkins,虽然在安装Jenkins时安装了一些必备的推荐插件,但在企业环境中使用Jenkins之前,我们仍需完成一系列手动配置工作,如配置 System Configuration、Secu
Jenkins 配置即代码(Configuration as Code)详解 Jenkins 配置即代码(Configuration as Code)详解 Jenkins 配置即代码(Configuration as Code)详解
Vue Hook 封装通用型表格
一、创建通用型表格的需求 实现一个通用型表格组件,具备以下功能: 动态列配置。 分页功能。 排序功能。 可扩展的行操作功能。 二、设计通用型表格组件 首先,需要设计一个基础的表格组件,它接受列配置、数据和分页信息等参数。 1. 创建 useTable Hook 在 src/hooks 目录下创建 u
在Python中使用sqlalchemy来操作数据库的几个小总结
在探索使用 FastAPI, SQLAlchemy, Pydantic,Redis, JWT 构建的项目的时候,其中数据库访问采用SQLAlchemy,并采用异步方式。数据库操作和控制器操作,采用基类继承的方式减少重复代码,提高代码复用性。在这个过程中设计接口和测试的时候,对一些问题进行跟踪解决,并
架构演化学习思考(3)
架构演化学习思考(3) 接上一篇我们继续对命令模式进行学习。 在这节内容中,我们聊一下经典的命令模式,还记得上一篇文章开头我们实现的简单的命令模式吗?来看代码,非常简单易解。 public interface ICommand { void Execute(); } public class Pla
架构演化学习思考(3)
P5665 [CSP-S2019] 划分
讲解 P5665 [CSP-S2019] 划分。 由朴素 dp 入手,先用二分优化,然后用走指针优化,之后注意到单调性,将状态数压缩,然后使用单调队列优化转移。
如何通过PowerShell批量修改O365用户的office phone属性值
我的博客园:https://www.cnblogs.com/CQman/ 如何通过PowerShell批量修改O365用户的office phone属性值? 需求信息: 组织中的O365用户在创建时,已手动录入了办公电话(Office phone),现在需要在办公电话前面加上统一的数字,如“0571
如何通过PowerShell批量修改O365用户的office phone属性值 如何通过PowerShell批量修改O365用户的office phone属性值 如何通过PowerShell批量修改O365用户的office phone属性值
前端使用 Konva 实现可视化设计器(19)- 连接线 - 直线、折线
本章响应小伙伴的反馈,除了算法自动画连接线(仍需优化完善),实现了可以手动绘制直线、折线连接线功能。
前端使用 Konva 实现可视化设计器(19)- 连接线 - 直线、折线 前端使用 Konva 实现可视化设计器(19)- 连接线 - 直线、折线 前端使用 Konva 实现可视化设计器(19)- 连接线 - 直线、折线