$_ CLI In Depth
Learning progress 0%
case packages services cron

System Deep-Dive: How the Dynamic Linker Loads a Program

5 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:04

The problem

A program starts quickly, but a few of them start slowly, and some fail at launch with a message about a missing shared object. Both problems happen before the program runs its own code, so they are invisible to a debugger attached at main. To understand them, you need to follow the work the kernel and the loader do between the execve call and the first line of the program. This case uses ls on the lab machine as the example.

Step one: the kernel reads the header

The execve system call opens the file and reads its header. An ELF file starts with a fixed header that names the type of the file and its entry point, and a program header table that describes the segments to map. The kernel also looks for a segment called the interpreter, which holds the path of the dynamic loader. For a dynamic program, that path is the loader, not the program.

BASH1 lines
file /bin/ls

The output names the type of the file and says it is dynamically linked. The interpreter path is recorded in the file itself, and the kernel uses it to decide what to run first. A statically linked program has no interpreter, and the kernel jumps to its entry point directly.

Step two: the kernel starts the loader

The kernel maps the program segments into the new address space and then maps the loader. It places the program arguments and environment on the stack, and it starts execution at the loader entry point, not at the program. From this point, the loader is the first code that runs in the process, and it runs with the same PID that execve kept.

Step three: the loader finds the libraries

The loader reads the dynamic section of the program, which lists the names of the libraries it needs. For each name, it searches the embedded RPATH and RUNPATH directories, the LD_LIBRARY_PATH variable, and then the configured system directories. The first match is mapped into memory. The loader then looks at each library in turn, because a library can need others, and it builds the complete list.

BASH1 lines
ldd /bin/ls

The ldd output is the list the loader built, shown with the path where each library was found. On this machine the loader is the musl loader, and the C library it provides is part of the same file. The order of the lines shows the order of discovery, which is useful when two copies of a library exist.

Step four: relocation and symbol resolution

A shared library does not know the address where it will be mapped, so the loader adjusts the code and data that refer to addresses. This process is called relocation. The loader also resolves symbols, which are names such as the function that prints a string. A call from the program to a library function is bound to the address of the function in the library that provides it.

Binding can be done at load time or at the first call. Lazy binding delays the work until a function is used, which speeds up the start. Some hardened programs choose eager binding, so that every symbol is resolved at start and a missing one fails at once.

Step five: run initializers and jump to main

Before control reaches the program, the loader runs the constructor functions of each library, in dependency order. The C library uses this step to set up its state. Only then does the loader jump to the program entry point, which calls main. The program sees its arguments, its environment, and a working C library.

Where the time goes

A slow start usually spends its time in relocation, in searching many directories for a library, or in reading many files from a cold disk. A failure at start usually happens in the search step, and the loader prints the name of the library it could not find. Both costs grow with the number of libraries, which is why a program that links many optional libraries starts more slowly than one that does not.

Tip

When a program fails at start with a loader error, compare the missing name with the output of ldd for that program. The name is the library the search could not satisfy, and the fix is to make it visible on the search path.

What to check when a program will not start

Start with the interpreter line from file, then list the libraries with ldd, and look for the word not found. Check the runpath and LD_LIBRARY_PATH if the library exists somewhere else on disk. Finally, read the loaded libraries of a running process from its maps file, because that shows what the loader actually mapped.

BASH1 lines
grep -c "\.so" /proc/$$/maps

The command counts the shared objects that the current shell has mapped. A running process shows its real libraries, which is the ground truth when the static view from ldd is not enough.

From execve to main: the dynamic linker

Visualizer

A dynamically linked program does not start in its own code. The kernel starts a small dynamic linker first, which maps the libraries and fixes addresses before main runs.

1 / 6
execve(curl)
kernel reads ELF header
PT_INTERP names ld-musl
ld-musl maps DT_NEEDED
relocations
entry point, then main

execve() starts the process as before. The kernel reads the ELF headers of curl and maps its code and data. The kernel does not load the libraries itself.

ldd $(command -v curl)
Check yourself Question 1 of 4
Concept Check First code to run recall

For a dynamically linked executable, which code does the kernel start first after execve?

Recap & Key Takeaways

The kernel reads the ELF interpreter path and starts the dynamic loader first, with the same PID that execve kept.
The loader searches embedded RPATH and RUNPATH directories, LD_LIBRARY_PATH, and then the system directories, and maps each library it finds.
Relocation and symbol binding connect the program to the library code before main runs.
A loader failure names the missing library, and /proc/PID/maps shows what a running process actually loaded.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue