4 years, 9 months ago.

Huge binary size when building with ARM GCC Toolchain

I am trying out mbed OS with flavor of RTOS. Everything works well when using online compiler. Example programs compiles just fine and binary size is adequate.

I then decided to export the program using "GNU ARM Eclipse" as an export toolchain and import in Eclipse to work offline. I managed to build it successfully but I was unable to flash the binary onto MCU (STM32F103). I then observed that binary produced by online compiler was just 41KB in size while the binary produced by GNU toolchain is 80KB.

I used Nucleo-F103RB as a target as it is compatible with STM32F103.

Can anyone comment on why there is a huge size difference in binaries sizes? May be I need to add some flags in linker scripts or may be make some change in target device?

Any help will be much appreciated!

I figured out that the compiler that online Mbed Compiler use is ARMC6 while to work offline I was using GCC_ARM compiler with Eclipse as well as Mbed CLI.

All compilers compile code differently. I tried changing the optimization flags, debug flags etc. to produce the shortest code possible but none worked.

I then used ARMC6 compiler and binary size was same as the Mbed Online Compiler and it worked!

Next issue was that if I start more than 2 threads, my microcontroller chokes. I then found in the Mbed Thread documentation that by default threads stack is allocated in heap. To allocate it in stack, we need to initialize the thread like this:

Thread thread(osPriorityNormal, 3 * 1024); 3K Stack Size

Allocating the memory for thread statically, only the amount you need (and doing it carefully not to cause stack overflow), we can make optimal use of available memory to start more threads.

Hope this information is helpful for someone!

posted by Hamza Rizwan 15 Jul 2019

1 Answer

4 years, 9 months ago.

I figured out that the compiler that online Mbed Compiler use is ARMC6 while to work offline I was using GCC_ARM compiler with Eclipse as well as Mbed CLI.

All compilers compile code differently. I tried changing the optimization flags, debug flags etc. to produce the shortest code possible but none worked.

I then used ARMC6 compiler and binary size was same as the Mbed Online Compiler and it worked!

Next issue was that if I start more than 2 threads, my microcontroller chokes. I then found in the Mbed Thread documentation that by default threads stack is allocated in heap. To allocate it in stack, we need to initialize the thread like this:

Thread thread(osPriorityNormal, 3 * 1024); 3K Stack Size

Allocating the memory for thread statically, only the amount you need (and doing it carefully not to cause stack overflow), we can make optimal use of available memory to start more threads.

Hope this information is helpful for someone!

Accepted Answer

Assigned to Hamza Rizwan 4 years, 9 months ago.

This means that the question has been accepted and is being worked on.