Runtime loading of dynamic libraries in Linux
In Linux, libraries which are linked during linking stage of the program, gets loaded during the startup of the program, unless a copy is already present in memory.
Another approach provided by Linux operating system is to load/unload the libraries at run time whenever it is needed.
API's are provided by Linux for
Flow:
1. Process begins with a call to dlopen API, providing the path of the shared library and the mode. Main two values for this mode are RTLD_NOW, RTLD_LAZY. This informs the dynamic linker when to perform relocations. RTLD_NOW will make all the necessary relocations at the dlopen call time, whereas RTLD_LAZY will perform relocations only when they're needed. dlopen function returns a handle which is used in all the subsequent API's
2. You then call dlsym() to get the addresses of the symbols (functions)
3. dlerror() function will returns a human readable error message if an API fails
4. When all the functions are accessed of the library, you can unload the shared library with a call to dlclose()
Example Code:
Let's create a shared library which we want to load at runtime using dlopen, dlsym calls
arithmetic.c:
arithmetic.h:
Then write application code for loading this library at run time
main.c:
Output:
Another approach provided by Linux operating system is to load/unload the libraries at run time whenever it is needed.
API's are provided by Linux for
- Loading the Library -- dlopen()
- Looking up for symbols -- dlsym()
- Handling Errors -- dlerror()
- Unloading the Library -- dlclose()
Header File : #include <dlfcn.h>
Note: While building the program, you need to link 'ldl' library: -ldl
Flow:
1. Process begins with a call to dlopen API, providing the path of the shared library and the mode. Main two values for this mode are RTLD_NOW, RTLD_LAZY. This informs the dynamic linker when to perform relocations. RTLD_NOW will make all the necessary relocations at the dlopen call time, whereas RTLD_LAZY will perform relocations only when they're needed. dlopen function returns a handle which is used in all the subsequent API's
2. You then call dlsym() to get the addresses of the symbols (functions)
3. dlerror() function will returns a human readable error message if an API fails
4. When all the functions are accessed of the library, you can unload the shared library with a call to dlclose()
Example Code:
Let's create a shared library which we want to load at runtime using dlopen, dlsym calls
arithmetic.c:
arithmetic.h:
Then write application code for loading this library at run time
main.c:
Output:
nice
ReplyDeleteSuch an informative blog.
ReplyDeleteI also faced a DLL error in my Linux window computer then DLLWin guides me it is happening because sometimes due to malware and guides me to which DLL files I need to keep update or install on my Linux computer for a smooth run of my system. If you require any kind of DLL file either it is the newest or popular file then DLLWin is the place Download DLL Files