Search code examples
clinuxglibcdynamic-linking

Why does libc depend on ld.so?


I always think that libc should be an independent dynamic library, until I find this:

$ ldd /lib/x86_64-linux-gnu/libc.so.6
        /lib64/ld-linux-x86-64.so.2 (0x00007fd743c00000)
        linux-vdso.so.1 (0x00007fffc75f4000)

Can someone tell me why libc needs ld.so and what function it uses?


Solution

  • ld.so is needed by any application or shared library that is linked with shared libraries:

    The programs ld.so and ld-linux.so* find and load the shared objects (shared libraries) needed by a program, prepare the program to run, and then run it.

    Applications don't normally invoke any functions from ld-linux-x86-64.so, rather it loads the executable and shared libraries and passes the control flow to the application, which is normally C and C++ library runtime initialization code. Such a dependency on ld-linux.so* is established with .interp section of ELF file (see readelf -l /lib/x86_64-linux-gnu/libc.so.6 output) and that is not what ldd shows.

    ldd, however, (recursively) shows libraries marked as NEEDED in dynamic section (see readelf -d /lib/x86_64-linux-gnu/libc.so.6 output). On Linux, thread local storage support for shared libraries is implemented by /lib64/ld-linux-x86-64.so.2. That is an implementation detail, but is the reason glibc depends on ld-linux-x86-64.so.