Dlopen; dlclose; dlsym

Source: Internet
Author: User

Dynamically Loaded (DL)

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
Hold the decision and wait for the problem to be solved.
Rtld_now
Determine immediately. Remove all undetermined symbols before returning.
Rtld_local
Rtld_global
Allowed to export 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, Apache
The 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
Rtld_now is calculated immediately. If rtld_lazy is set, 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 set dlopen ()
The returned handle is ()
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 void mytest () 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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.