How to change the priority order of shared libraries with ld?

by Martin Monperrus
On Linux, shared libraries (aka dynamically linked libraries) are handled with the ld* set of tools from libc-bin. I've recently encountered some bugs related to this, here are some raw notes on this technology.

How to list the shared libraries required by an executable?

ldd lists the names of the required libraries and where they are found on the system.
For instance, in the following, ls depends on which is found in /lib/i386-linux-gnu/.
$ ldd  /bin/ls =>  (0xb7725000) => /lib/i386-linux-gnu/ (0xb76d5000) => /lib/i386-linux-gnu/i686/cmov/ (0xb76cc000) => /lib/i386-linux-gnu/ (0xb76c1000) => /lib/i386-linux-gnu/i686/cmov/ (0xb7564000) => /lib/i386-linux-gnu/i686/cmov/ (0xb7560000)
	/lib/ (0xb7726000) => /lib/i386-linux-gnu/i686/cmov/ (0xb7547000) => /lib/i386-linux-gnu/ (0xb7541000)

How to list the missing symbols of an executable?

Sometimes, although the output of ldd seems correct, some symbols are not found at runtime (Undefined symbol...).
ldd has additional switches to try to completely resolve all symbols of an executable (-r, -d to "perform relocations for both data objects and functions, and report any missing objects or functions").

The point is that it happens that ldd finds a shared library with the correct name, but that the found one is of a slightly different version. In that case, symbols may still be missing. For instance:
$ ldd -r /usr/lib/ => (0xb7841000) => /lib/ (0xb74f2000) => /lib/ (0xb74d8000) => /lib/ (0xb737a000)
    /lib/ (0xb7842000)
undefined symbol: ATL_thread_free (/usr/lib/
undefined symbol: ATL_join_tree (/usr/lib/
undefined symbol: ATL_free_tree (/usr/lib/
undefined symbol: ATL_wait_tree (/usr/lib/
undefined symbol: ATL_signal_tree (/usr/lib/
undefined symbol: ATL_thread_init (/usr/lib/

How to configure shared library resolution?

To configure the resolution of, one has to add directory paths in /etc/ or in /etc/*. Then, a call to $ ldconfig updates /etc/ accordingly. Note that those manually defined directories are mixed with pre-configured ones (such as /lib).

How to change the priority order of directories of shared libraries?

The priority order is: 1) /etc/ 2) files in /etc/* by alphabetical order, 3) pre-configured directories.
To output the directory order used for resolving shared libraries:
$ ldconfig -v | grep "^/"
ldconfig: Path `/lib/i386-linux-gnu' given more than once
ldconfig: Path `/usr/lib/i386-linux-gnu' given more than once
Tagged as: