GCC option _-wl,-soname

Source: Internet
Author: User

The-wl option tells the compiler to pass the following parameters to the linker.

-Soname: Specifies the soname (simple sharing name, short for shared object name) of the dynamic library)

The key feature of soname is that it provides compatibility standards:

When you want to upgrade a database in the system, and the soname of the new database is the same as that of the old database, the program generated using the old library link can still run normally. This feature makes it easy to upgrade the shared library program and locate errors in Linux.

In Linux, the application uses soname to specify the version of the desired database. The database Author can declare which versions are compatible by retaining or changing the soname, this frees programmers from the problem of version conflicts in shared libraries.

You can use readelf-D to view the soname of each dynamic library.

1. Declare libto. so.1 and generate libto. so.1.2

[root@localhost c]# gcc -fPIC -shared -Wl,-soname,libto.so.1 -o libto.so.1.2 to.c[root@localhost c]# ls -lh-rwxr-xr-x 1 root root 4268 Jan 10 17:22 libto.so.1.2[root@localhost c]# ldconfig -n ./lrwxrwxrwx 1 root root   12 Jan 10 17:23 libto.so.1 -> libto.so.1.2-rwxr-xr-x 1 root root 4.2K Jan 10 17:22 libto.so.1.2
[root@localhost c]# readelf -d libto.so.1.2Dynamic section at offset 0x504 contains 21 entries:  Tag        Type                         Name/Value 0x00000001 (NEEDED)                     Shared library: [libc.so.6] 0x0000000e (SONAME)                     Library soname: [libto.so.1] 0x0000000c (INIT)                       0x2cc 0x0000000d (FINI)                       0x4c4 0x6ffffef5 (GNU_HASH)                   0xb4 0x00000005 (STRTAB)                     0x1b4 0x00000006 (SYMTAB)                     0xf4 0x0000000a (STRSZ)                      150 (bytes) 0x0000000b (SYMENT)                     16 (bytes) 0x00000003 (PLTGOT)                     0x15d8 0x00000002 (PLTRELSZ)                   24 (bytes) 0x00000014 (PLTREL)                     REL 0x00000017 (JMPREL)                     0x2b4 0x00000011 (REL)                        0x294 0x00000012 (RELSZ)                      32 (bytes) 0x00000013 (RELENT)                     8 (bytes) 0x6ffffffe (VERNEED)                    0x264 0x6fffffff (VERNEEDNUM)                 1 0x6ffffff0 (VERSYM)                     0x24a 0x6ffffffa (RELCOUNT)                   1 0x00000000 (NULL)                       0x0

2. Declare libto. so.1 and generate libto. so.1.3

[Root @ localhost C] # gcc-FPIC-shared-wl,-soname, libto. so.1-O libto. so.1.3. c [root @ localhost C] # ls-lhlrwxrwxrwx 1 Root 12 Jan 10 libto. so.1-> libto. so.1.2-rwxr-XR-x 1 Root 4.2 K Jan 10 libto. so.1.2-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 23 libto. so.1.3 [root @ localhost C] # ldconfig-n. /lrwxrwxrwx 1 Root 12 Jan 10 17:24 libto. so.1-> libto. so.1.3 # re-ldconfig to point to the new library file-rwxr-XR-x 1 Root 4.2 K Jan 10 libto. so.1.2-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 23 libto. so.1.3
[root@localhost c]# readelf -d libto.so.1.3Dynamic section at offset 0x504 contains 21 entries:  Tag        Type                         Name/Value 0x00000001 (NEEDED)                     Shared library: [libc.so.6] 0x0000000e (SONAME)                     Library soname: [libto.so.1] 0x0000000c (INIT)                       0x2cc 0x0000000d (FINI)                       0x4c4 0x6ffffef5 (GNU_HASH)                   0xb4 0x00000005 (STRTAB)                     0x1b4 0x00000006 (SYMTAB)                     0xf4 0x0000000a (STRSZ)                      150 (bytes) 0x0000000b (SYMENT)                     16 (bytes) 0x00000003 (PLTGOT)                     0x15d8 0x00000002 (PLTRELSZ)                   24 (bytes) 0x00000014 (PLTREL)                     REL 0x00000017 (JMPREL)                     0x2b4 0x00000011 (REL)                        0x294 0x00000012 (RELSZ)                      32 (bytes) 0x00000013 (RELENT)                     8 (bytes) 0x6ffffffe (VERNEED)                    0x264 0x6fffffff (VERNEEDNUM)                 1 0x6ffffff0 (VERSYM)                     0x24a 0x6ffffffa (RELCOUNT)                   1 0x00000000 (NULL)                       0x0

3. Declare libto. so.2 and generate libto. so.1.4

[Root @ localhost C] # gcc-FPIC-shared-wl,-soname, libto. so.2-O libto. so.1.4. c [root @ localhost C] # ls-lhlrwxrwxrwx 1 Root 12 Jan 10 17: 24 libto. so.1-> libto. so.1.3-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 22 libto. so.1.2-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 23 libto. so.1.3-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 24 libto. so.1.4 [root @ localhost C] # ldconfig-n. /lrwxrwxrwx 1 Root 12 Jan 10 17:24 libto. so.1-> libto. so.1.3 # re-ldconfig and do not point to the new library file, because the soname of the new library (1.4) is libto. so.2-rwxr-XR-x 1 Root 4.2 K Jan 10 libto. so.1.2-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 23 libto. so.1.3-rwxr-XR-x 1 Root 4.2 K Jan 10 17: 24 libto. so.1.4lrwxrwxrwx 1 Root 12 Jan 10 17: 24 libto. so.2-> libto. so.1.4
[root@localhost c]# readelf -d libto.so.1.4Dynamic section at offset 0x504 contains 21 entries:  Tag        Type                         Name/Value 0x00000001 (NEEDED)                     Shared library: [libc.so.6] 0x0000000e (SONAME)                     Library soname: [libto.so.2] 0x0000000c (INIT)                       0x2cc 0x0000000d (FINI)                       0x4c4 0x6ffffef5 (GNU_HASH)                   0xb4 0x00000005 (STRTAB)                     0x1b4 0x00000006 (SYMTAB)                     0xf4 0x0000000a (STRSZ)                      150 (bytes) 0x0000000b (SYMENT)                     16 (bytes) 0x00000003 (PLTGOT)                     0x15d8 0x00000002 (PLTRELSZ)                   24 (bytes) 0x00000014 (PLTREL)                     REL 0x00000017 (JMPREL)                     0x2b4 0x00000011 (REL)                        0x294 0x00000012 (RELSZ)                      32 (bytes) 0x00000013 (RELENT)                     8 (bytes) 0x6ffffffe (VERNEED)                    0x264 0x6fffffff (VERNEEDNUM)                 1 0x6ffffff0 (VERSYM)                     0x24a 0x6ffffffa (RELCOUNT)                   1 0x00000000 (NULL)                       0x0

Summary: major upgrades to the library will damage the compatibility, but minor upgrades may not. In this case, everything will be done in the following ways.
Gcc-shared-wl,-soname, libfoo. So. Major-O libfoo. So. Major. Minor

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.