Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 973
Farid Nouri Neshat Avatar asked Nov 15 '25 18:11

Farid Nouri Neshat


2 Answers

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/

like image 59
ForestPhoenix Avatar answered Nov 17 '25 08:11

ForestPhoenix


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.

like image 35
Employed Russian Avatar answered Nov 17 '25 08:11

Employed Russian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!