Shared Libraries

Howto Display Shared Libraries

What are shared Libraries?


Shared Libraries are an area where object code has been compiled and grouped generally into a single file (library). In many cases, code that can be shared by more than one application can be removed and compiled into a shared area. Generally there are two types of shared libraries:



Static Libraries


Statically linked programs tend to get large because they include executable files for all of the library functions linked into them. Statically Linked programs often use more memory when other programs that are currently running contain the same library functions. Statically Linked Libraries are identified by an extension of ".a". To avoid some of the issues encountered by using static libraries, many programs will use what is known as "Dynamically Linked" libraries.


Dynamic Libraries


Dynamically Linked libraries allow multiple programs to share the same library code in memory which results in a smaller memory footprint. Dynamically linked libraries are shared amongst multiple programs. The term "Shared Libraries" often relates to dynamically linked libraries. Dynamic Libraries have the extension of ".so"


Displaying Shared Library Dependencies


The "ldd" command can be used to print the shared libraries required by each program or shared library specified on the command.


ldd command


ldd example:



ls001a:/usr/lib # ldd /bin/bash
        linux-vdso.so.1 =>  (0x00007fffe35eb000)
        libreadline.so.5 => /lib64/libreadline.so.5 (0x00007fc30e349000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fc30e145000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fc30ddd0000)
        libncurses.so.5 => /lib64/libncurses.so.5 (0x00007fc30db88000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fc30e5bd000)

Syntax: ldd [OPTION]... FILE...



OPTIONS
       --version
              Print the version number of ldd.

       -v --verbose
              Print all information, including, for example, symbol versioning information.

       -u --unused
              Print unused direct dependencies.  (Since glibc 2.3.4.)

       -d --data-relocs
              Perform relocations and report any missing objects (ELF only).

       -r --function-relocs
              Perform relocations for both data objects and functions, and report any missing objects or functions (ELF only).

       --help Usage information.

Linking Shared Libraries


Dynamically linked executables are examined at their runtime by "ld.so" the Shared Object Dynamic Linker. "ld.so" checks for any dependencies in the executable and then attempts to satisfy these. If ld.so is unable to satisfy these dependencies, then the executable will fail.


Library Path


For an executable to find the necessary libraries to link with at its run time, the system has to be configured so that these libraries can be found. There are several ways this can be achieved:


/etc/ld.so.conf


Add your library directory into the location "/etc/ld.so.conf". Once your entry has been added, you must issue the "ldconfig" command as the root user to complete the process.


LD_LIBRARY_PATH


Use the environment variable LD_LIBRARY_PATH to point to the directory paths containing the shared object library. Multiple directories can be added separated by a colon ":".



LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib ; export LD_LIBRARY_PATH

This entry can be added into your .bashrc file located in your home directory. (Amend accordingly)


ldconfig command


ldconfig is used to configure dynamic linker run time bindings. ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified from the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). This cache is used by the run time linker "ld.so". ldconfig must be run when any changes are made to the file "/etc/ld.so.conf". This will refresh the cache of directories.


Frequently used options for ldconfig



OPTIONS
       -v     Verbose  mode.   Print  current version number, the name of each directory as it is scanned, and any links that are
              created.  Overrides quiet mode.

       -n     Only process directories specified on the command line.  Don't process the trusted directories (/lib and  /usr/lib)
              nor those specified in /etc/ld.so.conf.  Implies -N.

       -N     Don't rebuild the cache.  Unless -X is also specified, links are still updated.

       -X     Don't update links.  Unless -N is also specified, the cache is still rebuilt.

       -f conf
              Use conf instead of /etc/ld.so.conf.

       -C cache
              Use cache instead of /etc/ld.so.cache.

       -r root
              Change to and use root as the root directory.

       -l     Library mode.  Manually link individual libraries.  Intended for use by experts only.

       -p     Print the lists of directories and candidate libraries stored in the current cache.

Files associated with ldconfig command


/lib/ld.so

run time linker/loader

/etc/ld.so.conf

File containing a list of colon, space, tab, newline, or comma-separated directories in which to search for libraries.

/etc/ld.so.cache

File containing an ordered list of libraries found in the directories specified in /etc/ld.so.conf.