前言
《共享库链接和加载时的路径搜索优先级》中提到,共享库在运行期加载时的路径搜索优先级为: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.cppextern void hello();int main(){ hello(); return 0;}
makefile
# file: makefileCXX = g++CXXFLAGS = -fPICLDFLAGS = -sharedDEBUG_MODE ?= 0 ifeq ($(DEBUG_MODE),1) DEBUG_OPTS = -v -Wl,--verboseendif 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 $@ $^ # 仅使用-Lhello: obj/main.omkdir -p /opt/hellolib_compilecp ./lib/libhello.so.1.1.0 /opt/hellolib_compileln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.so$(CXX) $(DEBUG_OPTS) -o $@ $^ -L/opt/hellolib_compile -lhellohello_rpath: obj/main.omkdir -p /opt/hellolib_compilecp ./lib/libhello.so.1.1.0 /opt/hellolib_compileln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.somkdir -p /opt/hellolib_runtimecp ./lib/libhello.so.1.1.0 /opt/hellolib_runtimeln -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 -lhellohello_runpath: obj/main.omkdir -p /opt/hellolib_compilecp ./lib/libhello.so.1.1.0 /opt/hellolib_compileln -sf /opt/hellolib_compile/libhello.so.1.1.0 /opt/hellolib_compile/libhello.somkdir -p /opt/hellolib_runtimecp ./lib/libhello.so.1.1.0 /opt/hellolib_runtimeln -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/bashmakemake helloLD_DEBUG=libs ./hellomake clean
run_default
#!/bin/bashmakemake hellocp ./lib/libhello.so.1.1.0 /usr/libln -sf /usr/lib/libhello.so.1.1.0 /usr/lib/libhello.so.1LD_DEBUG=libs ./hellorm -f /usr/lib/libhello.so*make clean
run_ld_so_cache
#!/bin/bashmakemake hellomkdir -p /opt/hellolib_runtimecp ./lib/libhello.so.1.1.0 /opt/hellolib_runtimeln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1echo"/opt/hellolib_runtime" > /etc/ld.so.conf.d/libhello.confldconfigLD_DEBUG=libs ./hellorm -rf /opt/hellolib_runtime*rm -f /etc/ld.so.conf.d/libhello.confldconfigmake clean
run_runpath
#!/bin/bashmakemake hello_runpathLD_DEBUG=libs ./hello_runpathmake clean
run_ld_library_path
#!/bin/bashmakemake hellomkdir -p /opt/hellolib_runtimecp ./lib/libhello.so.1.1.0 /opt/hellolib_runtimeln -sf /opt/hellolib_runtime/libhello.so.1.1.0 /opt/hellolib_runtime/libhello.so.1export LD_LIBRARY_PATH=/opt/hellolib_runtime:$LD_LIBRARY_PATHLD_DEBUG=libs ./hellorm -rf /opt/hellolib_runtime*make clean
run_rpath
#!/bin/bashmakemake hello_rpathLD_DEBUG=libs ./hello_rpathmake clean
run_cmp_all
#!/bin/bashmakemake hello_rpathrm -rf /opt/hellolib_runtime/*mkdir -p /opt/hellolib_runtime1export LD_LIBRARY_PATH=/opt/hellolib_runtime1:$LD_LIBRARY_PATHmkdir -p /opt/hellolib_runtime2echo"/opt/hellolib_runtime2" > /etc/ld.so.conf.d/libhello.confldconfigcp ./lib/libhello.so.1.1.0 /usr/libln -sf /usr/lib/libhello.so.1.1.0 /usr/lib/libhello.so.1LD_DEBUG=libs ./hello_rpathrm -f /usr/lib/libhello.so*rm -f /etc/ld.so.conf.d/libhello.confldconfigrm -rf /opt/hellolib_runtime*make clean