Search code examples
clinuxgcccompilation

What does my gcc-compiled program depend on?


I'm trying to understand the dependencies of a C program, and I'm slowly realizing that gcc main.c -o main is hiding a lot of details.

Here's my experiment (ubuntu-wsl-x64): enter image description here

As you can see, my program is linked to

  • The GNU libc (through a symbolic link libc.so.6)
  • The GNU linker (ld-linux-x86-64.so.2)
  • A weird dynamic library (linux-vdso.so.1)

To my understanding, the linux-vdso.so.1 will implement certain libc API to improve the speed of certain system calls.

Questions:

  1. Why is linux-vdso.so.1 is not already included in libc?
  2. Why is ld dynamically linked to my executable? Is it used at runtime?
  3. To my understanding, my program also should depend on libgcc and crt0 are they linked statically?
  4. There must be a -I hidden so the program can see the libc header file. Am I correct? I so, where are they?
  5. Am I missing something else?

Thanks, kind strangers.


Solution

  • As you can see, my program is linked to

    • The GNU libc (through a symbolic link libc.so.6)

    Yes.

    • The GNU linker (ld-linux-x86-64.so.2)

    No. That is not the GNU linker (ld). It is the Linux dynamic linker. It serves as the standard ELF program loader on Linux. Every ELF executable needs such a loader in order to run.

    • A weird dynamic library (linux-vdso.so.1)

    Sort of. "vdso" is an initialism of "virtual dynamic shared object". This particular one is provided to every process by the Linux kernel, and the C standard library relies on it for various kernel services. It is not bundled into libc because for all intents and purposes, it is part of the kernel.

    1. To my understanding, my program also should depend on libgcc and crt0 are they linked statically?

    Some programs compiled with GCC depend on libgcc. Others don't. It depends on the program. libgcc could be linked statically -- GCC has a command-line option for that -- but if you didn't ask for that then it probably isn't.

    I don't know what crt0 is, maybe this: https://en.wikipedia.org/wiki/Crt0? As far as I know, that does not take the form of a separate shared library, so ldd would not tell you about it.

    There must be a -I hidden so the program can see the libc header file. Am I correct? I so, where are they?

    No, you are not correct. GCC has a default search path for include files that includes the headers for the standard library with which it is integrated. You use a -I option to add directories to that search path, but you don't need one for the standard library headers, or for others installed in that path (and what a pain it would be if you did!). Gcc will tell you what the default search path is if you ask it.

    1. Am I missing something else?

    Probably many things, given the nature of the specific questions posed. It's hard to say which of them may actually be of interest to you in this context, and likely impossible to fit them into the scope of an SO answer.