Knowledge about dynamic libraries in linux

Source: Internet
Author: User

The problem arises from the use of a dynamic library of glib2-2.28.8 when compiling a program, while the system comes with a glib2-2.22.5

If you do not want to upgrade the glib2 library of the system, you can use the library files that come with the program to load it (similar to windows systems, the DLL files in the current directory are preferentially loaded ).

1. At first compilation, use-L to specify the lib path,-lglib-2.0 to specify the library file name

2. When running, use LD_LIBRARY_PATH =./to specify the current directory as the library directory.

3. The test shows that the system library is used.

Ldd./test
Linux-vdso.so.1 = & gt; (0x00007fff86fff000)
Libglib-2.0.so.0 =>/lib64/libglib-2.0.so.0 (0x00007f0000e16c000)
Libgmodule-2.0.so.0 =>/lib64/libgmodule-2.0.so.0 (0x00007f0000df68000)
Libz. so.1 =>/lib64/libz. so.1 (0x00007f0000dd52000)
Libc. so.6 =>/lib64/libc. so.6 (0x00007f0000d9bf000)
Libdl. so.2 =>/lib64/libdl. so.2 (0x00007f0000d7bb000)
/Lib64/ld-linux-x86-64.so.2 (0x00007f0000e45f000)

The library path is at the end of the queue in the search path, causing priority to find the system library. Can you modify the "Linked Library File Name" to achieve the goal?

1. Modify the libglib-2.0.so file to libmyglib. so, and use-lmyglib to specify the link library when linking

2. When running, use LD_LIBRARY_PATH =./to specify the current directory as the library directory.

3.

Ldd./test
Linux-vdso.so.1 = & gt; (0x00007fff86fff000)
Libglib-2.0.so.0 =>/lib64/libglib-2.0.so.0 (0x00007f0000e16c000)
Libgmodule-2.0.so.0 =>/lib64/libgmodule-2.0.so.0 (0x00007f0000df68000)
Libz. so.1 =>/lib64/libz. so.1 (0x00007f0000dd52000)
Libc. so.6 =>/lib64/libc. so.6 (0x00007f0000d9bf000)
Libdl. so.2 =>/lib64/libdl. so.2 (0x00007f0000d7bb000)
/Lib64/ld-linux-x86-64.so.2 (0x00007f0000e45f000)

"When an executable file is generated, if it is linked to a static library, the linker will relocate the corresponding symbol reference according to the static link rules. If it is a dynamic library, the linker will mark this symbol as a dynamic link. It will not be relocated, but will be carried out during loading. Therefore, although it is a dynamic link, if it is already in the Link stage, it also needs to be in the corresponding. so, otherwise the Undefined reference to link error will be triggered. Because the linker can only use the. so file to determine whether a symbol is a dynamic link symbol, you also need to read these. so files and find the definition of the corresponding symbol"

This means that the connector only makes sure that the function symbol is available and compilation is passed. The file name of the dynamic library is not recorded here, which is different from that of WINDOWS.

During actual loading, only function symbols are used to load the required information. SO, you can index my libmyglib. so was not loaded in, and replaced by the system.

1. LD_PRELOAD can solve the loading order problem.

 

LD_LIBRARY_PATH adds other directories to the search path. Its content should be a colon
Directory List, which is in the same format as the PATH variable of the executable file.
If you call a program that sets the user ID or process ID, this variable is ignored.

 

LD_PRELOAD first loads User-Defined libraries so that they have the opportunity to overwrite or redefine standard libraries.

Export LD_PRELOAD =./libglib. so:./libgmodule. so

Ldd./test
Linux-vdso.so.1 = & gt; (0x00007fffc31ff000)
./Libglib. so (0x00007ffa5d757000)
./Libgmodule. so (0x00007ffa5d553000)
Libz. so.1 =>/lib64/libz. so.1 (0x00007ffa5d330000)
Libc. so.6 =>/lib64/libc. so.6 (0x00007ffa5cf9d000)
Librt. so.1 =>/lib64/librt. so.1 (0x00007ffa5cd95000)
Libdl. so.2 =>/lib64/libdl. so.2 (0x00007ffa5cb91000)
/Lib64/ld-linux-x86-64.so.2 (0x00007ffa5da6e000)
Libpthread. so.0 =>/lib64/libpthread. so.0 (0x00007ffa5c974000)

 

2. There are three methods to replace the glib2 library of the system. The first method is recommended.

2.1 upgrade and installation coverage

2.2 directly copy. so override, or change the point of the libglib-2.0.so.0 Link

2.3 share the current version of the library system sudo ldconfig/opt/glib-2.28.8/lib

 

Ldd ../bin/test
Linux-vdso.so.1 => (0x00007fff897ff000)
Libglib-2.0.so.0 =>/opt/glib-2.28.8/lib/libglib-2.0.so.0 (0x00007f59f5f02000)
Libgmodule-2.0.so.0 =>/opt/glib-2.28.8/lib/libgmodule-2.0.so.0 (0x00007f59f5cfe000)
Libz. so.1 =>/lib64/libz. so.1 (0x00007f59f5ae8000)
Libc. so.6 =>/lib64/libc. so.6 (0x00007f59f5755000)
Librt. so.1 =>/lib64/librt. so.1 (0x00007f59f554d000)
Libdl. so.2 =>/lib64/libdl. so.2 (0x00007f59f5349000)
/Lib64/ld-linux-x86-64.so.2 (0x00007f59f6226000)
Libpthread. so.0 =>/lib64/libpthread. so.0 (0x00007f59f512c000)

LD_DEBUG by setting this environment variable, you can easily see the loader loading process (including the Library Loading and symbol parsing processes ), use [LD_DEBUG = help executable file path] to view help.
 
The shared library specified by the LD_PRELOAD environment variable will be pre-loaded. If a function with the same name appears, the pre-loaded function will be called, for example, if a pre-loaded library contains a user-defined puts function, the User-Defined version of The puts function will be used when executing the program, rather than the puts function in the libc library.
 

List link symbols

Nm-Du/lib64/libglib-2.0.so.0

Related Article

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.