Multiple glibc libraries on a single host

后端 未结 11 1538
轮回少年
轮回少年 2020-11-21 05:13

Multiple glibc libraries on a single host

My linux (SLES-8) server currently has glibc-2.2.5-235, but I have a program which won\'t work on this version and requires

相关标签:
11条回答
  • 2020-11-21 05:56

    I am not sure that the question is still relevant, but there is another way of fixing the problem: Docker. One can install an almost empty container of the Source Distribution (The Distribution used for development) and copy the files into the Container. That way You do not need to create the filesystem needed for chroot.

    0 讨论(0)
  • 2020-11-21 06:02

    Use LD_PRELOAD: put your library somewhere out of the man lib directories and run:

    LD_PRELOAD='mylibc.so anotherlib.so' program
    

    See: the Wikipedia article

    0 讨论(0)
  • 2020-11-21 06:03

    First of all, the most important dependency of each dynamically linked program is the linker. All so libraries must match the version of the linker.

    Let's take simple exaple: I have the newset ubuntu system where I run some program (in my case it is D compiler - ldc2). I'd like to run it on the old CentOS, but because of the older glibc library it is impossible. I got

    ldc2-1.5.0-linux-x86_64/bin/ldc2: /lib64/libc.so.6: version `GLIBC_2.15' not found (required by ldc2-1.5.0-linux-x86_64/bin/ldc2)
    ldc2-1.5.0-linux-x86_64/bin/ldc2: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ldc2-1.5.0-linux-x86_64/bin/ldc2)
    

    I have to copy all dependencies from ubuntu to centos. The proper method is following:

    First, let's check all dependencies:

    ldd ldc2-1.5.0-linux-x86_64/bin/ldc2 
        linux-vdso.so.1 =>  (0x00007ffebad3f000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f965f597000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f965f378000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f965f15b000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f965ef57000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f965ec01000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f965e9ea000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f965e60a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f965f79f000)
    

    linux-vdso.so.1 is not a real library and we don't have to care about it.

    /lib64/ld-linux-x86-64.so.2 is the linker, which is used by the linux do link the executable with all dynamic libraries.

    Rest of the files are real libraries and all of them together with the linker must be copied somewhere in the centos.

    Let's assume all the libraries and linker are in "/mylibs" directory.

    ld-linux-x86-64.so.2 - as I've already said - is the linker. It's not dynamic library but static executable. You can run it and see that it even have some parameters, eg --library-path (I'll return to it).

    On the linux, dynamically linked program may be lunched just by its name, eg

    /bin/ldc2
    

    Linux loads such program into RAM, and checks which linker is set for it. Usually, on 64-bit system, it is /lib64/ld-linux-x86-64.so.2 (in your filesystem it is symbolic link to the real executable). Then linux runs the linker and it loads dynamic libraries.

    You can also change this a little and do such trick:

    /mylibs/ld-linux-x86-64.so.2 /bin/ldc2
    

    It is the method for forcing the linux to use specific linker.

    And now we can return to the mentioned earlier parameter --library-path

    /mylibs/ld-linux-x86-64.so.2 --library-path /mylibs /bin/ldc2
    

    It will run ldc2 and load dynamic libraries from /mylibs.

    This is the method to call the executable with choosen (not system default) libraries.

    0 讨论(0)
  • 2020-11-21 06:03

    When I wanted to run a chromium-browser on Ubuntu precise (glibc-2.15), I got the (typical) message "...libc.so.6: version `GLIBC_2.19' not found...". I considered the fact, that files are not needed permamently, but only for start. So I collected the files needed for the browser and sudo and created a mini-glibc-2.19- environment, started the browser and then copied the original files back again. The needed files are in RAM and the original glibc is the same.

    as root
    the files (*-2.15.so) already exist 
    

    mkdir -p /glibc-2.19/i386-linux-gnu

    /glibc-2.19/ld-linux.so.2 -> /glibc-2.19/i386-linux-gnu/ld-2.19.so
    /glibc-2.19/i386-linux-gnu/libc.so.6 -> libc-2.19.so
    /glibc-2.19/i386-linux-gnu/libdl.so.2 -> libdl-2.19.so
    /glibc-2.19/i386-linux-gnu/libpthread.so.0 -> libpthread-2.19.so
    

    mkdir -p /glibc-2.15/i386-linux-gnu

    /glibc-2.15/ld-linux.so.2 -> (/glibc-2.15/i386-linux-gnu/ld-2.15.so)
    /glibc-2.15/i386-linux-gnu/libc.so.6 -> (libc-2.15.so)
    /glibc-2.15/i386-linux-gnu/libdl.so.2 -> (libdl-2.15.so)
    /glibc-2.15/i386-linux-gnu/libpthread.so.0 -> (libpthread-2.15.so)
    

    the script to run the browser:

    #!/bin/sh
    sudo cp -r /glibc-2.19/* /lib
    /path/to/the/browser &
    sleep 1
    sudo cp -r /glibc-2.15/* /lib
    sudo rm -r /lib/i386-linux-gnu/*-2.19.so
    
    0 讨论(0)
  • 2020-11-21 06:06

    It is very possible to have multiple versions of glibc on the same system (we do that every day).

    However, you need to know that glibc consists of many pieces (200+ shared libraries) which all must match. One of the pieces is ld-linux.so.2, and it must match libc.so.6, or you'll see the errors you are seeing.

    The absolute path to ld-linux.so.2 is hard-coded into the executable at link time, and can not be easily changed after the link is done.

    To build an executable that will work with the new glibc, do this:

    g++ main.o -o myapp ... \
       -Wl,--rpath=/path/to/newglibc \
       -Wl,--dynamic-linker=/path/to/newglibc/ld-linux.so.2
    

    The -rpath linker option will make the runtime loader search for libraries in /path/to/newglibc (so you wouldn't have to set LD_LIBRARY_PATH before running it), and the -dynamic-linker option will "bake" path to correct ld-linux.so.2 into the application.

    If you can't relink the myapp application (e.g. because it is a third-party binary), not all is lost, but it gets trickier. One solution is to set a proper chroot environment for it. Another possibility is to use rtldi and a binary editor.

    0 讨论(0)
提交回复
热议问题