Question

My problem deals with Python, Qt, PyQt and other stuff, but the question is actually about how Linux's ld.so actually works.

THE QUESTION

If a program loads two different shared libraries which both have the same entry point name (i.e. they both define a function with the same name and signature) how can it tell which version is it calling?

MY PROBLEM

I have a third party, proprietary Linux application that was written in C++ (though the original language is irrelevant) and it's linked dynamically to Qt3.3. The application embeds a python interpreter which can be used to write scripts for it.

You can even use the application's embedded python instead of the original one using a command such as:

/path/to/the/program/python

And it shows the following:

Python 2.7.1 (r271:86832, Sep 16 2011, 18:16:32) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Using gcc 4.1.2 I have built and installed PyQt4 from sources, against the Qt4 libraries that the system also has. I believe the build was successful because I can run a small PyQt4 application using:

/path/to/the/program/python mypyqtapp.py

However, if I load the program with its GUI and load the same script, it crashes at the first PyQt4 call, which is an instantiation of the QApplication class.

Since the python environment is the same, I suspect it might be some kind of shared library conflict between Qt3 and Qt4. However, the command strace -e trace=file reveals that python is locating and loading the right Qt4 libraries in both cases.

So, my question is if a program loads two shared different libraries that define the same function, how does it know it's calling the right one? Does the linux loader ld.so somehow qualify the entry points with a file name or something? I suspect that my problem might lie there, that the app has eventually loaded two different instances of QApplication and it's calling the wrong one, but how ld.so actually works internally escapes me.

It might also be that my problem is caused by something completely different.

Thank you.

Was it helpful?

Solution

Conflict symbol only detected on linkage(ld) stage, loader(ld-linux.so) don't check conflict symbol.

When multiple library providing duplicated symbol, the one first loaded is used.

Actually somebody can make use of this feature. By providing LD_PRELOAD=overwrite.so, overwrite.so can overwrite any function the program needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top