Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RTLD_DEEPBIND when invoke implicitly?

Now i have 3 shared objects,A.so,B.so,C.so

    A.c    
    void libA()  
    {  
        common();  
    }  

    B.c
    void common()  
    {  
        printf("COME HERE B\n");  
    }  

    C.c
    void common()  
    {     
        printf("COME HERE C\n");  
    }  
    (just ingore the .h files)

    test.c
    int main()  
    {  
      libA();    
      return 1;  
    }  

complie:  
gcc -fPIC -shared libB.so libB.c  
gcc -fPIC -shared libA.so libA.c ./libB.so  
gcc -o test test.c libC.so libA.so  

I wish result to be "COME HERE B" and i could use dlopen with RTLD_DEEPBIND flag,
but it costs too much time to change functions from implicit call to explicit call in my project.
Is there anyway to solve this problem?

gcc -Wl,-Bsymbolic doesn't work in this solution.

Well, if A.c contains implementation of common. It does work.

like image 342
sonicx990 Avatar asked Nov 12 '22 18:11

sonicx990


1 Answers

It looks like when the dynamic linker searches for a symbol in runtime, it chooses the first one that it comes across. The search order depends on the order of libraries in DT_NEEDED section of the binary, which in turn depends on the exact order of libraries in the command line during compilation. So, make sure libB.so is before libC.so on the command line when compiling test.c.

like image 65
jmajnert Avatar answered Nov 15 '22 12:11

jmajnert