Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which GCC optimization flags affect binary size the most?

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-exceptions, and -Wl,--gc-sections is enabled.

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

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

I took a look at 3.11 Options That Control Optimization for details on to what optimizations are being performed with the -Og flag to see if that would give me any insight.

What optimization flags affect binary size the most? Is there anything I should be looking for to explain this massive difference?

like image 317
Patrick Wright Avatar asked Sep 03 '25 09:09

Patrick Wright


1 Answers

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
like image 198
eerorika Avatar answered Sep 04 '25 23:09

eerorika