When accessing the dynamic library, the linker will add the NEEDED entry to the dynamic section of the program. Then the dynamic loader will use them to find the library and use the library to resolve any dynamic undefined symbol.
Please note that there is no relationship between dynamic undefined symbols and dynamic libraries in which they are expected. Sometimes they can be found in another library, and interesting things can happen.
The specific name stored in the NEEDED record depends on whether the library has a SONAME record in its dynamic section:
- If there is
SONAME , then its contents will be copied to the NEEDED program - If there is no
SONAME , the library file name used in the linker command will be saved.
You can check the contents of a dynamic section of a library or program with:
$ objdump -p program
How is this used in practice? Well, most (all?) Linux distributions use the following scheme with system libraries (take libfoo.so ):
- The library is installed as
/usr/lib/libfoo.so.1.2 or any other version. - Symbolic links to this library named
/usr/lib/libfoo.so.1 and /usr/lib/libfoo.so . SONAME libraries libfoo.so.1 .- The path
/usr/lib is set as the path of the dynamic library.
Thus, when you contact -lfoo , it will find the libfoo.so symbolic link, but will write SONAME as libfoo.so.1 . And when the program is launched, it will find another symbolic link and load the library.
This trick is used so that you can install ABI-compatible, improved libfoo.so.1.3 and ABI-incompatible new libfoo.so.2.1 , and the old program will load the old library, and new compilations will use the new library.
Also note that the environment variables LD_PRELOAD , LD_LIBRARY_PATH and others affect runtime behavior. For more details you can read man ld.so
source share