Search code examples
linuxlinkerglibcstatic-linkingdynamic-linking

How statically linked binaries could be smaller than dynamically linked binaries?


If you read the description about stali, it mentions about statically linked binaries size:

It also targets binary size reduction through the avoidance of glibc and other bloated GNU libraries where possible (early experiments show that statically linked binaries are usually smaller than their dynamically linked glibc counterparts!!!).

I don't understand how including libraries in the binary itself will make the binary smaller than a binary with libraries included(Maybe there's something I'm missing regarding statically vs dynamically linked).

How's this possible? Does this only happen on some specific situations?


Solution

  • I don't understand how including libraries in the binary itself will make the binary smaller than a binary with libraries included

    There is certain overhead that is associated with dynamic linking: e.g. you need .dynsym, .dynstr, .got and .plt sections in order to import symbols from libc.so.6.

    However, unless the main executable is linked with -rdynamic, the sizes of these "overhead" sections are usually quite small, and so the claim that a fully-static binary is smaller appears quite dubious.