Linux DLL hell--link library real name, soname, link name

Source: Internet
Author: User

DLL hell refers to a new version of a dynamic library on a Windows system that overwrites an older version, and the new version is not compatible with the old version. For example: Install new software, but the original software does not work. Linux system is also facing the same as Windows Dynamic Library Multi-version problem, which seriously affect the software upgrade and maintenance.

So how is this problem solved?
Linux system to solve this problem, introduced a set of mechanisms, if you follow this mechanism to do, you can avoid this problem.
But it's an agreement, not a compulsion.
However, it is recommended to adhere to this Convention, otherwise the Linux system version of the DLL hell problem.

Here is an introduction to this mechanism.
This mechanism uses the file name to control the version of the shared library, which has three names, each with different purposes.

1) The first one is the actual file name of the shared library (real name),
It is the file name of the compiler when it generates a shared library or a person modifies the name, and the actual file name is intended to visually control the shared library version.
The format is: lib + math +. So + major version + minor version number + production number
such as: libmath.so.1.1.1234.

Lib is the contract prefix name of a library on a Linux system,
Math is the name of the library itself,
So is the suffix name of the shared library,
1.1.1234 is the version number of the shared library,
Format: Major version number + minor version number + make (build) number.
Major version number-represents the version of the current shared library,
If the interface function provided by the shared library changes, then this version number will be added one (1);
Minor version number-if a new feature (Feature) is introduced, then this version number will be added one (1);
Production number-generally only fixes a bug.

2) The second is a short file name for the shared library (Soname-short for shared object name),
It is the name of the file that the executable program loads when it is looking for.
The format is: lib + math +. So + major version number
such as: libmath.so.1
Note: When a compilation link is delivered to a shared library with a real file name, a short file name is also written into the file header of the shared library.
You can use this command to view: $readelf The actual file name of the-D shared Library

3) The third is the connection file name of the shared library (link name),
Is the name that is used to create the stage link shared library specifically for the executable program, without any version information.
The format is: lib + math +. So
such as: libmath.so. When the executable program links the shared library: The connection file name of the shared library is used first, the shared library is found by the connection file name, and the short file name of the shared library is removed and written in the shared library's own file header.
When an executable program loads a shared library: Find a shared library under a given path with a short file name for the shared library.  Example: A source code//hello.hvoid hello (); Hello.c#include "Hello.h"
#include <stdio.h>
void Hello () {
printf ("Libhello");
}//main.c#include "Hello.h"
int main () {
Hello ();
} two builds 1. Generate a dynamic-link library # gcc Hello.c-shared-fpic-wl,-soname,libhello.so.0-o libhello.so.0.0.0   2. Run readelf-d libhello.so.0.0.0 display: [Email protected] tst]$ readelf-d libhello.so.0.0.0
Dynamic section at offset 0xe08 contains entries:
Tag Type Name/value
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000e (SONAME) Library SONAME: [libhello.so.0]
0x000000000000000c (INIT) 0x588
0x000000000000000d (FINI) 0x708
0x0000000000000019 (Init_array) 0x200de8
0X000000000000001B (init_arraysz) 8 (bytes)
0X000000000000001A (Fini_array) 0x200df0
0X000000000000001C (fini_arraysz) 8 (bytes)
0X000000006FFFFEF5 (Gnu_hash) 0x1f0
0x0000000000000005 (Strtab) 0x380
0x0000000000000006 (Symtab) 0x230
0x000000000000000a (Strsz) (bytes)
0X000000000000000B (syment) (bytes)
0x0000000000000003 (Pltgot) 0x201000
0X0000000000000002 (PLTRELSZ) (bytes)
0x0000000000000014 (Pltrel) RELA
0x0000000000000017 (Jmprel) 0x540
0x0000000000000007 (RELA) 0x480
0x0000000000000008 (Relasz) 192 (bytes)
0x0000000000000009 (relaent) (bytes)
0x000000006ffffffe (verneed) 0x460
0X000000006FFFFFFF (verneednum) 1
0X000000006FFFFFF0 (Versym) 0x43e
0X000000006FFFFFF9 (Relacount) 3
0x0000000000000000 (NULL) 0x0
  3. Execute ldconfig-vn. (a bit), generate soname in the current directory County link to libhello.so.0.0.0, at this point: before running: [email protected] tst]$ LL
Total 20
-rw-rw-r--. 1 Qingze qingze 13:47 hello.c
-rw-rw-r--. 1 Qingze qingze 12:44 hello.h
-rwxrwxr-x. 1 Qingze qingze 8005 Nov 15:19 libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 15:26 main.c
after running: [email protected] tst]$ LL
Total 20
-rw-rw-r--. 1 Qingze qingze 13:47 hello.c
-rw-rw-r--. 1 Qingze qingze 12:44 hello.h
lrwxrwxrwx. 1 Qingze qingze 15:30 libhello.so.0-libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 8005 Nov 15:19 libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 15:26 main.c
  4. Run Ln-s libhello.so.0 libhello.so:
[email protected] tst]$ LL
Total 20
-rw-rw-r--. 1 Qingze qingze 13:47 hello.c
-rw-rw-r--. 1 Qingze qingze 12:44 hello.h
lrwxrwxrwx. 1 Qingze qingze 15:33 libhello.so-libhello.so.0
lrwxrwxrwx. 1 Qingze qingze 15:30 libhello.so.0-libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 8005 Nov 15:19 libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 15:26 main.c
  5. Run gcc main.c-o main-wl,-rpath=./-L-lhello
[email protected] tst]$ LL
Total 32
-rw-rw-r--. 1 Qingze qingze 13:47 hello.c
-rw-rw-r--. 1 Qingze qingze 12:44 hello.h
lrwxrwxrwx. 1 Qingze qingze 15:33 libhello.so-libhello.so.0
lrwxrwxrwx. 1 Qingze qingze 15:30 libhello.so.0-libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 8005 Nov 15:19 libhello.so.0.0.0
-rwxrwxr-x. 1 Qingze qingze 8538 Nov 15:36 Main
-rwxrwxr-x. 1 Qingze qingze 15:26 main.c
 

When GCC compiles a link to a dynamic library, it is very likely that it will compile, but when it does, it cannot find the dynamic link library, which is

Because the path specified by the-l option is only valid at compile time, the compiled executable does not know the value after the-l option,

Of course I can't find them. You can use LDD <your_execute> to see if there is no ' not found ' behind the library you are linking to,

The workaround is to use-WL,RPATH=<YOUR_LIB_DIR>, which enables execute to remember the location of the link library

Personal Understanding:

Refer to some online information, step by step implementation of the error, I follow the above method success, so I think when the dynamic link library version changes as long as the 4th step, the libhello.so link to the new version!

Awaiting verification!!!

Linux DLL hell--link library real name, soname, link name

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.