Linux provides a set of APIs to dynamically load libraries. These APIs are listed below:
-Dlopen, open a library and do some preparation for using the library.
-Dlsym, find the value of the symbol in the Open library.
-Dlclose, close the library.
-Dlerror, returns a string that describes the error message for the last call to Dlopen, Dlsym, or Dlclose.
C-language users need to include the header file Dlfcn.h to use the above API. The GLIBC also adds APIs that are not in the two POSIX standards:
-DLADDR, parses the symbol name from the function pointer and the file in which it resides.
-Dlvsym, similar to Dlsym, is just one more version of the string parameter.
On Linux, applications that use dynamic linking need to be linked with the library libdl.so, that is, with the option-LDL. However, compile-time does not need to be linked with dynamically loaded libraries. Program 3-1 is a simple example of using the dl* routine on Linux.
Deferred relocation (lazy relocation)
Deferred relocation/mount is an attribute that allows symbols to be repositioned only when they are needed. This is often used when parsing function calls on UNIX systems. This feature proves to be useful when an application that is linked to a shared library is hardly used for functions in that shared library. In this case, only the functions in the library are called by the application, the shared library is loaded, otherwise it is not loaded, and therefore some system resources are saved. However, if you set the environment variable Ld_bind_now to a non-null value, all relocation operations will take place at the start of the program. You can also disable deferred binding on a particular shared library by using the-Z NOW linker option on the linker command line. It is important to note that this setting for the shared library will remain in effect until the shared library is re-linked.
Initialize (initializing) and terminate (finalizing) functions
Sometimes, the previous code might have used two special functions: _init and _fini. The _init and _fini functions are used to control the constructors and destructors (or constructors and destructors) of the module, respectively, when loading and unloading a module (note 14). Their C-language prototypes are as follows:
void _init (void);
void _fini (void);
When a library is opened dynamically through Dlopen () or opened as a shared library, the _init function is called if _init exists in the library and is output. The _fini function is called before the library is unloaded if a library is closed dynamically through dlclose () or is unloaded because no application references its symbol. When using your own _init and _fini functions, be careful not to link with system startup files. You can do this using the GCC option-nostartfiles.
However, using the above function or GCC's-nostartfiles option is not a good habit, as this may have some unintended consequences. Instead, the library should use the __attribute__ (constructor) and __attribute__ ((destructor)) function properties to output its constructors and destructors. As shown below:
void __attribute__ ((constructor)) X_init (void)
void __attribute__ ((destructor)) X_fini (void)
The constructor is called before Dlopen () is returned or when the library is loaded. Destructors are invoked in several cases: Dlclose () before returning, or when main () is returned, or when exit () is called during the load library process.
We explain the use and operation of the Dlopen series functions through an example:
Main program:
- #include <stdlib.h>
- #include <dlfcn.h>
- #include <stdio.h>
- Affirm the structure of the body
- typedef struct __TEST {
- int i;
- void (* echo_fun) (struct __test *p);
- }test;
- Registration functions for use by dynamic libraries
- void __register (Test *p) {
- P->i = 1;
- P->echo_fun (P);
- }
- int main (void) {
- void *handle = NULL;
- Char *myso = "./mylib.so";
- if (handle = Dlopen (MYSO, rtld_now) = = = NULL) {
- printf ("Dlopen-%sn", Dlerror ());
- Exit (-1);
- }
- return 0;
- }
Dynamic libraries:
- #include <stdio.h>
- #include <stdlib.h>
- declaring struct type
- typedef struct __TEST {
- int i;
- void (*echo_fun) (struct __test *p);
- }test;
- Affirm the Registration function prototype
- void __register (Test *p);
- static void __printf (Test *p) {
- printf ("i =%dn", p->i);
- }
- Dynamic Library Request a global variable space
- The assignment of this ". Member" is C99 standard
- Static Test config = {
- . i = 0,
- . Echo_fun = __printf,
- };
- Automatic initialization functions for loading dynamic libraries
- void _init (void) {
- printf ("initn");
- Calling the main program's registration function
- __register (&config);
- }
Main program compilation: GCC test.c-ldl-rdynamic
Dynamic Library Compilation: Gcc-shared-fpic-nostartfiles-o mylib.so MYLIB.C
The main program loads a. So dynamic library file through Dlopen (), then the dynamic library automatically runs the _init () initialization function, the initialization function prints a hint, and then invokes the main program's registration function to reassign the struct, and then calls the struct's function pointer to print the value of the struct body. In this way, the functions of the main program and the dynamic library are called and the pointers are communicated with each other.
The GCC parameter-rdynamic is used to inform the linker to add all symbols to the dynamic symbol table (to enable backward tracking by using dlopen).
GCC parameter-fpic: When using a library of. So, when you encounter multiple executables that share this library, in memory, the library is not copied multiple copies, allowing each executable to be used one-on-another, rather than having multiple executables point to a library file for sharing. Purpose: Save the memory space, improve the space utilization.
Dlopen function Explanation