Search code examples
cgccstatic-linking

What is selective linking in GCC?


In this article, I found this line: The GNU linker uses selective linking, which keeps other unreferenced functions out of the linker’s output image.

I am not sure what this means exactly. But what I think is, if I include stdio.h in my source code, and use only printf from that, then the resulting exe contains only code for printf extracted from stdio.c and other functions defined in that file are discarded.

Is what I say is correct? If not, what does selective linking mean? Also, in the above case, does compiler include entire file, or only the used functions?


Solution

  • The GNU linker uses selective linking, which keeps other unreferenced functions out of the linker’s output image.

    That only applies when linking .a files. A .a file is a collection of .o files. That selective linking means it links in only a function rather than the entire .o where that function resides.

    if I include stdio.h in my source code, and use only printf from that, then the resulting exe contains only code for printf extracted from stdio.c and other functions defined in that file are discarded.

    C standard library functions normally reside in libc.so, unless you explicitly link statically. So, either you link libc.a and that copies printf function into your executable (selective linking), or you link libc.so and no copy of printf is made.

    stdio.c is only ever used to build libc.a and libc.so.


    As @JohannesSchaub-litb mentions in the comment:

    • -ffunction-sections is needed when compiling .o files to take advantage of selective linking linker feature:

    Together with a linker garbage collection (linker --gc-sections option) these options may lead to smaller statically-linked executables (after stripping).

    • --gc-sections linker option enables selective linking:

    --gc-sections decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced.