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.
file /bin/lsThe 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.
ldd /bin/lsThe 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.
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.
grep -c "\.so" /proc/$$/mapsThe 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.