在加载第三方库过程中,无法加载到库的问题(使用readelf, patchelf命令)

本文主要是介绍在加载第三方库过程中,无法加载到库的问题(使用readelf, patchelf命令),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

无法加载到库问题

  • 问题及分析过程
  • readelf 命令
  • patchelf命令

问题及分析过程

在开发一个程序过程中,需要加载第三方库iTapTradeAPI, 在CMakeList.txt中已经设置了CMAKE_INSTALL_RPATH,但是发布到生产之后由于目录问题无法加载到libiTapTradeAPI库了

下面时分析的过程图
在这里插入图片描述
从图中可以看出iTapTradeAPI使用的相对路径,与其他依赖库不同, 然后查看依赖库iTapTradeAPI的信息
在这里插入图片描述
可以看到使用的RPATH时 .:/RIGIN路径
刚开始想到的时是否把相对路径去掉, 使用如下命令:

patchelf --remove-rpath <path-to-elf>
eg: patchelf --remove-rpath libiTapTradeAPI.so

去掉之后还是不行,对比项目中的其他动态库,发现这个动态库少了一个选项SONAME, 然后使用命令

patchelf --set-soname libiTapTradeAPI.so ./libiTapTradeAPI.so

进行设置,这样之后就可以了

另外假如在生产上想先快速修复:还可以使用

patchelf --replace-needed LIBRARY NEW_LIBRARY

来做应急修复,替换依赖库的路径

readelf 命令

readelf是一个用于查看可执行文件和共享库的信息的命令行工具。它可以显示二进制文件的各种部分,包括头部信息、节(section)信息、符号表、动态链接信息等

readelf -h
readelf: Warning: Nothing to do.
Usage: readelf <option(s)> elf-file(s)Display information about the contents of ELF format filesOptions are:-a --all               Equivalent to: -h -l -S -s -r -d -V -A -I	//  显示所有信息,相当于 -h -l -S -s -r -d -V -A -I-h --file-header       Display the ELF file header			//  显示ELF文件头信息-l --program-headers   Display the program headers	// 显示程序头信息--segments          An alias for --program-headers	// --program-headers的别名-S --section-headers   Display the sections' header	// 显示节头信息--sections          An alias for --section-headers	// --section-headers的别名-g --section-groups    Display the section groups	// 显示节组信息-t --section-details   Display the section details	// 显示节的详细信息-e --headers           Equivalent to: -h -l -S	// 相当于 -h -l -S-s --syms              Display the symbol table	//  显示符号表--symbols           An alias for --syms	// --syms的别名--dyn-syms             Display the dynamic symbol table	// 显示动态符号表-n --notes             Display the core notes (if present)	// 显示核心注释(如果存在)-r --relocs            Display the relocations (if present)	// 显示重定位信息(如果存在)-u --unwind            Display the unwind info (if present)	// 显示展开信息(如果存在)-d --dynamic           Display the dynamic section (if present)	//  显示动态节信息(如果存在)-V --version-info      Display the version sections (if present)	// 显示版本节信息(如果存在)-A --arch-specific     Display architecture specific information (if any)	// 显示特定于体系结构的信息(如果有)-c --archive-index     Display the symbol/file index in an archive	// 在存档中显示符号/文件索引-D --use-dynamic       Use the dynamic section info when displaying symbols	// 显示符号时使用动态节信息-x --hex-dump=<number|name>Dump the contents of section <number|name> as bytes // 以字节形式显示节的内容-p --string-dump=<number|name>Dump the contents of section <number|name> as strings //  以字符串形式显示节的内容-R --relocated-dump=<number|name>Dump the contents of section <number|name> as relocated bytes // 以重定位后的字节形式显示节的内容-z --decompress        Decompress section before dumping it	// 在显示节内容之前解压缩节-w[lLiaprmfFsoRt] or--debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges,=pubtypes,=gdb_index,=trace_info,=trace_abbrev,=trace_aranges,=addr,=cu_index]Display the contents of DWARF2 debug sections	// 显示DWARF2调试节的内容--dwarf-depth=N        Do not display DIEs at depth N or greater	// 不显示深度大于或等于N的DIEs--dwarf-start=N        Display DIEs starting with N, at the same depthor deeper	// 从深度为N的DIE开始显示-I --histogram         Display histogram of bucket list lengths	// 显示桶列表长度的直方图-W --wide              Allow output width to exceed 80 characters	// 允许输出宽度超过80个字符@<file>                Read options from <file>		// 从文件中读取选项-H --help              Display this information-v --version           Display the version number of readelf
  1. 查看文件头部信息:
readelf -h executable
  1. 查看节(section)信息:
readelf -S executable
  1. 查看符号表:
readelf -s executable
  1. 查看动态链接信息:
readelf -d executable
  1. 查看库依赖:
readelf -d executable | grep NEEDED

patchelf命令

patchelf是一个用于修改可执行文件和共享库属性的工具。它可以用来修改运行时搜索路径(rpath)、修改依赖库路径、修改库版本等。

1. 查看文件属性:

patchelf --print-interpreter executable
patchelf --print-rpath executable
patchelf --print-needed executable

2.  将可执行文件的运行时搜索路径(rpath)修改为 "path/to/library-dir"。运行时搜索路径用于指定程序在运行时查找共享库的路径。通过修改运行时搜索路径,您可以控制程序在运行时加载特定的共享库。

patchelf --set-rpath path/to/library-dir executable

3. 将可执行文件的动态链接器(interpreter)路径修改为 "path/to/ld-linux.so.2"。动态链接器负责在程序启动时加载共享库并解析符号。通过修改动态链接器路径,您可以指定程序在运行时使用特定的动态链接器。

patchelf --set-interpreter path/to/ld-linux.so.2 executable

4. 将共享库的 soname 修改为 "new-soname.so.1"。Soname 是共享库的标识符,用于在运行时确定库的版本。通过修改 soname,您可以控制共享库的版本和依赖关系。

patchelf --set-soname new-soname.so.1 library.so

patchelf -h
syntax: patchelf
[–set-interpreter FILENAME] // 设置动态库解析器
[–page-size SIZE] // 设置页大小
[–print-interpreter]
[–print-soname] Prints ‘DT_SONAME’ entry of .dynamic section. Raises an error if DT_SONAME doesn’t exist
[–set-soname SONAME] Sets ‘DT_SONAME’ entry to SONAME. // 设置名字
[–set-rpath RPATH] // 设置 rpath
[–remove-rpath] // 删除 rpath
[–shrink-rpath] // 收缩rpath
[–print-rpath] // 打印 rpath
[–force-rpath] // 强制使用 rpath
[–add-needed LIBRARY] // 添加需要的动态库
[–remove-needed LIBRARY] // 删除需要的动态库
[–replace-needed LIBRARY NEW_LIBRARY] // 替换需要的动态库
[–print-needed] // 打印帮助信息
[–no-default-lib] // 不链接默认的动态库
[–debug]
[–version]
FILENAME


这篇关于在加载第三方库过程中,无法加载到库的问题(使用readelf, patchelf命令)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/595155

相关文章

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化