首页 > 解决方案 > LD_PRELOAD='/something/$LIB/libmyx.so' myprogram 在不同条件下如何展开?

问题描述

我在查找正确的库时遇到错误

ERROR: ld.so: object '/ws/sarvi-sjc/cel8root/$LIB/libwisktrack.so' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS32): ignored.

我的库设置如下

bash-4.4$ ls -alR /ws/sarvi-sjc/cel8root/
/ws/sarvi-sjc/cel8root/:
total 36
drwxr-xr-x.  6 sarvi eng  4096 Sep 23 22:46 .
drwxr-xr-x. 88 sarvi eng 16384 Sep 23 22:12 ..
drwxr-xr-x.  2 sarvi eng  4096 Sep 23 22:56 lib
drwxr-xr-x.  2 sarvi eng  4096 Sep 23 22:56 lib32
drwxr-xr-x.  2 sarvi eng  4096 Sep 23 22:56 lib64

/ws/sarvi-sjc/cel8root/lib:
total 29452
drwxr-xr-x. 2 sarvi eng     4096 Sep 23 22:56 .
drwxr-xr-x. 6 sarvi eng     4096 Sep 23 22:46 ..
-rwxrwxrwx. 1 sarvi eng 30024352 Sep 23 22:38 libwisktrack.so

/ws/sarvi-sjc/cel8root/lib32:
total 29428
drwxr-xr-x. 2 sarvi eng     4096 Sep 23 22:56 .
drwxr-xr-x. 6 sarvi eng     4096 Sep 23 22:46 ..
-rwxrwxrwx. 1 sarvi eng 30003020 Sep 23 22:56 libwisktrack.so

/ws/sarvi-sjc/cel8root/lib64:
total 28792
drwxr-xr-x. 2 sarvi eng     4096 Sep 23 22:56 .
drwxr-xr-x. 6 sarvi eng     4096 Sep 23 22:46 ..
-rwxrwxrwx. 1 sarvi eng 29349680 Sep 23 22:56 libwisktrack.so
bash-4.4$ 
bash-4.4$ file /ws/sarvi-sjc/cel8root/lib/libwisktrack.so 
/ws/sarvi-sjc/cel8root/lib/libwisktrack.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=c38570777cfc99005a4693f68adf1014b7d647fd, with debug_info, not stripped, too many notes (256)
bash-4.4$ file /ws/sarvi-sjc/cel8root/lib32/libwisktrack.so 
/ws/sarvi-sjc/cel8root/lib32/libwisktrack.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=be90026de8fad17a99582991f1983e321a17ea87, with debug_info, not stripped, too many notes (256)
bash-4.4$ file /ws/sarvi-sjc/cel8root/lib64/libwisktrack.so 
/ws/sarvi-sjc/cel8root/lib64/libwisktrack.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=223ca6c07b59b940355a3ca8299df8f68743c22a, with debug_info, not stripped, too many notes (256)
bash-4.4$ 

据我所知,我的 libwisktrack.so 的 32 位和 64 位版本的库安装已正确安装。

当我编译自己的 testprog32 和 testprog64 时,它们似乎正确加载了各自的库

bash-4.4$ file tests/testprog32 
tests/testprog32: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=a9db0c61d648adf715a79c5cfb7b06f6a128b90e, not stripped
bash-4.4$ LD_PRELOAD='/ws/sarvi-sjc/cel8root/$LIB/libwisktrack.so' tests/testprog32
Constructor: 92729
testprog running......
Command: takes one argument. Which system call to run
Options: readlink, vprintf, printf, open, fopen, creat

bash-4.4$ file tests/testprog64
tests/testprog64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=fbb14df720b2997c56995c98b2ad28734f554bb9, not stripped
bash-4.4$ LD_PRELOAD='/ws/sarvi-sjc/cel8root/$LIB/libwisktrack.so' tests/testprog64
Constructor: 92349
testprog running......
Command: takes one argument. Which system call to run
Options: readlink, vprintf, printf, open, fopen, creat

然而这个程序很难找到正确的库,并且似乎选择了错误的库

bash-4.4$ file /nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/usr/bin/protoc
/nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/usr/bin/protoc: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/lib/ld-linux-x86-64.so.2, BuildID[sha1]=f52e087673df429ad5849877b08d89b080e934c2, for GNU/Linux 2.6.32, stripped
bash-4.4$ 

bash-4.4$ LD_PRELOAD='/ws/sarvi-sjc/cel8root/$LIB/libwisktrack.so' /nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/usr/bin/protoc
ERROR: ld.so: object '/ws/sarvi-sjc/cel8root/$LIB/libwisktrack.so' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS32): ignored.
Usage: /nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/usr/bin/protoc [OPTION] PROTO_FILES

我在这里想念什么?

我注意到 64 位 testprog64 和失败的 64 位程序之间的一个区别是

interpreter /lib64/ld-linux-x86-64.so.2
Vs 
interpreter /nobackup/sarvi/xrwisktest/binos/linkfarm/host/sdk/sysroots/x86_64-xesdk-linux/lib/ld-linux-x86-64.so.2

不知道这有什么关系

更新: strace 显示它打开以下文件以进行 64 位程序调用,这对我来说没有意义

open("/ws/sarvi-sjc/wisktrack/cel8root/lib/libwisktrack.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

这不应该打开 lib64 或 lib/x86_64 或 lib/x86_64-linux-gnu 路径之一吗?

为了清楚起见,我不想使用 LD_LIBRRY_PATH,因为我遇到了一些程序/脚本明确禁止设置 LD_LIBRARY_PATH 的用例,例如我在 SDK 中找到的“python setup.py install”。

所以我希望在不必使用 LD_LIBRARY_PATH 的情况下完成这项工作。“$LIB” 持续工作是我知道的唯一选择。

我的是 RHEL8 开发主机。遇到问题的特定程序是从为主机编译的 SDK 中提取的,以构建其他交叉编译程序,作为一个非常大的构建系统的艺术。

标签: linux

解决方案


$LIB来自ld.soman 的文档: https ://man7.org/linux/man-pages/man8/ld.so.8.html

   $LIB (or equivalently ${LIB})
          This expands to lib or lib64 depending on the architecture
          (e.g., on x86-64, it expands to lib64 and on x86-32, it
          expands to lib).

这并不完全正确,实际值可能不同。例如,您正在从 OpenEmbedded SDK 运行 nativesdk 二进制文件。正如您所注意到的,SDK 使用自己的ld.so. 没有为多库支持配置它ld.so,它预计只有一个lib目录包含 64 位库。这就是为什么它解析$LIBlib.

您可以在此处查看默认库目录定义 ( BASELIB):http: //git.openembedded.org/openembedded-core/tree/meta/conf/bitbake.conf#n12


推荐阅读