背景:第三方so依赖glibc2.14版本,如何在不升级cenos 6.2自带的gblic2.12情况下,运行so?
结论: patchelf开源工具,可直接增加、删除、替换 ELF 文件依赖的库文件,通过patchelf工具的add-needed参数来添加对动态库的依赖,让程序优先加载高版本库
分析so
通过ldd命令,发现在cenos 6.2上,第三方so依赖/lib64/libc.so.6: version `GLIBC_2.14'
[root@localhost lee]# ldd libTaSESDK.so
ldd: warning: you do not have execution permission for `./libTaSESDK.so'
./libTaSESDK.so: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./libTaSESDK.so)
linux-vdso.so.1 => (0x00007fff675ff000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fdc27a17000)
libm.so.6 => /lib64/libm.so.6 (0x00007fdc27793000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fdc2757c000)
libc.so.6 => /lib64/libc.so.6 (0x00007fdc271db000)
/lib64/ld-linux-x86-64.so.2 (0x0000003810600000)
而当前系统libc.so.6 glibc最高为2.12
[root@localhost lee]# strings /lib64/libc.so.6 |grep GLIBC
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_PRIVATE
patchelf编译安装
patchelf在高版本的cenos上安装(在cenos6.2下安装有问题)
下载地址https://github.com/NixOS/patchelf/releases
下载好zip包后,通过以下命令安装patchelf
./bootstrap.sh
./configure
make
sudo make install
在高版本cenos上执行命令
执行patchelf --add-needed /lib/newlibc.so.6 libTaSESDK.so,即对第三方libTaSESDK.so修改elf,此时完成对so的修改,添加高版本libc.so的依赖
在cenos6.2验证
将修改后的libTaSESDK.so拷贝到cenos6.2
执行ldd命令
[root@localhost lee]# ldd libTaSESDK.so
ldd: warning: you do not have execution permission for `./libTaSESDK.so'
./libTaSESDK.so: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./libTaSESDK.so)
linux-vdso.so.1 => (0x00007fff8cdff000)
/lib/newlibc.so.6 => not found
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f1c626eb000)
libm.so.6 => /lib64/libm.so.6 (0x00007f1c62466000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f1c62250000)
libc.so.6 => /lib64/libc.so.6 (0x00007f1c61eaf000)
/lib64/ld-linux-x86-64.so.2 (0x0000003810600000)
此时发现 /lib/newlibc.so.6 => not found 已经成功定位到新的libc.so.6包
从高版本cenos拷贝libc.so.6到当前系统
找到一个高版本的cenos,确认glibc为2.14以上后,拷贝/lib64/libc.so.6到当前系统的lib下重命名为newlibc.so.6
最终
当lib 下有新版本的newlibc.so.6 后
再次执行ldd
[root@localhost lee]# ldd libTaSESDK.so
ldd: warning: you do not have execution permission for `./libTaSESDK.so'
linux-vdso.so.1 => (0x00007fffbb324000)
/lib/newlibc.so.6 (0x00007f30651cc000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f3064eb9000)
libm.so.6 => /lib64/libm.so.6 (0x00007f3064c34000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f3064a1e000)
/lib64/ld-linux-x86-64.so.2 (0x0000003810600000)
发现so已经没有问题