dlopen() returns undefined symbol for library w/ RTLD_LAZY
Steve Lawler
mirrorsh@atlantech.net
Sat Mar 10 13:00:00 GMT 2001
I am using gcc-2.95.2 and glibc-2.2 (sadly, I am
using RH7.0's glibc-2.2) w/ Linux 2.4.2.ÃÂ Now perhaps I don't know how
dlopen() is supposed to work, but....
ÃÂ
If I try to dlopen() a shared library with
RTLD_LAZY, and the library in question references an external symbol that is not
defined in a system library (like libc or something), dlopen() fails and dlerror
will print: "X.so: undefined symbol: FOO".
ÃÂ
For example:
ÃÂ
lib1.c:
#include <stdio.h>
ÃÂ
extern int x(void);
ÃÂ
void lib1_func(void) { printf("x = %d\n", x);
}
-------------------
ÃÂ
test.c:
#include <stdio.h>
ÃÂ
int x(void) { return 99; }
ÃÂ
int main(void) {
ÃÂ ÃÂ ÃÂ void *h;
ÃÂ ÃÂ ÃÂ h = dlopen("./lib1.so",
RTLD_LAZY);
ÃÂ ÃÂ ÃÂ if (!h) { printf("%s\n",
dlerror()); }
ÃÂ ÃÂ ÃÂ if (h) dlclose(h);
ÃÂ ÃÂ ÃÂ return 0;
}
--------------------
ÃÂ
$ cc -c lib1.c
$ cc -c test.c
$ ld -shared lib1.o -o lib1.so
$ cc test.o -o test -ldl
$ ./test
./lib1.so: undefined symbol: x
ÃÂ
ÃÂ
I thought that RTLD_LAZY forced symbols to be evaluated as
they are loaded from the shared object; thusÃÂ dlopen() would not try to load
all of ./lib1.so's symbols immediately upon opening; that would be deferred
until, say, I tried to load lib1_func with dlsym(), at which point it would try
to resolve x() (and, I would hope, it would be resolved to the 'x' found in
test.o but... ?)ÃÂ
ÃÂ
Also I am wondering why I don't get a message like
"./lib1.so: undefined symbol: printf"
ÃÂ
ÃÂ
More information about the Gcc-help
mailing list