ELF executable can be opimized for size by removing unused or dead functions. The options -ffunction-sections -Wl,–gc-sections can be used to do that.

Let us practically verify it using the following C file which has a function named dead_one which is not used anywhere in the code.

bash# cat test.c

#include <stdio.h>

int dead_one()
{
	int i;
	i = 10*30*40;
	return i;
}

int main(int argc, char *argv[])
{
	printf("Wake up, Neo\n");
}

Now let us compile the above C file with and without -ffunction-sections -Wl,–gc-sections options. Let us produce binary named test.1 without the options and a binary named test.2 with the options.

bash# cc test.c -o test.1
bash# cc test.c -o test.2 -ffunction-sections -Wl,--gc-sections
bash# ls -l
-rwxrwxr-x 1 neo neo 4715 Dec 15 18:06 test.1
-rwxrwxr-x 1 neo neo 4478 Dec 15 18:06 test.2

Now let us examine the both binaries for the symbol dead_one.

bash# nm test.1 | grep dead
0804837c T dead_one
bash# nm test.2 | grep dead

You can observe the ELF binary test.2 which was compiled with the options doesn’t include function dead_one.