Symbol name resolution
When the runtime linker loads a shared library, the symbols within that library have to be resolved. The order and the scope of the symbol resolution are important. If a shared library calls a function that happens to exist by the same name in several libraries that the program has loaded, the order in which these libraries are searched for this symbol is critical. This is why the OS defines several options that can be used when loading libraries.
All the objects (executables and libraries) that have global scope are stored on an internal list (the global list). Any global-scope object, by default, makes available all of its symbols to any shared library that gets loaded. The global list initially contains the executable and any libraries that are loaded at the program's startup.
By default, when a new shared library is loaded by using the dlopen() call, symbols within that library are resolved by searching in this order through:
- the shared library
- the list of libraries specified by the LD_PRELOAD environment variable. You can use this environment variable to add or change functionality when you run a program. For setuid or setgid ELF binaries, only libraries in the standard search directories that are also setuid will be loaded.
- the global list
- any dependent objects that the shared library references (i.e. any other libraries that the shared library was linked against)
The runtime linker's scoping behavior can be changed in two ways when dlopen()'ing a shared library:
- When the program loads a new library, it may instruct the runtime linker to place the library's symbols on the global list by passing the RTLD_GLOBAL flag to the dlopen() call. This will make the library's symbols available to any libraries that are subsequently loaded.
- The list of objects that are searched when resolving the symbols within the shared library can be modified. If the RTLD_GROUP flag is passed to dlopen(), then only objects that the library directly references will be searched for symbols. If the RTLD_WORLD flag is passed, only the objects on the global list will be searched.