Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are shared library dependency paths determined on Linux?

When I run ldd against a shared library such as libphp5.so I see that it has a dependency on libmysqlclient.so.16:

$ ldd ./libphp5.so
libmysqlclient.so.16 => /usr/lib/mysql/libmysqlclient.so.16 
[other dependencies snipped out]

Are these dependency filenames and paths (/usr/lib/mysql/libmysqlclient.so.16) baked into the shared library binary? Or is this path determined by some other means such as via /etc/ld.so.conf.d/mysql-i386.conf, which incidentally contains:

/usr/lib/mysql/

One other thing is puzzling me:

There is a shared library I have that I compile from source. This has a dependency on libmysqlclient_r. The gcc compiler switches to produce this this library look like:

gcc -shared -L/usr/lib/mysql -lmysqlclient_r [+various other switches]

When I do ldd mylib.so I see:

libmysqlclient_r.so.16 => /usr/lib/mysql/libmysqlclient_r.so.16 (0x0055c000)

However in the /usr/lib/mysql directory I see:

-rwxr-xr-x. libmysqlclient_r.so -> libmysqlclient_r.so.16.0.0
lrwxrwxrwx. libmysqlclient_r.so.16 -> libmysqlclient_r.so.16.0.0
-rwxr-xr-x. libmysqlclient_r.so.16.0.0
lrwxrwxrwx. libmysqlclient.so -> libmysqlclient.so.16.0.0
lrwxrwxrwx. libmysqlclient.so.16 -> libmysqlclient.so.16.0.0
-rwxr-xr-x. libmysqlclient.so.16.0.0

libmysqlclient_r.so is a symbolic link to libmysqlclient_r.so.16.0.0, so why does ldd show the dependency as libmysqlclient_r.so.16. Is there some magic I'm missing here?

Having been a Windows dev for many years I'm a bit new to gcc and development on Linux.

My Linux distribution is CentOS 6.0 x86-32bit.

like image 539
Kev Avatar asked Nov 17 '11 16:11

Kev


People also ask

How does Linux find shared libraries?

In Linux, shared libraries are stored in /lib* or /usr/lib*. Different Linux distributions or even versions of the same distribution might package different libraries, making a program compiled for a particular distribution or version not correctly run on another.

How are shared libraries loaded in Linux?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

How do I find the library path in Linux?

By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations. The library path can be defined in /etc/ld.


1 Answers

You can see which paths are coming from where by running

LD_DEBUG=libs ldd ./libphp5.so

Are these dependency filenames and paths (/usr/lib/mysql/libmysqlclient.so.16) baked into the shared library binary?

The filename almost certainly is. The path usually isn't. You can see what is baked into the binary with

readelf -d ./libphp5.so

Look for (NEEDED) and (RPATH) entries.

Also give man ld.so a read. There are many factors that affect how dynamic loader searches for shared libraries: ld.so.conf, LD_LIBRARY_PATH, whether the executable is suid or not, how glibc was configured, which -rpath settings were given at link time, etc. etc.

like image 149
Employed Russian Avatar answered Oct 23 '22 10:10

Employed Russian