http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55872



--- Comment #4 from Yogesh Gaur <gauryogesh.nsit at gmail dot com> 2013-01-07 
10:31:19 UTC ---

Hello,



Actually issue is combination of copy_relocation plus RTLD_DEEPBIND flag.



If I didn't give -fPIE flag while compiling my executable and pass

RTLD_DEEPBIND flag while opening library using dlopen(), then result is

un-expected.



I checked similar issue exist on gcc also:

------------------------------Source Code -----------------------------

$ cat main.c

#include <dlfcn.h>

extern int alpha;

int main()

{

        char const * const name = "./lib1.so";

        void * handle = dlopen(name, RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);

        typedef void (*library_function_type)();

        library_function_type func1 = dlsym(handle, "func1");



        alpha = 10;

        (*func1)();

        func2();



        dlclose(handle);

        return 0;

}



$ cat lib1.c

#include <stdio.h>

extern int alpha;

void func1(){

        printf("lib1: Addr=%p, value=%d\n", &alpha, alpha);

}



$ cat lib2.c

#include <stdio.h>

int alpha;

void func2(){

        printf("lib2: Addr=%p, value=%d\n", &alpha, alpha);

}

-------------------------------- END ----------------------------------



Compilation command and output:



gcc -shared -fPIC lib2.c -o lib2.so

gcc -shared -fPIC lib1.c lib2.so -o lib1.so

gcc -ldl main.c lib2.so -o a.out

LD_LIBRARY_PATH=$PWD ./a.out 

         lib1: Addr=0x7f5a39663028, value=0

         lib2: Addr=0x601038, value=10

=========================================



Thus for same symbol, alpha, we get two addresses values.



If I remove RTLD_DEEPBIND while opening library, I didn't get this issue:

          lib1: Addr=0x601038, value=10

          lib2: Addr=0x601038, value=10



Reason for this also I know that in case of RTLD_DEEPBIND scope of search for

lib1.so is its internal library first and then only global library's being

searched.



I want to know that apart from usage of -fPIE flag at compilation time did

any-other solution exist for this issue? As using -fPIE has it's own

side-affect.



--

Regards,

Yogesh Gaur.

Reply via email to