Picking glibc versions at runtime | Hugonweb Annotated Link Bibliography

https://blogsystem5.substack.com/p/glibc-versions-runtime

How do you test code against a different version of libc than the system default one? You can't reliably use an alternate libc by putting it in LD_LIBRARY_PATH. It will often crash.

You might think that the kernel is somehow responsible for dealing with dynamic libraries, but that’s not the case. Dynamic libraries are a user-space concept and this is precisely where the [ELF] interpreter [ld-linux.so] comes into play.

To summarize, glibc ships both the ld-linux.so dynamic linker and the libc.so.6 shared library. The two are closely coupled. Running a binary against the C library without also using a matching dynamic linker can lead to crashes. But running a binary directly with a specific dynamic linker ensures that the binary picks up the matching C library.

At compile time, you can include the interpreter ld-linux with:

bash gcc -o myexe -Wl,--dynamic-linker=/wherever/ld-linux-x86-64.so.2 mycode.c

At runtime, you can use patchelf to replace the reference to the interpreter in the binary:

bash patchelf --set-interpreter /wherever/ld-linux-x86-64.so.2 myexe

Keep in mind that glibc backwards compatibility means a newer glibc will work with code compiled with an older version, but not the other way around.