public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* Re: dlopen(RTLD_NOW|RTLD_GLOBAL) / dlsym(RTLD_NEXT) question
@ 2005-08-18 20:19 Yann LANGLAIS
  0 siblings, 0 replies; 2+ messages in thread
From: Yann LANGLAIS @ 2005-08-18 20:19 UTC (permalink / raw)
  To: glibc-bugs

I've made significant progress since my last mail.

It seems that dlsym(RTLD_NEXT, "foo") does not work when the libraries 
are loaded during runtime :

cat > chain5.c <<EOF
#include <dlfcn.h>
int main() {
        void *l1, *l2, *l3, *l4;
        void (*bar)();

        l1 = dlopen("lib1.so", RTLD_NOW | RTLD_GLOBAL);
        bar = (void (*)()) dlsym(l1, "foo");
printf("l1 is %x l1.foo is %x\n", l1, bar);

        l2 = dlopen("lib2.so", RTLD_NOW | RTLD_GLOBAL);
        bar = (void (*)()) dlsym(l2, "foo");
printf("l2 is %x l2.foo is %x\n", l2, bar);

        l3 = dlopen("lib3.so", RTLD_NOW | RTLD_GLOBAL);
        bar = (void (*)()) dlsym(l3, "foo");
printf("l3 is %x l3.foo is %x\n", l3, bar);

        l4 = dlopen("lib4.so", RTLD_NOW | RTLD_GLOBAL);
        bar = (void (*)()) dlsym(l4, "foo");
printf("l4 is %x l4.foo is %x\n", l4, bar);

        bar = (void (*)()) dlsym(l1, "foo");
printf("l1.foo is %x \n", bar);
        bar = (void (*)()) dlsym(l2, "foo");
printf("l2.foo is %x \n", bar);
        bar = (void (*)()) dlsym(l3, "foo");
printf("l3.foo is %x \n", bar);
        bar = (void (*)()) dlsym(l4, "foo");
printf("l4.foo is %x \n", bar);

        bar = (void (*)()) dlsym(RTLD_DEFAULT, "foo");
printf("default foo is %x \n", bar);

        bar();
        dlclose(l4);
        dlclose(l3);
        dlclose(l2);
        dlclose(l1);
        return 0;
}
EOF
gcc chain5.c -o chain5 -ldl -D_GNU_SOURCE

chain5
l1 is 804a018 l1.foo is 5556d584
l2 is 804a3b0 l2.foo is 55570584
l3 is 804a710 l3.foo is 55572584
l4 is 804aa70 l4.foo is 55574584
l1.foo is 5556d584
l2.foo is 55570584
l3.foo is 55572584
l4.foo is 55574584
default foo is 5556d584
lib1.foo()

Shall I consider it as a feature or as a bug ?

regards,

Yann LANGLAIS
18, avenue Nationale
91300 MASSY
06 67 11 07 10
langlais@ilay.org
http://ilay.org

---
    I r i s
L i l a
é   A n g e l a
o   Y a n n
    . O R G


^ permalink raw reply	[flat|nested] 2+ messages in thread

* dlopen(RTLD_NOW|RTLD_GLOBAL) / dlsym(RTLD_NEXT) question
@ 2005-08-18 12:16 Yann LANGLAIS
  0 siblings, 0 replies; 2+ messages in thread
From: Yann LANGLAIS @ 2005-08-18 12:16 UTC (permalink / raw)
  To: glibc-bugs

Hi.

I'm writing an article (in french) about libdl.so and faced a behavior I wasn't
expected while trying to chain functions w/ same name in different libraries with
dlsym/RTLD_NEXT :

Here is a compile time link of the chain components :
for i in 1 2 3 4
do
cat > lib$i.c <<EOF
#include <stdio.h>
#include <dlfcn.h>
void foo() {
    void (*next_foo)(void);
    printf("lib$i.foo()\n");
    if (next_foo = (void (*)(void)) dlsym(RTLD_NEXT, "foo")) next_foo();
}
EOF
gcc -shared -fPIC lib$i.c -o lib$i.so -D_GNU_SOURCE
done
cat > chain.c <<EOF
#include <dlfcn.h>
extern void foo();
int main() {
    foo();
    return 0;
}
EOF
gcc chain.c -o chain -L. -l1 -l2 -l3 -l4 -ldl
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./chain

And the result :

lib1.foo()
lib2.foo()
lib3.foo()
lib4.foo()

And now a runtime linking version:

cat > chain2.c <<EOF
#include <dlfcn.h>
int main() {
	void *l1, *l2, *l3, *l4;
	void (*bar)();
	l1 = dlopen("lib1.so", RTLD_NOW | RTLD_GLOBAL);
	l2 = dlopen("lib2.so", RTLD_NOW | RTLD_GLOBAL);
	l3 = dlopen("lib3.so", RTLD_NOW | RTLD_GLOBAL);
	l4 = dlopen("lib4.so", RTLD_NOW | RTLD_GLOBAL);
	bar = (void (*)()) dlsym(RTLD_DEFAULT, "foo");
	bar();
	dlclose(l4);
	dlclose(l3);
	dlclose(l2);
	dlclose(l1);
	return 0;
}
EOF
gcc chain2.c -o chain2 -ldl -D_GNU_SOURCE

LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./chain2

And the result :

lib1.foo()

Period. Other functions in other libs are ignored.

Is this behavior normal ?


Regards,

Yann LANGLAIS
http://ilay.org
langlais@ilay.org
--
    I r i s
L i l a
é   A n g e l a
o   Y a n n
    . o r g


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-08-18 20:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-18 20:19 dlopen(RTLD_NOW|RTLD_GLOBAL) / dlsym(RTLD_NEXT) question Yann LANGLAIS
  -- strict thread matches above, loose matches on Subject: below --
2005-08-18 12:16 Yann LANGLAIS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).