fbpx Shared Libraries in Linux: ldd, ldconfig and .so | Certificação Linux

Shared Libraries in Linux: How They Work and How to Manage Them

📅 03/07/2019 ⏱ 6 min ✍️ Uira Ribeiro
Linuxcommandshow-to
Shared Libraries in Linux: How They Work and How to Manage Them
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

How Shared Libraries Work in Linux (shared libraries), and how this can interfere with the functioning of programs.

Determining which shared libraries an executable program depends on to run and installing them when necessary is part of the exam objectives.

To understand shared library management, we first need to understand what libraries are and what they are used for.

During the writing of a program's source code, the developer makes use of various functions and procedures already defined by the operating system in files called libraries.

These functions allow the programmer to use resources such as writing to disk, writing to the screen, receiving data from the keyboard, mouse, sending data over the network, and much more, without the need to rewrite the wheel.

When the program is compiled, the last stage of its construction is to perform the linking.

Some compilers already perform this process of gathering all necessary objects and composing a final object automatically.

Others require the programmer to execute another program called a linker. Libraries can be understood in a rough way as the "DLLs" of Windows.

The /lib64 Directory

In modern 64-bit distributions, libraries are located in the /lib64 directory. Depending on the processor architecture, the directory name may vary (e.g., /lib for 32 bits).

Types of Libraries

There are two types of libraries in Linux: static and dynamic. The decision of which library to use is up to the programmer.

When using a static library, the linker finds the functions and procedures the program needs and copies them physically into the generated executable output file.

This makes the final executable able to run independently without using any library. But it loses in performance, unnecessary memory usage, and the size of the final program.

It is common for the programmer to use shared libraries instead of static ones. When linking a program that uses them, the linker makes a reference to the shared libraries.

This way, when this program is executed, the system will have to load the necessary libraries first, if they are not already in memory.

This way, the generated executables are more efficient because they tend to be smaller, use less memory, and take up less disk space.

The weak point of this methodology is that programs need the shared libraries and a change in the versions of these files can also affect their functioning.

What are "soname"?

Every shared library has a special name called "soname". The soname is composed of the prefix "lib", followed by the library name, the suffix ".so.", and the library version number which is incremented when there are changes to its interface.

For example:

libjack-0.80.0.so.0
libvorbis.so.0
libWand.so.6
libjpeg.so.62
libwv2.so.1

Executable files are examined at runtime by the runtime linker called ld.so.

This special interpreter completes the linkages between the executable and the shared libraries. If ld.so cannot find and read the dependencies, it will fail and the executable will not be loaded.

The Linker Links Libraries to Software

The linker ld.so maintains an index of all libraries and their location in a special file called /etc/ld.so.cache. It is binary and therefore can be read quickly by ld.so.

This is why a Linux administrator must be prepared to manage shared libraries and their versions for the proper functioning of the system and its applications.

In this article you can learn more about the compilation process and "linking" of programs to libraries.

Let's see the utilities that will help with this task:

ldd Program in Linux

The ldd command – List Dynamic Dependencies – provides a list of the dynamic dependencies that a particular program needs. It will return the shared library name and its expected location.

Example:

# ldd /bin/bash

libreadline.so.4 => /lib/libreadline.so.4 (0x4001c000)

libhistory.so.4 => /lib/libhistory.so.4 (0x40049000)

libncurses.so.5 => /lib/libncurses.so.5 (0x40050000)

libdl.so.2 => /lib/libdl.so.2 (0x40096000)

libc.so.6 => /lib/libc.so.6 (0x40099000)

/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

# ldd ldd teste_dijkstra

libc.so.6 => /lib/libc.so.6 (0x4001c000)

/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

This command is important for determining what libraries an executable needs.

ldconfig Program in Linux

The ldconfig utility creates the links and rebuilds the index of dynamic libraries in the /etc/ld.so.cache file.

It looks for libraries in the /usr/lib and /lib directories, as well as the directories listed in /etc/ld.so.conf, and the directory specified on the command line.

The most common options are:

  • -p: Lists the contents of the /etc/ld.so.cache cache.
  • -v: Shows the progress of cache update.
  • -f: file specifies a different configuration file than the default /etc/ld.so.conf.

Examples:

# ldconfig –p

229 libs found in cache '/etc/ld.so.cache'

src_vipa.so (ELF) => /usr/lib/src_vipa.so

libz.so.1 (libc6) => /lib/libz.so.1

libz.so.1 (libc6) => /usr/lib/libz.so.1

# ldconfig –v

/usr/X11R6/lib:

libSM.so.6 -> libSM.so.6.0

libdps.so.1 -> libdps.so.1.0

libXrender.so.1 -> libXrender.so.1.2

(…)

LD_LIBRARY_PATH Environment Variable

It is also possible to provide the runtime linker ld.so with a list of extra directories that may contain shared libraries through the LD_LIBRARY_PATH environment variable.

A list of directories can be configured, separating them by colons ":". This list precedes the list in the ls.so.conf file.

# set | grep LD_LIBRARY_PATH

LD_LIBRARY_PATH=/usr/lib

For security reasons, the LD_LIBRARY_PATH variable is ignored by ld.so when it links programs that have the SUID or SGID bit enabled.

Its use is common for testing new routines in libraries under development instead of running the already installed routines.

Every time a new library is installed, or a new version of a library, it is necessary to update the ld.so linker cache with the ldconfig command.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp