Source: http://blog.chinaunix.net/uid-26285146-id-3262288.html
These two functions are found when reading Hal-related source code.
In short, after the shared object is loaded by dynamically opening the dynamic library through dlopen, a handle is returned, and the function pointer you need to execute is located through dlsym, which can then be used in the program.
Dlopen -- open a dynamically linked Library
Dlsym -- get the address of a symbol in a dynamically linked Library
Example void * handle;
Int I, * iptr;
INT (* fptr) (INT );
/* Open the needed object */
Handle = dlopen ("/usr/mydir/libx. So", rtld_lazy );
/* Find address of function and Data Objects */
Fptr = (INT (*) (INT) dlsym (handle, "some_function ");
Iptr = (int *) dlsym (handle, "int_object ");
/* Invoke function, passing value of Integer as a parameter */
I = (* fptr) (* iptr );
Reference http://topic.csdn.net/u/20090422/17/2ddcab88-2d50-478a-9b2e-2c2978ad604b.html
Http://hi.baidu.com/janefei1603/blog/item/72ae708eed638eda503d923b.html
Additional link: http://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html
Implementation
Dlopen
Basic Definition
Function: open a dynamic link library.
Include header file:
# Include <dlfcn. h>
Function Definition:
Void * dlopen (const char * pathname, int mode );
Function Description:
In the dlopen () function, open the specified dynamic connection library file in the specified mode, and return a handle to the calling process. Use dlclose () to uninstall the opened library.
Mode: there are two types:
Rtld_lazy is suspended, and the symbol is resolved when necessary.
Rtld_now is determined immediately. All undetermined symbols are removed before return.
Rtld_local
Rtld_global allows exporting symbols
Rtld_group
Rtld_world
Return Value:
If an error is returned, null is returned.
Succeeded. Database reference is returned.
Add-LDL during compilation (specify the DL Library)
For example
GCC test. C-o Test-LDL
Edit this section
Use dlopen
Dlopen () is a powerful library function. This function will open a new library and load it into the memory. This function is mainly used to load symbols in the library. These symbols are unknown during compilation. For example, the Apache web server uses this function to load modules during running, which provides additional capabilities. A configuration file controls the process of loading modules. This mechanism does not need to be re-compiled when a module is added or deleted in the system.
You can use dlopen () in your own program (). Dlopen () in dlfcn. h
And in the DL
Library. It requires two parameters: a file name and a flag. The file name can be the soname in the library we have learned. Indicates whether to calculate the database dependency immediately. If it is set to rtld_now, it is calculated immediately. If it is set to rtld_lazy, it is calculated only when necessary. In addition, you can specify
Rtld_global, which allows the libraries loaded later to obtain the symbols.
After the library is loaded, you can use the handle returned by dlopen () as the handle to the dlsym ()
To obtain the address of the symbol in the library. With this address, you can obtain the pointer of a specific function in the library and call the corresponding function in the loading library.
Dlsym
The function prototype of dlsym () is
Void * dlsym (void * handle, const char * symbol)
This function is in the <dlfcn. h> file.
Handle is the pointer returned after dlopen opens the dynamic link library. symbol is the name of the function to be obtained. The return value of the function is void *. It points to the address of the function for calling.
Dynamic Object address:
# Include <dlfcn. h>
Void * dlsym (void * phandle, char * symbol );
Dlsym returns the address corresponding to the symbol based on the dynamic link library operation handle (phandle) and symbol (Symbol.
You can use this function to obtain both the function address and the variable address. For example, in so
A voidmytest () function is defined, and a function pointer is first declared when so is used:
Void (* pmytest) (), and then use the dlsym function to direct the function pointer pmytest to the mytest function,
Pmytest = (void (*) () dlsym (phandle, "mytest ");