我有一个动态库,我加载使用dlopen()然后卸载使用dlclose();
如果我不包含任何目标c代码dlopen()需要一个dlclose()预期行为的调用.但是当我将任何目标c代码包含在目标中时,我遇到的问题是我需要对dlclose()加载的库进行两次调用才能卸载.
这是预期的行为吗?我该如何解决?
我正在开发一个项目,该项目需要将基于 Rust 的插件(共享对象)任意加载/卸载到隔离的动态库命名空间中。
我用来dlmopen(LM_ID_NEWLM, "rust-plugin.so", RTLD_LAZY)为共享对象创建一个新的命名空间。当不再需要共享对象时,我调用dlclose().
不幸的是,我发现即使我dlclose()一次只有一个共享对象有效,在使用dlmopen()14 个 Rust 插件对象后,我也会收到错误消息:
dlmopen(rust-plugin.so) failed: /lib/x86_64-linux-gnu/libc.so.6: cannot allocate memory in static TLS block
Run Code Online (Sandbox Code Playgroud)
dlmopen()在此失败后继续尝试导致分段错误和no more namespaces available for dlmopen().
我似乎已将问题与libpthread.soRust 共享对象的依赖关系隔离。其他共享对象依赖项libgcc_s.so.1(以及我尝试过的任何 .so 文件,就此而言)可以通过以下代码无限期地打开和关闭,而libpthread.so在我打开和关闭它 14 次后会出现错误。
#include <link.h>
#include <stdio.h>
#include <dlfcn.h>
#include <cstdlib>
void load(char const *file) {
void *handle_ = dlmopen(LM_ID_NEWLM, file, RTLD_LAZY);
if (!handle_) {
printf("dlmopen(%s) failed: %s\n", file, dlerror());
exit(1);
}
if (dlclose(handle_) …Run Code Online (Sandbox Code Playgroud)