📅 2017-Nov-01 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ ld_library_path, ldconfig, ldd, rpath, runpath, shared library ⬩ 📚 Archive
You have successfully compiled an executable that is linked with one or more external shared libraries. You can view the shared libraries that the executable is dependent on by using the ldd tool. When you actually run the executable, the dynamic linker-loader ld-linux looks for each dependent shared library in the following locations, in order:
Using RPATH, if it exists, that is hard-coded in the executable. This is a colon-separated list of directories from where the shared libraries were linked into the executable by the linker during the linking stage of compilation. If this exists, you can view it using this command: readelf -d ./your_binary | grep RPATH
Using LD_LIBRARY_PATH
, if it is set. This is a colon-separated list of directories set as an environment variable by the user.
Using RUNPATH, it is exists, that is hard-coded in the executable. This is a colon-separated list of directories, just like RPATH. If this exists, you can view it using this command: readelf -d ./your_binary | grep RUNPATH
Checks the /etc/ld.so.cache
. This cache is populated by running the ldconfig program. This program is usually run when libraries are installed. You can view the shared libraries in the cache using this command: ldconfig -p
Check in /lib
Check in /usr/lib
You can actually witness the loader searching directories to find the location of each shared library. To see this in action, try this command:
$ LD_DEBUG=libs ldd ./some_executable
In the output of this command, you will see that:
Each shared library listed in the executable is picked up in order.
For each shared library, the locations listed above (RPATH, LD_LIBRARY_PATH, RUNPATH, cache, lib and user lib) are tried in order.
For each directory listed in the above colon-separated list, the shared library filename is appended and tried to see if the file path exists.
The first instance where such a file path exists, that is noted as the location of the shared library.
References: