Technical skill

Linux dynamic libraries

Share common pieces of a program

Imagine several people using the same toolbox instead of carrying a separate copy of every tool. Programs can share common helper code in a separate file called a dynamic library. A program loads that code while it runs rather than carrying its own copy. The library's code is compiled, meaning it has been translated into instructions the computer can run. Sharing it avoids duplicate files, but each program still expects particular routines and rules for calling them.

How a program finds its library

An executable is the file a computer starts when you run a program. Linux commonly stores executables in ELF, the Executable and Linkable Format. A dynamically linked ELF file has a dynamic section listing the libraries it needs. Many Linux programs use the C library for common routines such as file and text operations. Its shared-object file often ends in .so; the C library is commonly named libc.so.6. The executable also names an interpreter, usually the dynamic linker. At startup, the kernel starts that interpreter, which finds the libraries, places them in memory, connects the function and data names the program needs, and prepares the program to run.

The name requested by the program, the library's SONAME, and the actual filename on disk may differ. SONAME is the shared object's published identity, often indicating which compatible version family it belongs to. The application binary interface (ABI) describes how compiled programs call functions and exchange data. A symbol is a published name for a function or data item; a program can fail if a library does not provide a symbol it expects. Two libraries can have a similar filename while exposing incompatible ABIs. Package managers usually maintain the versioned files and links expected by installed programs.

On systems using glibc, the dynamic linker checks configured directories and any paths recorded in the executable. LD_LIBRARY_PATH can add directories to the search order for ordinary execution. Secure-execution mode ignores that variable. It can help test a program against a library in a temporary directory, but a permanent system-wide setting may cause unrelated programs to load the wrong file.

Inspect a program before running it

If a program reports a missing library, inspect its recorded dependencies first. readelf reads ELF information without starting the program:

readelf -d ./my-program | grep -E 'NEEDED|RPATH|RUNPATH'

The NEEDED entries list shared-object names. RPATH and RUNPATH entries can affect where the dynamic linker searches; they have different precedence and rules for searching indirect dependencies. Their presence does not prove that the named directory exists or contains a compatible library.

ldconfig -p displays glibc's current library cache. For a program you trust, ldd ./my-program shows how its dependencies resolve on the current computer. Do not use ldd as the first check on an untrusted file: depending on its interpreter and the implementation, inspection can run code. readelf -d is a safer first look at an unknown ELF file.

Match the repair to the error

When a library cannot be found, compare the executable's architecture and dependency list from readelf -d with installed packages and the loader cache. On glibc systems, ldconfig updates that cache and expected links from configured directories. It cannot make an incompatible library satisfy a different ABI.

Avoid copying an arbitrary .so into a system folder or hiding the error with a broad global search path. Install the package that provides the required compatible library, or rebuild the program for its target environment. A missing-file error points to installation or lookup. A missing-symbol or ABI error usually calls for a compatible library or a rebuilt program.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful