英文:
What are the scoping rules for subsequent calls to dlopen?
问题
The symbols from libsecond.so should be directly available in both (1) libfirst.so and (2) the executable when it uses RTLD_GLOBAL
to load libsecond.so.
英文:
Suppose I have an executable that dlopen
s libfirst.so with RTLD_LOCAL
which then dlopen
s libsecond.so
with RTLD_GLOBAL
. Should the symbols from libsecond.so now be directly available (i.e., without dlsym
) in (1) only libfirst.so or (2) in both libfirst.so and the executable?
From the manpage on dlopen
> RTLD_GLOBAL: The symbols defined by this shared object will be made available for symbol resolution of subsequently loaded shared objects.
This sounds like the symbols from libsecond.so are not made available to either the executable or libfirst.so (except via dlsym
). Is this correct?
答案1
得分: 2
以下是您要翻译的内容:
"Should the symbols from libsecond.so now be directly available (i.e., without dlsym) in (1) only libfirst.so or (2) in both libfirst.so and the executable?"
我不认为您理解在这个上下文中 "directly available" 的含义。
假设 libsecond.so
定义了 second()
。这个符号在 libfirst.so
或主程序 a.out
中都不可用(显而易见,它们在 libsecond.so
被引入之前已经加载)。
但是如果您现在通过 dlopen()
加载了 libthird.so
,并且如果该库将 second()
作为未解析符号(换句话说,libthird.so
直接依赖于 second()
),如果 libsecond.so
是使用 RTLD_GLOBAL
加载的,那么加载 libthird.so
将成功,但如果 libsecond.so
使用 RTLD_LOCAL
加载,加载将失败(带有未解析的 second()
)。
英文:
> Should the symbols from libsecond.so now be directly available (i.e., without dlsym) in (1) only libfirst.so or (2) in both libfirst.so and the executable?
I don't think you understand what "directly available" means in this context.
Suppose libsecond.so
defines second()
. That symbol is not available in either libfirst.so
or the main a.out
(obviously -- they have been already loaded before libsecond.so
was brought in).
But if you now load (via dlopen()
) libthird.so
, and if that library has second()
as an unresolved symbol (in other words, libthird.so
has a direct dependency on second()
), the load of libthird.so
would succeed if libsecond.so
was loaded with RTLD_GLOBAL
, but would fail (with unresolved second()
) if libsecond.so
was loaded with RTLD_LOCAL
.
答案2
得分: 1
符号将在可执行文件和libfirst中都可见。
以这个简单的例子为例,在可执行文件中尝试在加载libsecond.so
之后访问符号。
当运行时,在主可执行文件中对bar()
的调用成功执行:
这个例子与GNU ld不链接(无法生成正确的可执行文件中的foo
和bar
的重定位)。
英文:
Symbols will be visible in both executable and libfirst.
Take this simple example in which executable tries to access symbol from libsecond.so
after libsecond.so
is loaded.
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#ifdef FIRST
void foo() { dlopen("./libsecond.so", RTLD_LAZY | RTLD_GLOBAL); }
#elif defined SECOND
void bar() { printf("Hello from libsecond\n"); }
#else
extern void foo();
extern void bar();
int main() {
dlopen("./libfirst.so", RTLD_LAZY | RTLD_GLOBAL);
foo();
bar();
return 0;
}
#endif
When run, the call to bar()
in main executable is successfully executed:
$ gcc -fuse-ld=lld -shared -fPIC -DFIRST prog.c -o libfirst.so
$ gcc -fuse-ld=lld -shared -fPIC -DSECOND prog.c -o libsecond.so
$ gcc -fuse-ld=lld -Wl,-z,lazy -Wl,-z,undefs prog.c
$ ./a.out
Hello from libsecond
Interestingly this example does not link with GNU ld (it fails to generate correct relocations for foo
and bar
in executable).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论