What differences and relations are between the various `libc.so`?

7,845

They all serve different purposes:

  • /lib/i386-linux-gnu/libc.so.6 is a symbolic link to the 32-bit x86 C library, used to run 32-bit executables;
  • /lib/x86_64-linux-gnu/libc.so.6 is a symbolic link to the 64-bit x86 C library, used to run 64-bit executables;
  • /usr/lib/x86_64-linux-gnu/libc.so is (usually) a linker script pointing to the 64-bit C library (dynamic or shared, as needed) and dynamic linker, and is used to link 64-bit executables (when building them).

There are three different types of linking when building and running programs:

  • static linking: the build-time linker (ld) resolves all the objects used in the program during the build, merges the objects which are used, and produces an executable binary which doesn’t use external libraries;
  • dynamic linking, at build-time: ld resolves all objects used in the program, but instead of storing them in the executable, it only stores references to them;
  • dynamic linking, at run-time: the run-time linker (ld.so), or dynamic linker, resolves all the references stored in the executable, loading all the required libraries and updating all the object references before running the program.

The libc.so linker script provides instructions for ld, in the form of a linker script:

/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )

Usually dynamic libraries are set up using symlinks only (libfoo.so is used by ld, and points to libfoo.so.1 or whatever which is used by ld.so, and is itself typically a symlink to the currently-installed version of the library, e.g. libfoo.so.1.2.3). In the GNU C library’s case though programs being linked dynamically still need some symbols from the static library, so a linker script is used instead so that the linker can try both. The linker script also refers to the dynamic linker which will be used at runtime (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 above), and whose name is embedded in executables (in .interp).

The terms “dynamic linker” and “dynamic loader” are synonymous and used interchangeably (see the ld.so manpage).

Share:
7,845

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim over 1 year

    On Lubntu 18.04

    $ whereis libc
    libc: /usr/lib/x86_64-linux-gnu/libc.a /usr/lib/x86_64-linux-gnu/libc.so /usr/share/man/man7/libc.7.gz
    
    $ locate libc.so
    /lib/i386-linux-gnu/libc.so.6
    /lib/x86_64-linux-gnu/libc.so.6
    /usr/lib/x86_64-linux-gnu/libc.so
    
    $ ls -li /usr/lib/x86_64-linux-gnu/libc.so  /lib/x86_64-linux-gnu/libc.so.6 /lib/i386-linux-gnu/libc.so.6
     2101838 lrwxrwxrwx 1 root root  12 Apr 16 16:14 /lib/i386-linux-gnu/libc.so.6 -> libc-2.27.so
     2101796 lrwxrwxrwx 1 root root  12 May 13 20:09 /lib/x86_64-linux-gnu/libc.so.6 -> libc-2.27.so
    15736469 -rw-r--r-- 1 root root 298 Apr 16 16:14 /usr/lib/x86_64-linux-gnu/libc.so
    

    What differences and relations are between the various libc.so?

    Which one is in use?

    Thanks.

    My question comes from https://stackoverflow.com/questions/50798907/why-does-loading-the-libc-shared-library-have-libraryloader-object-is-not-cal

    Thanks.

  • Tim
    Tim almost 6 years
    Thanks. (1) /usr/lib/x86_64-linux-gnu/libc.so is "linker script and dynamic loader", so what is diffrence and relation between /usr/lib/x86_64-linux-gnu/libc.so and ld.so? What does "linker script" mean? (2) why does whereis libc.so only provide information about /usr/lib/x86_64-linux-gnu/libc.so?
  • Stephen Kitt
    Stephen Kitt almost 6 years
    No, it’s “a linker script pointing to the 64-bit C library and dynamic loader”, the “and” links “C library” and “dynamic loader”.
  • Tim
    Tim almost 6 years
    Thanks. According to your last bullet, is /usr/lib/x86_64-linux-gnu/libc.so used for both static linking and dynamic loading, but not for dynamic linking?
  • Tim
    Tim almost 6 years
    Thanks. Is /usr/lib/x86_64-linux-gnu/libc.so used for static linking only? Then what did you mean by "dynamic loader" in the third bullet in your reply?
  • Stephen Kitt
    Stephen Kitt almost 6 years
    I’m terribly sorry, I misread your previous comment as referring to my previous comment rather than the end of my answer. I’ve updated my answer, hopefully it clarifies the differences between static linking, dynamic linking and dynamic loading.
  • Tim
    Tim almost 6 years
    Thanks. About ""dynamic linker” and “dynamic loader” are synonymous and used interchangeably", are dynamic linking and dynamic loading also synonymous? If I understand correctly, dlopen() invokes dynamic loader to dynamic load a shared library, without dynamic linker and dynamic linking involved. OTOH, dynamic linking requires dynamic loading happening beforehand?
  • Tim
    Tim almost 6 years
    By the way, if you have a chance, I'd appreciate if you could also take a look at stackoverflow.com/questions/50786247/…