【发布时间】:2018-08-12 04:17:50
【问题描述】:
我正在尝试从 chroot 中动态加载库。 上述库依赖于 glibc,其版本与我的主机不同(主机有 2.26,chroot 有 2.23)。
有没有办法做到这一点?
这是我尝试过的:
$ curl http://cdimage.ubuntu.com/ubuntu-base/releases/16.04/release/ubuntu-base-16.04-core-amd64.tar.gz | tar -C ubuntu -xz
#include <unistd.h>
#include <dlfcn.h>
int main(int argc, char *argv[])
{
chdir("ubuntu");
chroot(".");
// 1. This will fail
dlopen("libpthread.so.0", RTLD_NOW);
// 2. This will crash
dlmopen(LM_ID_NEWLM, "libpthread.so.0", RTLD_NOW);
return 0;
}
-
显然失败是因为 glibc 不匹配:
/lib/x86_64-linux-gnu/libpthread.so.0: symbol __libc_dl_error_tsd, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference -
valgrind 的以下输出导致崩溃:
==19923== Process terminating with default action of signal 4 (SIGILL): dumping core ==19923== Illegal opcode at address 0x401D41C ==19923== at 0x401D41C: ??? (in /usr/lib/ld-2.26.so) ==19923== by 0x4013932: dl_open_worker (in /usr/lib/ld-2.26.so) ==19923== by 0x516EB63: _dl_catch_error (in /usr/lib/libc-2.26.so) ==19923== by 0x4013279: _dl_open (in /usr/lib/ld-2.26.so) ==19923== by 0x4E3A8CF: ??? (in /usr/lib/libdl-2.26.so) ==19923== by 0x516EB63: _dl_catch_error (in /usr/lib/libc-2.26.so) ==19923== by 0x4E3A586: ??? (in /usr/lib/libdl-2.26.so) ==19923== by 0x4E3A9B6: dlmopen (in /usr/lib/libdl-2.26.so)
【问题讨论】:
-
你为什么要
dlopen()inglibpthread?而且我认为没有办法,但我几乎可以肯定你有一个X Y Problem,所以请解释一下你的真正目标是什么。 -
您的可执行文件需要您的环境没有的库版本。尝试按原样运行它没有什么意义。此外,dlopen'ing libpthread 并在一开始就没有使用 -pthread 构建的程序中使用它并不是最好的主意,而且无论 glibc 的版本如何,都很有可能使您的程序崩溃。
-
如果有帮助,请将其视为您希望加载的插件包,并且可能具有冲突的依赖项。
libpthread只是为了示例,它确实与任何库都失败。至于环境,chroot下所有的需求和依赖都满足了,问题是如何安全的把它们带入进程的地址空间。
标签: c ld glibc dynamic-loading