博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCC选项_-Wl,-soname
阅读量:4153 次
发布时间:2019-05-25

本文共 4412 字,大约阅读时间需要 14 分钟。

http://blog.csdn.net/gohome520/article/details/7259450

-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]   
  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]   
  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]   
  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]   
  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]   
  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]   
  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

直观理解,加上soname之后,要想修改这个名字,只能用软链接到这个库,修改软链接的名字。连接到可执行文件中去的名字还是soname

你可能感兴趣的文章
【Python基础2】python字符串方法及格式设置
查看>>
【Python】random生成随机数
查看>>
【Python基础3】数字类型与常用运算
查看>>
【Python基础4】for循环、while循环与if分支
查看>>
【Python基础6】格式化字符串
查看>>
【Python基础7】字典
查看>>
【Python基础8】函数参数
查看>>
【Python基础9】浅谈深浅拷贝及变量赋值
查看>>
Jenkins定制一个具有筛选功能的列表视图
查看>>
【Python基础10】探索模块
查看>>
【Python】将txt文件转换为html
查看>>
[Linux]Shell脚本实现按照模块信息拆分文件内容
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
在C++中,关键字explicit有什么作用
查看>>
C++中异常的处理方法以及使用了哪些关键字
查看>>
内存分配的形式有哪些? C++
查看>>
什么是内存泄露,如何避免内存泄露 C++
查看>>
栈和堆的空间大小 C++
查看>>
什么是缓冲区溢出 C++
查看>>