Where do object file "Version References" come from?

时间:2016-12-09 13:07:06

标签: c++ c gcc linker

Currently I'm in a directory which has a file libshared-object.so (name changed for generality).

When I run

$ objdump -p libshared-object.so

I receive the following output:

libshared-object.so:     file format elf64-x86-64

Program Header:
        LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**21
                 filesz 0x00000000000828ee memsz 0x00000000000828ee flags r-x
        LOAD off    0x0000000000083768 vaddr 0x0000000000283768 paddr 0x0000000000283768 align 2**21
                 filesz 0x00000000000048e0 memsz 0x0000000000004af0 flags rw-
 DYNAMIC off    0x0000000000084af0 vaddr 0x0000000000284af0 paddr 0x0000000000284af0 align 2**3
                 filesz 0x00000000000002a0 memsz 0x00000000000002a0 flags rw-
        NOTE off    0x00000000000001c8 vaddr 0x00000000000001c8 paddr 0x00000000000001c8 align 2**2
                 filesz 0x0000000000000024 memsz 0x0000000000000024 flags r--
EH_FRAME off    0x0000000000072c6c vaddr 0x0000000000072c6c paddr 0x0000000000072c6c align 2**2
                 filesz 0x0000000000002ed4 memsz 0x0000000000002ed4 flags r--
     STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
                 filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
     RELRO off    0x0000000000083768 vaddr 0x0000000000283768 paddr 0x0000000000283768 align 2**0
                 filesz 0x0000000000001898 memsz 0x0000000000001898 flags r--

Dynamic Section:
    NEEDED               libQt5Widgets.so.5
    NEEDED               libQt5Compositor.so.5
    NEEDED               libQt5Quick.so.5
    NEEDED               libQt5Qml.so.5
    NEEDED               libQt5Network.so.5
    NEEDED               libQt5Gui.so.5
    NEEDED               libQt5Core.so.5
    NEEDED               libGL.so.1
    NEEDED               libpthread.so.0
    NEEDED               libstdc++.so.6
    NEEDED               libm.so.6
    NEEDED               libgcc_s.so.1
    NEEDED               libc.so.6
    SONAME               libshared-object.so.1
    RPATH                /opt/qt5/lib
    INIT                 0x000000000003fc68
    FINI                 0x000000000006c234
    INIT_ARRAY           0x0000000000283768
    INIT_ARRAYSZ         0x00000000000000e8
    FINI_ARRAY           0x0000000000283850
    FINI_ARRAYSZ         0x0000000000000008
    GNU_HASH             0x00000000000001f0
    STRTAB               0x00000000000101e8
    SYMTAB               0x00000000000036d8
    STRSZ                0x0000000000022072
    SYMENT               0x0000000000000018
    PLTGOT               0x0000000000285000
    PLTRELSZ             0x0000000000008df0
    PLTREL               0x0000000000000007
    JMPREL               0x0000000000036e78
    RELA                 0x0000000000033458
    RELASZ               0x0000000000003a20
    RELAENT              0x0000000000000018
    VERNEED              0x0000000000033348
    VERNEEDNUM           0x0000000000000006
    VERSYM               0x000000000003225a
    RELACOUNT            0x0000000000000052

Version References:
    required from libm.so.6:
        0x09691a75 0x00 09 GLIBC_2.2.5
    required from libgcc_s.so.1:
        0x0b792650 0x00 08 GCC_3.0
    required from libc.so.6:
        0x06969194 0x00 10 GLIBC_2.14
        0x09691a75 0x00 07 GLIBC_2.2.5
    required from libQt5Core.so.5:
        0x00058a25 0x00 06 Qt_5
    required from libQt5Gui.so.5:
        0x0dcbd2c9 0x00 12 Qt_5_PRIVATE_API
        0x00058a25 0x00 03 Qt_5
    required from libstdc++.so.6:
        0x0bafd178 0x00 11 CXXABI_1.3.8
        0x056bafd3 0x00 05 CXXABI_1.3
        0x0297f871 0x00 04 GLIBCXX_3.4.21
        0x08922974 0x00 02 GLIBCXX_3.4

Of particular interest is the very last of this information, the Version References:

Version References:
    required from libm.so.6:
        0x09691a75 0x00 09 GLIBC_2.2.5
    required from libgcc_s.so.1:
        0x0b792650 0x00 08 GCC_3.0
    required from libc.so.6:
        0x06969194 0x00 10 GLIBC_2.14
        0x09691a75 0x00 07 GLIBC_2.2.5
    required from libQt5Core.so.5:
        0x00058a25 0x00 06 Qt_5
    required from libQt5Gui.so.5:
        0x0dcbd2c9 0x00 12 Qt_5_PRIVATE_API
        0x00058a25 0x00 03 Qt_5
    required from libstdc++.so.6:
        0x0bafd178 0x00 11 CXXABI_1.3.8
        0x056bafd3 0x00 05 CXXABI_1.3
        0x0297f871 0x00 04 GLIBCXX_3.4.21
        0x08922974 0x00 02 GLIBCXX_3.4

Question: Where do these version references come from? Take, for example, the line required from libQt5Gui.so.5: .. Qt_5 and Qt_5_PRIVATE_API.

Are references to version Qt_5 and Qt_5_PRIVATE_API coming from the C code that generated libQt5Gui.so.5? Or from some linker option passed to to gcc or ld? Or from something else?

1 个答案:

答案 0 :(得分:5)

  

或者来自别的什么?

来自别的东西。

当您构建共享库(比如libfoo.so)时,您可以(尽管不必)提供链接器版本脚本,为某些符号提供版本标记。

如果稍后将可执行文件或共享库(例如libbar.so)与libfoo.so链接, iff 使用版本化符号,则会记录该符号的版本标记在libbar.so中(这是您在问题中观察到的内容)。

此设置允许libfoo.so以ABI不兼容的方式更改其符号,并且仍然支持与旧符号链接的旧客户端程序。

例如,libc.so.6上的x86_64具有memcpy的以下版本:

0000000000091620 g   iD  .text  000000000000003d  GLIBC_2.14  memcpy
000000000008c420 g   iD  .text  0000000000000047 (GLIBC_2.2.5) memcpy

与glibc-2.13或更早版本相关联的程序将使用GLIBC_2.2.5版本,与glibc-2.14或更新版本链接的程序将使用GLIBC_2.14版本。

如果您尝试在具有glibc-2.13的系统上运行与glibc-2.14链接的程序,则会出现错误(缺少符号版本),类似于this

在引入符号版本控制之前,更改现有符号的ABI需要您运送完全独立的库。这称为外部库版本控制。您可以阅读更多相关信息here