gmodule bug?



Hi,

gmodule-dl.c: _g_module_open()
	handle = dlopen (file_name, RTLD_GLOBAL | ...)

RTLD_GLOBAL is probably not, what one want to have
RTLD_LOCAL is a better choise

consider the following
a.c:
char problem_func(void) {return 'a';}
char x_func(void) {return problem_func();}

b.c:
char problem_func(void) {return 'b';}
char x_func(void) {return problem_func();}

main.c
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char *argv[])
{
        char res;
        void *ha, *hb;
        char (*fa)(void), (*fb)(void);
        int local;
        unsigned flags;

        if (argc != 2) {
                fprintf(stderr, "usage: %s 0|1\n"
                        "\t0 = use RTLD_GLOBAL\n"
                        "\t1 = use RTLD_LOCAL\n",
                        argv[0]);
                return 1;
        }

        local = atoi(argv[1]);

        flags = (local ? RTLD_LOCAL : RTLD_GLOBAL) | RTLD_NOW;

        ha = dlopen("./a.so", flags);
        fa = dlsym(ha, "x_func");

        hb = dlopen("./b.so", flags);
        fb = dlsym(hb, "x_func");

        res = (*fa)();
        fprintf(stderr, "a.so:x_func() -> '%c'\n", res);
        res = (*fb)();
        fprintf(stderr, "b.so:x_func() -> '%c'\n", res);

        return 0;
}

built for linux
$ cc -shared -oa.so a.c
$ cc -shared -ob.so b.c
$ cc -omain main.c -ldl

running
$ ./main 1
a.so:x_func() -> 'a'
b.so:x_func() -> 'b'
$ ./main 0
a.so:x_func() -> 'a'
b.so:x_func() -> 'a'

The second result is obviously not what we want.

It is because RTLD_GLOBAL enables the loaded module to be used for symbol
resolving and problem_func() is resolved to first one found when scanning
already loaded visible modules in load order.

I make a bugzila bug report later on

Regards
-- 
Miroslaw Dobrzanski-Neumann

MOSAIC SOFTWARE AG
Base Development and Research
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: mne mosaic-ag com




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]