Making C Programs Small: The Beauty of C
Introduction
Let’s write a simple hello-world program in C language and compile a static binary with GCC:
|
|
|
|
Eight hundred forty-three kilobytes for such a simple program! What a waste of space in 2020, yeah? Nevertheless, in this post, we’ll try to make our programs smaller (maybe a lot).
Getting rid of libc
The fact is that by default, C compilers automatically link our code with GNU C Library and most of the binary size is taken by it, so our initial goal is going to be to make our program independent from that. To make it possible, we need to write an entry point to our program and wrappers for system calls in Assembler. Let’s do that (I’m using x86-64 instructions set):
Dumb calculator
For the purpose of showing that one can do something meaningful without exploiting C library, I shall implement a CLI program that takes +|-|*|/ as an operation from an argument and reduces the list from the stdin with it.
Write a Makefile:
Now let’s build and test it:
|
|
|
|
How small is it?
|
|
The size equals 3.5KB, which is approximately $99.6\%$ smaller than the initial hello-world program. Sounds fascinating 🎉. Of course, we become seriously limited by the inability to use functions from the standard library. However, the critical point is that we could get independent from runtime libraries and thus have almost zero initialization runtime, making C the only good choice for implementing system kernels and developing for cheap microcontrollers.
Source code of the example is available here.