Linux Dynamic Link Library Loading

Source: Internet
Author: User

Linux Dynamic Link

Applications in Linux can be linked to external functions in either of the following ways (lib*.a)Static LinkAnd include the library code in the executable file of the application; or (lib*.so)Dynamic Link. The dynamic link loader maps the dynamic library to the executable memory of the application. Before starting the application, the dynamic link loader maps the required shared target Library to the application memory, or uses the system shared destination to parse the required external references for the application. Now the application can run.

As an example, the following is a small program that demonstrates the default use of dynamic link libraries in Linux:

main(){   printf("Hello world");}

 

When you use GCC to compile hello. C,a.out. Use Linux commandsldd a.out(This command prints the mutual dependency of the shared library.) It can be seen that the required shared library is:

        libc.so.6 => /lib/libc.so.6 (0x4001d000)         /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

 

Use the same dynamic link loader to map the DLL to the application memory after the application program runs. By using the Linux dynamic loader routine, the application controls the dynamic library to be loaded and the function in the library to execute the loading and link and return the address of the required entry point.

 



Back to Top

Linux DLL Functions

Linux provides four library functions (dlopen,dlerror,dlsymAnddlclose), An include file (dlfcn.h) And two shared libraries (static databaseslibdl.aAnd dynamic librarylibdl.so) To support dynamic link loaders. These library functions are:

  • DlopenOpen the shared target file, map it To the memory, and return a handle.
  • DlsymReturns a pointer to the requested entry point.
  • DlerrorReturns null or a pointer to the ASCII string that describes the most recent error.
  • DlcloseClose the handle and cancel the ing of the shared target file

The dynamic link loader routine dlopen needs to find the shared target file in the file system to open the file and create a handle. There are four ways to specify the file location:

  • dlopen callAbsolute file path in
  • In the directory specified in the LD_LIBRARY_PATH environment variable
  • In the library List specified in/etc/lD. So. Cache
  • First in/usr/lib, then in/lib

 



Back to Top

DLL example: small C program and dltest

The dynamic link loader sample program is a small C program designed to practice DL routines. This program is based on a C program compiled by everyone. It prints "Hello World" to the console. The first printed message is "Hello World ". The test program links to the two functions that print the message again: the first time, all uppercase characters, and the second time, all lowercase characters.

The following is a summary of the program:

 

  1. Use the absolute path "/home/dltest/lib library. So" and the option rtld_lazy,dlopenOpen the DLL shared destination file and return the handle.
    1. Option rtld_lazy delays parsing the DLL's external reference until the DLL is executed.

    2. Option rtld_now indlopenReturns all external references resolved previously.
  2. dlsymReturns the function address of the entry point.
  3. dlcloseClose the handle to Lib. So and cancel the DLL ing from the memory.
  4. dlopenUse the relative path of the Environment Variable LD_LIBRARY_PATH to find the shared Destination path, open the DLL shared destination file lowercase. So, and return a handle.

Note: Each calldlopen,dlsymOrdlcloseThen, calldlerrorTo obtain the final error message and print the string of the error message.

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.