Random Tech Thoughts

The title above is not random

Linx Program Library Related Notes

Program Library HOWTO

Notes for Chapter 3 Shared Libraries.

Shared libraries names

  • soname: when program internally list the shared libraries they need, use this name
    • /usr/lib/libreadline.so.3 3 is the version number which is changed whenever the interface changes
    • soname is a symbolic link to the shared library’s “real name”
    • ldconfig will examine the existing files and create soname
  • real name: filename containing the actual library code (not symbolic link). Use real name when creating shared library
    • /usr/lib/libreadline.so.3.0 0 is the minor version number
    • /usr/lib/libreadline.so.3.0.1 the last 1 is release number, optional
    • The additional version numbers specifies what version(s) of the library are installed
  • linker name: used by the compiler
    • /usr/lib/libreadline.so setup as symbolic link to the latest soname
    • Not setup by ldconfig. Typically setup during library installation

How Libraries are used

  • On GNU glibc-based systems, starting up an ELF binary executable automatically cause the program loader (/lib/ld-linux.so.X on Linux) to be loaded and run
    • /lib/ld-linux.so.X is sometimes called dynamic linker
    • It’s path is hard coded in ELF header
  • The loader will search all the directories listed in /etc/ld.so.conf for shared libraries used by the program
    • Can use /etc/ld.so.preload to list overriding libraries (specify files not directory)
  • Searching all of the directories is slow, so ldconfig will create a cache /etc/ld.so.cache and to be used by other programs
    • ldconfig must be run whenever a DLL is added/removed or directories changes

Environment Variables

  • LD_LIBRARY_PATH specify search directory before the standard set of directories for the loader
  • LD_PRELOAD specify shared libraries that with functions that override the standard set (same as /etc/ld.so.preload)

These 2 environment variables are implemented by the loader /lib/ld-linux.so. Other system may use other environment variables.

Another way to specify library search path is path an argument to the loader, as

/lib/ld-linux.so --library-path LIBPATH EXECUTABLE
  • LD_DEBUG trigger the dl* function to give quite verbose information on whay they are doing, possible values are
    • files
    • bindings
    • libs

For security reasons, if a program is setuid or setgid, these variables are ignored or greatly limited in what they can do.


Specify library search path at compile time

Learned from rtldi — indirect runtime loader

On Linux, pass the --rpath=<path> option to the linker. When the linker is invoked by gcc, use -Wl, to pass options to the loader, like this:

gcc ... -Wl,--rpath=<path>

Specify loader at compile time

Pass --dynamic-linker=<loader> option to the linker.