GCC选项_-Wl,-soname

-Wl选项告诉编译器将后面的参数传递给链接器。

-soname则指定了动态库的soname(简单共享名,Short for shared object name)


soname的关键功能是它提供了兼容性的标准:

当要升级系统中的一个库时,并且新库的soname和老库的soname一样,用旧库链接生成的程序使用新库依然能正常运行。这个特性使得在Linux下,升级使得共享库的程序和定位错误变得十分容易。

在Linux中,应用程序通过使用soname,来指定所希望库的版本,库作者可以通过保留或改变soname来声明,哪些版本是兼容的,这使得程序员摆脱了共享库版本冲突问题的困扰。

可以通过readelf -d来查看每个动态库的SONAME


1. 声明libto.so.1,并生成libto.so.1.2

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

2. 声明libto.so.1,并生成libto.so.1.3

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

3. 声明libto.so.2,并生成libto.so.1.4
[plain]  view plain copy
  1. [root@localhost c]# gcc -fPIC -shared -Wl,-soname,libto.so.2 -o libto.so.1.4 to.c  
  2. [root@localhost c]# ls -lh  
  3. lrwxrwxrwx 1 root root   12 Jan 10 17:24 libto.so.1 -> libto.so.1.3  
  4. -rwxr-xr-x 1 root root 4.2K Jan 10 17:22 libto.so.1.2  
  5. -rwxr-xr-x 1 root root 4.2K Jan 10 17:23 libto.so.1.3  
  6. -rwxr-xr-x 1 root root 4.2K Jan 10 17:24 libto.so.1.4  
  7. [root@localhost c]# ldconfig -n ./  
  8. lrwxrwxrwx 1 root root   12 Jan 10 17:24 libto.so.1 -> libto.so.1.3  #重新ldconfig,不指向新的库文件,因为新库(1.4)的soname为libto.so.2  
  9. -rwxr-xr-x 1 root root 4.2K Jan 10 17:22 libto.so.1.2  
  10. -rwxr-xr-x 1 root root 4.2K Jan 10 17:23 libto.so.1.3  
  11. -rwxr-xr-x 1 root root 4.2K Jan 10 17:24 libto.so.1.4  
  12. lrwxrwxrwx 1 root root   12 Jan 10 17:24 libto.so.2 -> libto.so.1.4  
[plain]  view plain copy
  1. [root@localhost c]# readelf -d libto.so.1.4  
  2.   
  3.   
  4. Dynamic section at offset 0x504 contains 21 entries:  
  5.   Tag        Type                         Name/Value  
  6.  0x00000001 (NEEDED)                     Shared library: [libc.so.6]  
  7.  0x0000000e (SONAME)                     Library soname: [libto.so.2]  
  8.  0x0000000c (INIT)                       0x2cc  
  9.  0x0000000d (FINI)                       0x4c4  
  10.  0x6ffffef5 (GNU_HASH)                   0xb4  
  11.  0x00000005 (STRTAB)                     0x1b4  
  12.  0x00000006 (SYMTAB)                     0xf4  
  13.  0x0000000a (STRSZ)                      150 (bytes)  
  14.  0x0000000b (SYMENT)                     16 (bytes)  
  15.  0x00000003 (PLTGOT)                     0x15d8  
  16.  0x00000002 (PLTRELSZ)                   24 (bytes)  
  17.  0x00000014 (PLTREL)                     REL  
  18.  0x00000017 (JMPREL)                     0x2b4  
  19.  0x00000011 (REL)                        0x294  
  20.  0x00000012 (RELSZ)                      32 (bytes)  
  21.  0x00000013 (RELENT)                     8 (bytes)  
  22.  0x6ffffffe (VERNEED)                    0x264  
  23.  0x6fffffff (VERNEEDNUM)                 1  
  24.  0x6ffffff0 (VERSYM)                     0x24a  
  25.  0x6ffffffa (RELCOUNT)                   1  
  26.  0x00000000 (NULL)                       0x0  


总结:程式库主要的升级会破坏相容性;而次要的升级则可能不会;那麽以下面的方式来连结,所有的一切就都会相安无事了。 
gcc -shared -Wl,-soname,libfoo.so.major -o libfoo.so.major.minor
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值