如何链接到特定的glibc版本?

时间:2021-11-05 23:02:03

When I compile something on my Ubuntu Lucid 10.04 PC it gets linked against glibc. Lucid uses 2.11 of glibc. When I run this binary on another PC with an older glibc, the command fails saying there's no glibc 2.11...

当我在我的Ubuntu Lucid 10.04上编译一些东西时,它会被链接到glibc上。Lucid使用了2.11的glibc。当我在另一台装有旧glibc的PC上运行这个二进制文件时,命令失败,说没有glibc 2.11……

As far as I know glibc uses symbol versioning. Can I force gcc to link against a specific symbol version?

据我所知,glibc使用符号版本控制。我可以强制gcc链接到特定的符号版本吗?

In my concrete use I try to compile a gcc cross toolchain for ARM.

在我的具体使用中,我尝试编译一个ARM的gcc交叉工具链。

2 个解决方案

#1


55  

You are correct in that glibc uses symbol versioning. If you are curious, the symbol versioning implementation introduced in glibc 2.1 is described here and is an extension of Sun's symbol versioning scheme described here.

您在glibc中使用符号版本控制是正确的。如果您好奇,这里将介绍glibc 2.1中引入的符号版本控制实现,它是这里描述的Sun符号版本控制方案的扩展。

One option is to statically link your binary. This is probably the easiest option.

一种选择是静态地链接二进制文件。这可能是最简单的选择。

You could also build your binary in a chroot build environment, or using a glibc-new => glibc-old cross-compiler.

您还可以在chroot构建环境中构建二进制文件,或者使用glibc-new => glibc-old交叉编译器。

According to the http://www.trevorpounds.com blog post Linking to Older Versioned Symbols (glibc), it is possible to to force any symbol to be linked against an older one so long as it is valid by using the the same .symver pseudo-op that is used for defining versioned symbols in the first place. The following example is excerpted from the blog post.

根据http://www.trevorpounds.com的博客链接到旧的版本化的符号(glibc),有可能迫使任何符号有关对一个年长的人只要是有效利用同一.symver伪运算中用于定义版本化的符号。以下示例摘自博客文章。

The following example makes use of glibc’s realpath, but makes sure it is linked against an older 2.2.5 version.

下面的示例使用了glibc的realpath,但是要确保它与旧的2.2.5版本相链接。

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

__asm__(".symver realpath,realpath@GLIBC_2.2.5");
int main()
{
    char* unresolved = "/lib64";
    char  resolved[PATH_MAX+1];

    if(!realpath(unresolved, resolved))
        { return 1; }

    printf("%s\n", resolved);

    return 0;
}

#2


17  

Link with -static. When you link with -static the linker embeds the library inside the executable, so the executable will be bigger, but it can be executed on a system with an older version of glibc because the program will use it's own library instead of that of the system.

与静态链接。当链接到-static时,链接器将库嵌入到可执行文件中,因此可执行文件将会更大,但是它可以在使用glibc的旧版本的系统上执行,因为程序将使用它自己的库而不是系统的库。

#1


55  

You are correct in that glibc uses symbol versioning. If you are curious, the symbol versioning implementation introduced in glibc 2.1 is described here and is an extension of Sun's symbol versioning scheme described here.

您在glibc中使用符号版本控制是正确的。如果您好奇,这里将介绍glibc 2.1中引入的符号版本控制实现,它是这里描述的Sun符号版本控制方案的扩展。

One option is to statically link your binary. This is probably the easiest option.

一种选择是静态地链接二进制文件。这可能是最简单的选择。

You could also build your binary in a chroot build environment, or using a glibc-new => glibc-old cross-compiler.

您还可以在chroot构建环境中构建二进制文件,或者使用glibc-new => glibc-old交叉编译器。

According to the http://www.trevorpounds.com blog post Linking to Older Versioned Symbols (glibc), it is possible to to force any symbol to be linked against an older one so long as it is valid by using the the same .symver pseudo-op that is used for defining versioned symbols in the first place. The following example is excerpted from the blog post.

根据http://www.trevorpounds.com的博客链接到旧的版本化的符号(glibc),有可能迫使任何符号有关对一个年长的人只要是有效利用同一.symver伪运算中用于定义版本化的符号。以下示例摘自博客文章。

The following example makes use of glibc’s realpath, but makes sure it is linked against an older 2.2.5 version.

下面的示例使用了glibc的realpath,但是要确保它与旧的2.2.5版本相链接。

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>

__asm__(".symver realpath,realpath@GLIBC_2.2.5");
int main()
{
    char* unresolved = "/lib64";
    char  resolved[PATH_MAX+1];

    if(!realpath(unresolved, resolved))
        { return 1; }

    printf("%s\n", resolved);

    return 0;
}

#2


17  

Link with -static. When you link with -static the linker embeds the library inside the executable, so the executable will be bigger, but it can be executed on a system with an older version of glibc because the program will use it's own library instead of that of the system.

与静态链接。当链接到-static时,链接器将库嵌入到可执行文件中,因此可执行文件将会更大,但是它可以在使用glibc的旧版本的系统上执行,因为程序将使用它自己的库而不是系统的库。