Linux 单个主机上有多个glibc库
我的linux(SLES-8)服务器目前有glibc-2.2.5-235,但我有一个程序不能在这个版本上运行,需要glibc-2.3.3 是否可以在同一台主机上安装多个GLIBC 这是我在旧glibc上运行程序时遇到的错误:Linux 单个主机上有多个glibc库,linux,gcc,glibc,Linux,Gcc,Glibc,我的linux(SLES-8)服务器目前有glibc-2.2.5-235,但我有一个程序不能在这个版本上运行,需要glibc-2.3.3 是否可以在同一台主机上安装多个GLIBC 这是我在旧glibc上运行程序时遇到的错误: ./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./myapp) ./myapp: /lib/i686/libpthread.so.0: version `GLIBC_2.3.
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./myapp)
./myapp: /lib/i686/libpthread.so.0: version `GLIBC_2.3.2' not found (required by ./myapp)
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./libxerces-c.so.27)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by ./libstdc++.so.6)
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./libstdc++.so.6)
因此,我创建了一个名为newglibc的新目录,并在中复制了以下文件:
libpthread.so.0
libm.so.6
libc.so.6
ld-2.3.3.so
ld-linux.so.2 -> ld-2.3.3.so
及
但我有一个错误:
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libpthread.so.0)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by libstdc++.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libm.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by ./newglibc/libc.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libc.so.6)
因此,它们似乎仍在链接到
/lib
,而不是从我放置它们的位置拾取。如果仔细查看第二个输出,您可以看到库的新位置已被使用。可能仍然缺少glibc的一部分库
我还认为,您的程序使用的所有库都应该针对该版本的glibc进行编译。如果您可以访问该程序的源代码,则新编译似乎是最佳解决方案。使用LD_PRELOAD:
将您的库放在man lib目录之外的某个位置,然后运行:
LD_PRELOAD='mylibc.so anotherlib.so' program
请参阅:在同一系统上很可能有多个版本的glibc(我们每天都这样做) 但是,您需要知道glibc由许多部分(200多个共享库)组成,所有这些部分都必须匹配。其中一个是ld linux.so.2,它必须与libc.so.6匹配,否则您将看到所看到的错误 ld linux.so.2的绝对路径在链接时硬编码到可执行文件中,链接完成后无法轻易更改(更新:可以使用;请参见下文) 要构建可与新glibc一起使用的可执行文件,请执行以下操作:
g++ main.o -o myapp ... \
-Wl,--rpath=/path/to/newglibc \
-Wl,--dynamic-linker=/path/to/newglibc/ld-linux.so.2
-rpath
链接器选项将使运行时加载程序在/path/to/newglibc
中搜索库(这样您就不必在运行前设置LD\u LIBRARY\u path
),而-dynamic linker
选项将“烘焙”路径以更正LD linux.so.2
如果无法重新链接
myapp
应用程序(例如,因为它是第三方二进制文件),并非所有内容都会丢失,但它会变得更加棘手。一种解决方案是为其设置适当的chroot
环境。另一种可能性是使用和。更新:或者你可以使用.你能考虑使用nix?< /p>
Nix支持多用户包管理:多个用户可以共享一个包
公共Nix存储安全,无需具有根权限即可
安装软件,并可以安装和使用不同版本的
包裹
“雇佣俄语”是最好的答案之一,我认为所有其他建议的答案可能都不起作用。原因很简单,因为当应用程序第一次创建时,它需要的所有API都在编译时解析。使用“ldd”可以查看所有静态链接的依赖项:
ldd /usr/lib/firefox/firefox
linux-vdso.so.1 => (0x00007ffd5c5f0000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f727e708000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f727e500000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f727e1f8000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f727def0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f727db28000)
/lib64/ld-linux-x86-64.so.2 (0x00007f727eb78000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f727d910000)
ldd ldc2-1.5.0-linux-x86_64/bin/ldc2
linux-vdso.so.1 => (0x00007ffebad3f000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f965f597000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f965f378000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f965f15b000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f965ef57000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f965ec01000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f965e9ea000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f965e60a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f965f79f000)
但在运行时,firefox还将加载许多其他动态库,例如(对于firefox),加载了许多带有“glib”标签的库(即使静态链接的库没有):
很多时候,您可以看到一个版本的名称被软链接到另一个版本。例如:
lrwxrwxrwx 1 root root 23 Dec 21 2014 libdbus-glib-1.so.2 -> libdbus-glib-1.so.2.2.2
-rw-r--r-- 1 root root 160832 Mar 1 2013 libdbus-glib-1.so.2.2.2
因此,这意味着一个系统中存在不同版本的“库”——这不是问题,因为它是同一个文件,并且当应用程序具有多个版本依赖关系时,它将提供兼容性
因此,在系统级别上,所有库几乎是相互依赖的,仅仅通过操纵LD_PRELOAD或LD_LIBRARY_PATH来更改库的加载优先级是没有帮助的-即使它可以加载,运行时它仍然可能崩溃
最好的替代方法是chroot(ER简要地提到):但为此,您需要重新创建整个环境,其中是原始的二进制执行-通常从/lib、/usr/lib/、/usr/lib/x86等开始。您可以使用“Buildroot”或YoctoProject,也可以只使用现有发行版环境中的tar。(如Fedora/Suse等)。这个问题是老问题,其他答案都是老问题。“Employed Russian”的答案很好,内容丰富,但它只有在你有源代码的情况下才有效。如果你不这样做,那么当时的选择是非常棘手的。幸运的是,现在我们有了一个解决这个问题的简单方法(正如他在一个回复中所评论的),使用。你所要做的就是:
$ ./patchelf --set-interpreter /path/to/newglibc/ld-linux.so.2 --set-rpath /path/to/newglibc/ myapp
然后,您可以执行您的文件:
$ ./myapp
谢天谢地,无需chroot
或手动编辑二进制文件。但是,如果您不确定自己在做什么,请记住在修补二进制文件之前备份它,因为它会修改二进制文件。修补后,无法将旧路径恢复到解释器/rpath。如果它不起作用,你将不得不继续修补它,直到你找到真正起作用的路径。。。嗯,这不一定是一个反复试验的过程。例如,在OP的示例中,他需要GLIBC_2.3
,因此您可以使用字符串轻松找到提供该版本的库:
$ strings /lib/i686/libc.so.6 | grep GLIBC_2.3
$ strings /path/to/newglib/libc.so.6 | grep GLIBC_2.3
理论上,第一个grep将是空的,因为系统libc没有他想要的版本,第二个grep应该输出GLIBC_2.3,因为它有myapp
正在使用的版本,所以我们知道我们可以使用该路径patchelf
二进制文件。如果您遇到了分段错误,请阅读结尾处的注释
当您尝试在linux中运行二进制文件时,二进制文件会尝试加载链接器,然后加载库,它们都应该位于正确的路径和/或位置。如果您的问题是链接器,并且您想找出二进制文件要查找的路径,可以使用以下命令:
$ readelf -l myapp | grep interpreter
[Requesting program interpreter: /lib/ld-linux.so.2]
如果您的问题是LIB,则提供所使用LIB的命令有:
$ readelf -d myapp | grep Shared
$ ldd myapp
这将列出二进制需要的lib,但您可能已经知道有问题的lib,因为它们已经产生了错误,就像OP的情况一样
“patchelf”适用于在运行程序时可能遇到的许多不同问题,与这两个问题有关。例如,如果您得到:ELF文件OS ABI inval
$ readelf -d myapp | grep Shared
$ ldd myapp
$ ldd myapp
./myapp: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./myapp)
./myapp: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./myapp)
linux-vdso.so.1 => (0x00007fffb167c000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9a9aad2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9a9a8ce000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9a9a6af000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9a9a3ab000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9a99fe6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9a9adeb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9a99dcf000)
ldc2-1.5.0-linux-x86_64/bin/ldc2: /lib64/libc.so.6: version `GLIBC_2.15' not found (required by ldc2-1.5.0-linux-x86_64/bin/ldc2)
ldc2-1.5.0-linux-x86_64/bin/ldc2: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ldc2-1.5.0-linux-x86_64/bin/ldc2)
ldd ldc2-1.5.0-linux-x86_64/bin/ldc2
linux-vdso.so.1 => (0x00007ffebad3f000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f965f597000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f965f378000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f965f15b000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f965ef57000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f965ec01000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f965e9ea000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f965e60a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f965f79f000)
/bin/ldc2
/mylibs/ld-linux-x86-64.so.2 /bin/ldc2
/mylibs/ld-linux-x86-64.so.2 --library-path /mylibs /bin/ldc2
as root
the files (*-2.15.so) already exist
/glibc-2.19/ld-linux.so.2 -> /glibc-2.19/i386-linux-gnu/ld-2.19.so
/glibc-2.19/i386-linux-gnu/libc.so.6 -> libc-2.19.so
/glibc-2.19/i386-linux-gnu/libdl.so.2 -> libdl-2.19.so
/glibc-2.19/i386-linux-gnu/libpthread.so.0 -> libpthread-2.19.so
/glibc-2.15/ld-linux.so.2 -> (/glibc-2.15/i386-linux-gnu/ld-2.15.so)
/glibc-2.15/i386-linux-gnu/libc.so.6 -> (libc-2.15.so)
/glibc-2.15/i386-linux-gnu/libdl.so.2 -> (libdl-2.15.so)
/glibc-2.15/i386-linux-gnu/libpthread.so.0 -> (libpthread-2.15.so)
#!/bin/sh
sudo cp -r /glibc-2.19/* /lib
/path/to/the/browser &
sleep 1
sudo cp -r /glibc-2.15/* /lib
sudo rm -r /lib/i386-linux-gnu/*-2.19.so
export glibc_install="$(pwd)/glibc/build/install"
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28
mkdir build
cd build
../configure --prefix "$glibc_install"
make -j `nproc`
make install -j `nproc`
#define _GNU_SOURCE
#include <assert.h>
#include <gnu/libc-version.h>
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
atomic_int acnt;
int cnt;
int f(void* thr_data) {
for(int n = 0; n < 1000; ++n) {
++cnt;
++acnt;
}
return 0;
}
int main(int argc, char **argv) {
/* Basic library version check. */
printf("gnu_get_libc_version() = %s\n", gnu_get_libc_version());
/* Exercise thrd_create from -pthread,
* which is not present in glibc 2.27 in Ubuntu 18.04.
* https://stackoverflow.com/questions/56810/how-do-i-start-threads-in-plain-c/52453291#52453291 */
thrd_t thr[10];
for(int n = 0; n < 10; ++n)
thrd_create(&thr[n], f, NULL);
for(int n = 0; n < 10; ++n)
thrd_join(thr[n], NULL);
printf("The atomic counter is %u\n", acnt);
printf("The non-atomic counter is %u\n", cnt);
}
#!/usr/bin/env bash
set -eux
gcc \
-L "${glibc_install}/lib" \
-I "${glibc_install}/include" \
-Wl,--rpath="${glibc_install}/lib" \
-Wl,--dynamic-linker="${glibc_install}/lib/ld-linux-x86-64.so.2" \
-std=c11 \
-o test_glibc.out \
-v \
test_glibc.c \
-pthread \
;
ldd ./test_glibc.out
./test_glibc.out
gnu_get_libc_version() = 2.28
The atomic counter is 10000
The non-atomic counter is 8674
cannot find /home/ciro/glibc/build/install/lib/libc.so.6 inside /home/ciro/glibc/build/install
+ ldd test_glibc.out
linux-vdso.so.1 (0x00007ffe4bfd3000)
libpthread.so.0 => /home/ciro/glibc/build/install/lib/libpthread.so.0 (0x00007fc12ed92000)
libc.so.6 => /home/ciro/glibc/build/install/lib/libc.so.6 (0x00007fc12e9dc000)
/home/ciro/glibc/build/install/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007fc12f1b3000)
COLLECT_GCC_OPTIONS=/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crt1.o
diff --git a/nptl/thrd_create.c b/nptl/thrd_create.c
index 113ba0d93e..b00f088abb 100644
--- a/nptl/thrd_create.c
+++ b/nptl/thrd_create.c
@@ -16,11 +16,14 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
+#include <stdio.h>
+
#include "thrd_priv.h"
int
thrd_create (thrd_t *thr, thrd_start_t func, void *arg)
{
+ puts("hacked");
_Static_assert (sizeof (thr) == sizeof (pthread_t),
"sizeof (thr) != sizeof (pthread_t)");
cd glibc/build
make -j `nproc`
make -j `nproc` install
./test_glibc.sh
git clone https://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
git checkout a6580b8e8b55345a5a342b5bd96e42c83e640ac5
export CT_PREFIX="$(pwd)/.build/install"
export PATH="/usr/lib/ccache:${PATH}"
./bootstrap
./configure --enable-local
make -j `nproc`
./ct-ng x86_64-unknown-linux-gnu
./ct-ng menuconfig
env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`
uname -a
4.15.0-34-generic
4.14.71
CT_GLIBC_V_2_27=y
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28
#!/usr/bin/env bash
set -eux
install_dir="${CT_PREFIX}/x86_64-unknown-linux-gnu"
PATH="${PATH}:${install_dir}/bin" \
x86_64-unknown-linux-gnu-gcc \
-Wl,--dynamic-linker="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib/ld-linux-x86-64.so.2" \
-Wl,--rpath="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib" \
-v \
-o test_glibc.out \
test_glibc.c \
-pthread \
;
ldd test_glibc.out
./test_glibc.out
COLLECT_GCC_OPTIONS=/home/ciro/crosstool-ng/.build/install/x86_64-unknown-linux-gnu/bin/../x86_64-unknown-linux-gnu/sysroot/usr/lib/../lib64/crt1.o
env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`
./ct-ng list-steps
Available build steps, in order:
- companion_tools_for_build
- companion_libs_for_build
- binutils_for_build
- companion_tools_for_host
- companion_libs_for_host
- binutils_for_host
- cc_core_pass_1
- kernel_headers
- libc_start_files
- cc_core_pass_2
- libc
- cc_for_build
- cc_for_host
- libc_post_cc
- companion_libs_for_target
- binutils_for_target
- debug
- test_suite
- finish
Use "<step>" as action to execute only that step.
Use "+<step>" as action to execute up to that step.
Use "<step>+" as action to execute from that step onward.
env -u LD_LIBRARY_PATH time ./ct-ng libc+ -j`nproc`
ImportError: /lib64/libc.so.6: version `GLIBC_2.16' not found (required by /home/
mkdir ~/glibc-install; cd ~/glibc-install
wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz
tar -zxvf glibc-2.17.tar.gz
cd glibc-2.17
mkdir build
cd build
../configure --prefix=/home/myself/opt/glibc-2.17 # <-- where you install new glibc
make -j<number of CPU Cores> # You can find your <number of CPU Cores> by using **nproc** command
make install
[myself@nfkd ~]$ patchelf --set-interpreter /home/myself/opt/glibc-2.17/lib/ld-linux-x86-64.so.2 --set-rpath /home/myself/opt/glibc-2.17/lib/ /home/myself/miniconda3/envs/tensorflow/bin/python