3
votes

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?

2

2 Answers

8
votes

If you use static linking, the linker can throw out symbols that are not used.

For instance, your library has both foo and bar, but the executable only uses bar, then foo will not be part of the executable.

In case of dynamic linking that is not possible, because the linker/compiler cannot know what will be used when building the library.

Aside from that, dynamic linking is a lovely source for errors (like segfaulting because the newer library is incompatible) that can be avoided by linking statically.

Further reading: http://harmful.cat-v.org/software/dynamic-linking/

1
votes

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.