Home > Blockchain >  Which GCC optimization flags affect binary size the most?
Which GCC optimization flags affect binary size the most?

Time:04-28

I am developing a C for ARM using GCC. I have ran into an issue where, I no optimizations are enabled, I am unable to create a binary (ELF) for my code because it will not fit in the available space. However, if I simply enable optimization for debugging (-Og), which is the lowest optimization available to my knowledge, the code easily fits.

In both cases, -ffunction-sections, -fdata-sections, -fno-excepctions, and -Wl,--gc-sections is enabled.

  • FLASH Size: 512kB
  • Without Optimizations: .text Overflows by ~200kB
  • With -Og Optimizations: .text is ~290kB

This is huge difference in binary size even with minimal optimizations.

I took a look here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for details on to what optimizations are being performed with the -Og flag to see if that would give me any insight.

My question is, what optimization flags affect binary size the most? Is there anything I should be looking for to explain this massive difference?

CodePudding user response:

Which GCC optimization flags affect binary size the most?

It will vary somewhat depending on the program itself. Most accurate way to find out how each flag affects your program is to try it out and compare the result with base-level.

A good choice of base-level for size optimisation is to use -Os which enables all optimisations of -O2 except those that are expected to potentially increase the binary size significantly which are (at the moment):

-falign-functions
-falign-jumps 
-falign-labels
-falign-loops 
-fprefetch-loop-arrays
-freorder-blocks-algorithm=stc

CodePudding user response:

Fast doesn't mean small. In fact, a large part of speed optimization revolves around loop unrolling, which increases code generation by a lot.

If you want to optimize for speed, use -Os, which is equivalent to -O2 except for all optimizations that increase size (again, like loop unrolling).

  • Related